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>
12 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
14 struct tdb_free_record f;
16 if (tdb_read_convert(tdb, off, &f, sizeof(f)) != 0)
18 if (frec_magic(&f) != TDB_FREE_MAGIC)
23 int main(int argc, char *argv[])
26 struct tdb_context *tdb;
27 struct tdb_layout *layout;
28 struct tdb_data data, key;
31 /* FIXME: Test TDB_CONVERT */
34 data.dptr = (void *)"world";
36 key.dptr = (void *)"hello";
39 /* No coalescing can be done due to EOF */
40 layout = new_tdb_layout(NULL);
41 tdb_layout_add_freetable(layout);
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);
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);
57 tdb_layout_free(layout);
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);
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);
77 tdb_layout_free(layout);
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);
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);
99 tdb_layout_free(layout);
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);
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);
122 tdb_layout_free(layout);
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);
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);
147 tdb_layout_free(layout);
149 ok1(tap_log_messages == 0);
150 return exit_status();