2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
28 /* for testing only */
29 int next_key(int i, int max_key) {
30 return rand() % max_key;
34 int main(int ac, char **av) {
36 struct btrfs_key last = { (u64)-1, 0, 0};
41 int run_size = 100000;
42 int max_key = 100000000;
44 struct btrfs_path path;
45 struct btrfs_super_block super;
46 struct btrfs_root *root;
47 struct btrfs_trans_handle *trans;
54 root = open_ctree(av[1], &super);
55 trans = btrfs_start_transaction(root, 1);
58 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
59 for (i = 0; i < run_size; i++) {
60 num = next_key(i, max_key);
62 sprintf(buf, "string-%d", num);
64 fprintf(stderr, "insert %d:%d\n", num, i);
67 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
70 if (i == run_size - 5) {
71 btrfs_commit_transaction(trans, root, &super);
74 close_ctree(root, &super);
75 root = open_ctree(av[1], &super);
76 printf("starting search\n");
78 for (i = 0; i < run_size; i++) {
79 num = next_key(i, max_key);
81 btrfs_init_path(&path);
83 fprintf(stderr, "search %d:%d\n", num, i);
84 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
86 btrfs_print_tree(root, root->node);
87 printf("unable to find %d\n", num);
90 btrfs_release_path(root, &path);
92 close_ctree(root, &super);
93 root = open_ctree(av[1], &super);
94 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
95 btrfs_header_level(&root->node->node.header),
96 btrfs_header_nritems(&root->node->node.header),
97 BTRFS_NODEPTRS_PER_BLOCK(root) -
98 btrfs_header_nritems(&root->node->node.header));
99 printf("all searches good, deleting some items\n");
102 for (i = 0 ; i < run_size/4; i++) {
103 num = next_key(i, max_key);
105 btrfs_init_path(&path);
106 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
109 fprintf(stderr, "del %d:%d\n", num, i);
110 ret = btrfs_del_item(trans, root, &path);
115 btrfs_release_path(root, &path);
117 close_ctree(root, &super);
118 root = open_ctree(av[1], &super);
120 for (i = 0; i < run_size; i++) {
121 num = next_key(i, max_key);
122 sprintf(buf, "string-%d", num);
125 fprintf(stderr, "insert %d:%d\n", num, i);
126 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
130 close_ctree(root, &super);
131 root = open_ctree(av[1], &super);
133 printf("starting search2\n");
134 for (i = 0; i < run_size; i++) {
135 num = next_key(i, max_key);
137 btrfs_init_path(&path);
139 fprintf(stderr, "search %d:%d\n", num, i);
140 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
142 btrfs_print_tree(root, root->node);
143 printf("unable to find %d\n", num);
146 btrfs_release_path(root, &path);
148 printf("starting big long delete run\n");
150 btrfs_header_nritems(&root->node->node.header) > 0) {
151 struct btrfs_leaf *leaf;
153 ins.objectid = (u64)-1;
154 btrfs_init_path(&path);
155 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
159 leaf = &path.nodes[0]->leaf;
160 slot = path.slots[0];
161 if (slot != btrfs_header_nritems(&leaf->header))
163 while(path.slots[0] > 0) {
165 slot = path.slots[0];
166 leaf = &path.nodes[0]->leaf;
168 btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
169 if (tree_size % 10000 == 0)
170 printf("big del %d:%d\n", tree_size, i);
171 ret = btrfs_del_item(trans, root, &path);
173 printf("del_item returned %d\n", ret);
178 btrfs_release_path(root, &path);
181 printf("previous tree:\n");
182 btrfs_print_tree(root, root->commit_root);
183 printf("map before commit\n");
184 btrfs_print_tree(root->extent_root, root->extent_root->node);
186 btrfs_commit_transaction(trans, root, &super);
187 printf("tree size is now %d\n", tree_size);
188 printf("root %p commit root %p\n", root->node, root->commit_root);
189 printf("map tree\n");
190 btrfs_print_tree(root->fs_info->extent_root,
191 root->fs_info->extent_root->node);
192 close_ctree(root, &super);