3 #include "kerncompat.h"
4 #include "radix-tree.h"
8 static inline void init_path(struct ctree_path *p)
10 memset(p, 0, sizeof(*p));
13 static void release_path(struct ctree_root *root, struct ctree_path *p)
16 for (i = 0; i < MAX_LEVEL; i++) {
19 tree_block_release(root, p->nodes[i]);
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
28 static inline unsigned int leaf_data_end(struct leaf *leaf)
30 unsigned int nr = leaf->header.nritems;
32 return ARRAY_SIZE(leaf->data);
33 return leaf->items[nr-1].offset;
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
41 static inline int leaf_free_space(struct leaf *leaf)
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;
50 * compare two keys in a memcmp fashion
52 int comp_keys(struct key *k1, struct key *k2)
54 if (k1->objectid > k2->objectid)
56 if (k1->objectid < k2->objectid)
58 if (k1->flags > k2->flags)
60 if (k1->flags < k2->flags)
62 if (k1->offset > k2->offset)
64 if (k1->offset < k2->offset)
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
76 * slot may point to max if the key is bigger than all of the keys
78 int generic_bin_search(char *p, int item_size, struct key *key,
88 mid = (low + high) / 2;
89 tmp = (struct key *)(p + mid * item_size);
90 ret = comp_keys(tmp, key);
105 int bin_search(struct node *c, struct key *key, int *slot)
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);
112 return generic_bin_search((void *)c->keys, sizeof(struct key),
113 key, c->header.nritems, slot);
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)
123 * If the key isn't found, the path points to the slot where it should
126 int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p)
128 struct tree_buffer *b = root->node;
137 level = node_level(c->header.flags);
139 ret = bin_search(c, key, &slot);
140 if (!is_leaf(c->header.flags)) {
143 p->slots[level] = slot;
144 b = read_tree_block(root, c->blockptrs[slot]);
147 p->slots[level] = slot;
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
161 static void fixup_low_keys(struct ctree_root *root,
162 struct ctree_path *path, struct key *key,
166 for (i = level; i < MAX_LEVEL; i++) {
168 int tslot = path->slots[i];
171 t = &path->nodes[i]->node;
172 memcpy(t->keys + tslot, key, sizeof(*key));
173 write_tree_block(root, path->nodes[i]);
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.
184 * Lower nodes/leaves in the path are not touched, higher nodes may
185 * be modified to reflect the push.
187 * The path is altered to reflect the push.
189 int push_node_left(struct ctree_root *root, struct ctree_path *path, int level)
197 struct tree_buffer *t;
198 struct tree_buffer *right_buf;
200 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
202 slot = path->slots[level + 1];
206 t = read_tree_block(root,
207 path->nodes[level + 1]->node.blockptrs[slot - 1]);
209 right_buf = path->nodes[level];
210 right = &right_buf->node;
211 left_nritems = left->header.nritems;
212 right_nritems = right->header.nritems;
213 push_items = NODEPTRS_PER_BLOCK - (left_nritems + 1);
214 if (push_items <= 0) {
215 tree_block_release(root, t);
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;
232 /* adjust the pointers going up the tree */
233 fixup_low_keys(root, path, right->keys, level + 1);
235 write_tree_block(root, t);
236 write_tree_block(root, right_buf);
238 /* then fixup the leaf pointer in the path */
239 if (path->slots[level] < push_items) {
240 path->slots[level] += left_nritems;
241 tree_block_release(root, path->nodes[level]);
242 path->nodes[level] = t;
243 path->slots[level + 1] -= 1;
245 path->slots[level] -= push_items;
246 tree_block_release(root, t);
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.
256 * Lower nodes/leaves in the path are not touched, higher nodes may
257 * be modified to reflect the push.
259 * The path is altered to reflect the push.
261 int push_node_right(struct ctree_root *root, struct ctree_path *path, int level)
264 struct tree_buffer *t;
265 struct tree_buffer *src_buffer;
272 /* can't push from the root */
273 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
276 /* only try to push inside the node higher up */
277 slot = path->slots[level + 1];
278 if (slot == NODEPTRS_PER_BLOCK - 1)
281 if (slot >= path->nodes[level + 1]->node.header.nritems -1)
284 t = read_tree_block(root,
285 path->nodes[level + 1]->node.blockptrs[slot + 1]);
287 src_buffer = path->nodes[level];
288 src = &src_buffer->node;
289 dst_nritems = dst->header.nritems;
290 src_nritems = src->header.nritems;
291 push_items = NODEPTRS_PER_BLOCK - (dst_nritems + 1);
292 if (push_items <= 0) {
293 tree_block_release(root, t);
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));
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));
309 src->header.nritems -= push_items;
310 dst->header.nritems += push_items;
312 /* adjust the pointers going up the tree */
313 memcpy(path->nodes[level + 1]->node.keys + path->slots[level + 1] + 1,
314 dst->keys, sizeof(struct key));
316 write_tree_block(root, path->nodes[level + 1]);
317 write_tree_block(root, t);
318 write_tree_block(root, src_buffer);
320 /* then fixup the pointers in the path */
321 if (path->slots[level] >= src->header.nritems) {
322 path->slots[level] -= src->header.nritems;
323 tree_block_release(root, path->nodes[level]);
324 path->nodes[level] = t;
325 path->slots[level + 1] += 1;
327 tree_block_release(root, t);
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.
338 int __insert_ptr(struct ctree_root *root,
339 struct ctree_path *path, struct key *key,
340 u64 blocknr, int slot, int level)
344 struct key *lower_key;
346 /* need a new root */
347 if (!path->nodes[level]) {
348 struct tree_buffer *t;
349 t = alloc_free_block(root);
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;
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);
368 write_tree_block(root, t);
369 path->nodes[level] = t;
370 path->slots[level] = 0;
371 if (c->keys[1].objectid == 0)
375 lower = &path->nodes[level]->node;
376 nritems = lower->header.nritems;
379 if (nritems == NODEPTRS_PER_BLOCK)
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));
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)
392 write_tree_block(root, path->nodes[level]);
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.
402 int insert_ptr(struct ctree_root *root,
403 struct ctree_path *path, struct key *key,
404 u64 blocknr, int level)
406 struct tree_buffer *t = path->nodes[level];
407 struct node *c = &path->nodes[level]->node;
409 struct tree_buffer *b_buffer;
410 struct tree_buffer *bal[MAX_LEVEL];
411 int bal_level = level;
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.
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.
424 memset(bal, 0, ARRAY_SIZE(bal));
425 while(t && t->node.header.nritems == NODEPTRS_PER_BLOCK) {
427 if (push_node_left(root, path,
428 node_level(c->header.flags)) == 0)
430 if (push_node_right(root, path,
431 node_level(c->header.flags)) == 0)
433 bal_start = bal_level;
434 if (bal_level == MAX_LEVEL - 1)
436 b_buffer = alloc_free_block(root);
438 b->header.flags = c->header.flags;
439 b->header.blocknr = b_buffer->blocknr;
440 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;
448 write_tree_block(root, t);
449 write_tree_block(root, b_buffer);
451 bal[bal_level] = b_buffer;
452 if (bal_level == MAX_LEVEL - 1)
455 t = path->nodes[bal_level];
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.
462 while(bal_start > 0) {
463 b_buffer = bal[bal_start];
464 c = &path->nodes[bal_start]->node;
465 __insert_ptr(root, path, b_buffer->node.keys, b_buffer->blocknr,
466 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;
469 tree_block_release(root, path->nodes[bal_start]);
470 path->nodes[bal_start] = b_buffer;
471 path->slots[bal_start + 1] += 1;
473 tree_block_release(root, b_buffer);
479 /* Now that the tree has room, insert the requested pointer */
480 return __insert_ptr(root, path, key, blocknr, path->slots[level] + 1,
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
489 int leaf_space_used(struct leaf *l, int start, int nr)
492 int end = start + nr - 1;
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;
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
506 int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
509 struct tree_buffer *right_buf = path->nodes[0];
510 struct leaf *right = &right_buf->leaf;
511 struct tree_buffer *t;
519 int old_left_nritems;
521 slot = path->slots[1];
525 if (!path->nodes[1]) {
528 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
530 free_space = leaf_free_space(left);
531 if (free_space < data_size + sizeof(struct item)) {
532 tree_block_release(root, t);
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)
542 push_space += item->size + sizeof(*item);
544 if (push_items == 0) {
545 tree_block_release(root, t);
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,
555 old_left_nritems = left->header.nritems;
556 BUG_ON(old_left_nritems < 0);
558 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;
562 left->header.nritems += push_items;
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;
573 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;
578 write_tree_block(root, t);
579 write_tree_block(root, right_buf);
581 fixup_low_keys(root, path, &right->items[0].key, 1);
583 /* then fixup the leaf pointer in the path */
584 if (path->slots[0] < push_items) {
585 path->slots[0] += old_left_nritems;
586 tree_block_release(root, path->nodes[0]);
590 tree_block_release(root, t);
591 path->slots[0] -= push_items;
593 BUG_ON(path->slots[0] < 0);
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.
601 int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size)
603 struct tree_buffer *l_buf = path->nodes[0];
604 struct leaf *l = &l_buf->leaf;
609 struct tree_buffer *right_buffer;
610 int space_needed = data_size + sizeof(struct item);
616 if (push_leaf_left(root, path, data_size) == 0) {
617 l_buf = path->nodes[0];
619 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
622 slot = path->slots[0];
623 nritems = l->header.nritems;
624 mid = (nritems + 1)/ 2;
626 right_buffer = alloc_free_block(root);
627 BUG_ON(!right_buffer);
628 BUG_ON(mid == nritems);
629 right = &right_buffer->leaf;
630 memset(right, 0, sizeof(*right));
632 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
636 if (leaf_space_used(l, 0, mid + 1) + space_needed >
640 right->header.nritems = nritems - mid;
641 right->header.blocknr = right_buffer->blocknr;
642 right->header.flags = node_level(0);
643 data_copy_size = l->items[mid].offset + l->items[mid].size -
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);
652 for (i = 0; i < right->header.nritems; i++)
653 right->items[i].offset += rt_data_off;
655 l->header.nritems = mid;
656 ret = insert_ptr(root, path, &right->items[0].key,
657 right_buffer->blocknr, 1);
659 write_tree_block(root, right_buffer);
660 write_tree_block(root, l_buf);
662 BUG_ON(path->slots[0] != slot);
664 tree_block_release(root, path->nodes[0]);
665 path->nodes[0] = right_buffer;
666 path->slots[0] -= mid;
669 tree_block_release(root, right_buffer);
670 BUG_ON(path->slots[0] < 0);
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.
678 int insert_item(struct ctree_root *root, struct key *key,
679 void *data, int data_size)
685 struct tree_buffer *leaf_buf;
686 unsigned int nritems;
687 unsigned int data_end;
688 struct ctree_path path;
690 /* create a root if there isn't one */
692 struct tree_buffer *t;
693 t = alloc_free_block(root);
695 t->node.header.nritems = 0;
696 t->node.header.flags = node_level(0);
697 t->node.header.blocknr = t->blocknr;
699 write_tree_block(root, t);
702 ret = search_slot(root, key, &path);
704 release_path(root, &path);
708 slot_orig = path.slots[0];
709 leaf_buf = path.nodes[0];
710 leaf = &leaf_buf->leaf;
712 /* make room if needed */
713 if (leaf_free_space(leaf) < sizeof(struct item) + data_size) {
714 split_leaf(root, &path, data_size);
715 leaf_buf = path.nodes[0];
716 leaf = &path.nodes[0]->leaf;
718 nritems = leaf->header.nritems;
719 data_end = leaf_data_end(leaf);
721 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
724 slot = path.slots[0];
727 fixup_low_keys(root, &path, key, 1);
728 if (slot != nritems) {
730 unsigned int old_data = leaf->items[slot].offset +
731 leaf->items[slot].size;
734 * item0..itemN ... dataN.offset..dataN.size .. data0.size
736 /* first correct the data pointers */
737 for (i = slot; i < nritems; i++)
738 leaf->items[i].offset -= data_size;
740 /* shift the items */
741 memmove(leaf->items + slot + 1, leaf->items + slot,
742 (nritems - slot) * sizeof(struct item));
745 memmove(leaf->data + data_end - data_size, leaf->data +
746 data_end, old_data - data_end);
749 /* copy the new data in */
750 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;
755 write_tree_block(root, leaf_buf);
756 if (leaf_free_space(leaf) < 0)
758 release_path(root, &path);
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.
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.
770 int del_ptr(struct ctree_root *root, struct ctree_path *path, int level)
773 struct tree_buffer *t;
778 t = path->nodes[level];
782 slot = path->slots[level];
783 nritems = node->header.nritems;
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));
792 node->header.nritems--;
793 write_tree_block(root, t);
794 if (node->header.nritems != 0) {
797 fixup_low_keys(root, path, node->keys,
799 tslot = path->slots[level+1];
801 push_node_left(root, path, level);
802 if (node->header.nritems) {
803 push_node_right(root, path, level);
805 if (node->header.nritems) {
806 tree_block_release(root, t);
809 tree_block_release(root, t);
810 path->slots[level+1] = tslot;
812 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);
819 if (!path->nodes[level])
826 * delete the item at the leaf level in path. If that empties
827 * the leaf, remove it from the tree
829 int del_item(struct ctree_root *root, struct ctree_path *path)
833 struct tree_buffer *leaf_buf;
837 leaf_buf = path->nodes[0];
838 leaf = &leaf_buf->leaf;
839 slot = path->slots[0];
840 doff = leaf->items[slot].offset;
841 dsize = leaf->items[slot].size;
843 if (slot != leaf->header.nritems - 1) {
845 int data_end = leaf_data_end(leaf);
846 memmove(leaf->data + data_end + dsize,
847 leaf->data + 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));
855 leaf->header.nritems -= 1;
856 /* delete the leaf if we've emptied it */
857 if (leaf->header.nritems == 0) {
858 if (leaf_buf == root->node) {
859 leaf->header.flags = node_level(0);
860 write_tree_block(root, leaf_buf);
862 del_ptr(root, path, 1);
865 fixup_low_keys(root, path, &leaf->items[0].key, 1);
866 write_tree_block(root, leaf_buf);
867 /* delete the leaf if it is mostly empty */
868 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
874 slot = path->slots[1];
876 push_leaf_left(root, path, 1);
877 if (leaf->header.nritems == 0) {
878 path->slots[1] = slot;
879 del_ptr(root, path, 1);
881 tree_block_release(root, leaf_buf);
887 void print_leaf(struct leaf *l)
890 int nr = l->header.nritems;
892 printf("leaf %lu total ptrs %d free space %d\n", l->header.blocknr, nr,
895 for (i = 0 ; i < nr ; i++) {
897 printf("\titem %d key (%lu %u %lu) itemoff %d itemsize %d\n",
899 item->key.objectid, item->key.flags, item->key.offset,
900 item->offset, item->size);
902 printf("\t\titem data %.*s\n", item->size, l->data+item->offset);
906 void print_tree(struct ctree_root *root, struct tree_buffer *t)
915 nr = c->header.nritems;
916 if (c->header.blocknr != t->blocknr)
918 if (is_leaf(c->header.flags)) {
919 print_leaf((struct leaf *)c);
922 printf("node %lu level %d total ptrs %d free spc %lu\n", t->blocknr,
923 node_level(c->header.flags), c->header.nritems,
924 NODEPTRS_PER_BLOCK - c->header.nritems);
926 for (i = 0; i < nr; i++) {
927 printf("\tkey %d (%lu %u %lu) block %lu\n",
929 c->keys[i].objectid, c->keys[i].flags, c->keys[i].offset,
933 for (i = 0; i < nr; i++) {
934 struct tree_buffer *next_buf = read_tree_block(root,
936 struct node *next = &next_buf->node;
937 if (is_leaf(next->header.flags) &&
938 node_level(c->header.flags) != 1)
940 if (node_level(next->header.flags) !=
941 node_level(c->header.flags) - 1)
943 print_tree(root, next_buf);
944 tree_block_release(root, next_buf);
949 /* for testing only */
950 int next_key(int i, int max_key) {
951 return rand() % max_key;
956 struct ctree_root *root;
958 struct key last = { (u64)-1, 0, 0};
963 int run_size = 25000;
964 int max_key = 100000000;
966 struct ctree_path path;
971 root = open_ctree("dbfile");
974 for (i = 0; i < run_size; i++) {
976 num = next_key(i, max_key);
978 sprintf(buf, "string-%d", num);
979 // printf("insert %d\n", num);
983 ret = insert_item(root, &ins, buf, strlen(buf));
988 root = open_ctree("dbfile");
989 printf("starting search\n");
991 for (i = 0; i < run_size; i++) {
992 num = next_key(i, max_key);
995 ret = search_slot(root, &ins, &path);
997 print_tree(root, root->node);
998 printf("unable to find %d\n", num);
1001 release_path(root, &path);
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");
1012 for (i = 0 ; i < run_size/4; i++) {
1013 num = next_key(i, max_key);
1016 ret = search_slot(root, &ins, &path);
1019 ret = del_item(root, &path);
1022 release_path(root, &path);
1026 for (i = 0; i < run_size; i++) {
1028 num = next_key(i, max_key);
1029 sprintf(buf, "string-%d", num);
1031 ret = insert_item(root, &ins, buf, strlen(buf));
1036 root = open_ctree("dbfile");
1037 printf("starting search2\n");
1039 for (i = 0; i < run_size; i++) {
1040 num = next_key(i, max_key);
1043 ret = search_slot(root, &ins, &path);
1045 print_tree(root, root->node);
1046 printf("unable to find %d\n", num);
1049 release_path(root, &path);
1051 printf("starting big long delete run\n");
1052 while(root->node && root->node->node.header.nritems > 0) {
1055 ins.objectid = (u64)-1;
1057 ret = search_slot(root, &ins, &path);
1061 leaf = &path.nodes[0]->leaf;
1062 slot = path.slots[0];
1063 if (slot != leaf->header.nritems)
1065 while(path.slots[0] > 0) {
1067 slot = path.slots[0];
1068 leaf = &path.nodes[0]->leaf;
1070 if (comp_keys(&last, &leaf->items[slot].key) <= 0)
1072 memcpy(&last, &leaf->items[slot].key, sizeof(last));
1073 ret = del_item(root, &path);
1075 printf("del_item returned %d\n", ret);
1080 release_path(root, &path);
1083 printf("tree size is now %d\n", tree_size);