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.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.KeyValue;
30 import org.apache.hadoop.hbase.KeyValue.KVComparator;
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 KeyValue peek() {
98 if (this.current == null) {
99 return null;
100 }
101 return this.current.peek();
102 }
103
104 public KeyValue next() throws IOException {
105 if(this.current == null) {
106 return null;
107 }
108 KeyValue kvReturn = this.current.next();
109 KeyValue 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
137 public boolean next(List<Cell> result, int limit) throws IOException {
138 if (this.current == null) {
139 return false;
140 }
141 InternalScanner currentAsInternal = (InternalScanner)this.current;
142 boolean mayContainMoreRows = currentAsInternal.next(result, limit);
143 KeyValue pee = this.current.peek();
144
145
146
147
148
149
150
151 if (pee == null || !mayContainMoreRows) {
152 this.current.close();
153 } else {
154 this.heap.add(this.current);
155 }
156 this.current = null;
157 this.current = pollRealKV();
158 return (this.current != null);
159 }
160
161
162
163
164
165
166
167
168
169
170
171 public boolean next(List<Cell> result) throws IOException {
172 return next(result, -1);
173 }
174
175 protected static class KVScannerComparator implements Comparator<KeyValueScanner> {
176 protected KVComparator kvComparator;
177
178
179
180
181 public KVScannerComparator(KVComparator kvComparator) {
182 this.kvComparator = kvComparator;
183 }
184 public int compare(KeyValueScanner left, KeyValueScanner right) {
185 int comparison = compare(left.peek(), right.peek());
186 if (comparison != 0) {
187 return comparison;
188 } else {
189
190
191 long leftSequenceID = left.getSequenceID();
192 long rightSequenceID = right.getSequenceID();
193 if (leftSequenceID > rightSequenceID) {
194 return -1;
195 } else if (leftSequenceID < rightSequenceID) {
196 return 1;
197 } else {
198 return 0;
199 }
200 }
201 }
202
203
204
205
206
207
208 public int compare(KeyValue left, KeyValue right) {
209 return this.kvComparator.compare(left, right);
210 }
211
212
213
214 public KVComparator getComparator() {
215 return this.kvComparator;
216 }
217 }
218
219 public void close() {
220 if (this.current != null) {
221 this.current.close();
222 }
223 if (this.heap != null) {
224 KeyValueScanner scanner;
225 while ((scanner = this.heap.poll()) != null) {
226 scanner.close();
227 }
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 @Override
247 public boolean seek(KeyValue seekKey) throws IOException {
248 return generalizedSeek(false,
249 seekKey,
250 false,
251 false);
252 }
253
254
255
256
257
258 @Override
259 public boolean reseek(KeyValue seekKey) throws IOException {
260 return generalizedSeek(false,
261 seekKey,
262 true,
263 false);
264 }
265
266
267
268
269 @Override
270 public boolean requestSeek(KeyValue key, boolean forward,
271 boolean useBloom) throws IOException {
272 return generalizedSeek(true, key, forward, useBloom);
273 }
274
275
276
277
278
279
280
281
282
283 private boolean generalizedSeek(boolean isLazy, KeyValue seekKey,
284 boolean forward, boolean useBloom) throws IOException {
285 if (!isLazy && useBloom) {
286 throw new IllegalArgumentException("Multi-column Bloom filter " +
287 "optimization requires a lazy seek");
288 }
289
290 if (current == null) {
291 return false;
292 }
293 heap.add(current);
294 current = null;
295
296 KeyValueScanner scanner;
297 while ((scanner = heap.poll()) != null) {
298 KeyValue topKey = scanner.peek();
299 if (comparator.getComparator().compare(seekKey, topKey) <= 0) {
300
301
302
303
304
305
306 heap.add(scanner);
307 current = pollRealKV();
308 return current != null;
309 }
310
311 boolean seekResult;
312 if (isLazy && heap.size() > 0) {
313
314 seekResult = scanner.requestSeek(seekKey, forward, useBloom);
315 } else {
316 seekResult = NonLazyKeyValueScanner.doRealSeek(
317 scanner, seekKey, forward);
318 }
319
320 if (!seekResult) {
321 scanner.close();
322 } else {
323 heap.add(scanner);
324 }
325 }
326
327
328 return false;
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342 protected KeyValueScanner pollRealKV() throws IOException {
343 KeyValueScanner kvScanner = heap.poll();
344 if (kvScanner == null) {
345 return null;
346 }
347
348 while (kvScanner != null && !kvScanner.realSeekDone()) {
349 if (kvScanner.peek() != null) {
350 try {
351 kvScanner.enforceSeek();
352 } catch (IOException ioe) {
353 kvScanner.close();
354 throw ioe;
355 }
356 KeyValue curKV = kvScanner.peek();
357 if (curKV != null) {
358 KeyValueScanner nextEarliestScanner = heap.peek();
359 if (nextEarliestScanner == null) {
360
361 return kvScanner;
362 }
363
364
365
366 KeyValue nextKV = nextEarliestScanner.peek();
367 if (nextKV == null || comparator.compare(curKV, nextKV) < 0) {
368
369 return kvScanner;
370 }
371
372
373
374
375 heap.add(kvScanner);
376 } else {
377
378
379 kvScanner.close();
380 }
381 } else {
382
383
384 kvScanner.close();
385 }
386 kvScanner = heap.poll();
387 }
388
389 return kvScanner;
390 }
391
392
393
394
395 public PriorityQueue<KeyValueScanner> getHeap() {
396 return this.heap;
397 }
398
399 @Override
400 public long getSequenceID() {
401 return 0;
402 }
403
404 KeyValueScanner getCurrentForTesting() {
405 return current;
406 }
407
408 @Override
409 public byte[] getNextIndexedKey() {
410
411 return current == null ? null : current.getNextIndexedKey();
412 }
413 }