forked from ObjectLayout/ObjectLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPlusTree.java
More file actions
863 lines (669 loc) · 24.1 KB
/
BPlusTree.java
File metadata and controls
863 lines (669 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
* Written by Gil Tene, Martin Thompson and Michael Barker, and released
* to the public domain, as explained at:
* http://creativecommons.org/publicdomain/zero/1.0/
*/
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import org.ObjectLayout.ReferenceArray;
import org.ObjectLayout.StructuredArray;
@SuppressWarnings("rawtypes")
public class BPlusTree<K, V> implements Iterable<Map.Entry<K, V>> {
private final int nodeSize;
private final Comparator comparator;
private final Leaf firstNode;
private Node root;
private int size;
public BPlusTree(int nodeSize) {
this(nodeSize, null);
}
public BPlusTree(int nodeSize, Comparator<K> comparator) {
this.nodeSize = nodeSize;
this.comparator = comparator;
firstNode = Leaf.newInstance(nodeSize);
this.root = firstNode;
}
@SuppressWarnings("unchecked")
public V put(K key, V val) {
if (key == null || val == null) {
throw new NullPointerException("Keys and values may not be null");
}
Object o = root.put(comparator, key, val);
if (isSplit(o)) {
Node next = root.next();
root = Branch.newInstance(root, next, nodeSize);
o = null;
}
if (null == o) {
size++;
}
return (V) o;
}
@SuppressWarnings("unchecked")
public V get(Object key) {
return (V) root.get(comparator, key);
}
@SuppressWarnings("unchecked")
public V remove(Object key) {
Object o = root.remove(comparator, key);
if (null != o) {
size--;
if (root.hasOnlyChild()) {
root = (Node) root.firstValue();
}
}
return (V) o;
}
public int size() {
return size;
}
static class Entry implements Map.Entry {
private Object key;
private Object val;
public Entry() {
}
@Override
public Object getKey() {
return key;
}
@Override
public Object getValue() {
return val;
}
@Override
public Object setValue(Object value) {
Object oldValue = this.val;
this.val = value;
return oldValue;
}
@Override
public String toString() {
return "[" + key + "->" + val + "]";
}
public void set(Object key, Object val) {
this.key = key;
this.val = val;
}
public void clear() {
key = null;
val = null;
}
}
private static int binarySearch(StructuredArray<Entry> entries, int fromIndex,
int toIndex, Object key, Comparator c) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
Object midVal = entries.get(mid).getKey();
int cmp = compare(c, midVal, key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
static class Leaf extends StructuredArray<Entry> implements Node {
private int size = 0;
private final int capacity;
private Leaf next;
public Leaf() {
capacity = (int) this.getLength();
}
public Object put(Comparator comparator, Object key, Object val) {
Object oldVal;
int search = binarySearch(this, 0, size, key, comparator);
if (search > -1) {
Entry entry = get(search);
oldVal = entry.getValue();
entry.setValue(val);
} else if (size < capacity) {
oldVal = null;
search = -(search + 1);
insert(search, key, val);
} else {
Leaf next = Leaf.newInstance(capacity);
int halfSize = size / 2;
shallowCopy(this, halfSize, next, 0, halfSize);
clear(halfSize, size);
size = halfSize;
next.size = halfSize;
if (compare(comparator, key, next.firstKey()) < 0) {
put(comparator, key, val);
} else {
next.put(comparator, key, val);
}
next.next = this.next;
this.next = next;
oldVal = Node.Sentinal.SPLIT;
}
return oldVal;
}
private void clear(int offset, int count) {
for (int i = offset; i < count; i++) {
get(i).clear();
}
}
public Object get(Comparator comparator, Object key) {
int search = binarySearch(this, 0, size, key, comparator);
if (search < 0) {
return null;
}
return get(search).getValue();
}
@Override
public Object remove(Comparator comparator, Object key) {
int search = binarySearch(this, 0, size, key, comparator);
if (search < 0) {
return null;
}
return remove(search);
}
public BPlusTree.Leaf next() {
return next;
}
@Override
public boolean requiresCompacting() {
return size < capacity / 2;
}
@Override
public Object firstKey() {
return get(0).getKey();
}
@Override
public Object firstValue() {
return get(0).getValue();
}
@Override
public Object lastKey() {
return get(size - 1).getKey();
}
@Override
public Object lastValue() {
return get(size - 1).getValue();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < size; i++) {
Entry entry = get(i);
sb.append(entry.getKey()).append("->").append(entry.getValue());
sb.append(",");
}
sb.setLength(sb.length() - 1);
sb.append("]");
return sb.toString();
}
@Override
public int size() {
return size;
}
@Override
public boolean hasOnlyChild() {
return false;
}
public void mergeFrom(Node right) {
assert (right.size() == capacity / 2) || (size == capacity / 2) : "Should have exactly half capacity nodes";
Leaf leaf = (Leaf) right;
shallowCopy(leaf, 0, this, size, leaf.size());
next = leaf.next();
size += leaf.size();
}
public boolean stealFromRight(Leaf right) {
if (right.size() > capacity / 2) {
append(right.firstKey(), right.firstValue());
right.removeFirst();
return true;
}
return false;
}
public boolean stealFromLeft(Leaf left) {
if (left.size() > capacity / 2) {
push(left.lastKey(), left.lastValue());
left.clearLast();
return true;
}
return false;
}
private void removeFirst() {
remove(0);
}
private Object remove(int search) {
Object o = this.get(search).getValue();
if (search != size - 1) {
shallowCopy(this, search + 1, this, search, size - (search + 1));
}
this.get(size - 1).clear();
size--;
return o;
}
private void insert(int search, Object key, Object val) {
if (search != size) {
shallowCopy(this, search, this, search + 1, size - search);
}
this.get(search).set(key, val);
size++;
}
private void push(Object key, Object val) {
insert(0, key, val);
}
private void append(Object key, Object val) {
get(size).set(key, val);
size++;
}
private void clearLast() {
get(size - 1).clear();
size--;
}
public static Leaf newInstance(int nodeSize) {
return newInstance(Leaf.class, Entry.class, nodeSize);
}
}
static class Branch extends ReferenceArray<Object> implements Node {
private final int capacity;
private int size = 0;
public Branch() {
capacity = ((int) getLength() - 1) / 2;
}
private void setChild(int i, Object o)
{
set(i, o);
}
private Object getChild(int i) {
return get(i);
}
@Override
public Object put(Comparator comparator, Object key, Object val) {
Node node = findNode(comparator, key);
Object oldVal = node.put(comparator, key, val);
if (isSplit(oldVal)) {
Node nextNode = node.next();
Object keyForNextNode = nextNode.firstKey();
if (size == capacity) {
splitBranch(comparator, keyForNextNode, nextNode);
} else {
insertNode(comparator, keyForNextNode, nextNode);
oldVal = null;
}
}
return oldVal;
}
private void splitBranch(Comparator comparator, Object keyForNextNode,
Node nextNode) {
int halfSize = size / 2;
int comparison = compareWithMidValues(comparator, keyForNextNode,
halfSize);
if (comparison == 0) {
Branch nextBranch = create(capacity);
// Copy half of the nodes from the original
int copyFrom = keyOffset(halfSize);
int length = arraySize() - copyFrom;
shallowCopy(this, copyFrom, nextBranch, 1, length);
nextBranch.size = halfSize;
// Key from new node moved up.
nextBranch.firstKey(keyForNextNode);
nextBranch.setChild(0, nextNode);
// clear out latter half...
clearArrayFrom(keyOffset(halfSize));
size = halfSize;
// temporarily store the nextBranch for the parent.
next(nextBranch);
} else if (comparison < 0) {
Branch nextBranch = create(capacity);
// Copy half of the nodes from the original
int copyFrom = keyOffset(halfSize);
int length = arraySize() - copyFrom;
shallowCopy(this, copyFrom, nextBranch, 1, length);
nextBranch.size = halfSize;
// Last key from first half moved up.
nextBranch.firstKey(storedKey(halfSize - 1));
nextBranch.setChild(0, getChild(halfSize * 2));
// clear out latter half...
clearArrayFrom(keyOffset(halfSize - 1));
size = halfSize - 1;
// Insert the new node
insertNode(comparator, keyForNextNode, nextNode);
// temporarily store the nextBranch for the parent.
next(nextBranch);
} else {
Branch nextBranch = create(capacity);
// Copy just under half of the nodes from the original
int copyFrom = keyOffset(halfSize + 1);
int length = arraySize() - copyFrom;
shallowCopy(this, copyFrom, nextBranch, 1, length);
nextBranch.size = halfSize - 1;
// First key from second half moved up.
nextBranch.firstKey(storedKey(halfSize));
nextBranch.setChild(0, getChild((halfSize + 1) * 2));
// clear out latter half...
clearArrayFrom(keyOffset(halfSize));
size = halfSize;
// Insert the new node
nextBranch.insertNode(comparator, keyForNextNode, nextNode);
// temporarily store the nextBranch for the parent.
next(nextBranch);
}
}
private void clearArrayFrom(int offset) {
for (int i = offset; i < getLength(); i++) {
setChild(i, null);
}
}
private void clear(int index) {
setChild(index, null);
}
@Override
public Object get(Comparator comparator, Object key) {
return findNode(comparator, key).get(comparator, key);
}
@Override
public Object remove(Comparator comparator, Object key) {
int index = findKeyIndex(comparator, key);
int nodeOffset = index * 2;
Node node = (Node) getChild(nodeOffset);
Object oldVal = node.remove(comparator, key);
if (node.requiresCompacting()) {
if (node instanceof Leaf) {
compactLeaves((Leaf) node, index, nodeOffset);
} else {
compactBranches((Branch) node, index, nodeOffset);
}
}
return oldVal;
}
private void compactLeaves(Leaf node, int index, int nodeOffset) {
if (index + 1 <= size) {
Leaf left = node;
Leaf right = (Leaf) getChild(nodeOffset + 2);
if (left.stealFromRight(right)) {
setChild(nodeOffset + 1, right.firstKey());
} else {
left.mergeFrom(right);
removeMergedNode(nodeOffset);
}
} else {
Leaf left = (Leaf) getChild(nodeOffset - 2);
Leaf right = node;
if (right.stealFromLeft(left)) {
setChild(nodeOffset - 1, right.firstKey());
} else {
left.mergeFrom(right);
removeMergedNode(nodeOffset);
}
}
}
private void compactBranches(Branch node, int index, int nodeOffset) {
if (index + 1 <= size) {
Branch right = (Branch) getChild(nodeOffset + 2);
Object rightKey = getChild(nodeOffset + 1);
if (right.size() > capacity / 2) {
node.append(rightKey, right.firstValue());
Object poppedKey = right.popKey();
setChild(nodeOffset + 1, poppedKey);
} else {
node.mergeFrom(rightKey, right);
removeMergedNode(nodeOffset);
}
} else {
Branch left = (Branch) getChild(nodeOffset - 2);
Object nodeKey = getChild(nodeOffset - 1);
if (left.size() > capacity / 2) {
node.push(left.lastValue(), nodeKey);
setChild(nodeOffset - 1, left.lastKey());
left.clearLast();
} else {
left.mergeFrom(nodeKey, node);
removeMergedNode(nodeOffset);
}
}
}
private void removeMergedNode(int nodeOffset) {
int length = arraySize() - (nodeOffset + 3);
if (length > 0) {
shallowCopy(this, nodeOffset + 3, this, nodeOffset + 1, length);
}
size--;
clear(arraySize());
clear(arraySize() + 1);
}
public void mergeFrom(Object rightKey, Node right) {
Branch branch = (Branch) right;
setChild(arraySize(), rightKey);
shallowCopy(branch, 0, this, arraySize() + 1, branch.arraySize());
size += branch.size() + 1;
}
private int compareWithMidValues(Comparator comparator, Object key,
int halfSize) {
Object keyA = storedKey(halfSize - 1);
Object keyB = storedKey(halfSize);
int compareA = compare(comparator, key, keyA);
int compareB = compare(comparator, key, keyB);
assert compareA != 0 : "Should not get a key match on split";
assert compareB != 0 : "Should not get a key match on split";
return Integer.signum(compareA) + Integer.signum(compareB);
}
private void insertNode(Comparator comparator, Object key, Node child) {
int keyIndex = findKeyIndex(comparator, key);
int offset = keyIndex * 2 + 1;
int length = arraySize() - offset;
if (length > 0) {
shallowCopy(this, offset, this, offset + 2, length);
}
setChild(offset, key);
setChild(offset + 1, child);
size++;
}
private void next(Branch nextBranch) {
assert size < capacity : "Can only store next node if node is not full";
setChild((int) getLength() - 1, nextBranch);
}
private void firstKey(Object storedKey) {
assert size < capacity : "Can only store first key if node is not full";
setChild((int) getLength() - 1, storedKey);
}
@Override
public Node next() {
int offset = (int) getLength() - 1;
Object nextNode = getChild(offset);
assert nextNode != null : "Can only fetch next once";
setChild(offset, null);
return (Node) nextNode;
}
@Override
public Object firstKey() {
int offset = (int) getLength() - 1;
Object firstKey = getChild(offset);
assert firstKey != null : "Can only fetch firstKey once";
setChild(offset, null);
return firstKey;
}
public Object popKey() {
Object key = getChild(1);
int length = arraySize() - 2;
shallowCopy(this, 2, this, 0, length);
size--;
clear(arraySize());
clear(arraySize() + 1);
return key;
}
@Override
public Node firstValue() {
return (Node) getChild(0);
}
@Override
public Object lastKey() {
assert size > 0 : "Should not be modifying branch with only child";
return getChild(arraySize() - 2);
}
@Override
public Node lastValue() {
assert size > 0 : "Should not be modifying branch with only child";
return (Node) getChild(arraySize() - 1);
}
private void clearLast() {
assert size > 0 : "Should not be modifying branch with only child";
clear(arraySize() - 1);
clear(arraySize() - 2);
size--;
}
public void push(Node val, Object key) {
shallowCopy(this, 0, this, 2, arraySize());
setChild(0, val);
setChild(1, key);
size++;
}
public void append(Object key, Node val) {
setChild(arraySize(), key);
setChild(arraySize() + 1, val);
size++;
}
@Override
public boolean requiresCompacting() {
return size < capacity / 2;
}
private Node findNode(Comparator comparator, Object key) {
return storedNode(findKeyIndex(comparator, key));
}
private int keyOffset(int index) {
return index * 2 + 1;
}
private Node storedNode(int search) {
return (Node) getChild(search * 2);
}
private Object storedKey(int index) {
return getChild(keyOffset(index));
}
private int findKeyIndex(Comparator comparator, Object key) {
int lo = 0;
int hi = size - 1;
while (lo <= hi) {
final int mid = (lo + hi) >>> 1;
Object stored = storedKey(mid);
int comparison = compare(comparator, key, stored);
if (comparison == 0) {
return mid + 1;
} else if (comparison < 0) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lo;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("B:[");
for (int i = 0; i < size; i++) {
sb.append(getChild(i)).append(",");
}
sb.setLength(sb.length() - 1);
sb.append("]");
return sb.toString();
}
@Override
public int size() {
return size;
}
@Override
public boolean hasOnlyChild() {
return size == 0;
}
private int arraySize() {
return size * 2 + 1;
}
private static Branch newInstance(Node left, Node right, int nodeSize) {
Branch branch = create(nodeSize);
branch.setChild(0, left);
branch.setChild(1, right.firstKey());
branch.setChild(2, right);
branch.size = 1;
return branch;
}
private static Branch create(int nodeSize) {
int length = (nodeSize * 2) + 1;
return ReferenceArray.newInstance(Branch.class, length);
}
}
interface Node {
enum Sentinal {
SPLIT
};
Object get(Comparator comparator, Object key);
Object put(Comparator comparator, Object key, Object val);
Object remove(Comparator comparator, Object key);
Object lastValue();
Object lastKey();
Object firstValue();
int size();
boolean requiresCompacting();
Object firstKey();
Node next();
boolean hasOnlyChild();
}
@SuppressWarnings("unchecked")
private static int compare(Comparator comparator, Object a, Object b) {
if (null == comparator) {
return ((Comparable) a).compareTo(b);
} else {
return comparator.compare(a, b);
}
}
private static boolean isSplit(Object o) {
return Node.Sentinal.SPLIT == o;
}
@Override
public String toString() {
return "BPlusTree [nodeSize=" + nodeSize + ", comparator=" + comparator
+ ", root=" + root + "]";
}
public Iterator<Map.Entry<K, V>> iterator() {
return new BPlusTreeIterator(firstNode);
}
public class BPlusTreeIterator implements Iterator<Map.Entry<K, V>> {
private Leaf leaf;
private int index = -1;
public BPlusTreeIterator(Leaf leaf) {
this.leaf = leaf;
}
@Override
public boolean hasNext() {
return (index + 1) < leaf.size() || leaf.next() != null;
}
@SuppressWarnings("unchecked")
@Override
public Map.Entry<K, V> next() {
if ((index + 1) < leaf.size()) {
index++;
Entry entry = leaf.get(index);
return entry;
} else if (leaf.next() != null) {
leaf = leaf.next();
index = 0;
Entry entry = leaf.get(index);
return entry;
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public static <T> void shallowCopy(
final ReferenceArray<T> src, final long srcOffset,
final ReferenceArray<T> dst, final long dstOffset,
final long count) {
if (srcOffset + count > Integer.MAX_VALUE || dstOffset + count > Integer.MAX_VALUE) {
throw new ArrayIndexOutOfBoundsException();
}
int length = (int) count;
int srcOff = (int) srcOffset;
int dstOff = (int) dstOffset;
System.arraycopy(src.asArray(), srcOff, dst.asArray(), dstOff, length);
}
}