1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.CellUtil;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
34 import org.apache.hadoop.hbase.regionserver.HRegion;
35 import org.apache.hadoop.hbase.regionserver.RegionScanner;
36 import org.mortbay.log.Log;
37
38
39
40
41
42 @InterfaceAudience.Private
43 public class ClientSideRegionScanner extends AbstractClientScanner {
44
45 private HRegion region;
46 RegionScanner scanner;
47 List<Cell> values;
48
49 public ClientSideRegionScanner(Configuration conf, FileSystem fs,
50 Path rootDir, HTableDescriptor htd, HRegionInfo hri, Scan scan, ScanMetrics scanMetrics)
51 throws IOException {
52
53
54 scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
55
56
57 this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
58
59
60 this.scanner = region.getScanner(scan);
61 values = new ArrayList<Cell>();
62
63 if (scanMetrics == null) {
64 initScanMetrics(scan);
65 } else {
66 this.scanMetrics = scanMetrics;
67 }
68 region.startRegionOperation();
69 }
70
71 @Override
72 public Result next() throws IOException {
73 values.clear();
74 scanner.nextRaw(values);
75 if (values.isEmpty()) {
76
77 return null;
78 }
79
80 Result result = Result.create(values);
81 if (this.scanMetrics != null) {
82 long resultSize = 0;
83 for (Cell cell : values) {
84 resultSize += CellUtil.estimatedSerializedSizeOf(cell);
85 }
86 this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
87 }
88
89 return result;
90 }
91
92 @Override
93 public void close() {
94 if (this.scanner != null) {
95 try {
96 this.scanner.close();
97 this.scanner = null;
98 } catch (IOException ex) {
99 Log.warn("Exception while closing scanner", ex);
100 }
101 }
102 if (this.region != null) {
103 try {
104 this.region.closeRegionOperation();
105 this.region.close(true);
106 this.region = null;
107 } catch (IOException ex) {
108 Log.warn("Exception while closing region", ex);
109 }
110 }
111 }
112
113 @Override
114 public boolean renewLease() {
115 throw new UnsupportedOperationException();
116 }
117 }