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>
10 /* We rig the hash so adjacent-numbered records always clash. */
11 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
13 return ((uint64_t)*(unsigned int *)key)
14 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
17 /* We use the same seed which we saw a failure on. */
18 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
20 return hash64_stable((const unsigned char *)key, len,
24 static bool store_records(struct tdb_context *tdb)
27 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
28 struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
30 for (i = 0; i < 1000; i++) {
31 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
33 if (tdb_fetch(tdb, key).dsize != data.dsize)
39 static void test_val(struct tdb_context *tdb, uint64_t val)
42 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
43 struct tdb_data data = { (unsigned char *)&v, sizeof(v) };
45 /* Insert an entry, then delete it. */
47 /* Delete should fail. */
48 ok1(tdb_delete(tdb, key) == -1);
49 ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
50 ok1(tdb_check(tdb, NULL, NULL) == 0);
52 /* Insert should succeed. */
53 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
54 ok1(tdb_check(tdb, NULL, NULL) == 0);
56 /* Delete should succeed. */
57 ok1(tdb_delete(tdb, key) == 0);
58 ok1(tdb_check(tdb, NULL, NULL) == 0);
60 /* Re-add it, then add collision. */
61 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
63 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
64 ok1(tdb_check(tdb, NULL, NULL) == 0);
67 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
69 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
71 /* Delete second one. */
73 ok1(tdb_delete(tdb, key) == 0);
74 ok1(tdb_check(tdb, NULL, NULL) == 0);
77 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
78 ok1(tdb_check(tdb, NULL, NULL) == 0);
80 /* Now, try deleting first one. */
82 ok1(tdb_delete(tdb, key) == 0);
83 ok1(tdb_check(tdb, NULL, NULL) == 0);
85 /* Can still find second? */
87 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
89 /* Now, this will be ideally placed. */
91 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
92 ok1(tdb_check(tdb, NULL, NULL) == 0);
94 /* This will collide with both. */
96 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
98 /* We can still find them all, right? */
99 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
101 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
103 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
105 /* And if we delete val + 1, that val + 2 should not move! */
107 ok1(tdb_delete(tdb, key) == 0);
108 ok1(tdb_check(tdb, NULL, NULL) == 0);
111 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
113 ok1(tdb_fetch(tdb, key).dsize == data.dsize);
115 /* Delete those two, so we are empty. */
116 ok1(tdb_delete(tdb, key) == 0);
118 ok1(tdb_delete(tdb, key) == 0);
120 ok1(tdb_check(tdb, NULL, NULL) == 0);
123 int main(int argc, char *argv[])
126 struct tdb_context *tdb;
127 uint64_t seed = 16014841315512641303ULL;
128 union tdb_attribute clash_hattr
129 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
130 .hash_fn = clash } };
131 union tdb_attribute fixed_hattr
132 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
133 .hash_fn = fixedhash,
134 .hash_private = &seed } };
135 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
136 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
137 TDB_NOMMAP|TDB_CONVERT };
138 /* These two values gave trouble before. */
139 int vals[] = { 755, 837 };
141 clash_hattr.base.next = &tap_log_attr;
142 fixed_hattr.base.next = &tap_log_attr;
144 plan_tests(sizeof(flags) / sizeof(flags[0])
145 * (32 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
146 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
147 tdb = tdb_open("run-13-delete.tdb", flags[i],
148 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
153 /* Check start of hash table. */
156 /* Check end of hash table. */
157 test_val(tdb, -1ULL);
159 /* Check mixed bitpattern. */
160 test_val(tdb, 0x123456789ABCDEF0ULL);
162 ok1(!tdb_has_locks(tdb));
165 /* Deleting these entries in the db gave problems. */
166 tdb = tdb_open("run-13-delete.tdb", flags[i],
167 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
172 ok1(store_records(tdb));
173 ok1(tdb_check(tdb, NULL, NULL) == 0);
174 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
177 key.dptr = (unsigned char *)&vals[j];
178 key.dsize = sizeof(vals[j]);
179 ok1(tdb_delete(tdb, key) == 0);
180 ok1(tdb_check(tdb, NULL, NULL) == 0);
185 ok1(tap_log_messages == 0);
186 return exit_status();