]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-03-coalesce.c
a382ae08ddcb9b10a053201ee08f68d119e9bcfd
[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
58         /* No coalescing can be done due to used record */
59         layout = new_tdb_layout(NULL);
60         tdb_layout_add_freetable(layout);
61         tdb_layout_add_free(layout, 1024, 0);
62         tdb_layout_add_used(layout, key, data, 6);
63         tdb = tdb_layout_get(layout);
64         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
65         ok1(tdb_check(tdb, NULL, NULL) == 0);
66
67         /* Figure out which bucket free entry is. */
68         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
69         /* Lock and fail to coalesce. */
70         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
71         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 0);
72         tdb_unlock_free_bucket(tdb, b_off);
73         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
74         ok1(tdb_check(tdb, NULL, NULL) == 0);
75         tdb_close(tdb);
76
77         /* Coalescing can be done due to two free records, then EOF */
78         layout = new_tdb_layout(NULL);
79         tdb_layout_add_freetable(layout);
80         tdb_layout_add_free(layout, 1024, 0);
81         tdb_layout_add_free(layout, 2048, 0);
82         tdb = tdb_layout_get(layout);
83         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
84         ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
85         ok1(tdb_check(tdb, NULL, NULL) == 0);
86
87         /* Figure out which bucket (first) free entry is. */
88         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
89         /* Lock and coalesce. */
90         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
91         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
92         ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
93         ok1(free_record_length(tdb, layout->elem[1].base.off)
94             == 1024 + sizeof(struct tdb_used_record) + 2048);
95         ok1(tdb_check(tdb, NULL, NULL) == 0);
96         tdb_close(tdb);
97
98         /* Coalescing can be done due to two free records, then data */
99         layout = new_tdb_layout(NULL);
100         tdb_layout_add_freetable(layout);
101         tdb_layout_add_free(layout, 1024, 0);
102         tdb_layout_add_free(layout, 512, 0);
103         tdb_layout_add_used(layout, key, data, 6);
104         tdb = tdb_layout_get(layout);
105         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
106         ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
107         ok1(tdb_check(tdb, NULL, NULL) == 0);
108
109         /* Figure out which bucket free entry is. */
110         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
111         /* Lock and coalesce. */
112         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
113         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
114         ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
115         ok1(free_record_length(tdb, layout->elem[1].base.off)
116             == 1024 + sizeof(struct tdb_used_record) + 512);
117         ok1(tdb_check(tdb, NULL, NULL) == 0);
118         tdb_close(tdb);
119
120         /* Coalescing can be done due to three free records, then EOF */
121         layout = new_tdb_layout(NULL);
122         tdb_layout_add_freetable(layout);
123         tdb_layout_add_free(layout, 1024, 0);
124         tdb_layout_add_free(layout, 512, 0);
125         tdb_layout_add_free(layout, 256, 0);
126         tdb = tdb_layout_get(layout);
127         ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
128         ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
129         ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
130         ok1(tdb_check(tdb, NULL, NULL) == 0);
131
132         /* Figure out which bucket free entry is. */
133         b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
134         /* Lock and coalesce. */
135         ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
136         ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
137         ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
138         ok1(free_record_length(tdb, layout->elem[1].base.off)
139             == 1024 + sizeof(struct tdb_used_record) + 512
140             + sizeof(struct tdb_used_record) + 256);
141         ok1(tdb_check(tdb, NULL, NULL) == 0);
142         tdb_close(tdb);
143
144         ok1(tap_log_messages == 0);
145         return exit_status();
146 }