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;
17 ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
18 if (ecode != TDB_SUCCESS)
20 if (frec_magic(&f) != TDB_FREE_MAGIC)
21 return TDB_ERR_CORRUPT;
25 int main(int argc, char *argv[])
28 struct tdb_context *tdb;
29 struct tdb_layout *layout;
30 struct tdb_data data, key;
33 /* FIXME: Test TDB_CONVERT */
36 data.dptr = (void *)"world";
38 key.dptr = (void *)"hello";
41 /* No coalescing can be done due to EOF */
42 layout = new_tdb_layout(NULL);
43 tdb_layout_add_freetable(layout);
45 tdb_layout_add_free(layout, len, 0);
46 tdb = tdb_layout_get(layout);
47 ok1(tdb_check(tdb, NULL, NULL) == 0);
48 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
50 /* Figure out which bucket free entry is. */
51 b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
52 /* Lock and fail to coalesce. */
53 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
54 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len) == 0);
55 tdb_unlock_free_bucket(tdb, b_off);
56 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
57 ok1(tdb_check(tdb, NULL, NULL) == 0);
59 tdb_layout_free(layout);
61 /* No coalescing can be done due to used record */
62 layout = new_tdb_layout(NULL);
63 tdb_layout_add_freetable(layout);
64 tdb_layout_add_free(layout, 1024, 0);
65 tdb_layout_add_used(layout, key, data, 6);
66 tdb = tdb_layout_get(layout);
67 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
68 ok1(tdb_check(tdb, NULL, NULL) == 0);
70 /* Figure out which bucket free entry is. */
71 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
72 /* Lock and fail to coalesce. */
73 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
74 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 0);
75 tdb_unlock_free_bucket(tdb, b_off);
76 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
77 ok1(tdb_check(tdb, NULL, NULL) == 0);
79 tdb_layout_free(layout);
81 /* Coalescing can be done due to two free records, then EOF */
82 layout = new_tdb_layout(NULL);
83 tdb_layout_add_freetable(layout);
84 tdb_layout_add_free(layout, 1024, 0);
85 tdb_layout_add_free(layout, 2048, 0);
86 tdb = tdb_layout_get(layout);
87 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
88 ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
89 ok1(tdb_check(tdb, NULL, NULL) == 0);
91 /* Figure out which bucket (first) free entry is. */
92 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
93 /* Lock and coalesce. */
94 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
95 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
96 ok1(tdb->allrecord_lock.count == 0 && tdb->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(NULL);
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) == 1);
119 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
120 ok1(free_record_length(tdb, layout->elem[1].base.off)
121 == 1024 + sizeof(struct tdb_used_record) + 512);
122 ok1(tdb_check(tdb, NULL, NULL) == 0);
124 tdb_layout_free(layout);
126 /* Coalescing can be done due to three free records, then EOF */
127 layout = new_tdb_layout(NULL);
128 tdb_layout_add_freetable(layout);
129 tdb_layout_add_free(layout, 1024, 0);
130 tdb_layout_add_free(layout, 512, 0);
131 tdb_layout_add_free(layout, 256, 0);
132 tdb = tdb_layout_get(layout);
133 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
134 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
135 ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
136 ok1(tdb_check(tdb, NULL, NULL) == 0);
138 /* Figure out which bucket free entry is. */
139 b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
140 /* Lock and coalesce. */
141 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
142 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
143 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
144 ok1(free_record_length(tdb, layout->elem[1].base.off)
145 == 1024 + sizeof(struct tdb_used_record) + 512
146 + sizeof(struct tdb_used_record) + 256);
147 ok1(tdb_check(tdb, NULL, NULL) == 0);
149 tdb_layout_free(layout);
151 ok1(tap_log_messages == 0);
152 return exit_status();