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 tdb_fetch(tdb, key, &d);
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) == TDB_ERR_NOEXIST);
54 ok1(tdb_check(tdb, NULL, NULL) == 0);
56 /* Insert should succeed. */
57 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
58 ok1(tdb_check(tdb, NULL, NULL) == 0);
60 /* Delete should succeed. */
61 ok1(tdb_delete(tdb, key) == 0);
62 ok1(tdb_check(tdb, NULL, NULL) == 0);
64 /* Re-add it, then add collision. */
65 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
67 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
68 ok1(tdb_check(tdb, NULL, NULL) == 0);
71 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
72 ok1(d.dsize == data.dsize);
75 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
76 ok1(d.dsize == data.dsize);
79 /* Delete second one. */
81 ok1(tdb_delete(tdb, key) == 0);
82 ok1(tdb_check(tdb, NULL, NULL) == 0);
85 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
86 ok1(tdb_check(tdb, NULL, NULL) == 0);
88 /* Now, try deleting first one. */
90 ok1(tdb_delete(tdb, key) == 0);
91 ok1(tdb_check(tdb, NULL, NULL) == 0);
93 /* Can still find second? */
95 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
96 ok1(d.dsize == data.dsize);
99 /* Now, this will be ideally placed. */
101 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
102 ok1(tdb_check(tdb, NULL, NULL) == 0);
104 /* This will collide with both. */
106 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
108 /* We can still find them all, right? */
109 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
110 ok1(d.dsize == data.dsize);
113 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
114 ok1(d.dsize == data.dsize);
117 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
118 ok1(d.dsize == data.dsize);
121 /* And if we delete val + 1, that val + 2 should not move! */
123 ok1(tdb_delete(tdb, key) == 0);
124 ok1(tdb_check(tdb, NULL, NULL) == 0);
127 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
128 ok1(d.dsize == data.dsize);
131 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
132 ok1(d.dsize == data.dsize);
135 /* Delete those two, so we are empty. */
136 ok1(tdb_delete(tdb, key) == 0);
138 ok1(tdb_delete(tdb, key) == 0);
140 ok1(tdb_check(tdb, NULL, NULL) == 0);
143 int main(int argc, char *argv[])
146 struct tdb_context *tdb;
147 uint64_t seed = 16014841315512641303ULL;
148 union tdb_attribute clash_hattr
149 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
150 .hash_fn = clash } };
151 union tdb_attribute fixed_hattr
152 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
153 .hash_fn = fixedhash,
154 .hash_private = &seed } };
155 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
156 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
157 TDB_NOMMAP|TDB_CONVERT };
158 /* These two values gave trouble before. */
159 int vals[] = { 755, 837 };
161 clash_hattr.base.next = &tap_log_attr;
162 fixed_hattr.base.next = &tap_log_attr;
164 plan_tests(sizeof(flags) / sizeof(flags[0])
165 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
166 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
167 tdb = tdb_open("run-13-delete.tdb", flags[i],
168 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
173 /* Check start of hash table. */
176 /* Check end of hash table. */
177 test_val(tdb, -1ULL);
179 /* Check mixed bitpattern. */
180 test_val(tdb, 0x123456789ABCDEF0ULL);
182 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
185 /* Deleting these entries in the db gave problems. */
186 tdb = tdb_open("run-13-delete.tdb", flags[i],
187 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
192 ok1(store_records(tdb));
193 ok1(tdb_check(tdb, NULL, NULL) == 0);
194 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
197 key.dptr = (unsigned char *)&vals[j];
198 key.dsize = sizeof(vals[j]);
199 ok1(tdb_delete(tdb, key) == 0);
200 ok1(tdb_check(tdb, NULL, NULL) == 0);
205 ok1(tap_log_messages == 0);
206 return exit_status();