View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
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.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.HTableDescriptor;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
36  import org.apache.hadoop.hbase.regionserver.HRegion;
37  import org.apache.hadoop.hbase.regionserver.RegionScanner;
38  
39  /**
40   * A client scanner for a region opened for read-only on the client side. Assumes region data
41   * is not changing.
42   */
43  @InterfaceAudience.Private
44  public class ClientSideRegionScanner extends AbstractClientScanner {
45  
46    private static final Log LOG = LogFactory.getLog(ClientSideRegionScanner.class);
47  
48    private HRegion region;
49    RegionScanner scanner;
50    List<Cell> values;
51  
52    public ClientSideRegionScanner(Configuration conf, FileSystem fs,
53        Path rootDir, HTableDescriptor htd, HRegionInfo hri, Scan scan, ScanMetrics scanMetrics)
54            throws IOException {
55  
56      // region is immutable, set isolation level
57      scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
58  
59      // open region from the snapshot directory
60      this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
61  
62      // create an internal region scanner
63      this.scanner = region.getScanner(scan);
64      values = new ArrayList<Cell>();
65  
66      if (scanMetrics == null) {
67        initScanMetrics(scan);
68      } else {
69        this.scanMetrics = scanMetrics;
70      }
71      region.startRegionOperation();
72    }
73  
74    @Override
75    public Result next() throws IOException {
76      values.clear();
77      scanner.nextRaw(values);
78      if (values.isEmpty()) {
79        //we are done
80        return null;
81      }
82  
83      Result result = Result.create(values);
84      if (this.scanMetrics != null) {
85        long resultSize = 0;
86        for (Cell cell : values) {
87          resultSize += CellUtil.estimatedSerializedSizeOf(cell);
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 }