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