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.
22 #include "kerncompat.h"
23 #include "radix-tree.h"
26 #include "print-tree.h"
27 #include "transaction.h"
29 /* for testing only */
30 int next_key(int i, int max_key) {
31 return rand() % max_key;
35 int main(int ac, char **av) {
37 struct btrfs_key last = { (u64)-1, 0, 0};
42 int run_size = 300000;
43 int max_key = 100000000;
45 struct btrfs_path path;
46 struct btrfs_root *root;
47 struct btrfs_trans_handle *trans;
54 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
55 trans = btrfs_start_transaction(root, 1);
57 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
58 for (i = 0; i < run_size; i++) {
59 num = next_key(i, max_key);
61 sprintf(buf, "string-%d", num);
63 fprintf(stderr, "insert %d:%d\n", num, i);
66 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
69 if (i == run_size - 5) {
70 btrfs_commit_transaction(trans, root);
71 trans = btrfs_start_transaction(root, 1);
74 btrfs_commit_transaction(trans, root);
77 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
78 printf("starting search\n");
80 for (i = 0; i < run_size; i++) {
81 num = next_key(i, max_key);
83 btrfs_init_path(&path);
85 fprintf(stderr, "search %d:%d\n", num, i);
86 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
88 btrfs_print_tree(root, root->node, 1);
89 printf("unable to find %d\n", num);
92 btrfs_release_path(root, &path);
96 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
97 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
98 btrfs_header_level(root->node),
99 btrfs_header_nritems(root->node),
100 (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root) -
101 btrfs_header_nritems(root->node));
102 printf("all searches good, deleting some items\n");
105 trans = btrfs_start_transaction(root, 1);
106 for (i = 0 ; i < run_size/4; i++) {
107 num = next_key(i, max_key);
109 btrfs_init_path(&path);
110 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
113 fprintf(stderr, "del %d:%d\n", num, i);
114 ret = btrfs_del_item(trans, root, &path);
119 btrfs_release_path(root, &path);
121 btrfs_commit_transaction(trans, root);
124 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
125 trans = btrfs_start_transaction(root, 1);
127 for (i = 0; i < run_size; i++) {
128 num = next_key(i, max_key);
129 sprintf(buf, "string-%d", num);
132 fprintf(stderr, "insert %d:%d\n", num, i);
133 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
137 btrfs_commit_transaction(trans, root);
140 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
142 printf("starting search2\n");
143 for (i = 0; i < run_size; i++) {
144 num = next_key(i, max_key);
146 btrfs_init_path(&path);
148 fprintf(stderr, "search %d:%d\n", num, i);
149 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
151 btrfs_print_tree(root, root->node, 1);
152 printf("unable to find %d\n", num);
155 btrfs_release_path(root, &path);
157 printf("starting big long delete run\n");
158 trans = btrfs_start_transaction(root, 1);
159 while(root->node && btrfs_header_nritems(root->node) > 0) {
160 struct extent_buffer *leaf;
162 ins.objectid = (u64)-1;
163 btrfs_init_path(&path);
164 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
168 leaf = path.nodes[0];
169 slot = path.slots[0];
170 if (slot != btrfs_header_nritems(leaf))
172 while(path.slots[0] > 0) {
174 slot = path.slots[0];
175 leaf = path.nodes[0];
177 btrfs_item_key_to_cpu(leaf, &last, slot);
179 if (tree_size % 10000 == 0)
180 printf("big del %d:%d\n", tree_size, i);
181 ret = btrfs_del_item(trans, root, &path);
183 printf("del_item returned %d\n", ret);
188 btrfs_release_path(root, &path);
191 printf("previous tree:\n");
192 btrfs_print_tree(root, root->commit_root);
193 printf("map before commit\n");
194 btrfs_print_tree(root->extent_root, root->extent_root->node);
196 btrfs_commit_transaction(trans, root);
197 printf("tree size is now %d\n", tree_size);
198 printf("root %p commit root %p\n", root->node, root->commit_root);
199 btrfs_print_tree(root, root->node, 1);