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
;
54 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
, OPEN_CTREE_WRITES
);
56 fprintf(stderr
, "Open ctree failed\n");
59 trans
= btrfs_start_transaction(root
, 1);
61 btrfs_set_key_type(&ins
, BTRFS_STRING_ITEM_KEY
);
62 for (i
= 0; i
< run_size
; i
++) {
63 num
= next_key(i
, max_key
);
65 sprintf(buf
, "string-%d", num
);
67 fprintf(stderr
, "insert %d:%d\n", num
, i
);
70 ret
= btrfs_insert_item(trans
, root
, &ins
, buf
, 512);
73 if (i
== run_size
- 5) {
74 btrfs_commit_transaction(trans
, root
);
75 trans
= btrfs_start_transaction(root
, 1);
78 btrfs_commit_transaction(trans
, root
);
81 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
, OPEN_CTREE_WRITES
);
83 fprintf(stderr
, "Open ctree failed\n");
86 printf("starting search\n");
88 for (i
= 0; i
< run_size
; i
++) {
89 num
= next_key(i
, max_key
);
91 btrfs_init_path(&path
);
93 fprintf(stderr
, "search %d:%d\n", num
, i
);
94 ret
= btrfs_search_slot(NULL
, root
, &ins
, &path
, 0, 0);
96 btrfs_print_tree(root
, root
->node
, 1);
97 printf("unable to find %d\n", num
);
100 btrfs_release_path(&path
);
104 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
, OPEN_CTREE_WRITES
);
106 fprintf(stderr
, "Open ctree failed\n");
109 printf("node %p level %d total ptrs %d free spc %lu\n", root
->node
,
110 btrfs_header_level(root
->node
),
111 btrfs_header_nritems(root
->node
),
112 (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root
) -
113 btrfs_header_nritems(root
->node
));
114 printf("all searches good, deleting some items\n");
117 trans
= btrfs_start_transaction(root
, 1);
118 for (i
= 0 ; i
< run_size
/4; i
++) {
119 num
= next_key(i
, max_key
);
121 btrfs_init_path(&path
);
122 ret
= btrfs_search_slot(trans
, root
, &ins
, &path
, -1, 1);
125 fprintf(stderr
, "del %d:%d\n", num
, i
);
126 ret
= btrfs_del_item(trans
, root
, &path
);
131 btrfs_release_path(&path
);
133 btrfs_commit_transaction(trans
, root
);
136 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
, OPEN_CTREE_WRITES
);
138 fprintf(stderr
, "Open ctree failed\n");
141 trans
= btrfs_start_transaction(root
, 1);
143 for (i
= 0; i
< run_size
; i
++) {
144 num
= next_key(i
, max_key
);
145 sprintf(buf
, "string-%d", num
);
148 fprintf(stderr
, "insert %d:%d\n", num
, i
);
149 ret
= btrfs_insert_item(trans
, root
, &ins
, buf
, 512);
153 btrfs_commit_transaction(trans
, root
);
156 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
, OPEN_CTREE_WRITES
);
158 fprintf(stderr
, "Open ctree failed\n");
162 printf("starting search2\n");
163 for (i
= 0; i
< run_size
; i
++) {
164 num
= next_key(i
, max_key
);
166 btrfs_init_path(&path
);
168 fprintf(stderr
, "search %d:%d\n", num
, i
);
169 ret
= btrfs_search_slot(NULL
, root
, &ins
, &path
, 0, 0);
171 btrfs_print_tree(root
, root
->node
, 1);
172 printf("unable to find %d\n", num
);
175 btrfs_release_path(&path
);
177 printf("starting big long delete run\n");
178 trans
= btrfs_start_transaction(root
, 1);
179 while(root
->node
&& btrfs_header_nritems(root
->node
) > 0) {
180 struct extent_buffer
*leaf
;
182 ins
.objectid
= (u64
)-1;
183 btrfs_init_path(&path
);
184 ret
= btrfs_search_slot(trans
, root
, &ins
, &path
, -1, 1);
188 leaf
= path
.nodes
[0];
189 slot
= path
.slots
[0];
190 if (slot
!= btrfs_header_nritems(leaf
))
192 while(path
.slots
[0] > 0) {
194 slot
= path
.slots
[0];
195 leaf
= path
.nodes
[0];
197 btrfs_item_key_to_cpu(leaf
, &last
, slot
);
199 if (tree_size
% 10000 == 0)
200 printf("big del %d:%d\n", tree_size
, i
);
201 ret
= btrfs_del_item(trans
, root
, &path
);
203 printf("del_item returned %d\n", ret
);
208 btrfs_release_path(&path
);
211 printf("previous tree:\n");
212 btrfs_print_tree(root, root->commit_root);
213 printf("map before commit\n");
214 btrfs_print_tree(root->extent_root, root->extent_root->node);
216 btrfs_commit_transaction(trans
, root
);
217 printf("tree size is now %d\n", tree_size
);
218 printf("root %p commit root %p\n", root
->node
, root
->commit_root
);
219 btrfs_print_tree(root
, root
->node
, 1);