1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mavibot.btree;
21
22
23 import java.io.IOException;
24 import java.lang.reflect.Array;
25
26 import org.apache.directory.mavibot.btree.KeyHolder;
27 import org.apache.directory.mavibot.btree.Page;
28 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
29 import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
30
31
32
33
34
35
36
37
38
39
40
41
42 {
43
44 protected transient BTree<K, V> btree;
45
46
47 protected KeyHolder<K>[] keys;
48
49
50 protected PageHolder<K, V>[] children;
51
52
53 protected int nbElems;
54
55
56 protected long revision;
57
58
59 protected long offset = -1L;
60
61
62 protected long lastOffset = -1L;
63
64
65 protected KeyNotFoundException KEY_NOT_FOUND_EXCEPTION = new KeyNotFoundException(
66 "Cannot find an entry associated with this key" );
67
68
69
70
71
72
73
74 protected AbstractPage( BTree<K, V> btree )
75 {
76 this.btree = btree;
77 }
78
79
80
81
82
83 @SuppressWarnings("unchecked")
84
85 protected AbstractPage( BTree<K, V> btree, long revision, int nbElems )
86 {
87 this.btree = btree;
88 this.revision = revision;
89 this.nbElems = nbElems;
90 this.keys = ( KeyHolder[] ) Array.newInstance( KeyHolder.class, nbElems );
91 }
92
93
94
95
96
97 public int getNbElems()
98 {
99 return nbElems;
100 }
101
102
103
104
105
106
107
108 {
109 this.nbElems = nbElems;
110 }
111
112
113
114
115
116 public K getKey( int pos )
117 {
118 if ( ( pos < nbElems ) && ( keys[pos] != null ) )
119 {
120 return keys[pos].getKey();
121 }
122 else
123 {
124 return null;
125 }
126 }
127
128
129
130
131
132 @Override
133 public boolean hasKey( K key ) throws IOException
134 {
135 int pos = findPos( key );
136
137 if ( pos < 0 )
138 {
139
140
141 return children[-pos].getValue().hasKey( key );
142 }
143 else
144 {
145 Page<K, V> page = children[pos].getValue();
146
147 if ( page == null )
148 {
149 System.out.println( "Page is null for pos = " + pos + ", children = " + children[pos] );
150 }
151
152 return page.hasKey( key );
153 }
154 }
155
156
157
158
159
160
161 {
162 if ( pos < nbElems + 1 )
163 {
164 return children[pos].getValue();
165 }
166 else
167 {
168 return null;
169 }
170 }
171
172
173
174
175
176 public TupleCursor<K, V> browse( K key, ReadTransaction<K, V> transaction, ParentPos<K, V>[] stack, int depth )
177 throws IOException
178 {
179 int pos = findPos( key );
180
181 if ( pos < 0 )
182 {
183 pos = -pos;
184 }
185
186
187 stack[depth++] = new ParentPos<K, V>( this, pos );
188
189 Page<K, V> page = children[pos].getValue();
190
191 return page.browse( key, transaction, stack, depth );
192 }
193
194
195
196
197
198 @Override
199 public boolean contains( K key, V value ) throws IOException
200 {
201 int pos = findPos( key );
202
203 if ( pos < 0 )
204 {
205
206
207 return children[-pos].getValue().contains( key, value );
208 }
209 else
210 {
211 return children[pos].getValue().contains( key, value );
212 }
213 }
214
215
216
217
218
219 public V get( K key ) throws IOException, KeyNotFoundException
220 {
221 int pos = findPos( key );
222
223 if ( pos < 0 )
224 {
225
226
227 return children[-pos].getValue().get( key );
228 }
229 else
230 {
231 return children[pos].getValue().get( key );
232 }
233 }
234
235
236
237
238
239
240 {
241 if ( ( pos >= 0 ) && ( pos < children.length ) )
242 {
243 return children[pos].getValue();
244 }
245 else
246 {
247 return null;
248 }
249 }
250
251
252
253
254
255
256 {
257 if ( ( pos >= 0 ) && ( pos < children.length ) )
258 {
259 children[pos] = pageHolder;
260 }
261 }
262
263
264
265
266
267 @Override
268 public ValueCursor<V> getValues( K key ) throws KeyNotFoundException, IOException, IllegalArgumentException
269 {
270 int pos = findPos( key );
271
272 if ( pos < 0 )
273 {
274
275
276 return children[-pos].getValue().getValues( key );
277 }
278 else
279 {
280 return children[pos].getValue().getValues( key );
281 }
282 }
283
284
285
286
287
288 public TupleCursor<K, V> browse( ReadTransaction<K, V> transaction, ParentPos<K, V>[] stack, int depth )
289 throws IOException
290 {
291 stack[depth++] = new ParentPos<K, V>( this, 0 );
292
293 Page<K, V> page = children[0].getValue();
294
295 return page.browse( transaction, stack, depth );
296 }
297
298
299
300
301
302
303
304
305
306
307
308 protected int selectSibling( Page<K, V> parent, int parentPos ) throws IOException
309 {
310 if ( parentPos == 0 )
311 {
312
313
314 return 1;
315 }
316
317 if ( parentPos == parent.getNbElems() )
318 {
319
320
321 return parentPos - 1;
322 }
323
324 Page<K, V> prevPage = ( ( AbstractPage<K, V> ) parent ).getPage( parentPos - 1 );
325 Page<K, V> nextPage = ( ( AbstractPage<K, V> ) parent ).getPage( parentPos + 1 );
326
327 int prevPageSize = prevPage.getNbElems();
328 int nextPageSize = nextPage.getNbElems();
329
330 if ( prevPageSize >= nextPageSize )
331 {
332 return parentPos - 1;
333 }
334 else
335 {
336 return parentPos + 1;
337 }
338 }
339
340
341
342
343
344 public K getLeftMostKey()
345 {
346 return keys[0].getKey();
347 }
348
349
350
351
352
353 public K getRightMostKey()
354 {
355 return keys[nbElems - 1].getKey();
356 }
357
358
359
360
361
362
363 {
364 return offset;
365 }
366
367
368
369
370
371
372 {
373 this.offset = offset;
374 }
375
376
377
378
379
380
381 {
382 return lastOffset;
383 }
384
385
386
387
388
389
390 {
391 this.lastOffset = lastOffset;
392 }
393
394
395
396
397
398
399 {
400 return keys;
401 }
402
403
404
405
406
407
408
409
410
411 {
412 keys[pos] = key;
413 }
414
415
416
417
418
419
420 {
421 this.keys = keys;
422 }
423
424
425
426
427
428
429 {
430
431 return null;
432 }
433
434
435
436
437
438 public long getRevision()
439 {
440 return revision;
441 }
442
443
444
445
446
447
448 {
449 this.revision = revision;
450 }
451
452
453
454
455
456
457
458
459
460
461 protected final int compare( K key1, K key2 )
462 {
463 if ( key1 == key2 )
464 {
465 return 0;
466 }
467
468 if ( key1 == null )
469 {
470 return 1;
471 }
472
473 if ( key2 == null )
474 {
475 return -1;
476 }
477
478 return btree.getComparator().compare( key1, key2 );
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 public int findPos( K key )
519 {
520
521 if ( nbElems == 0 )
522 {
523 return 0;
524 }
525
526 int min = 0;
527 int max = nbElems - 1;
528
529
530 while ( min < max )
531 {
532 int middle = ( min + max + 1 ) >> 1;
533
534 int comp = compare( keys[middle].getKey(), key );
535
536 if ( comp < 0 )
537 {
538 min = middle + 1;
539 }
540 else if ( comp > 0 )
541 {
542 max = middle - 1;
543 }
544 else
545 {
546
547
548
549 return -( middle + 1 );
550 }
551 }
552
553
554 int comp = compare( keys[max].getKey(), key );
555
556 if ( comp == 0 )
557 {
558 return -( max + 1 );
559 }
560 else
561 {
562 if ( comp < 0 )
563 {
564 return max + 1;
565 }
566 else
567 {
568 return max;
569 }
570 }
571 }
572
573
574
575
576
577 public Tuple<K, V> findLeftMost() throws EndOfFileExceededException, IOException
578 {
579 return children[0].getValue().findLeftMost();
580 }
581
582
583
584
585
586 public Tuple<K, V> findRightMost() throws EndOfFileExceededException, IOException
587 {
588 return children[nbElems].getValue().findRightMost();
589 }
590
591
592
593
594
595 public BTree<K, V> getBtree()
596 {
597 return btree;
598 }
599
600
601
602
603
604 public String dumpPage( String tabs )
605 {
606 StringBuilder sb = new StringBuilder();
607
608 if ( nbElems > 0 )
609 {
610
611 sb.append( children[0].getValue().dumpPage( tabs + " " ) );
612
613 for ( int i = 0; i < nbElems; i++ )
614 {
615 sb.append( tabs );
616 sb.append( "<" );
617 sb.append( getKey( i ) ).append( ">\n" );
618 sb.append( children[i + 1].getValue().dumpPage( tabs + " " ) );
619 }
620 }
621
622 return sb.toString();
623 }
624
625
626
627
628
629 public String toString()
630 {
631 StringBuilder sb = new StringBuilder();
632
633 sb.append( "r" ).append( revision );
634 sb.append( ", nbElems:" ).append( nbElems );
635
636 if ( offset > 0 )
637 {
638 sb.append( ", offset:" ).append( offset );
639 }
640
641 return sb.toString();
642 }
643 }