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 int main(int argc, char *argv[])
47 struct tdb_context *tdb;
48 union tdb_attribute seed_attr;
50 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
51 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
52 TDB_NOMMAP|TDB_CONVERT };
54 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
55 seed_attr.base.next = &tap_log_attr;
56 seed_attr.seed.seed = 6334326220117065685ULL;
58 plan_tests(sizeof(flags) / sizeof(flags[0])
59 * (NUM_RECORDS*4 + (NUM_RECORDS-1)*2 + 20) + 1);
60 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
61 tdb = tdb_open("run-traverse.tdb", flags[i],
62 O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
67 ok1(tdb_firstkey(tdb).dptr == NULL);
68 ok1(tdb_error(tdb) == TDB_SUCCESS);
71 k.dptr = (unsigned char *)#
72 k.dsize = sizeof(num);
74 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
75 k = tdb_firstkey(tdb);
76 ok1(k.dsize == sizeof(num));
77 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
78 k2 = tdb_nextkey(tdb, k);
79 ok1(k2.dsize == 0 && k2.dptr == NULL);
83 k.dptr = (unsigned char *)#
84 k.dsize = sizeof(num);
86 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
87 k = tdb_firstkey(tdb);
88 ok1(k.dsize == sizeof(num));
89 memcpy(&num, k.dptr, sizeof(num));
90 ok1(num == 0 || num == 1);
91 k2 = tdb_nextkey(tdb, k);
92 ok1(k2.dsize == sizeof(j));
94 memcpy(&j, k2.dptr, sizeof(j));
95 ok1(j == 0 || j == 1);
97 k = tdb_nextkey(tdb, k2);
98 ok1(k.dsize == 0 && k.dptr == NULL);
102 k.dptr = (unsigned char *)#
103 k.dsize = sizeof(num);
105 ok1(tdb_delete(tdb, k) == 0);
107 ok1(tdb_delete(tdb, k) == 0);
109 /* Now lots of records. */
110 ok1(store_records(tdb));
113 num = tdb_traverse(tdb, trav, &td);
114 ok1(num == NUM_RECORDS);
115 ok1(td.calls == NUM_RECORDS);
117 /* Simple loop should match tdb_traverse */
118 for (j = 0, k = tdb_firstkey(tdb); j < td.calls; j++) {
121 ok1(k.dsize == sizeof(val));
122 memcpy(&val, k.dptr, k.dsize);
123 ok1(td.records[j] == val);
124 k2 = tdb_nextkey(tdb, k);
129 /* But arbitrary orderings should work too. */
130 for (j = td.calls-1; j > 0; j--) {
131 k.dptr = (unsigned char *)&td.records[j-1];
132 k.dsize = sizeof(td.records[j-1]);
133 k = tdb_nextkey(tdb, k);
134 ok1(k.dsize == sizeof(td.records[j]));
135 ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
139 /* Even delete should work. */
140 for (j = 0, k = tdb_firstkey(tdb); k.dptr; j++) {
142 ok1(tdb_delete(tdb, k) == 0);
143 k2 = tdb_nextkey(tdb, k);
148 diag("delete using first/nextkey gave %u of %u records",
150 ok1(j == NUM_RECORDS);
154 ok1(tap_log_messages == 0);
155 return exit_status();