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>
12 #define NUM_RECORDS 1000
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 records[NUM_RECORDS];
31 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
33 struct trav_data *td = p;
36 memcpy(&val, dbuf.dptr, dbuf.dsize);
37 td->records[td->calls++] = val;
41 /* Since tdb_nextkey frees dptr, we need to clone it. */
42 static TDB_DATA dup_key(TDB_DATA key)
44 void *p = malloc(key.dsize);
45 memcpy(p, key.dptr, key.dsize);
50 int main(int argc, char *argv[])
56 struct tdb_context *tdb;
57 union tdb_attribute seed_attr;
60 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
61 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
62 TDB_NOMMAP|TDB_CONVERT };
64 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
65 seed_attr.base.next = &tap_log_attr;
66 seed_attr.seed.seed = 6334326220117065685ULL;
68 plan_tests(sizeof(flags) / sizeof(flags[0])
69 * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
70 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
71 tdb = tdb_open("run-traverse.tdb", flags[i],
72 O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
77 ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST);
80 k.dptr = (unsigned char *)#
81 k.dsize = sizeof(num);
83 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
84 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
85 ok1(k.dsize == sizeof(num));
86 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
87 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
90 k.dptr = (unsigned char *)#
91 k.dsize = sizeof(num);
93 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
94 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
95 ok1(k.dsize == sizeof(num));
96 memcpy(&num, k.dptr, sizeof(num));
97 ok1(num == 0 || num == 1);
98 ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
99 ok1(k.dsize == sizeof(j));
100 memcpy(&j, k.dptr, sizeof(j));
101 ok1(j == 0 || j == 1);
103 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
106 k.dptr = (unsigned char *)#
107 k.dsize = sizeof(num);
109 ok1(tdb_delete(tdb, k) == 0);
111 ok1(tdb_delete(tdb, k) == 0);
113 /* Now lots of records. */
114 ok1(store_records(tdb));
117 num = tdb_traverse(tdb, trav, &td);
118 ok1(num == NUM_RECORDS);
119 ok1(td.calls == NUM_RECORDS);
121 /* Simple loop should match tdb_traverse */
122 for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) {
125 ok1(ecode == TDB_SUCCESS);
126 ok1(k.dsize == sizeof(val));
127 memcpy(&val, k.dptr, k.dsize);
128 ok1(td.records[j] == val);
129 ecode = tdb_nextkey(tdb, &k);
132 /* But arbitrary orderings should work too. */
133 for (j = td.calls-1; j > 0; j--) {
134 k.dptr = (unsigned char *)&td.records[j-1];
135 k.dsize = sizeof(td.records[j-1]);
137 ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
138 ok1(k.dsize == sizeof(td.records[j]));
139 ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
143 /* Even delete should work. */
144 for (j = 0, ecode = tdb_firstkey(tdb, &k);
145 ecode != TDB_ERR_NOEXIST;
147 ok1(ecode == TDB_SUCCESS);
149 ok1(tdb_delete(tdb, k) == 0);
150 ecode = tdb_nextkey(tdb, &k);
153 diag("delete using first/nextkey gave %u of %u records",
155 ok1(j == NUM_RECORDS);
159 ok1(tap_log_messages == 0);
160 return exit_status();