001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with this
006 * work for additional information regarding copyright ownership. The ASF
007 * licenses this file to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016 * License for the specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import java.io.IOException;
022import java.util.List;
023import java.util.NavigableSet;
024
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellComparator;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.yetus.audience.InterfaceAudience;
031
032/**
033 * ReversedStoreScanner extends from StoreScanner, and is used to support
034 * reversed scanning.
035 */
036@InterfaceAudience.Private
037public class ReversedStoreScanner extends StoreScanner implements KeyValueScanner {
038
039  /**
040   * Opens a scanner across memstore, snapshot, and all StoreFiles. Assumes we
041   * are not in a compaction.
042   * 
043   * @param store who we scan
044   * @param scanInfo
045   * @param scan the spec
046   * @param columns which columns we are scanning
047   * @throws IOException
048   */
049  public ReversedStoreScanner(HStore store, ScanInfo scanInfo, Scan scan,
050      NavigableSet<byte[]> columns, long readPt)
051      throws IOException {
052    super(store, scanInfo, scan, columns, readPt);
053  }
054
055  /** Constructor for testing. */
056  public ReversedStoreScanner(Scan scan, ScanInfo scanInfo, NavigableSet<byte[]> columns,
057      List<? extends KeyValueScanner> scanners) throws IOException {
058    super(scan, scanInfo, columns, scanners);
059  }
060
061  @Override
062  protected KeyValueHeap newKVHeap(List<? extends KeyValueScanner> scanners,
063      CellComparator comparator) throws IOException {
064    return new ReversedKeyValueHeap(scanners, comparator);
065  }
066
067  @Override
068  protected void seekScanners(List<? extends KeyValueScanner> scanners,
069      Cell seekKey, boolean isLazy, boolean isParallelSeek)
070      throws IOException {
071    // Seek all scanners to the start of the Row (or if the exact matching row
072    // key does not exist, then to the start of the previous matching Row).
073    if (CellUtil.matchingRows(seekKey, HConstants.EMPTY_START_ROW)) {
074      for (KeyValueScanner scanner : scanners) {
075        scanner.seekToLastRow();
076      }
077    } else {
078      for (KeyValueScanner scanner : scanners) {
079        scanner.backwardSeek(seekKey);
080      }
081    }
082  }
083
084  @Override
085  protected boolean seekToNextRow(Cell kv) throws IOException {
086    return seekToPreviousRow(kv);
087  }
088
089  /**
090   * Do a backwardSeek in a reversed StoreScanner(scan backward)
091   */
092  @Override
093  protected boolean seekAsDirection(Cell kv) throws IOException {
094    return backwardSeek(kv);
095  }
096
097  @Override
098  protected void checkScanOrder(Cell prevKV, Cell kv,
099      CellComparator comparator) throws IOException {
100    // Check that the heap gives us KVs in an increasing order for same row and
101    // decreasing order for different rows.
102    assert prevKV == null || comparator == null || comparator.compareRows(kv, prevKV) < 0
103        || (CellUtil.matchingRows(kv, prevKV) && comparator.compare(kv,
104            prevKV) >= 0) : "Key " + prevKV
105        + " followed by a " + "error order key " + kv + " in cf " + store
106        + " in reversed scan";
107  }
108
109  @Override
110  public boolean reseek(Cell kv) throws IOException {
111    throw new IllegalStateException(
112        "reseek cannot be called on ReversedStoreScanner");
113  }
114
115  @Override
116  public boolean seek(Cell key) throws IOException {
117    throw new IllegalStateException(
118        "seek cannot be called on ReversedStoreScanner");
119  }
120
121  @Override
122  public boolean seekToPreviousRow(Cell key) throws IOException {
123    if (checkFlushed()) {
124      reopenAfterFlush();
125    }
126    return this.heap.seekToPreviousRow(key);
127  }
128
129  @Override
130  public boolean backwardSeek(Cell key) throws IOException {
131    if (checkFlushed()) {
132      reopenAfterFlush();
133    }
134    return this.heap.backwardSeek(key);
135  }
136}