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