]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-03-coalesce.c
tdb2: make tests include a single mega-header to simplify future patches
[ccan] / ccan / tdb2 / test / run-03-coalesce.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4 #include "layout.h"
5
6 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
7 {
8         struct tdb_free_record f;
9         enum TDB_ERROR ecode;
10
11         ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
12         if (ecode != TDB_SUCCESS)
13                 return ecode;
14         if (frec_magic(&f) != TDB_FREE_MAGIC)
15                 return TDB_ERR_CORRUPT;
16         return frec_len(&f);
17 }
18
19 int main(int argc, char *argv[])
20 {
21         tdb_off_t b_off, test;
22         struct tdb_context *tdb;
23         struct tdb_layout *layout;
24         struct tdb_data data, key;
25         tdb_len_t len;
26
27         /* FIXME: Test TDB_CONVERT */
28         /* FIXME: Test lock order fail. */
29
30         plan_tests(42);
31         data = tdb_mkdata("world", 5);
32         key = tdb_mkdata("hello", 5);
33
34         /* No coalescing can be done due to EOF */
35         layout = new_tdb_layout("run-03-coalesce.tdb");
36         tdb_layout_add_freetable(layout);
37         len = 1024;
38         tdb_layout_add_free(layout, len, 0);
39         tdb = tdb_layout_get(layout, &tap_log_attr);
40         ok1(tdb_check(tdb, NULL, NULL) == 0);
41         ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
42
43         /* Figure out which bucket free entry is. */
44         b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
45         /* Lock and fail to coalesce. */
46         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
47         test = layout->elem[1].base.off;
48         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len, &test)
49             == 0);
50         tdb_unlock_free_bucket(tdb, b_off);
51         ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
52         ok1(test == layout->elem[1].base.off);
53         ok1(tdb_check(tdb, NULL, NULL) == 0);
54         tdb_close(tdb);
55         tdb_layout_free(layout);
56
57         /* No coalescing can be done due to used record */
58         layout = new_tdb_layout("run-03-coalesce.tdb");
59         tdb_layout_add_freetable(layout);
60         tdb_layout_add_free(layout, 1024, 0);
61         tdb_layout_add_used(layout, key, data, 6);
62         tdb = tdb_layout_get(layout, &tap_log_attr);
63         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
64         ok1(tdb_check(tdb, NULL, NULL) == 0);
65
66         /* Figure out which bucket free entry is. */
67         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
68         /* Lock and fail to coalesce. */
69         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
70         test = layout->elem[1].base.off;
71         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
72             == 0);
73         tdb_unlock_free_bucket(tdb, b_off);
74         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
75         ok1(test == layout->elem[1].base.off);
76         ok1(tdb_check(tdb, NULL, NULL) == 0);
77         tdb_close(tdb);
78         tdb_layout_free(layout);
79
80         /* Coalescing can be done due to two free records, then EOF */
81         layout = new_tdb_layout("run-03-coalesce.tdb");
82         tdb_layout_add_freetable(layout);
83         tdb_layout_add_free(layout, 1024, 0);
84         tdb_layout_add_free(layout, 2048, 0);
85         tdb = tdb_layout_get(layout, &tap_log_attr);
86         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
87         ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
88         ok1(tdb_check(tdb, NULL, NULL) == 0);
89
90         /* Figure out which bucket (first) free entry is. */
91         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
92         /* Lock and coalesce. */
93         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
94         test = layout->elem[2].base.off;
95         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
96             == 1024 + sizeof(struct tdb_used_record) + 2048);
97         /* Should tell us it's erased this one... */
98         ok1(test == TDB_ERR_NOEXIST);
99         ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
100         ok1(free_record_length(tdb, layout->elem[1].base.off)
101             == 1024 + sizeof(struct tdb_used_record) + 2048);
102         ok1(tdb_check(tdb, NULL, NULL) == 0);
103         tdb_close(tdb);
104         tdb_layout_free(layout);
105
106         /* Coalescing can be done due to two free records, then data */
107         layout = new_tdb_layout("run-03-coalesce.tdb");
108         tdb_layout_add_freetable(layout);
109         tdb_layout_add_free(layout, 1024, 0);
110         tdb_layout_add_free(layout, 512, 0);
111         tdb_layout_add_used(layout, key, data, 6);
112         tdb = tdb_layout_get(layout, &tap_log_attr);
113         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
114         ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
115         ok1(tdb_check(tdb, NULL, NULL) == 0);
116
117         /* Figure out which bucket free entry is. */
118         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
119         /* Lock and coalesce. */
120         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
121         test = layout->elem[2].base.off;
122         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
123             == 1024 + sizeof(struct tdb_used_record) + 512);
124         ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
125         ok1(free_record_length(tdb, layout->elem[1].base.off)
126             == 1024 + sizeof(struct tdb_used_record) + 512);
127         ok1(test == TDB_ERR_NOEXIST);
128         ok1(tdb_check(tdb, NULL, NULL) == 0);
129         tdb_close(tdb);
130         tdb_layout_free(layout);
131
132         /* Coalescing can be done due to three free records, then EOF */
133         layout = new_tdb_layout("run-03-coalesce.tdb");
134         tdb_layout_add_freetable(layout);
135         tdb_layout_add_free(layout, 1024, 0);
136         tdb_layout_add_free(layout, 512, 0);
137         tdb_layout_add_free(layout, 256, 0);
138         tdb = tdb_layout_get(layout, &tap_log_attr);
139         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
140         ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
141         ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
142         ok1(tdb_check(tdb, NULL, NULL) == 0);
143
144         /* Figure out which bucket free entry is. */
145         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
146         /* Lock and coalesce. */
147         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
148         test = layout->elem[2].base.off;
149         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test)
150             == 1024 + sizeof(struct tdb_used_record) + 512
151             + sizeof(struct tdb_used_record) + 256);
152         ok1(tdb->file->allrecord_lock.count == 0
153             && tdb->file->num_lockrecs == 0);
154         ok1(free_record_length(tdb, layout->elem[1].base.off)
155             == 1024 + sizeof(struct tdb_used_record) + 512
156             + sizeof(struct tdb_used_record) + 256);
157         ok1(tdb_check(tdb, NULL, NULL) == 0);
158         tdb_close(tdb);
159         tdb_layout_free(layout);
160
161         ok1(tap_log_messages == 0);
162         return exit_status();
163 }