rename funcs and structs to btrfs
[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         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 = btrfs_insert_item(root, &ins, buf, strlen(buf));
44                 if (!ret)
45                         tree_size++;
46                 free(buf);
47                 if (i == run_size - 5) {
48                         btrfs_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                 btrfs_init_path(&path);
61                 if (i % 10000 == 0)
62                         fprintf(stderr, "search %d:%d\n", num, i);
63                 ret = btrfs_search_slot(root, &ins, &path, 0, 0);
64                 if (ret) {
65                         btrfs_print_tree(root, root->node);
66                         printf("unable to find %d\n", num);
67                         exit(1);
68                 }
69                 btrfs_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                 btrfs_header_level(&root->node->node.header),
75                 btrfs_header_nritems(&root->node->node.header),
76                 NODEPTRS_PER_BLOCK -
77                 btrfs_header_nritems(&root->node->node.header));
78         printf("all searches good, deleting some items\n");
79         i = 0;
80         srand(55);
81         for (i = 0 ; i < run_size/4; i++) {
82                 num = next_key(i, max_key);
83                 ins.objectid = num;
84                 btrfs_init_path(&path);
85                 ret = btrfs_search_slot(root, &ins, &path, -1, 1);
86                 if (!ret) {
87                         if (i % 10000 == 0)
88                                 fprintf(stderr, "del %d:%d\n", num, i);
89                         ret = btrfs_del_item(root, &path);
90                         if (ret != 0)
91                                 BUG();
92                         tree_size--;
93                 }
94                 btrfs_release_path(root, &path);
95         }
96         close_ctree(root, &super);
97         root = open_ctree("dbfile", &super);
98         srand(128);
99         for (i = 0; i < run_size; i++) {
100                 buf = malloc(64);
101                 num = next_key(i, max_key);
102                 sprintf(buf, "string-%d", num);
103                 ins.objectid = num;
104                 if (i % 10000 == 0)
105                         fprintf(stderr, "insert %d:%d\n", num, i);
106                 ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
107                 if (!ret)
108                         tree_size++;
109                 free(buf);
110         }
111         close_ctree(root, &super);
112         root = open_ctree("dbfile", &super);
113         srand(128);
114         printf("starting search2\n");
115         for (i = 0; i < run_size; i++) {
116                 num = next_key(i, max_key);
117                 ins.objectid = num;
118                 btrfs_init_path(&path);
119                 if (i % 10000 == 0)
120                         fprintf(stderr, "search %d:%d\n", num, i);
121                 ret = btrfs_search_slot(root, &ins, &path, 0, 0);
122                 if (ret) {
123                         btrfs_print_tree(root, root->node);
124                         printf("unable to find %d\n", num);
125                         exit(1);
126                 }
127                 btrfs_release_path(root, &path);
128         }
129         printf("starting big long delete run\n");
130         while(root->node &&
131               btrfs_header_nritems(&root->node->node.header) > 0) {
132                 struct btrfs_leaf *leaf;
133                 int slot;
134                 ins.objectid = (u64)-1;
135                 btrfs_init_path(&path);
136                 ret = btrfs_search_slot(root, &ins, &path, -1, 1);
137                 if (ret == 0)
138                         BUG();
139
140                 leaf = &path.nodes[0]->leaf;
141                 slot = path.slots[0];
142                 if (slot != btrfs_header_nritems(&leaf->header))
143                         BUG();
144                 while(path.slots[0] > 0) {
145                         path.slots[0] -= 1;
146                         slot = path.slots[0];
147                         leaf = &path.nodes[0]->leaf;
148
149                         btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
150                         if (tree_size % 10000 == 0)
151                                 printf("big del %d:%d\n", tree_size, i);
152                         ret = btrfs_del_item(root, &path);
153                         if (ret != 0) {
154                                 printf("del_item returned %d\n", ret);
155                                 BUG();
156                         }
157                         tree_size--;
158                 }
159                 btrfs_release_path(root, &path);
160         }
161         /*
162         printf("previous tree:\n");
163         btrfs_print_tree(root, root->commit_root);
164         printf("map before commit\n");
165         btrfs_print_tree(root->extent_root, root->extent_root->node);
166         */
167         btrfs_commit_transaction(root, &super);
168         printf("tree size is now %d\n", tree_size);
169         printf("root %p commit root %p\n", root->node, root->commit_root);
170         printf("map tree\n");
171         btrfs_print_tree(root->extent_root, root->extent_root->node);
172         close_ctree(root, &super);
173         return 0;
174 }