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.KVComparator;
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @InterfaceAudience.Private
44 public class KeyValueHeap extends NonReversedNonLazyKeyValueScanner
45 implements KeyValueScanner, InternalScanner {
46 protected PriorityQueue<KeyValueScanner> heap = null;
47
48
49
50
51
52
53
54
55
56
57
58 protected KeyValueScanner current = null;
59
60 protected KVScannerComparator comparator;
61
62
63
64
65
66
67
68 public KeyValueHeap(List<? extends KeyValueScanner> scanners,
69 KVComparator comparator) throws IOException {
70 this(scanners, new KVScannerComparator(comparator));
71 }
72
73
74
75
76
77
78
79 KeyValueHeap(List<? extends KeyValueScanner> scanners,
80 KVScannerComparator comparator) throws IOException {
81 this.comparator = comparator;
82 if (!scanners.isEmpty()) {
83 this.heap = new PriorityQueue<KeyValueScanner>(scanners.size(),
84 this.comparator);
85 for (KeyValueScanner scanner : scanners) {
86 if (scanner.peek() != null) {
87 this.heap.add(scanner);
88 } else {
89 scanner.close();
90 }
91 }
92 this.current = pollRealKV();
93 }
94 }
95
96 public Cell peek() {
97 if (this.current == null) {
98 return null;
99 }
100 return this.current.peek();
101 }
102
103 public Cell next() throws IOException {
104 if(this.current == null) {
105 return null;
106 }
107 Cell kvReturn = this.current.next();
108 Cell kvNext = this.current.peek();
109 if (kvNext == null) {
110 this.current.close();
111 this.current = pollRealKV();
112 } else {
113 KeyValueScanner topScanner = this.heap.peek();
114
115 if (topScanner != null && this.comparator.compare(kvNext, topScanner.peek()) >= 0) {
116 this.heap.add(this.current);
117 this.current = pollRealKV();
118 }
119 }
120 return kvReturn;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134 public boolean next(List<Cell> result, int limit) throws IOException {
135 if (this.current == null) {
136 return false;
137 }
138 InternalScanner currentAsInternal = (InternalScanner)this.current;
139 boolean mayContainMoreRows = currentAsInternal.next(result, limit);
140 Cell pee = this.current.peek();
141
142
143
144
145
146
147
148 if (pee == null || !mayContainMoreRows) {
149 this.current.close();
150 } else {
151 this.heap.add(this.current);
152 }
153 this.current = pollRealKV();
154 return (this.current != null);
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public boolean next(List<Cell> result) throws IOException {
168 return next(result, -1);
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 kvScanner.enforceSeek();
347 Cell curKV = kvScanner.peek();
348 if (curKV != null) {
349 KeyValueScanner nextEarliestScanner = heap.peek();
350 if (nextEarliestScanner == null) {
351
352 return kvScanner;
353 }
354
355
356
357 Cell nextKV = nextEarliestScanner.peek();
358 if (nextKV == null || comparator.compare(curKV, nextKV) < 0) {
359
360 return kvScanner;
361 }
362
363
364
365
366 heap.add(kvScanner);
367 } else {
368
369
370 kvScanner.close();
371 }
372 } else {
373
374
375 kvScanner.close();
376 }
377 kvScanner = heap.poll();
378 }
379
380 return kvScanner;
381 }
382
383
384
385
386 public PriorityQueue<KeyValueScanner> getHeap() {
387 return this.heap;
388 }
389
390 @Override
391 public long getSequenceID() {
392 return 0;
393 }
394
395 KeyValueScanner getCurrentForTesting() {
396 return current;
397 }
398 }