676825a38728f39b8e55b445a3f6c67c9791f791
[ccan] / ccan / tdb2 / test / run-03-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/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tap/tap.h>
8 #include "logging.h"
9 #include "layout.h"
10
11 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
12 {
13         struct tdb_free_record f;
14
15         if (tdb_read_convert(tdb, off, &f, sizeof(f)) != 0)
16                 return TDB_OFF_ERR;
17         if (frec_magic(&f) != TDB_FREE_MAGIC)
18                 return TDB_OFF_ERR;
19         return f.data_len;
20 }
21
22 int main(int argc, char *argv[])
23 {
24         tdb_off_t b_off, zone_off;
25         struct tdb_context *tdb;
26         struct tdb_layout *layout;
27         struct tdb_data data, key;
28         tdb_len_t len;
29         unsigned int zone_bits = 16;
30
31         /* FIXME: Test TDB_CONVERT */
32
33         plan_tests(45);
34         data.dptr = (void *)"world";
35         data.dsize = 5;
36         key.dptr = (void *)"hello";
37         key.dsize = 5;
38
39         /* No coalescing can be done due to EOF */
40         layout = new_tdb_layout(NULL);
41         tdb_layout_add_zone(layout, zone_bits, false);
42         len = 1024;
43         tdb_layout_add_free(layout, len);
44         tdb = tdb_layout_get(layout);
45         zone_off = layout->elem[0].base.off;
46         ok1(tdb_check(tdb, NULL, NULL) == 0);
47         ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
48
49         /* Figure out which bucket free entry is. */
50         b_off = bucket_off(zone_off, size_to_bucket(zone_bits, len));
51         /* Lock and fail to coalesce. */
52         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
53         ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
54                      b_off, len) == 0);
55         tdb_unlock_free_bucket(tdb, b_off);
56         ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
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(NULL);
62         tdb_layout_add_zone(layout, zone_bits, false);
63         tdb_layout_add_free(layout, 1024);
64         tdb_layout_add_used(layout, key, data, 6);
65         tdb = tdb_layout_get(layout);
66         zone_off = layout->elem[0].base.off;
67         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
68         ok1(tdb_check(tdb, NULL, NULL) == 0);
69
70         /* Figure out which bucket free entry is. */
71         b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
72         /* Lock and fail to coalesce. */
73         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
74         ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
75                      b_off, 1024) == 0);
76         tdb_unlock_free_bucket(tdb, b_off);
77         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
78         ok1(tdb_check(tdb, NULL, NULL) == 0);
79         tdb_close(tdb);
80
81         /* Coalescing can be done due to two free records, then EOF */
82         layout = new_tdb_layout(NULL);
83         tdb_layout_add_zone(layout, zone_bits, false);
84         tdb_layout_add_free(layout, 1024);
85         tdb_layout_add_free(layout, 2048);
86         tdb = tdb_layout_get(layout);
87         zone_off = layout->elem[0].base.off;
88         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
89         ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
90         ok1(tdb_check(tdb, NULL, NULL) == 0);
91
92         /* Figure out which bucket (first) free entry is. */
93         b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
94         /* Lock and coalesce. */
95         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
96         ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
97                      b_off, 1024) == 1);
98         ok1(!tdb_has_locks(tdb));
99         ok1(free_record_length(tdb, layout->elem[1].base.off)
100             == 1024 + sizeof(struct tdb_used_record) + 2048);
101         ok1(tdb_check(tdb, NULL, NULL) == 0);
102         tdb_close(tdb);
103
104         /* Coalescing can be done due to two free records, then data */
105         layout = new_tdb_layout(NULL);
106         tdb_layout_add_zone(layout, zone_bits, false);
107         tdb_layout_add_free(layout, 1024);
108         tdb_layout_add_free(layout, 512);
109         tdb_layout_add_used(layout, key, data, 6);
110         tdb = tdb_layout_get(layout);
111         zone_off = layout->elem[0].base.off;
112         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
113         ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
114         ok1(tdb_check(tdb, NULL, NULL) == 0);
115
116         /* Figure out which bucket free entry is. */
117         b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
118         /* Lock and coalesce. */
119         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
120         ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
121                      b_off, 1024) == 1);
122         ok1(!tdb_has_locks(tdb));
123         ok1(free_record_length(tdb, layout->elem[1].base.off)
124             == 1024 + sizeof(struct tdb_used_record) + 512);
125         ok1(tdb_check(tdb, NULL, NULL) == 0);
126         tdb_close(tdb);
127
128         /* Coalescing can be done due to three free records, then EOF */
129         layout = new_tdb_layout(NULL);
130         tdb_layout_add_zone(layout, zone_bits, false);
131         tdb_layout_add_free(layout, 1024);
132         tdb_layout_add_free(layout, 512);
133         tdb_layout_add_free(layout, 256);
134         tdb = tdb_layout_get(layout);
135         zone_off = layout->elem[0].base.off;
136         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
137         ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
138         ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
139         ok1(tdb_check(tdb, NULL, NULL) == 0);
140
141         /* Figure out which bucket free entry is. */
142         b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
143         /* Lock and coalesce. */
144         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
145         ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
146                      b_off, 1024) == 1);
147         ok1(!tdb_has_locks(tdb));
148         ok1(free_record_length(tdb, layout->elem[1].base.off)
149             == 1024 + sizeof(struct tdb_used_record) + 512
150             + sizeof(struct tdb_used_record) + 256);
151         ok1(tdb_check(tdb, NULL, NULL) == 0);
152         tdb_close(tdb);
153
154         /* Coalescing across two zones isn't possible. */
155         layout = new_tdb_layout(NULL);
156         tdb_layout_add_zone(layout, zone_bits, false);
157         tdb_layout_add_zone(layout, zone_bits, true);
158         tdb = tdb_layout_get(layout);
159         zone_off = layout->elem[0].base.off;
160         len = layout->elem[1].free.len;
161         ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
162         ok1(tdb_check(tdb, NULL, NULL) == 0);
163
164         /* Figure out which list free entry is. */
165         b_off = bucket_off(zone_off, size_to_bucket(zone_bits, len));
166         /* Lock and coalesce. */
167         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
168         ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
169                      b_off, len) == 0);
170         tdb_unlock_free_bucket(tdb, b_off);
171         ok1(!tdb_has_locks(tdb));
172         ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
173         ok1(tdb_check(tdb, NULL, NULL) == 0);
174         tdb_close(tdb);
175
176         ok1(tap_log_messages == 0);
177         return exit_status();
178 }