]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-firstkey-nextkey.c
db52c85281b4da599789ceffd3fd10166b2643d2
[ccan] / ccan / tdb2 / test / run-firstkey-nextkey.c
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>
9 #include "logging.h"
10
11 #define NUM_RECORDS 1000
12
13 static bool store_records(struct tdb_context *tdb)
14 {
15         int i;
16         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
17         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
18
19         for (i = 0; i < NUM_RECORDS; i++)
20                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
21                         return false;
22         return true;
23 }
24
25 struct trav_data {
26         unsigned int records[NUM_RECORDS];
27         unsigned int calls;
28 };
29
30 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
31 {
32         struct trav_data *td = p;
33         int val;
34
35         memcpy(&val, dbuf.dptr, dbuf.dsize);
36         td->records[td->calls++] = val;
37         return 0;
38 }
39
40 int main(int argc, char *argv[])
41 {
42         unsigned int i, j;
43         int num;
44         struct trav_data td;
45         TDB_DATA k, k2;
46         struct tdb_context *tdb;
47         union tdb_attribute seed_attr;
48
49         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
50                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
51                         TDB_NOMMAP|TDB_CONVERT };
52
53         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
54         seed_attr.base.next = &tap_log_attr;
55         seed_attr.seed.seed = 6334326220117065685ULL;
56
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);
62                 ok1(tdb);
63                 if (!tdb)
64                         continue;
65
66                 ok1(tdb_firstkey(tdb).dptr == NULL);
67                 ok1(tdb_error(tdb) == TDB_SUCCESS);
68
69                 /* One entry... */
70                 k.dptr = (unsigned char *)&num;
71                 k.dsize = sizeof(num);
72                 num = 0;
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);
79                 free(k.dptr);
80
81                 /* Two entries. */
82                 k.dptr = (unsigned char *)&num;
83                 k.dsize = sizeof(num);
84                 num = 1;
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));
92                 free(k.dptr);
93                 memcpy(&j, k2.dptr, sizeof(j));
94                 ok1(j == 0 || j == 1);
95                 ok1(j != num);
96                 k = tdb_nextkey(tdb, k2);
97                 ok1(k.dsize == 0 && k.dptr == NULL);
98                 free(k2.dptr);
99
100                 /* Clean up. */
101                 k.dptr = (unsigned char *)&num;
102                 k.dsize = sizeof(num);
103                 num = 0;
104                 ok1(tdb_delete(tdb, k) == 0);
105                 num = 1;
106                 ok1(tdb_delete(tdb, k) == 0);
107
108                 /* Now lots of records. */
109                 ok1(store_records(tdb));
110                 td.calls = 0;
111
112                 num = tdb_traverse_read(tdb, trav, &td);
113                 ok1(num == NUM_RECORDS);
114                 ok1(td.calls == NUM_RECORDS);
115
116                 /* Simple loop should match tdb_traverse_read */
117                 for (j = 0, k = tdb_firstkey(tdb); j < td.calls; j++) {
118                         int val;
119
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);
124                         free(k.dptr);
125                         k = k2;
126                 }
127
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);
135                         free(k.dptr);
136                 }
137
138                 /* Even delete should work. */
139                 for (j = 0, k = tdb_firstkey(tdb); k.dptr; j++) {
140                         ok1(k.dsize == 4);
141                         ok1(tdb_delete(tdb, k) == 0);
142                         k2 = tdb_nextkey(tdb, k);
143                         free(k.dptr);
144                         k = k2;
145                 }
146
147                 diag("delete using first/nextkey gave %u of %u records",
148                      j, NUM_RECORDS);
149                 ok1(j == NUM_RECORDS);
150                 tdb_close(tdb);
151         }
152
153         ok1(tap_log_messages == 0);
154         return exit_status();
155 }