]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-12-store.c
tdb2: transaction support
[ccan] / ccan / tdb2 / test / run-12-store.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/transaction.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 /* We use the same seed which we saw a failure on. */
12 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
13 {
14         return hash64_stable((const unsigned char *)key, len,
15                              *(uint64_t *)p);
16 }
17
18 static bool equal(struct tdb_data a, struct tdb_data b)
19 {
20         if (a.dsize != b.dsize)
21                 return false;
22         return memcmp(a.dptr, b.dptr, a.dsize) == 0;
23 }
24
25 int main(int argc, char *argv[])
26 {
27         unsigned int i, j;
28         struct tdb_context *tdb;
29         uint64_t seed = 16014841315512641303ULL;
30         union tdb_attribute fixed_hattr
31                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
32                               .hash_fn = fixedhash,
33                               .hash_private = &seed } };
34         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
35                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
36                         TDB_NOMMAP|TDB_CONVERT };
37         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
38         struct tdb_data data = { (unsigned char *)&j, sizeof(j) };
39
40         fixed_hattr.base.next = &tap_log_attr;
41
42         plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 500 * 2) + 1);
43         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
44                 tdb = tdb_open("run-12-store.tdb", flags[i],
45                                O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
46                 ok1(tdb);
47                 if (!tdb)
48                         continue;
49
50                 /* We seemed to lose some keys.
51                  * Insert and check they're in there! */
52                 for (j = 0; j < 500; j++) {
53                         ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
54                         ok1(equal(tdb_fetch(tdb, key), data));
55                 }
56                 tdb_close(tdb);
57         }
58
59         ok1(tap_log_messages == 0);
60         return exit_status();
61 }
62
63