]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-13-delete.c
tdb2: feature support.
[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                 tdb_fetch(tdb, key, &d);
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) == TDB_ERR_NOEXIST);
54         ok1(tdb_check(tdb, NULL, NULL) == 0);
55
56         /* Insert should succeed. */
57         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
58         ok1(tdb_check(tdb, NULL, NULL) == 0);
59
60         /* Delete should succeed. */
61         ok1(tdb_delete(tdb, key) == 0);
62         ok1(tdb_check(tdb, NULL, NULL) == 0);
63
64         /* Re-add it, then add collision. */
65         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
66         v = val + 1;
67         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
68         ok1(tdb_check(tdb, NULL, NULL) == 0);
69
70         /* Can find both? */
71         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
72         ok1(d.dsize == data.dsize);
73         free(d.dptr);
74         v = val;
75         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
76         ok1(d.dsize == data.dsize);
77         free(d.dptr);
78
79         /* Delete second one. */
80         v = val + 1;
81         ok1(tdb_delete(tdb, key) == 0);
82         ok1(tdb_check(tdb, NULL, NULL) == 0);
83
84         /* Re-add */
85         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
86         ok1(tdb_check(tdb, NULL, NULL) == 0);
87
88         /* Now, try deleting first one. */
89         v = val;
90         ok1(tdb_delete(tdb, key) == 0);
91         ok1(tdb_check(tdb, NULL, NULL) == 0);
92
93         /* Can still find second? */
94         v = val + 1;
95         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
96         ok1(d.dsize == data.dsize);
97         free(d.dptr);
98
99         /* Now, this will be ideally placed. */
100         v = val + 2;
101         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
102         ok1(tdb_check(tdb, NULL, NULL) == 0);
103
104         /* This will collide with both. */
105         v = val;
106         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
107
108         /* We can still find them all, right? */
109         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
110         ok1(d.dsize == data.dsize);
111         free(d.dptr);
112         v = val + 1;
113         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
114         ok1(d.dsize == data.dsize);
115         free(d.dptr);
116         v = val + 2;
117         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
118         ok1(d.dsize == data.dsize);
119         free(d.dptr);
120
121         /* And if we delete val + 1, that val + 2 should not move! */
122         v = val + 1;
123         ok1(tdb_delete(tdb, key) == 0);
124         ok1(tdb_check(tdb, NULL, NULL) == 0);
125
126         v = val;
127         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
128         ok1(d.dsize == data.dsize);
129         free(d.dptr);
130         v = val + 2;
131         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
132         ok1(d.dsize == data.dsize);
133         free(d.dptr);
134
135         /* Delete those two, so we are empty. */
136         ok1(tdb_delete(tdb, key) == 0);
137         v = val;
138         ok1(tdb_delete(tdb, key) == 0);
139
140         ok1(tdb_check(tdb, NULL, NULL) == 0);
141 }
142
143 int main(int argc, char *argv[])
144 {
145         unsigned int i, j;
146         struct tdb_context *tdb;
147         uint64_t seed = 16014841315512641303ULL;
148         union tdb_attribute clash_hattr
149                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
150                               .hash_fn = clash } };
151         union tdb_attribute fixed_hattr
152                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
153                               .hash_fn = fixedhash,
154                               .hash_private = &seed } };
155         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
156                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
157                         TDB_NOMMAP|TDB_CONVERT };
158         /* These two values gave trouble before. */
159         int vals[] = { 755, 837 };
160
161         clash_hattr.base.next = &tap_log_attr;
162         fixed_hattr.base.next = &tap_log_attr;
163
164         plan_tests(sizeof(flags) / sizeof(flags[0])
165                    * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
166         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
167                 tdb = tdb_open("run-13-delete.tdb", flags[i],
168                                O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
169                 ok1(tdb);
170                 if (!tdb)
171                         continue;
172
173                 /* Check start of hash table. */
174                 test_val(tdb, 0);
175
176                 /* Check end of hash table. */
177                 test_val(tdb, -1ULL);
178
179                 /* Check mixed bitpattern. */
180                 test_val(tdb, 0x123456789ABCDEF0ULL);
181
182                 ok1(tdb->allrecord_lock.count == 0 && tdb->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 }