001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.DoNotRetryIOException;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.PrivateCellUtil;
026import org.apache.hadoop.hbase.client.Scan;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * ReversibleRegionScannerImpl extends from RegionScannerImpl, and is used to support reversed
032 * scanning.
033 */
034@InterfaceAudience.Private
035class ReversedRegionScannerImpl extends RegionScannerImpl {
036
037  ReversedRegionScannerImpl(Scan scan, List<KeyValueScanner> additionalScanners, HRegion region,
038    long nonceGroup, long nonce) throws IOException {
039    super(scan, additionalScanners, region, nonceGroup, nonce);
040  }
041
042  @Override
043  protected void initializeKVHeap(List<KeyValueScanner> scanners,
044    List<KeyValueScanner> joinedScanners, HRegion region) throws IOException {
045    this.storeHeap = new ReversedKeyValueHeap(scanners, comparator);
046    if (!joinedScanners.isEmpty()) {
047      throw new DoNotRetryIOException("Reverse scan with loading CFs on demand is not supported");
048    }
049  }
050
051  @Override
052  protected boolean shouldStop(Cell currentRowCell) {
053    if (currentRowCell == null) {
054      return true;
055    }
056    if (stopRow == null || Bytes.equals(stopRow, HConstants.EMPTY_START_ROW)) {
057      return false;
058    }
059    int c = comparator.compareRows(currentRowCell, stopRow, 0, stopRow.length);
060    return c < 0 || (c == 0 && !includeStopRow);
061  }
062
063  @Override
064  protected boolean nextRow(ScannerContext scannerContext, Cell curRowCell) throws IOException {
065    assert super.joinedContinuationRow == null : "Trying to go to next row during joinedHeap read.";
066    this.storeHeap.seekToPreviousRow(PrivateCellUtil.createFirstOnRow(curRowCell));
067    resetFilters();
068    // Calling the hook in CP which allows it to do a fast forward
069    if (this.region.getCoprocessorHost() != null) {
070      return this.region.getCoprocessorHost().postScannerFilterRow(this, curRowCell);
071    }
072    return true;
073  }
074
075}