transaction handles everywhere
[btrfs-progs-unstable.git] / quick-test.c
blobd676577185d559d53827fe4566fb16fcdc810209
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "kerncompat.h"
4 #include "radix-tree.h"
5 #include "ctree.h"
6 #include "disk-io.h"
7 #include "print-tree.h"
8 #include "transaction.h"
10 /* for testing only */
11 int next_key(int i, int max_key) {
12 return rand() % max_key;
13 // return i;
16 int main(int ac, char **av) {
17 struct btrfs_key ins;
18 struct btrfs_key last = { (u64)-1, 0, 0};
19 char *buf;
20 int i;
21 int num;
22 int ret;
23 int run_size = 100000;
24 int max_key = 100000000;
25 int tree_size = 0;
26 struct btrfs_path path;
27 struct btrfs_super_block super;
28 struct btrfs_root *root;
29 struct btrfs_trans_handle *trans;
31 radix_tree_init();
33 root = open_ctree("dbfile", &super);
34 trans = btrfs_start_transaction(root, 1);
35 srand(55);
36 ins.flags = 0;
37 btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
38 for (i = 0; i < run_size; i++) {
39 buf = malloc(64);
40 num = next_key(i, max_key);
41 // num = i;
42 sprintf(buf, "string-%d", num);
43 if (i % 10000 == 0)
44 fprintf(stderr, "insert %d:%d\n", num, i);
45 ins.objectid = num;
46 ins.offset = 0;
47 ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
48 if (!ret)
49 tree_size++;
50 free(buf);
51 if (i == run_size - 5) {
52 btrfs_commit_transaction(trans, root, &super);
56 close_ctree(root, &super);
58 root = open_ctree("dbfile", &super);
59 printf("starting search\n");
60 srand(55);
61 for (i = 0; i < run_size; i++) {
62 num = next_key(i, max_key);
63 ins.objectid = num;
64 btrfs_init_path(&path);
65 if (i % 10000 == 0)
66 fprintf(stderr, "search %d:%d\n", num, i);
67 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
68 if (ret) {
69 btrfs_print_tree(root, root->node);
70 printf("unable to find %d\n", num);
71 exit(1);
73 btrfs_release_path(root, &path);
75 close_ctree(root, &super);
76 root = open_ctree("dbfile", &super);
77 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
78 btrfs_header_level(&root->node->node.header),
79 btrfs_header_nritems(&root->node->node.header),
80 BTRFS_NODEPTRS_PER_BLOCK(root) -
81 btrfs_header_nritems(&root->node->node.header));
82 printf("all searches good, deleting some items\n");
83 i = 0;
84 srand(55);
85 for (i = 0 ; i < run_size/4; i++) {
86 num = next_key(i, max_key);
87 ins.objectid = num;
88 btrfs_init_path(&path);
89 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
90 if (!ret) {
91 if (i % 10000 == 0)
92 fprintf(stderr, "del %d:%d\n", num, i);
93 ret = btrfs_del_item(trans, root, &path);
94 if (ret != 0)
95 BUG();
96 tree_size--;
98 btrfs_release_path(root, &path);
100 close_ctree(root, &super);
101 root = open_ctree("dbfile", &super);
102 srand(128);
103 for (i = 0; i < run_size; i++) {
104 buf = malloc(64);
105 num = next_key(i, max_key);
106 sprintf(buf, "string-%d", num);
107 ins.objectid = num;
108 if (i % 10000 == 0)
109 fprintf(stderr, "insert %d:%d\n", num, i);
110 ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
111 if (!ret)
112 tree_size++;
113 free(buf);
115 close_ctree(root, &super);
116 root = open_ctree("dbfile", &super);
117 srand(128);
118 printf("starting search2\n");
119 for (i = 0; i < run_size; i++) {
120 num = next_key(i, max_key);
121 ins.objectid = num;
122 btrfs_init_path(&path);
123 if (i % 10000 == 0)
124 fprintf(stderr, "search %d:%d\n", num, i);
125 ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
126 if (ret) {
127 btrfs_print_tree(root, root->node);
128 printf("unable to find %d\n", num);
129 exit(1);
131 btrfs_release_path(root, &path);
133 printf("starting big long delete run\n");
134 while(root->node &&
135 btrfs_header_nritems(&root->node->node.header) > 0) {
136 struct btrfs_leaf *leaf;
137 int slot;
138 ins.objectid = (u64)-1;
139 btrfs_init_path(&path);
140 ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
141 if (ret == 0)
142 BUG();
144 leaf = &path.nodes[0]->leaf;
145 slot = path.slots[0];
146 if (slot != btrfs_header_nritems(&leaf->header))
147 BUG();
148 while(path.slots[0] > 0) {
149 path.slots[0] -= 1;
150 slot = path.slots[0];
151 leaf = &path.nodes[0]->leaf;
153 btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
154 if (tree_size % 10000 == 0)
155 printf("big del %d:%d\n", tree_size, i);
156 ret = btrfs_del_item(trans, root, &path);
157 if (ret != 0) {
158 printf("del_item returned %d\n", ret);
159 BUG();
161 tree_size--;
163 btrfs_release_path(root, &path);
166 printf("previous tree:\n");
167 btrfs_print_tree(root, root->commit_root);
168 printf("map before commit\n");
169 btrfs_print_tree(root->extent_root, root->extent_root->node);
171 btrfs_commit_transaction(trans, root, &super);
172 printf("tree size is now %d\n", tree_size);
173 printf("root %p commit root %p\n", root->node, root->commit_root);
174 printf("map tree\n");
175 btrfs_print_tree(root->extent_root, root->extent_root->node);
176 close_ctree(root, &super);
177 return 0;