tdb2: move open routines into a separate file (open.c)
[ccan] / ccan / tdb2 / test / run-firstkey-nextkey.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/traverse.c>
9 #include <ccan/tdb2/transaction.c>
10 #include <ccan/tap/tap.h>
11 #include "logging.h"
12
13 #define NUM_RECORDS 1000
14
15 static bool store_records(struct tdb_context *tdb)
16 {
17         int i;
18         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
19         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
20
21         for (i = 0; i < NUM_RECORDS; i++)
22                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
23                         return false;
24         return true;
25 }
26
27 struct trav_data {
28         unsigned int records[NUM_RECORDS];
29         unsigned int calls;
30 };
31
32 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
33 {
34         struct trav_data *td = p;
35         int val;
36
37         memcpy(&val, dbuf.dptr, dbuf.dsize);
38         td->records[td->calls++] = val;
39         return 0;
40 }
41
42 /* Since tdb_nextkey frees dptr, we need to clone it. */
43 static TDB_DATA dup_key(TDB_DATA key)
44 {
45         void *p = malloc(key.dsize);
46         memcpy(p, key.dptr, key.dsize);
47         key.dptr = p;
48         return key;
49 }
50
51 int main(int argc, char *argv[])
52 {
53         unsigned int i, j;
54         int num;
55         struct trav_data td;
56         TDB_DATA k;
57         struct tdb_context *tdb;
58         union tdb_attribute seed_attr;
59         enum TDB_ERROR ecode;
60
61         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
62                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
63                         TDB_NOMMAP|TDB_CONVERT };
64
65         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
66         seed_attr.base.next = &tap_log_attr;
67         seed_attr.seed.seed = 6334326220117065685ULL;
68
69         plan_tests(sizeof(flags) / sizeof(flags[0])
70                    * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
71         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
72                 tdb = tdb_open("run-traverse.tdb", flags[i],
73                                O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
74                 ok1(tdb);
75                 if (!tdb)
76                         continue;
77
78                 ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST);
79
80                 /* One entry... */
81                 k.dptr = (unsigned char *)&num;
82                 k.dsize = sizeof(num);
83                 num = 0;
84                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
85                 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
86                 ok1(k.dsize == sizeof(num));
87                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
88                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
89
90                 /* Two entries. */
91                 k.dptr = (unsigned char *)&num;
92                 k.dsize = sizeof(num);
93                 num = 1;
94                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
95                 ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS);
96                 ok1(k.dsize == sizeof(num));
97                 memcpy(&num, k.dptr, sizeof(num));
98                 ok1(num == 0 || num == 1);
99                 ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
100                 ok1(k.dsize == sizeof(j));
101                 memcpy(&j, k.dptr, sizeof(j));
102                 ok1(j == 0 || j == 1);
103                 ok1(j != num);
104                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
105
106                 /* Clean up. */
107                 k.dptr = (unsigned char *)&num;
108                 k.dsize = sizeof(num);
109                 num = 0;
110                 ok1(tdb_delete(tdb, k) == 0);
111                 num = 1;
112                 ok1(tdb_delete(tdb, k) == 0);
113
114                 /* Now lots of records. */
115                 ok1(store_records(tdb));
116                 td.calls = 0;
117
118                 num = tdb_traverse(tdb, trav, &td);
119                 ok1(num == NUM_RECORDS);
120                 ok1(td.calls == NUM_RECORDS);
121
122                 /* Simple loop should match tdb_traverse */
123                 for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) {
124                         int val;
125
126                         ok1(ecode == TDB_SUCCESS);
127                         ok1(k.dsize == sizeof(val));
128                         memcpy(&val, k.dptr, k.dsize);
129                         ok1(td.records[j] == val);
130                         ecode = tdb_nextkey(tdb, &k);
131                 }
132
133                 /* But arbitrary orderings should work too. */
134                 for (j = td.calls-1; j > 0; j--) {
135                         k.dptr = (unsigned char *)&td.records[j-1];
136                         k.dsize = sizeof(td.records[j-1]);
137                         k = dup_key(k);
138                         ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS);
139                         ok1(k.dsize == sizeof(td.records[j]));
140                         ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
141                         free(k.dptr);
142                 }
143
144                 /* Even delete should work. */
145                 for (j = 0, ecode = tdb_firstkey(tdb, &k);
146                      ecode != TDB_ERR_NOEXIST;
147                      j++) {
148                         ok1(ecode == TDB_SUCCESS);
149                         ok1(k.dsize == 4);
150                         ok1(tdb_delete(tdb, k) == 0);
151                         ecode = tdb_nextkey(tdb, &k);
152                 }
153
154                 diag("delete using first/nextkey gave %u of %u records",
155                      j, NUM_RECORDS);
156                 ok1(j == NUM_RECORDS);
157                 tdb_close(tdb);
158         }
159
160         ok1(tap_log_messages == 0);
161         return exit_status();
162 }