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