]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-13-delete.c
tdb2: remove tdb_traverse_read
[ccan] / ccan / tdb2 / test / run-13-delete.c
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>
9 #include "logging.h"
10
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)
13 {
14         return ((uint64_t)*(unsigned int *)key)
15                 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
16 }
17
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)
20 {
21         return hash64_stable((const unsigned char *)key, len,
22                              *(uint64_t *)p);
23 }
24
25 static bool store_records(struct tdb_context *tdb)
26 {
27         int i;
28         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
29         struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
30
31         for (i = 0; i < 1000; i++) {
32                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
33                         return false;
34                 d = tdb_fetch(tdb, key);
35                 if (d.dsize != data.dsize)
36                         return false;
37                 if (memcmp(d.dptr, data.dptr, d.dsize) != 0)
38                         return false;
39                 free(d.dptr);
40         }
41         return true;
42 }
43
44 static void test_val(struct tdb_context *tdb, uint64_t val)
45 {
46         uint64_t v;
47         struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
48         struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
49
50         /* Insert an entry, then delete it. */
51         v = val;
52         /* Delete should fail. */
53         ok1(tdb_delete(tdb, key) == -1);
54         ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
55         ok1(tdb_check(tdb, NULL, NULL) == 0);
56
57         /* Insert should succeed. */
58         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
59         ok1(tdb_check(tdb, NULL, NULL) == 0);
60
61         /* Delete should succeed. */
62         ok1(tdb_delete(tdb, key) == 0);
63         ok1(tdb_check(tdb, NULL, NULL) == 0);
64
65         /* Re-add it, then add collision. */
66         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
67         v = val + 1;
68         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
69         ok1(tdb_check(tdb, NULL, NULL) == 0);
70
71         /* Can find both? */
72         d = tdb_fetch(tdb, key);
73         ok1(d.dsize == data.dsize);
74         free(d.dptr);
75         v = val;
76         d = tdb_fetch(tdb, key);
77         ok1(d.dsize == data.dsize);
78         free(d.dptr);
79
80         /* Delete second one. */
81         v = val + 1;
82         ok1(tdb_delete(tdb, key) == 0);
83         ok1(tdb_check(tdb, NULL, NULL) == 0);
84
85         /* Re-add */
86         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
87         ok1(tdb_check(tdb, NULL, NULL) == 0);
88
89         /* Now, try deleting first one. */
90         v = val;
91         ok1(tdb_delete(tdb, key) == 0);
92         ok1(tdb_check(tdb, NULL, NULL) == 0);
93
94         /* Can still find second? */
95         v = val + 1;
96         d = tdb_fetch(tdb, key);
97         ok1(d.dsize == data.dsize);
98         free(d.dptr);
99
100         /* Now, this will be ideally placed. */
101         v = val + 2;
102         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
103         ok1(tdb_check(tdb, NULL, NULL) == 0);
104
105         /* This will collide with both. */
106         v = val;
107         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
108
109         /* We can still find them all, right? */
110         d = tdb_fetch(tdb, key);
111         ok1(d.dsize == data.dsize);
112         free(d.dptr);
113         v = val + 1;
114         d = tdb_fetch(tdb, key);
115         ok1(d.dsize == data.dsize);
116         free(d.dptr);
117         v = val + 2;
118         d = tdb_fetch(tdb, key);
119         ok1(d.dsize == data.dsize);
120         free(d.dptr);
121
122         /* And if we delete val + 1, that val + 2 should not move! */
123         v = val + 1;
124         ok1(tdb_delete(tdb, key) == 0);
125         ok1(tdb_check(tdb, NULL, NULL) == 0);
126
127         v = val;
128         d = tdb_fetch(tdb, key);
129         ok1(d.dsize == data.dsize);
130         free(d.dptr);
131         v = val + 2;
132         d = tdb_fetch(tdb, key);
133         ok1(d.dsize == data.dsize);
134         free(d.dptr);
135
136         /* Delete those two, so we are empty. */
137         ok1(tdb_delete(tdb, key) == 0);
138         v = val;
139         ok1(tdb_delete(tdb, key) == 0);
140
141         ok1(tdb_check(tdb, NULL, NULL) == 0);
142 }
143
144 int main(int argc, char *argv[])
145 {
146         unsigned int i, j;
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 };
161
162         clash_hattr.base.next = &tap_log_attr;
163         fixed_hattr.base.next = &tap_log_attr;
164
165         plan_tests(sizeof(flags) / sizeof(flags[0])
166                    * (32 * 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);
170                 ok1(tdb);
171                 if (!tdb)
172                         continue;
173
174                 /* Check start of hash table. */
175                 test_val(tdb, 0);
176
177                 /* Check end of hash table. */
178                 test_val(tdb, -1ULL);
179
180                 /* Check mixed bitpattern. */
181                 test_val(tdb, 0x123456789ABCDEF0ULL);
182
183                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
184                 tdb_close(tdb);
185
186                 /* Deleting these entries in the db gave problems. */
187                 tdb = tdb_open("run-13-delete.tdb", flags[i],
188                                O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
189                 ok1(tdb);
190                 if (!tdb)
191                         continue;
192
193                 ok1(store_records(tdb));
194                 ok1(tdb_check(tdb, NULL, NULL) == 0);
195                 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
196                         struct tdb_data key;
197
198                         key.dptr = (unsigned char *)&vals[j];
199                         key.dsize = sizeof(vals[j]);
200                         ok1(tdb_delete(tdb, key) == 0);
201                         ok1(tdb_check(tdb, NULL, NULL) == 0);
202                 }
203                 tdb_close(tdb);
204         }
205
206         ok1(tap_log_messages == 0);
207         return exit_status();
208 }