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