1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
5 #define NUM_RECORDS 1000
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)
10 return hash64_stable((const unsigned char *)key, len,
14 static bool store_records(struct tdb_context *tdb)
17 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
18 struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
20 for (i = 0; i < NUM_RECORDS; i++)
21 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
27 unsigned int calls, call_limit;
31 enum TDB_ERROR delete_error;
34 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
40 if (key.dsize != sizeof(val) || dbuf.dsize != sizeof(val)
41 || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
45 memcpy(&val, dbuf.dptr, dbuf.dsize);
52 td->delete_error = tdb_delete(tdb, key);
53 if (td->delete_error != TDB_SUCCESS) {
58 if (td->calls == td->call_limit)
63 struct trav_grow_data {
65 unsigned int num_large;
70 static int trav_grow(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
71 struct trav_grow_data *tgd)
74 unsigned char buffer[128] = { 0 };
77 if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
78 || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
83 if (dbuf.dsize > sizeof(val))
84 /* We must have seen this before! */
87 /* Make a big difference to the database. */
89 dbuf.dsize = sizeof(buffer);
90 tgd->error = tdb_append(tdb, key, dbuf);
91 if (tgd->error != TDB_SUCCESS) {
97 int main(int argc, char *argv[])
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 },
112 hattr.base.next = &tap_log_attr;
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);
122 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
124 ok1(store_records(tdb));
125 num = tdb_traverse(tdb, NULL, NULL);
126 ok1(num == NUM_RECORDS);
130 td.call_limit = UINT_MAX;
136 num = tdb_traverse(tdb, trav, &td);
137 ok1(num == NUM_RECORDS);
139 ok1(td.calls == NUM_RECORDS);
141 ok1(td.high == NUM_RECORDS-1);
143 /* Short traverse. */
145 td.call_limit = NUM_RECORDS / 2;
151 num = tdb_traverse(tdb, trav, &td);
152 ok1(num == NUM_RECORDS / 2);
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);
160 /* Deleting traverse (delete everything). */
162 td.call_limit = UINT_MAX;
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);
172 ok1(td.calls == NUM_RECORDS);
174 ok1(td.high == NUM_RECORDS - 1);
175 ok1(tdb_check(tdb, NULL, NULL) == 0);
177 /* Now it's empty! */
178 ok1(tdb_traverse(tdb, NULL, NULL) == 0);
181 ok1(store_records(tdb));
182 ok1(tdb_traverse(tdb, NULL, NULL) == NUM_RECORDS);
183 ok1(tdb_check(tdb, NULL, NULL) == 0);
185 /* Grow. This will cause us to be reshuffled. */
188 tgd.mismatch = false;
189 tgd.error = TDB_SUCCESS;
190 ok1(tdb_traverse(tdb, trav_grow, &tgd) > 1);
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);
201 ok1(tap_log_messages == 0);
202 return exit_status();