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 void resetKVHeap(List<? extends KeyValueScanner> scanners, 063 CellComparator comparator) throws IOException { 064 // Combine all seeked scanners with a heap 065 heap = new ReversedKeyValueHeap(scanners, comparator); 066 } 067 068 @Override 069 protected void seekScanners(List<? extends KeyValueScanner> scanners, 070 Cell seekKey, boolean isLazy, boolean isParallelSeek) 071 throws IOException { 072 // Seek all scanners to the start of the Row (or if the exact matching row 073 // key does not exist, then to the start of the previous matching Row). 074 if (CellUtil.matchingRows(seekKey, HConstants.EMPTY_START_ROW)) { 075 for (KeyValueScanner scanner : scanners) { 076 scanner.seekToLastRow(); 077 } 078 } else { 079 for (KeyValueScanner scanner : scanners) { 080 scanner.backwardSeek(seekKey); 081 } 082 } 083 } 084 085 @Override 086 protected boolean seekToNextRow(Cell kv) throws IOException { 087 return seekToPreviousRow(kv); 088 } 089 090 /** 091 * Do a backwardSeek in a reversed StoreScanner(scan backward) 092 */ 093 @Override 094 protected boolean seekAsDirection(Cell kv) throws IOException { 095 return backwardSeek(kv); 096 } 097 098 @Override 099 protected void checkScanOrder(Cell prevKV, Cell kv, 100 CellComparator comparator) throws IOException { 101 // Check that the heap gives us KVs in an increasing order for same row and 102 // decreasing order for different rows. 103 assert prevKV == null || comparator == null || comparator.compareRows(kv, prevKV) < 0 104 || (CellUtil.matchingRows(kv, prevKV) && comparator.compare(kv, 105 prevKV) >= 0) : "Key " + prevKV 106 + " followed by a " + "error order key " + kv + " in cf " + store 107 + " in reversed scan"; 108 } 109 110 @Override 111 public boolean reseek(Cell kv) throws IOException { 112 throw new IllegalStateException( 113 "reseek cannot be called on ReversedStoreScanner"); 114 } 115 116 @Override 117 public boolean seek(Cell key) throws IOException { 118 throw new IllegalStateException( 119 "seek cannot be called on ReversedStoreScanner"); 120 } 121 122 @Override 123 public boolean seekToPreviousRow(Cell key) throws IOException { 124 if (checkFlushed()) { 125 reopenAfterFlush(); 126 } 127 return this.heap.seekToPreviousRow(key); 128 } 129 130 @Override 131 public boolean backwardSeek(Cell key) throws IOException { 132 if (checkFlushed()) { 133 reopenAfterFlush(); 134 } 135 return this.heap.backwardSeek(key); 136 } 137}