1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver;
21
22 import java.io.IOException;
23 import java.util.Comparator;
24 import java.util.List;
25 import java.util.PriorityQueue;
26
27 import org.apache.hadoop.hbase.Cell;
28 import org.apache.hadoop.hbase.KeyValue.KVComparator;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.regionserver.ScannerContext.NextState;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 @InterfaceAudience.Private
45 public class KeyValueHeap extends NonReversedNonLazyKeyValueScanner
46 implements KeyValueScanner, InternalScanner {
47 protected PriorityQueue<KeyValueScanner> heap = null;
48
49
50
51
52
53
54
55
56
57
58
59 protected KeyValueScanner current = null;
60
61 protected KVScannerComparator comparator;
62
63
64
65
66
67
68
69 public KeyValueHeap(List<? extends KeyValueScanner> scanners,
70 KVComparator comparator) throws IOException {
71 this(scanners, new KVScannerComparator(comparator));
72 }
73
74
75
76
77
78
79
80 KeyValueHeap(List<? extends KeyValueScanner> scanners,
81 KVScannerComparator comparator) throws IOException {
82 this.comparator = comparator;
83 if (!scanners.isEmpty()) {
84 this.heap = new PriorityQueue<KeyValueScanner>(scanners.size(),
85 this.comparator);
86 for (KeyValueScanner scanner : scanners) {
87 if (scanner.peek() != null) {
88 this.heap.add(scanner);
89 } else {
90 scanner.close();
91 }
92 }
93 this.current = pollRealKV();
94 }
95 }
96
97 public Cell peek() {
98 if (this.current == null) {
99 return null;
100 }
101 return this.current.peek();
102 }
103
104 public Cell next() throws IOException {
105 if(this.current == null) {
106 return null;
107 }
108 Cell kvReturn = this.current.next();
109 Cell kvNext = this.current.peek();
110 if (kvNext == null) {
111 this.current.close();
112 this.current = null;
113 this.current = pollRealKV();
114 } else {
115 KeyValueScanner topScanner = this.heap.peek();
116
117 if (topScanner != null && this.comparator.compare(kvNext, topScanner.peek()) >= 0) {
118 this.heap.add(this.current);
119 this.current = null;
120 this.current = pollRealKV();
121 }
122 }
123 return kvReturn;
124 }
125
126
127
128
129
130
131
132
133
134
135
136 @Override
137 public boolean next(List<Cell> result) throws IOException {
138 return next(result, NoLimitScannerContext.getInstance());
139 }
140
141 @Override
142 public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOException {
143 if (this.current == null) {
144 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
145 }
146 InternalScanner currentAsInternal = (InternalScanner)this.current;
147 boolean moreCells = currentAsInternal.next(result, scannerContext);
148 Cell pee = this.current.peek();
149
150
151
152
153
154
155
156
157
158 if (pee == null || !moreCells) {
159 this.current.close();
160 } else {
161 this.heap.add(this.current);
162 }
163 this.current = null;
164 this.current = pollRealKV();
165 if (this.current == null) {
166 moreCells = scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
167 }
168 return moreCells;
169 }
170
171 protected static class KVScannerComparator implements Comparator<KeyValueScanner> {
172 protected KVComparator kvComparator;
173
174
175
176
177 public KVScannerComparator(KVComparator kvComparator) {
178 this.kvComparator = kvComparator;
179 }
180 public int compare(KeyValueScanner left, KeyValueScanner right) {
181 int comparison = compare(left.peek(), right.peek());
182 if (comparison != 0) {
183 return comparison;
184 } else {
185
186
187 long leftSequenceID = left.getSequenceID();
188 long rightSequenceID = right.getSequenceID();
189 if (leftSequenceID > rightSequenceID) {
190 return -1;
191 } else if (leftSequenceID < rightSequenceID) {
192 return 1;
193 } else {
194 return 0;
195 }
196 }
197 }
198
199
200
201
202
203
204 public int compare(Cell left, Cell right) {
205 return this.kvComparator.compare(left, right);
206 }
207
208
209
210 public KVComparator getComparator() {
211 return this.kvComparator;
212 }
213 }
214
215 public void close() {
216 if (this.current != null) {
217 this.current.close();
218 }
219 if (this.heap != null) {
220 KeyValueScanner scanner;
221 while ((scanner = this.heap.poll()) != null) {
222 scanner.close();
223 }
224 }
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 @Override
243 public boolean seek(Cell seekKey) throws IOException {
244 return generalizedSeek(false,
245 seekKey,
246 false,
247 false);
248 }
249
250
251
252
253
254 @Override
255 public boolean reseek(Cell seekKey) throws IOException {
256 return generalizedSeek(false,
257 seekKey,
258 true,
259 false);
260 }
261
262
263
264
265 @Override
266 public boolean requestSeek(Cell key, boolean forward,
267 boolean useBloom) throws IOException {
268 return generalizedSeek(true, key, forward, useBloom);
269 }
270
271
272
273
274
275
276
277
278
279 private boolean generalizedSeek(boolean isLazy, Cell seekKey,
280 boolean forward, boolean useBloom) throws IOException {
281 if (!isLazy && useBloom) {
282 throw new IllegalArgumentException("Multi-column Bloom filter " +
283 "optimization requires a lazy seek");
284 }
285
286 if (current == null) {
287 return false;
288 }
289 heap.add(current);
290 current = null;
291
292 KeyValueScanner scanner;
293 while ((scanner = heap.poll()) != null) {
294 Cell topKey = scanner.peek();
295 if (comparator.getComparator().compare(seekKey, topKey) <= 0) {
296
297
298
299
300
301
302 heap.add(scanner);
303 current = pollRealKV();
304 return current != null;
305 }
306
307 boolean seekResult;
308 if (isLazy && heap.size() > 0) {
309
310 seekResult = scanner.requestSeek(seekKey, forward, useBloom);
311 } else {
312 seekResult = NonLazyKeyValueScanner.doRealSeek(
313 scanner, seekKey, forward);
314 }
315
316 if (!seekResult) {
317 scanner.close();
318 } else {
319 heap.add(scanner);
320 }
321 }
322
323
324 return false;
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338 protected KeyValueScanner pollRealKV() throws IOException {
339 KeyValueScanner kvScanner = heap.poll();
340 if (kvScanner == null) {
341 return null;
342 }
343
344 while (kvScanner != null && !kvScanner.realSeekDone()) {
345 if (kvScanner.peek() != null) {
346 try {
347 kvScanner.enforceSeek();
348 } catch (IOException ioe) {
349 kvScanner.close();
350 throw ioe;
351 }
352 Cell curKV = kvScanner.peek();
353 if (curKV != null) {
354 KeyValueScanner nextEarliestScanner = heap.peek();
355 if (nextEarliestScanner == null) {
356
357 return kvScanner;
358 }
359
360
361
362 Cell nextKV = nextEarliestScanner.peek();
363 if (nextKV == null || comparator.compare(curKV, nextKV) < 0) {
364
365 return kvScanner;
366 }
367
368
369
370
371 heap.add(kvScanner);
372 } else {
373
374
375 kvScanner.close();
376 }
377 } else {
378
379
380 kvScanner.close();
381 }
382 kvScanner = heap.poll();
383 }
384
385 return kvScanner;
386 }
387
388
389
390
391 public PriorityQueue<KeyValueScanner> getHeap() {
392 return this.heap;
393 }
394
395 @Override
396 public long getSequenceID() {
397 return 0;
398 }
399
400 KeyValueScanner getCurrentForTesting() {
401 return current;
402 }
403
404 @Override
405 public Cell getNextIndexedKey() {
406
407 return current == null ? null : current.getNextIndexedKey();
408 }
409 }