node->blockptrs endian fixes
[btrfs-progs-unstable.git] / quick-test.c
blobab3bda53a2f622dac80291cdd219189a108c1451
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"
9 /* for testing only */
10 int next_key(int i, int max_key) {
11 return rand() % max_key;
12 // return i;
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 ctree_path path;
26 struct ctree_super_block super;
27 struct ctree_root *root;
29 radix_tree_init();
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);
52 close_ctree(root, &super);
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);
69 release_path(root, &path);
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 init_path(&path);
85 ret = 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 = del_item(root, &path);
90 if (ret != 0)
91 BUG();
92 tree_size--;
94 release_path(root, &path);
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 = insert_item(root, &ins, buf, strlen(buf));
107 if (!ret)
108 tree_size++;
109 free(buf);
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 init_path(&path);
119 if (i % 10000 == 0)
120 fprintf(stderr, "search %d:%d\n", num, i);
121 ret = search_slot(root, &ins, &path, 0, 0);
122 if (ret) {
123 print_tree(root, root->node);
124 printf("unable to find %d\n", num);
125 exit(1);
127 release_path(root, &path);
129 printf("starting big long delete run\n");
130 while(root->node &&
131 btrfs_header_nritems(&root->node->node.header) > 0) {
132 struct leaf *leaf;
133 int slot;
134 ins.objectid = (u64)-1;
135 init_path(&path);
136 ret = search_slot(root, &ins, &path, -1, 1);
137 if (ret == 0)
138 BUG();
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;
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 = del_item(root, &path);
153 if (ret != 0) {
154 printf("del_item returned %d\n", ret);
155 BUG();
157 tree_size--;
159 release_path(root, &path);
162 printf("previous tree:\n");
163 print_tree(root, root->commit_root);
164 printf("map before commit\n");
165 print_tree(root->extent_root, root->extent_root->node);
167 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 print_tree(root->extent_root, root->extent_root->node);
172 close_ctree(root, &super);
173 return 0;