]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-03-coalesce.c
dc76e6a4ad3c37e504d8af4c0ddf7fc7782d9532
[ccan] / ccan / ntdb / test / run-03-coalesce.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "layout.h"
5
6 static ntdb_len_t free_record_length(struct ntdb_context *ntdb, ntdb_off_t off)
7 {
8         struct ntdb_free_record f;
9         enum NTDB_ERROR ecode;
10
11         ecode = ntdb_read_convert(ntdb, off, &f, sizeof(f));
12         if (ecode != NTDB_SUCCESS)
13                 return ecode;
14         if (frec_magic(&f) != NTDB_FREE_MAGIC)
15                 return NTDB_ERR_CORRUPT;
16         return frec_len(&f);
17 }
18
19 int main(int argc, char *argv[])
20 {
21         ntdb_off_t b_off, test;
22         struct ntdb_context *ntdb;
23         struct ntdb_layout *layout;
24         NTDB_DATA data, key;
25         ntdb_len_t len;
26
27         /* FIXME: Test NTDB_CONVERT */
28         /* FIXME: Test lock order fail. */
29
30         plan_tests(42);
31         data = ntdb_mkdata("world", 5);
32         key = ntdb_mkdata("hello", 5);
33
34         /* No coalescing can be done due to EOF */
35         layout = new_ntdb_layout();
36         ntdb_layout_add_freetable(layout);
37         len = 15560;
38         ntdb_layout_add_free(layout, len, 0);
39         ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
40         /* NOMMAP is for lockcheck. */
41         ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
42                          O_RDWR, 0, &tap_log_attr);
43         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
44         ok1(free_record_length(ntdb, layout->elem[1].base.off) == len);
45
46         /* Figure out which bucket free entry is. */
47         b_off = bucket_off(ntdb->ftable_off, size_to_bucket(len));
48         /* Lock and fail to coalesce. */
49         ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
50         test = layout->elem[1].base.off;
51         ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, len, &test)
52             == 0);
53         ntdb_unlock_free_bucket(ntdb, b_off);
54         ok1(free_record_length(ntdb, layout->elem[1].base.off) == len);
55         ok1(test == layout->elem[1].base.off);
56         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
57         ntdb_close(ntdb);
58         ntdb_layout_free(layout);
59
60         /* No coalescing can be done due to used record */
61         layout = new_ntdb_layout();
62         ntdb_layout_add_freetable(layout);
63         ntdb_layout_add_free(layout, 15528, 0);
64         ntdb_layout_add_used(layout, key, data, 6);
65         ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
66         /* NOMMAP is for lockcheck. */
67         ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
68                          O_RDWR, 0, &tap_log_attr);
69         ok1(free_record_length(ntdb, layout->elem[1].base.off) == 15528);
70         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
71
72         /* Figure out which bucket free entry is. */
73         b_off = bucket_off(ntdb->ftable_off, size_to_bucket(15528));
74         /* Lock and fail to coalesce. */
75         ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
76         test = layout->elem[1].base.off;
77         ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 15528, &test)
78             == 0);
79         ntdb_unlock_free_bucket(ntdb, b_off);
80         ok1(free_record_length(ntdb, layout->elem[1].base.off) == 15528);
81         ok1(test == layout->elem[1].base.off);
82         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
83         ntdb_close(ntdb);
84         ntdb_layout_free(layout);
85
86         /* Coalescing can be done due to two free records, then EOF */
87         layout = new_ntdb_layout();
88         ntdb_layout_add_freetable(layout);
89         ntdb_layout_add_free(layout, 1024, 0);
90         ntdb_layout_add_free(layout, 14520, 0);
91         ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
92         /* NOMMAP is for lockcheck. */
93         ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
94                          O_RDWR, 0, &tap_log_attr);
95         ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
96         ok1(free_record_length(ntdb, layout->elem[2].base.off) == 14520);
97         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
98
99         /* Figure out which bucket (first) free entry is. */
100         b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
101         /* Lock and coalesce. */
102         ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
103         test = layout->elem[2].base.off;
104         ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
105             == 1024 + sizeof(struct ntdb_used_record) + 14520);
106         /* Should tell us it's erased this one... */
107         ok1(test == NTDB_ERR_NOEXIST);
108         ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
109         ok1(free_record_length(ntdb, layout->elem[1].base.off)
110             == 1024 + sizeof(struct ntdb_used_record) + 14520);
111         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
112         ntdb_close(ntdb);
113         ntdb_layout_free(layout);
114
115         /* Coalescing can be done due to two free records, then data */
116         layout = new_ntdb_layout();
117         ntdb_layout_add_freetable(layout);
118         ntdb_layout_add_free(layout, 1024, 0);
119         ntdb_layout_add_free(layout, 14488, 0);
120         ntdb_layout_add_used(layout, key, data, 6);
121         ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
122         /* NOMMAP is for lockcheck. */
123         ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
124                          O_RDWR, 0, &tap_log_attr);
125         ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
126         ok1(free_record_length(ntdb, layout->elem[2].base.off) == 14488);
127         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
128
129         /* Figure out which bucket free entry is. */
130         b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
131         /* Lock and coalesce. */
132         ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
133         test = layout->elem[2].base.off;
134         ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
135             == 1024 + sizeof(struct ntdb_used_record) + 14488);
136         ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
137         ok1(free_record_length(ntdb, layout->elem[1].base.off)
138             == 1024 + sizeof(struct ntdb_used_record) + 14488);
139         ok1(test == NTDB_ERR_NOEXIST);
140         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
141         ntdb_close(ntdb);
142         ntdb_layout_free(layout);
143
144         /* Coalescing can be done due to three free records, then EOF */
145         layout = new_ntdb_layout();
146         ntdb_layout_add_freetable(layout);
147         ntdb_layout_add_free(layout, 1024, 0);
148         ntdb_layout_add_free(layout, 512, 0);
149         ntdb_layout_add_free(layout, 13992, 0);
150         ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
151         /* NOMMAP is for lockcheck. */
152         ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
153                          O_RDWR, 0, &tap_log_attr);
154         ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
155         ok1(free_record_length(ntdb, layout->elem[2].base.off) == 512);
156         ok1(free_record_length(ntdb, layout->elem[3].base.off) == 13992);
157         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
158
159         /* Figure out which bucket free entry is. */
160         b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
161         /* Lock and coalesce. */
162         ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
163         test = layout->elem[2].base.off;
164         ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
165             == 1024 + sizeof(struct ntdb_used_record) + 512
166             + sizeof(struct ntdb_used_record) + 13992);
167         ok1(ntdb->file->allrecord_lock.count == 0
168             && ntdb->file->num_lockrecs == 0);
169         ok1(free_record_length(ntdb, layout->elem[1].base.off)
170             == 1024 + sizeof(struct ntdb_used_record) + 512
171             + sizeof(struct ntdb_used_record) + 13992);
172         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
173         ntdb_close(ntdb);
174         ntdb_layout_free(layout);
175
176         ok1(tap_log_messages == 0);
177         return exit_status();
178 }