-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree_per_small_page.cpp
1733 lines (1493 loc) · 71.7 KB
/
btree_per_small_page.cpp
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 <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/auxv.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <math.h>
#include <time.h>
#include <fstream>
// Number of small pages: (considering max 64GB RAM)
#define SMALL_PAGE_SIZE 4 // 4KB 4*1024
#define MAX_NUMBER_OF_SMALL_PAGES (64*1024*1024)/SMALL_PAGE_SIZE // 16777216
#define PER_PAGE_BTREE_SZ (SMALL_PAGE_SIZE*1024)/32
// #define DEBUG
// #define BIGTAG // if we want 8bit tags
#define FREED_NODES_BITARR // if we want to store freed nodes' addresses in the form of a bit array.
// COPY btree full code
#define BT_MAX_CHILD 5
#define BT_MAX_NODE 4
#define BT_MIN_CHILD 3
#define BT_MIN_NODE 2
#ifdef BIGTAG
#define PER_NODE_TAGARR_NUM_BYTES BT_MAX_NODE
#define MAX_TAG_VALUE 255
#define INVALID_TAG_VALUE 256
#else
#define PER_NODE_TAGARR_NUM_BYTES BT_MAX_NODE/2
#define MAX_TAG_VALUE 15
#define INVALID_TAG_VALUE 16
#endif
// Using 8-bit key/child/parent ptrs for within each small_page BTree.
// SMALL PAGE: 4KB, Size of array space: 128bytes. Total #small pages : M/4KB, ~2^24 for
// ONLY need 7bits to address offset within
struct BTreeNode {
unsigned char keys[BT_MAX_NODE]; // stores upto 4 8-bit keys : 4byt. Each small page only has 4KB/16 granules, i.e. 8-bit keys
unsigned char tags[PER_NODE_TAGARR_NUM_BYTES]; // stores a 4/8bit tag per key : 2/4byt i.e. 0-15 OR 0-255
char count; // 8bits: bits[3:0]: #keys : 1byt
unsigned char children[BT_MAX_CHILD]; // stores upto 5 8-bit offsets. : 5byt
unsigned char parent; // stores 32-bit parent ptr offset. : 1byt // parent == node_offset means node is root.
// bool is_leaf;
};
#define REUSING_FREED_NODE 1
#define ALLOC_FREED_ARR_SZ 5
#define PER_PAGE_MAX_BTREE_NODES (int)PER_PAGE_BTREE_SZ/sizeof(struct BTreeNode)
char* global_btree_buffer; // 64-bit METADATA
// PER PAGE BUFFER:
// For Physical Page i, it should be global_buffer + i*per_page_buffer_sz
// PER PAGE ROOT_OFFSET : 8bit for each page
unsigned char per_page_root_offset[MAX_NUMBER_OF_SMALL_PAGES];
// PER PAGE BUFFER_FIRST_AVAIL : 8bit for each page: 0-128, 7bit!
unsigned char per_page_buffer_first_avail[MAX_NUMBER_OF_SMALL_PAGES];
#ifndef FREED_NODES_BITARR
// PER PAGE freed_nodes ct, freed_nodes_arr (char [])
char per_page_alloc_freed_nodes_arr[MAX_NUMBER_OF_SMALL_PAGES][ALLOC_FREED_ARR_SZ];
#else
bool per_page_alloc_freed_nodes_bitarr[MAX_NUMBER_OF_SMALL_PAGES][16]; // 2bytes cuz upto 9nodes per page
#endif
// PER page current #freed nodes:
char per_page_alloc_freed_nodes[MAX_NUMBER_OF_SMALL_PAGES];
// PER PAGE total_alloc_freed_nodes: Just for accounting, not metadata
int per_page_total_alloc_freed_nodes[MAX_NUMBER_OF_SMALL_PAGES];
// Just for printing / book-keeping: [Not metadata]
long long int global_stg_call_count;
bool btree_verify_failed = false;
// PER PAGE last_printed_page_btree_buf_first_avail
char per_page_last_printed_btree_buf_first_avail[MAX_NUMBER_OF_SMALL_PAGES];
template <class myType>
myType min(myType a, myType b)
{
return (a>b) ? b : a;
}
inline int getPageNumber(long long int addr)
{
return (addr/(1024*SMALL_PAGE_SIZE));
}
int getSelfOffset(struct BTreeNode* node, int page_id)
{
return (char*)node - global_btree_buffer - page_id*PER_PAGE_BTREE_SZ;
}
void printIntArr(int* arr, int len)
{
printf("IntArr L:%i:", len);
for (int x = 0; x < len; x++)
printf("%i, ", arr[x]);
// printf("\n");
}
void printCharArr(char* arr, int len)
{
for (int x = 0; x < len; x++)
printf("%i, ", arr[x]); // a[%i] :
// printf("\n");
}
void printUCharArr(unsigned char* arr, int len)
{
for (int x = 0; x < len; x++)
printf("%i, ", arr[x]); // a[%i] :
// printf("\n");
}
void printBoolArr(bool* arr, int len)
{
for (int x = 0; x < len; x++)
printf("%i, ", arr[x]);
}
void printBTreeNode(struct BTreeNode* node, int page_id)
{
printf("[pg: %i] N off: %i , p_off: %i , ct: %i | K: ", page_id, getSelfOffset(node, page_id), node->parent, node->count);
printUCharArr(node->keys, BT_MAX_NODE);
printf(" | T: ");
printUCharArr(node->tags, PER_NODE_TAGARR_NUM_BYTES);
printf(" | Ch: ");
printUCharArr(node->children, BT_MAX_CHILD);
}
void printBTree(struct BTreeNode* rnode, int level, int page_id)
{
printf("\n LEVEL %i : ", level);
if (level < 7)
{
printBTreeNode(rnode, page_id);
for (int i = 0; i <= rnode->count; i++)
// ASSUMPTION: if child ptr = rnode, invalid ptr.
if (rnode->children[i] != getSelfOffset(rnode, page_id))
printBTree((struct BTreeNode*)(global_btree_buffer + page_id*PER_PAGE_BTREE_SZ + rnode->children[i]), level+1, page_id);
}
else
printf("ERRORRRRR!!!! TREE DEPTH MORE THAN 6!!!! [page: %i, first_avail: %i, freed_nodes: %i] \n", page_id, per_page_buffer_first_avail[page_id], per_page_alloc_freed_nodes[page_id]);
}
static struct BTreeNode* getPageRootPtr(int page_id)
{
// global_btree_buffer + page_id*PER_PAGE_BTREE_SZ : buffer_start point for page_id page
// then, add root offset.
return (struct BTreeNode*) (global_btree_buffer + page_id*PER_PAGE_BTREE_SZ + per_page_root_offset[page_id]);
}
void initBTreeNode(struct BTreeNode* rnode, char parent_offset, char child_offsets)
{
rnode->count = 0;
for (int i = 0; i < BT_MAX_NODE; i++)
rnode->keys[i] = 0;
for (int i = 0; i < BT_MAX_CHILD; i++)
rnode->children[i] = child_offsets; // null ptr
rnode->parent = parent_offset;
// rnode->is_leaf = true;
}
void initBTree(char* page_btree_buffer, int page_id)
{
// make a new BTreeNode, with key=0,tag=0. []
struct BTreeNode* root = (struct BTreeNode*) page_btree_buffer;
initBTreeNode(root, 0, 0); // parent = null ptr, equal to root's offset.
root->count = 1;
root->keys[0] = 0;
// UPDATE per-page metadata:
per_page_buffer_first_avail[page_id] += sizeof(struct BTreeNode);
per_page_root_offset[page_id] = 0;
#ifndef FREED_NODES_BITARR
for (int i = 0; i < ALLOC_FREED_ARR_SZ; i++)
per_page_alloc_freed_nodes_arr[page_id][i] = -1; // initialize freed addresses with -1.
#else
for (int i = 0; i < 16; i++)
per_page_alloc_freed_nodes_bitarr[page_id][i] = false; // initialize: no addr is freed (first avail will denote all addresses are available)
#endif
per_page_alloc_freed_nodes[page_id] = 0; // tracks fragmentation.
per_page_total_alloc_freed_nodes[page_id] = 0;
if (page_id % (16*1024) == 1)
printf("##### FINISHED initBTree: root ptr: %p, new first_avail: %i, page_btree_root_offset: %i, alloc_freed_nodes: %i, total_alloc_freed_nodes: %i, PER_PAGE_MAX_BTREE_NODES: %li, BTreeNode sz: %li [FOR PAGE %i] \n", root, per_page_buffer_first_avail[page_id], per_page_root_offset[page_id], per_page_alloc_freed_nodes[page_id], per_page_total_alloc_freed_nodes[page_id], PER_PAGE_MAX_BTREE_NODES, sizeof(struct BTreeNode), page_id);
}
unsigned char getArrIthTag(unsigned char* tag_arr, int i)
{
#ifndef BIGTAG
return (i%2 == 1) ? ((tag_arr[i/2])%16) : (tag_arr[i/2] >> 4) ;
#else
return tag_arr[i];
#endif
}
void setArrIthTag(unsigned char* tag_arr, int i, unsigned char tag)
{
if (tag > MAX_TAG_VALUE)
{
printf("ERRORRRRR! setArrIthTag called with invalid tag value!!! %i (more than max possible: %i) \n", tag, MAX_TAG_VALUE);
// throw;
}
#ifndef BIGTAG
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]);
#else
tag_arr[i] = tag;
#endif
}
// leaf if all children are -1 / point to itself (i.e. null ptr)
bool isBTreeNodeLeaf(struct BTreeNode* root, int page_id)
{
// return root->is_leaf;
for (int i = 0; i < BT_MAX_CHILD; i++)
if (root->children[i] != getSelfOffset(root, page_id) )
return false;
return true;
}
bool isValidChild(struct BTreeNode* root, int i, int page_id)
{
// return (!root->is_leaf) && (i <= root->count);
return (i <= root->count) && (root->children[i] != getSelfOffset(root, page_id));
}
void freeNodeOffset(int offset, int page_id)
{
per_page_total_alloc_freed_nodes[page_id] += 1;
#ifndef FREED_NODES_BITARR
per_page_alloc_freed_nodes_arr[page_id][per_page_alloc_freed_nodes[page_id]%ALLOC_FREED_ARR_SZ] = offset;
per_page_alloc_freed_nodes[page_id] = min(per_page_alloc_freed_nodes[page_id]+1, ALLOC_FREED_ARR_SZ);
#else
per_page_alloc_freed_nodes_bitarr[page_id][offset/sizeof(struct BTreeNode)] = true; // offset can be b/w 0 - [128/sizeof(BTreeNode)]
per_page_alloc_freed_nodes[page_id] = min( per_page_alloc_freed_nodes[page_id]+1, (int)(PER_PAGE_MAX_BTREE_NODES) );
// if (page_id == 1)
// printf("Pg %i, Just freed offset %i (id = %li), per_page_alloc_freed_nodes: %i \n", page_id, offset, offset/sizeof(struct BTreeNode), per_page_alloc_freed_nodes[page_id] );
#endif
// printf("FREE-ed addr %i, alloc_freed_nodes: %i, total_alloc_freed_nodes: %i ", offset, alloc_freed_nodes, total_alloc_freed_nodes);
// printIntArr(alloc_freed_nodes_arr, ALLOC_FREED_ARR_SZ);
}
// Use this func to get offset for assigning new node:
// Manages free-d nodes' space. [if REUSING_FREED_NODE == 1, it tries to re-use freed node space]
char getNewNodeOffset(int page_id)
{
long long int ans = 0;
if ((per_page_alloc_freed_nodes[page_id] == 0) || (REUSING_FREED_NODE == 0))
{
// Use fresh unused space, increment buffer_first_avail:
ans = per_page_buffer_first_avail[page_id];
per_page_buffer_first_avail[page_id] += sizeof(struct BTreeNode);
#ifdef DEBUG
if (page_id == 1)
{
printf("IN getNewNodeOffset for page %i : Assigning new first-avail: ans: %lli, first_avail: %i \n", page_id, ans, per_page_buffer_first_avail[page_id]);
// printBTree( getPageRootPtr(page_id), 0, page_id );
}
#endif
}
else
{
// use a free-d addr, update free-d count:
// printf("IN getNewNodeOffset: RE-using a free-d space: alloc_freed_nodes: %i, total_alloc_freed_nodes: %i", alloc_freed_nodes, total_alloc_freed_nodes);
// printIntArr(alloc_freed_nodes_arr, ALLOC_FREED_ARR_SZ);
per_page_alloc_freed_nodes[page_id] -= 1;
per_page_total_alloc_freed_nodes[page_id] -= 1;
#ifndef FREED_NODES_BITARR
ans = per_page_alloc_freed_nodes_arr[page_id][ (per_page_alloc_freed_nodes[page_id]) % ALLOC_FREED_ARR_SZ];
per_page_alloc_freed_nodes_arr[page_id][ (per_page_alloc_freed_nodes[page_id]) % ALLOC_FREED_ARR_SZ] = -1;
#else
ans = -1;
for (int i = 0; i < PER_PAGE_MAX_BTREE_NODES; i++)
if (per_page_alloc_freed_nodes_bitarr[page_id][i])
{
ans = sizeof(struct BTreeNode) * i;
per_page_alloc_freed_nodes_bitarr[page_id][i] = false;
// if (page_id == 1)
// printf("IN PAGE (%i) BITARR: Found %i offset FREE!! per_page_alloc_freed_nodes: %i \n", page_id, i, per_page_alloc_freed_nodes[page_id]);
break;
}
#endif
if (ans == -1)
printf("IN getNewNodeOffset: ERRORRRR!!!! freed_addr was -1!!! page_alloc_freed_nodes: %i, page_total_alloc_freed_nodes: %i, addr: %lli", per_page_alloc_freed_nodes[page_id], per_page_total_alloc_freed_nodes[page_id], ans);
}
if ((per_page_buffer_first_avail[page_id] > (PER_PAGE_MAX_BTREE_NODES*sizeof(struct BTreeNode))) && (per_page_alloc_freed_nodes[page_id] == 0))
{
printf("WARNINGGGGGG!!!!! BTREE FOR PAGE %i IS TOO BIGG!!!! first_avail: %i, freed_nodes: %i, total_freed_nodes: %i \n", page_id, per_page_buffer_first_avail[page_id], per_page_alloc_freed_nodes[page_id], per_page_total_alloc_freed_nodes[page_id]);
printBTree(getPageRootPtr(page_id), 0, page_id);
#ifndef FREED_NODES_BITARR
printCharArr(per_page_alloc_freed_nodes_arr[page_id], (int)ALLOC_FREED_ARR_SZ);
#else
printBoolArr(per_page_alloc_freed_nodes_bitarr[page_id], 16);
#endif
ans = 0; // DONT WANT THIS BTREE TO OVERFLOW TO ANOTHER BTree. SHOULD COPY TAGS TO ARRAY AT THIS POINT.
}
return ans;
}
// 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.
// this addr is the granule# in page pageID, and returns char since nodes within the small_page BTree have 8-bit addr.
int getLargestNodeLessThan(struct BTreeNode* root, unsigned char addr, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
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) && (isValidChild(root, i, page_id)) ) // root->children[i] != -1
{
int ans_in_right_child = getLargestNodeLessThan( (struct BTreeNode*)(page_btree_buffer + root->children[i]), addr, page_id );
return (ans_in_right_child == -1) ? ((char*)root - page_btree_buffer) : ans_in_right_child;
}
else
return ((char*)root - page_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 - page_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) && (isValidChild(root, i, page_id) ) ) // root->children[i] != -1
ans_in_left_child = getLargestNodeLessThan( (struct BTreeNode*)(page_btree_buffer + root->children[i]), addr, page_id );
return (ans_in_left_child == -1) ? ( (i == 0) ? -1 : ((char*)root - page_btree_buffer) ) : ans_in_left_child;
}
return -1;
}
// 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, unsigned char addr, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
if (!root)
return -1;
int i=(root->count)-1;
while ( (i >= 0) && (root->keys[i] >= addr))
i--;
// printf("IN getSmallestNodeMoreThan: addr: %i, i: %i, root->count: %i \n", addr, i, root->count);
// 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 (isValidChild(root, 0, page_id) ) // root->children[0] != -1
{
int ans_in_left_child = getSmallestNodeMoreThan( (struct BTreeNode*)(page_btree_buffer+root->children[0]) , addr, page_id);
return (ans_in_left_child == -1) ? ((char*) root - page_btree_buffer) : ans_in_left_child;
}
else
return (char)((char*)root - page_btree_buffer); // the current root node has the smallest key>=addr.
}
// case2:
if (root->keys[i] == addr)
return ((char*)root - page_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 (isValidChild(root, i+1, page_id)) // root->children[i+1] != -1
ans_in_right_child = getSmallestNodeMoreThan((struct BTreeNode*)(page_btree_buffer+root->children[i+1]) , addr, page_id);
// printf("IN getSmallestNodeMoreThan: addr: %i, i: %i, root->keys[i]: %i, ans_in_right_child: %i, root->children[i+1]: %i \n", addr, i, root->keys[i], ans_in_right_child, root->children[i+1]);
return (ans_in_right_child == -1) ? ((i == (root->count-1)) ? -1 : ((char*) root - page_btree_buffer) ) : ans_in_right_child;
}
return -1;
}
void getLargestKeyTagLessThan(struct BTreeNode* root, unsigned char 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, unsigned char 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, unsigned char key, int tag, bool right_child, int child_offset, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
int rnode_offset = getSelfOffset(rnode, page_id);
// find where to insert
int i = 0;
while ((i < rnode->count) && (rnode->keys[i] < key) )
i++;
#ifdef DEBUG
if ((page_id == 3299) )
printf("INSERTING key %i into node %i at i=%i , with tag %i, right_child: %i, child_offset: %i \n", key, rnode_offset, i, tag, right_child, child_offset);
#endif
// insert key at rnode->keys[i]
// shift tags,keys,children
int curr = key;
int curr_tag = tag;
int curr_child = (child_offset == -1) ? rnode_offset : 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) // child_offset != -1
{
struct BTreeNode* child_ptr = (struct BTreeNode*) (page_btree_buffer + child_offset);
child_ptr->parent = (char*)rnode - page_btree_buffer;
}
rnode->count += 1;
}
// deletes key&tag from rnode - i.e. updates keys, count, children.
// equivalent to removeVal func in programiz code.
// rebalanceBTree can call 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)
{
// printf("in deleteBTreeKeyFromNode: FOR KEY %i, del_child: %i, ", key, 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) // deleting left child
rnode->children[i] = rnode->children[i+1];
else // deleting right child
rnode->children[i+1] = rnode->children[i+2];
i++;
}
rnode->keys[rnode->count - 1] = 0; // 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;
}
// v2: get tag of addr granule, equiv to TagArr[addr]
// NOTE: ALWAYS CALL this func with root as the global root ptr.
int getBTreeTag(struct BTreeNode* root, unsigned char addr, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
// struct BTreeNode* groot = (struct BTreeNode*) (global_btree_buffer + global_btree_root_offset);
if (!root)
return -1;
int node_with_largest_less_than_off = getLargestNodeLessThan(getPageRootPtr(page_id), addr, page_id);
struct BTreeNode* node_with_largest_less_than = (struct BTreeNode*) (page_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;
}
// 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!!! [for getBTreeTag for addr %i in page: %i] node_with_largest_less_than_off: %i, 0th key: %i", addr, page_id, node_with_largest_less_than_off, node_with_largest_less_than->keys[i]);
printBTree(getPageRootPtr(page_id), 0, page_id);
// 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;
}
return -1;
}
// re-balance, starting from a deficient leaf node:
void rebalanceBTree(struct BTreeNode* rnode, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
struct BTreeNode* rleaf = rnode;
int iter_ct = 0;
while (iter_ct < 20)
{
int rleaf_offset = (char*) rleaf - page_btree_buffer;
#ifdef DEBUG
if (page_id == 3299)
printf("IN rebalanceBTree: for node %p (offset: %i), parent: %i \n", rleaf, rleaf_offset, rleaf->parent);
#endif
if (rleaf->parent != rleaf_offset ) // non null parent ~ parent=self_offset // rleaf->parent != -1
{
struct BTreeNode* rleaf_parent = (struct BTreeNode*) (page_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) && (isValidChild(rleaf_parent, i+1, page_id))) // rleaf_parent->children[i+1] != -1
{
// rleaf_right_bro_offset = rleaf_parent->children[i+1];
rleaf_right_bro = (struct BTreeNode*) (page_btree_buffer + rleaf_parent->children[i+1]);
if (rleaf_right_bro->count > BT_MIN_NODE)
{
// separator key moves to rleaf
rleaf->keys[(int)(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[(int)(rleaf->count)] = (!isValidChild(rleaf_right_bro, 0, page_id)) ? rleaf_offset : rleaf_right_bro->children[0];
if (isValidChild(rleaf_right_bro, 0, page_id) ) // rleaf_right_bro->children[0] != -1
{
struct BTreeNode* rl_right_bro_c0 = (struct BTreeNode*) (page_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 (offset: %i) \n", rleaf_right_bro, rleaf_right_bro_offset);
// printBTreeNode(rleaf);
// printBTreeNode(rleaf_right_bro);
// printBTreeNode(rleaf_parent);
return;
}
}
// check if can rotate with left sibling:
if ((i > 0) && (isValidChild(rleaf_parent, i-1, page_id) )) // rleaf_parent->children[i-1] != -1
{
rleaf_left_bro_offset = rleaf_parent->children[i-1];
rleaf_left_bro = (struct BTreeNode*) (page_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:
int insert_child_in_node = (rleaf_left_bro->children[(int)(rleaf_left_bro->count)] == rleaf_left_bro_offset) ? -1 : rleaf_left_bro->children[(int)(rleaf_left_bro->count)];
insertBTreeKeyInNode(rleaf, rleaf_parent->keys[i-1], getArrIthTag(rleaf_parent->tags,i-1), 0, insert_child_in_node, page_id);
// make rleaf_left_bro's last child, the first child of rleaf: [Doing this as part of insertBTreeKeyInNode]
// 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[(int)(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[(int)(rleaf_left_bro->count)] = rleaf->keys[rx];
setArrIthTag(rleaf_left_bro->tags, rleaf_left_bro->count, getArrIthTag(rleaf->tags,rx));
rleaf_left_bro->children[(int)(rleaf_left_bro->count)] = (rleaf->children[rx] == rleaf_offset) ? rleaf_left_bro_offset : rleaf->children[rx];
if (isValidChild(rleaf, rx, page_id) ) // rleaf->children[rx] != -1
{
struct BTreeNode* rleaf_child_rx = (struct BTreeNode*) (page_btree_buffer + rleaf->children[rx]);
rleaf_child_rx->parent = rleaf_left_bro_offset;
}
rleaf_left_bro->count += 1;
}
rleaf_left_bro->children[(int)(rleaf_left_bro->count)] = (rleaf->children[(int)(rleaf->count)] == rleaf_offset) ? rleaf_left_bro_offset : rleaf->children[(int)(rleaf->count)];
if (isValidChild(rleaf, (int)(rleaf->count), page_id) ) // rleaf->children[] != -1
{
struct BTreeNode* rleaf_child_last = (struct BTreeNode*) (page_btree_buffer + rleaf->children[(int)(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!!!
#ifdef DEBUG
if (page_id == 3299)
{
printf("IN rebalanceBTree: Merged with rleaf_left_bro: %p \n", rleaf_left_bro);
printBTreeNode(rleaf_left_bro, page_id);
printBTreeNode(rleaf_parent, page_id);
}
#endif
}
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!
// NOTE: a child ptr will point to itself if its invalid. So, invalid child ptr value is different for different nodes!
// We pass -1 as the child offset if its an invalid ptr. The insertBTreeKeyInNode function takes care of this internally.
// 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] == rleaf_offset) ? -1 : rleaf->children[lx]), page_id);
// if (page_id == 62)
// printBTreeNode(rleaf_right_bro, page_id);
// put separator in rleaf_right_bro
insertBTreeKeyInNode(rleaf_right_bro, rleaf_parent->keys[i], getArrIthTag(rleaf_parent->tags, i), 0, ((rleaf->children[(int)(rleaf->count)] == rleaf_offset) ? -1 : rleaf->children[(int)(rleaf->count)]), page_id);
// if (page_id == 62)
// printBTreeNode(rleaf_right_bro, page_id);
// delete separator from parent: delete left child of this key
deleteBTreeKeyFromNode(rleaf_parent, rleaf_parent->keys[i], 0);
#ifdef DEBUG
if (page_id == 3299)
{
printf("IN rebalanceBTree: Merged with rleaf_right_bro: %p \n", rleaf_right_bro);
printBTreeNode(rleaf_right_bro, page_id);
}
#endif
// printBTreeNode(rleaf_parent);
}
else
{
printf("ERRORRR!!! Both left, right bros are NULL!! \n");
// throw;
}
// 'delete' rleaf node.
freeNodeOffset(rleaf_offset, page_id);
// Moved this code to freeNodeOffset function:
// alloc_freed_nodes_arr[total_alloc_freed_nodes%ALLOC_FREED_ARR_SZ] = rleaf_offset;
// total_alloc_freed_nodes += 1;
// alloc_freed_nodes = std::min(alloc_freed_nodes+1, ALLOC_FREED_ARR_SZ);
// // printf("IN rebalanceBTree: UPDATED alloc_freed_nodes to %i \n", alloc_freed_nodes);
// printIntArr(alloc_freed_nodes_arr, ALLOC_FREED_ARR_SZ);
// // freeNodeOffset code ends
// Call re-balance on rleaf_parent, if needed
if (rleaf_parent->count == 0)
{
if (rleaf_parent->parent != rleaf->parent) // rleaf_parent->parent != -1, rleaf_parent->parent is invalid if its equal to rleaf_parent offset, which is rleaf->parent.
{
printf("ERRORRR: SOMEHOW NON-ROOT node is EMPTY!!!! page_id: %i, rleaf_offset: %i, rleaf_parent: %i, rleaf_parent->parent: %i \n ", page_id, rleaf_offset, rleaf->parent, rleaf_parent->parent);
// throw;
}
else
{
// free current root:
freeNodeOffset(per_page_root_offset[page_id], page_id);
// alloc_freed_nodes_arr[total_alloc_freed_nodes%ALLOC_FREED_ARR_SZ] = global_btree_root_offset;
// total_alloc_freed_nodes += 1;
// printIntArr(alloc_freed_nodes_arr, ALLOC_FREED_ARR_SZ);
// 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 = ((char*) new_root - page_btree_buffer);
per_page_root_offset[page_id] = (char*) new_root - page_btree_buffer;
// 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]
}
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!! pg: %i (rleaf_offset: %i, rleaf->count: %i, rlea->parent: %i) \n", page_id, rleaf_offset, rleaf->count, rleaf->parent);
// throw; // this should never happen!
}
return; // WE'RE DONE!!!
}
iter_ct++;
}
printf("ERRORRRRRR!!!!!! RECURSION IN deleteBTreeKey MORE THAN 20 TIMES!!!!");
}
// TODO: FIX CHILD PTR INVALID ~~ POINT TO ITSELF.
// deletes key from rnode, and tries to balance the BTree
bool deleteBTreeKey(struct BTreeNode* rnode, int key, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
#ifdef DEBUG
if (page_id == 3299)
printf("IN deleteBTreeKey!!! for key %i, on page %i, ", key, page_id);
#endif
if (isBTreeNodeLeaf(rnode, page_id))
{
deleteBTreeKeyFromNode(rnode, key, 1);
if (rnode->count < BT_MIN_NODE)
{
// re-balance
rebalanceBTree(rnode, page_id);
}
}
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*)(page_btree_buffer + rnode->children[i+1]), key, page_id );
struct BTreeNode* smallest_node_more_key = (struct BTreeNode*) (page_btree_buffer + smallest_node_more_key_off);
// replacement should be in a leaf.
if (!isBTreeNodeLeaf(smallest_node_more_key, page_id))
{
printf("IN deleteBTreeKey: ERRORRRR!!!! smallestNodeMoreThan key %i is NOT a leaf!!! \n", key);
return false;
}
// 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, page_id);
}
}
return true;
}
int findLeafNodeToInsertAddr(struct BTreeNode* rnode, int addr, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
if (isBTreeNodeLeaf(rnode, page_id))
return (char*)rnode - page_btree_buffer;
int i = 0;
while (i < rnode->count && (rnode->keys[i] < addr) )
i++;
if ( !isValidChild(rnode, i, page_id) ) // 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]);
return -1;
// throw;
}
else
return findLeafNodeToInsertAddr( (struct BTreeNode*)(page_btree_buffer + rnode->children[i]), addr, page_id );
// 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);
}
// if there's any child that has the wrong parent ptr, returns false.
bool verifyBTreePtrs(struct BTreeNode* root, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
int root_offset = (char*) root - page_btree_buffer;
for (int i = 0; i <= root->count; i++)
{
if (root->children[i] != root_offset)
{
struct BTreeNode* r_child_i = (struct BTreeNode*) (page_btree_buffer + root->children[i]);
if (r_child_i->parent != root_offset)
return false;
if (!verifyBTreePtrs(r_child_i, page_id))
return false;
}
}
return true;
}
// 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, int page_id)
{
char* page_btree_buffer = global_btree_buffer + page_id*PER_PAGE_BTREE_SZ;
int rnode_offset = (char*)rnode - page_btree_buffer;
if (rnode->count < BT_MAX_NODE)
{
printf("ERROR: 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] == rnode_offset) ? -1 : rnode->children[j];
}
full_arr[i] = insert_addr;
setArrIthTag(full_tag_arr, i, insert_tag);
full_children_arr[i] = (rnode->children[i] == rnode_offset) ? -1 : 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] == rnode_offset) ? -1 : 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) );