-
Notifications
You must be signed in to change notification settings - Fork 24
/
kv.c
1469 lines (1292 loc) · 34.6 KB
/
kv.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
/*
* Copyright (c) 2016--2021 Wu, Xingbo <[email protected]>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
// headers {{{
#include <assert.h> // static_assert
#include <ctype.h>
#include "lib.h"
#include "ctypes.h"
#include "kv.h"
// }}} headers
// crc32c {{{
inline u32
kv_crc32c(const void * const ptr, u32 len)
{
return crc32c_inc((const u8 *)ptr, len, KV_CRC32C_SEED);
}
inline u64
kv_crc32c_extend(const u32 lo)
{
const u64 hi = (u64)(~lo);
return (hi << 32) | ((u64)lo);
}
// }}} crc32c
// kv {{{
// size {{{
inline size_t
kv_size(const struct kv * const kv)
{
return sizeof(*kv) + kv->klen + kv->vlen;
}
inline size_t
kv_size_align(const struct kv * const kv, const u64 align)
{
debug_assert(align && ((align & (align - 1)) == 0));
return (sizeof(*kv) + kv->klen + kv->vlen + (align - 1)) & (~(align - 1));
}
inline size_t
key_size(const struct kv *const key)
{
return sizeof(*key) + key->klen;
}
inline size_t
key_size_align(const struct kv *const key, const u64 align)
{
debug_assert(align && ((align & (align - 1)) == 0));
return (sizeof(*key) + key->klen + (align - 1)) & (~(align - 1));
}
// }}} size
// construct {{{
inline void
kv_update_hash(struct kv * const kv)
{
const u32 lo = kv_crc32c((const void *)kv->kv, kv->klen);
kv->hash = kv_crc32c_extend(lo);
}
inline void
kv_refill_value(struct kv * const kv, const void * const value, const u32 vlen)
{
debug_assert((vlen == 0) || value);
memcpy(&(kv->kv[kv->klen]), value, vlen);
kv->vlen = vlen;
}
inline void
kv_refill(struct kv * const kv, const void * const key, const u32 klen,
const void * const value, const u32 vlen)
{
debug_assert(kv);
kv->klen = klen;
memcpy(&(kv->kv[0]), key, klen);
kv_refill_value(kv, value, vlen);
kv_update_hash(kv);
}
inline void
kv_refill_str(struct kv * const kv, const char * const key,
const void * const value, const u32 vlen)
{
kv_refill(kv, key, (u32)strlen(key), value, vlen);
}
inline void
kv_refill_str_str(struct kv * const kv, const char * const key,
const char * const value)
{
kv_refill(kv, key, (u32)strlen(key), value, (u32)strlen(value));
}
// the u64 key is filled in big-endian byte order for correct ordering
inline void
kv_refill_u64(struct kv * const kv, const u64 key, const void * const value, const u32 vlen)
{
kv->klen = sizeof(u64);
*(u64 *)(kv->kv) = __builtin_bswap64(key); // bswap on little endian
kv_refill_value(kv, value, vlen);
kv_update_hash(kv);
}
inline void
kv_refill_hex32(struct kv * const kv, const u32 hex, const void * const value, const u32 vlen)
{
kv->klen = 8;
strhex_32(kv->kv, hex);
kv_refill_value(kv, value, vlen);
kv_update_hash(kv);
}
inline void
kv_refill_hex64(struct kv * const kv, const u64 hex, const void * const value, const u32 vlen)
{
kv->klen = 16;
strhex_64(kv->kv, hex);
kv_refill_value(kv, value, vlen);
kv_update_hash(kv);
}
inline void
kv_refill_hex64_klen(struct kv * const kv, const u64 hex,
const u32 klen, const void * const value, const u32 vlen)
{
strhex_64(kv->kv, hex);
if (klen > 16) {
kv->klen = klen;
memset(kv->kv + 16, '!', klen - 16);
} else {
kv->klen = 16;
}
kv_refill_value(kv, value, vlen);
kv_update_hash(kv);
}
inline void
kv_refill_kref(struct kv * const kv, const struct kref * const kref)
{
kv->klen = kref->len;
kv->vlen = 0;
kv->hash = kv_crc32c_extend(kref->hash32);
memmove(kv->kv, kref->ptr, kref->len);
}
inline void
kv_refill_kref_v(struct kv * const kv, const struct kref * const kref,
const void * const value, const u32 vlen)
{
kv->klen = kref->len;
kv->vlen = vlen;
kv->hash = kv_crc32c_extend(kref->hash32);
memmove(kv->kv, kref->ptr, kref->len);
memcpy(kv->kv + kv->klen, value, vlen);
}
inline struct kref
kv_kref(const struct kv * const key)
{
return (struct kref){.ptr = key->kv, .len = key->klen, .hash32 = key->hashlo};
}
inline struct kv *
kv_create(const void * const key, const u32 klen, const void * const value, const u32 vlen)
{
struct kv * const kv = malloc(sizeof(*kv) + klen + vlen);
if (kv)
kv_refill(kv, key, klen, value, vlen);
return kv;
}
inline struct kv *
kv_create_str(const char * const key, const void * const value, const u32 vlen)
{
return kv_create(key, (u32)strlen(key), value, vlen);
}
inline struct kv *
kv_create_str_str(const char * const key, const char * const value)
{
return kv_create(key, (u32)strlen(key), value, (u32)strlen(value));
}
inline struct kv *
kv_create_kref(const struct kref * const kref, const void * const value, const u32 vlen)
{
return kv_create(kref->ptr, kref->len, value, vlen);
}
static struct kv __kv_null = {};
__attribute__((constructor))
static void
kv_null_init(void)
{
kv_update_hash(&__kv_null);
}
inline const struct kv *
kv_null(void)
{
return &__kv_null;
}
// }}} construct
// dup {{{
inline struct kv *
kv_dup(const struct kv * const kv)
{
if (kv == NULL)
return NULL;
const size_t sz = kv_size(kv);
struct kv * const new = malloc(sz);
if (new)
memcpy(new, kv, sz);
return new;
}
inline struct kv *
kv_dup_key(const struct kv * const kv)
{
if (kv == NULL)
return NULL;
const size_t sz = key_size(kv);
struct kv * const new = malloc(sz);
if (new) {
memcpy(new, kv, sz);
new->vlen = 0;
}
return new;
}
inline struct kv *
kv_dup2(const struct kv * const from, struct kv * const to)
{
if (from == NULL)
return NULL;
const size_t sz = kv_size(from);
struct kv * const new = to ? to : malloc(sz);
if (new)
memcpy(new, from, sz);
return new;
}
inline struct kv *
kv_dup2_key(const struct kv * const from, struct kv * const to)
{
if (from == NULL)
return NULL;
const size_t sz = key_size(from);
struct kv * const new = to ? to : malloc(sz);
if (new) {
memcpy(new, from, sz);
new->vlen = 0;
}
return new;
}
inline struct kv *
kv_dup2_key_prefix(const struct kv * const from, struct kv * const to, const u32 plen)
{
if (from == NULL)
return NULL;
debug_assert(plen <= from->klen);
const size_t sz = key_size(from) - from->klen + plen;
struct kv * const new = to ? to : malloc(sz);
if (new) {
new->klen = plen;
memcpy(new->kv, from->kv, plen);
new->vlen = 0;
kv_update_hash(new);
}
return new;
}
// }}} dup
// compare {{{
static inline int
klen_compare(const u32 len1, const u32 len2)
{
if (len1 < len2)
return -1;
else if (len1 > len2)
return 1;
else
return 0;
}
// compare whether the two keys are identical
// optimistic: do not check hash
inline bool
kv_match(const struct kv * const key1, const struct kv * const key2)
{
//cpu_prefetch0(((u8 *)key2) + 64);
//return (key1->hash == key2->hash)
// && (key1->klen == key2->klen)
// && (!memcmp(key1->kv, key2->kv, key1->klen));
return (key1->klen == key2->klen) && (!memcmp(key1->kv, key2->kv, key1->klen));
}
// compare whether the two keys are identical
// check hash first
// pessimistic: return false quickly if their hashes mismatch
inline bool
kv_match_hash(const struct kv * const key1, const struct kv * const key2)
{
return (key1->hash == key2->hash)
&& (key1->klen == key2->klen)
&& (!memcmp(key1->kv, key2->kv, key1->klen));
}
inline bool
kv_match_full(const struct kv * const kv1, const struct kv * const kv2)
{
return (kv1->kvlen == kv2->kvlen)
&& (!memcmp(kv1, kv2, sizeof(*kv1) + kv1->klen + kv1->vlen));
}
bool
kv_match_kv128(const struct kv * const sk, const u8 * const kv128)
{
debug_assert(sk);
debug_assert(kv128);
u32 klen128 = 0;
u32 vlen128 = 0;
const u8 * const pdata = vi128_decode_u32(vi128_decode_u32(kv128, &klen128), &vlen128);
(void)vlen128;
return (sk->klen == klen128) && (!memcmp(sk->kv, pdata, klen128));
}
inline int
kv_compare(const struct kv * const kv1, const struct kv * const kv2)
{
const u32 len = kv1->klen < kv2->klen ? kv1->klen : kv2->klen;
const int cmp = memcmp(kv1->kv, kv2->kv, (size_t)len);
return cmp ? cmp : klen_compare(kv1->klen, kv2->klen);
}
// for qsort and bsearch
static int
kv_compare_ptrs(const void * const p1, const void * const p2)
{
const struct kv * const * const pp1 = (typeof(pp1))p1;
const struct kv * const * const pp2 = (typeof(pp2))p2;
return kv_compare(*pp1, *pp2);
}
int
kv_k128_compare(const struct kv * const sk, const u8 * const k128)
{
debug_assert(sk);
const u32 klen1 = sk->klen;
u32 klen2 = 0;
const u8 * const ptr2 = vi128_decode_u32(k128, &klen2);
debug_assert(ptr2);
const u32 len = (klen1 < klen2) ? klen1 : klen2;
const int cmp = memcmp(sk->kv, ptr2, len);
return cmp ? cmp : klen_compare(klen1, klen2);
}
int
kv_kv128_compare(const struct kv * const sk, const u8 * const kv128)
{
debug_assert(sk);
const u32 klen1 = sk->klen;
u32 klen2 = 0;
u32 vlen2 = 0;
const u8 * const ptr2 = vi128_decode_u32(vi128_decode_u32(kv128, &klen2), &vlen2);
const u32 len = (klen1 < klen2) ? klen1 : klen2;
const int cmp = memcmp(sk->kv, ptr2, len);
return cmp ? cmp : klen_compare(klen1, klen2);
}
inline void
kv_qsort(struct kv ** const kvs, const size_t nr)
{
qsort(kvs, nr, sizeof(kvs[0]), kv_compare_ptrs);
}
// return the length of longest common prefix of the two keys
inline u32
kv_key_lcp(const struct kv * const key1, const struct kv * const key2)
{
const u32 max = (key1->klen < key2->klen) ? key1->klen : key2->klen;
return memlcp(key1->kv, key2->kv, max);
}
// return the length of longest common prefix of the two keys with a known lcp0
inline u32
kv_key_lcp_skip(const struct kv * const key1, const struct kv * const key2, const u32 lcp0)
{
const u32 max = (key1->klen < key2->klen) ? key1->klen : key2->klen;
debug_assert(max >= lcp0);
return lcp0 + memlcp(key1->kv+lcp0, key2->kv+lcp0, max-lcp0);
}
// }}}
// psort {{{
static inline void
kv_psort_exchange(struct kv ** const kvs, const u64 i, const u64 j)
{
if (i != j) {
struct kv * const tmp = kvs[i];
kvs[i] = kvs[j];
kvs[j] = tmp;
}
}
static u64
kv_psort_partition(struct kv ** const kvs, const u64 lo, const u64 hi)
{
if (lo >= hi)
return lo;
const u64 p = (lo+hi) >> 1;
kv_psort_exchange(kvs, lo, p);
u64 i = lo;
u64 j = hi + 1;
do {
while (kv_compare(kvs[++i], kvs[lo]) < 0 && i < hi);
while (kv_compare(kvs[--j], kvs[lo]) > 0);
if (i >= j)
break;
kv_psort_exchange(kvs, i, j);
} while (true);
kv_psort_exchange(kvs, lo, j);
return j;
}
static void
kv_psort_rec(struct kv ** const kvs, const u64 lo, const u64 hi, const u64 tlo, const u64 thi)
{
if (lo >= hi)
return;
const u64 c = kv_psort_partition(kvs, lo, hi);
if (c > tlo) // go left
kv_psort_rec(kvs, lo, c-1, tlo, thi);
if (c < thi) // go right
kv_psort_rec(kvs, c+1, hi, tlo, thi);
}
inline void
kv_psort(struct kv ** const kvs, const u64 nr, const u64 tlo, const u64 thi)
{
debug_assert(tlo <= thi);
debug_assert(thi < nr);
kv_psort_rec(kvs, 0, nr-1, tlo, thi);
}
// }}} psort
// ptr {{{
inline void *
kv_vptr(struct kv * const kv)
{
return (void *)(&(kv->kv[kv->klen]));
}
inline void *
kv_kptr(struct kv * const kv)
{
return (void *)(&(kv->kv[0]));
}
inline const void *
kv_vptr_c(const struct kv * const kv)
{
return (const void *)(&(kv->kv[kv->klen]));
}
inline const void *
kv_kptr_c(const struct kv * const kv)
{
return (const void *)(&(kv->kv[0]));
}
// }}} ptr
// print {{{
// cmd "KV" K and V can be 's': string, 'x': hex, 'd': dec, or else for not printing.
// n for newline after kv
void
kv_print(const struct kv * const kv, const char * const cmd, FILE * const out)
{
debug_assert(cmd);
const u32 klen = kv->klen;
fprintf(out, "#%016lx k[%3u]", kv->hash, klen);
switch(cmd[0]) {
case 's': fprintf(out, " %.*s", klen, kv->kv); break;
case 'x': str_print_hex(out, kv->kv, klen); break;
case 'd': str_print_dec(out, kv->kv, klen); break;
default: break;
}
const u32 vlen = kv->vlen;
switch (cmd[1]) {
case 's': fprintf(out, " v[%4u] %.*s", vlen, vlen, kv->kv+klen); break;
case 'x': fprintf(out, " v[%4u]", vlen); str_print_hex(out, kv->kv+klen, vlen); break;
case 'd': fprintf(out, " v[%4u]", vlen); str_print_dec(out, kv->kv+klen, vlen); break;
default: break;
}
if (strchr(cmd, 'n'))
fprintf(out, "\n");
}
// }}} print
// mm {{{
struct kv *
kvmap_mm_in_noop(struct kv * const kv, void * const priv)
{
(void)priv;
return kv;
}
// copy-out
struct kv *
kvmap_mm_out_noop(struct kv * const kv, struct kv * const out)
{
(void)out;
return kv;
}
void
kvmap_mm_free_noop(struct kv * const kv, void * const priv)
{
(void)kv;
(void)priv;
}
// copy-in
struct kv *
kvmap_mm_in_dup(struct kv * const kv, void * const priv)
{
(void)priv;
return kv_dup(kv);
}
// copy-out
struct kv *
kvmap_mm_out_dup(struct kv * const kv, struct kv * const out)
{
return kv_dup2(kv, out);
}
void
kvmap_mm_free_free(struct kv * const kv, void * const priv)
{
(void)priv;
free(kv);
}
const struct kvmap_mm kvmap_mm_dup = {
.in = kvmap_mm_in_dup,
.out = kvmap_mm_out_dup,
.free = kvmap_mm_free_free,
.priv = NULL,
};
const struct kvmap_mm kvmap_mm_ndf = {
.in = kvmap_mm_in_noop,
.out = kvmap_mm_out_dup,
.free = kvmap_mm_free_free,
.priv = NULL,
};
// }}} mm
// kref {{{
inline void
kref_ref_raw(struct kref * const kref, const u8 * const ptr, const u32 len)
{
kref->ptr = ptr;
kref->len = len;
kref->hash32 = 0;
}
inline void
kref_ref_hash32(struct kref * const kref, const u8 * const ptr, const u32 len)
{
kref->ptr = ptr;
kref->len = len;
kref->hash32 = kv_crc32c(ptr, len);
}
inline void
kref_update_hash32(struct kref * const kref)
{
kref->hash32 = kv_crc32c(kref->ptr, kref->len);
}
inline void
kref_ref_kv(struct kref * const kref, const struct kv * const kv)
{
kref->ptr = kv->kv;
kref->len = kv->klen;
kref->hash32 = kv->hashlo;
}
inline void
kref_ref_kv_hash32(struct kref * const kref, const struct kv * const kv)
{
kref->ptr = kv->kv;
kref->len = kv->klen;
kref->hash32 = kv_crc32c(kv->kv, kv->klen);
}
inline bool
kref_match(const struct kref * const k1, const struct kref * const k2)
{
return (k1->len == k2->len) && (!memcmp(k1->ptr, k2->ptr, k1->len));
}
// match a kref and a key
inline bool
kref_kv_match(const struct kref * const kref, const struct kv * const k)
{
return (kref->len == k->klen) && (!memcmp(kref->ptr, k->kv, kref->len));
}
inline int
kref_compare(const struct kref * const kref1, const struct kref * const kref2)
{
const u32 len = kref1->len < kref2->len ? kref1->len : kref2->len;
const int cmp = memcmp(kref1->ptr, kref2->ptr, (size_t)len);
return cmp ? cmp : klen_compare(kref1->len, kref2->len);
}
// compare a kref and a key
inline int
kref_kv_compare(const struct kref * const kref, const struct kv * const k)
{
debug_assert(kref);
debug_assert(k);
const u32 len = kref->len < k->klen ? kref->len : k->klen;
const int cmp = memcmp(kref->ptr, k->kv, (size_t)len);
return cmp ? cmp : klen_compare(kref->len, k->klen);
}
inline u32
kref_lcp(const struct kref * const k1, const struct kref * const k2)
{
const u32 max = (k1->len < k2->len) ? k1->len : k2->len;
return memlcp(k1->ptr, k2->ptr, max);
}
inline u32
kref_kv_lcp(const struct kref * const kref, const struct kv * const kv)
{
const u32 max = (kref->len < kv->klen) ? kref->len : kv->klen;
return memlcp(kref->ptr, kv->kv, max);
}
// klen, key, ...
inline int
kref_k128_compare(const struct kref * const sk, const u8 * const k128)
{
debug_assert(sk);
const u32 klen1 = sk->len;
u32 klen2 = 0;
const u8 * const ptr2 = vi128_decode_u32(k128, &klen2);
debug_assert(ptr2);
const u32 len = (klen1 < klen2) ? klen1 : klen2;
const int cmp = memcmp(sk->ptr, ptr2, len);
return cmp ? cmp : klen_compare(klen1, klen2);
}
// klen, vlen, key, ...
inline int
kref_kv128_compare(const struct kref * const sk, const u8 * const kv128)
{
debug_assert(sk);
const u32 klen1 = sk->len;
u32 klen2 = 0;
u32 vlen2 = 0;
const u8 * const ptr2 = vi128_decode_u32(vi128_decode_u32(kv128, &klen2), &vlen2);
const u32 len = (klen1 < klen2) ? klen1 : klen2;
const int cmp = memcmp(sk->ptr, ptr2, len);
return cmp ? cmp : klen_compare(klen1, klen2);
}
static struct kref __kref_null = {.hash32 = KV_CRC32C_SEED};
inline const struct kref *
kref_null(void)
{
return &__kref_null;
}
// }}} kref
// kvref {{{
inline void
kvref_ref_kv(struct kvref * const ref, struct kv * const kv)
{
ref->kptr = kv->kv;
ref->vptr = kv->kv + kv->klen;
ref->hdr = *kv;
}
struct kv *
kvref_dup2_kv(struct kvref * const ref, struct kv * const to)
{
if (ref == NULL)
return NULL;
const size_t sz = sizeof(*to) + ref->hdr.klen + ref->hdr.vlen;
struct kv * const new = to ? to : malloc(sz);
if (new == NULL)
return NULL;
*new = ref->hdr;
memcpy(new->kv, ref->kptr, new->klen);
memcpy(new->kv + new->klen, ref->vptr, new->vlen);
return new;
}
struct kv *
kvref_dup2_key(struct kvref * const ref, struct kv * const to)
{
if (ref == NULL)
return NULL;
const size_t sz = sizeof(*to) + ref->hdr.klen;
struct kv * const new = to ? to : malloc(sz);
if (new == NULL)
return NULL;
*new = ref->hdr;
memcpy(new->kv, ref->kptr, new->klen);
return new;
}
int
kvref_kv_compare(const struct kvref * const ref, const struct kv * const kv)
{
const u32 len = ref->hdr.klen < kv->klen ? ref->hdr.klen : kv->klen;
const int cmp = memcmp(ref->kptr, kv->kv, (size_t)len);
return cmp ? cmp : klen_compare(ref->hdr.klen, kv->klen);
}
// }}} kvref
// kv128 {{{
// estimate the encoded size
inline size_t
kv128_estimate_kv(const struct kv * const kv)
{
return vi128_estimate_u32(kv->klen) + vi128_estimate_u32(kv->vlen) + kv->klen + kv->vlen;
}
// create a kv128 from kv
u8 *
kv128_encode_kv(const struct kv * const kv, u8 * const out, size_t * const pesize)
{
u8 * const ptr = out ? out : malloc(kv128_estimate_kv(kv));
if (!ptr)
return NULL;
u8 * const pdata = vi128_encode_u32(vi128_encode_u32(ptr, kv->klen), kv->vlen);
memcpy(pdata, kv->kv, kv->klen + kv->vlen);
if (pesize)
*pesize = (size_t)(pdata - ptr) + kv->klen + kv->vlen;
return ptr; // return the head of the encoded kv128
}
// dup kv128 to a kv
struct kv *
kv128_decode_kv(const u8 * const ptr, struct kv * const out, size_t * const pesize)
{
u32 klen, vlen;
const u8 * const pdata = vi128_decode_u32(vi128_decode_u32(ptr, &klen), &vlen);
struct kv * const ret = out ? out : malloc(sizeof(struct kv) + klen + vlen);
if (ret)
kv_refill(ret, pdata, klen, pdata + klen, vlen);
if (pesize)
*pesize = (size_t)(pdata - ptr) + klen + vlen;
return ret; // return the kv
}
inline size_t
kv128_size(const u8 * const ptr)
{
u32 klen, vlen;
const u8 * const pdata = vi128_decode_u32(vi128_decode_u32(ptr, &klen), &vlen);
return ((size_t)(pdata - ptr)) + klen + vlen;
}
// }}} kv128
// }}} kv
// kvmap {{{
// registry {{{
// increase MAX if need more
#define KVMAP_API_MAX ((32))
static struct kvmap_api_reg kvmap_api_regs[KVMAP_API_MAX];
static u64 kvmap_api_regs_nr = 0;
void
kvmap_api_register(const int nargs, const char * const name, const char * const args_msg,
void * (*create)(const char *, const struct kvmap_mm *, char **), const struct kvmap_api * const api)
{
if (kvmap_api_regs_nr < KVMAP_API_MAX) {
kvmap_api_regs[kvmap_api_regs_nr].nargs = nargs;
kvmap_api_regs[kvmap_api_regs_nr].name = name;
kvmap_api_regs[kvmap_api_regs_nr].args_msg = args_msg;
kvmap_api_regs[kvmap_api_regs_nr].create = create;
kvmap_api_regs[kvmap_api_regs_nr].api = api;
kvmap_api_regs_nr++;
} else {
fprintf(stderr, "%s failed to register [%s]\n", __func__, name);
}
}
void
kvmap_api_helper_message(void)
{
fprintf(stderr, "%s Usage: api <map-type> <param1> ...\n", __func__);
for (u64 i = 0; i < kvmap_api_regs_nr; i++) {
fprintf(stderr, "%s example: api %s %s\n", __func__,
kvmap_api_regs[i].name, kvmap_api_regs[i].args_msg);
}
}
int
kvmap_api_helper(int argc, char ** const argv, const struct kvmap_mm * const mm,
const struct kvmap_api ** const api_out, void ** const map_out)
{
// "api" "name" "arg1", ...
if (argc < 2 || strcmp(argv[0], "api") != 0)
return -1;
for (u64 i = 0; i < kvmap_api_regs_nr; i++) {
const struct kvmap_api_reg * const reg = &kvmap_api_regs[i];
if (0 != strcmp(argv[1], reg->name))
continue;
if ((argc - 2) < reg->nargs)
return -1;
void * const map = reg->create(argv[1], mm, argv + 2); // skip "api" "name"
if (map) {
*api_out = reg->api;
*map_out = map;
return 2 + reg->nargs;
} else {
return -1;
}
}
// no match
return -1;
}
// }}} registry
// misc {{{
void
kvmap_inp_steal_kv(struct kv * const kv, void * const priv)
{
// steal the kv pointer out so we don't need a dangerous get_key_interanl()
if (priv)
*(struct kv **)priv = kv;
}
inline void *
kvmap_ref(const struct kvmap_api * const api, void * const map)
{
return api->ref ? api->ref(map) : map;
}
// return the original map pointer; usually unused by caller
inline void *
kvmap_unref(const struct kvmap_api * const api, void * const ref)
{
return api->unref ? api->unref(ref) : ref;
}
// }}} misc
// kvmap_kv_op {{{
inline struct kv *
kvmap_kv_get(const struct kvmap_api * const api, void * const ref,
const struct kv * const key, struct kv * const out)
{
const struct kref kref = kv_kref(key);
return api->get(ref, &kref, out);
}
inline bool
kvmap_kv_probe(const struct kvmap_api * const api, void * const ref,
const struct kv * const key)
{
const struct kref kref = kv_kref(key);
return api->probe(ref, &kref);
}
inline bool
kvmap_kv_put(const struct kvmap_api * const api, void * const ref,
struct kv * const kv)
{
return api->put(ref, kv);
}
inline bool
kvmap_kv_del(const struct kvmap_api * const api, void * const ref,
const struct kv * const key)
{
const struct kref kref = kv_kref(key);
return api->del(ref, &kref);
}
inline bool
kvmap_kv_inpr(const struct kvmap_api * const api, void * const ref,
const struct kv * const key, kv_inp_func uf, void * const priv)
{
const struct kref kref = kv_kref(key);
return api->inpr(ref, &kref, uf, priv);
}
inline bool
kvmap_kv_inpw(const struct kvmap_api * const api, void * const ref,
const struct kv * const key, kv_inp_func uf, void * const priv)
{
const struct kref kref = kv_kref(key);
return api->inpw(ref, &kref, uf, priv);
}
inline bool
kvmap_kv_merge(const struct kvmap_api * const api, void * const ref,
const struct kv * const key, kv_merge_func uf, void * const priv)
{
const struct kref kref = kv_kref(key);
return api->merge(ref, &kref, uf, priv);
}
inline u64
kvmap_kv_delr(const struct kvmap_api * const api, void * const ref,
const struct kv * const start, const struct kv * const end)
{
const struct kref kref0 = kv_kref(start);
if (end) {
const struct kref krefz = kv_kref(end);
return api->delr(ref, &kref0, &krefz);
} else {
return api->delr(ref, &kref0, NULL);
}
}
inline void
kvmap_kv_iter_seek(const struct kvmap_api * const api, void * const iter,
const struct kv * const key)
{
const struct kref kref = kv_kref(key);
api->iter_seek(iter, &kref);
}
// }}} kvmap_kv_op
// kvmap_raw_op {{{
inline struct kv *
kvmap_raw_get(const struct kvmap_api * const api, void * const ref,
const u32 len, const u8 * const ptr, struct kv * const out)
{
const struct kref kref = {.ptr = ptr, .len = len,
.hash32 = api->hashkey ? kv_crc32c(ptr, len) : 0};
return api->get(ref, &kref, out);
}
inline bool
kvmap_raw_probe(const struct kvmap_api * const api, void * const ref,
const u32 len, const u8 * const ptr)
{
const struct kref kref = {.ptr = ptr, .len = len,
.hash32 = api->hashkey ? kv_crc32c(ptr, len) : 0};
return api->probe(ref, &kref);
}
inline bool
kvmap_raw_del(const struct kvmap_api * const api, void * const ref,
const u32 len, const u8 * const ptr)
{
const struct kref kref = {.ptr = ptr, .len = len,
.hash32 = api->hashkey ? kv_crc32c(ptr, len) : 0};
return api->del(ref, &kref);
}
inline bool
kvmap_raw_inpr(const struct kvmap_api * const api, void * const ref,
const u32 len, const u8 * const ptr, kv_inp_func uf, void * const priv)
{
const struct kref kref = {.ptr = ptr, .len = len,
.hash32 = api->hashkey ? kv_crc32c(ptr, len) : 0};
return api->inpr(ref, &kref, uf, priv);