Commit | Line | Data |
---|---|---|
79f95c82 CM |
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; | |
20524f02 | 12 | // return i; |
79f95c82 CM |
13 | } |
14 | ||
15 | int main(int ac, char **av) { | |
e2fa7227 CM |
16 | struct btrfs_key ins; |
17 | struct btrfs_key last = { (u64)-1, 0, 0}; | |
79f95c82 CM |
18 | char *buf; |
19 | int i; | |
20 | int num; | |
21 | int ret; | |
a28ec197 | 22 | int run_size = 100000; |
79f95c82 CM |
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); | |
20524f02 CM |
47 | if (i == run_size - 5) { |
48 | commit_transaction(root, &super); | |
49 | } | |
a28ec197 | 50 | |
79f95c82 | 51 | } |
a28ec197 | 52 | close_ctree(root, &super); |
79f95c82 CM |
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); | |
02217ed2 | 63 | ret = search_slot(root, &ins, &path, 0, 0); |
79f95c82 CM |
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 | } | |
a28ec197 | 71 | close_ctree(root, &super); |
79f95c82 CM |
72 | root = open_ctree("dbfile", &super); |
73 | printf("node %p level %d total ptrs %d free spc %lu\n", root->node, | |
7518a238 CM |
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)); | |
79f95c82 CM |
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); | |
02217ed2 | 85 | ret = search_slot(root, &ins, &path, -1, 1); |
79f95c82 CM |
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--; | |
93 | } | |
94 | release_path(root, &path); | |
95 | } | |
a28ec197 | 96 | close_ctree(root, &super); |
79f95c82 CM |
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); | |
110 | } | |
a28ec197 | 111 | close_ctree(root, &super); |
79f95c82 CM |
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); | |
02217ed2 | 121 | ret = search_slot(root, &ins, &path, 0, 0); |
79f95c82 CM |
122 | if (ret) { |
123 | print_tree(root, root->node); | |
124 | printf("unable to find %d\n", num); | |
125 | exit(1); | |
126 | } | |
127 | release_path(root, &path); | |
128 | } | |
129 | printf("starting big long delete run\n"); | |
7518a238 CM |
130 | while(root->node && |
131 | btrfs_header_nritems(&root->node->node.header) > 0) { | |
79f95c82 CM |
132 | struct leaf *leaf; |
133 | int slot; | |
134 | ins.objectid = (u64)-1; | |
135 | init_path(&path); | |
02217ed2 | 136 | ret = search_slot(root, &ins, &path, -1, 1); |
79f95c82 CM |
137 | if (ret == 0) |
138 | BUG(); | |
139 | ||
140 | leaf = &path.nodes[0]->leaf; | |
141 | slot = path.slots[0]; | |
7518a238 | 142 | if (slot != btrfs_header_nritems(&leaf->header)) |
79f95c82 CM |
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 | ||
e2fa7227 | 149 | btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key); |
79f95c82 CM |
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(); | |
156 | } | |
157 | tree_size--; | |
158 | } | |
159 | release_path(root, &path); | |
160 | } | |
a28ec197 CM |
161 | /* |
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); | |
166 | */ | |
167 | commit_transaction(root, &super); | |
79f95c82 | 168 | printf("tree size is now %d\n", tree_size); |
a28ec197 | 169 | printf("root %p commit root %p\n", root->node, root->commit_root); |
79f95c82 CM |
170 | printf("map tree\n"); |
171 | print_tree(root->extent_root, root->extent_root->node); | |
a28ec197 | 172 | close_ctree(root, &super); |
79f95c82 CM |
173 | return 0; |
174 | } |