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