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>
11 /* We rig the hash so adjacent-numbered records always clash. */
12 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
14 return ((uint64_t)*(unsigned int *)key)
15 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
18 /* We use the same seed which we saw a failure on. */
19 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
21 return hash64_stable((const unsigned char *)key, len,
25 static bool store_records(struct tdb_context *tdb)
28 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
29 struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
31 for (i = 0; i < 1000; i++) {
32 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
34 d = tdb_fetch(tdb, key);
35 if (d.dsize != data.dsize)
37 if (memcmp(d.dptr, data.dptr, d.dsize) != 0)
44 static void test_val(struct tdb_context *tdb, uint64_t val)
47 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
48 struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
50 /* Insert an entry, then delete it. */
52 /* Delete should fail. */
53 ok1(tdb_delete(tdb, key) == -1);
54 ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
55 ok1(tdb_check(tdb, NULL, NULL) == 0);
57 /* Insert should succeed. */
58 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
59 ok1(tdb_check(tdb, NULL, NULL) == 0);
61 /* Delete should succeed. */
62 ok1(tdb_delete(tdb, key) == 0);
63 ok1(tdb_check(tdb, NULL, NULL) == 0);
65 /* Re-add it, then add collision. */
66 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
68 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
69 ok1(tdb_check(tdb, NULL, NULL) == 0);
72 d = tdb_fetch(tdb, key);
73 ok1(d.dsize == data.dsize);
76 d = tdb_fetch(tdb, key);
77 ok1(d.dsize == data.dsize);
80 /* Delete second one. */
82 ok1(tdb_delete(tdb, key) == 0);
83 ok1(tdb_check(tdb, NULL, NULL) == 0);
86 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
87 ok1(tdb_check(tdb, NULL, NULL) == 0);
89 /* Now, try deleting first one. */
91 ok1(tdb_delete(tdb, key) == 0);
92 ok1(tdb_check(tdb, NULL, NULL) == 0);
94 /* Can still find second? */
96 d = tdb_fetch(tdb, key);
97 ok1(d.dsize == data.dsize);
100 /* Now, this will be ideally placed. */
102 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
103 ok1(tdb_check(tdb, NULL, NULL) == 0);
105 /* This will collide with both. */
107 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
109 /* We can still find them all, right? */
110 d = tdb_fetch(tdb, key);
111 ok1(d.dsize == data.dsize);
114 d = tdb_fetch(tdb, key);
115 ok1(d.dsize == data.dsize);
118 d = tdb_fetch(tdb, key);
119 ok1(d.dsize == data.dsize);
122 /* And if we delete val + 1, that val + 2 should not move! */
124 ok1(tdb_delete(tdb, key) == 0);
125 ok1(tdb_check(tdb, NULL, NULL) == 0);
128 d = tdb_fetch(tdb, key);
129 ok1(d.dsize == data.dsize);
132 d = tdb_fetch(tdb, key);
133 ok1(d.dsize == data.dsize);
136 /* Delete those two, so we are empty. */
137 ok1(tdb_delete(tdb, key) == 0);
139 ok1(tdb_delete(tdb, key) == 0);
141 ok1(tdb_check(tdb, NULL, NULL) == 0);
144 int main(int argc, char *argv[])
147 struct tdb_context *tdb;
148 uint64_t seed = 16014841315512641303ULL;
149 union tdb_attribute clash_hattr
150 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
151 .hash_fn = clash } };
152 union tdb_attribute fixed_hattr
153 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
154 .hash_fn = fixedhash,
155 .hash_private = &seed } };
156 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
157 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
158 TDB_NOMMAP|TDB_CONVERT };
159 /* These two values gave trouble before. */
160 int vals[] = { 755, 837 };
162 clash_hattr.base.next = &tap_log_attr;
163 fixed_hattr.base.next = &tap_log_attr;
165 plan_tests(sizeof(flags) / sizeof(flags[0])
166 * (32 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
167 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
168 tdb = tdb_open("run-13-delete.tdb", flags[i],
169 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
174 /* Check start of hash table. */
177 /* Check end of hash table. */
178 test_val(tdb, -1ULL);
180 /* Check mixed bitpattern. */
181 test_val(tdb, 0x123456789ABCDEF0ULL);
183 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
186 /* Deleting these entries in the db gave problems. */
187 tdb = tdb_open("run-13-delete.tdb", flags[i],
188 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
193 ok1(store_records(tdb));
194 ok1(tdb_check(tdb, NULL, NULL) == 0);
195 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
198 key.dptr = (unsigned char *)&vals[j];
199 key.dsize = sizeof(vals[j]);
200 ok1(tdb_delete(tdb, key) == 0);
201 ok1(tdb_check(tdb, NULL, NULL) == 0);
206 ok1(tap_log_messages == 0);
207 return exit_status();