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 static 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;
53 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
55 fprintf(stderr, "Open ctree failed\n");
58 trans = btrfs_start_transaction(root, 1);
60 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
61 for (i = 0; i < run_size; i++) {
62 num = next_key(i, max_key);
64 sprintf(buf, "string-%d", num);
66 fprintf(stderr, "insert %d:%d\n", num, i);
69 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
72 if (i == run_size - 5) {
73 btrfs_commit_transaction(trans, root);
74 trans = btrfs_start_transaction(root, 1);
77 btrfs_commit_transaction(trans, root);
80 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
82 fprintf(stderr, "Open ctree failed\n");
85 printf("starting search\n");
87 for (i = 0; i < run_size; i++) {
88 num = next_key(i, max_key);
90 btrfs_init_path(&path);
92 fprintf(stderr, "search %d:%d\n", num, i);
93 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
95 btrfs_print_tree(root, root->node, 1);
96 printf("unable to find %d\n", num);
99 btrfs_release_path(&path);
103 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
105 fprintf(stderr, "Open ctree failed\n");
108 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
109 btrfs_header_level(root->node),
110 btrfs_header_nritems(root->node),
111 (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root) -
112 btrfs_header_nritems(root->node));
113 printf("all searches good, deleting some items\n");
116 trans = btrfs_start_transaction(root, 1);
117 for (i = 0 ; i < run_size/4; i++) {
118 num = next_key(i, max_key);
120 btrfs_init_path(&path);
121 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
124 fprintf(stderr, "del %d:%d\n", num, i);
125 ret = btrfs_del_item(trans, root, &path);
130 btrfs_release_path(&path);
132 btrfs_commit_transaction(trans, root);
135 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
137 fprintf(stderr, "Open ctree failed\n");
140 trans = btrfs_start_transaction(root, 1);
142 for (i = 0; i < run_size; i++) {
143 num = next_key(i, max_key);
144 sprintf(buf, "string-%d", num);
147 fprintf(stderr, "insert %d:%d\n", num, i);
148 ret = btrfs_insert_item(trans, root, &ins, buf, 512);
152 btrfs_commit_transaction(trans, root);
155 root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
157 fprintf(stderr, "Open ctree failed\n");
161 printf("starting search2\n");
162 for (i = 0; i < run_size; i++) {
163 num = next_key(i, max_key);
165 btrfs_init_path(&path);
167 fprintf(stderr, "search %d:%d\n", num, i);
168 ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0);
170 btrfs_print_tree(root, root->node, 1);
171 printf("unable to find %d\n", num);
174 btrfs_release_path(&path);
176 printf("starting big long delete run\n");
177 trans = btrfs_start_transaction(root, 1);
178 while(root->node && btrfs_header_nritems(root->node) > 0) {
179 struct extent_buffer *leaf;
181 ins.objectid = (u64)-1;
182 btrfs_init_path(&path);
183 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
187 leaf = path.nodes[0];
188 slot = path.slots[0];
189 if (slot != btrfs_header_nritems(leaf))
191 while(path.slots[0] > 0) {
193 slot = path.slots[0];
194 leaf = path.nodes[0];
196 btrfs_item_key_to_cpu(leaf, &last, slot);
198 if (tree_size % 10000 == 0)
199 printf("big del %d:%d\n", tree_size, i);
200 ret = btrfs_del_item(trans, root, &path);
202 printf("del_item returned %d\n", ret);
207 btrfs_release_path(&path);
210 printf("previous tree:\n");
211 btrfs_print_tree(root, root->commit_root);
212 printf("map before commit\n");
213 btrfs_print_tree(root->extent_root, root->extent_root->node);
215 btrfs_commit_transaction(trans, root);
216 printf("tree size is now %d\n", tree_size);
217 printf("root %p commit root %p\n", root->node, root->commit_root);
218 btrfs_print_tree(root, root->node, 1);