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