]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-13-delete.c
tdb2: move mmap into struct tdb_file
[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 (d.dsize != data.dsize)
37                         return false;
38                 if (memcmp(d.dptr, data.dptr, d.dsize) != 0)
39                         return false;
40                 free(d.dptr);
41         }
42         return true;
43 }
44
45 static void test_val(struct tdb_context *tdb, uint64_t val)
46 {
47         uint64_t v;
48         struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
49         struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
50
51         /* Insert an entry, then delete it. */
52         v = val;
53         /* Delete should fail. */
54         ok1(tdb_delete(tdb, key) == 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         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
73         ok1(d.dsize == data.dsize);
74         free(d.dptr);
75         v = val;
76         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
111         ok1(d.dsize == data.dsize);
112         free(d.dptr);
113         v = val + 1;
114         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
115         ok1(d.dsize == data.dsize);
116         free(d.dptr);
117         v = val + 2;
118         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
129         ok1(d.dsize == data.dsize);
130         free(d.dptr);
131         v = val + 2;
132         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
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                    * (39 * 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->file || (tdb->file->allrecord_lock.count == 0
184                                    && tdb->file->num_lockrecs == 0));
185                 tdb_close(tdb);
186
187                 /* Deleting these entries in the db gave problems. */
188                 tdb = tdb_open("run-13-delete.tdb", flags[i],
189                                O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
190                 ok1(tdb);
191                 if (!tdb)
192                         continue;
193
194                 ok1(store_records(tdb));
195                 ok1(tdb_check(tdb, NULL, NULL) == 0);
196                 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
197                         struct tdb_data key;
198
199                         key.dptr = (unsigned char *)&vals[j];
200                         key.dsize = sizeof(vals[j]);
201                         ok1(tdb_delete(tdb, key) == 0);
202                         ok1(tdb_check(tdb, NULL, NULL) == 0);
203                 }
204                 tdb_close(tdb);
205         }
206
207         ok1(tap_log_messages == 0);
208         return exit_status();
209 }