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