]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-20-growhash.c
tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-20-growhash.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/check.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 static uint64_t myhash(const void *key, size_t len, uint64_t seed, void *priv)
13 {
14         return *(const uint64_t *)key;
15 }
16
17 static void add_bits(uint64_t *val, unsigned new, unsigned new_bits,
18                      unsigned *done)
19 {
20         *done += new_bits;
21         *val |= ((uint64_t)new << (64 - *done));
22 }
23
24 static uint64_t make_key(unsigned topgroup, unsigned topbucket,
25                          unsigned subgroup1, unsigned subbucket1,
26                          unsigned subgroup2, unsigned subbucket2)
27 {
28         uint64_t key = 0;
29         unsigned done = 0;
30
31         add_bits(&key, topgroup, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS,
32                  &done);
33         add_bits(&key, topbucket, TDB_HASH_GROUP_BITS, &done);
34         add_bits(&key, subgroup1, TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS,
35                  &done);
36         add_bits(&key, subbucket1, TDB_HASH_GROUP_BITS, &done);
37         add_bits(&key, subgroup2, TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS,
38                  &done);
39         add_bits(&key, subbucket2, TDB_HASH_GROUP_BITS, &done);
40         return key;
41 }
42
43 int main(int argc, char *argv[])
44 {
45         unsigned int i, j;
46         struct tdb_context *tdb;
47         uint64_t kdata;
48         struct tdb_used_record rec;
49         struct tdb_data key = { (unsigned char *)&kdata, sizeof(kdata) };
50         struct tdb_data dbuf = { (unsigned char *)&kdata, sizeof(kdata) };
51         union tdb_attribute hattr = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
52                                                 .fn = myhash } };
53         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
54                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
55                         TDB_NOMMAP|TDB_CONVERT,
56         };
57
58         hattr.base.next = &tap_log_attr;
59
60         plan_tests(sizeof(flags) / sizeof(flags[0])
61                    * (9 + (20 + 2 * ((1 << TDB_HASH_GROUP_BITS) - 2))
62                       * (1 << TDB_HASH_GROUP_BITS)) + 1);
63         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
64                 struct hash_info h;
65
66                 tdb = tdb_open("run-04-basichash.tdb", flags[i],
67                                O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
68                 ok1(tdb);
69                 if (!tdb)
70                         continue;
71
72                 /* Fill a group. */
73                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
74                         kdata = make_key(0, j, 0, 0, 0, 0);
75                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
76                 }
77                 ok1(tdb_check(tdb, NULL, NULL) == 0);
78
79                 /* Check first still exists. */
80                 kdata = make_key(0, 0, 0, 0, 0, 0);
81                 ok1(find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL) != 0);
82                 /* Should have created correct hash. */
83                 ok1(h.h == tdb_hash(tdb, key.dptr, key.dsize));
84                 /* Should have located space in group 0, bucket 0. */
85                 ok1(h.group_start == offsetof(struct tdb_header, hashtable));
86                 ok1(h.home_bucket == 0);
87                 ok1(h.found_bucket == 0);
88                 ok1(h.hash_used == TDB_TOPLEVEL_HASH_BITS);
89                 /* Entire group should be full! */
90                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++)
91                         ok1(h.group[j] != 0);
92
93                 ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
94                                       F_RDLCK) == 0);
95
96                 /* Now, add one more to each should expand (that) bucket. */
97                 for (j = 0; j < (1 << TDB_HASH_GROUP_BITS); j++) {
98                         unsigned int k;
99                         kdata = make_key(0, j, 0, 1, 0, 0);
100                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
101                         ok1(tdb_check(tdb, NULL, NULL) == 0);
102
103                         ok1(find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL));
104                         /* Should have created correct hash. */
105                         ok1(h.h == tdb_hash(tdb, key.dptr, key.dsize));
106                         /* Should have moved to subhash */
107                         ok1(h.group_start >= sizeof(struct tdb_header));
108                         ok1(h.home_bucket == 1);
109                         ok1(h.found_bucket == 1);
110                         ok1(h.hash_used == TDB_TOPLEVEL_HASH_BITS
111                             + TDB_SUBLEVEL_HASH_BITS);
112                         ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
113                                               F_RDLCK) == 0);
114
115                         /* Keep adding, make it expand again. */
116                         for (k = 2; k < (1 << TDB_HASH_GROUP_BITS); k++) {
117                                 kdata = make_key(0, j, 0, k, 0, 0);
118                                 ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
119                                 ok1(tdb_check(tdb, NULL, NULL) == 0);
120                         }
121
122                         /* This should tip it over to sub-sub-hash. */
123                         kdata = make_key(0, j, 0, 0, 0, 1);
124                         ok1(tdb_store(tdb, key, dbuf, TDB_INSERT) == 0);
125                         ok1(tdb_check(tdb, NULL, NULL) == 0);
126
127                         ok1(find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL));
128                         /* Should have created correct hash. */
129                         ok1(h.h == tdb_hash(tdb, key.dptr, key.dsize));
130                         /* Should have moved to subhash */
131                         ok1(h.group_start >= sizeof(struct tdb_header));
132                         ok1(h.home_bucket == 1);
133                         ok1(h.found_bucket == 1);
134                         ok1(h.hash_used == TDB_TOPLEVEL_HASH_BITS
135                             + TDB_SUBLEVEL_HASH_BITS + TDB_SUBLEVEL_HASH_BITS);
136                         ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
137                                               F_RDLCK) == 0);
138                 }
139                 tdb_close(tdb);
140         }
141
142         ok1(tap_log_messages == 0);
143         return exit_status();
144 }