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>
13 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
15 struct tdb_free_record f;
18 ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
19 if (ecode != TDB_SUCCESS)
21 if (frec_magic(&f) != TDB_FREE_MAGIC)
22 return TDB_ERR_CORRUPT;
26 int main(int argc, char *argv[])
29 struct tdb_context *tdb;
30 struct tdb_layout *layout;
31 struct tdb_data data, key;
34 /* FIXME: Test TDB_CONVERT */
37 data = tdb_mkdata("world", 5);
38 key = tdb_mkdata("hello", 5);
40 /* No coalescing can be done due to EOF */
41 layout = new_tdb_layout("run-03-coalesce.tdb");
42 tdb_layout_add_freetable(layout);
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);
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);
58 tdb_layout_free(layout);
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);
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);
78 tdb_layout_free(layout);
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);
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)
95 == 1024 + sizeof(struct tdb_used_record) + 2048);
96 ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->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);
101 tdb_layout_free(layout);
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);
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)
119 == 1024 + sizeof(struct tdb_used_record) + 512);
120 ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
121 ok1(free_record_length(tdb, layout->elem[1].base.off)
122 == 1024 + sizeof(struct tdb_used_record) + 512);
123 ok1(tdb_check(tdb, NULL, NULL) == 0);
125 tdb_layout_free(layout);
127 /* Coalescing can be done due to three free records, then EOF */
128 layout = new_tdb_layout("run-03-coalesce.tdb");
129 tdb_layout_add_freetable(layout);
130 tdb_layout_add_free(layout, 1024, 0);
131 tdb_layout_add_free(layout, 512, 0);
132 tdb_layout_add_free(layout, 256, 0);
133 tdb = tdb_layout_get(layout);
134 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
135 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
136 ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
137 ok1(tdb_check(tdb, NULL, NULL) == 0);
139 /* Figure out which bucket free entry is. */
140 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
141 /* Lock and coalesce. */
142 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
143 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) ==
144 1024 + sizeof(struct tdb_used_record) + 512
145 + sizeof(struct tdb_used_record) + 256);
146 ok1(tdb->file->allrecord_lock.count == 0
147 && tdb->file->num_lockrecs == 0);
148 ok1(free_record_length(tdb, layout->elem[1].base.off)
149 == 1024 + sizeof(struct tdb_used_record) + 512
150 + sizeof(struct tdb_used_record) + 256);
151 ok1(tdb_check(tdb, NULL, NULL) == 0);
153 tdb_layout_free(layout);
155 ok1(tap_log_messages == 0);
156 return exit_status();