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)*(const 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 (!tdb_deq(d, data))
43 static void test_val(struct tdb_context *tdb, uint64_t val)
46 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
47 struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
49 /* Insert an entry, then delete it. */
51 /* Delete should fail. */
52 ok1(tdb_delete(tdb, key) == TDB_ERR_NOEXIST);
53 ok1(tdb_check(tdb, NULL, NULL) == 0);
55 /* Insert should succeed. */
56 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
57 ok1(tdb_check(tdb, NULL, NULL) == 0);
59 /* Delete should succeed. */
60 ok1(tdb_delete(tdb, key) == 0);
61 ok1(tdb_check(tdb, NULL, NULL) == 0);
63 /* Re-add it, then add collision. */
64 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
66 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
67 ok1(tdb_check(tdb, NULL, NULL) == 0);
70 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
71 ok1(d.dsize == data.dsize);
74 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
75 ok1(d.dsize == data.dsize);
78 /* Delete second one. */
80 ok1(tdb_delete(tdb, key) == 0);
81 ok1(tdb_check(tdb, NULL, NULL) == 0);
84 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
85 ok1(tdb_check(tdb, NULL, NULL) == 0);
87 /* Now, try deleting first one. */
89 ok1(tdb_delete(tdb, key) == 0);
90 ok1(tdb_check(tdb, NULL, NULL) == 0);
92 /* Can still find second? */
94 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
95 ok1(d.dsize == data.dsize);
98 /* Now, this will be ideally placed. */
100 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
101 ok1(tdb_check(tdb, NULL, NULL) == 0);
103 /* This will collide with both. */
105 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
107 /* We can still find them all, right? */
108 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
109 ok1(d.dsize == data.dsize);
112 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
113 ok1(d.dsize == data.dsize);
116 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
117 ok1(d.dsize == data.dsize);
120 /* And if we delete val + 1, that val + 2 should not move! */
122 ok1(tdb_delete(tdb, key) == 0);
123 ok1(tdb_check(tdb, NULL, NULL) == 0);
126 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
127 ok1(d.dsize == data.dsize);
130 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
131 ok1(d.dsize == data.dsize);
134 /* Delete those two, so we are empty. */
135 ok1(tdb_delete(tdb, key) == 0);
137 ok1(tdb_delete(tdb, key) == 0);
139 ok1(tdb_check(tdb, NULL, NULL) == 0);
142 int main(int argc, char *argv[])
145 struct tdb_context *tdb;
146 uint64_t seed = 16014841315512641303ULL;
147 union tdb_attribute clash_hattr
148 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
149 .hash_fn = clash } };
150 union tdb_attribute fixed_hattr
151 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
152 .hash_fn = fixedhash,
153 .hash_private = &seed } };
154 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
155 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
156 TDB_NOMMAP|TDB_CONVERT };
157 /* These two values gave trouble before. */
158 int vals[] = { 755, 837 };
160 clash_hattr.base.next = &tap_log_attr;
161 fixed_hattr.base.next = &tap_log_attr;
163 plan_tests(sizeof(flags) / sizeof(flags[0])
164 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
165 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
166 tdb = tdb_open("run-13-delete.tdb", flags[i],
167 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
172 /* Check start of hash table. */
175 /* Check end of hash table. */
176 test_val(tdb, -1ULL);
178 /* Check mixed bitpattern. */
179 test_val(tdb, 0x123456789ABCDEF0ULL);
181 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
182 && tdb->file->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();