tdb2: feature support.
[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,
42                 struct trav_data *td)
43 {
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                      struct trav_grow_data *tgd)             
79 {
80         int val;
81         unsigned char buffer[128] = { 0 };
82
83         tgd->calls++;
84         if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
85             || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
86                 tgd->mismatch = true;
87                 return -1;
88         }
89
90         if (dbuf.dsize > sizeof(val))
91                 /* We must have seen this before! */
92                 tgd->num_large++;
93
94         /* Make a big difference to the database. */
95         dbuf.dptr = buffer;
96         dbuf.dsize = sizeof(buffer);
97         tgd->error = tdb_append(tdb, key, dbuf);
98         if (tgd->error != TDB_SUCCESS) {
99                 return -1;
100         }
101         return 0;
102 }
103
104 int main(int argc, char *argv[])
105 {
106         unsigned int i;
107         int num;
108         struct trav_data td;
109         struct trav_grow_data tgd;
110         struct tdb_context *tdb;
111         uint64_t seed = 16014841315512641303ULL;
112         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
113                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
114                         TDB_NOMMAP|TDB_CONVERT };
115         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
116                                                 .hash_fn = fixedhash,
117                                                 .hash_private = &seed } };
118
119         hattr.base.next = &tap_log_attr;
120
121         plan_tests(sizeof(flags) / sizeof(flags[0]) * 32 + 1);
122         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
123                 tdb = tdb_open("run-traverse.tdb", flags[i],
124                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
125                 ok1(tdb);
126                 if (!tdb)
127                         continue;
128
129                 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
130
131                 ok1(store_records(tdb));
132                 num = tdb_traverse(tdb, NULL, NULL);
133                 ok1(num == NUM_RECORDS);
134
135                 /* Full traverse. */
136                 td.calls = 0;
137                 td.call_limit = UINT_MAX;
138                 td.low = INT_MAX;
139                 td.high = INT_MIN;
140                 td.mismatch = false;
141                 td.delete = false;
142
143                 num = tdb_traverse(tdb, trav, &td);
144                 ok1(num == NUM_RECORDS);
145                 ok1(!td.mismatch);
146                 ok1(td.calls == NUM_RECORDS);
147                 ok1(td.low == 0);
148                 ok1(td.high == NUM_RECORDS-1);
149
150                 /* Short traverse. */
151                 td.calls = 0;
152                 td.call_limit = NUM_RECORDS / 2;
153                 td.low = INT_MAX;
154                 td.high = INT_MIN;
155                 td.mismatch = false;
156                 td.delete = false;
157
158                 num = tdb_traverse(tdb, trav, &td);
159                 ok1(num == NUM_RECORDS / 2);
160                 ok1(!td.mismatch);
161                 ok1(td.calls == NUM_RECORDS / 2);
162                 ok1(td.low <= NUM_RECORDS / 2);
163                 ok1(td.high > NUM_RECORDS / 2);
164                 ok1(tdb_check(tdb, NULL, NULL) == 0);
165                 ok1(tap_log_messages == 0);
166
167                 /* Deleting traverse (delete everything). */
168                 td.calls = 0;
169                 td.call_limit = UINT_MAX;
170                 td.low = INT_MAX;
171                 td.high = INT_MIN;
172                 td.mismatch = false;
173                 td.delete = true;
174                 td.delete_error = TDB_SUCCESS;
175                 num = tdb_traverse(tdb, trav, &td);
176                 ok1(num == NUM_RECORDS);
177                 ok1(td.delete_error == TDB_SUCCESS);
178                 ok1(!td.mismatch);
179                 ok1(td.calls == NUM_RECORDS);
180                 ok1(td.low == 0);
181                 ok1(td.high == NUM_RECORDS - 1);
182                 ok1(tdb_check(tdb, NULL, NULL) == 0);
183
184                 /* Now it's empty! */
185                 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
186
187                 /* Re-add. */
188                 ok1(store_records(tdb));
189                 ok1(tdb_traverse(tdb, NULL, NULL) == NUM_RECORDS);
190                 ok1(tdb_check(tdb, NULL, NULL) == 0);
191
192                 /* Grow.  This will cause us to be reshuffled. */
193                 tgd.calls = 0;
194                 tgd.num_large = 0;
195                 tgd.mismatch = false;
196                 tgd.error = TDB_SUCCESS;
197                 ok1(tdb_traverse(tdb, trav_grow, &tgd) > 1);
198                 ok1(tgd.error == 0);
199                 ok1(!tgd.mismatch);
200                 ok1(tdb_check(tdb, NULL, NULL) == 0);
201                 ok1(tgd.num_large < tgd.calls);
202                 diag("growing db: %u calls, %u repeats",
203                      tgd.calls, tgd.num_large);
204
205                 tdb_close(tdb);
206         }
207
208         ok1(tap_log_messages == 0);
209         return exit_status();
210 }