1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
6 static tdb_len_t
free_record_length(struct tdb_context
*tdb
, tdb_off_t off
)
8 struct tdb_free_record f
;
11 ecode
= tdb_read_convert(tdb
, off
, &f
, sizeof(f
));
12 if (ecode
!= TDB_SUCCESS
)
14 if (frec_magic(&f
) != TDB_FREE_MAGIC
)
15 return TDB_ERR_CORRUPT
;
19 int main(int argc
, char *argv
[])
21 tdb_off_t b_off
, test
;
22 struct tdb_context
*tdb
;
23 struct tdb_layout
*layout
;
24 struct tdb_data data
, key
;
27 /* FIXME: Test TDB_CONVERT */
28 /* FIXME: Test lock order fail. */
31 data
= tdb_mkdata("world", 5);
32 key
= tdb_mkdata("hello", 5);
34 /* No coalescing can be done due to EOF */
35 layout
= new_tdb_layout("run-03-coalesce.tdb");
36 tdb_layout_add_freetable(layout
);
38 tdb_layout_add_free(layout
, len
, 0);
39 tdb
= tdb_layout_get(layout
);
40 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
41 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == len
);
43 /* Figure out which bucket free entry is. */
44 b_off
= bucket_off(tdb
->tdb2
.ftable_off
, size_to_bucket(len
));
45 /* Lock and fail to coalesce. */
46 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
47 test
= layout
->elem
[1].base
.off
;
48 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, len
, &test
)
50 tdb_unlock_free_bucket(tdb
, b_off
);
51 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == len
);
52 ok1(test
== layout
->elem
[1].base
.off
);
53 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
55 tdb_layout_free(layout
);
57 /* No coalescing can be done due to used record */
58 layout
= new_tdb_layout("run-03-coalesce.tdb");
59 tdb_layout_add_freetable(layout
);
60 tdb_layout_add_free(layout
, 1024, 0);
61 tdb_layout_add_used(layout
, key
, data
, 6);
62 tdb
= tdb_layout_get(layout
);
63 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
64 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
66 /* Figure out which bucket free entry is. */
67 b_off
= bucket_off(tdb
->tdb2
.ftable_off
, size_to_bucket(1024));
68 /* Lock and fail to coalesce. */
69 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
70 test
= layout
->elem
[1].base
.off
;
71 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
73 tdb_unlock_free_bucket(tdb
, b_off
);
74 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
75 ok1(test
== layout
->elem
[1].base
.off
);
76 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
78 tdb_layout_free(layout
);
80 /* Coalescing can be done due to two free records, then EOF */
81 layout
= new_tdb_layout("run-03-coalesce.tdb");
82 tdb_layout_add_freetable(layout
);
83 tdb_layout_add_free(layout
, 1024, 0);
84 tdb_layout_add_free(layout
, 2048, 0);
85 tdb
= tdb_layout_get(layout
);
86 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
87 ok1(free_record_length(tdb
, layout
->elem
[2].base
.off
) == 2048);
88 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
90 /* Figure out which bucket (first) free entry is. */
91 b_off
= bucket_off(tdb
->tdb2
.ftable_off
, size_to_bucket(1024));
92 /* Lock and coalesce. */
93 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
94 test
= layout
->elem
[2].base
.off
;
95 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
96 == 1024 + sizeof(struct tdb_used_record
) + 2048);
97 /* Should tell us it's erased this one... */
98 ok1(test
== TDB_ERR_NOEXIST
);
99 ok1(tdb
->file
->allrecord_lock
.count
== 0 && tdb
->file
->num_lockrecs
== 0);
100 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
)
101 == 1024 + sizeof(struct tdb_used_record
) + 2048);
102 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
104 tdb_layout_free(layout
);
106 /* Coalescing can be done due to two free records, then data */
107 layout
= new_tdb_layout("run-03-coalesce.tdb");
108 tdb_layout_add_freetable(layout
);
109 tdb_layout_add_free(layout
, 1024, 0);
110 tdb_layout_add_free(layout
, 512, 0);
111 tdb_layout_add_used(layout
, key
, data
, 6);
112 tdb
= tdb_layout_get(layout
);
113 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
114 ok1(free_record_length(tdb
, layout
->elem
[2].base
.off
) == 512);
115 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
117 /* Figure out which bucket free entry is. */
118 b_off
= bucket_off(tdb
->tdb2
.ftable_off
, size_to_bucket(1024));
119 /* Lock and coalesce. */
120 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
121 test
= layout
->elem
[2].base
.off
;
122 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
123 == 1024 + sizeof(struct tdb_used_record
) + 512);
124 ok1(tdb
->file
->allrecord_lock
.count
== 0 && tdb
->file
->num_lockrecs
== 0);
125 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
)
126 == 1024 + sizeof(struct tdb_used_record
) + 512);
127 ok1(test
== TDB_ERR_NOEXIST
);
128 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
130 tdb_layout_free(layout
);
132 /* Coalescing can be done due to three free records, then EOF */
133 layout
= new_tdb_layout("run-03-coalesce.tdb");
134 tdb_layout_add_freetable(layout
);
135 tdb_layout_add_free(layout
, 1024, 0);
136 tdb_layout_add_free(layout
, 512, 0);
137 tdb_layout_add_free(layout
, 256, 0);
138 tdb
= tdb_layout_get(layout
);
139 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
) == 1024);
140 ok1(free_record_length(tdb
, layout
->elem
[2].base
.off
) == 512);
141 ok1(free_record_length(tdb
, layout
->elem
[3].base
.off
) == 256);
142 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
144 /* Figure out which bucket free entry is. */
145 b_off
= bucket_off(tdb
->tdb2
.ftable_off
, size_to_bucket(1024));
146 /* Lock and coalesce. */
147 ok1(tdb_lock_free_bucket(tdb
, b_off
, TDB_LOCK_WAIT
) == 0);
148 test
= layout
->elem
[2].base
.off
;
149 ok1(coalesce(tdb
, layout
->elem
[1].base
.off
, b_off
, 1024, &test
)
150 == 1024 + sizeof(struct tdb_used_record
) + 512
151 + sizeof(struct tdb_used_record
) + 256);
152 ok1(tdb
->file
->allrecord_lock
.count
== 0
153 && tdb
->file
->num_lockrecs
== 0);
154 ok1(free_record_length(tdb
, layout
->elem
[1].base
.off
)
155 == 1024 + sizeof(struct tdb_used_record
) + 512
156 + sizeof(struct tdb_used_record
) + 256);
157 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
159 tdb_layout_free(layout
);
161 ok1(tap_log_messages
== 0);
162 return exit_status();