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