HBASE-18503 Change ***Util and Master to use TableDescriptor and ColumnFamilyDescriptor
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / client / ClientSideRegionScanner.java
blob1d0d57bb650ac7beaa5bb21fb22823969899dae2
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.apache.hadoop.hbase.client;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.Cell;
31 import org.apache.hadoop.hbase.CellUtil;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.classification.InterfaceAudience;
34 import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
35 import org.apache.hadoop.hbase.regionserver.HRegion;
36 import org.apache.hadoop.hbase.regionserver.RegionScanner;
38 /**
39 * A client scanner for a region opened for read-only on the client side. Assumes region data
40 * is not changing.
42 @InterfaceAudience.Private
43 public class ClientSideRegionScanner extends AbstractClientScanner {
45 private static final Log LOG = LogFactory.getLog(ClientSideRegionScanner.class);
47 private HRegion region;
48 RegionScanner scanner;
49 List<Cell> values;
51 public ClientSideRegionScanner(Configuration conf, FileSystem fs,
52 Path rootDir, TableDescriptor htd, HRegionInfo hri, Scan scan, ScanMetrics scanMetrics)
53 throws IOException {
54 // region is immutable, set isolation level
55 scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
57 // open region from the snapshot directory
58 this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
60 // create an internal region scanner
61 this.scanner = region.getScanner(scan);
62 values = new ArrayList<>();
64 if (scanMetrics == null) {
65 initScanMetrics(scan);
66 } else {
67 this.scanMetrics = scanMetrics;
69 region.startRegionOperation();
72 @Override
73 public Result next() throws IOException {
74 values.clear();
75 scanner.nextRaw(values);
76 if (values.isEmpty()) {
77 //we are done
78 return null;
81 Result result = Result.create(values);
82 if (this.scanMetrics != null) {
83 long resultSize = 0;
84 for (Cell cell : values) {
85 resultSize += CellUtil.estimatedSerializedSizeOf(cell);
87 this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
90 return result;
93 @Override
94 public void close() {
95 if (this.scanner != null) {
96 try {
97 this.scanner.close();
98 this.scanner = null;
99 } catch (IOException ex) {
100 LOG.warn("Exception while closing scanner", ex);
103 if (this.region != null) {
104 try {
105 this.region.closeRegionOperation();
106 this.region.close(true);
107 this.region = null;
108 } catch (IOException ex) {
109 LOG.warn("Exception while closing region", ex);
114 @Override
115 public boolean renewLease() {
116 throw new UnsupportedOperationException();