-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.c
1198 lines (1041 loc) · 44.9 KB
/
btree.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define BT_MAX_CHILD 5
#define BT_MAX_NODE 4
#define BT_MIN_CHILD 3
#define BT_MIN_NODE 2
// v1: one BTree per unique memory_region_get_ram_ptr(mr)
// xlat is the actual index of the arr we'll search for.
// v1.5: store a byte (2tags) per key (i.e. xlat).
// Then, store_tag1, load_tag1 work similar to original code.
// qemu: 1 tag array, so, for us: one BTree...
// v2: one BTree,
#define ALLOC_FREED_ARR_SZ 10
char* global_btree_buffer; // 64-bit
long long int global_btree_buffer_first_avail; // 64-bit
int global_btree_root_offset;
int alloc_freed_nodes;
int alloc_freed_nodes_arr[ALLOC_FREED_ARR_SZ];
// total 43 bytes, rounded off to 44.
struct BTreeNode {
int keys[BT_MAX_NODE]; // stores upto 4 32-bit keys : 16byt
unsigned char tags[BT_MAX_NODE/2]; // stores a 4bit tag per key : 2byt
char count; // 8bits: bits[3:0]: #keys : 1byt
int children[BT_MAX_CHILD]; // stores upto 5 32-bit offsets. : 20byt
int parent; // stores 32-bit parent ptr offset. : 4byt
};
// struct BTreeNode *root;
// v2: Assuming global_btree_buffer is the root ptr (cast)
// There can NEVER be duplicate keys!
void printIntArr(int* arr, int len)
{
for (int x = 0; x < len; x++)
printf("arr[%i] : %i, ", x, arr[x]);
printf("\n");
}
void printCharArr(unsigned char* arr, int len)
{
for (int x = 0; x < len; x++)
printf("arr[%i] : %i, ", x, arr[x]);
printf("\n");
}
void printBTreeNode(struct BTreeNode* node)
{
printf("NODE!!! Offset: %li , parent_offset: %i , count: %i \n", ((char*)node-global_btree_buffer), node->parent, node->count);
printIntArr(node->keys, BT_MAX_NODE);
printCharArr(node->tags, BT_MAX_NODE/2);
printIntArr(node->children, BT_MAX_CHILD);
}
void printBTree(struct BTreeNode* rnode, int level)
{
printf("LEVEL %i : ", level);
printBTreeNode(rnode);
for (int i = 0; i <= rnode->count; i++)
if (rnode->children[i] != -1)
printBTree((struct BTreeNode*)(global_btree_buffer + rnode->children[i]), level+1);
}
struct BTreeNode* getGlobalRootPtr()
{
return (struct BTreeNode*) (global_btree_buffer + global_btree_root_offset);
}
void initBTreeNode(struct BTreeNode* rnode, int parent_offset)
{
rnode->count = 0;
for (int i = 0; i < BT_MAX_NODE; i++)
rnode->keys[i] = -1;
for (int i = 0; i < BT_MAX_CHILD; i++)
rnode->children[i] = -1; // null ptr
rnode->parent = parent_offset;
}
void initBTree(char* global_btree_buffer)
{
// make a new BTreeNode, with key=0,tag=0. []
struct BTreeNode* root = (struct BTreeNode*) global_btree_buffer;
initBTreeNode(root, -1); // parent = null ptr
root->count = 1;
root->keys[0] = 0;
global_btree_buffer_first_avail += sizeof(struct BTreeNode);
global_btree_root_offset = 0;
alloc_freed_nodes = 0; // tracks fragmentation.
printf("##### FINISHED initBTree: root ptr: %p, new first_avail: %lli, global_btree_root_offset: %i, alloc_freed_nodes: %i \n", root, global_btree_buffer_first_avail, global_btree_root_offset, alloc_freed_nodes);
}
unsigned char getArrIthTag(unsigned char* tag_arr, int i)
{
return (i%2 == 1) ? ((tag_arr[i/2])%16) : (tag_arr[i/2] >> 4) ;
}
unsigned char setArrIthTag(unsigned char* tag_arr, int i, unsigned char tag)
{
if (tag >= 16)
{
printf("ERRORRRRR! setArrIthTag called with invalid tag value!!! %i \n", tag);
// throw;
}
if (i%2 == 1)
tag_arr[i/2] = (tag_arr[i/2] & 0xf0) | tag;
else
tag_arr[i/2] = (tag_arr[i/2] & 0x0f) | (tag << 4);
printf("Done with setArrIthTag [inputs: i: %i, tag: %i] new i/2th tag byte: %i \n", i, tag, tag_arr[i/2]);
}
// leaf if all children are -1 (i.e. null ptr)
bool isBTreeNodeLeaf(struct BTreeNode* root)
{
for (int i = 0; i < BT_MAX_CHILD; i++)
if (root->children[i] != -1)
return false;
return true;
}
// if there's any child that has the wrong parent ptr, returns false.
bool verifyBTreePtrs(struct BTreeNode* root)
{
int root_offset = (char*) root - global_btree_buffer;
for (int i = 0; i <= root->count; i++)
{
if (root->children[i] != -1)
{
struct BTreeNode* r_child_i = (struct BTreeNode*) (global_btree_buffer + root->children[i]);
if (r_child_i->parent != root_offset)
return false;
if (!verifyBTreePtrs(r_child_i))
return false;
}
}
return true;
}
// get ptr (in terms of offset from root addr) to node containing largest key <= addr.
// note that since this func is called for random ptrs in BTree, the root input is NOT the absolute root of tree.
// Hence, child ptr = global buffer ptr + offset.
int getLargestNodeLessThan(struct BTreeNode* root, int addr)
{
if (!root)
return -1;
int i=0;
while ((i < root->count) && (root->keys[i] < addr) )
i++;
// case1: i = root->count: ans is in the right most child OR root node.
if (i >= root->count)
{
if ( (i < BT_MAX_CHILD) && (root->children[i] != -1) )
{
int ans_in_right_child = getLargestNodeLessThan( (struct BTreeNode*)(global_btree_buffer + root->children[i]), addr );
return (ans_in_right_child == -1) ? ((char*)root - global_btree_buffer) : ans_in_right_child;
}
else
return ((char*)root - global_btree_buffer); // the current root node has the largest key<=addr.
}
// case2: keys[i] = addr: return root node!
if (root->keys[i] == addr)
return ((char*)root - global_btree_buffer);
// case3: keys[i] > addr: go to the just-left child of ith key, i.e. children[i] OR root node.
if (root->keys[i] > addr)
{
int ans_in_left_child = -1;
if ( (i < BT_MAX_CHILD) && (root->children[i] != -1) )
ans_in_left_child = getLargestNodeLessThan( (struct BTreeNode*)(global_btree_buffer + root->children[i]), addr );
return (ans_in_left_child == -1) ? ( (i == 0) ? -1 : ((char*)root - global_btree_buffer) ) : ans_in_left_child;
}
}
// get ptr (in terms of offset from root addr) to node containing smallest key >= addr.
// note that since this func is called for random ptrs in BTree, the root input is NOT the absolute root of tree.
// Hence, child ptr = global buffer ptr + offset.
int getSmallestNodeMoreThan(struct BTreeNode* root, int addr)
{
if (!root)
return -1;
int i=(root->count)-1;
while ( (i >= 0) && (root->keys[i] >= addr))
i--;
// i < (root->ct)-1 implies there's atleast one elem in root which is >= addr.
// case1: i = -1, i.e. all elems in array are >= addr
if (i <= -1)
{
if (root->children[0] != -1)
{
int ans_in_left_child = getSmallestNodeMoreThan( (struct BTreeNode*)(global_btree_buffer+root->children[0]) , addr);
return (ans_in_left_child == -1) ? ((char*) root - global_btree_buffer) : ans_in_left_child;
}
else
return ((char*)root - global_btree_buffer); // the current root node has the smallest key>=addr.
}
// case2:
if (root->keys[i] == addr)
return ((char*)root - global_btree_buffer);
// case3: all elems including keys[i] are < addr. check right child of keys[i]
if (root->keys[i] < addr)
{
int ans_in_right_child = -1;
if (root->children[i+1] != -1)
ans_in_right_child = getSmallestNodeMoreThan((struct BTreeNode*)(global_btree_buffer+root->children[i+1]) , addr);
return (ans_in_right_child == -1) ? ((i == (root->count-1)) ? -1 : ((char*) root - global_btree_buffer) ) : ans_in_right_child;
}
}
void getLargestKeyTagLessThan(struct BTreeNode* root, int addr, int* x_key, int* x_tag)
{
int i=0;
while ((i < root->count) && (root->keys[i] < addr) )
i++;
if (i >= root->count)
{
*x_key = root->keys[i-1];
*x_tag = getArrIthTag(root->tags, i-1);
}
else if (root->keys[i] == addr)
{
*x_key = root->keys[i];
*x_tag = getArrIthTag(root->tags, i);
}
else if (root->keys[i] > addr)
{
if (i == 0)
{
printf("IN getLargestKeyTagLessThan: 1st Elem in root is > addr!!! \n");
*x_key = -1;
*x_tag = -1;
// throw;
}
*x_key = root->keys[i-1];
*x_tag = getArrIthTag(root->tags, i-1);
}
else
{
printf("IN getLargestKeyTagLessThan: WEIRD ERROR!!!! \n");
// throw;
}
}
void getSmallestKeyTagMoreThan(struct BTreeNode* root, int addr, int* x_key, int* x_tag)
{
int i=(root->count)-1;
while ((i >= 0) && (root->keys[i] > addr) )
i--;
if (i < 0) // all elems in root are >= addr
{
*x_key = root->keys[0];
*x_tag = getArrIthTag(root->tags, 0);
}
else if (root->keys[i] == addr)
{
*x_key = root->keys[i];
*x_tag = getArrIthTag(root->tags, i);
}
else if (root->keys[i] < addr)
{
if (i == (root->count - 1))
{
printf("IN getSmallestKeyTagMoreThan: last elem is < addr!!! \n");
*x_key = -1;
*x_tag = -1;
// throw;
}
*x_key = root->keys[i+1];
*x_tag = getArrIthTag(root->tags, i+1);
}
else
{
printf("IN getSmallestKeyTagMoreThan: WEIRD ERROR!!!! \n");
// throw;
}
}
// adding 1 key,tag & 1 child to node
void insertBTreeKeyInNode(struct BTreeNode* rnode, int key, int tag, bool right_child, int child_offset)
{
// find where to insert
int i = 0;
while ((i < rnode->count) && (rnode->keys[i] < key) )
i++;
// insert key at rnode->keys[i]
// shift tags,keys,children
int curr = key;
int curr_tag = tag;
int curr_child = child_offset;
while (i <= rnode->count)
{
int new_curr = rnode->keys[i];
int new_curr_tag = getArrIthTag(rnode->tags, i);
int new_curr_child = (right_child) ? rnode->children[i+1] : rnode->children[i];
rnode->keys[i] = curr;
setArrIthTag(rnode->tags, i, curr_tag);
if (right_child)
rnode->children[i+1] = curr_child;
else
rnode->children[i] = curr_child;
curr = new_curr;
curr_tag = new_curr_tag;
curr_child = new_curr_child;
i++;
}
if (!right_child)
rnode->children[i] = curr_child;
if (child_offset != -1)
{
struct BTreeNode* child_ptr = (struct BTreeNode*) (global_btree_buffer + child_offset);
child_ptr->parent = (char*)rnode - global_btree_buffer;
}
rnode->count += 1;
}
// deletes key&tag from rnode - i.e. updates keys, count, children.
// equivalent to removeVal func in programiz code.
// rebalanceBTree calls this for non-leaf nodes, so,
// del_child: if 0, delete left child of key, else delete right child of key.
bool deleteBTreeKeyFromNode(struct BTreeNode* rnode, int key, int del_child)
{
int i = 0;
while ((i < rnode->count) && (rnode->keys[i] != key))
i++;
while (i < (rnode->count-1))
{
// update tags, keys, children:
rnode->keys[i] = rnode->keys[i+1];
setArrIthTag(rnode->tags, i, getArrIthTag(rnode->tags, i+1));
// children:
if (del_child == 0)
rnode->children[i] = rnode->children[i+1];
else
rnode->children[i+1] = rnode->children[i+2];
i++;
}
rnode->keys[rnode->count - 1] = -1; // just to avoid confusion when printing/reading BTree node
// set last tag to 0
setArrIthTag(rnode->tags, i, 0);
// move last child, extra move if del_left_child.
if (del_child == 0)
rnode->children[i] = rnode->children[i+1];
rnode->count -= 1;
return true;
}
// v1: return ptr to the byte containing the tag
// v2: get tag of addr granule, equiv to TagArr[addr]
// NOTE: ALWAYS CALL this func with root as the global root ptr.
unsigned char getTag(struct BTreeNode* root, int addr)
{
struct BTreeNode* groot = (struct BTreeNode*) (global_btree_buffer + global_btree_root_offset);
if (!root)
return -1;
int node_with_largest_less_than_off = getLargestNodeLessThan(getGlobalRootPtr(), addr);
struct BTreeNode* node_with_largest_less_than = (struct BTreeNode*) (global_btree_buffer + node_with_largest_less_than_off);
// find the largest key <= addr in the tree
int i=0;
while ((i < node_with_largest_less_than->count) && (node_with_largest_less_than->keys[i] < addr) )
i++;
// case1: i = root->count: go to the right most child OR Null.
if (i >= node_with_largest_less_than->count)
{
unsigned char node_last_tag = getArrIthTag(node_with_largest_less_than->tags, i-1);
return node_last_tag;
/* Old:
// if ( (i < BT_MAX_CHILD) && root->children[i])
// {
// char rightmostC_tag = getTag(root->children[i], addr);
// return (rightmostC_tag != -1) ? rightmostC_tag : node_last_tag;
// }
// else
// return node_last_tag; // root->keys[count-1] is the largest node <= addr. */
}
// case2: keys[i] = addr: return ith tag!
if (node_with_largest_less_than->keys[i] == addr)
return getArrIthTag(node_with_largest_less_than->tags, i); // (i%2 == 1) ? ((root->tags[i/2])%16) : (root->tags[i/2] >> 4); // &(root->tags[i]);
// case3: keys[i] > addr: go to the just-left child of ith key, i.e. children[i]
if (node_with_largest_less_than->keys[i] > addr)
{
// TODO: its possible that all elems in this child are > addr.
if (i == 0)
{
printf("ERRORRR: No ELEMENT in tree <= addr!!!");
// throw;
}
unsigned char node_last_tag = getArrIthTag(node_with_largest_less_than->tags, i-1);
return node_last_tag;
// Old code:
// char leftC_tag = getTag(root->children[i], addr);
// return (leftC_tag != -1) ? leftC_tag : node_last_tag;
}
}
// re-balance, starting from a deficient leaf node:
void rebalanceBTree(struct BTreeNode* rnode)
{
struct BTreeNode* rleaf = rnode;
int iter_ct = 0;
while (iter_ct < 20)
{
int rleaf_offset = (char*) rleaf - global_btree_buffer;
printf("IN rebalanceBTree: for node %p, parent: %i \n", rleaf, rleaf->parent);
if (rleaf->parent != -1)
{
struct BTreeNode* rleaf_parent = (struct BTreeNode*) (global_btree_buffer + rleaf->parent);
int i = 0;
while ((i <= rleaf_parent->count) && (rleaf_parent->children[i] != rleaf_offset))
i++;
struct BTreeNode* rleaf_left_bro = NULL;
struct BTreeNode* rleaf_right_bro = NULL;
int rleaf_left_bro_offset = -1, rleaf_right_bro_offset = -1;
// check right sibling:
if ((i < rleaf_parent->count) && (rleaf_parent->children[i+1] != -1))
{
rleaf_right_bro_offset = rleaf_parent->children[i+1];
rleaf_right_bro = (struct BTreeNode*) (global_btree_buffer + rleaf_parent->children[i+1]);
if (rleaf_right_bro->count > BT_MIN_NODE)
{
// separator key moves to rleaf
rleaf->keys[rleaf->count] = rleaf_parent->keys[i];
setArrIthTag(rleaf->tags, rleaf->count, getArrIthTag(rleaf_parent->tags, i));
rleaf->count += 1;
// use rleaf_right_bro->children[0] as the last child of rleaf:
rleaf->children[rleaf->count] = rleaf_right_bro->children[0];
if (rleaf_right_bro->children[0] != -1)
{
struct BTreeNode* rl_right_bro_c0 = (struct BTreeNode*) (global_btree_buffer + rleaf_right_bro->children[0]);
rl_right_bro_c0->parent = rleaf_offset;
}
// delete left most key from rleaf_right_bro,
// use it to replace separator_key in rleaf_parent.
rleaf_parent->keys[i] = rleaf_right_bro->keys[0];
setArrIthTag(rleaf_parent->tags, i, getArrIthTag(rleaf_right_bro->tags, 0));
deleteBTreeKeyFromNode(rleaf_right_bro, rleaf_right_bro->keys[0], 0); // ensures we delete left child of key[0], i.e. child[0]
printf("IN rebalanceBTree: rotated with rleaf_right_bro: %p", rleaf_right_bro);
printBTreeNode(rleaf);
printBTreeNode(rleaf_right_bro);
printBTreeNode(rleaf_parent);
return;
}
}
// check if can rotate with left sibling:
if ((i > 0) && (rleaf_parent->children[i-1] != -1))
{
rleaf_left_bro_offset = rleaf_parent->children[i-1];
rleaf_left_bro = (struct BTreeNode*) (global_btree_buffer + rleaf_parent->children[i-1]);
if (rleaf_left_bro->count > BT_MIN_NODE)
{
// separator key moves to rleaf TODO: this will move at rleaf->keys[0], slide all existing keys/children/tags:
insertBTreeKeyInNode(rleaf, rleaf_parent->keys[i-1], getArrIthTag(rleaf_parent->tags,i-1), 0, rleaf_left_bro->children[rleaf_left_bro->count]);
// rleaf->keys[rleaf->count] = rleaf_parent->keys[i-1];
// setArrIthTag(rleaf->tags, rleaf->count, getArrIthTag(rleaf_parent->tags, i-1));
// rleaf->count += 1;
// make rleaf_left_bro's last child, the first child of rleaf: [Doing this as part of insertBTreeKeyInNode]
// rleaf->children[0] = rleaf_left_bro->children[rleaf_left_bro->count];
// delete rightmost key from rleaf_left_bro, use it to replace separator_key in rleaf_parent.
rleaf_parent->keys[i-1] = rleaf_left_bro->keys[rleaf_left_bro->count-1];
setArrIthTag(rleaf_parent->tags, i-1, getArrIthTag(rleaf_left_bro->tags, rleaf_left_bro->count-1));
deleteBTreeKeyFromNode(rleaf_left_bro, rleaf_left_bro->keys[rleaf_left_bro->count-1], 1 ); // deleting right child of last key
printf("IN rebalanceBTree: rotated with rleaf_right_bro: %p", rleaf_left_bro);
printBTreeNode(rleaf);
printBTreeNode(rleaf_left_bro);
printBTreeNode(rleaf_parent);
return;
}
}
// Need to merge: [no sibling with >min_keys]
// put separator, rleaf->keys in either left_bro or right_bro.
if (rleaf_left_bro != NULL)
{
// put separator in rleaf_left_bro
rleaf_left_bro->keys[rleaf_left_bro->count] = rleaf_parent->keys[i-1];
setArrIthTag(rleaf_left_bro->tags, rleaf_left_bro->count, getArrIthTag(rleaf_parent->tags,i-1));
rleaf_left_bro->count += 1;
// put rleaf->keys in rleaf_left_bro
for (int rx = 0; rx < rleaf->count; rx++)
{
rleaf_left_bro->keys[rleaf_left_bro->count] = rleaf->keys[rx];
setArrIthTag(rleaf_left_bro->tags, rleaf_left_bro->count, getArrIthTag(rleaf->tags,rx));
rleaf_left_bro->children[rleaf_left_bro->count] = rleaf->children[rx];
if (rleaf->children[rx] != -1)
{
struct BTreeNode* rleaf_child_rx = (struct BTreeNode*) (global_btree_buffer + rleaf->children[rx]);
rleaf_child_rx->parent = rleaf_left_bro_offset;
}
rleaf_left_bro->count += 1;
}
rleaf_left_bro->children[rleaf_left_bro->count] = rleaf->children[rleaf->count];
if (rleaf->children[rleaf->count] != -1)
{
struct BTreeNode* rleaf_child_last = (struct BTreeNode*) (global_btree_buffer + rleaf->children[rleaf->count]);
rleaf_child_last->parent = rleaf_left_bro_offset;
}
// delete separator from parent: delete right child of this key
deleteBTreeKeyFromNode(rleaf_parent, rleaf_parent->keys[i-1], 1); // rleaf_parent is a non leaf node!!!
printf("IN rebalanceBTree: Merged with rleaf_left_bro: %p \n", rleaf_left_bro);
printBTreeNode(rleaf_left_bro);
printBTreeNode(rleaf_parent);
}
else if (rleaf_right_bro != NULL)
{
// Done: Handle children here!! [insertBTreeKeyInNode handles it]
// children rleaf_right_bro: key_ct += 1+rleaf->ct, child_ct += 0+rleaf->ct+1. Works!
// put rleaf's keys in rleaf_right_bro
for (int lx = 0; lx < rleaf->count; lx++)
insertBTreeKeyInNode(rleaf_right_bro, rleaf->keys[lx], getArrIthTag(rleaf->tags,lx), 0, rleaf->children[lx]);
// put separator in rleaf_right_bro
insertBTreeKeyInNode(rleaf_right_bro, rleaf_parent->keys[i], getArrIthTag(rleaf_parent->tags, i), 0, rleaf->children[rleaf->count]);
// delete separator from parent: delete left child of this key
deleteBTreeKeyFromNode(rleaf_parent, rleaf_parent->keys[i], 0);
printf("IN rebalanceBTree: Merged with rleaf_right_bro: %p \n", rleaf_right_bro);
printBTreeNode(rleaf_right_bro);
printBTreeNode(rleaf_parent);
}
else
{
printf("ERRORRR!!! Both left, right bros are NULL!! \n");
// throw;
}
// 'delete' rleaf node.
alloc_freed_nodes_arr[alloc_freed_nodes%ALLOC_FREED_ARR_SZ] = rleaf_offset;
alloc_freed_nodes += 1;
printf("IN rebalanceBTree: UPDATED alloc_freed_nodes to %i \n", alloc_freed_nodes);
printIntArr(alloc_freed_nodes_arr, ALLOC_FREED_ARR_SZ);
// Call re-balance on rleaf_parent, if needed
if (rleaf_parent->count == 0)
{
if (rleaf_parent->parent != -1)
{
printf("ERRORRR: SOMEHOW NON-ROOT node is EMPTY!!!!");
// throw;
}
else
{
// make rleaf_left/right_bro as the new root.
struct BTreeNode* new_root = (rleaf_left_bro != NULL) ? rleaf_left_bro : rleaf_right_bro;
new_root->parent = -1;
global_btree_root_offset = (char*) new_root - global_btree_buffer;
alloc_freed_nodes_arr[alloc_freed_nodes%ALLOC_FREED_ARR_SZ] = global_btree_root_offset;
alloc_freed_nodes += 1;
printf("IN rebalanceBTree: JUST MODIFIED the global_btree_root_offset to %i !!!! \n", global_btree_root_offset);
return;
}
}
if (rleaf_parent->count < BT_MIN_NODE)
rleaf = rleaf_parent;
else
return; // WE ARE DONE!!! [Update: Mar24] If we dont return, we'll recurse again on rleaf (which is deleted actually!)
}
else
{
// rleaf is the root Node. Its okay for it to be deficient.
printf("ROOT has %i keys, < min %i \n", rleaf->count, BT_MIN_NODE);
if (rleaf->count == 0)
{
printf("ERRORRR!!!! Somehow ROOT is empty, and we called rebalance!! \n");
// throw; // this should never happen!
}
return;
}
iter_ct++;
}
printf("ERRORRRRRR!!!!!! RECURSION IN deleteBTreeKey MORE THAN 20 TIMES!!!!");
}
// deletes key from rnode, and tries to balance the BTree
bool deleteBTreeKey(struct BTreeNode* rnode, int key)
{
if (isBTreeNodeLeaf(rnode))
{
deleteBTreeKeyFromNode(rnode, key, 1);
if (rnode->count < BT_MIN_NODE)
{
// re-balance
rebalanceBTree(rnode);
}
}
else
{
int i = 0;
while ((i < rnode->count) && (rnode->keys[i] != key))
i++;
// Need to find a replacement for this key: largest key' < key OR smallest key' > key
// node with smallest elem >= key.
int smallest_node_more_key_off = getSmallestNodeMoreThan( (struct BTreeNode*)(global_btree_buffer + rnode->children[i+1]), key );
struct BTreeNode* smallest_node_more_key = (struct BTreeNode*) (global_btree_buffer + smallest_node_more_key_off);
// replacement should be in a leaf.
if (!isBTreeNodeLeaf(smallest_node_more_key))
{
printf("IN deleteBTreeKey: ERRORRRR!!!! smallestNodeMoreThan key %i is NOT a leaf!!! \n", key);
// throw;
}
// remove the replacement (key,tag) from its leaf and put it in rnode (at ith place)
int sj = smallest_node_more_key->count-1;
while ( (sj >= 0) && (smallest_node_more_key->keys[sj] > key) )
sj--;
printf("IN deleteBTreeKey: Deleting %i from node %p, borrowing key %i from leaf_ptr %p \n", key, rnode, smallest_node_more_key->keys[sj+1], smallest_node_more_key);
rnode->keys[i] = smallest_node_more_key->keys[sj+1]; // this was previously key, replacing with smallest > key.
setArrIthTag(rnode->tags, i, getArrIthTag(smallest_node_more_key->tags, sj+1) );
deleteBTreeKeyFromNode(smallest_node_more_key, smallest_node_more_key->keys[sj+1], 1); // note smallest_node_more_key is a leaf
printBTreeNode(rnode);
printBTreeNode(smallest_node_more_key);
// if leaf count < BT_MIN_NODE
if (smallest_node_more_key->count < BT_MIN_NODE)
{
// re-balance
rebalanceBTree(smallest_node_more_key);
}
}
}
int findLeafNodeToInsertAddr(struct BTreeNode* rnode, int addr)
{
if (isBTreeNodeLeaf(rnode))
return (char*)rnode - global_btree_buffer;
int i = 0;
while (i < rnode->count && (rnode->keys[i] < addr) )
i++;
if ( rnode->children[i] == -1)
{
printf("In findLeafNodeToInsertAddr: ERROR!!! Non leaf node (count=%i) has null child (%ith child: %i)!!! \n", rnode->count, i, rnode->children[i]);
// throw;
}
else
return findLeafNodeToInsertAddr( (struct BTreeNode*)(global_btree_buffer + rnode->children[i]), addr );
// leftmost / rightmost / middle child:
// if (i == rnode->count)
// return findLeafNodeToInsertAddr( global_btree_buffer + rnode->children[i], addr);
// else if (i == 0)
// return findLeafNodeToInsertAddr( global_btree_buffer + rnode->children[0], addr);
}
// splits rnode->keys + insert_addr array.
// returns offset for new node, and writes median_key to the ptr, and tag for median key to the ptr.
// ASSUMES rnode has BT_MAX_NODE keys!!!
// Note: need to pass new child ptr too, since we need it to correctly update children of new_node_right that we make here:
int splitNode(struct BTreeNode* rnode, int insert_addr, int insert_tag, int insert_child_right_offset, int* pmedian_key, unsigned char* pmedian_tag)
{
if (rnode->count < BT_MAX_NODE)
{
printf("SPLITNODE CALLED for NON-FULL node!!!");
// throw;
}
// find median, split.
int i = 0;
while ( (i < rnode->count) && (rnode->keys[i] < insert_addr) )
i++;
// ith elem would be addr, elems i - count will be shifted to right.
int median_id = (rnode->count + 1)/2;
int median_key = (median_id < i) ? rnode->keys[median_id] : ( (median_id == i) ? insert_addr : (rnode->keys[median_id-1]) ) ;
// make new arr with rnode->count + 1 elems. Will make updating rnode, new_node_right MUCH easier.
int full_arr[rnode->count+1];
unsigned char full_tag_arr[ (int)ceil((rnode->count+1)/2.0) ]; // Note: important to divide by 2.0, o.w. ct/2 was int division.
int full_children_arr[rnode->count+2];
// printf("rnode->count %i, %f, ceil((rnode->count+1)/2): %f, int_cast: %i \n", rnode->count, (rnode->count+1)/2.0, ceil((rnode->count+1)/2.0), (int)ceil((rnode->count+1)/2.0));
for (int xx = 0; xx < (int)ceil((rnode->count+1)/2.0); xx++)
full_tag_arr[xx] = 0;
// printf("full_tag_arr[%i] : %i \n", xx, full_tag_arr[xx]);
for (int j = 0; j < i; j++)
{
full_arr[j] = rnode->keys[j];
setArrIthTag(full_tag_arr, j, getArrIthTag(rnode->tags, j) );
full_children_arr[j] = rnode->children[j];
}
full_arr[i] = insert_addr;
setArrIthTag(full_tag_arr, i, insert_tag);
full_children_arr[i] = rnode->children[i]; // note that this child would've been split in a previous recursive call.
full_children_arr[i+1] = insert_child_right_offset; // the new_node_right made by previous recursive call
for (int j = i; j < rnode->count; j++)
{
full_arr[j+1] = rnode->keys[j];
setArrIthTag(full_tag_arr, j+1, getArrIthTag(rnode->tags, j) );
full_children_arr[j+2] = rnode->children[j+1];
}
// update rnode keys & count & children:
// We DO need to update full_chil_arr[0 - median_id]->parent to rnode?
// median_id is always <= rnode->count, but we dont know where insert_addr, insert_child_right_offset are in the full_arr!
for (int j = 0; j < BT_MAX_CHILD; j++)
{
if (j<median_id)
{
rnode->keys[j] = full_arr[j];
setArrIthTag(rnode->tags, j, getArrIthTag(full_tag_arr, j) );
rnode->children[j] = full_children_arr[j];
}
else if (j == median_id)
rnode->children[j] = full_children_arr[j];
else
rnode->children[j] = -1; // null ptrs
// Update children's parent to rnode, for j <= median_id:
if ((j <= median_id) && (full_children_arr[j] != -1))
{
struct BTreeNode* full_child_j = (struct BTreeNode*) (global_btree_buffer + full_children_arr[j]);
full_child_j->parent = (char*)rnode - global_btree_buffer;
}
}
rnode->count = median_id;
// update the return values:
*pmedian_key = full_arr[median_id];
*pmedian_tag = getArrIthTag(full_tag_arr, median_id);
// Split rnode: elems>median_key in new node.
struct BTreeNode* new_node_right = (struct BTreeNode*) (global_btree_buffer + global_btree_buffer_first_avail);
int new_node_right_offset = global_btree_buffer_first_avail;
global_btree_buffer_first_avail += sizeof(struct BTreeNode);
// new_node_right->count = rnode->count / 2;
// add keys, tags, children to new_node_right.
for (int j = 0; j < BT_MAX_CHILD; j++)
{
int jth_id = j+median_id+1;
struct BTreeNode* new_node_right_child = NULL;
if (full_children_arr[jth_id] != -1)
new_node_right_child = (struct BTreeNode*) (global_btree_buffer + full_children_arr[jth_id]);
if (jth_id < (BT_MAX_NODE+1))
{
new_node_right->keys[j] = full_arr[jth_id];
setArrIthTag(new_node_right->tags, (j), getArrIthTag(full_tag_arr, jth_id) );
new_node_right->children[j] = full_children_arr[jth_id];
if (new_node_right_child)
new_node_right_child->parent = new_node_right_offset;
new_node_right->count += 1;
}
else if ( jth_id == (BT_MAX_NODE + 1) )
{
new_node_right->children[j] = full_children_arr[jth_id]; // last elem of full_children_arr
if (new_node_right_child)
new_node_right_child->parent = new_node_right_offset;
}
else
new_node_right->children[j] = -1; // NULL ptr
}
// update new_node_right's parent ptr:
new_node_right->parent = rnode->parent;
printf("SPLIT node ptr %p (%li) [orig ct: %i, new ct: %i] [inserting addr: %i, median_key: %i (full_arr[%i]: %i), new_node_right ptr: %p (%li), count: %i] \n", rnode, (char*)rnode - global_btree_buffer, BT_MAX_NODE, rnode->count, insert_addr, median_key, median_id, full_arr[median_id], new_node_right, (char*)new_node_right - global_btree_buffer, new_node_right->count);
printBTreeNode(rnode);
printBTreeNode(new_node_right);
return ((char*)new_node_right - global_btree_buffer);
}
bool insertBTreeKey(int addr, unsigned char tag)
{
// find the leaf where we'll insert addr:
int leaf_to_insert_offset = findLeafNodeToInsertAddr(getGlobalRootPtr(), addr);
struct BTreeNode* leaf_to_insert = (struct BTreeNode*) (global_btree_buffer + leaf_to_insert_offset);
printf("IN insertBTreeKey: addr: %i, tag: %i, leaf_to_insert_offset: %i \n", addr, tag, leaf_to_insert_offset);
// we recurse, and update insert_addr, insert_tag, insert_child_right, node_to_insert in each iteration:
int insert_addr = addr;
int insert_tag = tag;
int insert_child_right_offs = -1;
struct BTreeNode* node_to_insert = leaf_to_insert;
while (true)
{
if (node_to_insert->count < BT_MAX_NODE)
{
// just insert in this leaf & done
int i = 0;
while ( (i < node_to_insert->count) && (node_to_insert->keys[i] < insert_addr) )
i++;
if (i == node_to_insert->count)
{
node_to_insert->keys[i] = insert_addr;
node_to_insert->children[i+1] = insert_child_right_offs;
setArrIthTag(node_to_insert->tags, i, insert_tag);
}
else
{
// move all other elems [keys, tags, children] to right.
int curr = node_to_insert->keys[i];
int curr_tag = getArrIthTag(node_to_insert->tags, i);
int curr_child = node_to_insert->children[i+1];
// put insert_addr at i:
node_to_insert->keys[i] = insert_addr;
setArrIthTag(node_to_insert->tags, i, insert_tag);
node_to_insert->children[i+1] = insert_child_right_offs;
i++;
while (i <= node_to_insert->count)
{
int new_curr = node_to_insert->keys[i];
int new_curr_tag = getArrIthTag(node_to_insert->tags, i);
int new_curr_child = node_to_insert->children[i+1];
node_to_insert->keys[i] = curr;
setArrIthTag(node_to_insert->tags, i, curr_tag);
node_to_insert->children[i+1] = curr_child;
curr = new_curr;
curr_tag = new_curr_tag;
curr_child = new_curr_child;
i++;
}
}
// update insert_child_right's parent to node_to_insert.
if (insert_child_right_offs != -1)
{
struct BTreeNode* insert_child_right = (struct BTreeNode*) (global_btree_buffer + insert_child_right_offs);
insert_child_right->parent = (char*)(node_to_insert) - global_btree_buffer;
}
node_to_insert->count += 1;
return true;
}
else
{
// get median key, push to parent. Parent might split too, recurse till root.
int median_key;
unsigned char median_tag;
int new_node_right_offset = splitNode(node_to_insert, insert_addr, insert_tag, insert_child_right_offs, &median_key, &median_tag);
// recurse to parent:
if (node_to_insert->parent == -1) // node_to_insert was root
{
// Construct new root!
struct BTreeNode* new_root = (struct BTreeNode*)(global_btree_buffer + global_btree_buffer_first_avail);
initBTreeNode(new_root, -1); // parent = -1
new_root->keys[0] = median_key;
setArrIthTag(new_root->tags, 0, median_tag);
new_root->children[0] = (char*)node_to_insert - global_btree_buffer;
new_root->children[1] = new_node_right_offset;
new_root->count = 1;
// update parents of node_to_insert & new_node_right_offset:
node_to_insert->parent = global_btree_buffer_first_avail;
struct BTreeNode* new_node_right = (struct BTreeNode*)(global_btree_buffer+new_node_right_offset);
new_node_right->parent = global_btree_buffer_first_avail;
// update global_btree_root_offset, first_avail
global_btree_root_offset = global_btree_buffer_first_avail; // (char*)new_root - global_btree_buffer;
global_btree_buffer_first_avail += sizeof(struct BTreeNode);
printf("IN insertBTreeKey: JUST MODIFIED the global_btree_root_offset to %i !!!! \n", global_btree_root_offset);
return true;
}
else
{
int node_to_insert_offset = node_to_insert->parent;
// recurse here:
insert_addr = median_key;
insert_tag = median_tag;
insert_child_right_offs = new_node_right_offset;
node_to_insert = (struct BTreeNode*) (global_btree_buffer + node_to_insert_offset);
printf("IN insertBTreeKey: Recursing for splitNode on node %p , inserting addr %i \n", node_to_insert, insert_addr);
}
}
}
}
// set tag of addr granule.
// TODO: root is at (global_btree_buffer + global_btree_root_offset)
void setTag(struct BTreeNode* root, int addr, int tag)
{
printf("STARTING setTag root: %p , addr: %i, tag: %i \n", root, addr, tag);
// struct BTreeNode* groot = (struct BTreeNode*) (global_btree_buffer + global_btree_root_offset);
// first find largest key x <= addr.
if (!root)
return;
int largest_node_less_offset = getLargestNodeLessThan(getGlobalRootPtr(), addr);
int tag_x = -1, x = -1, x_ind = -1;
struct BTreeNode* largest_node_less = NULL;
if (largest_node_less_offset != -1)
{
// get tag_x, x from largest_node_less_offset:
largest_node_less = (struct BTreeNode*) (global_btree_buffer + largest_node_less_offset);
getLargestKeyTagLessThan(largest_node_less, addr, &x, &tag_x);
x_ind = 0;
while ((x_ind < largest_node_less->count) && (largest_node_less->keys[x_ind] != x))
x_ind++;
}
else
{
printf("IN setTag: COULDN'T FIND ANY x <= addr (%i) !!! \n", addr);
// throw;
}
// if tag[x] = tag, done
if (tag_x == tag)
{
printf("IN setTag: [x: %i] Returning cuz current tag for addr %i is %i \n !!!", x, addr, tag_x);
return;
}
// Find the run on immediate right (xnext) i.e. smallest>x, smallest>=x+1. Needed for later logic.
int smallest_node_more_x_offset = getSmallestNodeMoreThan(getGlobalRootPtr(), x+1);
int tag_xnext = -1, xnext = -1;
struct BTreeNode* smallest_node_more_x = NULL;
int tag_xprev = -1, xprev = -1;
if (smallest_node_more_x_offset != -1)
{
// This offset could be -1!!! (i.e. x is the last run)
smallest_node_more_x = (struct BTreeNode*) (global_btree_buffer + smallest_node_more_x_offset);
getSmallestKeyTagMoreThan(smallest_node_more_x, x+1, &xnext, &tag_xnext);
}
printf("Midway thru setTag: x: %i, tag_x: %i, xnext: %i, tag_xnext: %i \n", x, tag_x, xnext, tag_xnext);
// Possibility of merging with left run.
if (x == addr)
{
// find xprev: largest key < x. i.e. largest <= (x-1)
int largest_node_less_x_offset = getLargestNodeLessThan( getGlobalRootPtr(), x-1);
printf("IN setTag: largest_node_less_offset: %i \n", largest_node_less_x_offset);
// This offset could be -1!!! (i.e. x is the first run)
if (largest_node_less_x_offset != -1)
{
struct BTreeNode* largest_node_less_x = (struct BTreeNode*) (global_btree_buffer + largest_node_less_x_offset);
getLargestKeyTagLessThan(largest_node_less_x, x-1, &xprev, &tag_xprev);
printf("IN setTag: xprev: %i , tag_xprev: %i \n", xprev, tag_xprev);
// if tag = tag[xprev] & addr=x : [merge with xprev run]
if (tag == tag_xprev)
{