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_freelist(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->flist_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);
58 /* No coalescing can be done due to used record */
59 layout = new_tdb_layout(NULL);
60 tdb_layout_add_freelist(layout);
61 tdb_layout_add_free(layout, 1024, 0);
62 tdb_layout_add_used(layout, key, data, 6);
63 tdb = tdb_layout_get(layout);
64 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
65 ok1(tdb_check(tdb, NULL, NULL) == 0);
67 /* Figure out which bucket free entry is. */
68 b_off = bucket_off(tdb->flist_off, size_to_bucket(1024));
69 /* Lock and fail to coalesce. */
70 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
71 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 0);
72 tdb_unlock_free_bucket(tdb, b_off);
73 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
74 ok1(tdb_check(tdb, NULL, NULL) == 0);
77 /* Coalescing can be done due to two free records, then EOF */
78 layout = new_tdb_layout(NULL);
79 tdb_layout_add_freelist(layout);
80 tdb_layout_add_free(layout, 1024, 0);
81 tdb_layout_add_free(layout, 2048, 0);
82 tdb = tdb_layout_get(layout);
83 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
84 ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
85 ok1(tdb_check(tdb, NULL, NULL) == 0);
87 /* Figure out which bucket (first) free entry is. */
88 b_off = bucket_off(tdb->flist_off, size_to_bucket(1024));
89 /* Lock and coalesce. */
90 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
91 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
92 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
93 ok1(free_record_length(tdb, layout->elem[1].base.off)
94 == 1024 + sizeof(struct tdb_used_record) + 2048);
95 ok1(tdb_check(tdb, NULL, NULL) == 0);
98 /* Coalescing can be done due to two free records, then data */
99 layout = new_tdb_layout(NULL);
100 tdb_layout_add_freelist(layout);
101 tdb_layout_add_free(layout, 1024, 0);
102 tdb_layout_add_free(layout, 512, 0);
103 tdb_layout_add_used(layout, key, data, 6);
104 tdb = tdb_layout_get(layout);
105 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
106 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
107 ok1(tdb_check(tdb, NULL, NULL) == 0);
109 /* Figure out which bucket free entry is. */
110 b_off = bucket_off(tdb->flist_off, size_to_bucket(1024));
111 /* Lock and coalesce. */
112 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
113 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
114 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
115 ok1(free_record_length(tdb, layout->elem[1].base.off)
116 == 1024 + sizeof(struct tdb_used_record) + 512);
117 ok1(tdb_check(tdb, NULL, NULL) == 0);
120 /* Coalescing can be done due to three free records, then EOF */
121 layout = new_tdb_layout(NULL);
122 tdb_layout_add_freelist(layout);
123 tdb_layout_add_free(layout, 1024, 0);
124 tdb_layout_add_free(layout, 512, 0);
125 tdb_layout_add_free(layout, 256, 0);
126 tdb = tdb_layout_get(layout);
127 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
128 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
129 ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
130 ok1(tdb_check(tdb, NULL, NULL) == 0);
132 /* Figure out which bucket free entry is. */
133 b_off = bucket_off(tdb->flist_off, size_to_bucket(1024));
134 /* Lock and coalesce. */
135 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
136 ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
137 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
138 ok1(free_record_length(tdb, layout->elem[1].base.off)
139 == 1024 + sizeof(struct tdb_used_record) + 512
140 + sizeof(struct tdb_used_record) + 256);
141 ok1(tdb_check(tdb, NULL, NULL) == 0);
144 ok1(tap_log_messages == 0);
145 return exit_status();