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/tap/tap.h>
11 #define NUM_RECORDS 1000
13 static bool store_records(struct tdb_context *tdb)
16 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
17 struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
19 for (i = 0; i < NUM_RECORDS; i++)
20 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
26 unsigned int records[NUM_RECORDS];
30 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
32 struct trav_data *td = p;
35 memcpy(&val, dbuf.dptr, dbuf.dsize);
36 td->records[td->calls++] = val;
40 int main(int argc, char *argv[])
46 struct tdb_context *tdb;
47 union tdb_attribute seed_attr;
49 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
50 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
51 TDB_NOMMAP|TDB_CONVERT };
53 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
54 seed_attr.base.next = &tap_log_attr;
55 seed_attr.seed.seed = 6334326220117065685ULL;
57 plan_tests(sizeof(flags) / sizeof(flags[0])
58 * (NUM_RECORDS*4 + (NUM_RECORDS-1)*2 + 20) + 1);
59 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
60 tdb = tdb_open("run-traverse.tdb", flags[i],
61 O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
66 ok1(tdb_firstkey(tdb).dptr == NULL);
67 ok1(tdb_error(tdb) == TDB_SUCCESS);
70 k.dptr = (unsigned char *)#
71 k.dsize = sizeof(num);
73 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
74 k = tdb_firstkey(tdb);
75 ok1(k.dsize == sizeof(num));
76 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
77 k2 = tdb_nextkey(tdb, k);
78 ok1(k2.dsize == 0 && k2.dptr == NULL);
82 k.dptr = (unsigned char *)#
83 k.dsize = sizeof(num);
85 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
86 k = tdb_firstkey(tdb);
87 ok1(k.dsize == sizeof(num));
88 memcpy(&num, k.dptr, sizeof(num));
89 ok1(num == 0 || num == 1);
90 k2 = tdb_nextkey(tdb, k);
91 ok1(k2.dsize == sizeof(j));
93 memcpy(&j, k2.dptr, sizeof(j));
94 ok1(j == 0 || j == 1);
96 k = tdb_nextkey(tdb, k2);
97 ok1(k.dsize == 0 && k.dptr == NULL);
101 k.dptr = (unsigned char *)#
102 k.dsize = sizeof(num);
104 ok1(tdb_delete(tdb, k) == 0);
106 ok1(tdb_delete(tdb, k) == 0);
108 /* Now lots of records. */
109 ok1(store_records(tdb));
112 num = tdb_traverse_read(tdb, trav, &td);
113 ok1(num == NUM_RECORDS);
114 ok1(td.calls == NUM_RECORDS);
116 /* Simple loop should match tdb_traverse_read */
117 for (j = 0, k = tdb_firstkey(tdb); j < td.calls; j++) {
120 ok1(k.dsize == sizeof(val));
121 memcpy(&val, k.dptr, k.dsize);
122 ok1(td.records[j] == val);
123 k2 = tdb_nextkey(tdb, k);
128 /* But arbitary orderings should work too. */
129 for (j = td.calls-1; j > 0; j--) {
130 k.dptr = (unsigned char *)&td.records[j-1];
131 k.dsize = sizeof(td.records[j-1]);
132 k = tdb_nextkey(tdb, k);
133 ok1(k.dsize == sizeof(td.records[j]));
134 ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
138 /* Even delete should work. */
139 for (j = 0, k = tdb_firstkey(tdb); k.dptr; j++) {
141 ok1(tdb_delete(tdb, k) == 0);
142 k2 = tdb_nextkey(tdb, k);
147 diag("delete using first/nextkey gave %u of %u records",
149 ok1(j == NUM_RECORDS);
153 ok1(tap_log_messages == 0);
154 return exit_status();