]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-traverse.c
tdb2: import TDB1 code.
[ccan] / ccan / tdb2 / test / run-traverse.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 #define NUM_RECORDS 1000
6
7 /* We use the same seed which we saw a failure on. */
8 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
9 {
10         return hash64_stable((const unsigned char *)key, len,
11                              *(uint64_t *)p);
12 }
13
14 static bool store_records(struct tdb_context *tdb)
15 {
16         int i;
17         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
18         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
19
20         for (i = 0; i < NUM_RECORDS; i++)
21                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
22                         return false;
23         return true;
24 }
25
26 struct trav_data {
27         unsigned int calls, call_limit;
28         int low, high;
29         bool mismatch;
30         bool delete;
31         enum TDB_ERROR delete_error;
32 };
33
34 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
35                 struct trav_data *td)
36 {
37         int val;
38
39         td->calls++;
40         if (key.dsize != sizeof(val) || dbuf.dsize != sizeof(val)
41             || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
42                 td->mismatch = true;
43                 return -1;
44         }
45         memcpy(&val, dbuf.dptr, dbuf.dsize);
46         if (val < td->low)
47                 td->low = val;
48         if (val > td->high)
49                 td->high = val;
50
51         if (td->delete) {
52                 td->delete_error = tdb_delete(tdb, key);
53                 if (td->delete_error != TDB_SUCCESS) {
54                         return -1;
55                 }
56         }
57
58         if (td->calls == td->call_limit)
59                 return 1;
60         return 0;
61 }
62
63 struct trav_grow_data {
64         unsigned int calls;
65         unsigned int num_large;
66         bool mismatch;
67         enum TDB_ERROR error;
68 };
69
70 static int trav_grow(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
71                      struct trav_grow_data *tgd)             
72 {
73         int val;
74         unsigned char buffer[128] = { 0 };
75
76         tgd->calls++;
77         if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
78             || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
79                 tgd->mismatch = true;
80                 return -1;
81         }
82
83         if (dbuf.dsize > sizeof(val))
84                 /* We must have seen this before! */
85                 tgd->num_large++;
86
87         /* Make a big difference to the database. */
88         dbuf.dptr = buffer;
89         dbuf.dsize = sizeof(buffer);
90         tgd->error = tdb_append(tdb, key, dbuf);
91         if (tgd->error != TDB_SUCCESS) {
92                 return -1;
93         }
94         return 0;
95 }
96
97 int main(int argc, char *argv[])
98 {
99         unsigned int i;
100         int num;
101         struct trav_data td;
102         struct trav_grow_data tgd;
103         struct tdb_context *tdb;
104         uint64_t seed = 16014841315512641303ULL;
105         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
106                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
107                         TDB_NOMMAP|TDB_CONVERT };
108         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
109                                                 .fn = fixedhash,
110                                                 .data = &seed } };
111
112         hattr.base.next = &tap_log_attr;
113
114         plan_tests(sizeof(flags) / sizeof(flags[0]) * 32 + 1);
115         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
116                 tdb = tdb_open("run-traverse.tdb", flags[i],
117                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
118                 ok1(tdb);
119                 if (!tdb)
120                         continue;
121
122                 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
123
124                 ok1(store_records(tdb));
125                 num = tdb_traverse(tdb, NULL, NULL);
126                 ok1(num == NUM_RECORDS);
127
128                 /* Full traverse. */
129                 td.calls = 0;
130                 td.call_limit = UINT_MAX;
131                 td.low = INT_MAX;
132                 td.high = INT_MIN;
133                 td.mismatch = false;
134                 td.delete = false;
135
136                 num = tdb_traverse(tdb, trav, &td);
137                 ok1(num == NUM_RECORDS);
138                 ok1(!td.mismatch);
139                 ok1(td.calls == NUM_RECORDS);
140                 ok1(td.low == 0);
141                 ok1(td.high == NUM_RECORDS-1);
142
143                 /* Short traverse. */
144                 td.calls = 0;
145                 td.call_limit = NUM_RECORDS / 2;
146                 td.low = INT_MAX;
147                 td.high = INT_MIN;
148                 td.mismatch = false;
149                 td.delete = false;
150
151                 num = tdb_traverse(tdb, trav, &td);
152                 ok1(num == NUM_RECORDS / 2);
153                 ok1(!td.mismatch);
154                 ok1(td.calls == NUM_RECORDS / 2);
155                 ok1(td.low <= NUM_RECORDS / 2);
156                 ok1(td.high > NUM_RECORDS / 2);
157                 ok1(tdb_check(tdb, NULL, NULL) == 0);
158                 ok1(tap_log_messages == 0);
159
160                 /* Deleting traverse (delete everything). */
161                 td.calls = 0;
162                 td.call_limit = UINT_MAX;
163                 td.low = INT_MAX;
164                 td.high = INT_MIN;
165                 td.mismatch = false;
166                 td.delete = true;
167                 td.delete_error = TDB_SUCCESS;
168                 num = tdb_traverse(tdb, trav, &td);
169                 ok1(num == NUM_RECORDS);
170                 ok1(td.delete_error == TDB_SUCCESS);
171                 ok1(!td.mismatch);
172                 ok1(td.calls == NUM_RECORDS);
173                 ok1(td.low == 0);
174                 ok1(td.high == NUM_RECORDS - 1);
175                 ok1(tdb_check(tdb, NULL, NULL) == 0);
176
177                 /* Now it's empty! */
178                 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
179
180                 /* Re-add. */
181                 ok1(store_records(tdb));
182                 ok1(tdb_traverse(tdb, NULL, NULL) == NUM_RECORDS);
183                 ok1(tdb_check(tdb, NULL, NULL) == 0);
184
185                 /* Grow.  This will cause us to be reshuffled. */
186                 tgd.calls = 0;
187                 tgd.num_large = 0;
188                 tgd.mismatch = false;
189                 tgd.error = TDB_SUCCESS;
190                 ok1(tdb_traverse(tdb, trav_grow, &tgd) > 1);
191                 ok1(tgd.error == 0);
192                 ok1(!tgd.mismatch);
193                 ok1(tdb_check(tdb, NULL, NULL) == 0);
194                 ok1(tgd.num_large < tgd.calls);
195                 diag("growing db: %u calls, %u repeats",
196                      tgd.calls, tgd.num_large);
197
198                 tdb_close(tdb);
199         }
200
201         ok1(tap_log_messages == 0);
202         return exit_status();
203 }