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
= 300000;
42 int max_key
= 100000000;
44 struct btrfs_path path
;
45 struct btrfs_root
*root
;
46 struct btrfs_trans_handle
*trans
;
53 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
);
54 trans
= btrfs_start_transaction(root
, 1);
56 btrfs_set_key_type(&ins
, BTRFS_STRING_ITEM_KEY
);
57 for (i
= 0; i
< run_size
; i
++) {
58 num
= next_key(i
, max_key
);
60 sprintf(buf
, "string-%d", num
);
62 fprintf(stderr
, "insert %d:%d\n", num
, i
);
65 ret
= btrfs_insert_item(trans
, root
, &ins
, buf
, 512);
68 if (i
== run_size
- 5) {
69 btrfs_commit_transaction(trans
, root
);
70 trans
= btrfs_start_transaction(root
, 1);
73 btrfs_commit_transaction(trans
, root
);
76 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
);
77 printf("starting search\n");
79 for (i
= 0; i
< run_size
; i
++) {
80 num
= next_key(i
, max_key
);
82 btrfs_init_path(&path
);
84 fprintf(stderr
, "search %d:%d\n", num
, i
);
85 ret
= btrfs_search_slot(NULL
, root
, &ins
, &path
, 0, 0);
87 btrfs_print_tree(root
, root
->node
);
88 printf("unable to find %d\n", num
);
91 btrfs_release_path(root
, &path
);
95 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
);
96 printf("node %p level %d total ptrs %d free spc %lu\n", root
->node
,
97 btrfs_header_level(root
->node
),
98 btrfs_header_nritems(root
->node
),
99 BTRFS_NODEPTRS_PER_BLOCK(root
) -
100 btrfs_header_nritems(root
->node
));
101 printf("all searches good, deleting some items\n");
104 trans
= btrfs_start_transaction(root
, 1);
105 for (i
= 0 ; i
< run_size
/4; i
++) {
106 num
= next_key(i
, max_key
);
108 btrfs_init_path(&path
);
109 ret
= btrfs_search_slot(trans
, root
, &ins
, &path
, -1, 1);
112 fprintf(stderr
, "del %d:%d\n", num
, i
);
113 ret
= btrfs_del_item(trans
, root
, &path
);
118 btrfs_release_path(root
, &path
);
120 btrfs_commit_transaction(trans
, root
);
123 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
);
124 trans
= btrfs_start_transaction(root
, 1);
126 for (i
= 0; i
< run_size
; i
++) {
127 num
= next_key(i
, max_key
);
128 sprintf(buf
, "string-%d", num
);
131 fprintf(stderr
, "insert %d:%d\n", num
, i
);
132 ret
= btrfs_insert_item(trans
, root
, &ins
, buf
, 512);
136 btrfs_commit_transaction(trans
, root
);
139 root
= open_ctree(av
[1], BTRFS_SUPER_INFO_OFFSET
);
141 printf("starting search2\n");
142 for (i
= 0; i
< run_size
; i
++) {
143 num
= next_key(i
, max_key
);
145 btrfs_init_path(&path
);
147 fprintf(stderr
, "search %d:%d\n", num
, i
);
148 ret
= btrfs_search_slot(NULL
, root
, &ins
, &path
, 0, 0);
150 btrfs_print_tree(root
, root
->node
);
151 printf("unable to find %d\n", num
);
154 btrfs_release_path(root
, &path
);
156 printf("starting big long delete run\n");
157 trans
= btrfs_start_transaction(root
, 1);
158 while(root
->node
&& btrfs_header_nritems(root
->node
) > 0) {
159 struct extent_buffer
*leaf
;
161 ins
.objectid
= (u64
)-1;
162 btrfs_init_path(&path
);
163 ret
= btrfs_search_slot(trans
, root
, &ins
, &path
, -1, 1);
167 leaf
= path
.nodes
[0];
168 slot
= path
.slots
[0];
169 if (slot
!= btrfs_header_nritems(leaf
))
171 while(path
.slots
[0] > 0) {
173 slot
= path
.slots
[0];
174 leaf
= path
.nodes
[0];
176 btrfs_item_key_to_cpu(leaf
, &last
, slot
);
178 if (tree_size
% 10000 == 0)
179 printf("big del %d:%d\n", tree_size
, i
);
180 ret
= btrfs_del_item(trans
, root
, &path
);
182 printf("del_item returned %d\n", ret
);
187 btrfs_release_path(root
, &path
);
190 printf("previous tree:\n");
191 btrfs_print_tree(root, root->commit_root);
192 printf("map before commit\n");
193 btrfs_print_tree(root->extent_root, root->extent_root->node);
195 btrfs_commit_transaction(trans
, root
);
196 printf("tree size is now %d\n", tree_size
);
197 printf("root %p commit root %p\n", root
->node
, root
->commit_root
);
198 btrfs_print_tree(root
, root
->node
);