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