-
Notifications
You must be signed in to change notification settings - Fork 28
/
buffered_tree.c
772 lines (649 loc) · 19.8 KB
/
buffered_tree.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
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "buffered_tree.h"
struct container {
struct payload *payload_first;
uint32_t payload_size;
struct node *child;
};
struct node {
struct node *parent;
struct container **containers;
uint32_t container_count;
uint32_t container_size;
};
struct bftree {
struct node *root;
struct bftree_opts *opts;
uint32_t height;
int is_migrated;
uint32_t del_payload_count;
uint32_t put_payload_count;
};
struct bftree_iterator {
struct bftree *tree;
struct payload *next;
int closed;
};
static struct container *container_insert(struct bftree *tree, struct node *node,
uint32_t container_idx, struct payload *new_payload);
void bftree_node_print(struct node *node);
static void validate_containers(struct node *node, key_compare_func compare);
struct payload *payload_create(void *key, void *val, enum payload_type type)
{
struct payload *payload;
payload = malloc(sizeof(*payload));
payload->key = key;
payload->val = val;
payload->next = NULL;
payload->type = type;
return payload;
}
void payload_free(struct bftree *tree, struct payload *payload, int nofree)
{
if (payload->key && tree->opts->key_destructor)
tree->opts->key_destructor(payload->key);
if (payload->val && tree->opts->val_destructor && !nofree)
tree->opts->val_destructor(payload->val);
if (payload->type == Put)
tree->put_payload_count--;
else
tree->del_payload_count--;
free(payload);
}
void payload_replace(struct bftree *tree, struct payload *older, struct payload *newer)
{
void *temp;
temp = older->val;
older->val = newer->val;
newer->val = temp;
older->type = newer->type;
payload_free(tree, newer, 0);
}
struct container *container_create()
{
struct container *container;
container = malloc(sizeof(*container));
container->payload_first = NULL;
container->payload_size = 0;
container->child = NULL;
return container;
}
void container_free(struct bftree *tree, struct container *container)
{
struct payload *curr, *next;
curr = container->payload_first;
while (curr) {
next = curr->next;
payload_free(tree, curr, 0);
curr = next;
}
free(container);
}
void insert_after_container(struct node *node, struct container *container,
uint32_t container_idx)
{
if (node->container_size == node->container_count) {
node->containers = realloc(node->containers, sizeof(void*)*node->container_count*2);
node->container_count *= 2;
}
if (node->container_size == 0) {
node->containers[node->container_size++] = container;
} else {
memmove(&node->containers[container_idx+2], &node->containers[container_idx+1],
(node->container_size-container_idx-1)*sizeof(void*));
node->containers[container_idx+1] = container;
node->container_size++;
}
}
struct node *node_create(struct node *parent_node)
{
struct node *node;
node = malloc(sizeof(*node));
node->parent = parent_node;
node->containers = malloc(sizeof(struct container*)*BFTREE_DEFAULT_CONTAINER);
node->container_count = BFTREE_DEFAULT_CONTAINER;
node->container_size = 0;
return node;
}
void node_free(struct bftree *tree, struct node *node)
{
int i;
struct container *container;
for (i = 0; i < node->container_size; i++) {
container = node->containers[i];
container_free(tree, container);
}
free(node->containers);
free(node);
}
void bftree_free_node(struct bftree *tree, struct node *node)
{
int i;
struct container *container;
for (i = 0; i < node->container_size; i++) {
container = node->containers[i];
if (container->child)
bftree_free_node(tree, container->child);
}
node_free(tree, node);
}
static uint32_t find_container(key_compare_func compare, struct node *node,
void *key, uint32_t start_container)
{
int left, right, middle, result, compared;
struct container **containers;
left = start_container;
right = node->container_size - 1;
containers = node->containers;
compared = 0;
while (left <= right) {
middle = (left + right) / 2;
compared = compare(key, containers[middle]->payload_first->key);
if (compared < 0) {
right = middle - 1;
} else if (compared > 0) {
left = middle + 1;
} else {
right = middle;
break ;
}
}
if (compared > 0)
result = left - 1;
else if (compared < 0)
result = right;
else
result = right;
if (result == 0)
return 0;
return right - 1;
}
static struct container *remove_container(struct node *node, uint32_t idx)
{
struct container *removed;
removed = node->containers[idx];
memmove(&node->containers[idx], &node->containers[idx+1],
(node->container_size-idx-1)*sizeof(void*));
node->container_size--;
return removed;
}
static struct payload *get_payload(key_compare_func compare, struct payload *payload_start,
void *key, int *is_equal)
{
struct payload *curr_payload, *prev_payload;
int compared;
prev_payload = NULL;
curr_payload = payload_start;
*is_equal = 0;
while (curr_payload) {
compared = compare(key, curr_payload->key);
if (compared <= 0) {
if (compared == 0) {
*is_equal = 1;
return curr_payload;
}
return prev_payload;
}
prev_payload = curr_payload;
curr_payload = curr_payload->next;
}
return prev_payload;
}
static void push_to_child(struct bftree *tree, struct node *node,
struct container *container)
{
struct payload *curr_payload, *next_payload;
uint32_t child_container, push_count;
key_compare_func compare;
compare = tree->opts->key_compare;
curr_payload = container->payload_first->next;
child_container = 0;
push_count = container->payload_size / 2;
container->payload_size -= push_count;
while (push_count--) {
next_payload = curr_payload->next;
container->payload_first->next = next_payload;
child_container = find_container(compare, container->child,
curr_payload->key, child_container);
container_insert(tree, container->child, child_container, curr_payload);
curr_payload = next_payload;
}
}
static void order_container_payload(struct bftree *tree, struct node *node,
uint32_t migrated_idx, uint32_t import_idx)
{
struct payload *separator, *curr;
key_compare_func compare;
int is_equal;
struct container *left, *right;
left = node->containers[migrated_idx];
right = node->containers[import_idx];
compare = tree->opts->key_compare;
separator = get_payload(compare, left->payload_first,
right->payload_first->key, &is_equal);
if (is_equal) {
// TODO need optimize
struct payload *prev;
prev = left->payload_first;
while (prev->next != separator) {
prev = prev->next;
}
payload_replace(tree, right->payload_first, separator);
separator = prev;
left->payload_size--;
}
if (separator) {
curr = separator->next;
separator->next = NULL;
tree->is_migrated = 1;
while (curr) {
left->payload_size--;
container_insert(tree, node, import_idx, curr);
curr = curr->next;
}
tree->is_migrated = 0;
}
}
static void try_split_node(struct bftree *tree, struct node *node)
{
uint32_t middle_container_idx, parent_container_idx;
int i;
struct node *new_node, *new_root;
struct container *container, *new_node_first_container;
if (node->container_size < BFTREE_CONTAINER_THRESHOLD) {
// the number of container in this node is full
return ;
}
middle_container_idx = node->container_size / 2;
new_node = node_create(node->parent);
new_node_first_container = node->containers[middle_container_idx];
new_node_first_container->child = new_node;
for (i = middle_container_idx+1; i < node->container_size; ++i) {
container = node->containers[i];
insert_after_container(new_node, container, i-middle_container_idx-2);
}
node->container_size -= (i-middle_container_idx);
if (node == tree->root) {
// produce new root
new_root = node_create(NULL);
tree->root = new_root;
tree->height++;
node->parent = new_root;
new_node->parent = new_root;
container = remove_container(node, 0);
container->child = node;
insert_after_container(new_root, container, 0);
insert_after_container(new_root, new_node_first_container, 0);
} else {
parent_container_idx = find_container(tree->opts->key_compare,
node->parent, new_node_first_container->payload_first->key, 0);
insert_after_container(node->parent, new_node_first_container, parent_container_idx);
order_container_payload(tree, node->parent, parent_container_idx, parent_container_idx+1);
try_split_node(tree, node->parent);
}
}
static void split_container(struct bftree *tree, struct node *node,
uint32_t container_idx)
{
uint32_t half_count, i;
struct container *new_container, *target;
struct payload *payload;
new_container = container_create();
insert_after_container(node, new_container, container_idx);
target = node->containers[container_idx];
half_count = target->payload_size / 2;
payload = target->payload_first;
for (i = 0; i < half_count - 1; ++i)
payload = payload->next;
new_container->payload_first = payload->next;
payload->next = NULL;
new_container->payload_size = target->payload_size - half_count;
target->payload_size = half_count;
try_split_node(tree, node);
}
static struct container *container_insert(struct bftree *tree, struct node *node,
uint32_t container_idx, struct payload *new_payload)
{
struct payload *curr_payload;
struct container *target;
int is_equal;
key_compare_func compare;
compare = tree->opts->key_compare;
if (container_idx >= node->container_size) {
target = container_create();
insert_after_container(node, target, 0);
} else {
target = node->containers[container_idx];
}
curr_payload = get_payload(tree->opts->key_compare, target->payload_first,
new_payload->key, &is_equal);
if (is_equal) {
// exist same key, swap value of payload
payload_replace(tree, curr_payload, new_payload);
} else {
if (curr_payload) {
// not at the header of payload list
new_payload->next = curr_payload->next;
curr_payload->next = new_payload;
} else {
// at the header of payload list
new_payload->next = target->payload_first;
target->payload_first = new_payload;
}
target->payload_size++;
}
if (target->payload_size > BFTREE_PAYLOAD_THRESHOLD && tree->is_migrated) {
if (target->child)
push_to_child(tree, node, target);
else
split_container(tree, node, container_idx);
}
return target;
}
static struct payload *container_get(struct bftree *tree, struct node *node,
uint32_t container_idx, void *key)
{
struct payload *curr_payload;
struct container *container;
int (*compare)(const void *, const void *);
int is_equal;
if (container_idx >= node->container_size)
return NULL;
compare = tree->opts->key_compare;
container = node->containers[container_idx];
curr_payload = get_payload(compare, container->payload_first, key, &is_equal);
if (is_equal) {
if (curr_payload->type == Put)
return curr_payload;
return NULL;
}
if (container->child) {
container_idx = find_container(compare, container->child, key, 0);
return container_get(tree, container->child, container_idx, key);
}
return NULL;
}
// ================================================================
// ========================== Public API ==========================
// ================================================================
struct bftree *bftree_create(struct bftree_opts *opts)
{
struct node *root;
struct bftree *tree;
tree = malloc(sizeof(*tree));
root = node_create(NULL);
tree->root = root;
tree->height = 1;
tree->opts = opts;
tree->is_migrated = 0;
tree->del_payload_count = tree->put_payload_count = 0;
assert(opts->key_destructor && opts->val_destructor);
return tree;
}
void bftree_free(struct bftree *tree)
{
bftree_free_node(tree, tree->root);
free(tree);
}
int bftree_put(struct bftree *tree, void *key, void *val)
{
struct payload *new_payload;
uint32_t idx;
if (!tree || !key)
return BF_WRONG;
new_payload = payload_create(key, val, Put);
idx = find_container(tree->opts->key_compare, tree->root, new_payload->key, 0);
container_insert(tree, tree->root, idx, new_payload);
return BF_OK;
}
void *bftree_get(struct bftree *tree, void *key)
{
uint32_t idx;
struct payload *r;
if (!tree || !key)
return NULL;
idx = find_container(tree->opts->key_compare, tree->root, key, 0);
r = container_get(tree, tree->root, idx, key);
if (r) {
return r->val;
}
return NULL;
}
int bftree_del(struct bftree *tree, void *key)
{
uint32_t idx;
struct payload *new_payload;
if (!tree || !key)
return BF_WRONG;
new_payload = payload_create(key, NULL, Del);
idx = find_container(tree->opts->key_compare, tree->root, new_payload->key, 0);
container_insert(tree, tree->root, idx, new_payload);
return BF_OK;
}
struct bftree_iterator *bftree_get_iterator(struct bftree *tree)
{
struct bftree_iterator *iter;
iter = malloc(sizeof(*iter));
iter->tree = tree;
iter->next = NULL;
iter->closed = 0;
return iter;
}
struct payload *bftree_next(struct bftree_iterator *iter)
{
struct bftree *tree;
struct container *container;
struct node *node;
struct payload *curr, *next, *min;
uint32_t idx;
key_compare_func key_compare;
int is_equal;
if (iter->closed)
return NULL;
tree = iter->tree;
key_compare = tree->opts->key_compare;
if (!iter->next) {
if (tree->root->container_size == 0)
return NULL;
iter->next = tree->root->containers[0]->payload_first;
}
curr = iter->next;
min = NULL;
node = tree->root;
do {
idx = find_container(key_compare, node, curr->key, 0);
container = node->containers[idx];
next = get_payload(key_compare, container->payload_first,
curr->key, &is_equal);
if (!next)
next = container->payload_first;
else
next = next->next;
if (next) {
if (!min) {
min = next;
} else if (key_compare(next->key, min->key) < 0) {
min = next;
}
}
node = container->child;
} while(node);
iter->next = min;
if (!min)
iter->closed = 1;
return curr;
}
void bftree_free_iterator(struct bftree_iterator *iter)
{
free(iter);
}
int bftree_count(struct bftree *tree)
{
struct bftree_iterator *iter;
int count;
count = 0;
iter = bftree_get_iterator(tree);
while (bftree_next(iter) != NULL)
count++;
bftree_free_iterator(iter);
return count;
}
// ================================================================
// ========================== Debug Area ==========================
// ================================================================
static void validate_containers(struct node *node, key_compare_func compare)
{
int i;
struct payload *curr, *prev;
for (i = 0; i < node->container_size; i++) {
prev = node->containers[i]->payload_first;
curr = prev->next;
while (curr) {
assert(compare(prev->key, curr->key) < 0);
prev = curr;
curr = curr->next;
}
if (i == 0)
continue;
assert(compare(node->containers[i-1]->payload_first->key,
node->containers[i]->payload_first->key) < 0);
}
}
void bftree_node_print(struct node *node)
{
int i;
struct payload *payload;
for (i = 0; i < node->container_size; ++i) {
printf("container%d %d %s\t", i, node->containers[i]->payload_size,
node->containers[i]->payload_first->key);
payload = node->containers[i]->payload_first;
while (payload) {
printf("%s => %s ", payload->key, payload->val);
payload = payload->next;
}
printf("\n");
}
printf("\n");
}
// ================================================================
// ========================== Map Type Area =======================
// ================================================================
typedef char *wstr;
struct wstrhd {
size_t len;
char buf[];
};
static inline wstr wstr_newlen(const void *init, size_t init_len)
{
struct wstrhd *sh;
sh = malloc(sizeof(struct wstrhd)+init_len+1);
if (sh == NULL) {
return NULL;
}
if (init) {
memcpy(sh->buf, init, init_len);
sh->len = init_len;
} else {
sh->len = 0;
}
sh->buf[sh->len] = '\0';
return (wstr)(sh->buf);
}
static inline inline void wstr_free(wstr s)
{
if (s == NULL) {
return ;
}
free(s - sizeof(struct wstrhd));
}
static inline size_t wstrlen(const wstr s)
{
struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
return hd->len;
}
static inline int wstr_keycompare(const void *key1, const void *key2)
{
size_t l1,l2;
l1 = wstrlen((wstr)key1);
l2 = wstrlen((wstr)key2);
if (l1 != l2) return l1 < l2 ? -1 : 1;
return memcmp(key1, key2, l1);
}
static struct bftree_opts map_opt = {
NULL,
NULL,
wstr_keycompare,
(void (*)(void*))wstr_free,
free,
};
struct bftree *bftmap_create()
{
return bftree_create(&map_opt);
}
void bftmap_free(struct bftree *tree)
{
bftree_free(tree);
}
int bftmap_put(struct bftree *tree, char *key, size_t key_len, void *val)
{
if (!key || !key_len)
return BF_WRONG;
wstr s = wstr_newlen(key, key_len);
return bftree_put(tree, s, val);
}
void *bftmap_get(struct bftree *tree, char *key, size_t key_len)
{
if (!key || !key_len)
return NULL;
void *r;
wstr s = wstr_newlen(key, key_len);
r = bftree_get(tree, s);
wstr_free(s);
return r;
}
int bftmap_del(struct bftree *tree, char *key, size_t key_len)
{
if (!key || !key_len)
return BF_WRONG;
wstr s = wstr_newlen(key, key_len);
return bftree_del(tree, s);
}
// ================================================================
// ========================== Set Type Area =======================
// ================================================================
struct bftree *bftset_create()
{
return bftree_create(&map_opt);
}
void bftset_free(struct bftree *tree)
{
bftree_free(tree);
}
int bftset_put(struct bftree *tree, char *key, size_t key_len)
{
if (!key || !key_len)
return BF_WRONG;
wstr s = wstr_newlen(key, key_len);
return bftree_put(tree, s, NULL);
}
void *bftset_get(struct bftree *tree, char *key, size_t key_len)
{
if (!key || !key_len)
return NULL;
void *r;
wstr s = wstr_newlen(key, key_len);
r = bftree_get(tree, s);
wstr_free(s);
return r;
}
int bftset_del(struct bftree *tree, char *key, size_t key_len)
{
if (!key || !key_len)
return BF_WRONG;
wstr s = wstr_newlen(key, key_len);
return bftree_del(tree, s);
}