ntdb: next-generation trivial key-value database
[ccan] / ccan / ntdb / test / api-55-transaction.c
1 #include "private.h" // struct ntdb_context
2 #include "ntdb.h"
3 #include "tap-interface.h"
4 #include <stdlib.h>
5 #include "logging.h"
6
7 int main(int argc, char *argv[])
8 {
9         unsigned int i;
10         struct ntdb_context *ntdb;
11         unsigned char *buffer;
12         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
13                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
14         NTDB_DATA key = ntdb_mkdata("key", 3);
15         NTDB_DATA data;
16
17         buffer = malloc(1000);
18         for (i = 0; i < 1000; i++)
19                 buffer[i] = i;
20
21         plan_tests(sizeof(flags) / sizeof(flags[0]) * 20 + 1);
22
23         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
24                 ntdb = ntdb_open("run-55-transaction.ntdb",
25                                  flags[i]|MAYBE_NOSYNC,
26                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
27                 ok1(ntdb);
28                 if (!ntdb)
29                         continue;
30
31                 ok1(ntdb_transaction_start(ntdb) == 0);
32                 data.dptr = buffer;
33                 data.dsize = 1000;
34                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
35                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
36                 ok1(data.dsize == 1000);
37                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
38                 free(data.dptr);
39
40                 /* Cancelling a transaction means no store */
41                 ntdb_transaction_cancel(ntdb);
42                 ok1(ntdb->file->allrecord_lock.count == 0
43                     && ntdb->file->num_lockrecs == 0);
44                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
45                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_ERR_NOEXIST);
46
47                 /* Commit the transaction. */
48                 ok1(ntdb_transaction_start(ntdb) == 0);
49                 data.dptr = buffer;
50                 data.dsize = 1000;
51                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
52                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
53                 ok1(data.dsize == 1000);
54                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
55                 free(data.dptr);
56                 ok1(ntdb_transaction_commit(ntdb) == 0);
57                 ok1(ntdb->file->allrecord_lock.count == 0
58                     && ntdb->file->num_lockrecs == 0);
59                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
60                 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
61                 ok1(data.dsize == 1000);
62                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
63                 free(data.dptr);
64
65                 ntdb_close(ntdb);
66         }
67
68         ok1(tap_log_messages == 0);
69         free(buffer);
70         return exit_status();
71 }