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