/
btree_modify.c
568 lines (502 loc) · 18.4 KB
/
btree_modify.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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "couch_btree.h"
#include "util.h"
#include "arena.h"
#include "bitfield.h"
#define CHUNK_THRESHOLD 1279
#define ARENA_CHUNK_SIZE 32768 // Size of malloc blocks the arena creates. Also largest alloc size.
static couchstore_error_t flush_mr(couchfile_modify_result *res);
static couchstore_error_t maybe_flush(couchfile_modify_result *mr)
{
if (mr->modified && mr->node_len > CHUNK_THRESHOLD) {
return flush_mr(mr);
}
return COUCHSTORE_SUCCESS;
}
static nodelist *make_nodelist(arena* a, size_t bufsize)
{
nodelist *r = arena_alloc(a, sizeof(nodelist) + bufsize);
if (!r) {
return NULL;
}
memset(r, 0, sizeof(nodelist));
r->data.size = bufsize;
r->data.buf = ((char *) r) + (sizeof(nodelist));
return r;
}
static couchfile_modify_result *make_modres(arena* a, couchfile_modify_request *rq)
{
couchfile_modify_result *res = arena_alloc(a, sizeof(couchfile_modify_result));
if (!res) {
return NULL;
}
res->arena = a;
res->values = make_nodelist(a, 0);
if (!res->values) {
return NULL;
}
res->values_end = res->values;
res->pointers = make_nodelist(a, 0);
if (!res->pointers) {
return NULL;
}
res->pointers_end = res->pointers;
res->node_len = 0;
res->count = 0;
res->modified = 0;
res->error_state = 0;
res->rq = rq;
return res;
}
static couchstore_error_t mr_push_item(sized_buf *k, sized_buf *v, couchfile_modify_result *dst)
{
nodelist *itm = make_nodelist(dst->arena, 0);
if (!itm) {
return COUCHSTORE_ERROR_ALLOC_FAIL;
}
itm->key = *k;
itm->data = *v;
itm->pointer = NULL;
dst->values_end->next = itm;
dst->values_end = itm;
//Encoded size (see flush_mr)
dst->node_len += k->size + v->size + 5;
dst->count++;
return maybe_flush(dst);
}
static nodelist *encode_pointer(arena* a, node_pointer *ptr)
{
nodelist *pel = make_nodelist(a, 14 + ptr->reduce_value.size);
if (!pel) {
return NULL;
}
memset(pel->data.buf, 0, 14);
set_bits(pel->data.buf + 0, 0, 48, ptr->pointer);
set_bits(pel->data.buf + 6, 0, 48, ptr->subtreesize);
set_bits(pel->data.buf + 12, 0, 16, ptr->reduce_value.size);
memcpy(pel->data.buf + 14, ptr->reduce_value.buf, ptr->reduce_value.size);
pel->pointer = ptr;
pel->key = ptr->key;
return pel;
}
static couchstore_error_t mr_push_pointerinfo(node_pointer *ptr,
couchfile_modify_result *dst)
{
nodelist *pel = encode_pointer(dst->arena, ptr);
if (!pel) {
return COUCHSTORE_ERROR_ALLOC_FAIL;
}
dst->values_end->next = pel;
dst->values_end = pel;
dst->node_len += pel->key.size + pel->data.size + 5;
dst->count++;
return maybe_flush(dst);
}
static node_pointer *read_pointer(arena* a, sized_buf *key, char *buf)
{
//Parse KP pair into a node_pointer {K, {ptr, reduce_value, subtreesize}}
node_pointer *p = (node_pointer *) arena_alloc(a, sizeof(node_pointer));
if (!p) {
return NULL;
}
p->pointer = get_48(buf);
p->subtreesize = get_48(buf + 6);
p->reduce_value.size = get_16(buf + 12);
p->reduce_value.buf = buf + 14;
p->key = *key;
return p;
}
//Write the current contents of the values list to disk as a node
//and add the resulting pointer to the pointers list.
static couchstore_error_t flush_mr(couchfile_modify_result *res)
{
size_t bufpos = 0;
int errcode = COUCHSTORE_SUCCESS;
char *nodebuf = NULL;
sized_buf writebuf;
char reducebuf[30];
size_t reducesize = 0;
uint64_t subtreesize = 0;
off_t diskpos;
size_t disk_size;
sized_buf final_key = {NULL, 0};
if (res->values_end == res->values || ! res->modified) {
//Empty
return COUCHSTORE_SUCCESS;
}
// nodebuf/writebuf is very short-lived and can be large, so use regular malloc heap for it:
nodebuf = malloc(res->node_len + 1);
if (!nodebuf) {
return COUCHSTORE_ERROR_ALLOC_FAIL;
}
writebuf.buf = nodebuf;
writebuf.size = res->node_len + 1;
nodebuf[0] = (char) res->node_type;
bufpos = 1;
nodelist *i = res->values->next;
while (i != NULL) {
memset(nodebuf + bufpos, 0, 5);
set_bits(nodebuf + bufpos , 0, 12, i->key.size);
set_bits(nodebuf + bufpos + 1, 4, 28, i->data.size);
memcpy(nodebuf + bufpos + 5, i->key.buf, i->key.size);
memcpy(nodebuf + bufpos + 5 + i->key.size, i->data.buf, i->data.size);
bufpos = bufpos + 5 + i->data.size + i->key.size;
if (i->pointer) {
subtreesize += i->pointer->subtreesize;
}
if (i->next == NULL) {
final_key = i->key;
}
i = i->next;
}
errcode = db_write_buf_compressed(res->rq->db, &writebuf, &diskpos, &disk_size);
free(nodebuf); // here endeth the nodebuf.
if (errcode != COUCHSTORE_SUCCESS) {
return errcode;
}
if (res->node_type == KV_NODE && res->rq->reduce) {
res->rq->reduce(reducebuf, &reducesize, res->values->next, res->count);
}
if (res->node_type == KP_NODE && res->rq->rereduce) {
res->rq->rereduce(reducebuf, &reducesize, res->values->next, res->count);
}
node_pointer *ptr = (node_pointer *) arena_alloc(res->arena, sizeof(node_pointer) + final_key.size + reducesize);
if (!ptr) {
return COUCHSTORE_ERROR_ALLOC_FAIL;
}
ptr->key.buf = ((char *)ptr) + sizeof(node_pointer);
ptr->reduce_value.buf = ((char *)ptr) + sizeof(node_pointer) + final_key.size;
ptr->key.size = final_key.size;
ptr->reduce_value.size = reducesize;
memcpy(ptr->key.buf, final_key.buf, final_key.size);
memcpy(ptr->reduce_value.buf, reducebuf, reducesize);
ptr->subtreesize = subtreesize + disk_size;
ptr->pointer = diskpos;
nodelist *pel = encode_pointer(res->arena, ptr);
if (!pel) {
return COUCHSTORE_ERROR_ALLOC_FAIL;
}
res->pointers_end->next = pel;
res->pointers_end = pel;
res->node_len = 0;
res->count = 0;
res->values_end = res->values;
res->values->next = NULL;
return COUCHSTORE_SUCCESS;
}
//Move this node's pointers list to dst node's values list.
static couchstore_error_t mr_move_pointers(couchfile_modify_result *src,
couchfile_modify_result *dst)
{
couchstore_error_t errcode = COUCHSTORE_SUCCESS;
if (src->pointers_end == src->pointers) {
return 0;
}
nodelist *ptr = src->pointers->next;
nodelist *next = ptr;
while (ptr != NULL && errcode == 0) {
dst->node_len += ptr->data.size + ptr->key.size + 5;
dst->count++;
next = ptr->next;
ptr->next = NULL;
dst->values_end->next = ptr;
dst->values_end = ptr;
ptr = next;
error_pass(maybe_flush(dst));
}
cleanup:
src->pointers->next = next;
src->pointers_end = src->pointers;
return errcode;
}
static couchstore_error_t modify_node(couchfile_modify_request *rq,
node_pointer *nptr,
int start, int end,
couchfile_modify_result *dst)
{
char *nodebuf = NULL; // FYI, nodebuf is a malloced block, not in the arena
int bufpos = 1;
int nodebuflen = 0;
int errcode = 0;
couchfile_modify_result *local_result = NULL;
if (start == end) {
return 0;
}
if (nptr) {
if ((nodebuflen = pread_compressed(rq->db, nptr->pointer, (char **) &nodebuf)) < 0) {
error_pass(COUCHSTORE_ERROR_READ);
}
}
local_result = make_modres(dst->arena, rq);
error_unless(local_result, COUCHSTORE_ERROR_ALLOC_FAIL);
if (nptr == NULL || nodebuf[0] == 1) { //KV Node
local_result->node_type = KV_NODE;
while (bufpos < nodebuflen) {
uint32_t klen, vlen;
get_kvlen(nodebuf + bufpos, &klen, &vlen);
sized_buf cmp_key = {nodebuf + bufpos + 5, klen};
sized_buf val_buf = {nodebuf + bufpos + 5 + klen, vlen};
bufpos += 5 + klen + vlen;
int advance = 0;
while (!advance && start < end) {
advance = 1;
int cmp_val = rq->cmp.compare(&cmp_key, rq->actions[start].key);
if (cmp_val < 0) { //Key less than action key
mr_push_item(&cmp_key, &val_buf, local_result);
} else if (cmp_val > 0) { //Key greater than action key
switch (rq->actions[start].type) {
case ACTION_INSERT:
local_result->modified = 1;
mr_push_item(rq->actions[start].key, rq->actions[start].value.data, local_result);
break;
case ACTION_REMOVE:
local_result->modified = 1;
break;
case ACTION_FETCH:
if (rq->fetch_callback) {
//not found
(*rq->fetch_callback)(rq, rq->actions[start].key, NULL, rq->actions[start].value.arg);
}
}
start++;
//Do next action on same item in the node, as our action was
//not >= it.
advance = 0;
} else if (cmp_val == 0) { //Node key is equal to action key
switch (rq->actions[start].type) {
case ACTION_INSERT:
local_result->modified = 1;
mr_push_item(rq->actions[start].key, rq->actions[start].value.data, local_result);
break;
case ACTION_REMOVE:
local_result->modified = 1;
break;
case ACTION_FETCH:
if (rq->fetch_callback) {
(*rq->fetch_callback)(rq, rq->actions[start].key, &val_buf, rq->actions[start].value.arg);
}
//Do next action on same item in the node, as our action was a fetch
//and there may be an equivalent insert or remove
//following.
advance = 0;
}
start++;
}
}
if (start == end && !advance) {
//If we've exhausted actions then just keep this key
mr_push_item(&cmp_key, &val_buf, local_result);
}
}
while (start < end) {
//We're at the end of a leaf node.
switch (rq->actions[start].type) {
case ACTION_INSERT:
local_result->modified = 1;
mr_push_item(rq->actions[start].key, rq->actions[start].value.data, local_result);
break;
case ACTION_REMOVE:
local_result->modified = 1;
break;
case ACTION_FETCH:
if (rq->fetch_callback) {
//not found
(*rq->fetch_callback)(rq, rq->actions[start].key, NULL, rq->actions[start].value.arg);
}
break;
}
start++;
}
} else if (nodebuf[0] == 0) { //KP Node
local_result->node_type = KP_NODE;
while (bufpos < nodebuflen && start < end) {
uint32_t klen, vlen;
get_kvlen(nodebuf + bufpos, &klen, &vlen);
sized_buf cmp_key = {nodebuf + bufpos + 5, klen};
sized_buf val_buf = {nodebuf + bufpos + 5 + klen, vlen};
bufpos += 5 + klen + vlen;
int cmp_val = rq->cmp.compare(&cmp_key, rq->actions[start].key);
if (bufpos == nodebuflen) {
//We're at the last item in the kpnode, must apply all our
//actions here.
node_pointer *desc = read_pointer(dst->arena, &cmp_key, val_buf.buf);
if (!desc) {
errcode = COUCHSTORE_ERROR_ALLOC_FAIL;
goto cleanup;
}
errcode = modify_node(rq, desc, start, end, local_result);
if (errcode != COUCHSTORE_SUCCESS) {
goto cleanup;
}
break;
}
if (cmp_val < 0) {
//Key in node item less than action item and not at end
//position, so just add it and continue.
node_pointer *add = read_pointer(dst->arena, &cmp_key, val_buf.buf);
if (!add) {
errcode = COUCHSTORE_ERROR_ALLOC_FAIL;
goto cleanup;
}
errcode = mr_push_pointerinfo(add, local_result);
if (errcode != COUCHSTORE_SUCCESS) {
goto cleanup;
}
} else if (cmp_val >= 0) {
//Found a key in the node greater than the one in the current
//action. Descend into the pointed node with as many actions as
//are less than the key here.
int range_end = start;
while (range_end < end &&
rq->cmp.compare(rq->actions[range_end].key, &cmp_key) <= 0) {
range_end++;
}
node_pointer *desc = read_pointer(dst->arena, &cmp_key, val_buf.buf);
if (!desc) {
errcode = COUCHSTORE_ERROR_ALLOC_FAIL;
goto cleanup;
}
errcode = modify_node(rq, desc, start, range_end, local_result);
start = range_end;
if (errcode != COUCHSTORE_SUCCESS) {
goto cleanup;
}
}
}
while (bufpos < nodebuflen) {
uint32_t klen, vlen;
get_kvlen(nodebuf + bufpos, &klen, &vlen);
sized_buf cmp_key = {nodebuf + bufpos + 5, klen};
sized_buf val_buf = {nodebuf + bufpos + 5 + klen, vlen};
bufpos += 5 + klen + vlen;
node_pointer *add = read_pointer(dst->arena, &cmp_key, val_buf.buf);
if (!add) {
errcode = COUCHSTORE_ERROR_ALLOC_FAIL;
goto cleanup;
}
errcode = mr_push_pointerinfo(add, local_result);
if (errcode != COUCHSTORE_SUCCESS) {
goto cleanup;
}
}
} else {
errcode = COUCHSTORE_ERROR_CORRUPT;
goto cleanup;
}
//If we've done modifications, write out the last leaf node.
error_pass(flush_mr(local_result))
if (!local_result->modified && nptr != NULL) {
//If we didn't do anything, give back the pointer to the original
mr_push_pointerinfo(nptr, dst);
} else {
//Otherwise, give back the pointers to the nodes we've created.
dst->modified = 1;
error_pass(mr_move_pointers(local_result, dst));
}
cleanup:
if (nodebuf) {
free(nodebuf);
}
return errcode;
}
static node_pointer *finish_root(couchfile_modify_request *rq,
couchfile_modify_result *root_result,
couchstore_error_t *errcode)
{
node_pointer *ret_ptr = NULL;
couchfile_modify_result *collector = make_modres(root_result->arena, rq);
if (!collector) {
*errcode = COUCHSTORE_ERROR_ALLOC_FAIL;
return NULL;
}
collector->modified = 1;
collector->node_type = KP_NODE;
flush_mr(root_result);
while (1) {
if (root_result->pointers_end == root_result->pointers->next) {
//The root result split into exactly one kp_node.
//Return the pointer to it.
ret_ptr = root_result->pointers_end->pointer;
break;
} else {
//The root result split into more than one kp_node.
//Move the pointer list to the value list and write out the new node.
*errcode = mr_move_pointers(root_result, collector);
if (*errcode < 0) {
goto cleanup;
}
*errcode = flush_mr(collector);
if (*errcode < 0) {
goto cleanup;
}
//Swap root_result and collector mr's.
couchfile_modify_result *tmp = root_result;
root_result = collector;
collector = tmp;
}
}
cleanup:
return ret_ptr;
}
// Copies a node_pointer and its values to the malloc heap.
static node_pointer* copy_node_pointer(node_pointer* ptr)
{
if (!ptr) {
return NULL;
}
node_pointer* ret_ptr = malloc(sizeof(node_pointer) + ptr->key.size + ptr->reduce_value.size);
if (!ret_ptr) {
return NULL;
}
*ret_ptr = *ptr;
ret_ptr->key.buf = (char*)ret_ptr + sizeof(node_pointer);
memcpy(ret_ptr->key.buf, ptr->key.buf, ptr->key.size);
ret_ptr->reduce_value.buf = (char*)ret_ptr->key.buf + ptr->key.size;
memcpy(ret_ptr->reduce_value.buf, ptr->reduce_value.buf, ptr->reduce_value.size);
return ret_ptr;
}
node_pointer *modify_btree(couchfile_modify_request *rq,
node_pointer *root,
couchstore_error_t *errcode)
{
arena* a = new_arena(ARENA_CHUNK_SIZE);
node_pointer *ret_ptr = root;
couchfile_modify_result *root_result = make_modres(a, rq);
if (!root_result) {
delete_arena(a);
*errcode = COUCHSTORE_ERROR_ALLOC_FAIL;
return root;
}
root_result->node_type = KP_NODE;
*errcode = modify_node(rq, root, 0, rq->num_actions, root_result);
if (*errcode < 0) {
delete_arena(a);
return NULL;
}
if (root_result->values_end->pointer == root) {
//If we got the root pointer back, remove it from the list
//so we don't try to free it.
root_result->values_end->pointer = NULL;
}
if (root_result->modified) {
if (root_result->count > 1 || root_result->pointers != root_result->pointers_end) {
//The root was split
//Write it to disk and return the pointer to it.
ret_ptr = finish_root(rq, root_result, errcode);
if (*errcode < 0) {
ret_ptr = NULL;
}
} else {
ret_ptr = root_result->values_end->pointer;
}
}
if (ret_ptr != root) {
ret_ptr = copy_node_pointer(ret_ptr);
}
delete_arena(a);
return ret_ptr;
}