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