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.hadoop.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   * A client scanner for a region opened for read-only on the client side. Assumes region data
40   * is not changing.
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) 
52            throws IOException {
53  
54      this.scan = scan;
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  
78      scanner.nextRaw(values, -1); // pass -1 as limit so that we see the whole row.
79      if (values == null || 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 kv : values) {
88          // TODO add getLength to Cell/use CellUtil#estimatedSizeOf
89          resultSize += KeyValueUtil.ensureKeyValue(kv).getLength();
90        }
91        this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
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 }