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/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
12 /* We rig the hash so adjacent-numbered records always clash. */
13 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
15 return ((uint64_t)*(unsigned int *)key)
16 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
19 /* We use the same seed which we saw a failure on. */
20 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
22 return hash64_stable((const unsigned char *)key, len,
26 static bool store_records(struct tdb_context *tdb)
29 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
30 struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
32 for (i = 0; i < 1000; i++) {
33 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
35 tdb_fetch(tdb, key, &d);
36 if (d.dsize != data.dsize)
38 if (memcmp(d.dptr, data.dptr, d.dsize) != 0)
45 static void test_val(struct tdb_context *tdb, uint64_t val)
48 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
49 struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
51 /* Insert an entry, then delete it. */
53 /* Delete should fail. */
54 ok1(tdb_delete(tdb, key) == 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 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
73 ok1(d.dsize == data.dsize);
76 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
111 ok1(d.dsize == data.dsize);
114 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
115 ok1(d.dsize == data.dsize);
118 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
129 ok1(d.dsize == data.dsize);
132 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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 * (39 * 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->file || (tdb->file->allrecord_lock.count == 0
184 && tdb->file->num_lockrecs == 0));
187 /* Deleting these entries in the db gave problems. */
188 tdb = tdb_open("run-13-delete.tdb", flags[i],
189 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
194 ok1(store_records(tdb));
195 ok1(tdb_check(tdb, NULL, NULL) == 0);
196 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
199 key.dptr = (unsigned char *)&vals[j];
200 key.dsize = sizeof(vals[j]);
201 ok1(tdb_delete(tdb, key) == 0);
202 ok1(tdb_check(tdb, NULL, NULL) == 0);
207 ok1(tap_log_messages == 0);
208 return exit_status();