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