recursion free-first pass
[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                 if (i == run_size - 5) {
48                         commit_transaction(root, &super);
49                 }
50
51         }
52         close_ctree(root, &super);
53
54         root = open_ctree("dbfile", &super);
55         printf("starting search\n");
56         srand(55);
57         for (i = 0; i < run_size; i++) {
58                 num = next_key(i, max_key);
59                 ins.objectid = num;
60                 init_path(&path);
61                 if (i % 10000 == 0)
62                         fprintf(stderr, "search %d:%d\n", num, i);
63                 ret = search_slot(root, &ins, &path, 0, 0);
64                 if (ret) {
65                         print_tree(root, root->node);
66                         printf("unable to find %d\n", num);
67                         exit(1);
68                 }
69                 release_path(root, &path);
70         }
71         close_ctree(root, &super);
72         root = open_ctree("dbfile", &super);
73         printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
74                 node_level(root->node->node.header.flags),
75                 root->node->node.header.nritems,
76                 NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
77         printf("all searches good, deleting some items\n");
78         i = 0;
79         srand(55);
80         for (i = 0 ; i < run_size/4; i++) {
81                 num = next_key(i, max_key);
82                 ins.objectid = num;
83                 init_path(&path);
84                 ret = search_slot(root, &ins, &path, -1, 1);
85                 if (!ret) {
86                         if (i % 10000 == 0)
87                                 fprintf(stderr, "del %d:%d\n", num, i);
88                         ret = del_item(root, &path);
89                         if (ret != 0)
90                                 BUG();
91                         tree_size--;
92                 }
93                 release_path(root, &path);
94         }
95         close_ctree(root, &super);
96         root = open_ctree("dbfile", &super);
97         srand(128);
98         for (i = 0; i < run_size; i++) {
99                 buf = malloc(64);
100                 num = next_key(i, max_key);
101                 sprintf(buf, "string-%d", num);
102                 ins.objectid = num;
103                 if (i % 10000 == 0)
104                         fprintf(stderr, "insert %d:%d\n", num, i);
105                 ret = insert_item(root, &ins, buf, strlen(buf));
106                 if (!ret)
107                         tree_size++;
108                 free(buf);
109         }
110         close_ctree(root, &super);
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, 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, 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         /*
160         printf("previous tree:\n");
161         print_tree(root, root->commit_root);
162         printf("map before commit\n");
163         print_tree(root->extent_root, root->extent_root->node);
164         */
165         commit_transaction(root, &super);
166         printf("tree size is now %d\n", tree_size);
167         printf("root %p commit root %p\n", root->node, root->commit_root);
168         printf("map tree\n");
169         print_tree(root->extent_root, root->extent_root->node);
170         close_ctree(root, &super);
171         return 0;
172 }