blob: 6b64f49a0279d07f8fc7552012e3225e77716c8b [file] [log] [blame]
Chris Masonbe0e5c02007-01-26 15:51:26 -05001#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05004#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
Chris Masonbe0e5c02007-01-26 15:51:26 -05007
8static inline void init_path(struct ctree_path *p)
9{
10 memset(p, 0, sizeof(*p));
11}
12
Chris Masoneb60cea2007-02-02 09:18:22 -050013static void release_path(struct ctree_root *root, struct ctree_path *p)
14{
15 int i;
16 for (i = 0; i < MAX_LEVEL; i++) {
17 if (!p->nodes[i])
18 break;
19 tree_block_release(root, p->nodes[i]);
20 }
21}
22
Chris Mason74123bd2007-02-02 11:05:29 -050023/*
24 * The leaf data grows from end-to-front in the node.
25 * this returns the address of the start of the last item,
26 * which is the stop of the leaf data stack
27 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050028static inline unsigned int leaf_data_end(struct leaf *leaf)
29{
30 unsigned int nr = leaf->header.nritems;
31 if (nr == 0)
32 return ARRAY_SIZE(leaf->data);
33 return leaf->items[nr-1].offset;
34}
35
Chris Mason74123bd2007-02-02 11:05:29 -050036/*
37 * The space between the end of the leaf items and
38 * the start of the leaf data. IOW, how much room
39 * the leaf has left for both items and data
40 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050041static inline int leaf_free_space(struct leaf *leaf)
42{
43 int data_end = leaf_data_end(leaf);
44 int nritems = leaf->header.nritems;
45 char *items_end = (char *)(leaf->items + nritems + 1);
46 return (char *)(leaf->data + data_end) - (char *)items_end;
47}
48
Chris Mason74123bd2007-02-02 11:05:29 -050049/*
50 * compare two keys in a memcmp fashion
51 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050052int comp_keys(struct key *k1, struct key *k2)
53{
54 if (k1->objectid > k2->objectid)
55 return 1;
56 if (k1->objectid < k2->objectid)
57 return -1;
58 if (k1->flags > k2->flags)
59 return 1;
60 if (k1->flags < k2->flags)
61 return -1;
62 if (k1->offset > k2->offset)
63 return 1;
64 if (k1->offset < k2->offset)
65 return -1;
66 return 0;
67}
Chris Mason74123bd2007-02-02 11:05:29 -050068
69/*
70 * search for key in the array p. items p are item_size apart
71 * and there are 'max' items in p
72 * the slot in the array is returned via slot, and it points to
73 * the place where you would insert key if it is not found in
74 * the array.
75 *
76 * slot may point to max if the key is bigger than all of the keys
77 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050078int generic_bin_search(char *p, int item_size, struct key *key,
79 int max, int *slot)
80{
81 int low = 0;
82 int high = max;
83 int mid;
84 int ret;
85 struct key *tmp;
86
87 while(low < high) {
88 mid = (low + high) / 2;
89 tmp = (struct key *)(p + mid * item_size);
90 ret = comp_keys(tmp, key);
91
92 if (ret < 0)
93 low = mid + 1;
94 else if (ret > 0)
95 high = mid;
96 else {
97 *slot = mid;
98 return 0;
99 }
100 }
101 *slot = low;
102 return 1;
103}
104
105int bin_search(struct node *c, struct key *key, int *slot)
106{
107 if (is_leaf(c->header.flags)) {
108 struct leaf *l = (struct leaf *)c;
109 return generic_bin_search((void *)l->items, sizeof(struct item),
110 key, c->header.nritems, slot);
111 } else {
112 return generic_bin_search((void *)c->keys, sizeof(struct key),
113 key, c->header.nritems, slot);
114 }
115 return -1;
116}
117
Chris Mason74123bd2007-02-02 11:05:29 -0500118/*
119 * look for key in the tree. path is filled in with nodes along the way
120 * if key is found, we return zero and you can find the item in the leaf
121 * level of the path (level 0)
122 *
123 * If the key isn't found, the path points to the slot where it should
124 * be inserted.
125 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500126int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p)
127{
Chris Masoneb60cea2007-02-02 09:18:22 -0500128 struct tree_buffer *b = root->node;
129 struct node *c;
130
Chris Masonbe0e5c02007-01-26 15:51:26 -0500131 int slot;
132 int ret;
133 int level;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 b->count++;
135 while (b) {
136 c = &b->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500137 level = node_level(c->header.flags);
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 p->nodes[level] = b;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500139 ret = bin_search(c, key, &slot);
140 if (!is_leaf(c->header.flags)) {
141 if (ret && slot > 0)
142 slot -= 1;
143 p->slots[level] = slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 b = read_tree_block(root, c->blockptrs[slot]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500145 continue;
146 } else {
147 p->slots[level] = slot;
148 return ret;
149 }
150 }
151 return -1;
152}
153
Chris Mason74123bd2007-02-02 11:05:29 -0500154/*
155 * adjust the pointers going up the tree, starting at level
156 * making sure the right key of each node is points to 'key'.
157 * This is used after shifting pointers to the left, so it stops
158 * fixing up pointers when a given leaf/node is not in slot 0 of the
159 * higher levels
160 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500161static void fixup_low_keys(struct ctree_root *root,
162 struct ctree_path *path, struct key *key,
163 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500164{
165 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500166 for (i = level; i < MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500167 struct node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500168 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500169 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500170 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500171 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500172 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Masoneb60cea2007-02-02 09:18:22 -0500173 write_tree_block(root, path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500174 if (tslot != 0)
175 break;
176 }
177}
178
Chris Mason74123bd2007-02-02 11:05:29 -0500179/*
180 * try to push data from one node into the next node left in the
181 * tree. The src node is found at specified level in the path.
182 * If some bytes were pushed, return 0, otherwise return 1.
183 *
184 * Lower nodes/leaves in the path are not touched, higher nodes may
185 * be modified to reflect the push.
186 *
187 * The path is altered to reflect the push.
188 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500189int push_node_left(struct ctree_root *root, struct ctree_path *path, int level)
190{
191 int slot;
192 struct node *left;
193 struct node *right;
194 int push_items = 0;
195 int left_nritems;
196 int right_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500197 struct tree_buffer *t;
198 struct tree_buffer *right_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500199
200 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
201 return 1;
202 slot = path->slots[level + 1];
203 if (slot == 0)
204 return 1;
205
Chris Masoneb60cea2007-02-02 09:18:22 -0500206 t = read_tree_block(root,
207 path->nodes[level + 1]->node.blockptrs[slot - 1]);
208 left = &t->node;
209 right_buf = path->nodes[level];
210 right = &right_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500211 left_nritems = left->header.nritems;
212 right_nritems = right->header.nritems;
213 push_items = NODEPTRS_PER_BLOCK - (left_nritems + 1);
Chris Masoneb60cea2007-02-02 09:18:22 -0500214 if (push_items <= 0) {
215 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500216 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500217 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500218
219 if (right_nritems < push_items)
220 push_items = right_nritems;
221 memcpy(left->keys + left_nritems, right->keys,
222 push_items * sizeof(struct key));
223 memcpy(left->blockptrs + left_nritems, right->blockptrs,
224 push_items * sizeof(u64));
225 memmove(right->keys, right->keys + push_items,
226 (right_nritems - push_items) * sizeof(struct key));
227 memmove(right->blockptrs, right->blockptrs + push_items,
228 (right_nritems - push_items) * sizeof(u64));
229 right->header.nritems -= push_items;
230 left->header.nritems += push_items;
231
232 /* adjust the pointers going up the tree */
Chris Masoneb60cea2007-02-02 09:18:22 -0500233 fixup_low_keys(root, path, right->keys, level + 1);
234
235 write_tree_block(root, t);
236 write_tree_block(root, right_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500237
238 /* then fixup the leaf pointer in the path */
239 if (path->slots[level] < push_items) {
240 path->slots[level] += left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500241 tree_block_release(root, path->nodes[level]);
242 path->nodes[level] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500243 path->slots[level + 1] -= 1;
244 } else {
245 path->slots[level] -= push_items;
Chris Masoneb60cea2007-02-02 09:18:22 -0500246 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500247 }
248 return 0;
249}
250
Chris Mason74123bd2007-02-02 11:05:29 -0500251/*
252 * try to push data from one node into the next node right in the
253 * tree. The src node is found at specified level in the path.
254 * If some bytes were pushed, return 0, otherwise return 1.
255 *
256 * Lower nodes/leaves in the path are not touched, higher nodes may
257 * be modified to reflect the push.
258 *
259 * The path is altered to reflect the push.
260 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500261int push_node_right(struct ctree_root *root, struct ctree_path *path, int level)
262{
263 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500264 struct tree_buffer *t;
265 struct tree_buffer *src_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500266 struct node *dst;
267 struct node *src;
268 int push_items = 0;
269 int dst_nritems;
270 int src_nritems;
271
Chris Mason74123bd2007-02-02 11:05:29 -0500272 /* can't push from the root */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500273 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
274 return 1;
Chris Mason74123bd2007-02-02 11:05:29 -0500275
276 /* only try to push inside the node higher up */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500277 slot = path->slots[level + 1];
278 if (slot == NODEPTRS_PER_BLOCK - 1)
279 return 1;
280
Chris Masoneb60cea2007-02-02 09:18:22 -0500281 if (slot >= path->nodes[level + 1]->node.header.nritems -1)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500282 return 1;
283
Chris Masoneb60cea2007-02-02 09:18:22 -0500284 t = read_tree_block(root,
285 path->nodes[level + 1]->node.blockptrs[slot + 1]);
286 dst = &t->node;
287 src_buffer = path->nodes[level];
288 src = &src_buffer->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500289 dst_nritems = dst->header.nritems;
290 src_nritems = src->header.nritems;
291 push_items = NODEPTRS_PER_BLOCK - (dst_nritems + 1);
Chris Masoneb60cea2007-02-02 09:18:22 -0500292 if (push_items <= 0) {
293 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500294 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500295 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500296
297 if (src_nritems < push_items)
298 push_items = src_nritems;
299 memmove(dst->keys + push_items, dst->keys,
300 dst_nritems * sizeof(struct key));
301 memcpy(dst->keys, src->keys + src_nritems - push_items,
302 push_items * sizeof(struct key));
303
304 memmove(dst->blockptrs + push_items, dst->blockptrs,
305 dst_nritems * sizeof(u64));
306 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
307 push_items * sizeof(u64));
308
309 src->header.nritems -= push_items;
310 dst->header.nritems += push_items;
311
312 /* adjust the pointers going up the tree */
Chris Masoneb60cea2007-02-02 09:18:22 -0500313 memcpy(path->nodes[level + 1]->node.keys + path->slots[level + 1] + 1,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500314 dst->keys, sizeof(struct key));
Chris Masoneb60cea2007-02-02 09:18:22 -0500315
316 write_tree_block(root, path->nodes[level + 1]);
317 write_tree_block(root, t);
318 write_tree_block(root, src_buffer);
319
Chris Mason74123bd2007-02-02 11:05:29 -0500320 /* then fixup the pointers in the path */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500321 if (path->slots[level] >= src->header.nritems) {
322 path->slots[level] -= src->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500323 tree_block_release(root, path->nodes[level]);
324 path->nodes[level] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500325 path->slots[level + 1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500326 } else {
327 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500328 }
329 return 0;
330}
331
Chris Mason74123bd2007-02-02 11:05:29 -0500332/*
333 * worker function to insert a single pointer in a node.
334 * the node should have enough room for the pointer already
335 * slot and level indicate where you want the key to go, and
336 * blocknr is the block the key points to.
337 */
338int __insert_ptr(struct ctree_root *root,
339 struct ctree_path *path, struct key *key,
340 u64 blocknr, int slot, int level)
341{
342 struct node *c;
343 struct node *lower;
344 struct key *lower_key;
345 int nritems;
346 /* need a new root */
347 if (!path->nodes[level]) {
348 struct tree_buffer *t;
349 t = alloc_free_block(root);
350 c = &t->node;
351 memset(c, 0, sizeof(c));
352 c->header.nritems = 2;
353 c->header.flags = node_level(level);
354 c->header.blocknr = t->blocknr;
355 lower = &path->nodes[level-1]->node;
356 if (is_leaf(lower->header.flags))
357 lower_key = &((struct leaf *)lower)->items[0].key;
358 else
359 lower_key = lower->keys;
360 memcpy(c->keys, lower_key, sizeof(struct key));
361 memcpy(c->keys + 1, key, sizeof(struct key));
362 c->blockptrs[0] = path->nodes[level-1]->blocknr;
363 c->blockptrs[1] = blocknr;
364 /* the path has an extra ref to root->node */
365 tree_block_release(root, root->node);
366 root->node = t;
367 t->count++;
368 write_tree_block(root, t);
369 path->nodes[level] = t;
370 path->slots[level] = 0;
371 if (c->keys[1].objectid == 0)
372 BUG();
373 return 0;
374 }
375 lower = &path->nodes[level]->node;
376 nritems = lower->header.nritems;
377 if (slot > nritems)
378 BUG();
379 if (nritems == NODEPTRS_PER_BLOCK)
380 BUG();
381 if (slot != nritems) {
382 memmove(lower->keys + slot + 1, lower->keys + slot,
383 (nritems - slot) * sizeof(struct key));
384 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
385 (nritems - slot) * sizeof(u64));
386 }
387 memcpy(lower->keys + slot, key, sizeof(struct key));
388 lower->blockptrs[slot] = blocknr;
389 lower->header.nritems++;
390 if (lower->keys[1].objectid == 0)
391 BUG();
392 write_tree_block(root, path->nodes[level]);
393 return 0;
394}
395
396
397/*
398 * insert a key,blocknr pair into the tree at a given level
399 * If the node at that level in the path doesn't have room,
400 * it is split or shifted as appropriate.
401 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500402int insert_ptr(struct ctree_root *root,
403 struct ctree_path *path, struct key *key,
404 u64 blocknr, int level)
405{
Chris Masoneb60cea2007-02-02 09:18:22 -0500406 struct tree_buffer *t = path->nodes[level];
407 struct node *c = &path->nodes[level]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500408 struct node *b;
Chris Masoneb60cea2007-02-02 09:18:22 -0500409 struct tree_buffer *b_buffer;
410 struct tree_buffer *bal[MAX_LEVEL];
Chris Masonbe0e5c02007-01-26 15:51:26 -0500411 int bal_level = level;
412 int mid;
413 int bal_start = -1;
414
Chris Mason74123bd2007-02-02 11:05:29 -0500415 /*
416 * check to see if we need to make room in the node for this
417 * pointer. If we do, keep walking the tree, making sure there
418 * is enough room in each level for the required insertions.
419 *
420 * The bal array is filled in with any nodes to be inserted
421 * due to splitting. Once we've done all the splitting required
422 * do the inserts based on the data in the bal array.
423 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500424 memset(bal, 0, ARRAY_SIZE(bal));
Chris Masoneb60cea2007-02-02 09:18:22 -0500425 while(t && t->node.header.nritems == NODEPTRS_PER_BLOCK) {
426 c = &t->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500427 if (push_node_left(root, path,
428 node_level(c->header.flags)) == 0)
429 break;
430 if (push_node_right(root, path,
431 node_level(c->header.flags)) == 0)
432 break;
433 bal_start = bal_level;
434 if (bal_level == MAX_LEVEL - 1)
435 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500436 b_buffer = alloc_free_block(root);
437 b = &b_buffer->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500438 b->header.flags = c->header.flags;
Chris Masoneb60cea2007-02-02 09:18:22 -0500439 b->header.blocknr = b_buffer->blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500440 mid = (c->header.nritems + 1) / 2;
441 memcpy(b->keys, c->keys + mid,
442 (c->header.nritems - mid) * sizeof(struct key));
443 memcpy(b->blockptrs, c->blockptrs + mid,
444 (c->header.nritems - mid) * sizeof(u64));
445 b->header.nritems = c->header.nritems - mid;
446 c->header.nritems = mid;
Chris Masoneb60cea2007-02-02 09:18:22 -0500447
448 write_tree_block(root, t);
449 write_tree_block(root, b_buffer);
450
451 bal[bal_level] = b_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500452 if (bal_level == MAX_LEVEL - 1)
453 break;
454 bal_level += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500455 t = path->nodes[bal_level];
Chris Masonbe0e5c02007-01-26 15:51:26 -0500456 }
Chris Mason74123bd2007-02-02 11:05:29 -0500457 /*
458 * bal_start tells us the first level in the tree that needed to
459 * be split. Go through the bal array inserting the new nodes
460 * as needed. The path is fixed as we go.
461 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500462 while(bal_start > 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500463 b_buffer = bal[bal_start];
464 c = &path->nodes[bal_start]->node;
465 __insert_ptr(root, path, b_buffer->node.keys, b_buffer->blocknr,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500466 path->slots[bal_start + 1] + 1, bal_start + 1);
467 if (path->slots[bal_start] >= c->header.nritems) {
468 path->slots[bal_start] -= c->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500469 tree_block_release(root, path->nodes[bal_start]);
470 path->nodes[bal_start] = b_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500471 path->slots[bal_start + 1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500472 } else {
473 tree_block_release(root, b_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500474 }
475 bal_start--;
476 if (!bal[bal_start])
477 break;
478 }
Chris Mason74123bd2007-02-02 11:05:29 -0500479 /* Now that the tree has room, insert the requested pointer */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500480 return __insert_ptr(root, path, key, blocknr, path->slots[level] + 1,
481 level);
482}
483
Chris Mason74123bd2007-02-02 11:05:29 -0500484/*
485 * how many bytes are required to store the items in a leaf. start
486 * and nr indicate which items in the leaf to check. This totals up the
487 * space used both by the item structs and the item data
488 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500489int leaf_space_used(struct leaf *l, int start, int nr)
490{
491 int data_len;
492 int end = start + nr - 1;
493
494 if (!nr)
495 return 0;
496 data_len = l->items[start].offset + l->items[start].size;
497 data_len = data_len - l->items[end].offset;
498 data_len += sizeof(struct item) * nr;
499 return data_len;
500}
501
Chris Mason74123bd2007-02-02 11:05:29 -0500502/*
503 * push some data in the path leaf to the left, trying to free up at
504 * least data_size bytes. returns zero if the push worked, nonzero otherwise
505 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500506int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
507 int data_size)
508{
Chris Masoneb60cea2007-02-02 09:18:22 -0500509 struct tree_buffer *right_buf = path->nodes[0];
510 struct leaf *right = &right_buf->leaf;
511 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500512 struct leaf *left;
513 int slot;
514 int i;
515 int free_space;
516 int push_space = 0;
517 int push_items = 0;
518 struct item *item;
519 int old_left_nritems;
520
521 slot = path->slots[1];
522 if (slot == 0) {
523 return 1;
524 }
525 if (!path->nodes[1]) {
526 return 1;
527 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500528 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
529 left = &t->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500530 free_space = leaf_free_space(left);
531 if (free_space < data_size + sizeof(struct item)) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500532 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500533 return 1;
534 }
535 for (i = 0; i < right->header.nritems; i++) {
536 item = right->items + i;
537 if (path->slots[0] == i)
538 push_space += data_size + sizeof(*item);
539 if (item->size + sizeof(*item) + push_space > free_space)
540 break;
541 push_items++;
542 push_space += item->size + sizeof(*item);
543 }
544 if (push_items == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500545 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500546 return 1;
547 }
548 /* push data from right to left */
549 memcpy(left->items + left->header.nritems,
550 right->items, push_items * sizeof(struct item));
551 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
552 memcpy(left->data + leaf_data_end(left) - push_space,
553 right->data + right->items[push_items - 1].offset,
554 push_space);
555 old_left_nritems = left->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500556 BUG_ON(old_left_nritems < 0);
557
Chris Masonbe0e5c02007-01-26 15:51:26 -0500558 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
559 left->items[i].offset -= LEAF_DATA_SIZE -
560 left->items[old_left_nritems -1].offset;
561 }
562 left->header.nritems += push_items;
563
564 /* fixup right node */
565 push_space = right->items[push_items-1].offset - leaf_data_end(right);
566 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
567 leaf_data_end(right), push_space);
568 memmove(right->items, right->items + push_items,
569 (right->header.nritems - push_items) * sizeof(struct item));
570 right->header.nritems -= push_items;
571 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500572
Chris Masonbe0e5c02007-01-26 15:51:26 -0500573 for (i = 0; i < right->header.nritems; i++) {
574 right->items[i].offset = push_space - right->items[i].size;
575 push_space = right->items[i].offset;
576 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500577
578 write_tree_block(root, t);
579 write_tree_block(root, right_buf);
580
581 fixup_low_keys(root, path, &right->items[0].key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500582
583 /* then fixup the leaf pointer in the path */
584 if (path->slots[0] < push_items) {
585 path->slots[0] += old_left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500586 tree_block_release(root, path->nodes[0]);
587 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500588 path->slots[1] -= 1;
589 } else {
Chris Masoneb60cea2007-02-02 09:18:22 -0500590 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500591 path->slots[0] -= push_items;
592 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500593 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500594 return 0;
595}
596
Chris Mason74123bd2007-02-02 11:05:29 -0500597/*
598 * split the path's leaf in two, making sure there is at least data_size
599 * available for the resulting leaf level of the path.
600 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500601int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size)
602{
Chris Masoneb60cea2007-02-02 09:18:22 -0500603 struct tree_buffer *l_buf = path->nodes[0];
604 struct leaf *l = &l_buf->leaf;
605 int nritems;
606 int mid;
607 int slot;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500608 struct leaf *right;
Chris Masoneb60cea2007-02-02 09:18:22 -0500609 struct tree_buffer *right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500610 int space_needed = data_size + sizeof(struct item);
611 int data_copy_size;
612 int rt_data_off;
613 int i;
614 int ret;
615
616 if (push_leaf_left(root, path, data_size) == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500617 l_buf = path->nodes[0];
618 l = &l_buf->leaf;
619 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
620 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500621 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500622 slot = path->slots[0];
623 nritems = l->header.nritems;
624 mid = (nritems + 1)/ 2;
625
626 right_buffer = alloc_free_block(root);
627 BUG_ON(!right_buffer);
628 BUG_ON(mid == nritems);
629 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500630 memset(right, 0, sizeof(*right));
631 if (mid <= slot) {
632 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
633 LEAF_DATA_SIZE)
634 BUG();
635 } else {
636 if (leaf_space_used(l, 0, mid + 1) + space_needed >
637 LEAF_DATA_SIZE)
638 BUG();
639 }
640 right->header.nritems = nritems - mid;
Chris Masoneb60cea2007-02-02 09:18:22 -0500641 right->header.blocknr = right_buffer->blocknr;
642 right->header.flags = node_level(0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500643 data_copy_size = l->items[mid].offset + l->items[mid].size -
644 leaf_data_end(l);
645 memcpy(right->items, l->items + mid,
646 (nritems - mid) * sizeof(struct item));
647 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
648 l->data + leaf_data_end(l), data_copy_size);
649 rt_data_off = LEAF_DATA_SIZE -
650 (l->items[mid].offset + l->items[mid].size);
Chris Mason74123bd2007-02-02 11:05:29 -0500651
652 for (i = 0; i < right->header.nritems; i++)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500653 right->items[i].offset += rt_data_off;
Chris Mason74123bd2007-02-02 11:05:29 -0500654
Chris Masonbe0e5c02007-01-26 15:51:26 -0500655 l->header.nritems = mid;
656 ret = insert_ptr(root, path, &right->items[0].key,
Chris Masoneb60cea2007-02-02 09:18:22 -0500657 right_buffer->blocknr, 1);
658
659 write_tree_block(root, right_buffer);
660 write_tree_block(root, l_buf);
661
662 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500663 if (mid <= slot) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500664 tree_block_release(root, path->nodes[0]);
665 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500666 path->slots[0] -= mid;
667 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500668 } else
669 tree_block_release(root, right_buffer);
670 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500671 return ret;
672}
673
Chris Mason74123bd2007-02-02 11:05:29 -0500674/*
675 * Given a key and some data, insert an item into the tree.
676 * This does all the path init required, making room in the tree if needed.
677 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500678int insert_item(struct ctree_root *root, struct key *key,
679 void *data, int data_size)
680{
681 int ret;
682 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500683 int slot_orig;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500684 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500685 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500686 unsigned int nritems;
687 unsigned int data_end;
688 struct ctree_path path;
689
Chris Mason74123bd2007-02-02 11:05:29 -0500690 /* create a root if there isn't one */
Chris Masoneb60cea2007-02-02 09:18:22 -0500691 if (!root->node) {
692 struct tree_buffer *t;
693 t = alloc_free_block(root);
694 BUG_ON(!t);
695 t->node.header.nritems = 0;
696 t->node.header.flags = node_level(0);
697 t->node.header.blocknr = t->blocknr;
698 root->node = t;
699 write_tree_block(root, t);
700 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500701 init_path(&path);
702 ret = search_slot(root, key, &path);
Chris Masoneb60cea2007-02-02 09:18:22 -0500703 if (ret == 0) {
704 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500705 return -EEXIST;
Chris Masoneb60cea2007-02-02 09:18:22 -0500706 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500707
Chris Masoneb60cea2007-02-02 09:18:22 -0500708 slot_orig = path.slots[0];
709 leaf_buf = path.nodes[0];
710 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -0500711
712 /* make room if needed */
Chris Masoneb60cea2007-02-02 09:18:22 -0500713 if (leaf_free_space(leaf) < sizeof(struct item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500714 split_leaf(root, &path, data_size);
Chris Masoneb60cea2007-02-02 09:18:22 -0500715 leaf_buf = path.nodes[0];
716 leaf = &path.nodes[0]->leaf;
717 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500718 nritems = leaf->header.nritems;
719 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500720
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
722 BUG();
723
724 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -0500725 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726 if (slot == 0)
Chris Masoneb60cea2007-02-02 09:18:22 -0500727 fixup_low_keys(root, &path, key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728 if (slot != nritems) {
729 int i;
730 unsigned int old_data = leaf->items[slot].offset +
731 leaf->items[slot].size;
732
733 /*
734 * item0..itemN ... dataN.offset..dataN.size .. data0.size
735 */
736 /* first correct the data pointers */
737 for (i = slot; i < nritems; i++)
738 leaf->items[i].offset -= data_size;
739
740 /* shift the items */
741 memmove(leaf->items + slot + 1, leaf->items + slot,
742 (nritems - slot) * sizeof(struct item));
743
744 /* shift the data */
745 memmove(leaf->data + data_end - data_size, leaf->data +
746 data_end, old_data - data_end);
747 data_end = old_data;
748 }
Chris Mason74123bd2007-02-02 11:05:29 -0500749 /* copy the new data in */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500750 memcpy(&leaf->items[slot].key, key, sizeof(struct key));
751 leaf->items[slot].offset = data_end - data_size;
752 leaf->items[slot].size = data_size;
753 memcpy(leaf->data + data_end - data_size, data, data_size);
754 leaf->header.nritems += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500755 write_tree_block(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500756 if (leaf_free_space(leaf) < 0)
757 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500758 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500759 return 0;
760}
761
Chris Mason74123bd2007-02-02 11:05:29 -0500762/*
763 * delete the pointer from a given level in the path. The path is not
764 * fixed up, so after calling this it is not valid at that level.
765 *
766 * If the delete empties a node, the node is removed from the tree,
767 * continuing all the way the root if required. The root is converted into
768 * a leaf if all the nodes are emptied.
769 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500770int del_ptr(struct ctree_root *root, struct ctree_path *path, int level)
771{
772 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500773 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500774 struct node *node;
775 int nritems;
776
777 while(1) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500778 t = path->nodes[level];
779 if (!t)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500780 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500781 node = &t->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500782 slot = path->slots[level];
783 nritems = node->header.nritems;
784
785 if (slot != nritems -1) {
786 memmove(node->keys + slot, node->keys + slot + 1,
787 sizeof(struct key) * (nritems - slot - 1));
788 memmove(node->blockptrs + slot,
789 node->blockptrs + slot + 1,
790 sizeof(u64) * (nritems - slot - 1));
791 }
792 node->header.nritems--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500793 write_tree_block(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500794 if (node->header.nritems != 0) {
795 int tslot;
796 if (slot == 0)
Chris Masoneb60cea2007-02-02 09:18:22 -0500797 fixup_low_keys(root, path, node->keys,
798 level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500799 tslot = path->slots[level+1];
Chris Masoneb60cea2007-02-02 09:18:22 -0500800 t->count++;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500801 push_node_left(root, path, level);
802 if (node->header.nritems) {
803 push_node_right(root, path, level);
804 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500805 if (node->header.nritems) {
806 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500807 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500808 }
809 tree_block_release(root, t);
Chris Mason4920c9a2007-01-26 16:38:42 -0500810 path->slots[level+1] = tslot;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500811 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500812 if (t == root->node) {
813 /* just turn the root into a leaf and break */
814 root->node->node.header.flags = node_level(0);
815 write_tree_block(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500816 break;
817 }
818 level++;
819 if (!path->nodes[level])
820 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -0500821 }
822 return 0;
823}
824
Chris Mason74123bd2007-02-02 11:05:29 -0500825/*
826 * delete the item at the leaf level in path. If that empties
827 * the leaf, remove it from the tree
828 */
Chris Mason4920c9a2007-01-26 16:38:42 -0500829int del_item(struct ctree_root *root, struct ctree_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500830{
Chris Masonbe0e5c02007-01-26 15:51:26 -0500831 int slot;
832 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500833 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500834 int doff;
835 int dsize;
836
Chris Masoneb60cea2007-02-02 09:18:22 -0500837 leaf_buf = path->nodes[0];
838 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -0500839 slot = path->slots[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -0500840 doff = leaf->items[slot].offset;
841 dsize = leaf->items[slot].size;
842
843 if (slot != leaf->header.nritems - 1) {
844 int i;
845 int data_end = leaf_data_end(leaf);
846 memmove(leaf->data + data_end + dsize,
847 leaf->data + data_end,
848 doff - data_end);
849 for (i = slot + 1; i < leaf->header.nritems; i++)
850 leaf->items[i].offset += dsize;
851 memmove(leaf->items + slot, leaf->items + slot + 1,
852 sizeof(struct item) *
853 (leaf->header.nritems - slot - 1));
854 }
855 leaf->header.nritems -= 1;
Chris Mason74123bd2007-02-02 11:05:29 -0500856 /* delete the leaf if we've emptied it */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500857 if (leaf->header.nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500858 if (leaf_buf == root->node) {
859 leaf->header.flags = node_level(0);
860 write_tree_block(root, leaf_buf);
861 } else
Chris Mason4920c9a2007-01-26 16:38:42 -0500862 del_ptr(root, path, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500863 } else {
864 if (slot == 0)
Chris Masoneb60cea2007-02-02 09:18:22 -0500865 fixup_low_keys(root, path, &leaf->items[0].key, 1);
866 write_tree_block(root, leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -0500867 /* delete the leaf if it is mostly empty */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500868 if (leaf_space_used(leaf, 0, leaf->header.nritems) <
869 LEAF_DATA_SIZE / 4) {
870 /* push_leaf_left fixes the path.
871 * make sure the path still points to our leaf
872 * for possible call to del_ptr below
873 */
Chris Mason4920c9a2007-01-26 16:38:42 -0500874 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -0500875 leaf_buf->count++;
Chris Mason4920c9a2007-01-26 16:38:42 -0500876 push_leaf_left(root, path, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500877 if (leaf->header.nritems == 0) {
Chris Mason4920c9a2007-01-26 16:38:42 -0500878 path->slots[1] = slot;
879 del_ptr(root, path, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500880 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500881 tree_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500882 }
883 }
884 return 0;
885}
886
887void print_leaf(struct leaf *l)
888{
889 int i;
890 int nr = l->header.nritems;
891 struct item *item;
Chris Masoneb60cea2007-02-02 09:18:22 -0500892 printf("leaf %lu total ptrs %d free space %d\n", l->header.blocknr, nr,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500893 leaf_free_space(l));
894 fflush(stdout);
895 for (i = 0 ; i < nr ; i++) {
896 item = l->items + i;
897 printf("\titem %d key (%lu %u %lu) itemoff %d itemsize %d\n",
898 i,
899 item->key.objectid, item->key.flags, item->key.offset,
900 item->offset, item->size);
901 fflush(stdout);
902 printf("\t\titem data %.*s\n", item->size, l->data+item->offset);
903 fflush(stdout);
904 }
905}
Chris Masoneb60cea2007-02-02 09:18:22 -0500906void print_tree(struct ctree_root *root, struct tree_buffer *t)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500907{
908 int i;
909 int nr;
Chris Masoneb60cea2007-02-02 09:18:22 -0500910 struct node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500911
Chris Masoneb60cea2007-02-02 09:18:22 -0500912 if (!t)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500913 return;
Chris Masoneb60cea2007-02-02 09:18:22 -0500914 c = &t->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500915 nr = c->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500916 if (c->header.blocknr != t->blocknr)
917 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -0500918 if (is_leaf(c->header.flags)) {
919 print_leaf((struct leaf *)c);
920 return;
921 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500922 printf("node %lu level %d total ptrs %d free spc %lu\n", t->blocknr,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500923 node_level(c->header.flags), c->header.nritems,
924 NODEPTRS_PER_BLOCK - c->header.nritems);
925 fflush(stdout);
926 for (i = 0; i < nr; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500927 printf("\tkey %d (%lu %u %lu) block %lu\n",
Chris Masonbe0e5c02007-01-26 15:51:26 -0500928 i,
929 c->keys[i].objectid, c->keys[i].flags, c->keys[i].offset,
930 c->blockptrs[i]);
931 fflush(stdout);
932 }
933 for (i = 0; i < nr; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500934 struct tree_buffer *next_buf = read_tree_block(root,
935 c->blockptrs[i]);
936 struct node *next = &next_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500937 if (is_leaf(next->header.flags) &&
938 node_level(c->header.flags) != 1)
939 BUG();
940 if (node_level(next->header.flags) !=
941 node_level(c->header.flags) - 1)
942 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500943 print_tree(root, next_buf);
944 tree_block_release(root, next_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500945 }
946
947}
948
949/* for testing only */
950int next_key(int i, int max_key) {
951 return rand() % max_key;
952 // return i;
953}
954
955int main() {
Chris Masoneb60cea2007-02-02 09:18:22 -0500956 struct ctree_root *root;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500957 struct key ins;
Chris Mason4920c9a2007-01-26 16:38:42 -0500958 struct key last = { (u64)-1, 0, 0};
Chris Masonbe0e5c02007-01-26 15:51:26 -0500959 char *buf;
960 int i;
961 int num;
962 int ret;
Chris Mason74123bd2007-02-02 11:05:29 -0500963 int run_size = 25000;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500964 int max_key = 100000000;
965 int tree_size = 0;
966 struct ctree_path path;
967
Chris Masoneb60cea2007-02-02 09:18:22 -0500968 radix_tree_init();
969
970
971 root = open_ctree("dbfile");
Chris Masonbe0e5c02007-01-26 15:51:26 -0500972
973 srand(55);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500974 for (i = 0; i < run_size; i++) {
975 buf = malloc(64);
976 num = next_key(i, max_key);
977 // num = i;
978 sprintf(buf, "string-%d", num);
979 // printf("insert %d\n", num);
980 ins.objectid = num;
981 ins.offset = 0;
982 ins.flags = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500983 ret = insert_item(root, &ins, buf, strlen(buf));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500984 if (!ret)
985 tree_size++;
986 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500987 close_ctree(root);
988 root = open_ctree("dbfile");
989 printf("starting search\n");
Chris Masonbe0e5c02007-01-26 15:51:26 -0500990 srand(55);
991 for (i = 0; i < run_size; i++) {
992 num = next_key(i, max_key);
993 ins.objectid = num;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500994 init_path(&path);
Chris Masoneb60cea2007-02-02 09:18:22 -0500995 ret = search_slot(root, &ins, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500996 if (ret) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500997 print_tree(root, root->node);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500998 printf("unable to find %d\n", num);
999 exit(1);
1000 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001001 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001002 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001003 close_ctree(root);
1004 root = open_ctree("dbfile");
1005 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
1006 node_level(root->node->node.header.flags),
1007 root->node->node.header.nritems,
1008 NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
1009 printf("all searches good, deleting some items\n");
Chris Masonbe0e5c02007-01-26 15:51:26 -05001010 i = 0;
1011 srand(55);
Chris Mason4920c9a2007-01-26 16:38:42 -05001012 for (i = 0 ; i < run_size/4; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001013 num = next_key(i, max_key);
1014 ins.objectid = num;
Chris Mason4920c9a2007-01-26 16:38:42 -05001015 init_path(&path);
Chris Masoneb60cea2007-02-02 09:18:22 -05001016 ret = search_slot(root, &ins, &path);
Chris Mason4920c9a2007-01-26 16:38:42 -05001017 if (ret)
1018 continue;
Chris Masoneb60cea2007-02-02 09:18:22 -05001019 ret = del_item(root, &path);
Chris Mason4920c9a2007-01-26 16:38:42 -05001020 if (ret != 0)
1021 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -05001022 release_path(root, &path);
Chris Mason4920c9a2007-01-26 16:38:42 -05001023 tree_size--;
1024 }
1025 srand(128);
1026 for (i = 0; i < run_size; i++) {
1027 buf = malloc(64);
1028 num = next_key(i, max_key);
1029 sprintf(buf, "string-%d", num);
1030 ins.objectid = num;
Chris Masoneb60cea2007-02-02 09:18:22 -05001031 ret = insert_item(root, &ins, buf, strlen(buf));
Chris Mason4920c9a2007-01-26 16:38:42 -05001032 if (!ret)
1033 tree_size++;
1034 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001035 close_ctree(root);
1036 root = open_ctree("dbfile");
1037 printf("starting search2\n");
1038 srand(128);
1039 for (i = 0; i < run_size; i++) {
1040 num = next_key(i, max_key);
1041 ins.objectid = num;
1042 init_path(&path);
1043 ret = search_slot(root, &ins, &path);
1044 if (ret) {
1045 print_tree(root, root->node);
1046 printf("unable to find %d\n", num);
1047 exit(1);
1048 }
1049 release_path(root, &path);
1050 }
1051 printf("starting big long delete run\n");
1052 while(root->node && root->node->node.header.nritems > 0) {
Chris Mason4920c9a2007-01-26 16:38:42 -05001053 struct leaf *leaf;
1054 int slot;
1055 ins.objectid = (u64)-1;
1056 init_path(&path);
Chris Masoneb60cea2007-02-02 09:18:22 -05001057 ret = search_slot(root, &ins, &path);
Chris Mason4920c9a2007-01-26 16:38:42 -05001058 if (ret == 0)
1059 BUG();
1060
Chris Masoneb60cea2007-02-02 09:18:22 -05001061 leaf = &path.nodes[0]->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001062 slot = path.slots[0];
1063 if (slot != leaf->header.nritems)
1064 BUG();
1065 while(path.slots[0] > 0) {
1066 path.slots[0] -= 1;
1067 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001068 leaf = &path.nodes[0]->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001069
1070 if (comp_keys(&last, &leaf->items[slot].key) <= 0)
1071 BUG();
1072 memcpy(&last, &leaf->items[slot].key, sizeof(last));
Chris Masoneb60cea2007-02-02 09:18:22 -05001073 ret = del_item(root, &path);
1074 if (ret != 0) {
1075 printf("del_item returned %d\n", ret);
Chris Mason4920c9a2007-01-26 16:38:42 -05001076 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -05001077 }
Chris Mason4920c9a2007-01-26 16:38:42 -05001078 tree_size--;
1079 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001080 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001081 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001082 close_ctree(root);
Chris Mason4920c9a2007-01-26 16:38:42 -05001083 printf("tree size is now %d\n", tree_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001084 return 0;
1085}