tdb2: unify tdb1_traverse into tdb_traverse
[ccan] / ccan / tdb2 / test / run-tdb1-nested-traverse.c
1 #include "tdb1-lock-tracking.h"
2 #define fcntl fcntl_with_lockcheck1
3 #include "tdb2-source.h"
4 #include <ccan/tap/tap.h>
5 #undef fcntl
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <err.h>
9 #include "tdb1-external-agent.h"
10 #include "logging.h"
11
12 static struct agent *agent;
13
14 static bool correct_key(TDB_DATA key)
15 {
16         return key.dsize == strlen("hi")
17                 && memcmp(key.dptr, "hi", key.dsize) == 0;
18 }
19
20 static bool correct_data(TDB_DATA data)
21 {
22         return data.dsize == strlen("world")
23                 && memcmp(data.dptr, "world", data.dsize) == 0;
24 }
25
26 static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
27                      void *p)
28 {
29         ok1(correct_key(key));
30         ok1(correct_data(data));
31         return 0;
32 }
33
34 static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
35                      void *p)
36 {
37         ok1(correct_key(key));
38         ok1(correct_data(data));
39         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
40             == WOULD_HAVE_BLOCKED);
41         tdb_traverse(tdb, traverse2, NULL);
42
43         /* That should *not* release the transaction lock! */
44         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
45             == WOULD_HAVE_BLOCKED);
46         return 0;
47 }
48
49 int main(int argc, char *argv[])
50 {
51         struct tdb_context *tdb;
52         TDB_DATA key, data;
53         union tdb_attribute hsize;
54
55         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
56         hsize.base.next = &tap_log_attr;
57         hsize.tdb1_hashsize.hsize = 1024;
58
59         plan_tests(17);
60         agent = prepare_external_agent1();
61         if (!agent)
62                 err(1, "preparing agent");
63
64         tdb = tdb_open("run-nested-traverse.tdb1", TDB_VERSION1,
65                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
66         ok1(tdb);
67
68         ok1(external_agent_operation1(agent, OPEN, tdb->name) == SUCCESS);
69         ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
70             == SUCCESS);
71         ok1(external_agent_operation1(agent, TRANSACTION_COMMIT, tdb->name)
72             == SUCCESS);
73
74         key.dsize = strlen("hi");
75         key.dptr = (void *)"hi";
76         data.dptr = (void *)"world";
77         data.dsize = strlen("world");
78
79         ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
80         tdb_traverse(tdb, traverse1, NULL);
81         tdb_add_flag(tdb, TDB_RDONLY);
82         tdb_traverse(tdb, traverse1, NULL);
83         tdb_remove_flag(tdb, TDB_RDONLY);
84         tdb_close(tdb);
85
86         return exit_status();
87 }