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 java.util.NavigableSet;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellComparator;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.client.Scan;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * ReversedStoreScanner extends from StoreScanner, and is used to support reversed scanning.
032 */
033@InterfaceAudience.Private
034public class ReversedStoreScanner extends StoreScanner implements KeyValueScanner {
035
036  /**
037   * Opens a scanner across memstore, snapshot, and all StoreFiles. Assumes we are not in a
038   * compaction.
039   * @param store   who we scan
040   * @param scan    the spec
041   * @param columns which columns we are scanning
042   */
043  public ReversedStoreScanner(HStore store, ScanInfo scanInfo, Scan scan,
044    NavigableSet<byte[]> columns, long readPt) throws IOException {
045    super(store, scanInfo, scan, columns, readPt);
046  }
047
048  /** Constructor for testing. */
049  public ReversedStoreScanner(Scan scan, ScanInfo scanInfo, NavigableSet<byte[]> columns,
050    List<? extends KeyValueScanner> scanners) throws IOException {
051    super(scan, scanInfo, columns, scanners);
052  }
053
054  @Override
055  protected KeyValueHeap newKVHeap(List<? extends KeyValueScanner> scanners,
056    CellComparator comparator) throws IOException {
057    return new ReversedKeyValueHeap(scanners, comparator);
058  }
059
060  @Override
061  protected void seekScanners(List<? extends KeyValueScanner> scanners, Cell seekKey,
062    boolean isLazy, boolean isParallelSeek) throws IOException {
063    // Seek all scanners to the start of the Row (or if the exact matching row
064    // key does not exist, then to the start of the previous matching Row).
065    if (CellUtil.matchingRows(seekKey, HConstants.EMPTY_START_ROW)) {
066      for (KeyValueScanner scanner : scanners) {
067        scanner.seekToLastRow();
068      }
069    } else {
070      for (KeyValueScanner scanner : scanners) {
071        scanner.backwardSeek(seekKey);
072      }
073    }
074  }
075
076  @Override
077  protected boolean seekToNextRow(Cell kv) throws IOException {
078    return seekToPreviousRow(kv);
079  }
080
081  /**
082   * Do a backwardSeek in a reversed StoreScanner(scan backward)
083   */
084  @Override
085  protected boolean seekAsDirection(Cell kv) throws IOException {
086    return backwardSeek(kv);
087  }
088
089  @Override
090  protected void checkScanOrder(Cell prevKV, Cell kv, CellComparator comparator)
091    throws IOException {
092    // Check that the heap gives us KVs in an increasing order for same row and
093    // decreasing order for different rows.
094    assert prevKV == null || comparator == null || comparator.compareRows(kv, prevKV) < 0
095      || (CellUtil.matchingRows(kv, prevKV) && comparator.compare(kv, prevKV) >= 0)
096      : "Key " + prevKV + " followed by a " + "error order key " + kv + " in cf " + store
097        + " in reversed scan";
098  }
099
100  @Override
101  public boolean reseek(Cell kv) throws IOException {
102    throw new IllegalStateException("reseek cannot be called on ReversedStoreScanner");
103  }
104
105  @Override
106  public boolean seek(Cell key) throws IOException {
107    throw new IllegalStateException("seek cannot be called on ReversedStoreScanner");
108  }
109
110  @Override
111  public boolean seekToPreviousRow(Cell key) throws IOException {
112    if (checkFlushed()) {
113      reopenAfterFlush();
114    }
115    return this.heap.seekToPreviousRow(key);
116  }
117
118  @Override
119  public boolean backwardSeek(Cell key) throws IOException {
120    if (checkFlushed()) {
121      reopenAfterFlush();
122    }
123    return this.heap.backwardSeek(key);
124  }
125}