3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
8 #include "transaction.h"
10 /* for testing only */
11 int next_key(int i, int max_key) {
12 return rand() % max_key;
16 int main(int ac, char **av) {
18 struct btrfs_key last = { (u64)-1, 0, 0};
23 int run_size = 100000;
24 int max_key = 100000000;
26 struct btrfs_path path;
27 struct btrfs_super_block super;
28 struct btrfs_root *root;
29 struct btrfs_trans_handle *trans;
36 root = open_ctree(av[1], &super);
37 trans = btrfs_start_transaction(root, 1);
40 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
41 for (i = 0; i < run_size; i++) {
42 num = next_key(i, max_key);
44 sprintf(buf, "string-%d", num);
46 fprintf(stderr, "insert %d:%d\n", num, i);
49 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
52 if (i == run_size - 5) {
53 btrfs_commit_transaction(trans, root, &super);
56 close_ctree(root, &super);
57 root = open_ctree(av[1], &super);
58 printf("starting search\n");
60 for (i = 0; i < run_size; i++) {
61 num = next_key(i, max_key);
63 btrfs_init_path(&path);
65 fprintf(stderr, "search %d:%d\n", num, i);
66 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
68 btrfs_print_tree(root, root->node);
69 printf("unable to find %d\n", num);
72 btrfs_release_path(root, &path);
74 close_ctree(root, &super);
75 root = open_ctree(av[1], &super);
76 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
77 btrfs_header_level(&root->node->node.header),
78 btrfs_header_nritems(&root->node->node.header),
79 BTRFS_NODEPTRS_PER_BLOCK(root) -
80 btrfs_header_nritems(&root->node->node.header));
81 printf("all searches good, deleting some items\n");
84 for (i = 0 ; i < run_size/4; i++) {
85 num = next_key(i, max_key);
87 btrfs_init_path(&path);
88 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
91 fprintf(stderr, "del %d:%d\n", num, i);
92 ret = btrfs_del_item(trans, root, &path);
97 btrfs_release_path(root, &path);
99 close_ctree(root, &super);
100 root = open_ctree(av[1], &super);
102 for (i = 0; i < run_size; i++) {
103 num = next_key(i, max_key);
104 sprintf(buf, "string-%d", num);
107 fprintf(stderr, "insert %d:%d\n", num, i);
108 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
112 close_ctree(root, &super);
113 root = open_ctree(av[1], &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(trans, 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(trans, 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(trans, 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(trans, 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->fs_info->extent_root,
173 root->fs_info->extent_root->node);
174 close_ctree(root, &super);