tdb2: TDB_SEQNUM and tdb_get_seqnum support.
[ccan] / ccan / tdb2 / test / run-81-seqnum.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/transaction.c>
8 #include <ccan/tdb2/traverse.c>
9 #include <ccan/tdb2/check.c>
10 #include <ccan/tap/tap.h>
11 #include "logging.h"
12
13 int main(int argc, char *argv[])
14 {
15         unsigned int i;
16         struct tdb_context *tdb;
17         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
18         struct tdb_data key = { (unsigned char *)"key", 3 };
19         struct tdb_data data = { (unsigned char *)"data", 4 };
20         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
21                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
22                         TDB_NOMMAP|TDB_CONVERT };
23
24         plan_tests(sizeof(flags) / sizeof(flags[0]) * 15 + 4 * 13);
25         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
26                 tdb = tdb_open("run-new_database.tdb", flags[i]|TDB_SEQNUM,
27                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
28                 if (!ok1(tdb))
29                         continue;
30                 
31                 ok1(tdb_get_seqnum(tdb) == 0);
32                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
33                 ok1(tdb_get_seqnum(tdb) == 1);
34                 /* Fetch doesn't change seqnum */
35                 if (ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
36                         free(d.dptr);
37                 ok1(tdb_get_seqnum(tdb) == 1);
38                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
39                 ok1(tdb_get_seqnum(tdb) == 2);
40
41                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
42                 ok1(tdb_get_seqnum(tdb) == 3);
43                 /* Empty append works */
44                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
45                 ok1(tdb_get_seqnum(tdb) == 4);
46
47                 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
48                 ok1(tdb_get_seqnum(tdb) == 5);
49
50                 if (!(flags[i] & TDB_INTERNAL)) {
51                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
52                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
53                         ok1(tdb_get_seqnum(tdb) == 6);
54                         ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
55                         ok1(tdb_get_seqnum(tdb) == 7);
56                         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
57                         ok1(tdb_get_seqnum(tdb) == 8);
58                         ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
59                         ok1(tdb_get_seqnum(tdb) == 8);
60
61                         ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
62                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
63                         ok1(tdb_get_seqnum(tdb) == 9);
64                         tdb_transaction_cancel(tdb);
65                         ok1(tdb_get_seqnum(tdb) == 8);
66                 }
67                 tdb_close(tdb);
68                 ok1(tap_log_messages == 0);
69         }
70         return exit_status();
71 }