Fixup the code to merge during path walks
[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 key ins;
17         struct 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 ctree_path path;
26         struct ctree_super_block super;
27         struct ctree_root *root;
28
29         radix_tree_init();
30
31         root = open_ctree("dbfile", &super);
32         srand(55);
33         for (i = 0; i < run_size; i++) {
34                 buf = malloc(64);
35                 num = next_key(i, max_key);
36                 // num = i;
37                 sprintf(buf, "string-%d", num);
38                 if (i % 10000 == 0)
39                         fprintf(stderr, "insert %d:%d\n", num, i);
40                 ins.objectid = num;
41                 ins.offset = 0;
42                 ins.flags = 0;
43                 ret = insert_item(root, &ins, buf, strlen(buf));
44                 if (!ret)
45                         tree_size++;
46                 free(buf);
47         }
48         write_ctree_super(root, &super);
49         close_ctree(root);
50
51         root = open_ctree("dbfile", &super);
52         printf("starting search\n");
53         srand(55);
54         for (i = 0; i < run_size; i++) {
55                 num = next_key(i, max_key);
56                 ins.objectid = num;
57                 init_path(&path);
58                 if (i % 10000 == 0)
59                         fprintf(stderr, "search %d:%d\n", num, i);
60                 ret = search_slot(root, &ins, &path, 0);
61                 if (ret) {
62                         print_tree(root, root->node);
63                         printf("unable to find %d\n", num);
64                         exit(1);
65                 }
66                 release_path(root, &path);
67         }
68         write_ctree_super(root, &super);
69         close_ctree(root);
70         root = open_ctree("dbfile", &super);
71         printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
72                 node_level(root->node->node.header.flags),
73                 root->node->node.header.nritems,
74                 NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
75         printf("all searches good, deleting some items\n");
76         i = 0;
77         srand(55);
78         for (i = 0 ; i < run_size/4; i++) {
79                 num = next_key(i, max_key);
80                 ins.objectid = num;
81                 init_path(&path);
82                 ret = search_slot(root, &ins, &path, -1);
83                 if (!ret) {
84                         if (i % 10000 == 0)
85                                 fprintf(stderr, "del %d:%d\n", num, i);
86                         ret = del_item(root, &path);
87                         if (ret != 0)
88                                 BUG();
89                         tree_size--;
90                 }
91                 release_path(root, &path);
92         }
93         write_ctree_super(root, &super);
94         close_ctree(root);
95         root = open_ctree("dbfile", &super);
96         srand(128);
97         for (i = 0; i < run_size; i++) {
98                 buf = malloc(64);
99                 num = next_key(i, max_key);
100                 sprintf(buf, "string-%d", num);
101                 ins.objectid = num;
102                 if (i % 10000 == 0)
103                         fprintf(stderr, "insert %d:%d\n", num, i);
104                 ret = insert_item(root, &ins, buf, strlen(buf));
105                 if (!ret)
106                         tree_size++;
107                 free(buf);
108         }
109         write_ctree_super(root, &super);
110         close_ctree(root);
111         root = open_ctree("dbfile", &super);
112         srand(128);
113         printf("starting search2\n");
114         for (i = 0; i < run_size; i++) {
115                 num = next_key(i, max_key);
116                 ins.objectid = num;
117                 init_path(&path);
118                 if (i % 10000 == 0)
119                         fprintf(stderr, "search %d:%d\n", num, i);
120                 ret = search_slot(root, &ins, &path, 0);
121                 if (ret) {
122                         print_tree(root, root->node);
123                         printf("unable to find %d\n", num);
124                         exit(1);
125                 }
126                 release_path(root, &path);
127         }
128         printf("starting big long delete run\n");
129         while(root->node && root->node->node.header.nritems > 0) {
130                 struct leaf *leaf;
131                 int slot;
132                 ins.objectid = (u64)-1;
133                 init_path(&path);
134                 ret = search_slot(root, &ins, &path, -1);
135                 if (ret == 0)
136                         BUG();
137
138                 leaf = &path.nodes[0]->leaf;
139                 slot = path.slots[0];
140                 if (slot != leaf->header.nritems)
141                         BUG();
142                 while(path.slots[0] > 0) {
143                         path.slots[0] -= 1;
144                         slot = path.slots[0];
145                         leaf = &path.nodes[0]->leaf;
146
147                         memcpy(&last, &leaf->items[slot].key, sizeof(last));
148                         if (tree_size % 10000 == 0)
149                                 printf("big del %d:%d\n", tree_size, i);
150                         ret = del_item(root, &path);
151                         if (ret != 0) {
152                                 printf("del_item returned %d\n", ret);
153                                 BUG();
154                         }
155                         tree_size--;
156                 }
157                 release_path(root, &path);
158         }
159         printf("tree size is now %d\n", tree_size);
160         printf("map tree\n");
161         print_tree(root->extent_root, root->extent_root->node);
162         write_ctree_super(root, &super);
163         close_ctree(root);
164         return 0;
165 }