tdb2: change API to return the error value.
[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/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 #define NUM_RECORDS 1000
13
14 static bool store_records(struct tdb_context *tdb)
15 {
16         int i;
17         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
18         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
19
20         for (i = 0; i < NUM_RECORDS; i++)
21                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
22                         return false;
23         return true;
24 }
25
26 struct trav_data {
27         unsigned int records[NUM_RECORDS];
28         unsigned int calls;
29 };
30
31 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
32 {
33         struct trav_data *td = p;
34         int val;
35
36         memcpy(&val, dbuf.dptr, dbuf.dsize);
37         td->records[td->calls++] = val;
38         return 0;
39 }
40
41 /* Since tdb_nextkey frees dptr, we need to clone it. */
42 static TDB_DATA dup_key(TDB_DATA key)
43 {
44         void *p = malloc(key.dsize);
45         memcpy(p, key.dptr, key.dsize);
46         key.dptr = p;
47         return key;
48 }
49
50 int main(int argc, char *argv[])
51 {
52         unsigned int i, j;
53         int num;
54         struct trav_data td;
55         TDB_DATA k;
56         struct tdb_context *tdb;
57         union tdb_attribute seed_attr;
58         enum TDB_ERROR ecode;
59
60         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
61                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
62                         TDB_NOMMAP|TDB_CONVERT };
63
64         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
65         seed_attr.base.next = &tap_log_attr;
66         seed_attr.seed.seed = 6334326220117065685ULL;
67
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);
73                 ok1(tdb);
74                 if (!tdb)
75                         continue;
76
77                 ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST);
78
79                 /* One entry... */
80                 k.dptr = (unsigned char *)&num;
81                 k.dsize = sizeof(num);
82                 num = 0;
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);
88
89                 /* Two entries. */
90                 k.dptr = (unsigned char *)&num;
91                 k.dsize = sizeof(num);
92                 num = 1;
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);
102                 ok1(j != num);
103                 ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST);
104
105                 /* Clean up. */
106                 k.dptr = (unsigned char *)&num;
107                 k.dsize = sizeof(num);
108                 num = 0;
109                 ok1(tdb_delete(tdb, k) == 0);
110                 num = 1;
111                 ok1(tdb_delete(tdb, k) == 0);
112
113                 /* Now lots of records. */
114                 ok1(store_records(tdb));
115                 td.calls = 0;
116
117                 num = tdb_traverse(tdb, trav, &td);
118                 ok1(num == NUM_RECORDS);
119                 ok1(td.calls == NUM_RECORDS);
120
121                 /* Simple loop should match tdb_traverse */
122                 for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) {
123                         int val;
124
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);
130                 }
131
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]);
136                         k = dup_key(k);
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);
140                         free(k.dptr);
141                 }
142
143                 /* Even delete should work. */
144                 for (j = 0, ecode = tdb_firstkey(tdb, &k);
145                      ecode != TDB_ERR_NOEXIST;
146                      j++) {
147                         ok1(ecode == TDB_SUCCESS);
148                         ok1(k.dsize == 4);
149                         ok1(tdb_delete(tdb, k) == 0);
150                         ecode = tdb_nextkey(tdb, &k);
151                 }
152
153                 diag("delete using first/nextkey gave %u of %u records",
154                      j, NUM_RECORDS);
155                 ok1(j == NUM_RECORDS);
156                 tdb_close(tdb);
157         }
158
159         ok1(tap_log_messages == 0);
160         return exit_status();
161 }