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      // region is immutable, set isolation level
56      scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
57  
58      htd.setReadOnly(true);
59  
60      // open region from the snapshot directory
61      this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
62  
63      // create an internal region scanner
64      this.scanner = region.getScanner(scan);
65      values = new ArrayList<Cell>();
66  
67      if (scanMetrics == null) {
68        initScanMetrics(scan);
69      } else {
70        this.scanMetrics = scanMetrics;
71      }
72      region.startRegionOperation();
73    }
74  
75    @Override
76    public Result next() throws IOException {
77      values.clear();
78      scanner.nextRaw(values);
79      if (values.isEmpty()) {
80        //we are done
81        return null;
82      }
83  
84      Result result = Result.create(values);
85      if (this.scanMetrics != null) {
86        long resultSize = 0;
87        for (Cell cell : values) {
88          resultSize += CellUtil.estimatedSerializedSizeOf(cell);
89        }
90        this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
91        this.scanMetrics.countOfRowsScanned.incrementAndGet();
92      }
93  
94      return result;
95    }
96  
97    @Override
98    public void close() {
99      if (this.scanner != null) {
100       try {
101         this.scanner.close();
102         this.scanner = null;
103       } catch (IOException ex) {
104         LOG.warn("Exception while closing scanner", ex);
105       }
106     }
107     if (this.region != null) {
108       try {
109         this.region.closeRegionOperation();
110         this.region.close(true);
111         this.region = null;
112       } catch (IOException ex) {
113         LOG.warn("Exception while closing region", ex);
114       }
115     }
116   }
117 
118   @Override
119   public boolean renewLease() {
120     throw new UnsupportedOperationException();
121   }
122 }