tdb2: clean up logging
[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
60                 if (tdb_delete(tdb, key) != 0) {
61                         td->delete_error = tdb_error(tdb);
62                         return -1;
63                 }
64         }
65
66         if (td->calls == td->call_limit)
67                 return 1;
68         return 0;
69 }
70
71 struct trav_grow_data {
72         unsigned int calls;
73         unsigned int num_large;
74         bool mismatch;
75         enum TDB_ERROR error;
76 };
77
78 static int trav_grow(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
79                      void *p)
80 {
81         struct trav_grow_data *tgd = p;
82         int val;
83         unsigned char buffer[128] = { 0 };
84
85         tgd->calls++;
86         if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
87             || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
88                 tgd->mismatch = true;
89                 return -1;
90         }
91
92         if (dbuf.dsize > sizeof(val))
93                 /* We must have seen this before! */
94                 tgd->num_large++;
95
96         /* Make a big difference to the database. */
97         dbuf.dptr = buffer;
98         dbuf.dsize = sizeof(buffer);
99         if (tdb_append(tdb, key, dbuf) != 0) {
100                 tgd->error = tdb_error(tdb);
101                 return -1;
102         }
103         return 0;
104 }
105
106 int main(int argc, char *argv[])
107 {
108         unsigned int i;
109         int num;
110         struct trav_data td;
111         struct trav_grow_data tgd;
112         struct tdb_context *tdb;
113         uint64_t seed = 16014841315512641303ULL;
114         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
115                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
116                         TDB_NOMMAP|TDB_CONVERT };
117         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
118                                                 .hash_fn = fixedhash,
119                                                 .hash_private = &seed } };
120
121         hattr.base.next = &tap_log_attr;
122
123         plan_tests(sizeof(flags) / sizeof(flags[0]) * 53 + 1);
124         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
125                 tdb = tdb_open("run-traverse.tdb", flags[i],
126                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
127                 ok1(tdb);
128                 if (!tdb)
129                         continue;
130
131                 ok1(tdb_traverse_read(tdb, NULL, NULL) == 0);
132                 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
133
134                 ok1(store_records(tdb));
135                 num = tdb_traverse_read(tdb, NULL, NULL);
136                 ok1(num == NUM_RECORDS);
137                 num = tdb_traverse(tdb, NULL, NULL);
138                 ok1(num == NUM_RECORDS);
139
140                 /* Full traverse (read-only). */
141                 td.calls = 0;
142                 td.call_limit = UINT_MAX;
143                 td.low = INT_MAX;
144                 td.high = INT_MIN;
145                 td.mismatch = false;
146                 td.delete = false;
147
148                 num = tdb_traverse_read(tdb, trav, &td);
149                 ok1(num == NUM_RECORDS);
150                 ok1(!td.mismatch);
151                 ok1(td.calls == NUM_RECORDS);
152                 ok1(td.low == 0);
153                 ok1(td.high == NUM_RECORDS-1);
154
155                 /* Full traverse. */
156                 td.calls = 0;
157                 td.call_limit = UINT_MAX;
158                 td.low = INT_MAX;
159                 td.high = INT_MIN;
160                 td.mismatch = false;
161                 td.delete = false;
162
163                 num = tdb_traverse(tdb, trav, &td);
164                 ok1(num == NUM_RECORDS);
165                 ok1(!td.mismatch);
166                 ok1(td.calls == NUM_RECORDS);
167                 ok1(td.low == 0);
168                 ok1(td.high == NUM_RECORDS-1);
169
170                 /* Short traverse. */
171                 td.calls = 0;
172                 td.call_limit = NUM_RECORDS / 2;
173                 td.low = INT_MAX;
174                 td.high = INT_MIN;
175                 td.mismatch = false;
176                 td.delete = false;
177
178                 num = tdb_traverse(tdb, trav, &td);
179                 ok1(num == NUM_RECORDS / 2);
180                 ok1(!td.mismatch);
181                 ok1(td.calls == NUM_RECORDS / 2);
182                 ok1(td.low <= NUM_RECORDS / 2);
183                 ok1(td.high > NUM_RECORDS / 2);
184                 ok1(tdb_check(tdb, NULL, NULL) == 0);
185                 ok1(tap_log_messages == 0);
186
187                 /* Growing traverse.  Expect failure on r/o traverse. */
188                 tgd.calls = 0;
189                 tgd.num_large = 0;
190                 tgd.mismatch = false;
191                 tgd.error = TDB_SUCCESS;
192                 num = tdb_traverse_read(tdb, trav_grow, &tgd);
193                 ok1(num == 1);
194                 ok1(tgd.error == TDB_ERR_RDONLY);
195                 ok1(tgd.calls == 1);
196                 ok1(!tgd.mismatch);
197                 ok1(tap_log_messages == 1);
198                 tap_log_messages = 0;
199                 ok1(tdb_check(tdb, NULL, NULL) == 0);
200
201                 /* Deleting traverse.  Expect failure on r/o traverse. */
202                 td.calls = 0;
203                 td.call_limit = UINT_MAX;
204                 td.low = INT_MAX;
205                 td.high = INT_MIN;
206                 td.mismatch = false;
207                 td.delete = true;
208                 td.delete_error = TDB_SUCCESS;
209                 num = tdb_traverse_read(tdb, trav, &td);
210                 ok1(num == 1);
211                 ok1(td.delete_error == TDB_ERR_RDONLY);
212                 ok1(!td.mismatch);
213                 ok1(td.calls == 1);
214                 ok1(td.low == td.high);
215                 ok1(tap_log_messages == 1);
216                 tap_log_messages = 0;
217                 ok1(tdb_check(tdb, NULL, NULL) == 0);
218
219                 /* Deleting traverse (delete everything). */
220                 td.calls = 0;
221                 td.call_limit = UINT_MAX;
222                 td.low = INT_MAX;
223                 td.high = INT_MIN;
224                 td.mismatch = false;
225                 td.delete = true;
226                 td.delete_error = TDB_SUCCESS;
227                 num = tdb_traverse(tdb, trav, &td);
228                 ok1(num == NUM_RECORDS);
229                 ok1(td.delete_error == TDB_SUCCESS);
230                 ok1(!td.mismatch);
231                 ok1(td.calls == NUM_RECORDS);
232                 ok1(td.low == 0);
233                 ok1(td.high == NUM_RECORDS - 1);
234                 ok1(tdb_check(tdb, NULL, NULL) == 0);
235
236                 /* Now it's empty! */
237                 ok1(tdb_traverse_read(tdb, NULL, NULL) == 0);
238                 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
239
240                 /* Re-add. */
241                 ok1(store_records(tdb));
242                 ok1(tdb_traverse_read(tdb, NULL, NULL) == NUM_RECORDS);
243                 ok1(tdb_check(tdb, NULL, NULL) == 0);
244
245                 /* Grow.  This will cause us to be reshuffled. */
246                 tgd.calls = 0;
247                 tgd.num_large = 0;
248                 tgd.mismatch = false;
249                 tgd.error = TDB_SUCCESS;
250                 ok1(tdb_traverse(tdb, trav_grow, &tgd) > 1);
251                 ok1(tgd.error == 0);
252                 ok1(!tgd.mismatch);
253                 ok1(tdb_check(tdb, NULL, NULL) == 0);
254                 ok1(tgd.num_large < tgd.calls);
255                 diag("growing db: %u calls, %u repeats",
256                      tgd.calls, tgd.num_large);
257
258                 tdb_close(tdb);
259         }
260
261         ok1(tap_log_messages == 0);
262         return exit_status();
263 }