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