-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflexdb.c
2943 lines (2646 loc) · 101 KB
/
flexdb.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 "flexdb.h"
// sparse index tree {{{
static struct flexdb_tree_node *flexdb_tree_create_node(
struct flexdb_tree *const tree, struct flexdb_tree_node *const parent)
{
struct flexdb_tree_node *const node = slab_alloc_unsafe(tree->node_slab);
if (!node) {
return node;
}
memset(node, 0, sizeof(*node));
node->parent = parent;
node->count = 0;
node->tree = tree;
return node;
}
static inline struct flexdb_tree_node *flexdb_tree_create_leaf_node(
struct flexdb_tree *const tree, struct flexdb_tree_node *const parent)
{
struct flexdb_tree_node *const node = flexdb_tree_create_node(tree, parent);
node->is_leaf = 1;
return node;
}
static inline struct flexdb_tree_node *flexdb_tree_create_internal_node(
struct flexdb_tree *const tree, struct flexdb_tree_node *const parent)
{
// note: parent_id is not initialized here
return flexdb_tree_create_node(tree, parent);
}
static void flexdb_tree_free_node_rec(struct flexdb_tree_node *const node)
{
debug_assert(node);
if (!node->is_leaf) {
struct flexdb_tree_internal_entry *const ie = &node->internal_entry;
for (u32 i=0; i<node->count+1; ++i) {
flexdb_tree_free_node_rec(ie->children[i].node);
}
for (u32 i=0; i<node->count; i++) {
free(ie->pivots[i]);
}
}
else {
struct flexdb_tree_leaf_entry *const le = &node->leaf_entry;
for (u32 i=0; i<node->count; i++) {
free(le->anchors[i]->key);
slab_free_unsafe(node->tree->anchor_slab, le->anchors[i]);
}
}
slab_free_unsafe(node->tree->node_slab, node);
}
static void flexdb_tree_destroy(struct flexdb_tree *const tree)
{
flexdb_tree_free_node_rec(tree->root);
slab_destroy(tree->node_slab);
slab_destroy(tree->anchor_slab);
free(tree);
}
static inline u8 flexdb_tree_node_full(const struct flexdb_tree_node *const node)
{
const u64 cap = node->is_leaf ? FLEXDB_TREE_LEAF_CAP : FLEXDB_TREE_INTERNAL_CAP;
return (cap - 1 <= node->count) ? 1 : 0;
}
static inline u8 flexdb_tree_node_empty(const struct flexdb_tree_node *const node)
{
return (node->count == 0) ? 1 : 0;
}
static void flexdb_tree_node_rebase(struct flexdb_tree_node *const node)
{
debug_assert(node->is_leaf);
struct flexdb_tree_leaf_entry *const le = &node->leaf_entry;
if (le->anchors[node->count-1]->loff >= (u32)(~0) >> 2) {
const u64 new_base = le->anchors[0]->loff;
debug_assert(new_base != 0);
const u32 p_idx = node->parent_id;
struct flexdb_tree_node *const parent = node->parent;
parent->internal_entry.children[p_idx].shift += new_base;
for (u32 i=0; i<node->count; i++) {
le->anchors[i]->loff -= new_base;
}
}
}
static void flexdb_tree_link_two_nodes(struct flexdb_tree_node *const node1, struct flexdb_tree_node *const node2)
{
struct flexdb_tree_leaf_entry *const le1 = &node1->leaf_entry;
struct flexdb_tree_leaf_entry *const le2 = &node2->leaf_entry;
le2->prev = node1;
le2->next = le1->next;
le1->next = node2;
if (le2->next) {
le2->next->leaf_entry.prev = node2;
}
}
static void flexdb_tree_node_shift_apply(struct flexdb_tree_node *const node, const s64 shift)
{
// only call this when confident no overflow will happen
if (node->is_leaf) {
struct flexdb_tree_leaf_entry *const le = &node->leaf_entry;
debug_assert((u32)(le->anchors[node->count-1]->loff + (u64)shift) <= ~(u32)0); // always true
for (u32 i=0; i<node->count; i++) {
le->anchors[i]->loff = (u32)(le->anchors[i]->loff + (u64)shift);
}
} else {
struct flexdb_tree_internal_entry *const ie = &node->internal_entry;
for (u32 i=0; i<node->count+1; i++) {
ie->children[i].shift += shift;
}
}
}
static void flexdb_tree_split_internal_node(struct flexdb_tree_node *const node)
{
debug_assert(!node->is_leaf);
struct flexdb_tree_node *const node1 = node;
struct flexdb_tree_node *const node2 = flexdb_tree_create_internal_node(node1->tree, node1->parent);
struct flexdb_tree_internal_entry *const ie1 = &node1->internal_entry;
struct flexdb_tree_internal_entry *const ie2 = &node2->internal_entry;
const u32 count = (node1->count + 1) / 2;
struct kv *const new_base = ie1->pivots[count];
//manipulate node2
node2->count = node1->count - count - 1;
memmove(ie2->pivots, &ie1->pivots[count+1], node2->count * sizeof(ie2->pivots[0]));
memmove(ie2->children, &ie1->children[count+1], (node2->count+1) * sizeof(ie2->children[0]));
//manipulate node1
node1->count = count;
// if no parent, create one and set as root
struct flexdb_tree_node *parent = node1->parent;
if (!node1->parent) {
parent = flexdb_tree_create_internal_node(node1->tree, node1->parent);
node1->parent = parent;
node2->parent = parent;
debug_assert(parent->tree);
parent->tree->root = parent;
}
// insert one index entry in parent
struct flexdb_tree_internal_entry *const ie = &parent->internal_entry;
if (parent->count == 0) {
ie->children[0].node = node1;
ie->children[0].shift = 0;
ie->children[0+1].node = node2;
ie->children[0+1].shift = 0;
ie->pivots[0] = new_base;
parent->count = 1;
node1->parent_id = 0;
node2->parent_id = 1;
}
else {
const u32 p_idx = node1->parent_id;
const s64 orig_shift = ie->children[p_idx].shift;
memmove(&ie->pivots[p_idx+1], &ie->pivots[p_idx], (parent->count - p_idx) * sizeof(ie->pivots[0]));
memmove(&ie->children[p_idx+2], &ie->children[p_idx+1], (parent->count - p_idx) * sizeof(ie->children[0]));
ie->children[p_idx+1].node = node2;
ie->children[p_idx+1].shift = orig_shift;
ie->pivots[p_idx] = new_base;
parent->count += 1;
node2->parent_id = node1->parent_id;
for (u32 i=p_idx+1; i<parent->count+1; i++) {
ie->children[i].node->parent_id++;
}
}
for (u32 i=0; i<node2->count+1; ++i) {
ie2->children[i].node->parent_id = i;
ie2->children[i].node->parent = node2;
}
if (flexdb_tree_node_full(parent)) {
flexdb_tree_split_internal_node(parent);
}
}
static void flexdb_tree_split_leaf_node(struct flexdb_tree_node *const node)
{
debug_assert(node->is_leaf);
struct flexdb_tree_node *const node1 = node;
struct flexdb_tree_node *const node2 = flexdb_tree_create_leaf_node(node1->tree, node1->parent);
flexdb_tree_link_two_nodes(node1, node2);
struct flexdb_tree_leaf_entry *const le1 = &node1->leaf_entry;
struct flexdb_tree_leaf_entry *const le2 = &node2->leaf_entry;
const u32 count = (node1->count + 1) / 2;
// manipulate node2
node2->count = node1->count - count;
memmove(le2->anchors, &le1->anchors[count], node2->count * sizeof(le2->anchors[0]));
// manipulate node1
node1->count = count;
// if no parent, create one and set as root
struct flexdb_tree_node *parent = node1->parent;
if (!parent) {
parent = flexdb_tree_create_internal_node(node1->tree, node1->parent);
node1->parent = parent;
node2->parent = parent;
parent->tree->root = parent;
}
// insert one index entry in parent
struct flexdb_tree_internal_entry *const ie = &parent->internal_entry;
if (parent->count == 0) {
ie->children[0].node = node1;
ie->children[0].shift = 0;
ie->children[0+1].node = node2;
ie->children[0+1].shift = 0;
ie->pivots[0] = kv_dup_key(node2->leaf_entry.anchors[0]->key);
node1->parent_id = 0;
node2->parent_id = 1;
parent->count = 1;
}
else {
const u32 p_idx = node1->parent_id;
const s64 orig_shift = ie->children[p_idx].shift;
memmove(&ie->pivots[p_idx+1], &ie->pivots[p_idx], (parent->count - p_idx) * sizeof(ie->pivots[0]));
memmove(&ie->children[p_idx+2], &ie->children[p_idx+1], (parent->count - p_idx) * sizeof(ie->children[0]));
ie->children[p_idx+1].node = node2;
ie->children[p_idx+1].shift = orig_shift;
ie->pivots[p_idx] = kv_dup_key(node2->leaf_entry.anchors[0]->key);
node2->parent_id = node1->parent_id;
parent->count += 1;
for (u32 i=p_idx+1; i<parent->count+1; i++) {
ie->children[i].node->parent_id++;
}
}
if (node->parent->count > 1) {
flexdb_tree_node_rebase(node1);
flexdb_tree_node_rebase(node2);
}
if (flexdb_tree_node_full(parent)) {
flexdb_tree_split_internal_node(parent);
}
}
static void flexdb_tree_node_shift_up_propagate(struct flexdb_tree_node_handler *const nh, const s64 shift)
{
struct flexdb_tree_node *node = nh->node;
const u32 target = nh->idx;
debug_assert(node->is_leaf);
for (u32 i=target+1; i<node->count; i++) { // cannot use SIMD
node->leaf_entry.anchors[i]->loff += shift;
}
while (node->parent) {
const u32 p_idx = node->parent_id;
node = node->parent;
debug_assert(!node->is_leaf);
struct flexdb_tree_internal_entry *const ie = &node->internal_entry;
for (u32 i=p_idx; i<node->count; i++) {
ie->children[i+1].shift += shift;
}
}
}
static u32 flexdb_tree_find_pos_in_leaf_le(const struct flexdb_tree_node *const node, const struct kref *const key)
{
debug_assert(node->is_leaf);
u32 target = 0; // find position
u32 hi = node->count;
u32 lo = 0;
const struct flexdb_tree_leaf_entry *const le = &node->leaf_entry;
// binary search
// while ((lo + 3) < hi) {
// target = (lo + hi) >> 1;
// const struct kv * const curr = le->anchors[target]->key;
// cpu_prefetch0(curr);
// cpu_prefetch0(&le->anchors[(lo+target)>>1]);
// cpu_prefetch0(&le->anchors[(target+hi)>>1]);
// const int cmp = kref_kv_compare(key, curr);
// if (cmp > 0) {
// lo = target;
// }
// else if (cmp < 0) {
// hi = target;
// }
// else {
// return target;
// }
// }
while ((lo + 1) < hi) {
target = (lo + hi) >> 1;
const int cmp = kref_kv_compare(key, le->anchors[target]->key);
if (cmp > 0) {
lo = target;
}
else if (cmp < 0) {
hi = target;
}
else {
return target;
}
}
return lo;
}
static u32 flexdb_tree_find_pos_in_internal(const struct flexdb_tree_node *const node, const struct kref *const key)
{
debug_assert(!node->is_leaf);
u32 hi = node->count;
u32 lo = 0;
const struct flexdb_tree_internal_entry *const ie = &node->internal_entry;
// binary search
// while ((lo + 2) < hi) {
// const u32 target = (lo + hi) >> 1;
// const struct kv * const curr = ie->pivots[target];
// //cpu_prefetch0(curr+4); // the 2nd line ?
// cpu_prefetch0(ie->pivots[(lo+target)>>1]);
// cpu_prefetch0(ie->pivots[(target+1+hi)>>1]);
// const int cmp = kref_kv_compare(key, curr);
// if (cmp >= 0) {
// lo = target + 1;
// }
// else {
// hi = target;
// }
// }
while (lo < hi) {
const u32 target = (lo + hi) >> 1;
const int cmp = kref_kv_compare(key, ie->pivots[target]);
if (cmp >= 0) {
lo = target + 1;
}
else {
hi = target;
}
}
return lo;
}
static struct flexdb_tree *flexdb_tree_create(struct flexdb *const db)
{
struct flexdb_tree *const tree = malloc(sizeof(*tree));
debug_assert(tree);
tree->db = db;
// slabs
tree->node_slab = slab_create(sizeof(struct flexdb_tree_node), 2lu << 20);
tree->anchor_slab = slab_create(sizeof(struct flexdb_tree_anchor), 2lu << 20);
if (tree->node_slab == NULL || tree->anchor_slab == NULL) {
printf("node_slab or anchor_slab alloc failed\n");
exit(1);
}
tree->root = flexdb_tree_create_leaf_node(tree, NULL);
tree->leaf_head = tree->root;
debug_assert(tree->root);
// insert smallest key
struct flexdb_tree_anchor *const sanchor = slab_alloc_unsafe(tree->anchor_slab);
*sanchor= (struct flexdb_tree_anchor)
{.key = kv_dup_key(kv_null()), .loff = 0, .psize = 0, .unsorted = 0, .cache_entry = NULL};
kv_update_hash(sanchor->key);
tree->root->leaf_entry.anchors[0] = sanchor;
tree->root->count++;
return tree;
}
static void flexdb_tree_find_anchor_pos(const struct flexdb_tree *const tree,
const struct kref *const key, struct flexdb_tree_node_handler * const nh)
{
s64 shift = 0;
struct flexdb_tree_node *node = tree->root;
while (!node->is_leaf) {
const u32 target = flexdb_tree_find_pos_in_internal(node, key);
const struct flexdb_tree_internal_entry *const ie = &node->internal_entry;
shift += ie->children[target].shift;
node = ie->children[target].node;
}
debug_assert(node->is_leaf);
nh->node = node;
nh->shift = shift;
nh->idx = flexdb_tree_find_pos_in_leaf_le(node, key);
}
static struct flexdb_tree_anchor *flexdb_tree_handler_insert(
const struct flexdb_tree_node_handler *const nh, struct kv *const key,
const u64 loff, const u32 psize)
{
struct flexdb_tree_node *const node = nh->node;
debug_assert(node->is_leaf);
struct flexdb_tree_anchor *const t = slab_alloc_unsafe(nh->node->tree->anchor_slab);
*t = (struct flexdb_tree_anchor)
{.key = key, .loff = (u32)(loff - (u64)nh->shift), .psize = psize, .unsorted = 0, .cache_entry = NULL};
// find insertion position
const u32 target = nh->idx;
struct flexdb_tree_leaf_entry *const le = &node->leaf_entry;
if (target == node->count) {
le->anchors[node->count++] = t;
}
else {
struct flexdb_tree_anchor **const curr_anchor= &le->anchors[target];
memmove(curr_anchor+ 1, curr_anchor, sizeof(*curr_anchor) * (node->count - target));
*curr_anchor= t;
node->count++;
}
if (flexdb_tree_node_full(node)) {
flexdb_tree_split_leaf_node(node);
}
return t;
}
static void flexdb_tree_node_update_smallest_key(struct flexdb_tree_node *const since, const struct kv *const key)
{
u32 p_idx = since->parent_id;
struct flexdb_tree_node *tnode = since->parent;
while (tnode) {
if (p_idx == 0) {
p_idx = tnode->parent_id;
tnode = tnode->parent;
} else {
break;
}
}
if (tnode) {
// which means the current node is not the leftmost one
free(tnode->internal_entry.pivots[p_idx-1]);
tnode->internal_entry.pivots[p_idx-1] = kv_dup_key(key);
}
}
static void flexdb_tree_node_recycle_linked_list(const struct flexdb_tree_node *const node)
{
debug_assert(node->is_leaf);
debug_assert(node->tree->root != node); // should not be root
const struct flexdb_tree_leaf_entry *const le = &node->leaf_entry;
struct flexdb_tree_node *const prev = le->prev;
struct flexdb_tree_node *const next = le->next;
debug_assert(prev || next);
if (prev) {
prev->leaf_entry.next = next;
}
else {
node->tree->leaf_head = next;
}
if (next) {
next->leaf_entry.prev = prev;
}
}
static struct kv *flexdb_tree_node_find_smallest_key(struct flexdb_tree_node *const node)
{
struct flexdb_tree_node *tnode = node;
while (!tnode->is_leaf) {
debug_assert(tnode->count > 0);
tnode = tnode->internal_entry.children[0].node;
}
debug_assert(tnode->count > 0);
return tnode->leaf_entry.anchors[0]->key;
}
static void flexdb_tree_recycle_node(struct flexdb_tree_node *const node)
{
debug_assert(node->count == 0);
struct flexdb_tree_node *const parent = node->parent;
const u32 p_idx = node->parent_id;
u8 parent_exist = 0;
if (parent) {
parent_exist = 1;
}
if (node->tree->root == node) {
// case 1: node is the root
debug_assert(0); // this should not happen
} else if (parent->count == 1) {
// case 2: only one pivot in parent
debug_assert(p_idx <= 1);
const u32 s_idx = p_idx == 0 ? 1 : 0;
const s64 s_shift = parent->internal_entry.children[s_idx].shift;
struct flexdb_tree_node *const s_node = parent->internal_entry.children[s_idx].node;
// pre-process the linked list if node is leaf
if (node->is_leaf) {
flexdb_tree_node_recycle_linked_list(node);
}
// free nodes
slab_free_unsafe(node->tree->node_slab, node);
// do recycling
if (s_node->tree->root == parent) {
free(parent->internal_entry.pivots[0]);
slab_free_unsafe(node->tree->node_slab, parent);
// case 2.1: parent is root
// no more shift pointers, so need to apply
flexdb_tree_node_shift_apply(s_node, s_shift);
// set new root to sibling
s_node->tree->root = s_node;
s_node->parent = NULL;
s_node->parent_id = 0;
debug_assert(!s_node->is_leaf || s_node->tree->leaf_head == s_node);
} else {
// case 2.2: parent is not root
debug_assert(node->parent->parent);
struct flexdb_tree_node *const gparent = parent->parent;
const u32 gp_idx = parent->parent_id;
gparent->internal_entry.children[gp_idx].node = s_node;
gparent->internal_entry.children[gp_idx].shift += s_shift;
s_node->parent = gparent;
s_node->parent_id = gp_idx;
free(parent->internal_entry.pivots[0]);
slab_free_unsafe(node->tree->node_slab, parent);
// now the gparent's child points to s_node
// if the sibling is the right one, we need to update the pivots
struct kv *new_pivot = NULL;
if (s_idx == 1) {
debug_assert(s_node->count > 0);
new_pivot = flexdb_tree_node_find_smallest_key(s_node);
}
if (new_pivot) {
if (gp_idx == 0) {
flexdb_tree_node_update_smallest_key(gparent, new_pivot);
} else {
free(gparent->internal_entry.pivots[gp_idx-1]);
gparent->internal_entry.pivots[gp_idx-1] = kv_dup_key(new_pivot);
}
}
}
parent_exist = 0;
} else {
// case 3: normal case, remove one pivot from parent..
if (node->is_leaf) {
flexdb_tree_node_recycle_linked_list(node);
}
slab_free_unsafe(node->tree->node_slab, node);
struct flexdb_tree_internal_entry *const ie = &parent->internal_entry;
if (p_idx == 0) {
// case 3.1: first child
free(ie->pivots[0]);
memmove(ie->pivots, &ie->pivots[1],
(parent->count-1) * sizeof(ie->pivots[0]));
memmove(ie->children, &ie->children[1],
(parent->count) * sizeof(ie->children[0]));
parent->count--;
for (u32 i=0; i<parent->count+1; i++) {
debug_assert(ie->children[i].node->parent_id > 0);
ie->children[i].node->parent_id--;
}
// smilarly, needs to update the pivot in this case
debug_assert(ie->children[0].node->count > 0);
struct kv *new_pivot = flexdb_tree_node_find_smallest_key(parent);
flexdb_tree_node_update_smallest_key(parent, new_pivot);
} else {
// case 3.2: not first child, quite similar
// but no need to update the key here
free(ie->pivots[p_idx-1]);
memmove(&ie->pivots[p_idx-1], &ie->pivots[p_idx],
(parent->count - p_idx) * sizeof(ie->pivots[0]));
memmove(&ie->children[p_idx], &ie->children[p_idx+1],
(parent->count - p_idx + 1) * sizeof(ie->children[0]));
parent->count--;
for (u32 i=p_idx; i<parent->count+1; i++) {
debug_assert(ie->children[i].node->parent_id > 0);
ie->children[i].node->parent_id--;
}
}
}
if (parent_exist == 1 && flexdb_tree_node_empty(parent)) {
flexdb_tree_recycle_node(parent);
}
}
// }}} sparse index tree
// {{{ cache
#define FLEXDB_CACHE_ENTRY_FIND_EQ (1u << 31)
static struct flexdb_cache *flexdb_cache_create(struct flexdb *const db, const u64 cache_cap_mb)
{
struct flexdb_cache *const cache = malloc(sizeof(*cache));
cache->cap = cache_cap_mb * (1lu << 20);
cache->db = db;
// init partitions
for (u32 i=0; i<FLEXDB_CACHE_PARTITION_COUNT; i++) {
struct flexdb_cache_partition *const p = &cache->partitions[i];
p->cap = cache->cap / FLEXDB_CACHE_PARTITION_COUNT;
p->size = 0;
p->entry_slab = slab_create(sizeof(struct flexdb_cache_entry), 2lu << 20);
if (p->entry_slab == NULL) {
printf("entry slab alloc failed for partition %u\n", i);
exit(1);
}
p->cache = cache;
spinlock_init(&p->spinlock);
p->tick = NULL;
}
return cache;
}
static inline u32 flexdb_cache_entry_get_access(const struct flexdb_cache_entry *const entry)
{
return entry->access;
}
static inline void flexdb_cache_entry_set_access(struct flexdb_cache_entry *const entry)
{
if (entry->access < FLEXDB_CACHE_ENTRY_CHANCE) {
entry->access = FLEXDB_CACHE_ENTRY_CHANCE;
}
}
static inline void flexdb_cache_entry_set_access_warmup(struct flexdb_cache_entry *const entry)
{
entry->access = FLEXDB_CACHE_ENTRY_CHANCE_WARMUP;
}
static inline void flexdb_cache_entry_waste_access(struct flexdb_cache_entry *const entry)
{
if (entry->access > 0) entry->access--;
}
static inline u32 flexdb_cache_entry_get_refcnt(const struct flexdb_cache_entry *const entry)
{
return entry->refcnt;
}
static inline void flexdb_cache_entry_inc_refcnt(struct flexdb_cache_entry *const entry)
{
entry->refcnt++;
}
static inline void flexdb_cache_entry_dec_refcnt(struct flexdb_cache_entry *const entry)
{
entry->refcnt--;
}
static inline void flexdb_cache_entry_set_frag(struct flexdb_cache_entry *const entry, const u64 frag)
{
if (frag > (entry->count >> 1)) {
entry->frag = 1; // need defrag
}
}
static inline void flexdb_cache_entry_clear_frag(struct flexdb_cache_entry *const entry)
{
entry->frag = 0;
}
static inline u16 flexdb_cache_entry_get_frag(const struct flexdb_cache_entry *const entry)
{
return entry->frag;
}
static struct flexdb_cache_entry *flexdb_cache_partition_find_victim(struct flexdb_cache_partition *const partition)
{
struct flexdb_cache_entry *victim = partition->tick;
u32 access = flexdb_cache_entry_get_access(victim);
u32 refcnt = flexdb_cache_entry_get_refcnt(victim);
while (access > 0 || refcnt > 0) {
if (refcnt == 0) {
flexdb_cache_entry_waste_access(victim); // lose one chance
}
victim = victim->next;
partition->tick = victim;
access = flexdb_cache_entry_get_access(victim);
refcnt = flexdb_cache_entry_get_refcnt(victim);
}
return victim;
}
static inline void flexdb_cache_partition_free_kv(struct flexdb_cache_partition *const partition, struct kv *const kv)
{
// no need for a lock as it is already au64
partition->size -= kv_size(kv);
free(kv);
}
// returns freed space
static u64 flexdb_cache_partition_free_entry(
struct flexdb_cache_partition *const partition, struct flexdb_cache_entry *const entry)
{
// if we call this function, it must be a known interval
const u64 size = sizeof(*entry) + entry->size;
for (u32 i=0; i<entry->count; i++) {
free(entry->kv_interval[i]);
}
if (entry->anchor) {
entry->anchor->cache_entry = NULL;
} // else, this is an orphan..
entry->prev->next = entry->next;
entry->next->prev = entry->prev;
slab_free_unsafe(partition->entry_slab, entry);
return size;
}
// caller holds the spinlock
static u64 flexdb_cache_partition_regain(struct flexdb_cache_partition *const partition, const u64 size)
{
if (size > partition->cap) {
return 0;
}
const u64 need = size - (partition->cap - partition->size);
u64 gained = 0;
struct flexdb_cache_entry *victim = NULL;
while (gained < need) {
victim = flexdb_cache_partition_find_victim(partition);
if (partition->tick == victim) {
if (victim->next != victim) {
partition->tick = victim->next;
}
else {
partition->tick = NULL;
}
}
gained += flexdb_cache_partition_free_entry(partition, victim);
}
// printf("total gained %lu bytes\n", gained);
partition->size -= gained;
return gained;
}
// TODO: alloc and add size first, then clean the partition
// caller holds the spinlock
static struct flexdb_cache_entry *flexdb_cache_partition_alloc_entry(
struct flexdb_cache_partition *const partition, struct flexdb_tree_anchor *const anchor)
{
struct flexdb_cache_entry *entry = slab_alloc_unsafe(partition->entry_slab);
memset(entry, 0, sizeof(*entry));
entry->anchor = anchor;
if (partition->tick == NULL) {
partition->tick = entry;
entry->prev = entry;
entry->next = entry;
} else {
entry->prev = partition->tick->prev;
entry->next = partition->tick;
entry->prev->next = entry;
entry->next->prev = entry;
// cache->tick unchanged
}
return entry;
}
// static inline void flexdb_cache_kv_access(struct kv *const kv)
// {
// kv->priv = 1;
// }
// caller holds the spinlock
static inline void flexdb_cache_partition_calibrate(struct flexdb_cache_partition *const partition)
{
if (partition->size > partition->cap) {
flexdb_cache_partition_regain(partition, partition->size - partition->cap);
}
}
static inline struct kv *flexdb_cache_entry_read_kv(const struct flexdb_cache_entry *const entry, const u32 idx)
{
return entry->kv_interval[idx];
}
static inline u16 flexdb_cache_entry_fingerprint(const u32 hash32)
{
const u16 fp = ((u16)hash32) ^ ((u16)(hash32 >> 16));
return fp ? fp : 1;
}
static void flexdb_cache_entry_insert(
struct flexdb_cache_entry *const entry, struct kv *const kv, const u32 idx)
{
debug_assert(entry);
debug_assert(kv);
const u32 count = entry->count;
if (idx < count) {
memmove(&entry->kv_interval[idx+1], &entry->kv_interval[idx], (count - idx) * sizeof(entry->kv_interval[0]));
memmove(&entry->kv_fps[idx+1], &entry->kv_fps[idx], (count - idx) * sizeof(entry->kv_fps[0]));
}
entry->kv_interval[idx] = kv;
entry->kv_fps[idx] = flexdb_cache_entry_fingerprint(kv->hashlo);
entry->size += kv_size(kv);
entry->count++;
}
static void flexdb_cache_entry_append(
struct flexdb_cache_entry *const entry, struct kv *const kv)
{
entry->kv_interval[entry->count] = kv;
entry->kv_fps[entry->count] = flexdb_cache_entry_fingerprint(kv->hashlo);
entry->size += kv_size(kv);
entry->count++;
}
static void flexdb_cache_entry_delete(
struct flexdb_cache_entry *const entry, const u32 idx,
struct flexdb_cache_partition *const partition)
{
const u32 count = entry->count;
struct kv *const okv = entry->kv_interval[idx];
entry->count--;
entry->size -= kv_size(okv);
flexdb_cache_partition_free_kv(partition, okv);
if (idx + 1 < count) {
memmove(&entry->kv_interval[idx], &entry->kv_interval[idx+1], (count - idx) * sizeof(entry->kv_interval[0]));
memmove(&entry->kv_fps[idx], &entry->kv_fps[idx+1], (count - idx) * sizeof(entry->kv_fps[0]));
}
entry->kv_fps[count-1] = 0;
entry->kv_interval[count-1] = NULL;
}
static void flexdb_cache_entry_replace(
struct flexdb_cache_entry *const entry, struct kv *const kv,
const u32 idx, struct flexdb_cache_partition *const partition)
{
struct kv *const okv = entry->kv_interval[idx];
const u32 osize = (u32)kv_size(okv);
flexdb_cache_partition_free_kv(partition, okv);
entry->kv_interval[idx] = kv;
const u32 size = (u32)kv_size(kv);
entry->size += (size - osize);
// entry->kv_fps[idx] = flexdb_cache_entry_fingerprint(kv->hashlo);
}
static u8 *flexdb_cache_partition_read_interval(
const struct flexdb_cache_partition *const partition,
const struct flexdb_tree_anchor *const anchor, const u64 loff, u64 *const frag, u8 *const itvbuf,
void *const priv)
{
if (anchor->psize == 0) {
return NULL;
}
const struct flexdb *const db = partition->cache->db;
// read db from flexfile to cache
#ifdef FLEXDB_USE_RRING
const ssize_t r = flexfile_read_fragmentation_rring(
db->flexfile, itvbuf, loff, anchor->psize, frag, (struct flexfile_rring *)priv);
#else
const ssize_t r = flexfile_read_fragmentation(db->flexfile, itvbuf, loff, anchor->psize, frag);
(void)priv;
#endif
debug_assert(r == (ssize_t)anchor->psize);
(void)r;
return itvbuf;
}
static struct flexdb_cache_entry *flexdb_cache_partition_get_entry_new_anchor(
struct flexdb_cache_partition *const partition, struct flexdb_tree_anchor *const anchor)
{
spinlock_lock(&partition->spinlock);
struct flexdb_cache_entry *fce = anchor->cache_entry;
debug_assert(!fce);
fce = flexdb_cache_partition_alloc_entry(partition, anchor);
partition->size += sizeof(*fce);
flexdb_cache_entry_set_access(fce);
flexdb_cache_entry_inc_refcnt(fce);
anchor->cache_entry = fce;
flexdb_cache_partition_calibrate(partition);
spinlock_unlock(&partition->spinlock);
return fce;
}
static u32 flexdb_cache_entry_kv_interval_dedup(
struct kv **const kv_interval, u16 *const kv_fps, u8 *const anchor_count)
{
const u8 count = *anchor_count;
u32 size = 0;
u8 idx = 0; // slower
u8 sidx = 1; // faster
// now all the kv->hashlo is still there so we can use them
while (sidx < count) {
const bool identical = (kv_interval[idx]->hashlo == kv_interval[sidx]->hashlo) &&
kv_match(kv_interval[idx], kv_interval[sidx]);
if (identical) {
free(kv_interval[idx]);
} else {
size += kv_size(kv_interval[idx++]);
}
kv_interval[idx] = kv_interval[sidx]; // move sidx to the next slot
kv_fps[idx] = kv_fps[sidx];
sidx++;
}
size += kv_size(kv_interval[idx++]); // the last one
*anchor_count = idx;
return size;
}
static inline int kv_compare_priv(const void *const _kv1, const void *const _kv2)
{
const struct kv *const kv1 = *(const struct kv **)_kv1;
const struct kv *const kv2 = *(const struct kv **)_kv2;
const u32 len = kv1->klen < kv2->klen ? kv1->klen : kv2->klen;
const int cmp = memcmp(kv1->kv, kv2->kv, (size_t)len);
if (cmp) {
return cmp;
}
const u64 x1 = ((u64)kv1->klen) << 32 | kv1->privhi;
const u64 x2 = ((u64)kv2->klen) << 32 | kv2->privhi;
debug_assert(x1 != x2);
return x1 < x2 ? -1 : 1;
}
static void flexdb_cache_partition_load_interval(
struct flexdb_cache_partition *const partition, struct flexdb_tree_anchor *const anchor,
struct flexdb_cache_entry *const fce, const u64 loff, u8 *const itvbuf, void *const priv)
{
u64 frag = 0;
u8 *const interval = flexdb_cache_partition_read_interval(partition, anchor, loff, &frag, itvbuf, priv);
fce->size = 0;
fce->count = 0;
if (interval != NULL) { // which means psize > 0
const u8 *kv = interval;
size_t psize = 0;
const u8 * const end = interval + anchor->psize;
while (kv < end) {
struct kv *const r = kv128_decode_kv(kv, NULL, &psize); // malloc
r->privhi = (u32)fce->count;
// r->privhi -> the idx
// r->hashlo -> the crc32 of the kv
flexdb_cache_entry_append(fce, r); // here the fps are calculated
kv += psize;
}
debug_assert(kv == interval+anchor->psize);
if (anchor->unsorted > 0) {
// 1. sort
qsort(fce->kv_interval, fce->count, sizeof(fce->kv_interval[0]), kv_compare_priv);
for (u32 i=0; i<fce->count; i++) {
fce->kv_fps[i] = flexdb_cache_entry_fingerprint(fce->kv_interval[i]->hashlo);
}
// 2. de-duplicate, and get the latest .count, .size, .psize
const u32 new_size = flexdb_cache_entry_kv_interval_dedup(fce->kv_interval, fce->kv_fps, &fce->count);
debug_assert(new_size <= fce->size);
fce->size = new_size;
// .count and .size now updated, .psize is still the original one to record the correct offset
// but leave .unsorted as positive for writers to re-write the whole interval
// now any readers can safely consume this fce
}
// for (u32 j=0; j<fce->count; j++) {
// fce->kv_interval[j]->priv = 0; // clean the priv here, to count access when evicting
// }
} else {
// psize == 0
debug_assert(anchor->psize == 0);
}
flexdb_cache_entry_set_frag(fce, frag); // it is now by default
}
static struct flexdb_cache_entry *flexdb_cache_partition_get_entry(
struct flexdb_cache_partition *const partition, struct flexdb_tree_anchor *const anchor, const u64 loff,
u8 *const itvbuf, void *const priv)
{
u64 total_size = 0;
spinlock_lock(&partition->spinlock);
struct flexdb_cache_entry *fce = anchor->cache_entry;
if (fce) { // hit
flexdb_cache_entry_inc_refcnt(fce);
if (unlikely(fce->loading == 1)) {
spinlock_unlock(&partition->spinlock);
while (fce->loading == 1) {
cpu_pause();
}
spinlock_lock(&partition->spinlock);
}
} else { // miss; slow
fce = flexdb_cache_partition_alloc_entry(partition, anchor);
total_size += sizeof(*fce);
flexdb_cache_entry_inc_refcnt(fce);
fce->loading = 1; // let me load it!
anchor->cache_entry = fce;
// do real loading, no need for a lock
spinlock_unlock(&partition->spinlock);
flexdb_cache_partition_load_interval(partition, anchor, fce, loff, itvbuf, priv);
total_size += fce->size;
fce->loading = 0;
spinlock_lock(&partition->spinlock);
}
// TODO: trylock + wait ?
if (total_size > 0) {
partition->size += total_size;
flexdb_cache_partition_calibrate(partition);
}
flexdb_cache_entry_set_access(fce);
spinlock_unlock(&partition->spinlock);
return fce;
}
static inline struct flexdb_cache_entry *flexdb_cache_partition_get_entry_unsorted(
struct flexdb_cache_partition *const partition, struct flexdb_tree_anchor *const anchor, const u64 loff,
u8 *const itvbuf, void *const priv)
{
spinlock_lock(&partition->spinlock);
struct flexdb_cache_entry *fce = anchor->cache_entry;
spinlock_unlock(&partition->spinlock);