3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
10 int next_key(int i, int max_key) {
11 return rand() % max_key;
15 int main(int ac, char **av) {
17 struct btrfs_key last = { (u64)-1, 0, 0};
22 int run_size = 100000;
23 int max_key = 100000000;
25 struct btrfs_path path;
26 struct btrfs_super_block super;
27 struct btrfs_root *root;
31 root = open_ctree("dbfile", &super);
34 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
35 for (i = 0; i < run_size; i++) {
37 num = next_key(i, max_key);
39 sprintf(buf, "string-%d", num);
41 fprintf(stderr, "insert %d:%d\n", num, i);
44 ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
48 if (i == run_size - 5) {
49 btrfs_commit_transaction(root, &super);
53 close_ctree(root, &super);
55 root = open_ctree("dbfile", &super);
56 printf("starting search\n");
58 for (i = 0; i < run_size; i++) {
59 num = next_key(i, max_key);
61 btrfs_init_path(&path);
63 fprintf(stderr, "search %d:%d\n", num, i);
64 ret = btrfs_search_slot(root, &ins, &path, 0, 0);
66 btrfs_print_tree(root, root->node);
67 printf("unable to find %d\n", num);
70 btrfs_release_path(root, &path);
72 close_ctree(root, &super);
73 root = open_ctree("dbfile", &super);
74 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
75 btrfs_header_level(&root->node->node.header),
76 btrfs_header_nritems(&root->node->node.header),
77 BTRFS_NODEPTRS_PER_BLOCK(root) -
78 btrfs_header_nritems(&root->node->node.header));
79 printf("all searches good, deleting some items\n");
82 for (i = 0 ; i < run_size/4; i++) {
83 num = next_key(i, max_key);
85 btrfs_init_path(&path);
86 ret = btrfs_search_slot(root, &ins, &path, -1, 1);
89 fprintf(stderr, "del %d:%d\n", num, i);
90 ret = btrfs_del_item(root, &path);
95 btrfs_release_path(root, &path);
97 close_ctree(root, &super);
98 root = open_ctree("dbfile", &super);
100 for (i = 0; i < run_size; i++) {
102 num = next_key(i, max_key);
103 sprintf(buf, "string-%d", num);
106 fprintf(stderr, "insert %d:%d\n", num, i);
107 ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
112 close_ctree(root, &super);
113 root = open_ctree("dbfile", &super);
115 printf("starting search2\n");
116 for (i = 0; i < run_size; i++) {
117 num = next_key(i, max_key);
119 btrfs_init_path(&path);
121 fprintf(stderr, "search %d:%d\n", num, i);
122 ret = btrfs_search_slot(root, &ins, &path, 0, 0);
124 btrfs_print_tree(root, root->node);
125 printf("unable to find %d\n", num);
128 btrfs_release_path(root, &path);
130 printf("starting big long delete run\n");
132 btrfs_header_nritems(&root->node->node.header) > 0) {
133 struct btrfs_leaf *leaf;
135 ins.objectid = (u64)-1;
136 btrfs_init_path(&path);
137 ret = btrfs_search_slot(root, &ins, &path, -1, 1);
141 leaf = &path.nodes[0]->leaf;
142 slot = path.slots[0];
143 if (slot != btrfs_header_nritems(&leaf->header))
145 while(path.slots[0] > 0) {
147 slot = path.slots[0];
148 leaf = &path.nodes[0]->leaf;
150 btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
151 if (tree_size % 10000 == 0)
152 printf("big del %d:%d\n", tree_size, i);
153 ret = btrfs_del_item(root, &path);
155 printf("del_item returned %d\n", ret);
160 btrfs_release_path(root, &path);
163 printf("previous tree:\n");
164 btrfs_print_tree(root, root->commit_root);
165 printf("map before commit\n");
166 btrfs_print_tree(root->extent_root, root->extent_root->node);
168 btrfs_commit_transaction(root, &super);
169 printf("tree size is now %d\n", tree_size);
170 printf("root %p commit root %p\n", root->node, root->commit_root);
171 printf("map tree\n");
172 btrfs_print_tree(root->extent_root, root->extent_root->node);
173 close_ctree(root, &super);