1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.regionserver;
20
21 import java.io.Closeable;
22 import java.io.IOException;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.Cell;
26 import org.apache.hadoop.hbase.KeyValue;
27 import org.apache.hadoop.hbase.client.Scan;
28
29 /**
30 * Scanner that returns the next KeyValue.
31 */
32 @InterfaceAudience.Private
33 // TODO: Change name from KeyValueScanner to CellScanner only we already have a simple CellScanner
34 // so this should be something else altogether, a decoration on our base CellScanner. TODO.
35 // This class shows in CPs so do it all in one swell swoop. HBase-2.0.0.
36 public interface KeyValueScanner {
37 /**
38 * The byte array represents for NO_NEXT_INDEXED_KEY;
39 * The actual value is irrelevant because this is always compared by reference.
40 */
41 public static final Cell NO_NEXT_INDEXED_KEY = new KeyValue();
42
43 /**
44 * Look at the next Cell in this scanner, but do not iterate scanner.
45 * @return the next Cell
46 */
47 Cell peek();
48
49 /**
50 * Return the next Cell in this scanner, iterating the scanner
51 * @return the next Cell
52 */
53 Cell next() throws IOException;
54
55 /**
56 * Seek the scanner at or after the specified KeyValue.
57 * @param key seek value
58 * @return true if scanner has values left, false if end of scanner
59 */
60 boolean seek(Cell key) throws IOException;
61
62 /**
63 * Reseek the scanner at or after the specified KeyValue.
64 * This method is guaranteed to seek at or after the required key only if the
65 * key comes after the current position of the scanner. Should not be used
66 * to seek to a key which may come before the current position.
67 * @param key seek value (should be non-null)
68 * @return true if scanner has values left, false if end of scanner
69 */
70 boolean reseek(Cell key) throws IOException;
71
72 /**
73 * Get the sequence id associated with this KeyValueScanner. This is required
74 * for comparing multiple files to find out which one has the latest data.
75 * The default implementation for this would be to return 0. A file having
76 * lower sequence id will be considered to be the older one.
77 */
78 // TODO: Implement SequenceId Interface instead.
79 long getSequenceID();
80
81 /**
82 * Close the KeyValue scanner.
83 */
84 void close();
85
86 /**
87 * Allows to filter out scanners (both StoreFile and memstore) that we don't
88 * want to use based on criteria such as Bloom filters and timestamp ranges.
89 * @param scan the scan that we are selecting scanners for
90 * @param store the store we are performing the scan on.
91 * @param oldestUnexpiredTS the oldest timestamp we are interested in for
92 * this query, based on TTL
93 * @return true if the scanner should be included in the query
94 */
95 boolean shouldUseScanner(Scan scan, Store store, long oldestUnexpiredTS);
96
97 // "Lazy scanner" optimizations
98
99 /**
100 * Similar to {@link #seek} (or {@link #reseek} if forward is true) but only
101 * does a seek operation after checking that it is really necessary for the
102 * row/column combination specified by the kv parameter. This function was
103 * added to avoid unnecessary disk seeks by checking row-column Bloom filters
104 * before a seek on multi-column get/scan queries, and to optimize by looking
105 * up more recent files first.
106 * @param forward do a forward-only "reseek" instead of a random-access seek
107 * @param useBloom whether to enable multi-column Bloom filter optimization
108 */
109 boolean requestSeek(Cell kv, boolean forward, boolean useBloom)
110 throws IOException;
111
112 /**
113 * We optimize our store scanners by checking the most recent store file
114 * first, so we sometimes pretend we have done a seek but delay it until the
115 * store scanner bubbles up to the top of the key-value heap. This method is
116 * then used to ensure the top store file scanner has done a seek operation.
117 */
118 boolean realSeekDone();
119
120 /**
121 * Does the real seek operation in case it was skipped by
122 * seekToRowCol(KeyValue, boolean) (TODO: Whats this?). Note that this function should
123 * be never called on scanners that always do real seek operations (i.e. most
124 * of the scanners). The easiest way to achieve this is to call
125 * {@link #realSeekDone()} first.
126 */
127 void enforceSeek() throws IOException;
128
129 /**
130 * @return true if this is a file scanner. Otherwise a memory scanner is
131 * assumed.
132 */
133 boolean isFileScanner();
134
135 // Support for "Reversed Scanner"
136 /**
137 * Seek the scanner at or before the row of specified Cell, it firstly
138 * tries to seek the scanner at or after the specified Cell, return if
139 * peek KeyValue of scanner has the same row with specified Cell,
140 * otherwise seek the scanner at the first Cell of the row which is the
141 * previous row of specified KeyValue
142 *
143 * @param key seek KeyValue
144 * @return true if the scanner is at the valid KeyValue, false if such
145 * KeyValue does not exist
146 *
147 */
148 public boolean backwardSeek(Cell key) throws IOException;
149
150 /**
151 * Seek the scanner at the first Cell of the row which is the previous row
152 * of specified key
153 * @param key seek value
154 * @return true if the scanner at the first valid Cell of previous row,
155 * false if not existing such Cell
156 */
157 public boolean seekToPreviousRow(Cell key) throws IOException;
158
159 /**
160 * Seek the scanner at the first KeyValue of last row
161 *
162 * @return true if scanner has values left, false if the underlying data is
163 * empty
164 * @throws IOException
165 */
166 public boolean seekToLastRow() throws IOException;
167
168 /**
169 * @return the next key in the index, usually the first key of next block OR a key that falls
170 * between last key of current block and first key of next block..
171 * see HFileWriterImpl#getMidpoint, or null if not known.
172 */
173 public Cell getNextIndexedKey();
174 }