-
Notifications
You must be signed in to change notification settings - Fork 0
/
bptree.go
1133 lines (1000 loc) · 27.1 KB
/
bptree.go
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* // Copyright (c) 2021. Scott Cagno. All rights reserved.
* // The license can be found in the root of this project; see LICENSE.
*/
package bptree
import (
"encoding/binary"
"log"
"strconv"
"strings"
"unsafe"
)
var stringZero = *new(string)
var ValTypeZero = *new([]byte)
func Compare(a, b string) int {
return strings.Compare(a, b)
}
func Equal(a, b string) bool {
return a == b
}
const (
defaultOrder = orderSize32
orderSize4 = 4
orderSize8 = 8
orderSize16 = 16
orderSize32 = 32
orderSize64 = 64
orderSize128 = 128
orderSize256 = 256
orderSize512 = 512
)
// bpNode represents a bpNode of the BPTree
type bpNode struct {
numKeys int
keys [defaultOrder - 1]string
pointers [defaultOrder]unsafe.Pointer
parent *bpNode
isLeaf bool
}
func (n *bpNode) hasKey(key string) bool {
if n.isLeaf {
for i := 0; i < n.numKeys; i++ {
if Equal(key, n.keys[i]) {
return true
}
}
}
return false
}
func (n *bpNode) closest(key string) (*entry, bool) {
if n.isLeaf {
i := 0
for ; i < n.numKeys; i++ {
if Compare(key, n.keys[i]) < 0 {
break
}
}
if i > 0 {
i--
}
return (*entry)(n.pointers[i]), true
}
return nil, false
}
func (n *bpNode) record(key string) (*entry, bool) {
if n.isLeaf {
for i := 0; i < n.numKeys; i++ {
if Equal(key, n.keys[i]) {
return (*entry)(n.pointers[i]), true
}
}
}
return nil, false
}
// entry represents an entry pointed to by a leaf bpNode
type entry struct {
Key string
Value []byte
}
// BPTree represents the root of a b+tree
type BPTree struct {
root *bpNode
}
// NewBPTree creates and returns a new tree
func NewBPTree() *BPTree {
return new(BPTree)
}
// Has returns a boolean indicating weather or not
// the provided key and associated record exists.
func (t *BPTree) Has(key string) bool {
return t.findEntry(key) != nil
}
// HasInt tests and returns a boolean value if the
// provided key exists in the tree
func (t *BPTree) HasInt(key int64) bool {
return t.findEntry(IntToKey(key)) != nil
}
// Add inserts a new record using the provided key. It
// only inserts an entry if the key does not already exist.
func (t *BPTree) Add(key string, value []byte) {
// master insertUnique method only inserts if the key
// does not currently exist in the tree
t.insertUnique(key, value)
}
// AddInt inserts a new record using the provided integer key
// and value. It only inserts an entry if the key does not
// already exist.
func (t *BPTree) AddInt(key int64, value int64) {
// master insertUnique method only inserts if the key
// does not currently exist in the tree
t.insertUnique(IntToKey(key), IntToVal(value))
}
// insertUnique inserts a new record using the provided key. It
// only inserts an entry if the key does not already exist.
func (t *BPTree) insertUnique(key string, value []byte) {
// If the tree is empty, start a new one and return.
if t.root == nil {
t.root = startNewTree(key, &entry{key, value})
return
}
// If the tree already exists, let's see what we
// get when we try to find the correct leaf.
leaf := findLeaf(t.root, key)
// check to ensure the leaf node does already contain the key
if leaf.hasKey(key) {
return // Key already exists! So lets just return.
}
// If the tree already exists and the key has not been
// found, then let's insert it into the tree and return!
if leaf.numKeys < defaultOrder-1 {
insertIntoLeaf(leaf, key, &entry{key, value})
return
}
// Otherwise, insert, split and balance returning the updated root.
t.root = insertIntoLeafAfterSplitting(t.root, leaf, key, &entry{key, value})
}
// Put is mainly used when you wish to upsert as it assumes the
// data to already be contained the tree. It will overwrite
// duplicate keys, as it does not check to see if the key exists
func (t *BPTree) Put(key string, value []byte) bool {
// master insert method treats insertion much like
// "setting" in a hashmap (an upsert) by default
return t.insert(key, value)
}
// PutInt is mainly used when you wish to upsert as it assumes the
// data to already be contained the tree. It will overwrite
// duplicate keys, as it does not check to see if the key exists
func (t *BPTree) PutInt(key int64, value int64) bool {
// master insert method treats insertion much like
// "setting" in a hashmap (an upsert) by default
return t.insert(IntToKey(key), IntToVal(value))
}
func (t *BPTree) GetClosest(key string) (string, []byte) {
l := findLeaf(t.root, key)
if l == nil {
return "", nil
}
e, ok := l.closest(key)
if !ok {
return "", nil
}
return e.Key, e.Value
}
// Get returns the record for a given key if it exists
func (t *BPTree) Get(key string) (string, []byte) {
e := t.findEntry(key)
if e == nil {
return "", nil
}
return e.Key, e.Value
}
// GetInt returns the record for a given key if it exists
func (t *BPTree) GetInt(key int64) (int64, int64) {
e := t.findEntry(IntToKey(key))
if e == nil {
return -1, -1
}
return KeyToInt(e.Key), ValToInt(e.Value)
}
func (t *BPTree) Del(key string) (string, []byte) {
e := t.delete(key)
if e == nil {
return "", nil
}
return e.Key, e.Value
}
func (t *BPTree) DelInt(key int64) (int64, int64) {
e := t.delete(IntToKey(key))
if e == nil {
return -1, -1
}
return KeyToInt(e.Key), ValToInt(e.Value)
}
func (t *BPTree) Range(iter func(k string, v []byte) bool) {
c := findFirstLeaf(t.root)
if c == nil {
return
}
var e *entry
for {
for i := 0; i < c.numKeys; i++ {
e = (*entry)(c.pointers[i])
if e != nil && !iter(e.Key, e.Value) {
continue
}
}
if c.pointers[defaultOrder-1] != nil {
c = (*bpNode)(c.pointers[defaultOrder-1])
} else {
break
}
}
}
func (t *BPTree) Min() (string, []byte) {
c := findFirstLeaf(t.root)
if c == nil {
return "", nil
}
e := (*entry)(c.pointers[0])
return e.Key, e.Value
}
func (t *BPTree) Max() (string, []byte) {
c := findLastLeaf(t.root)
if c == nil {
return "", nil
}
e := (*entry)(c.pointers[c.numKeys-1])
return e.Key, e.Value
}
func (t *BPTree) Len() int {
var count int
for n := findFirstLeaf(t.root); n != nil; n = n.nextLeaf() {
count += n.numKeys
}
return count
}
func (t *BPTree) Size() int64 {
c := findFirstLeaf(t.root)
if c == nil {
return 0
}
var s int64
var e *entry
for {
for i := 0; i < c.numKeys; i++ {
e = (*entry)(c.pointers[i])
if e != nil {
s += int64(len(e.Key) + len(e.Value))
}
}
if c.pointers[defaultOrder-1] != nil {
c = (*bpNode)(c.pointers[defaultOrder-1])
} else {
break
}
}
return s
}
func (t *BPTree) Close() {
//t.destroyTree()
t.root = nil
}
// insert is the "master" insertion function.
// Inserts a key and an associated Value into
// the B+ tree, causing the tree to be adjusted
// however necessary to maintain the B+ tree
// properties
func (t *BPTree) insert(key string, value []byte) bool {
// CASE: BPTree does not exist yet, start a new tree
if t.root == nil {
t.root = startNewTree(key, &entry{key, value})
return false
}
// The current implementation ignores duplicates (will treat it kind of like a set operation)
leaf, recordPointer := t.find(key)
if recordPointer != nil {
// If the key already exists in this tree then proceed to update Value and return
recordPointer.Value = value
return true
}
// No Record found, so create a new Record for the Value. NOTE: Normally t.find would not return
// a record pointer in which case we would need the line below this.
//recordPointer = makeRecord(Value)
// CASE: BPTree already exists (continue through the rest of the function)
// Leaf has room for the key and recordPointer--insert into leaf and return
if leaf.numKeys < defaultOrder-1 {
insertIntoLeaf(leaf, key, &entry{key, value})
return false
}
// Leaf does not have enough room and needs to be split
t.root = insertIntoLeafAfterSplitting(t.root, leaf, key, &entry{key, value})
return false
}
// startNewTree first insertion case: starts a new tree
func startNewTree(key string, pointer *entry) *bpNode {
root := &bpNode{isLeaf: true} // makeLeaf()
root.keys[0] = key
root.pointers[0] = unsafe.Pointer(pointer)
root.pointers[defaultOrder-1] = nil
root.parent = nil
root.numKeys++
return root
}
// insertIntoNewRoot creates a new root for two subtrees and inserts the appropriate key into the new root
func insertIntoNewRoot(left *bpNode, key string, right *bpNode) *bpNode {
root := &bpNode{} // makeNode()
root.keys[0] = key
root.pointers[0] = unsafe.Pointer(left)
root.pointers[1] = unsafe.Pointer(right)
root.numKeys++
root.parent = nil
left.parent = root
right.parent = root
return root
}
/*
* insertIntoParent inserts a new bpNode (leaf or internal bpNode) into the tree--returns the root of
* the tree after insertion is complete
*/
func insertIntoParent(root *bpNode, left *bpNode, key string, right *bpNode) *bpNode {
/*
* Case: new root
*/
if left.parent == nil {
return insertIntoNewRoot(left, key, right)
}
/*
* Case: leaf or bpNode. (Remainder of function body.)
* Find the parents pointer to the left bpNode
*/
leftIndex := getLeftIndex(left.parent, left)
/*
* Simple case: the new key fits into the bpNode.
*/
if left.parent.numKeys < defaultOrder-1 {
return insertIntoNode(root, left.parent, leftIndex, key, right)
}
/* Harder case: split a bpNode in order
* to preserve the B+ tree properties.
*/
return insertIntoNodeAfterSplitting(root, left.parent, leftIndex, key, right)
}
/*
* getLeftIndex helper function used in insertIntoParent to find the index of the parents
* pointer to the bpNode to the left of the key to be inserted
*/
func getLeftIndex(parent, left *bpNode) int {
var leftIndex int
for leftIndex <= parent.numKeys && (*bpNode)(parent.pointers[leftIndex]) != left {
leftIndex++
}
return leftIndex
}
/*
* insertIntoNode inserts a new key and pointer to a bpNode into a bpNode into which these can fit without violating the
* tree's properties
*/
func insertIntoNode(root, n *bpNode, leftIndex int, key string, right *bpNode) *bpNode {
// Consider using copy, it might be better
copy(n.pointers[leftIndex+2:], n.pointers[leftIndex+1:])
copy(n.keys[leftIndex+1:], n.keys[leftIndex:])
/* // ORIG
for i := n.numKeys; i > leftIndex; i-- {
n.pointers[i+1] = n.pointers[i]
n.keys[i] = n.keys[i-1]
}
*/
n.pointers[leftIndex+1] = unsafe.Pointer(right)
n.keys[leftIndex] = key
n.numKeys++
return root
}
// insertIntoNodeAfterSplitting inserts a new key and pointer to a bpNode into a bpNode, causing
// the nodes size to exceed the ORDER, and causing the bpNode to split
func insertIntoNodeAfterSplitting(root, oldNode *bpNode, leftIndex int, key string, right *bpNode) *bpNode {
// first create a temp set of keys and pointers to hold everything in ORDER, including
// the new key and pointer, inserted in their correct places--then create a new bpNode
// and copy half of the keys and pointers to the old bpNode and the other half to the new
var i, j int
var tempKeys [defaultOrder]string //tempKeys := make([]int, ORDER)
var tempPointers [defaultOrder + 1]unsafe.Pointer //tempPointers := make([]interface{}, ORDER+1)
for i, j = 0, 0; i < oldNode.numKeys+1; i, j = i+1, j+1 {
if j == leftIndex+1 {
j++
}
tempPointers[j] = oldNode.pointers[i]
}
for i, j = 0, 0; i < oldNode.numKeys; i, j = i+1, j+1 {
if j == leftIndex {
j++
}
tempKeys[j] = oldNode.keys[i]
}
tempPointers[leftIndex+1] = unsafe.Pointer(right)
tempKeys[leftIndex] = key
/*
* copy half the keys and pointers to the old bpNode...
*/
split := cut(defaultOrder)
oldNode.numKeys = 0
for i = 0; i < split-1; i++ {
oldNode.pointers[i] = tempPointers[i]
oldNode.keys[i] = tempKeys[i]
oldNode.numKeys++
}
oldNode.pointers[i] = tempPointers[i]
kPrime := tempKeys[split-1]
/*
* ...create the new bpNode and copy the other half the keys and pointers
*/
newNode := &bpNode{} // makeNode()
for i, j = i+1, 0; i < defaultOrder; i, j = i+1, j+1 {
newNode.pointers[j] = tempPointers[i]
newNode.keys[j] = tempKeys[i]
newNode.numKeys++
}
newNode.pointers[j] = tempPointers[i]
newNode.parent = oldNode.parent
/*
* Free up tempPointers and tempKeys
*/
for i = 0; i < defaultOrder; i++ {
tempKeys[i] = *new(string) // zero values
tempPointers[i] = nil // zero values
}
tempPointers[defaultOrder] = nil
var child *bpNode
for i = 0; i <= newNode.numKeys; i++ {
child = (*bpNode)(newNode.pointers[i])
child.parent = newNode
}
/* Insert a new key into the parent of the two
* nodes resulting from the split, with
* the old bpNode to the left and the new to the right.
*/
return insertIntoParent(root, oldNode, kPrime, newNode)
}
// insertIntoLeaf inserts a new pointer to a Record and its
// corresponding key into a leaf.
func insertIntoLeaf(leaf *bpNode, key string, pointer *entry) /* *bpNode */ {
var i, insertionPoint int
for insertionPoint < leaf.numKeys && Compare(leaf.keys[insertionPoint], key) == -1 {
insertionPoint++
}
for i = leaf.numKeys; i > insertionPoint; i-- {
leaf.keys[i] = leaf.keys[i-1]
leaf.pointers[i] = leaf.pointers[i-1]
}
leaf.keys[insertionPoint] = key
leaf.pointers[insertionPoint] = unsafe.Pointer(pointer)
leaf.numKeys++
//return leaf // might not need to return this leaf
}
// insertIntoLeafAfterSplitting inserts a new key and pointer to a new Record into a leaf
// so as to exceed the tree's ORDER, causing the leaf to be split in half
func insertIntoLeafAfterSplitting(root, leaf *bpNode, key string, pointer *entry) *bpNode {
// perform linear search to find index to insert new record
var insertionIndex int
for insertionIndex < defaultOrder-1 && Compare(leaf.keys[insertionIndex], key) == -1 {
insertionIndex++
}
var i, j int
var tempKeys [defaultOrder]string
var tempPointers [defaultOrder]unsafe.Pointer
// copy leaf keys and pointers to temp sets
// reserve space at insertion index for new record
for i, j = 0, 0; i < leaf.numKeys; i, j = i+1, j+1 {
if j == insertionIndex {
j++
}
tempKeys[j] = leaf.keys[i]
tempPointers[j] = leaf.pointers[i]
}
tempKeys[insertionIndex] = key
tempPointers[insertionIndex] = unsafe.Pointer(pointer)
leaf.numKeys = 0
// find pivot index where to split leaf
split := cut(defaultOrder - 1)
// overwrite original leaf up to the split point
for i = 0; i < split; i++ {
leaf.keys[i] = tempKeys[i]
leaf.pointers[i] = tempPointers[i]
leaf.numKeys++
}
// create new leaf
newLeaf := &bpNode{isLeaf: true} // makeLeaf()
// writing to new leaf from split point to end of original leaf pre split
for i, j = split, 0; i < defaultOrder; i, j = i+1, j+1 {
newLeaf.keys[j] = tempKeys[i]
newLeaf.pointers[j] = tempPointers[i]
newLeaf.numKeys++
}
// free temps
for i = 0; i < defaultOrder; i++ {
tempKeys[i] = *new(string) // zero Value
tempPointers[i] = nil // zero Value
}
newLeaf.pointers[defaultOrder-1] = leaf.pointers[defaultOrder-1]
leaf.pointers[defaultOrder-1] = unsafe.Pointer(newLeaf)
for i = leaf.numKeys; i < defaultOrder-1; i++ {
leaf.pointers[i] = nil
}
for i = newLeaf.numKeys; i < defaultOrder-1; i++ {
newLeaf.pointers[i] = nil
}
newLeaf.parent = leaf.parent
newKey := newLeaf.keys[0]
return insertIntoParent(root, leaf, newKey, newLeaf)
}
/*
* findRecord finds and returns the Record to which a key refers
*/
func (t *BPTree) find(key string) (*bpNode, *entry) {
leaf := findLeaf(t.root, key)
if leaf == nil {
return nil, nil
}
/*
* If root/leaf != nil, leaf must have a Value, even if it does not contain the desired key.
* The leaf holds the range of keys that would include the desired key
*/
var i int
for i = 0; i < leaf.numKeys; i++ {
if Equal(leaf.keys[i], key) {
break
}
}
if i == leaf.numKeys {
return leaf, nil
}
return leaf, (*entry)(leaf.pointers[i])
}
/*
* findEntry finds and returns the entry to which a key refers
*/
func (t *BPTree) findEntry(key string) *entry {
leaf := findLeaf(t.root, key)
if leaf == nil {
return nil
}
/*
* If root/leaf != nil, leaf must have a Value, even if it does not contain the desired key.
* The leaf holds the range of keys that would include the desired key
*/
var i int
for i = 0; i < leaf.numKeys; i++ {
if Equal(leaf.keys[i], key) {
break
}
}
if i == leaf.numKeys {
return nil
}
return (*entry)(leaf.pointers[i])
}
/*
* findLeaf traces the path from the root to a leaf, searching by key and displaying information about the path if the
* verbose flag is set--findLeaf returns the leaf containing the given key
*/
func findLeaf(root *bpNode, key string) *bpNode {
if root == nil {
return root
}
i, c := 0, root
for !c.isLeaf {
i = 0
for i < c.numKeys {
if Compare(key, c.keys[i]) >= 0 {
i++
} else {
break
}
}
c = (*bpNode)(c.pointers[i])
}
// c is the found leaf bpNode
return c
}
/*
* findFirstLeaf traces the path from the root to the leftmost leaf in the tree
*/
func findFirstLeaf(root *bpNode) *bpNode {
if root == nil {
return root
}
c := root
for !c.isLeaf {
c = (*bpNode)(c.pointers[0])
}
return c
}
func findLastLeaf(root *bpNode) *bpNode {
if root == nil {
return root
}
c := root
for !c.isLeaf {
c = (*bpNode)(c.pointers[c.numKeys])
}
return c
}
/*
* nextLeaf returns the next non-nil leaf in the chain (to the right) of the current leaf
*/
func (n *bpNode) nextLeaf() *bpNode {
if p := (*bpNode)(n.pointers[defaultOrder-1]); p != nil && p.isLeaf {
return p
}
return nil
}
/*
* delete is the master delete function
*/
func (t *BPTree) delete(key string) *entry {
var old *entry
keyLeaf, keyEntry := t.find(key)
if keyEntry != nil && keyLeaf != nil {
t.root = deleteEntry(t.root, keyLeaf, key, unsafe.Pointer(keyEntry))
old = keyEntry
keyEntry = nil
}
return old // return the old entry we just deleted
}
/*
* getNeighborIndex is a utility function for deletion. It gets the index of a bpNode's nearest
* sibling (that exists) to the left. If not then bpNode is already the leftmost child and (in
* such a case the bpNode) will return -1
*/
func getNeighborIndex(n *bpNode) int {
var i int
for i = 0; i <= n.parent.numKeys; i++ {
if (*bpNode)(n.parent.pointers[i]) == n {
return i - 1
}
}
log.Panicf("getNeighborIndex: Search for nonexistent pointer to bpNode in parent.\nNode: %#v\n", n)
return i
}
/*
* removeEntryFromNode does just that
*/
func removeEntryFromNode(n *bpNode, key string, pointer unsafe.Pointer) *bpNode {
/*
* Remove the key and shift the other keys accordingly
*/
var i, numPointers int
for !Equal(n.keys[i], key) {
i++
}
for i++; i < n.numKeys; i++ { // was for i+=1;
n.keys[i-1] = n.keys[i]
}
/*
* Remove the pointer and shift other pointers accordingly
*/
if n.isLeaf {
numPointers = n.numKeys
} else {
numPointers = n.numKeys + 1
}
i = 0
for n.pointers[i] != pointer {
i++
}
for i++; i < numPointers; i++ { // was for i+=1;
n.pointers[i-1] = n.pointers[i]
}
/*
* One key fewer
*/
n.numKeys--
/*
* Set the other pointers to nil for tidiness. A leaf uses
* the last pointer to point to the next leaf
*/
if n.isLeaf {
for i = n.numKeys; i < defaultOrder-1; i++ {
n.pointers[i] = nil
}
} else {
for i = n.numKeys + 1; i < defaultOrder; i++ {
n.pointers[i] = nil
}
}
return n
}
/*
* deleteEntry deletes and entry from the tree. Removes the Record and it's key and pointer from
* the leaf, and then makes all appropriate changes to preserve the tree's properties
*/
func deleteEntry(root, n *bpNode, key string, pointer unsafe.Pointer) *bpNode {
var minKeys, kPrimeIndex, capacity int
/*
* Remove key and pointer from bpNode
*/
n = removeEntryFromNode(n, key, pointer)
/*
* CASE: deletion from the root bpNode
*/
if n == root {
return adjustRoot(root)
}
/*
* CASE: deletion from a bpNode below the root (continue rest of function)
*
* Determine minimum allowable size of bpNode to be preserved after deletion
*/
if n.isLeaf {
minKeys = cut(defaultOrder - 1)
} else {
minKeys = cut(defaultOrder) - 1
}
/*
* CASE: bpNode stays at or above minimum (simple case)
*/
if n.numKeys >= minKeys {
return root
}
/*
* CASE: bpNode falls below minimum. Either coalescence or redistribution is needed
*
* Find the appropriate neighbor bpNode with which to coalesce. Also find the key (kPrime)
* in the parent between the pointer to bpNode n and the pointer to the neighbor
*/
neighborIndex := getNeighborIndex(n)
if neighborIndex == -1 {
kPrimeIndex = 0
} else {
kPrimeIndex = neighborIndex
}
kPrime := n.parent.keys[kPrimeIndex]
var neighbor *bpNode
if neighborIndex == -1 {
neighbor = (*bpNode)(n.parent.pointers[1])
} else {
neighbor = (*bpNode)(n.parent.pointers[neighborIndex])
}
if n.isLeaf {
capacity = defaultOrder
} else {
capacity = defaultOrder - 1
}
/*
* Coalescence
*/
if neighbor.numKeys+n.numKeys < capacity {
return coalesceNodes(root, n, neighbor, neighborIndex, kPrime)
}
/*S
* Redistribution
*/
return redistributeNodes(root, n, neighbor, neighborIndex, kPrimeIndex, kPrime)
}
/*
* adjustRoot does some magic in the root bpNode (not really)
*/
func adjustRoot(root *bpNode) *bpNode {
/*
* CASE: nonempty root. key and pointer have already been deleted, so nothing to be done
*/
if root.numKeys > 0 {
return root
}
/*
* CASE: empty root. If it has a child, promote the first (only) child as the new root
*/
var newRoot *bpNode
if !root.isLeaf {
newRoot = (*bpNode)(root.pointers[0])
newRoot.parent = nil
} else {
/*
* If it is a leaf (has no children), then the whole tree is in fact empty
*/
newRoot = nil // free
}
root = nil
return newRoot
}
/*
* coalesceNodes coalesces a bpNode that has become too small after deletion with a
* neighboring bpNode that can accept the additional entries without exceeing the maximum
*/
func coalesceNodes(root, n, neighbor *bpNode, neighborIndex int, kPrime string) *bpNode {
var tmp *bpNode
/*
* Swap neighbor with bpNode if bpNode is on the extreme left and neighbor is to it's right
*/
if neighborIndex == -1 {
tmp = n
n = neighbor
neighbor = tmp
}
/*
* Starting point in the neighbor for copying keys and pointers from n. Recall that n and
* neighbor have swapped places in the special case of n being a leftmost child
*/
neighborInsertionIndex := neighbor.numKeys
var i, j, nEnd int
/*
* CASE: Nonleaf bpNode. Append kPrime and the following pointer and append all pointers and keys from the neighbor
*/
if !n.isLeaf {
/*
* Append kPrime
*/
neighbor.keys[neighborInsertionIndex] = kPrime
neighbor.numKeys++
nEnd = n.numKeys
for i, j = neighborInsertionIndex+1, 0; j < nEnd; i, j = i+1, j+1 {
neighbor.keys[i] = n.keys[j]
neighbor.pointers[i] = n.pointers[j]
neighbor.numKeys++
n.numKeys--
}
/*
* The number of pointers is always one more than the number of keys
*/
neighbor.pointers[i] = n.pointers[j]
/*
* All children must now point up to the same parent
*/
for i = 0; i < neighbor.numKeys+1; i++ {
tmp = (*bpNode)(neighbor.pointers[i])
tmp.parent = neighbor
}
/*
* CASE: In a leaf, append the keys and pointers of n to the neighbor.
* Set the neighbor's last pointer to point to what had been n's right neighbor.
*/
} else {
for i, j = neighborInsertionIndex, 0; j < n.numKeys; i, j = i+1, j+1 {
neighbor.keys[i] = n.keys[j]
neighbor.pointers[i] = n.pointers[j]
neighbor.numKeys++
}
neighbor.pointers[defaultOrder-1] = n.pointers[defaultOrder-1]
}
root = deleteEntry(root, n.parent, kPrime, unsafe.Pointer(n))
n = nil // free
return root
}
/*
* redistributeNodes redistributes entries between two nodes when one has become too small
* after deletion but its neighbor is too big to append the small bpNode's entries without
* exceeding the maximum
*/
func redistributeNodes(root, n, neighbor *bpNode, neighborIndex, kPrimeIndex int, kPrime string) *bpNode {
var i int
var tmp *bpNode
/*
* CASE: n has a neighbor to the left. Pull the neighbor's last key-pointer pair over
* from the neighbor's right end to n's lef end
*/
if neighborIndex != -1 {
if !n.isLeaf {
n.pointers[n.numKeys+1] = n.pointers[n.numKeys]
}
for i = n.numKeys; i > 0; i-- {
n.keys[i] = n.keys[i-1]
n.pointers[i] = n.pointers[i-1]
}
if !n.isLeaf {
n.pointers[0] = neighbor.pointers[neighbor.numKeys]
tmp = (*bpNode)(n.pointers[0])
tmp.parent = n
neighbor.pointers[neighbor.numKeys] = nil
n.keys[0] = kPrime
n.parent.keys[kPrimeIndex] = neighbor.keys[neighbor.numKeys-1]
} else {
n.pointers[0] = neighbor.pointers[neighbor.numKeys-1]
neighbor.pointers[neighbor.numKeys-1] = nil
n.keys[0] = neighbor.keys[neighbor.numKeys-1]
n.parent.keys[kPrimeIndex] = n.keys[0]
}
/*
* CASE: n is the leftmost child. Take a key-pointer pair from the neighbor
* to the right. Move the neighbor's leftmost key-pointer pair to n's rightmost position
*/
} else {