blob: ab3bda53a2f622dac80291cdd219189a108c1451 [file] [log] [blame]
Chris Mason79f95c82007-03-01 15:16:26 -05001#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 */
10int next_key(int i, int max_key) {
11 return rand() % max_key;
Chris Mason20524f02007-03-10 06:35:47 -050012 // return i;
Chris Mason79f95c82007-03-01 15:16:26 -050013}
14
15int main(int ac, char **av) {
Chris Masone2fa7222007-03-12 16:22:34 -040016 struct btrfs_key ins;
17 struct btrfs_key last = { (u64)-1, 0, 0};
Chris Mason79f95c82007-03-01 15:16:26 -050018 char *buf;
19 int i;
20 int num;
21 int ret;
Chris Masona28ec192007-03-06 20:08:01 -050022 int run_size = 100000;
Chris Mason79f95c82007-03-01 15:16:26 -050023 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);
Chris Mason20524f02007-03-10 06:35:47 -050047 if (i == run_size - 5) {
48 commit_transaction(root, &super);
49 }
Chris Masona28ec192007-03-06 20:08:01 -050050
Chris Mason79f95c82007-03-01 15:16:26 -050051 }
Chris Masona28ec192007-03-06 20:08:01 -050052 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -050053
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);
Chris Mason02217ed2007-03-02 16:08:05 -050063 ret = search_slot(root, &ins, &path, 0, 0);
Chris Mason79f95c82007-03-01 15:16:26 -050064 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 }
Chris Masona28ec192007-03-06 20:08:01 -050071 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -050072 root = open_ctree("dbfile", &super);
73 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
Chris Mason7518a232007-03-12 12:01:18 -040074 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));
Chris Mason79f95c82007-03-01 15:16:26 -050078 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);
Chris Mason02217ed2007-03-02 16:08:05 -050085 ret = search_slot(root, &ins, &path, -1, 1);
Chris Mason79f95c82007-03-01 15:16:26 -050086 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 }
Chris Masona28ec192007-03-06 20:08:01 -050096 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -050097 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 }
Chris Masona28ec192007-03-06 20:08:01 -0500111 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -0500112 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);
Chris Mason02217ed2007-03-02 16:08:05 -0500121 ret = search_slot(root, &ins, &path, 0, 0);
Chris Mason79f95c82007-03-01 15:16:26 -0500122 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");
Chris Mason7518a232007-03-12 12:01:18 -0400130 while(root->node &&
131 btrfs_header_nritems(&root->node->node.header) > 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500132 struct leaf *leaf;
133 int slot;
134 ins.objectid = (u64)-1;
135 init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -0500136 ret = search_slot(root, &ins, &path, -1, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500137 if (ret == 0)
138 BUG();
139
140 leaf = &path.nodes[0]->leaf;
141 slot = path.slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400142 if (slot != btrfs_header_nritems(&leaf->header))
Chris Mason79f95c82007-03-01 15:16:26 -0500143 BUG();
144 while(path.slots[0] > 0) {
145 path.slots[0] -= 1;
146 slot = path.slots[0];
147 leaf = &path.nodes[0]->leaf;
148
Chris Masone2fa7222007-03-12 16:22:34 -0400149 btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
Chris Mason79f95c82007-03-01 15:16:26 -0500150 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 }
Chris Masona28ec192007-03-06 20:08:01 -0500161 /*
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);
Chris Mason79f95c82007-03-01 15:16:26 -0500168 printf("tree size is now %d\n", tree_size);
Chris Masona28ec192007-03-06 20:08:01 -0500169 printf("root %p commit root %p\n", root->node, root->commit_root);
Chris Mason79f95c82007-03-01 15:16:26 -0500170 printf("map tree\n");
171 print_tree(root->extent_root, root->extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500172 close_ctree(root, &super);
Chris Mason79f95c82007-03-01 15:16:26 -0500173 return 0;
174}