reftable/block: fix binary search over restart counter
[alt-git.git] / reftable / merged_test.c
blobd0f77a3b8f51e0abb9af1162106c757d3fc3e553
1 /*
2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
9 #include "merged.h"
11 #include "system.h"
13 #include "basics.h"
14 #include "blocksource.h"
15 #include "reader.h"
16 #include "record.h"
17 #include "test_framework.h"
18 #include "reftable-merged.h"
19 #include "reftable-tests.h"
20 #include "reftable-generic.h"
21 #include "reftable-writer.h"
23 static void write_test_table(struct strbuf *buf,
24 struct reftable_ref_record refs[], int n)
26 uint64_t min = 0xffffffff;
27 uint64_t max = 0;
28 int i = 0;
29 int err;
31 struct reftable_write_options opts = {
32 .block_size = 256,
34 struct reftable_writer *w = NULL;
35 for (i = 0; i < n; i++) {
36 uint64_t ui = refs[i].update_index;
37 if (ui > max) {
38 max = ui;
40 if (ui < min) {
41 min = ui;
45 w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
46 reftable_writer_set_limits(w, min, max);
48 for (i = 0; i < n; i++) {
49 uint64_t before = refs[i].update_index;
50 int n = reftable_writer_add_ref(w, &refs[i]);
51 EXPECT(n == 0);
52 EXPECT(before == refs[i].update_index);
55 err = reftable_writer_close(w);
56 EXPECT_ERR(err);
58 reftable_writer_free(w);
61 static void write_test_log_table(struct strbuf *buf,
62 struct reftable_log_record logs[], int n,
63 uint64_t update_index)
65 int i = 0;
66 int err;
68 struct reftable_write_options opts = {
69 .block_size = 256,
70 .exact_log_message = 1,
72 struct reftable_writer *w = NULL;
73 w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
74 reftable_writer_set_limits(w, update_index, update_index);
76 for (i = 0; i < n; i++) {
77 int err = reftable_writer_add_log(w, &logs[i]);
78 EXPECT_ERR(err);
81 err = reftable_writer_close(w);
82 EXPECT_ERR(err);
84 reftable_writer_free(w);
87 static struct reftable_merged_table *
88 merged_table_from_records(struct reftable_ref_record **refs,
89 struct reftable_block_source **source,
90 struct reftable_reader ***readers, int *sizes,
91 struct strbuf *buf, size_t n)
93 struct reftable_merged_table *mt = NULL;
94 struct reftable_table *tabs;
95 int err;
97 REFTABLE_CALLOC_ARRAY(tabs, n);
98 REFTABLE_CALLOC_ARRAY(*readers, n);
99 REFTABLE_CALLOC_ARRAY(*source, n);
101 for (size_t i = 0; i < n; i++) {
102 write_test_table(&buf[i], refs[i], sizes[i]);
103 block_source_from_strbuf(&(*source)[i], &buf[i]);
105 err = reftable_new_reader(&(*readers)[i], &(*source)[i],
106 "name");
107 EXPECT_ERR(err);
108 reftable_table_from_reader(&tabs[i], (*readers)[i]);
111 err = reftable_new_merged_table(&mt, tabs, n, GIT_SHA1_FORMAT_ID);
112 EXPECT_ERR(err);
113 return mt;
116 static void readers_destroy(struct reftable_reader **readers, size_t n)
118 int i = 0;
119 for (; i < n; i++)
120 reftable_reader_free(readers[i]);
121 reftable_free(readers);
124 static void test_merged_between(void)
126 struct reftable_ref_record r1[] = { {
127 .refname = "b",
128 .update_index = 1,
129 .value_type = REFTABLE_REF_VAL1,
130 .value.val1 = { 1, 2, 3, 0 },
131 } };
132 struct reftable_ref_record r2[] = { {
133 .refname = "a",
134 .update_index = 2,
135 .value_type = REFTABLE_REF_DELETION,
136 } };
138 struct reftable_ref_record *refs[] = { r1, r2 };
139 int sizes[] = { 1, 1 };
140 struct strbuf bufs[2] = { STRBUF_INIT, STRBUF_INIT };
141 struct reftable_block_source *bs = NULL;
142 struct reftable_reader **readers = NULL;
143 struct reftable_merged_table *mt =
144 merged_table_from_records(refs, &bs, &readers, sizes, bufs, 2);
145 int i;
146 struct reftable_ref_record ref = { NULL };
147 struct reftable_iterator it = { NULL };
148 int err = reftable_merged_table_seek_ref(mt, &it, "a");
149 EXPECT_ERR(err);
151 err = reftable_iterator_next_ref(&it, &ref);
152 EXPECT_ERR(err);
153 EXPECT(ref.update_index == 2);
154 reftable_ref_record_release(&ref);
155 reftable_iterator_destroy(&it);
156 readers_destroy(readers, 2);
157 reftable_merged_table_free(mt);
158 for (i = 0; i < ARRAY_SIZE(bufs); i++) {
159 strbuf_release(&bufs[i]);
161 reftable_free(bs);
164 static void test_merged(void)
166 struct reftable_ref_record r1[] = {
168 .refname = "a",
169 .update_index = 1,
170 .value_type = REFTABLE_REF_VAL1,
171 .value.val1 = { 1 },
174 .refname = "b",
175 .update_index = 1,
176 .value_type = REFTABLE_REF_VAL1,
177 .value.val1 = { 1 },
180 .refname = "c",
181 .update_index = 1,
182 .value_type = REFTABLE_REF_VAL1,
183 .value.val1 = { 1 },
186 struct reftable_ref_record r2[] = { {
187 .refname = "a",
188 .update_index = 2,
189 .value_type = REFTABLE_REF_DELETION,
190 } };
191 struct reftable_ref_record r3[] = {
193 .refname = "c",
194 .update_index = 3,
195 .value_type = REFTABLE_REF_VAL1,
196 .value.val1 = { 2 },
199 .refname = "d",
200 .update_index = 3,
201 .value_type = REFTABLE_REF_VAL1,
202 .value.val1 = { 1 },
206 struct reftable_ref_record *want[] = {
207 &r2[0],
208 &r1[1],
209 &r3[0],
210 &r3[1],
213 struct reftable_ref_record *refs[] = { r1, r2, r3 };
214 int sizes[3] = { 3, 1, 2 };
215 struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT };
216 struct reftable_block_source *bs = NULL;
217 struct reftable_reader **readers = NULL;
218 struct reftable_merged_table *mt =
219 merged_table_from_records(refs, &bs, &readers, sizes, bufs, 3);
221 struct reftable_iterator it = { NULL };
222 int err = reftable_merged_table_seek_ref(mt, &it, "a");
223 struct reftable_ref_record *out = NULL;
224 size_t len = 0;
225 size_t cap = 0;
226 int i = 0;
228 EXPECT_ERR(err);
229 EXPECT(reftable_merged_table_hash_id(mt) == GIT_SHA1_FORMAT_ID);
230 EXPECT(reftable_merged_table_min_update_index(mt) == 1);
232 while (len < 100) { /* cap loops/recursion. */
233 struct reftable_ref_record ref = { NULL };
234 int err = reftable_iterator_next_ref(&it, &ref);
235 if (err > 0)
236 break;
238 REFTABLE_ALLOC_GROW(out, len + 1, cap);
239 out[len++] = ref;
241 reftable_iterator_destroy(&it);
243 EXPECT(ARRAY_SIZE(want) == len);
244 for (i = 0; i < len; i++) {
245 EXPECT(reftable_ref_record_equal(want[i], &out[i],
246 GIT_SHA1_RAWSZ));
248 for (i = 0; i < len; i++) {
249 reftable_ref_record_release(&out[i]);
251 reftable_free(out);
253 for (i = 0; i < 3; i++) {
254 strbuf_release(&bufs[i]);
256 readers_destroy(readers, 3);
257 reftable_merged_table_free(mt);
258 reftable_free(bs);
261 static struct reftable_merged_table *
262 merged_table_from_log_records(struct reftable_log_record **logs,
263 struct reftable_block_source **source,
264 struct reftable_reader ***readers, int *sizes,
265 struct strbuf *buf, size_t n)
267 struct reftable_merged_table *mt = NULL;
268 struct reftable_table *tabs;
269 int err;
271 REFTABLE_CALLOC_ARRAY(tabs, n);
272 REFTABLE_CALLOC_ARRAY(*readers, n);
273 REFTABLE_CALLOC_ARRAY(*source, n);
275 for (size_t i = 0; i < n; i++) {
276 write_test_log_table(&buf[i], logs[i], sizes[i], i + 1);
277 block_source_from_strbuf(&(*source)[i], &buf[i]);
279 err = reftable_new_reader(&(*readers)[i], &(*source)[i],
280 "name");
281 EXPECT_ERR(err);
282 reftable_table_from_reader(&tabs[i], (*readers)[i]);
285 err = reftable_new_merged_table(&mt, tabs, n, GIT_SHA1_FORMAT_ID);
286 EXPECT_ERR(err);
287 return mt;
290 static void test_merged_logs(void)
292 uint8_t hash1[GIT_SHA1_RAWSZ] = { 1 };
293 uint8_t hash2[GIT_SHA1_RAWSZ] = { 2 };
294 uint8_t hash3[GIT_SHA1_RAWSZ] = { 3 };
295 struct reftable_log_record r1[] = {
297 .refname = "a",
298 .update_index = 2,
299 .value_type = REFTABLE_LOG_UPDATE,
300 .value.update = {
301 .old_hash = hash2,
302 /* deletion */
303 .name = "jane doe",
304 .email = "jane@invalid",
305 .message = "message2",
309 .refname = "a",
310 .update_index = 1,
311 .value_type = REFTABLE_LOG_UPDATE,
312 .value.update = {
313 .old_hash = hash1,
314 .new_hash = hash2,
315 .name = "jane doe",
316 .email = "jane@invalid",
317 .message = "message1",
321 struct reftable_log_record r2[] = {
323 .refname = "a",
324 .update_index = 3,
325 .value_type = REFTABLE_LOG_UPDATE,
326 .value.update = {
327 .new_hash = hash3,
328 .name = "jane doe",
329 .email = "jane@invalid",
330 .message = "message3",
334 struct reftable_log_record r3[] = {
336 .refname = "a",
337 .update_index = 2,
338 .value_type = REFTABLE_LOG_DELETION,
341 struct reftable_log_record *want[] = {
342 &r2[0],
343 &r3[0],
344 &r1[1],
347 struct reftable_log_record *logs[] = { r1, r2, r3 };
348 int sizes[3] = { 2, 1, 1 };
349 struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT };
350 struct reftable_block_source *bs = NULL;
351 struct reftable_reader **readers = NULL;
352 struct reftable_merged_table *mt = merged_table_from_log_records(
353 logs, &bs, &readers, sizes, bufs, 3);
355 struct reftable_iterator it = { NULL };
356 int err = reftable_merged_table_seek_log(mt, &it, "a");
357 struct reftable_log_record *out = NULL;
358 size_t len = 0;
359 size_t cap = 0;
360 int i = 0;
362 EXPECT_ERR(err);
363 EXPECT(reftable_merged_table_hash_id(mt) == GIT_SHA1_FORMAT_ID);
364 EXPECT(reftable_merged_table_min_update_index(mt) == 1);
366 while (len < 100) { /* cap loops/recursion. */
367 struct reftable_log_record log = { NULL };
368 int err = reftable_iterator_next_log(&it, &log);
369 if (err > 0)
370 break;
372 REFTABLE_ALLOC_GROW(out, len + 1, cap);
373 out[len++] = log;
375 reftable_iterator_destroy(&it);
377 EXPECT(ARRAY_SIZE(want) == len);
378 for (i = 0; i < len; i++) {
379 EXPECT(reftable_log_record_equal(want[i], &out[i],
380 GIT_SHA1_RAWSZ));
383 err = reftable_merged_table_seek_log_at(mt, &it, "a", 2);
384 EXPECT_ERR(err);
385 reftable_log_record_release(&out[0]);
386 err = reftable_iterator_next_log(&it, &out[0]);
387 EXPECT_ERR(err);
388 EXPECT(reftable_log_record_equal(&out[0], &r3[0], GIT_SHA1_RAWSZ));
389 reftable_iterator_destroy(&it);
391 for (i = 0; i < len; i++) {
392 reftable_log_record_release(&out[i]);
394 reftable_free(out);
396 for (i = 0; i < 3; i++) {
397 strbuf_release(&bufs[i]);
399 readers_destroy(readers, 3);
400 reftable_merged_table_free(mt);
401 reftable_free(bs);
404 static void test_default_write_opts(void)
406 struct reftable_write_options opts = { 0 };
407 struct strbuf buf = STRBUF_INIT;
408 struct reftable_writer *w =
409 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
411 struct reftable_ref_record rec = {
412 .refname = "master",
413 .update_index = 1,
415 int err;
416 struct reftable_block_source source = { NULL };
417 struct reftable_table *tab = reftable_calloc(1, sizeof(*tab));
418 uint32_t hash_id;
419 struct reftable_reader *rd = NULL;
420 struct reftable_merged_table *merged = NULL;
422 reftable_writer_set_limits(w, 1, 1);
424 err = reftable_writer_add_ref(w, &rec);
425 EXPECT_ERR(err);
427 err = reftable_writer_close(w);
428 EXPECT_ERR(err);
429 reftable_writer_free(w);
431 block_source_from_strbuf(&source, &buf);
433 err = reftable_new_reader(&rd, &source, "filename");
434 EXPECT_ERR(err);
436 hash_id = reftable_reader_hash_id(rd);
437 EXPECT(hash_id == GIT_SHA1_FORMAT_ID);
439 reftable_table_from_reader(&tab[0], rd);
440 err = reftable_new_merged_table(&merged, tab, 1, GIT_SHA1_FORMAT_ID);
441 EXPECT_ERR(err);
443 reftable_reader_free(rd);
444 reftable_merged_table_free(merged);
445 strbuf_release(&buf);
448 /* XXX test refs_for(oid) */
450 int merged_test_main(int argc, const char *argv[])
452 RUN_TEST(test_merged_logs);
453 RUN_TEST(test_merged_between);
454 RUN_TEST(test_merged);
455 RUN_TEST(test_default_write_opts);
456 return 0;