-
Notifications
You must be signed in to change notification settings - Fork 2
/
btree.c
720 lines (580 loc) · 17.4 KB
/
btree.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
#include "bolo.h"
/* reserve the first 8 octets for header data */
#define BTREE_HEADER_SIZE 8
#define BTREE_LEAF 0x80
/* where do the keys start in the mapped page? */
#define BTREE_KEYS_OFFSET (BTREE_HEADER_SIZE)
/* where do the values start in the mapped page? */
#define BTREE_VALS_OFFSET (BTREE_KEYS_OFFSET + BTREE_DEGREE * sizeof(bolo_msec_t))
#define koffset(i) (BTREE_KEYS_OFFSET + (i) * sizeof(bolo_msec_t))
#define voffset(i) (BTREE_VALS_OFFSET + (i) * sizeof(uint64_t))
#define keyat(t,i) (page_read64(&(t)->page, koffset(i)))
#define valueat(t,i) (page_read64(&(t)->page, voffset(i)))
#define setkeyat(t,i,k) (page_write64(&(t)->page, koffset(i), k))
#define setvalueat(t,i,v) (page_write64(&(t)->page, voffset(i), v))
#define setchildat(t,i,c) do {\
setvalueat((t),(i),(c)->id); \
(t)->kids[(i)] = (c); \
} while (0)
static void
_print(struct btree *t, int indent)
{
int i;
CHECK(t != NULL, "btree_print() given a NULL btree to print");
CHECK(indent >= 0, "btree_print() given a negative indent");
CHECK(indent < 7, "btree_print() recursed more than 7 levels deep; possible recursion loop");
fprintf(stderr, "%*s[btree %p @%lu page %p // %d keys %s]\n",
indent * 8, "", (void *)t, t->id, t->page.data, t->used, t->leaf ? "LEAF" : "interior");
for (i = 0; i < t->used; i++) {
if (t->leaf) {
fprintf(stderr, "%*s[%03d] % 10ld / %010lx (= %lu / %010lx)\n",
indent * 8 + 2, "", i, keyat(t,i), keyat(t,i), valueat(t,i), valueat(t,i));
} else {
fprintf(stderr, "%*s[%03d] % 10ld / %010lx (%p) -->\n",
indent * 8 + 2, "", i, keyat(t,i), keyat(t,i), (void *)t->kids[i]);
if (t->kids[i])
_print(t->kids[i], indent + 1);
}
}
if (t->leaf) {
fprintf(stderr, "%*s[%03d] ~ (= %lu / %010lx)\n",
indent * 8 + 2, "", i, valueat(t,t->used), valueat(t,t->used));
} else {
fprintf(stderr, "%*s[%03d] ~ (%p) -->\n",
indent * 8 + 2, "", i, (void *)t->kids[t->used]);
if (t->kids[t->used])
_print(t->kids[t->used], indent + 1);
}
}
void
btree_print(struct btree *bt)
{
_print(bt, 0);
}
static struct btree *
s_mapat1(int fd, off_t offset)
{
struct btree *t;
t = xmalloc(sizeof(*t));
t->id = (uint64_t)offset;
if (page_map(&t->page, fd, offset, BTREE_PAGE_SIZE) != 0)
goto fail;
t->leaf = page_read8 (&t->page, 5) & BTREE_LEAF;
t->used = page_read16(&t->page, 6);
return t;
fail:
free(t);
return NULL;
}
static struct btree *
s_mapat(int fd, off_t offset)
{
struct btree *t;
int i;
t = s_mapat1(fd, offset);
if (!t)
return NULL;
if (t->leaf)
return t;
for (i = 0; i <= t->used; i++) {
t->kids[i] = s_mapat(fd, valueat(t,i));
if (!t->kids[i]) {
/* clean up the ones that succeeded */
for (i = i - 1; i >= 0; i--)
free(t->kids[i]);
free(t);
return NULL;
}
}
return t;
}
static struct btree *
s_extend(int fd)
{
uint64_t id;
id = lseek(fd, 0, SEEK_END);
lseek(fd, BTREE_PAGE_SIZE - 1, SEEK_CUR);
if (write(fd, "\0", 1) != 1)
return NULL;
lseek(fd, -1 * BTREE_PAGE_SIZE, SEEK_END);
CHECK(BTREE_HEADER_SIZE == 8, "BTREE_HEADER_SIZE constant is under- or oversized");
if (write(fd, "BTREE\x80\x00\x00", BTREE_HEADER_SIZE) != BTREE_HEADER_SIZE)
return NULL;
return s_mapat(fd, id);
}
static int
s_flush(struct btree *t)
{
CHECK(t != NULL, "btree_write() given a NULL btree to flush");
CHECK(t->page.data != NULL, "btree_write() given a btree without a backing page");
page_write8 (&t->page, 5, t->leaf ? BTREE_LEAF : 0);
page_write16(&t->page, 6, t->used);
return page_sync(&t->page);
}
int
btree_write(struct btree *t)
{
int i, rc;
CHECK(t != NULL, "btree_write() given a NULL btree to write");
rc = 0;
if (s_flush(t) != 0)
rc = -1;
if (!t->leaf)
for (i = 0; i <= t->used; i++)
if (btree_write(t->kids[i]) != 0)
rc = -1;
return rc;
}
int
btree_close(struct btree *t)
{
int i, rc;
if (!t)
return 0;
rc = 0;
if (!t->leaf)
for (i = 0; i <= t->used; i++)
if (btree_close(t->kids[i]) != 0)
rc = -1;
if (page_unmap(&t->page) != 0)
rc = -1;
free(t);
return rc;
}
static int
s_find(struct btree *t, bolo_msec_t key)
{
CHECK(t != NULL, "s_find (by way of btree_find or btree_insert) given a NULL btree node");
int lo, mid, hi;
lo = -1;
hi = t->used;
while (lo + 1 < hi) {
mid = (lo + hi) / 2;
if (keyat(t,mid) == key) return mid;
if (keyat(t,mid) > key) hi = mid;
else lo = mid;
}
return hi;
}
static void
s_shift(struct btree *t, int n)
{
if (t->used - n <= 0)
return;
/* slide all keys above [n] one slot to the right */
memmove((uint8_t *)t->page.data + koffset(n + 1),
(uint8_t *)t->page.data + koffset(n),
sizeof(bolo_msec_t) * (t->used - n));
/* slide all values above [n] one slot to the right */
memmove((uint8_t *)t->page.data + voffset(n + 2),
(uint8_t *)t->page.data + voffset(n + 1),
sizeof(uint64_t) * (t->used - n));
memmove(&t->kids[n + 2],
&t->kids[n + 1],
sizeof(struct btree*) * (t->used - n));
}
static struct btree *
s_clone(struct btree *t)
{
struct btree *c;
c = s_extend(t->page.fd);
if (!c)
bail("btree extension failed");
c->leaf = t->leaf;
return c;
}
static void
s_divide(struct btree *l, struct btree *r, int mid)
{
CHECK(l != NULL, "btree_insert() divide given a NULL left node");
CHECK(r != NULL, "btree_insert() divide given a NULL right node");
CHECK(l != r, "btree_insert() divide given identical left and right nodes");
CHECK(l->page.data != NULL, "btree_insert() divide given a left node with no backing page");
CHECK(r->page.data != NULL, "btree_insert() divide given a right node with no backing page");
CHECK(l->page.data != r->page.data, "btree_insert() divide given left and right nodes that use the same backing page");
CHECK(mid != 0, "btree_insert() divide attempted to divide with midpoint of 0");
CHECK(l->used >= mid, "btree_insert() divide attempted to divide with out-of-range midpoint");
r->used = l->used - mid - 1;
l->used = mid;
/* divide the keys at midpoint */
memmove((uint8_t *)r->page.data + koffset(0),
(uint8_t *)l->page.data + koffset(mid + 1),
sizeof(bolo_msec_t) * r->used);
/* divide the values at midpoint */
memmove((uint8_t *)r->page.data + voffset(0),
(uint8_t *)l->page.data + voffset(mid + 1),
sizeof(uint64_t) * (r->used + 1));
memmove(r->kids,
&l->kids[mid + 1],
sizeof(struct btree *) * (r->used + 1));
/* note: we don't have to clean up l above mid, so we don't.
keep that in mind if you go examining memory in gdb */
#if PEDANTIC
memset((uint8_t *)l->page.data + koffset(l->used), 0,
sizeof(bolo_msec_t) * (BTREE_DEGREE - l->used));
memset((uint8_t *)l->page.data + voffset(l->used + 1), 0,
sizeof(uint64_t) * (BTREE_DEGREE - l->used + 1));
#endif
}
static struct btree *
s_insert(struct btree *t, bolo_msec_t key, uint64_t block_number, bolo_msec_t *median)
{
int i, mid;
struct btree *r;
CHECK(t != NULL, "btree_insert() given a NULL node to insert into");
CHECK(t->used <= BTREE_DEGREE, "btree_insert() given a node that was impossibly full");
/* invariant: Each node in the btree will always have enough
free space in it to insert at least one value
(either a literal, or a node pointer).
Splitting is done later in this function (right
before returning) as necessary. */
i = s_find(t, key);
if (t->leaf) { /* insert into this node */
if (i < t->used && keyat(t,i) == key) {
setvalueat(t,i,block_number);
return NULL;
}
s_shift(t, i);
t->used++;
setkeyat(t,i,key);
setvalueat(t,i,block_number);
} else { /* insert in child */
if (!t->kids[i])
t->kids[i] = s_extend(t->page.fd);
if (!t->kids[i])
return NULL; /* FIXME this is wrong */
r = s_insert(t->kids[i], key, block_number, median);
if (r) {
s_shift(t, i);
t->used++;
setkeyat(t,i,*median);
setchildat(t,i+1,r);
return NULL;
}
}
/* split the node now, if it is full, to save complexity */
if (t->used == BTREE_DEGREE) {
mid = t->used * BTREE_SPLIT_FACTOR;
*median = keyat(t,mid);
r = s_clone(t);
s_divide(t,r,mid);
return r;
}
return NULL;
}
int
btree_insert(struct btree *t, bolo_msec_t key, uint64_t block_number)
{
struct btree *l, *r;
bolo_msec_t m;
CHECK(t != NULL, "btree_insert() given a NULL node to insert into");
r = s_insert(t, key, block_number, &m);
if (r) {
/* pivot root to the left */
l = s_clone(t);
l->used = t->used;
l->leaf = t->leaf;
memmove(l->kids, t->kids, sizeof(t->kids));
memmove(l->page.data, t->page.data, BTREE_PAGE_SIZE);
/* re-initialize root as [ l . m . r ] */
t->used = 1;
t->leaf = 0;
setchildat(t, 0, l);
setkeyat (t, 0, m);
setchildat(t, 1, r);
}
return 0;
}
int
btree_find(struct btree *t, uint64_t *dst, bolo_msec_t key)
{
int i;
CHECK(t != NULL, "btree_find() given a NULL btree node");
CHECK(dst != NULL, "btree_find() told to place results in a NULL pointer");
if (t->leaf && t->used == 0)
return -1; /* empty root node */
i = s_find(t, key);
if (t->leaf) {
if (i == 0 || key == keyat(t,i))
*dst = valueat(t, i);
else
*dst = valueat(t, i-1);
return 0;
}
return t->kids[i] ? btree_find(t->kids[i], dst, key)
: -1;
}
int
btree_isempty(struct btree *t)
{
CHECK(t != NULL, "btree_isempty() given a NULL btree node");
return t->used == 0;
}
bolo_msec_t
btree_first(struct btree *t)
{
CHECK(t != NULL, "btree_first() given a NULL btree node");
CHECK(!btree_isempty(t), "btree_first() given an empty btree");
while (!t->leaf)
t = t->kids[0];
return keyat(t, 0);
}
bolo_msec_t
btree_last(struct btree *t)
{
CHECK(t != NULL, "btree_last() given a NULL btree node");
CHECK(!btree_isempty(t), "btree_last() given an empty btree");
while (!t->leaf)
t = t->kids[t->used];
return keyat(t, t->used - 1);
}
int
btallocator(struct btallocator *a, int rootfd)
{
int fd;
unsigned int i;
off_t off;
uint64_t id;
struct btblock *blk;
struct btree *t;
char path[64];
CHECK(a != NULL, "btallocator() given a NULL allocator object to initialize");
CHECK(rootfd >= 0, "btallocator() given an invalid root directory file descriptor");
a->rootfd = rootfd;
empty(&a->blocks);
for (id = 0; ; id += BTREE_PAGE_SIZE * BTBLOCK_DENSITY) {
snprintf(path, sizeof(path), "idx/%04lx.%04lx/%04lx.%04lx.%04lx.%04lx.idx",
((id & 0xffff000000000000ul) >> 48),
((id & 0x0000ffff00000000ul) >> 32),
/* --- */
((id & 0xffff000000000000ul) >> 48),
((id & 0x0000ffff00000000ul) >> 32),
((id & 0x00000000ffff0000ul) >> 16),
((id & 0x000000000000fffful)));
fd = openat(a->rootfd, path, O_RDWR);
if (fd < 0)
break;
off = lseek(fd, 0, SEEK_END);
if (off < 0)
goto fail;
errno = BOLO_EBADTREE;
if (off % BTREE_PAGE_SIZE != 0)
goto fail;
blk = xmalloc(sizeof(*blk));
blk->fd = fd;
blk->used = off / BTREE_PAGE_SIZE;
push(&a->blocks, &blk->l);
empty(&blk->btrees);
/* map all of the btrees */
for (i = 0; i < blk->used; i++) {
CHECK(BTREE_HEADER_SIZE == 8, "BTREE_HEADER_SIZE constant is under- or oversized");
if (write(blk->fd, "BTREE\x80\x00\x00", BTREE_HEADER_SIZE) != BTREE_HEADER_SIZE)
return -1;
/* map the btree region */
t = xmalloc(sizeof(*t));
memset(t, 0, sizeof(*t));
if (page_map(&t->page, blk->fd, i * BTREE_PAGE_SIZE, BTREE_PAGE_SIZE) != 0)
return -1;
/* initialize the btree node */
t->id = id + i;
t->leaf = page_read8 (&t->page, 5) & BTREE_LEAF;
t->used = page_read16(&t->page, 6);
}
}
return 0;
fail:
/* FIXME: deallocate memory */
return -1;
}
struct btree *
btmake(struct btallocator *a)
{
off_t off;
uint64_t id;
struct btblock *blk;
struct btree *t;
char path[64];
CHECK(a != NULL, "btmake() given a NULL btallocator");
id = 0;
for_each(blk, &a->blocks, l) {
if (blk->used < BTBLOCK_DENSITY)
goto alloc;
id += BTREE_PAGE_SIZE;
}
/* we have no empty blocks; allocate a new one, starting at `id` */
blk = xmalloc(sizeof(*blk));
push(&a->blocks, &blk->l);
snprintf(path, sizeof(path), "idx/%04lx.%04lx/%04lx.%04lx.%04lx.%04lx.idx",
((id & 0xffff000000000000ul) >> 48),
((id & 0x0000ffff00000000ul) >> 32),
/* --- */
((id & 0xffff000000000000ul) >> 48),
((id & 0x0000ffff00000000ul) >> 32),
((id & 0x00000000ffff0000ul) >> 16),
((id & 0x000000000000fffful)));
if (mktree(a->rootfd, path, 0777) != 0)
return NULL;
blk->fd = openat(a->rootfd, path, O_RDWR|O_CREAT, 0666);
if (blk->fd < 0)
return NULL;
alloc:
/* extend the underlying fd */
off = lseek(blk->fd, 0, SEEK_END);
lseek(blk->fd, BTREE_PAGE_SIZE - 1, SEEK_CUR);
if (write(blk->fd, "\0", 1) != 1)
return NULL;
lseek(blk->fd, -1 * BTREE_PAGE_SIZE, SEEK_END);
CHECK(BTREE_HEADER_SIZE == 8, "BTREE_HEADER_SIZE constant is under- or oversized");
if (write(blk->fd, "BTREE\x80\x00\x00", BTREE_HEADER_SIZE) != BTREE_HEADER_SIZE)
return NULL;
/* map the btree region */
t = xmalloc(sizeof(*t));
if (page_map(&t->page, blk->fd, off, BTREE_PAGE_SIZE) != 0)
goto fail;
/* initialize the btree node */
t->id = id;
t->leaf = page_read8 (&t->page, 5) & BTREE_LEAF;
t->used = page_read16(&t->page, 6);
/* increment the block reference counter */
blk->used++;
return t;
fail:
free(t);
return NULL;
}
struct btree *
btfind(struct btallocator *a, uint64_t id)
{
struct btblock *blk;
struct btree *t;
for_each(blk, &a->blocks, l) {
if (id > blk->used) {
id -= blk->used;
continue;
}
for_each(t, &blk->btrees, l) {
if (id > 0) {
id--;
continue;
}
return t;
}
}
return NULL;
}
#ifdef TEST
/* LCOV_EXCL_START */
/* Tests will be inserting arbitrary values,
so we will iterate over a range of keys.
To generate the values from the keys, we
will add an arbitrary, non-even constant
(PERTURB) to make things interesting */
#define PERTURB 0xbad
#define KEYSTART 0x0c00
#define KEYEND (KEYSTART + 7 * BTREE_DEGREE)
static int
iszero(const void *buf, off_t offset, size_t size)
{
size_t i;
insist(buf != NULL, "buf must not be NULL");
insist(offset >= 0, "offset must be positive or zero");
for (i = 0; i < size; i++)
if (((const uint8_t *)buf)[offset + i] != 0)
return 0; /* found a non-zero; fail */
return 1; /* checks out! */
}
TESTS {
#if 0
subtest {
int fd;
struct btree *t, *tmp;
bolo_msec_t key;
uint64_t value;
fd = memfd("btree");
t = btree_create(fd);
if (!t)
BAIL_OUT("btree_create(fd) returned NULL");
if (!btree_isempty(t))
BAIL_OUT("btree_create(fd) created a non-empty btree, somehow");
if (!t->leaf)
BAIL_OUT("initial root node is not considered a leaf");
if (memcmp(t->page.data, "BTREE\x80\x00\x00", 8) != 0)
BAIL_OUT("initial root node header incorrect");
if (!iszero(t->page.data, 8, BTREE_PAGE_SIZE - 8))
BAIL_OUT("initial root node (less header) should be zeroed (but wasn't!)");
is_unsigned(lseek(fd, 0, SEEK_END), BTREE_PAGE_SIZE,
"new btree file should be exactly ONE page long");
for (key = KEYSTART; key <= KEYEND; key++) {
if (btree_find(t, &value, key) == 0)
fail("btree find(%lx) before insertion should fail, but got %#lx\n", key, value);
}
pass("lookups should fail before insertion");
for (key = KEYSTART; key <= KEYEND; key++) {
if (btree_insert(t, key, key + PERTURB) != 0)
fail("failed to insert %#lx => %#lx", key, key + PERTURB);
}
pass("btree insertions should succeed");
for (key = KEYSTART; key <= KEYEND; key++) {
if (btree_find(t, &value, key) != 0)
fail("find(%lx) should succeed", key);
else if (value != key + PERTURB)
is_unsigned(value, key + PERTURB, "find(%lx)", key);
}
pass("lookups should succeed");
if (btree_write(t) != 0)
BAIL_OUT("btree_write failed");
#if 0
tmp = t;
t = btree_read(fd);
if (!t)
BAIL_OUT("btree_read(fd) failed!");
for (key = KEYSTART; key <= KEYEND; key++) {
if (btree_find(t, &value, key) != 0)
fail("find(%lx) failed after re-read", key);
else if (value != key + PERTURB)
is_unsigned(value, key + PERTURB, "find(%lx) after re-read", key);
}
pass("lookups after re-read should succeed");
#endif
if (btree_close(t) != 0)
BAIL_OUT("btree_close failed");
btree_close(tmp);
close(fd);
}
subtest {
int fd;
struct btree *t;
uint64_t value;
fd = memfd("btree");
t = btree_create(fd);
if (!t)
BAIL_OUT("btree_create(fd) returned NULL");
ok(btree_find(t, &value, 100) != 0, "find(100) should fail before insertion");
ok(btree_find(t, &value, 1000) != 0, "find(1000) should fail before insertion");
ok(btree_find(t, &value, 10000) != 0, "find(10000) should fail before insertion");
ok(btree_insert(t, 500, 501) == 0, "insert(500) should succeed");
ok(btree_insert(t, 1500, 1501) == 0, "insert(1500) should succeed");
ok(btree_find(t, &value, 100) == 0, "find(100) should succeed after insertion(s)");
is_unsigned(value, 501, "find(100) should find first key, 500 => 501");
ok(btree_find(t, &value, 1000) == 0, "find(1000) should succeed after insertion(s)");
is_unsigned(value, 501, "find(1000) should find nearest lesser key, 500 => 501");
ok(btree_find(t, &value, 10000) == 0, "find(10000) should succeed after insertion(s)");
is_unsigned(value, 1501, "find(10000) should find nearest lesser key, 1500 => 1501");
btree_close(t);
close(fd);
}
subtest {
int fd;
struct btree *t;
fd = memfd("btree");
t = btree_create(fd);
if (!t)
BAIL_OUT("btree_create(fd) returned NULL");
btree_print(t);
btree_close(t);
close(fd);
}
#endif
}
/* LCOV_EXCL_STOP */
#endif