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.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.hbase.KeyValueUtil;
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 private Scan scan;
47 RegionScanner scanner;
48 List<Cell> values;
49
50 public ClientSideRegionScanner(Configuration conf, FileSystem fs,
51 Path rootDir, HTableDescriptor htd, HRegionInfo hri, Scan scan, ScanMetrics scanMetrics) throws IOException {
52
53 this.scan = scan;
54
55
56 scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
57
58
59 this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
60
61
62 this.scanner = region.getScanner(scan);
63 values = new ArrayList<Cell>();
64
65 if (scanMetrics == null) {
66 initScanMetrics(scan);
67 } else {
68 this.scanMetrics = scanMetrics;
69 }
70 region.startRegionOperation();
71 }
72
73 @Override
74 public Result next() throws IOException {
75 values.clear();
76 scanner.nextRaw(values);
77 if (values.isEmpty()) {
78
79 return null;
80 }
81
82 Result result = Result.create(values);
83 if (this.scanMetrics != null) {
84 long resultSize = 0;
85 for (Cell kv : values) {
86
87 resultSize += KeyValueUtil.ensureKeyValue(kv).getLength();
88 }
89 this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
90 }
91
92 return result;
93 }
94
95 @Override
96 public void close() {
97 if (this.scanner != null) {
98 try {
99 this.scanner.close();
100 this.scanner = null;
101 } catch (IOException ex) {
102 Log.warn("Exception while closing scanner", ex);
103 }
104 }
105 if (this.region != null) {
106 try {
107 this.region.closeRegionOperation();
108 this.region.close(true);
109 this.region = null;
110 } catch (IOException ex) {
111 Log.warn("Exception while closing region", ex);
112 }
113 }
114 }
115
116 @Override
117 public boolean renewLease() {
118 throw new UnsupportedOperationException();
119 }
120 }