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
[])
28 tdb_off_t b_off
, test
;
29 struct tdb_context
*tdb
;
30 struct tdb_layout
*layout
;
31 struct tdb_data data
, key
;
34 /* FIXME: Test TDB_CONVERT */
35 /* FIXME: Test lock order fail. */
38 data
= tdb_mkdata("world", 5);
39 key
= tdb_mkdata("hello", 5);
41 /* No coalescing can be done due to EOF */
42 layout
= new_tdb_layout("run-03-coalesce.tdb");
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 test
= layout
->elem
[1].base
.off
;
55 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, len
, &test
)
57 tdb_unlock_free_bucket(tdb
, b_off
);
58 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == len
);
59 ok1(test
== layout
->elem
[1].base
.off
);
60 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
62 tdb_layout_free(layout
);
64 /* No coalescing can be done due to used record */
65 layout
= new_tdb_layout("run-03-coalesce.tdb");
66 tdb_layout_add_freetable(layout
);
67 tdb_layout_add_free(layout
, 1024, 0);
68 tdb_layout_add_used(layout
, key
, data
, 6);
69 tdb
= tdb_layout_get(layout
);
70 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
71 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
73 /* Figure out which bucket free entry is. */
74 b_off
= bucket_off(tdb
->ftable_off
, size_to_bucket(1024));
75 /* Lock and fail to coalesce. */
76 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
77 test
= layout
->elem
[1].base
.off
;
78 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
80 tdb_unlock_free_bucket(tdb
, b_off
);
81 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
82 ok1(test
== layout
->elem
[1].base
.off
);
83 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
85 tdb_layout_free(layout
);
87 /* Coalescing can be done due to two free records, then EOF */
88 layout
= new_tdb_layout("run-03-coalesce.tdb");
89 tdb_layout_add_freetable(layout
);
90 tdb_layout_add_free(layout
, 1024, 0);
91 tdb_layout_add_free(layout
, 2048, 0);
92 tdb
= tdb_layout_get(layout
);
93 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
94 ok1(free_record_length(tdb
, layout
->elem
[2].base
.off
) == 2048);
95 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
97 /* Figure out which bucket (first) free entry is. */
98 b_off
= bucket_off(tdb
->ftable_off
, size_to_bucket(1024));
99 /* Lock and coalesce. */
100 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
101 test
= layout
->elem
[2].base
.off
;
102 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
103 == 1024 + sizeof(struct tdb_used_record
) + 2048);
104 /* Should tell us it's erased this one... */
105 ok1(test
== TDB_ERR_NOEXIST
);
106 ok1(tdb
->file
->allrecord_lock
.count
== 0 && tdb
->file
->num_lockrecs
== 0);
107 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
)
108 == 1024 + sizeof(struct tdb_used_record
) + 2048);
109 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
111 tdb_layout_free(layout
);
113 /* Coalescing can be done due to two free records, then data */
114 layout
= new_tdb_layout("run-03-coalesce.tdb");
115 tdb_layout_add_freetable(layout
);
116 tdb_layout_add_free(layout
, 1024, 0);
117 tdb_layout_add_free(layout
, 512, 0);
118 tdb_layout_add_used(layout
, key
, data
, 6);
119 tdb
= tdb_layout_get(layout
);
120 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
121 ok1(free_record_length(tdb
, layout
->elem
[2].base
.off
) == 512);
122 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
124 /* Figure out which bucket free entry is. */
125 b_off
= bucket_off(tdb
->ftable_off
, size_to_bucket(1024));
126 /* Lock and coalesce. */
127 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
128 test
= layout
->elem
[2].base
.off
;
129 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
130 == 1024 + sizeof(struct tdb_used_record
) + 512);
131 ok1(tdb
->file
->allrecord_lock
.count
== 0 && tdb
->file
->num_lockrecs
== 0);
132 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
)
133 == 1024 + sizeof(struct tdb_used_record
) + 512);
134 ok1(test
== TDB_ERR_NOEXIST
);
135 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
137 tdb_layout_free(layout
);
139 /* Coalescing can be done due to three free records, then EOF */
140 layout
= new_tdb_layout("run-03-coalesce.tdb");
141 tdb_layout_add_freetable(layout
);
142 tdb_layout_add_free(layout
, 1024, 0);
143 tdb_layout_add_free(layout
, 512, 0);
144 tdb_layout_add_free(layout
, 256, 0);
145 tdb
= tdb_layout_get(layout
);
146 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
147 ok1(free_record_length(tdb
, layout
->elem
[2].base
.off
) == 512);
148 ok1(free_record_length(tdb
, layout
->elem
[3].base
.off
) == 256);
149 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
151 /* Figure out which bucket free entry is. */
152 b_off
= bucket_off(tdb
->ftable_off
, size_to_bucket(1024));
153 /* Lock and coalesce. */
154 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
155 test
= layout
->elem
[2].base
.off
;
156 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
157 == 1024 + sizeof(struct tdb_used_record
) + 512
158 + sizeof(struct tdb_used_record
) + 256);
159 ok1(tdb
->file
->allrecord_lock
.count
== 0
160 && tdb
->file
->num_lockrecs
== 0);
161 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
)
162 == 1024 + sizeof(struct tdb_used_record
) + 512
163 + sizeof(struct tdb_used_record
) + 256);
164 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
166 tdb_layout_free(layout
);
168 ok1(tap_log_messages
== 0);
169 return exit_status();