View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.KeyValueUtil;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.client.Scan;
29  import org.apache.hadoop.hbase.regionserver.HRegion.RegionScannerImpl;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * ReversibleRegionScannerImpl extends from RegionScannerImpl, and is used to
34   * support reversed scanning.
35   */
36  @InterfaceAudience.Private
37  class ReversedRegionScannerImpl extends RegionScannerImpl {
38  
39    /**
40     * @param scan
41     * @param additionalScanners
42     * @param region
43     * @throws IOException
44     */
45    ReversedRegionScannerImpl(Scan scan,
46        List<KeyValueScanner> additionalScanners, HRegion region)
47        throws IOException {
48      region.super(scan, additionalScanners, region);
49    }
50  
51    @Override
52    protected void initializeKVHeap(List<KeyValueScanner> scanners,
53        List<KeyValueScanner> joinedScanners, HRegion region) throws IOException {
54      this.storeHeap = new ReversedKeyValueHeap(scanners, region.getComparator());
55      if (!joinedScanners.isEmpty()) {
56        this.joinedHeap = new ReversedKeyValueHeap(joinedScanners,
57            region.getComparator());
58      }
59    }
60  
61    protected boolean shouldStop(Cell currentRowCell) {
62      if (currentRowCell == null) {
63        return true;
64      }
65      if (stopRow == null || Bytes.equals(stopRow, HConstants.EMPTY_START_ROW)) {
66        return false;
67      }
68      int c = region.getComparator().compareRows(currentRowCell, stopRow, 0, stopRow.length);
69      return c < 0 || (c == 0 && !includeStopRow);
70    }
71  
72    @Override
73    protected boolean nextRow(ScannerContext scannerContext, byte[] currentRow, int offset,
74        short length) throws IOException {
75      assert super.joinedContinuationRow == null : "Trying to go to next row during joinedHeap read.";
76      byte row[] = new byte[length];
77      System.arraycopy(currentRow, offset, row, 0, length);
78      this.storeHeap.seekToPreviousRow(KeyValueUtil.createFirstOnRow(row));
79      resetFilters();
80  
81      // Calling the hook in CP which allows it to do a fast forward
82      if (this.region.getCoprocessorHost() != null) {
83        return this.region.getCoprocessorHost().postScannerFilterRow(this,
84            currentRow, offset, length);
85      }
86      return true;
87    }
88  
89  }