1 #include "ntdb-source.h"
2 #include "tap-interface.h"
4 #include "helprun-external-agent.h"
6 #define NUM_RECORDS 1000
8 /* We use the same seed which we saw a failure on. */
9 static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
11 return hash64_stable((const unsigned char *)key, len,
15 static bool store_records(struct ntdb_context *ntdb)
18 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
19 NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
21 for (i = 0; i < NUM_RECORDS; i++)
22 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
28 unsigned int calls, call_limit;
32 enum NTDB_ERROR delete_error;
35 static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
41 if (key.dsize != sizeof(val) || dbuf.dsize != sizeof(val)
42 || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
46 memcpy(&val, dbuf.dptr, dbuf.dsize);
53 td->delete_error = ntdb_delete(ntdb, key);
54 if (td->delete_error != NTDB_SUCCESS) {
59 if (td->calls == td->call_limit)
64 struct trav_grow_data {
66 unsigned int num_large;
68 enum NTDB_ERROR error;
71 static int trav_grow(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
72 struct trav_grow_data *tgd)
75 unsigned char buffer[128] = { 0 };
78 if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
79 || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
84 if (dbuf.dsize > sizeof(val))
85 /* We must have seen this before! */
88 /* Make a big difference to the database. */
90 dbuf.dsize = sizeof(buffer);
91 tgd->error = ntdb_append(ntdb, key, dbuf);
92 if (tgd->error != NTDB_SUCCESS) {
98 int main(int argc, char *argv[])
103 struct trav_grow_data tgd;
104 struct ntdb_context *ntdb;
105 uint64_t seed = 16014841315512641303ULL;
106 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
107 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
108 NTDB_NOMMAP|NTDB_CONVERT };
109 union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
113 hattr.base.next = &tap_log_attr;
115 plan_tests(sizeof(flags) / sizeof(flags[0]) * 32 + 1);
116 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
117 ntdb = ntdb_open("run-traverse.ntdb", flags[i]|MAYBE_NOSYNC,
118 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
123 ok1(ntdb_traverse(ntdb, NULL, NULL) == 0);
125 ok1(store_records(ntdb));
126 num = ntdb_traverse(ntdb, NULL, NULL);
127 ok1(num == NUM_RECORDS);
131 td.call_limit = UINT_MAX;
137 num = ntdb_traverse(ntdb, trav, &td);
138 ok1(num == NUM_RECORDS);
140 ok1(td.calls == NUM_RECORDS);
142 ok1(td.high == NUM_RECORDS-1);
144 /* Short traverse. */
146 td.call_limit = NUM_RECORDS / 2;
152 num = ntdb_traverse(ntdb, trav, &td);
153 ok1(num == NUM_RECORDS / 2);
155 ok1(td.calls == NUM_RECORDS / 2);
156 ok1(td.low <= NUM_RECORDS / 2);
157 ok1(td.high > NUM_RECORDS / 2);
158 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
159 ok1(tap_log_messages == 0);
161 /* Deleting traverse (delete everything). */
163 td.call_limit = UINT_MAX;
168 td.delete_error = NTDB_SUCCESS;
169 num = ntdb_traverse(ntdb, trav, &td);
170 ok1(num == NUM_RECORDS);
171 ok1(td.delete_error == NTDB_SUCCESS);
173 ok1(td.calls == NUM_RECORDS);
175 ok1(td.high == NUM_RECORDS - 1);
176 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
178 /* Now it's empty! */
179 ok1(ntdb_traverse(ntdb, NULL, NULL) == 0);
182 ok1(store_records(ntdb));
183 ok1(ntdb_traverse(ntdb, NULL, NULL) == NUM_RECORDS);
184 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
186 /* Grow. This will cause us to be reshuffled. */
189 tgd.mismatch = false;
190 tgd.error = NTDB_SUCCESS;
191 ok1(ntdb_traverse(ntdb, trav_grow, &tgd) > 1);
194 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
195 ok1(tgd.num_large < tgd.calls);
196 diag("growing db: %u calls, %u repeats",
197 tgd.calls, tgd.num_large);
202 ok1(tap_log_messages == 0);
203 return exit_status();