-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathord.c
More file actions
2761 lines (2487 loc) · 72.5 KB
/
ord.c
File metadata and controls
2761 lines (2487 loc) · 72.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016--2021 Wu, Xingbo <wuxb45@gmail.com>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
// headers {{{
#include <assert.h> // static_assert
#include "lib.h"
#include "ctypes.h"
#include "kv.h"
#include "ord.h"
// }}} headers
// skiplist {{{
#define SL_MAXH ((32))
struct skipnode {
struct kv * kv;
union {
struct skipnode * ptr;
au64 a;
} next[0];
};
struct skippath {
struct skipnode * vec[SL_MAXH][2]; // left and right
};
struct skiplist {
mutex lock;
u64 padding[7];
struct kvmap_mm mm;
u64 height;
struct skipnode n0;
};
struct skiplist *
skiplist_create(const struct kvmap_mm * const mm)
{
const size_t sz = sizeof(struct skiplist) + (sizeof(void *) * SL_MAXH);
struct skiplist * const list = yalloc(sz);
if (list == NULL)
return NULL;
memset(list, 0, sz);
mutex_init(&(list->lock));
list->mm = mm ? (*mm) : kvmap_mm_dup;
list->height = 1;
return list;
}
static inline struct skipnode *
skiplist_next(struct skipnode * const node, const u64 h)
{
return (struct skipnode *)(void *)atomic_load_explicit(&node->next[h].a, MO_ACQUIRE);
}
static inline void
skiplist_update_next(struct skipnode * const node, const u64 h, struct skipnode * const next)
{
atomic_store_explicit(&(node->next[h].a), (u64)next, MO_RELEASE);
}
static inline void
skiplist_lock(struct skiplist * const list)
{
mutex_lock(&(list->lock));
}
static inline void
skiplist_unlock(struct skiplist * const list)
{
mutex_unlock(&(list->lock));
}
// perform a search on skiplist and record the path
// return true for match, false for mismatch
// *out will have the node >= key
// on match, the path could be incomplete (the path is not used unless for del, which will be handled separately)
// on mismatch, at every level of the path, left < key < right (or NULL); a new key should be inserted in between
static bool
skiplist_search_ge_path(struct skiplist * const list, const struct kref * const key,
struct skipnode ** const out, struct skippath * const path, u64 h)
{
debug_assert(h);
struct skipnode * left = &(list->n0); // leftmost
struct skipnode * next;
while ((--h) < SL_MAXH) {
while ((next = skiplist_next(left, h)) != NULL) {
const int cmp = kref_kv_compare(key, next->kv);
if (cmp > 0) { // forward and continue
left = next;
} else if (cmp < 0) { // done at this level
break;
} else { // match
*out = next;
return true;
}
}
path->vec[h][0] = left;
path->vec[h][1] = next;
}
// no match; return the first node > key
*out = next;
return false;
}
static bool
skiplist_search_ge(struct skiplist * const list, const struct kref * const key,
struct skipnode ** const out)
{
u64 h = list->height;
debug_assert(h);
struct skipnode * left = &(list->n0); // leftmost
struct skipnode * next;
while ((--h) < SL_MAXH) {
while ((next = skiplist_next(left, h)) != NULL) {
const int cmp = kref_kv_compare(key, next->kv);
if (cmp > 0) { // forward and continue
left = next;
} else if (cmp < 0) { // done at this level
break;
} else { // match
*out = next;
return true;
}
}
}
// no match; return the first node > key
*out = next;
return false;
}
struct kv *
skiplist_get(struct skiplist * const list, const struct kref * const key, struct kv * const out)
{
struct skipnode * node;
if (skiplist_search_ge(list, key, &node)) {
debug_assert(node && node->kv);
return list->mm.out(node->kv, out);
}
return NULL;
}
bool
skiplist_probe(struct skiplist * const list, const struct kref * const key)
{
struct skipnode * node;
return skiplist_search_ge(list, key, &node);
}
// generate a random height; if it's higher than hh, fill the path
static u64
skiplist_random_height(struct skiplist * const list, struct skippath * const path, const u64 hh)
{
const u64 r = random_u64(); // r can be 0
// 1 <= height <= 32
const u64 height = (u64)(__builtin_ctzl(r ? r : 1) >> 1) + 1;
for (u64 i = hh; i < height; i++) {
path->vec[i][0] = &(list->n0); // the very beginning
path->vec[i][1] = NULL; // the very end
}
return height;
}
static bool
skiplist_insert_height(struct skiplist * const list, struct skippath * const path,
struct kv * const kv, const u64 height)
{
if (height > list->height)
list->height = height;
const u64 nodesize = sizeof(list->n0) + (sizeof(list->n0.next[0]) * height);
struct skipnode * const node = malloc(nodesize);
if (!node) { // malloc failed
list->mm.free(kv, list->mm.priv);
return false;
}
kv->privptr = NULL; // end of chain
node->kv = kv;
for (u64 i = 0; i < height; i++) {
node->next[i].ptr = path->vec[i][1];
skiplist_update_next(path->vec[i][0], i, node);
}
return true;
}
static bool
skiplist_insert_fix_path(struct skippath * const path, const u64 height, struct kv * const kv)
{
for (u64 h = 0; h < height; h++) { // must check every level
struct skipnode * left = path->vec[h][0];
struct skipnode * right = left->next[h].ptr;
if (likely(right == path->vec[h][1]))
continue;
debug_assert(right); // insertions only; won't disappear
do {
const int cmp = kv_compare(kv, right->kv);
if (cmp < 0) { // right is ok
break;
} else if (cmp > 0) { // forward path[h]
left = right;
right = left->next[h].ptr;
} else {
kv->privptr = right->kv;
right->kv = kv;
// insertion is already done
return true;
}
} while (right);
path->vec[h][0] = left;
path->vec[h][1] = right;
}
// should continue insert
return false;
}
static bool
skiplist_insert_helper(struct skiplist * const list, struct skippath * const path,
const u64 hh, struct kv * const kv, const bool safe)
{
const u64 height = skiplist_random_height(list, path, hh);
if (safe) { // other writers may insert keys to the path
skiplist_lock(list);
if (skiplist_insert_fix_path(path, height, kv)) {
skiplist_unlock(list);
return true;
}
}
const bool r = skiplist_insert_height(list, path, kv, height);
if (safe)
skiplist_unlock(list);
return r;
}
static bool
skiplist_put_helper(struct skiplist * const list, struct kv * const kv, const bool safe)
{
struct kv * const newkv = list->mm.in(kv, list->mm.priv);
if (!newkv)
return false;
struct kref kref;
kref_ref_kv(&kref, kv);
struct skipnode * node;
struct skippath path;
const u64 hh = list->height;
const bool r = skiplist_search_ge_path(list, &kref, &node, &path, hh);
if (r) { // replace
if (safe) {
skiplist_lock(list);
newkv->privptr = node->kv;
node->kv = newkv;
skiplist_unlock(list);
} else {
list->mm.free(node->kv, list->mm.priv);
newkv->privptr = NULL;
node->kv = newkv;
}
return true;
}
return skiplist_insert_helper(list, &path, hh, newkv, safe);
}
bool
skiplist_put(struct skiplist * const list, struct kv * const kv)
{
return skiplist_put_helper(list, kv, false);
}
bool
skipsafe_put(struct skiplist * const list, struct kv * const kv)
{
return skiplist_put_helper(list, kv, true);
}
static bool
skiplist_merge_helper(struct skiplist * const list, const struct kref * const kref,
kv_merge_func uf, void * const priv, const bool safe)
{
struct skipnode * node;
struct skippath path;
const u64 hh = list->height;
const bool r = skiplist_search_ge_path(list, kref, &node, &path, hh);
if (r) {
if (safe) {
skiplist_lock(list);
struct kv * const old = node->kv;
struct kv * const kv = uf(old, priv);
if ((kv != old) && (kv != NULL)) { // replace
struct kv * const newkv = list->mm.in(kv, list->mm.priv);
if (!newkv) {
skiplist_unlock(list);
return false;
}
newkv->privptr = old;
node->kv = newkv;
}
skiplist_unlock(list);
} else { // unsafe
struct kv * const old = node->kv;
struct kv * const kv = uf(old, priv);
if (kv != old) { // replace
struct kv * const newkv = list->mm.in(kv, list->mm.priv);
if (!newkv)
return false;
list->mm.free(old, list->mm.priv);
newkv->privptr = NULL;
node->kv = newkv;
}
}
return true;
}
struct kv * const kv = uf(NULL, priv);
if (!kv) // do nothing
return true;
struct kv * const newkv = list->mm.in(kv, list->mm.priv);
if (!newkv)
return false;
return skiplist_insert_helper(list, &path, hh, newkv, safe);
}
bool
skiplist_merge(struct skiplist * const list, const struct kref * const kref,
kv_merge_func uf, void * const priv)
{
return skiplist_merge_helper(list, kref, uf, priv, false);
}
bool
skipsafe_merge(struct skiplist * const list, const struct kref * const kref,
kv_merge_func uf, void * const priv)
{
return skiplist_merge_helper(list, kref, uf, priv, true);
}
bool
skiplist_inp(struct skiplist * const list, const struct kref * const key,
kv_inp_func uf, void * const priv)
{
struct skipnode * node;
const bool r = skiplist_search_ge(list, key, &node);
uf(r ? node->kv : NULL, priv);
return r;
}
// return the previous node if ret->next matches the key
static u64
skiplist_search_del_prev(struct skiplist * const list, const struct kref * const key,
struct skipnode ** const prev)
{
debug_assert(list->height);
u64 h = list->height;
struct skipnode * left = &(list->n0); // leftmost
struct skipnode * next;
while ((--h) < SL_MAXH) {
while ((next = skiplist_next(left, h)) != NULL) {
const int cmp = kref_kv_compare(key, next->kv);
if (cmp > 0) { // forward and continue
left = next;
} else if (cmp < 0) { // done at this level
break;
} else { // match
*prev = left;
return h;
}
}
}
return SL_MAXH;
}
// for unsafe skiplist only
bool
skiplist_del(struct skiplist * const list, const struct kref * const key)
{
struct skipnode * prev = NULL;
u64 h = skiplist_search_del_prev(list, key, &prev);
if (h == SL_MAXH)
return false;
struct skipnode * const victim = skiplist_next(prev, h);
prev->next[h].ptr = victim->next[h].ptr;
while ((--h) < SL_MAXH) {
while (prev->next[h].ptr != victim)
prev = prev->next[h].ptr;
prev->next[h].ptr = victim->next[h].ptr;
}
list->mm.free(victim->kv, list->mm.priv);
free(victim);
return true;
}
void
skiplist_clean(struct skiplist * const list)
{
struct skipnode * iter = list->n0.next[0].ptr;
while (iter) {
struct skipnode * const next = iter->next[0].ptr;
struct kv * kviter = iter->kv;
while (kviter) { // free the chain
struct kv * tmp = kviter->privptr;
list->mm.free(kviter, list->mm.priv);
kviter = tmp;
}
free(iter);
iter = next;
}
for (u64 i = 0; i < SL_MAXH; i++)
list->n0.next[i].ptr = NULL;
}
void
skiplist_destroy(struct skiplist * const list)
{
skiplist_clean(list);
free(list);
}
void
skiplist_fprint(struct skiplist * const list, FILE * const out)
{
u64 hs[SL_MAXH] = {};
u32 costs[SL_MAXH];
struct skipnode * nexts[SL_MAXH+1];
const u64 hh = list->height;
debug_assert(hh && hh < SL_MAXH);
for (u64 i = 0; i < hh; i++) {
nexts[i] = skiplist_next(&list->n0, i);
costs[i] = 1u;
}
nexts[hh] = NULL;
struct skipnode * iter = nexts[0]; // the first item
u64 totcost = 0;
u64 totkv = 0;
while (iter) {
u64 h = 0;
while ((h+1) < SL_MAXH && nexts[h+1] == iter) {
costs[h] = 1;
nexts[h] = skiplist_next(iter, h);
h++;
}
nexts[h] = skiplist_next(iter, h);
hs[h]++;
u32 cost = 0;
for (u64 i = h; i < hh; i++)
cost += costs[i];
// uncomment to print
//fprintf(out, "h=%2lu c=%3u", h, cost);
//kv_print(iter->kv, "sn", out);
costs[h]++;
iter = skiplist_next(iter, 0);
totcost += cost;
totkv++;
}
const double avgcost = (double)totcost / (double)totkv;
fprintf(out, "SKIPLIST count %lu height %lu avgcost %.3lf\n", totkv, hh, avgcost);
for (u64 i = 0; i < hh; i += 4) {
fprintf(out, "SKIPLIST H[%lu] %lu H[%lu] %lu H[%lu] %lu H[%lu] %lu\n",
i, hs[i], i+1, hs[i+1], i+2, hs[i+2], i+3, hs[i+3]);
}
}
struct skiplist_iter {
struct skipnode * curr;
struct skiplist * list;
};
struct skiplist_iter *
skiplist_iter_create(struct skiplist * const list)
{
struct skiplist_iter * const iter = malloc(sizeof(*iter));
if (iter == NULL)
return NULL;
iter->curr = NULL; // invalid
iter->list = list;
return iter;
}
void
skiplist_iter_seek(struct skiplist_iter * const iter, const struct kref * const key)
{
struct skiplist * list = iter->list;
skiplist_search_ge(list, key, &iter->curr);
}
bool
skiplist_iter_valid(struct skiplist_iter * const iter)
{
return iter->curr != NULL;
}
struct kv *
skiplist_iter_peek(struct skiplist_iter * const iter, struct kv * const out)
{
if (!skiplist_iter_valid(iter))
return NULL;
struct kv * const curr = iter->curr->kv;
struct kv * const ret = iter->list->mm.out(curr, out);
return ret;
}
bool
skiplist_iter_kref(struct skiplist_iter * const iter, struct kref * const kref)
{
if (!skiplist_iter_valid(iter))
return false;
kref_ref_kv(kref, iter->curr->kv);
return true;
}
bool
skiplist_iter_kvref(struct skiplist_iter * const iter, struct kvref * const kvref)
{
if (!skiplist_iter_valid(iter))
return false;
kvref_ref_kv(kvref, iter->curr->kv);
return true;
}
void
skiplist_iter_skip1(struct skiplist_iter * const iter)
{
if (skiplist_iter_valid(iter))
iter->curr = skiplist_next(iter->curr, 0);
}
void
skiplist_iter_skip(struct skiplist_iter * const iter, const u32 nr)
{
for (u32 i = 0; i < nr; i++) {
if (!skiplist_iter_valid(iter))
return;
iter->curr = skiplist_next(iter->curr, 0);
}
}
struct kv *
skiplist_iter_next(struct skiplist_iter * const iter, struct kv * const out)
{
struct kv * const ret = skiplist_iter_peek(iter, out);
skiplist_iter_skip1(iter);
return ret;
}
bool
skiplist_iter_inp(struct skiplist_iter * const iter, kv_inp_func uf, void * const priv)
{
struct kv * const kv = iter->curr ? iter->curr->kv : NULL;
uf(kv, priv); // call uf even if (kv == NULL)
return kv != NULL;
}
void
skiplist_iter_destroy(struct skiplist_iter * const iter)
{
free(iter);
}
const struct kvmap_api kvmap_api_skiplist = {
.ordered = true,
.unique = true,
.put = (void *)skiplist_put,
.get = (void *)skiplist_get,
.probe = (void *)skiplist_probe,
.del = (void *)skiplist_del,
.inpr = (void *)skiplist_inp,
.inpw = (void *)skiplist_inp,
.merge = (void *)skiplist_merge,
.iter_create = (void *)skiplist_iter_create,
.iter_seek = (void *)skiplist_iter_seek,
.iter_valid = (void *)skiplist_iter_valid,
.iter_peek = (void *)skiplist_iter_peek,
.iter_kref = (void *)skiplist_iter_kref,
.iter_kvref = (void *)skiplist_iter_kvref,
.iter_skip1 = (void *)skiplist_iter_skip1,
.iter_skip = (void *)skiplist_iter_skip,
.iter_next = (void *)skiplist_iter_next,
.iter_inp = (void *)skiplist_iter_inp,
.iter_destroy = (void *)skiplist_iter_destroy,
.clean = (void *)skiplist_clean,
.destroy = (void *)skiplist_destroy,
.fprint = (void *)skiplist_fprint,
};
const struct kvmap_api kvmap_api_skipsafe = {
.ordered = true,
.unique = true,
.irefsafe = true,
.put = (void *)skipsafe_put,
.get = (void *)skiplist_get,
.probe = (void *)skiplist_probe,
.del = NULL,
.inpr = (void *)skiplist_inp,
.inpw = (void *)skiplist_inp,
.merge = (void *)skipsafe_merge,
.iter_create = (void *)skiplist_iter_create,
.iter_seek = (void *)skiplist_iter_seek,
.iter_valid = (void *)skiplist_iter_valid,
.iter_peek = (void *)skiplist_iter_peek,
.iter_kref = (void *)skiplist_iter_kref,
.iter_kvref = (void *)skiplist_iter_kvref,
.iter_skip1 = (void *)skiplist_iter_skip1,
.iter_skip = (void *)skiplist_iter_skip,
.iter_next = (void *)skiplist_iter_next,
.iter_inp = (void *)skiplist_iter_inp,
.iter_destroy = (void *)skiplist_iter_destroy,
.clean = (void *)skiplist_clean,
.destroy = (void *)skiplist_destroy,
.fprint = (void *)skiplist_fprint,
};
static void *
skiplist_kvmap_api_create(const char * const name, const struct kvmap_mm * const mm, char ** args)
{
if (strcmp(name, "skiplist") && strcmp(name, "skipsafe"))
return NULL;
(void)args;
return skiplist_create(mm);
}
__attribute__((constructor))
static void
skiplist_kvmap_api_init(void)
{
kvmap_api_register(0, "skiplist", "", skiplist_kvmap_api_create, &kvmap_api_skiplist);
kvmap_api_register(0, "skipsafe", "", skiplist_kvmap_api_create, &kvmap_api_skipsafe);
}
// }}} skiplist
// bptree {{{
#define BPTREE_META_FO ((64))
#define BPTREE_LEAF_FO ((BPTREE_META_FO * 2))
#define BPTREE_CMP_MATCH ((1u << 31))
// struct {{{
struct bpnode {
bool leaf; // is_leaf
u32 nr; // number of keys
// links to the siblings at the same level
struct bpnode * prev;
struct bpnode * next;
union {
// meta node
struct {
struct kv * key[BPTREE_META_FO];
struct bpnode * sub[BPTREE_META_FO];
};
// leaf node
struct kv * kvs[BPTREE_LEAF_FO];
};
};
static_assert(sizeof(struct bpnode) < PGSZ, "bpnode size");
struct bptree {
u64 depth; // optional
struct bpnode * root;
struct slab * slab;
struct kvmap_mm mm;
};
// }}} struct
// helpers {{{
static inline struct bpnode *
bptree_alloc_node(struct bptree * const tree, const bool leaf)
{
struct bpnode * const node = slab_alloc_unsafe(tree->slab);
node->leaf = leaf;
node->nr = 0;
node->prev = NULL;
node->next = NULL;
// the pointers/keys are uninitialized
return node;
}
static u32
bptree_leaf_search_ge(struct bpnode * const node, const struct kref * const key)
{
debug_assert(node && node->leaf);
u32 l = 0;
u32 r = node->nr;
while ((l + 2) < r) {
const u32 m = (l + r) >> 1;
struct kv * const curr = node->kvs[m];
cpu_prefetch0(curr);
cpu_prefetch0(node->kvs+((l+m)>>1));
cpu_prefetch0(node->kvs+((m+1+r)>>1));
const int cmp = kref_kv_compare(key, curr);
if (cmp < 0)
r = m;
else if (cmp > 0)
l = m + 1;
else
return m | BPTREE_CMP_MATCH;
}
while (l < r) {
const u32 m = (l + r) >> 1;
struct kv * const curr = node->kvs[m];
const int cmp = kref_kv_compare(key, curr);
if (cmp < 0)
r = m;
else if (cmp > 0)
l = m + 1;
else
return m | BPTREE_CMP_MATCH;
}
return l;
}
// for insert
static u32
bptree_meta_search_ge(struct bpnode * const node, const struct kv * const key)
{
debug_assert(node && (!node->leaf));
debug_assert(node->nr); // nr > 0
u32 l = 0;
u32 r = node->nr;
while ((l + 2) < r) {
const u32 m = (l + r) >> 1;
struct kv * const curr = node->key[m];
cpu_prefetch0(curr);
cpu_prefetch0(node->key+((l+m)>>1));
cpu_prefetch0(node->key+((m+1+r)>>1));
const int cmp = kv_compare(key, curr);
if (cmp < 0)
r = m; // m always > 0
else if (cmp > 0)
l = m + 1;
else
return m;
}
while (l < r) {
const u32 m = (l + r) >> 1;
struct kv * const curr = node->key[m];
const int cmp = kv_compare(key, curr);
if (cmp < 0)
r = m; // m always > 0
else if (cmp > 0)
l = m + 1;
else
return m;
}
return l;
}
// for lookup
static u32
bptree_meta_search_le(struct bpnode * const node, const struct kref * const key)
{
debug_assert(node->leaf == false);
debug_assert(node->nr);
u32 l = 0;
u32 r = node->nr;
while ((l + 3) < r) {
const u32 m = (l + r) >> 1;
struct kv * const curr = node->key[m];
cpu_prefetch0(curr);
cpu_prefetch0(node->key+((l+m)>>1));
cpu_prefetch0(node->key+((m+r)>>1));
const int cmp = kref_kv_compare(key, curr);
if (cmp < 0)
r = m; // m always > 0
else if (cmp > 0)
l = m;
else
return m;
}
while ((l + 1) < r) {
const u32 m = (l + r) >> 1;
struct kv * const curr = node->key[m];
const int cmp = kref_kv_compare(key, curr);
if (cmp < 0)
r = m; // m always > 0
else if (cmp > 0)
l = m;
else
return m;
}
return l;
}
static struct bpnode *
bptree_down_leaf(struct bptree * const tree, const struct kref * const key)
{
struct bpnode * node = tree->root;
while (node->leaf == false) {
const u32 subidx = bptree_meta_search_le(node, key);
node = node->sub[subidx];
}
return node;
}
// }}} helpers
// create {{{
struct bptree *
bptree_create(const struct kvmap_mm * const mm)
{
struct bptree * const tree = malloc(sizeof(*tree));
if (tree == NULL)
return NULL;
tree->mm = mm ? (*mm) : kvmap_mm_dup;
tree->slab = slab_create(sizeof(struct bpnode), 1lu << 21);
if (tree->slab == NULL) {
free(tree);
return NULL;
}
// the new root is leaf
struct bpnode * const root = bptree_alloc_node(tree, true);
if (root == NULL) {
slab_destroy(tree->slab);
free(tree);
return NULL;
}
tree->root = root;
tree->depth = 0;
return tree;
}
// }}} create
// read-only {{{
struct kv *
bptree_get(struct bptree * const tree, const struct kref * const key, struct kv * const out)
{
struct bpnode * const leaf = bptree_down_leaf(tree, key);
debug_assert(leaf->leaf);
const u32 idx = bptree_leaf_search_ge(leaf, key);
if (idx & BPTREE_CMP_MATCH) { // update
const u32 idxm = idx & ~BPTREE_CMP_MATCH;
struct kv * const kv = leaf->kvs[idxm];
struct kv * const ret = tree->mm.out(kv, out);
return ret;
}
return NULL;
}
bool
bptree_probe(struct bptree * const tree, const struct kref * const key)
{
struct bpnode * const leaf = bptree_down_leaf(tree, key);
debug_assert(leaf->leaf);
const u32 idx = bptree_leaf_search_ge(leaf, key);
return (idx & BPTREE_CMP_MATCH) != 0;
}
bool
bptree_inp(struct bptree * const tree, const struct kref * const key,
kv_inp_func uf, void * const priv)
{
struct bpnode * const leaf = bptree_down_leaf(tree, key);
debug_assert(leaf->leaf);
const u32 idx = bptree_leaf_search_ge(leaf, key);
if (idx & BPTREE_CMP_MATCH) { // update
const u32 idxm = idx & ~BPTREE_CMP_MATCH;
struct kv * const kv = leaf->kvs[idxm];
uf(kv, priv);
return true;
} else {
uf(NULL, priv);
return false;
}
}
// }}} read-only
// balance {{{
static bool
bptree_leaf_balance(struct bpnode * const left, struct bpnode * const right)
{
debug_assert(left->leaf && right->leaf);
const u32 nr_r = (left->nr + right->nr) >> 1;
const u32 nr_l = (left->nr + right->nr) - nr_r;
if (nr_l == left->nr) { // unchanged
return false;
} else if (nr_l < left->nr) { // shift right
const u32 nmove = nr_r - right->nr;
memmove(&(right->kvs[nmove]), &(right->kvs[0]), sizeof(right->kvs[0]) * right->nr);
memmove(&(right->kvs[0]), &(left->kvs[nr_l]), sizeof(right->kvs[0]) * nmove);
} else { // shift left
const u32 nmove = nr_l - left->nr;
memmove(&(left->kvs[left->nr]), &(right->kvs[0]), sizeof(right->kvs[0]) * nmove);
memmove(&(right->kvs[0]), &(right->kvs[nmove]), sizeof(right->kvs[0]) * nr_r);
}
left->nr = nr_l;
right->nr = nr_r;
return true;
}
static bool
bptree_meta_rebalance(struct bpnode * const left, struct bpnode * const right)
{
debug_assert((left->leaf == false) && (right->leaf == false));
const u32 nr_r = (left->nr + right->nr) >> 1;
const u32 nr_l = (left->nr + right->nr) - nr_r;
if (nr_l == left->nr) {
return false;
} else if (nr_l < left->nr) { // shift right
const u32 nmove = nr_r - right->nr;
memmove(&(right->key[nmove]), &(right->key[0]), sizeof(right->key[0]) * right->nr);
memmove(&(right->sub[nmove]), &(right->sub[0]), sizeof(right->sub[0]) * right->nr);
memmove(&(right->key[0]), &(left->key[nr_l]), sizeof(right->key[0]) * nmove);
memmove(&(right->sub[0]), &(left->sub[nr_l]), sizeof(right->sub[0]) * nmove);
} else { // shift left
const u32 nmove = nr_l - left->nr;
memmove(&(left->key[left->nr]), &(right->key[0]), sizeof(right->key[0]) * nmove);
memmove(&(left->sub[left->nr]), &(right->sub[0]), sizeof(right->sub[0]) * nmove);
memmove(&(right->key[0]), &(right->key[nmove]), sizeof(right->key[0]) * nr_r);
memmove(&(right->sub[0]), &(right->sub[nmove]), sizeof(right->sub[0]) * nr_r);
}
left->nr = nr_l;
right->nr = nr_r;
return true;
}
// }}} balance
// put {{{
static void
bptree_leaf_insert(struct bpnode * const leaf, struct kv * const kv, const u32 idx)
{
debug_assert(leaf->nr < BPTREE_LEAF_FO);
const u32 nmove = leaf->nr - idx;
memmove(&(leaf->kvs[idx+1]), &(leaf->kvs[idx]), sizeof(leaf->kvs[0]) * nmove);
leaf->kvs[idx] = kv;
leaf->nr++;
}
// return the new (right) sibliing of node if split
static struct bpnode *
bptree_leaf_put(struct bptree * const tree, struct bpnode * const leaf,
const struct kref * const kref, struct kv * const kv)
{
debug_assert(leaf->leaf);
const u32 idx = bptree_leaf_search_ge(leaf, kref);
if (idx & BPTREE_CMP_MATCH) { // update
const u32 idxm = idx & ~BPTREE_CMP_MATCH;
struct kv * const victim = (typeof(victim))(leaf->kvs[idxm]);
if (tree->mm.free)
tree->mm.free(victim, tree->mm.priv);
leaf->kvs[idxm] = kv;
return NULL; // no split
} else if (leaf->nr < BPTREE_LEAF_FO) { // insert
bptree_leaf_insert(leaf, kv, idx);
return NULL;
}
// leaf is full; split leaf
struct bpnode * const newleaf = bptree_alloc_node(tree, true);
bptree_leaf_balance(leaf, newleaf); // half-half
// link
newleaf->next = leaf->next;
newleaf->prev = leaf;
leaf->next = newleaf;
if (newleaf->next)
newleaf->next->prev = newleaf;
// insert
struct bpnode * const down = (kv_compare(kv, newleaf->kvs[0]) < 0) ? leaf : newleaf;
bptree_leaf_insert(down, kv, bptree_leaf_search_ge(down, kref));
// the caller will insert newleaf into the meta node
// the parent node may recursively split
return newleaf;
}
static void
bptree_meta_insert(struct bpnode * const parent, struct bpnode * const newchild)
{
debug_assert(parent->nr < BPTREE_META_FO);
const struct kv * const anchor0 = newchild->leaf ? newchild->kvs[0] : newchild->key[0];
struct kv * const anchor = kv_dup_key(anchor0);
const u32 idx = bptree_meta_search_ge(parent, anchor);
const u32 nmove = parent->nr - idx;
memmove(&(parent->key[idx+1]), &(parent->key[idx]), sizeof(parent->key[0]) * nmove);
parent->key[idx] = anchor;
memmove(&(parent->sub[idx+1]), &(parent->sub[idx]), sizeof(parent->sub[0]) * nmove);
parent->sub[idx] = newchild;