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 data = { (unsigned char *)&i, sizeof(i) };
31 for (i = 0; i < 1000; i++) {
32 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
34 if (tdb_fetch(tdb, key).dsize != data.dsize)
40 static void test_val(struct tdb_context *tdb, uint64_t val)
43 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
44 struct tdb_data data = { (unsigned char *)&v, sizeof(v) };
46 /* Insert an entry, then delete it. */
48 /* Delete should fail. */
49 ok1(tdb_delete(tdb, key) == -1);
50 ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
51 ok1(tdb_check(tdb, NULL, NULL) == 0);
53 /* Insert should succeed. */
54 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
55 ok1(tdb_check(tdb, NULL, NULL) == 0);
57 /* Delete should succeed. */
58 ok1(tdb_delete(tdb, key) == 0);
59 ok1(tdb_check(tdb, NULL, NULL) == 0);
61 /* Re-add it, then add collision. */
62 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
64 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
65 ok1(tdb_check(tdb, NULL, NULL) == 0);
68 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
70 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
72 /* Delete second one. */
74 ok1(tdb_delete(tdb, key) == 0);
75 ok1(tdb_check(tdb, NULL, NULL) == 0);
78 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
79 ok1(tdb_check(tdb, NULL, NULL) == 0);
81 /* Now, try deleting first one. */
83 ok1(tdb_delete(tdb, key) == 0);
84 ok1(tdb_check(tdb, NULL, NULL) == 0);
86 /* Can still find second? */
88 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
90 /* Now, this will be ideally placed. */
92 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
93 ok1(tdb_check(tdb, NULL, NULL) == 0);
95 /* This will collide with both. */
97 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
99 /* We can still find them all, right? */
100 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
102 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
104 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
106 /* And if we delete val + 1, that val + 2 should not move! */
108 ok1(tdb_delete(tdb, key) == 0);
109 ok1(tdb_check(tdb, NULL, NULL) == 0);
112 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
114 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
116 /* Delete those two, so we are empty. */
117 ok1(tdb_delete(tdb, key) == 0);
119 ok1(tdb_delete(tdb, key) == 0);
121 ok1(tdb_check(tdb, NULL, NULL) == 0);
124 int main(int argc, char *argv[])
127 struct tdb_context *tdb;
128 uint64_t seed = 16014841315512641303ULL;
129 union tdb_attribute clash_hattr
130 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
131 .hash_fn = clash } };
132 union tdb_attribute fixed_hattr
133 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
134 .hash_fn = fixedhash,
135 .hash_private = &seed } };
136 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
137 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
138 TDB_NOMMAP|TDB_CONVERT };
139 /* These two values gave trouble before. */
140 int vals[] = { 755, 837 };
142 clash_hattr.base.next = &tap_log_attr;
143 fixed_hattr.base.next = &tap_log_attr;
145 plan_tests(sizeof(flags) / sizeof(flags[0])
146 * (32 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
147 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
148 tdb = tdb_open("run-13-delete.tdb", flags[i],
149 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
154 /* Check start of hash table. */
157 /* Check end of hash table. */
158 test_val(tdb, -1ULL);
160 /* Check mixed bitpattern. */
161 test_val(tdb, 0x123456789ABCDEF0ULL);
163 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
166 /* Deleting these entries in the db gave problems. */
167 tdb = tdb_open("run-13-delete.tdb", flags[i],
168 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
173 ok1(store_records(tdb));
174 ok1(tdb_check(tdb, NULL, NULL) == 0);
175 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
178 key.dptr = (unsigned char *)&vals[j];
179 key.dsize = sizeof(vals[j]);
180 ok1(tdb_delete(tdb, key) == 0);
181 ok1(tdb_check(tdb, NULL, NULL) == 0);
186 ok1(tap_log_messages == 0);
187 return exit_status();