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.dptr = (void *)"world";
39 key.dptr = (void *)"hello";
42 /* No coalescing can be done due to EOF */
43 layout = new_tdb_layout("run-03-coalesce.tdb");
44 tdb_layout_add_freetable(layout);
46 tdb_layout_add_free(layout, len, 0);
47 tdb = tdb_layout_get(layout);
48 ok1(tdb_check(tdb, NULL, NULL) == 0);
49 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
51 /* Figure out which bucket free entry is. */
52 b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
53 /* Lock and fail to coalesce. */
54 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
55 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len) == 0);
56 tdb_unlock_free_bucket(tdb, b_off);
57 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
58 ok1(tdb_check(tdb, NULL, NULL) == 0);
60 tdb_layout_free(layout);
62 /* No coalescing can be done due to used record */
63 layout = new_tdb_layout("run-03-coalesce.tdb");
64 tdb_layout_add_freetable(layout);
65 tdb_layout_add_free(layout, 1024, 0);
66 tdb_layout_add_used(layout, key, data, 6);
67 tdb = tdb_layout_get(layout);
68 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
69 ok1(tdb_check(tdb, NULL, NULL) == 0);
71 /* Figure out which bucket free entry is. */
72 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
73 /* Lock and fail to coalesce. */
74 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
75 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 0);
76 tdb_unlock_free_bucket(tdb, b_off);
77 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
78 ok1(tdb_check(tdb, NULL, NULL) == 0);
80 tdb_layout_free(layout);
82 /* Coalescing can be done due to two free records, then EOF */
83 layout = new_tdb_layout("run-03-coalesce.tdb");
84 tdb_layout_add_freetable(layout);
85 tdb_layout_add_free(layout, 1024, 0);
86 tdb_layout_add_free(layout, 2048, 0);
87 tdb = tdb_layout_get(layout);
88 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
89 ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
90 ok1(tdb_check(tdb, NULL, NULL) == 0);
92 /* Figure out which bucket (first) free entry is. */
93 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
94 /* Lock and coalesce. */
95 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
96 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
97 ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
98 ok1(free_record_length(tdb, layout->elem[1].base.off)
99 == 1024 + sizeof(struct tdb_used_record) + 2048);
100 ok1(tdb_check(tdb, NULL, NULL) == 0);
102 tdb_layout_free(layout);
104 /* Coalescing can be done due to two free records, then data */
105 layout = new_tdb_layout("run-03-coalesce.tdb");
106 tdb_layout_add_freetable(layout);
107 tdb_layout_add_free(layout, 1024, 0);
108 tdb_layout_add_free(layout, 512, 0);
109 tdb_layout_add_used(layout, key, data, 6);
110 tdb = tdb_layout_get(layout);
111 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
112 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
113 ok1(tdb_check(tdb, NULL, NULL) == 0);
115 /* Figure out which bucket free entry is. */
116 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
117 /* Lock and coalesce. */
118 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
119 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
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) == 1);
144 ok1(tdb->file->allrecord_lock.count == 0
145 && tdb->file->num_lockrecs == 0);
146 ok1(free_record_length(tdb, layout->elem[1].base.off)
147 == 1024 + sizeof(struct tdb_used_record) + 512
148 + sizeof(struct tdb_used_record) + 256);
149 ok1(tdb_check(tdb, NULL, NULL) == 0);
151 tdb_layout_free(layout);
153 ok1(tap_log_messages == 0);
154 return exit_status();