1 #include "ntdb-source.h"
2 #include "tap-interface.h"
5 #include "helprun-external-agent.h"
7 static ntdb_len_t free_record_length(struct ntdb_context *ntdb, ntdb_off_t off)
9 struct ntdb_free_record f;
10 enum NTDB_ERROR ecode;
12 ecode = ntdb_read_convert(ntdb, off, &f, sizeof(f));
13 if (ecode != NTDB_SUCCESS)
15 if (frec_magic(&f) != NTDB_FREE_MAGIC)
16 return NTDB_ERR_CORRUPT;
20 int main(int argc, char *argv[])
22 ntdb_off_t b_off, test;
23 struct ntdb_context *ntdb;
24 struct ntdb_layout *layout;
28 /* FIXME: Test NTDB_CONVERT */
29 /* FIXME: Test lock order fail. */
32 data = ntdb_mkdata("world", 5);
33 key = ntdb_mkdata("hello", 5);
35 /* No coalescing can be done due to EOF */
36 layout = new_ntdb_layout();
37 ntdb_layout_add_freetable(layout);
39 ntdb_layout_add_free(layout, len, 0);
40 ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
41 /* NOMMAP is for lockcheck. */
42 ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
43 O_RDWR, 0, &tap_log_attr);
44 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
45 ok1(free_record_length(ntdb, layout->elem[1].base.off) == len);
47 /* Figure out which bucket free entry is. */
48 b_off = bucket_off(ntdb->ftable_off, size_to_bucket(len));
49 /* Lock and fail to coalesce. */
50 ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
51 test = layout->elem[1].base.off;
52 ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, len, &test)
54 ntdb_unlock_free_bucket(ntdb, b_off);
55 ok1(free_record_length(ntdb, layout->elem[1].base.off) == len);
56 ok1(test == layout->elem[1].base.off);
57 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
59 ntdb_layout_free(layout);
61 /* No coalescing can be done due to used record */
62 layout = new_ntdb_layout();
63 ntdb_layout_add_freetable(layout);
64 ntdb_layout_add_free(layout, 15528, 0);
65 ntdb_layout_add_used(layout, key, data, 6);
66 ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
67 /* NOMMAP is for lockcheck. */
68 ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
69 O_RDWR, 0, &tap_log_attr);
70 ok1(free_record_length(ntdb, layout->elem[1].base.off) == 15528);
71 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
73 /* Figure out which bucket free entry is. */
74 b_off = bucket_off(ntdb->ftable_off, size_to_bucket(15528));
75 /* Lock and fail to coalesce. */
76 ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
77 test = layout->elem[1].base.off;
78 ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 15528, &test)
80 ntdb_unlock_free_bucket(ntdb, b_off);
81 ok1(free_record_length(ntdb, layout->elem[1].base.off) == 15528);
82 ok1(test == layout->elem[1].base.off);
83 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
85 ntdb_layout_free(layout);
87 /* Coalescing can be done due to two free records, then EOF */
88 layout = new_ntdb_layout();
89 ntdb_layout_add_freetable(layout);
90 ntdb_layout_add_free(layout, 1024, 0);
91 ntdb_layout_add_free(layout, 14520, 0);
92 ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
93 /* NOMMAP is for lockcheck. */
94 ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
95 O_RDWR, 0, &tap_log_attr);
96 ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
97 ok1(free_record_length(ntdb, layout->elem[2].base.off) == 14520);
98 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
100 /* Figure out which bucket (first) free entry is. */
101 b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
102 /* Lock and coalesce. */
103 ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
104 test = layout->elem[2].base.off;
105 ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
106 == 1024 + sizeof(struct ntdb_used_record) + 14520);
107 /* Should tell us it's erased this one... */
108 ok1(test == NTDB_ERR_NOEXIST);
109 ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
110 ok1(free_record_length(ntdb, layout->elem[1].base.off)
111 == 1024 + sizeof(struct ntdb_used_record) + 14520);
112 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
114 ntdb_layout_free(layout);
116 /* Coalescing can be done due to two free records, then data */
117 layout = new_ntdb_layout();
118 ntdb_layout_add_freetable(layout);
119 ntdb_layout_add_free(layout, 1024, 0);
120 ntdb_layout_add_free(layout, 14488, 0);
121 ntdb_layout_add_used(layout, key, data, 6);
122 ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
123 /* NOMMAP is for lockcheck. */
124 ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
125 O_RDWR, 0, &tap_log_attr);
126 ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
127 ok1(free_record_length(ntdb, layout->elem[2].base.off) == 14488);
128 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
130 /* Figure out which bucket free entry is. */
131 b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
132 /* Lock and coalesce. */
133 ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
134 test = layout->elem[2].base.off;
135 ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
136 == 1024 + sizeof(struct ntdb_used_record) + 14488);
137 ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
138 ok1(free_record_length(ntdb, layout->elem[1].base.off)
139 == 1024 + sizeof(struct ntdb_used_record) + 14488);
140 ok1(test == NTDB_ERR_NOEXIST);
141 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
143 ntdb_layout_free(layout);
145 /* Coalescing can be done due to three free records, then EOF */
146 layout = new_ntdb_layout();
147 ntdb_layout_add_freetable(layout);
148 ntdb_layout_add_free(layout, 1024, 0);
149 ntdb_layout_add_free(layout, 512, 0);
150 ntdb_layout_add_free(layout, 13992, 0);
151 ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
152 /* NOMMAP is for lockcheck. */
153 ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP|MAYBE_NOSYNC,
154 O_RDWR, 0, &tap_log_attr);
155 ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
156 ok1(free_record_length(ntdb, layout->elem[2].base.off) == 512);
157 ok1(free_record_length(ntdb, layout->elem[3].base.off) == 13992);
158 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
160 /* Figure out which bucket free entry is. */
161 b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
162 /* Lock and coalesce. */
163 ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
164 test = layout->elem[2].base.off;
165 ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
166 == 1024 + sizeof(struct ntdb_used_record) + 512
167 + sizeof(struct ntdb_used_record) + 13992);
168 ok1(ntdb->file->allrecord_lock.count == 0
169 && ntdb->file->num_lockrecs == 0);
170 ok1(free_record_length(ntdb, layout->elem[1].base.off)
171 == 1024 + sizeof(struct ntdb_used_record) + 512
172 + sizeof(struct ntdb_used_record) + 13992);
173 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
175 ntdb_layout_free(layout);
177 ok1(tap_log_messages == 0);
178 return exit_status();