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