tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-25-hashoverload.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 static uint64_t badhash(const void *key, size_t len, uint64_t seed, void *priv)
14 {
15         return 0;
16 }
17
18 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
19 {
20         if (p)
21                 return tdb_delete(tdb, key);
22         return 0;
23 }
24
25 int main(int argc, char *argv[])
26 {
27         unsigned int i, j;
28         struct tdb_context *tdb;
29         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
30         struct tdb_data dbuf = { (unsigned char *)&j, sizeof(j) };
31         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
32                                                 .fn = badhash } };
33         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
34                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
35                         TDB_NOMMAP|TDB_CONVERT,
36         };
37
38         hattr.base.next = &tap_log_attr;
39
40         plan_tests(6883);
41         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
42                 struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
43
44                 tdb = tdb_open("run-25-hashoverload.tdb", flags[i],
45                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
46                 ok1(tdb);
47                 if (!tdb)
48                         continue;
49
50                 /* Fill a group. */
51                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
52                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
53                 }
54                 ok1(tdb_check(tdb, NULL, NULL) == 0);
55
56                 /* Now store one last value: should form chain. */
57                 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
58                 ok1(tdb_check(tdb, NULL, NULL) == 0);
59
60                 /* Check we can find them all. */
61                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS) + 1; j++) {
62                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
63                         ok1(d.dsize == sizeof(j));
64                         ok1(d.dptr != NULL);
65                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
66                         free(d.dptr);
67                 }
68
69                 /* Now add a *lot* more. */
70                 for (j = (1 << TDB_HASH_GROUP_BITS) + 1;
71                      j < (16 << TDB_HASH_GROUP_BITS);
72                      j++) {
73                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
74                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
75                         ok1(d.dsize == sizeof(j));
76                         ok1(d.dptr != NULL);
77                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
78                         free(d.dptr);
79                 }
80                 ok1(tdb_check(tdb, NULL, NULL) == 0);
81
82                 /* Traverse through them. */
83                 ok1(tdb_traverse(tdb, trav, NULL) == j);
84
85                 /* Empty the first chain-worth. */
86                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++)
87                         ok1(tdb_delete(tdb, key) == 0);
88
89                 ok1(tdb_check(tdb, NULL, NULL) == 0);
90
91                 for (j = (1 << TDB_HASH_GROUP_BITS);
92                      j < (16 << TDB_HASH_GROUP_BITS);
93                      j++) {
94                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
95                         ok1(d.dsize == sizeof(j));
96                         ok1(d.dptr != NULL);
97                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
98                         free(d.dptr);
99                 }
100
101                 /* Traverse through them. */
102                 ok1(tdb_traverse(tdb, trav, NULL)
103                     == (15 << TDB_HASH_GROUP_BITS));
104
105                 /* Re-add */
106                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
107                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
108                 }
109                 ok1(tdb_check(tdb, NULL, NULL) == 0);
110
111                 /* Now try deleting as we go. */
112                 ok1(tdb_traverse(tdb, trav, trav)
113                     == (16 << TDB_HASH_GROUP_BITS));
114                 ok1(tdb_check(tdb, NULL, NULL) == 0);
115                 ok1(tdb_traverse(tdb, trav, NULL) == 0);
116                 tdb_close(tdb);
117         }
118
119         ok1(tap_log_messages == 0);
120         return exit_status();
121 }