]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-coalesce.c
tdb2: new tests, and new fixes.
[ccan] / ccan / tdb2 / test / run-coalesce.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/check.c>
6 #include <ccan/tap/tap.h>
7 #include "logging.h"
8 #include "layout.h"
9
10 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
11 {
12         struct tdb_free_record f;
13
14         if (tdb_read_convert(tdb, off, &f, sizeof(f)) != 0)
15                 return TDB_OFF_ERR;
16         if (f.magic != TDB_FREE_MAGIC)
17                 return TDB_OFF_ERR;
18         return f.data_len;
19 }
20
21 int main(int argc, char *argv[])
22 {
23         tdb_off_t list;
24         struct tdb_context *tdb;
25         struct tdb_layout *layout;
26         struct tdb_data data, key;
27         tdb_len_t total;
28         unsigned int i;
29
30         /* FIXME: Test TDB_CONVERT */
31
32         plan_tests(62);
33         data.dptr = (void *)"world";
34         data.dsize = 5;
35         key.dptr = (void *)"hello";
36         key.dsize = 5;
37
38         /* No coalescing can be done due to EOF */
39         layout = new_tdb_layout();
40         tdb_layout_add_hashtable(layout, 12, 0);
41         tdb_layout_add_freetable(layout, 1, 16, 12, 0);
42         tdb_layout_add_free(layout, 1024);
43         tdb = tdb_layout_get(layout);
44         tdb->log = tap_log_fn;
45         ok1(tdb_check(tdb, NULL, NULL) == 0);
46         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
47
48         /* Figure out which list free entry is. */
49         list = size_to_bucket(tdb, 1024);
50         /* Lock and fail to coalesce. */
51         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
52         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
53         ok1(coalesce(tdb, layout->elem[2].base.off, list, 1024) == 0);
54         tdb_unlock_free_list(tdb, list);
55         tdb_unlock_list(tdb, 0, F_WRLCK);
56         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
57         ok1(tdb_check(tdb, NULL, NULL) == 0);
58         tdb_close(tdb);
59
60         /* No coalescing can be done due to used record */
61         layout = new_tdb_layout();
62         tdb_layout_add_hashtable(layout, 12, 0);
63         tdb_layout_add_freetable(layout, 1, 16, 12, 0);
64         tdb_layout_add_free(layout, 1024);
65         tdb_layout_add_used(layout, key, data, 6);
66         tdb = tdb_layout_get(layout);
67         tdb->log = tap_log_fn;
68         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
69         ok1(tdb_check(tdb, NULL, NULL) == 0);
70
71         /* Figure out which list free entry is. */
72         list = size_to_bucket(tdb, 1024);
73         /* Lock and fail to coalesce. */
74         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
75         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
76         ok1(coalesce(tdb, layout->elem[2].base.off, list, 1024) == 0);
77         tdb_unlock_free_list(tdb, list);
78         tdb_unlock_list(tdb, 0, F_WRLCK);
79         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
80         ok1(tdb_check(tdb, NULL, NULL) == 0);
81         tdb_close(tdb);
82
83         /* Coalescing can be done due to two free records, then EOF */
84         layout = new_tdb_layout();
85         tdb_layout_add_hashtable(layout, 12, 0);
86         tdb_layout_add_freetable(layout, 1, 16, 12, 0);
87         tdb_layout_add_free(layout, 1024);
88         tdb_layout_add_free(layout, 512);
89         tdb = tdb_layout_get(layout);
90         tdb->log = tap_log_fn;
91         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
92         ok1(free_record_length(tdb, layout->elem[3].base.off) == 512);
93         ok1(tdb_check(tdb, NULL, NULL) == 0);
94
95         /* Figure out which list free entry is. */
96         list = size_to_bucket(tdb, 1024);
97         /* Lock and coalesce. */
98         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
99         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
100         ok1(coalesce(tdb, layout->elem[2].base.off, list, 1024) == 1);
101         tdb_unlock_list(tdb, 0, F_WRLCK);
102         ok1(!tdb_has_locks(tdb));
103         ok1(free_record_length(tdb, layout->elem[2].base.off)
104             == 1024 + sizeof(struct tdb_used_record) + 512);
105         ok1(tdb_check(tdb, NULL, NULL) == 0);
106         tdb_close(tdb);
107
108         /* Coalescing can be done due to two free records, then data */
109         layout = new_tdb_layout();
110         tdb_layout_add_hashtable(layout, 12, 0);
111         tdb_layout_add_freetable(layout, 1, 16, 12, 0);
112         tdb_layout_add_free(layout, 1024);
113         tdb_layout_add_free(layout, 512);
114         tdb_layout_add_used(layout, key, data, 6);
115         tdb = tdb_layout_get(layout);
116         tdb->log = tap_log_fn;
117         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
118         ok1(free_record_length(tdb, layout->elem[3].base.off) == 512);
119         ok1(tdb_check(tdb, NULL, NULL) == 0);
120
121         /* Figure out which list free entry is. */
122         list = size_to_bucket(tdb, 1024);
123         /* Lock and coalesce. */
124         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
125         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
126         ok1(coalesce(tdb, layout->elem[2].base.off, list, 1024) == 1);
127         tdb_unlock_list(tdb, 0, F_WRLCK);
128         ok1(!tdb_has_locks(tdb));
129         ok1(free_record_length(tdb, layout->elem[2].base.off)
130             == 1024 + sizeof(struct tdb_used_record) + 512);
131         ok1(tdb_check(tdb, NULL, NULL) == 0);
132         tdb_close(tdb);
133
134         /* Coalescing can be done due to three free records, then EOF */
135         layout = new_tdb_layout();
136         tdb_layout_add_hashtable(layout, 12, 0);
137         tdb_layout_add_freetable(layout, 1, 16, 12, 0);
138         tdb_layout_add_free(layout, 1024);
139         tdb_layout_add_free(layout, 512);
140         tdb_layout_add_free(layout, 32);
141         tdb = tdb_layout_get(layout);
142         tdb->log = tap_log_fn;
143         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1024);
144         ok1(free_record_length(tdb, layout->elem[3].base.off) == 512);
145         ok1(free_record_length(tdb, layout->elem[4].base.off) == 32);
146         ok1(tdb_check(tdb, NULL, NULL) == 0);
147
148         /* Figure out which list free entry is. */
149         list = size_to_bucket(tdb, 1024);
150         /* Lock and coalesce. */
151         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
152         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
153         ok1(coalesce(tdb, layout->elem[2].base.off, list, 1024) == 1);
154         tdb_unlock_list(tdb, 0, F_WRLCK);
155         ok1(!tdb_has_locks(tdb));
156         ok1(free_record_length(tdb, layout->elem[2].base.off)
157             == 1024 + sizeof(struct tdb_used_record) + 512
158             + sizeof(struct tdb_used_record) + 32);
159         ok1(tdb_check(tdb, NULL, NULL) == 0);
160         tdb_close(tdb);
161
162         /* Coalescing across two zones. */
163         layout = new_tdb_layout();
164         tdb_layout_add_hashtable(layout, 12, 0);
165         tdb_layout_add_freetable(layout, 2, 16, 12, 0);
166         tdb_layout_add_free(layout, 32768);
167         tdb_layout_add_free(layout, 30000);
168         tdb = tdb_layout_get(layout);
169         tdb->log = tap_log_fn;
170         ok1(free_record_length(tdb, layout->elem[2].base.off) == 32768);
171         ok1(zone_of(tdb, layout->elem[2].base.off) == 0);
172         ok1(free_record_length(tdb, layout->elem[3].base.off) == 30000);
173         ok1(zone_of(tdb, layout->elem[3].base.off) == 1);
174         ok1(tdb_check(tdb, NULL, NULL) == 0);
175
176         /* Figure out which list free entry is. */
177         list = size_to_bucket(tdb, 32768);
178         /* Lock and coalesce. */
179         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
180         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
181         ok1(coalesce(tdb, layout->elem[2].base.off, list, 32768) == 1);
182         tdb_unlock_list(tdb, 0, F_WRLCK);
183         ok1(!tdb_has_locks(tdb));
184         ok1(free_record_length(tdb, layout->elem[2].base.off)
185             == 32768 + sizeof(struct tdb_used_record) + 30000);
186         ok1(tdb_check(tdb, NULL, NULL) == 0);
187         tdb_close(tdb);
188
189         /* Coalescing many across many zones. */
190         layout = new_tdb_layout();
191         tdb_layout_add_hashtable(layout, 12, 0);
192         tdb_layout_add_freetable(layout, 8, 16, 12, 0);
193         total = 0;
194         for (i = 4; i < 16; i++) {
195                 tdb_layout_add_free(layout, 1 << i);
196                 total += sizeof(struct tdb_used_record) + (1 << i);
197         }
198         total -= sizeof(struct tdb_used_record);
199         tdb = tdb_layout_get(layout);
200         tdb->log = tap_log_fn;
201         ok1(free_record_length(tdb, layout->elem[2].base.off) == 1 << 4);
202         ok1(tdb_check(tdb, NULL, NULL) == 0);
203
204         /* Figure out which list free entry is. */
205         list = size_to_bucket(tdb, 1 << 4);
206         /* Lock and coalesce. */
207         ok1(tdb_lock_list(tdb, 0, F_WRLCK, TDB_LOCK_WAIT) == 0);
208         ok1(tdb_lock_free_list(tdb, list, TDB_LOCK_WAIT) == 0);
209         ok1(coalesce(tdb, layout->elem[2].base.off, list, 1 << 4) == 1);
210         tdb_unlock_list(tdb, 0, F_WRLCK);
211         ok1(!tdb_has_locks(tdb));
212         ok1(free_record_length(tdb, layout->elem[2].base.off) == total);
213         ok1(tdb_check(tdb, NULL, NULL) == 0);
214         tdb_close(tdb);
215
216         ok1(tap_log_messages == 0);
217         return exit_status();
218 }