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