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.util.LinkedList;
25
26 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class BTreeFactory<K, V>
41 {
42
43
44
45
46
47
48
49 public static <K, V> BTree<K, V> createPersistedBTree()
50 {
51 BTree<K, V> btree = new PersistedBTree<K, V>();
52
53 return btree;
54 }
55
56
57
58
59
60
61
62
63 public static <K, V> BTree<K, V> createPersistedBTree( PersistedBTreeConfiguration<K, V> configuration )
64 {
65 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
66
67 return btree;
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 public static <K, V> BTree<K, V> createPersistedBTree( String name, ElementSerializer<K> keySerializer,
82 ElementSerializer<V> valueSerializer )
83 {
84 PersistedBTreeConfiguration<K, V> configuration = new PersistedBTreeConfiguration<K, V>();
85
86 configuration.setName( name );
87 configuration.setKeySerializer( keySerializer );
88 configuration.setValueSerializer( valueSerializer );
89 configuration.setPageSize( BTree.DEFAULT_PAGE_SIZE );
90 configuration.setAllowDuplicates( BTree.FORBID_DUPLICATES );
91 configuration.setCacheSize( PersistedBTree.DEFAULT_CACHE_SIZE );
92 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
93
94 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
95
96 return btree;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 public static <K, V> BTree<K, V> createPersistedBTree( String name, ElementSerializer<K> keySerializer,
111 ElementSerializer<V> valueSerializer, boolean allowDuplicates )
112 {
113 PersistedBTreeConfiguration<K, V> configuration = new PersistedBTreeConfiguration<K, V>();
114
115 configuration.setName( name );
116 configuration.setKeySerializer( keySerializer );
117 configuration.setValueSerializer( valueSerializer );
118 configuration.setPageSize( BTree.DEFAULT_PAGE_SIZE );
119 configuration.setAllowDuplicates( allowDuplicates );
120 configuration.setCacheSize( PersistedBTree.DEFAULT_CACHE_SIZE );
121 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
122
123 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
124
125 return btree;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public static <K, V> BTree<K, V> createPersistedBTree( String name, ElementSerializer<K> keySerializer,
141 ElementSerializer<V> valueSerializer, boolean allowDuplicates, int cacheSize )
142 {
143 PersistedBTreeConfiguration<K, V> configuration = new PersistedBTreeConfiguration<K, V>();
144
145 configuration.setName( name );
146 configuration.setKeySerializer( keySerializer );
147 configuration.setValueSerializer( valueSerializer );
148 configuration.setPageSize( BTree.DEFAULT_PAGE_SIZE );
149 configuration.setAllowDuplicates( allowDuplicates );
150 configuration.setCacheSize( cacheSize );
151 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
152
153 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
154
155 return btree;
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169 public static <K, V> BTree<K, V> createPersistedBTree( String name, ElementSerializer<K> keySerializer,
170 ElementSerializer<V> valueSerializer, int pageSize )
171 {
172 PersistedBTreeConfiguration<K, V> configuration = new PersistedBTreeConfiguration<K, V>();
173
174 configuration.setName( name );
175 configuration.setKeySerializer( keySerializer );
176 configuration.setValueSerializer( valueSerializer );
177 configuration.setPageSize( pageSize );
178 configuration.setAllowDuplicates( BTree.FORBID_DUPLICATES );
179 configuration.setCacheSize( PersistedBTree.DEFAULT_CACHE_SIZE );
180 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
181
182 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
183
184 return btree;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public static <K, V> BTree<K, V> createPersistedBTree( String name, ElementSerializer<K> keySerializer,
200 ElementSerializer<V> valueSerializer, int pageSize, boolean allowDuplicates )
201 {
202 PersistedBTreeConfiguration<K, V> configuration = new PersistedBTreeConfiguration<K, V>();
203
204 configuration.setName( name );
205 configuration.setKeySerializer( keySerializer );
206 configuration.setValueSerializer( valueSerializer );
207 configuration.setPageSize( pageSize );
208 configuration.setAllowDuplicates( allowDuplicates );
209 configuration.setCacheSize( PersistedBTree.DEFAULT_CACHE_SIZE );
210 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
211
212 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
213
214 return btree;
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public static <K, V> BTree<K, V> createPersistedBTree( String name, ElementSerializer<K> keySerializer,
231 ElementSerializer<V> valueSerializer, int pageSize, boolean allowDuplicates, int cacheSize )
232 {
233 PersistedBTreeConfiguration<K, V> configuration = new PersistedBTreeConfiguration<K, V>();
234
235 configuration.setName( name );
236 configuration.setKeySerializer( keySerializer );
237 configuration.setValueSerializer( valueSerializer );
238 configuration.setPageSize( pageSize );
239 configuration.setAllowDuplicates( allowDuplicates );
240 configuration.setCacheSize( cacheSize );
241 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
242
243 BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
244
245 return btree;
246 }
247
248
249
250
251
252
253
254
255 public static <K, V> BTree<K, V> createInMemoryBTree()
256 {
257 BTree<K, V> btree = new InMemoryBTree<K, V>();
258
259 return btree;
260 }
261
262
263
264
265
266
267
268
269 public static <K, V> BTree<K, V> createInMemoryBTree( InMemoryBTreeConfiguration<K, V> configuration )
270 {
271 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
272
273 return btree;
274 }
275
276
277
278
279
280
281
282
283
284
285 public static <K, V> BTree<K, V> createInMemoryBTree( String name, ElementSerializer<K> keySerializer,
286 ElementSerializer<V> valueSerializer )
287 {
288 InMemoryBTreeConfiguration<K, V> configuration = new InMemoryBTreeConfiguration<K, V>();
289
290 configuration.setName( name );
291 configuration.setKeySerializer( keySerializer );
292 configuration.setValueSerializer( valueSerializer );
293 configuration.setPageSize( BTree.DEFAULT_PAGE_SIZE );
294 configuration.setAllowDuplicates( BTree.FORBID_DUPLICATES );
295 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
296
297 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
298
299 return btree;
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313 public static <K, V> BTree<K, V> createInMemoryBTree( String name, ElementSerializer<K> keySerializer,
314 ElementSerializer<V> valueSerializer, boolean allowDuplicates )
315 {
316 InMemoryBTreeConfiguration<K, V> configuration = new InMemoryBTreeConfiguration<K, V>();
317
318 configuration.setName( name );
319 configuration.setKeySerializer( keySerializer );
320 configuration.setValueSerializer( valueSerializer );
321 configuration.setPageSize( BTree.DEFAULT_PAGE_SIZE );
322 configuration.setAllowDuplicates( allowDuplicates );
323 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
324
325 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
326
327 return btree;
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341 public static <K, V> BTree<K, V> createInMemoryBTree( String name, ElementSerializer<K> keySerializer,
342 ElementSerializer<V> valueSerializer, int pageSize )
343 {
344 InMemoryBTreeConfiguration<K, V> configuration = new InMemoryBTreeConfiguration<K, V>();
345
346 configuration.setName( name );
347 configuration.setKeySerializer( keySerializer );
348 configuration.setValueSerializer( valueSerializer );
349 configuration.setPageSize( pageSize );
350 configuration.setAllowDuplicates( BTree.FORBID_DUPLICATES );
351 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
352
353 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
354
355 return btree;
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369 public static <K, V> BTree<K, V> createInMemoryBTree( String name, String filePath,
370 ElementSerializer<K> keySerializer,
371 ElementSerializer<V> valueSerializer )
372 {
373 InMemoryBTreeConfiguration<K, V> configuration = new InMemoryBTreeConfiguration<K, V>();
374
375 configuration.setName( name );
376 configuration.setFilePath( filePath );
377 configuration.setKeySerializer( keySerializer );
378 configuration.setValueSerializer( valueSerializer );
379 configuration.setPageSize( BTree.DEFAULT_PAGE_SIZE );
380 configuration.setAllowDuplicates( BTree.FORBID_DUPLICATES );
381 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
382
383 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
384
385 return btree;
386 }
387
388
389
390
391
392
393
394
395
396
397
398
399
400 public static <K, V> BTree<K, V> createInMemoryBTree( String name, String filePath,
401 ElementSerializer<K> keySerializer,
402 ElementSerializer<V> valueSerializer, int pageSize )
403 {
404 InMemoryBTreeConfiguration<K, V> configuration = new InMemoryBTreeConfiguration<K, V>();
405
406 configuration.setName( name );
407 configuration.setFilePath( filePath );
408 configuration.setKeySerializer( keySerializer );
409 configuration.setValueSerializer( valueSerializer );
410 configuration.setPageSize( pageSize );
411 configuration.setAllowDuplicates( BTree.FORBID_DUPLICATES );
412 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
413
414 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
415
416 return btree;
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432 public static <K, V> BTree<K, V> createInMemoryBTree( String name, String filePath,
433 ElementSerializer<K> keySerializer,
434 ElementSerializer<V> valueSerializer, int pageSize, boolean allowDuplicates )
435 {
436 InMemoryBTreeConfiguration<K, V> configuration = new InMemoryBTreeConfiguration<K, V>();
437
438 configuration.setName( name );
439 configuration.setFilePath( filePath );
440 configuration.setKeySerializer( keySerializer );
441 configuration.setValueSerializer( valueSerializer );
442 configuration.setPageSize( pageSize );
443 configuration.setAllowDuplicates( allowDuplicates );
444 configuration.setWriteBufferSize( BTree.DEFAULT_WRITE_BUFFER_SIZE );
445
446 BTree<K, V> btree = new InMemoryBTree<K, V>( configuration );
447
448 return btree;
449 }
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465 {
466 if ( btree.getType() == BTreeTypeEnum.PERSISTED )
467 {
468 return new PersistedLeaf<K, V>( btree, revision, nbElems );
469 }
470 else
471 {
472 return new InMemoryLeaf<K, V>( btree, revision, nbElems );
473 }
474 }
475
476
477
478
479
480
481
482
483
484
485
486 {
487 if ( btree.getType() == BTreeTypeEnum.PERSISTED )
488 {
489 return new PersistedNode<K, V>( btree, revision, nbElems );
490 }
491 else
492 {
493 return new InMemoryNode<K, V>( btree, revision, nbElems );
494 }
495 }
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510 {
511 KeyHolder<K> keyHolder;
512
513 if ( btree.getType() == BTreeTypeEnum.PERSISTED )
514 {
515 keyHolder = new PersistedKeyHolder<K>( btree.getKeySerializer(), key );
516 }
517 else
518 {
519 keyHolder = new KeyHolder<K>( key );
520 }
521
522 ( ( AbstractPage<K, V> ) page ).setKey( pos, keyHolder );
523 }
524
525
526
527
528
529
530
531
532 {
533 if ( btree.getType() == BTreeTypeEnum.PERSISTED )
534 {
535 ( ( PersistedLeaf<K, V> ) page ).setValue( pos, value );
536 }
537 else
538 {
539 ( ( InMemoryLeaf<K, V> ) page ).setValue( pos, value );
540 }
541 }
542
543
544
545
546
547
548
549
550
551
552
553 {
554 if ( btree.getType() == BTreeTypeEnum.PERSISTED )
555 {
556 ( ( PersistedNode<K, V> ) page ).setValue( pos, new PersistedPageHolder<K, V>( btree, child ) );
557 }
558 else
559 {
560 ( ( InMemoryNode<K, V> ) page ).setPageHolder( pos, new PageHolder<K, V>( btree, child ) );
561 }
562 }
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578 throws ClassNotFoundException, IllegalAccessException, InstantiationException
579 {
580 Class<?> keySerializer = Class.forName( keySerializerFqcn );
581 @SuppressWarnings("unchecked")
582 ElementSerializer<K> instance = ( ElementSerializer<K> ) keySerializer.newInstance();
583 btree.setKeySerializer( instance );
584 }
585
586
587
588
589
590
591
592
593
594
595
596
597 throws ClassNotFoundException, IllegalAccessException, InstantiationException
598 {
599 Class<?> valueSerializer = Class.forName( valueSerializerFqcn );
600 @SuppressWarnings("unchecked")
601 ElementSerializer<V> instance = ( ElementSerializer<V> ) valueSerializer.newInstance();
602 btree.setValueSerializer( instance );
603 }
604
605
606
607
608
609
610
611
612
613
614 {
615 ( ( AbstractBTree<K, V> ) btree ).setRootPage( root );
616 }
617
618
619
620
621
622
623
624
625
626 {
627 return btree.getRootPage();
628 }
629
630
631
632
633
634
635
636
637
638 {
639 ( ( AbstractBTree<K, V> ) btree ).setNbElems( nbElems );
640 }
641
642
643
644
645
646
647
648
649
650 {
651 ( ( AbstractBTree<K, V> ) btree ).setRevision( revision );
652 }
653
654
655
656
657
658
659
660
661
662 {
663 btree.setName( name );
664 }
665
666
667
668
669
670
671
672
673
674 {
675 btree.setPageSize( pageSize );
676 }
677
678
679
680
681
682
683
684
685
686
687
688
689 {
690 LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
691
692 ParentPos<K, V> last = new ParentPos<K, V>( btree.getRootPage(), btree.getRootPage().getNbElems() );
693 stack.push( last );
694
695 if ( btree.getRootPage().isLeaf() )
696 {
697 Page<K, V> leaf = btree.getRootPage();
698 ValueHolder<V> valueHolder = ( ( AbstractPage<K, V> ) leaf ).getValue( last.pos );
699 last.valueCursor = valueHolder.getCursor();
700 }
701 else
702 {
703 Page<K, V> node = btree.getRootPage();
704
705 while ( true )
706 {
707 Page<K, V> p = ( ( AbstractPage<K, V> ) node ).getPage( node.getNbElems() );
708
709 last = new ParentPos<K, V>( p, p.getNbElems() );
710 stack.push( last );
711
712 if ( p.isLeaf() )
713 {
714 Page<K, V> leaf = last.page;
715 ValueHolder<V> valueHolder = ( ( AbstractPage<K, V> ) leaf ).getValue( last.pos );
716 last.valueCursor = valueHolder.getCursor();
717 break;
718 }
719 }
720 }
721
722 return stack;
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736 {
737 if ( btree instanceof PersistedBTree )
738 {
739 ( ( PersistedBTree<K, V> ) btree ).setRootPageOffset( rootPageOffset );
740 }
741 else
742 {
743 throw new IllegalArgumentException( "The BTree must be a PersistedBTree" );
744 }
745 }
746
747
748
749
750
751
752
753
754
755 {
756 if ( btree instanceof PersistedBTree )
757 {
758 ( ( PersistedBTree<K, V> ) btree ).setNextBTreeOffset( nextBTreeOffset );
759 }
760 else
761 {
762 throw new IllegalArgumentException( "The BTree must be a PersistedBTree" );
763 }
764 }
765
766
767
768
769
770
771
772
773
774 {
775 if ( btree instanceof PersistedBTree )
776 {
777 ( ( PersistedBTree<K, V> ) btree ).setRecordManager( recordManager );
778 }
779 else
780 {
781 throw new IllegalArgumentException( "The BTree must be a PersistedBTree" );
782 }
783 }
784
785
786
787
788
789
790
791
792
793
794
795 {
796 if ( btree instanceof PersistedBTree )
797 {
798 KeyHolder<K> keyHolder = new PersistedKeyHolder<K>( btree.getKeySerializer(), buffer );
799 ( ( AbstractPage<K, V> ) page ).setKey( pos, keyHolder );
800 }
801 else
802 {
803 throw new IllegalArgumentException( "The BTree must be a PersistedBTree" );
804 }
805 }
806
807
808
809
810
811
812
813
814
815 {
816 if ( btree instanceof PersistedBTree )
817 {
818 LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
819
820 ParentPos<K, V> first = new ParentPos<K, V>( btree.getRootPage(), 0 );
821 stack.push( first );
822
823 if ( btree.getRootPage().isLeaf() )
824 {
825 Page<K, V> leaf = btree.getRootPage();
826 ValueHolder<V> valueHolder = ( ( AbstractPage<K, V> ) leaf ).getValue( first.pos );
827 first.valueCursor = valueHolder.getCursor();
828 }
829 else
830 {
831 Page<K, V> node = btree.getRootPage();
832
833 while ( true )
834 {
835 Page<K, V> page = ( ( AbstractPage<K, V> ) node ).getPage( 0 );
836
837 first = new ParentPos<K, V>( page, 0 );
838 stack.push( first );
839
840 if ( page.isLeaf() )
841 {
842 ValueHolder<V> valueHolder = ( ( AbstractPage<K, V> ) page ).getValue( first.pos );
843 first.valueCursor = valueHolder.getCursor();
844 break;
845 }
846 }
847 }
848
849 return stack;
850 }
851 else
852 {
853 throw new IllegalArgumentException( "The BTree must be a PersistedBTree" );
854 }
855 }
856 }