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