reftable: make reftable_record a tagged union

This reduces the amount of glue code, because we don't need a void
pointer or vtable within the structure.

The only snag is that reftable_index_record contain a strbuf, so it
cannot be zero-initialized. To address this, use reftable_new_record()
to return fresh instance, given a record type. Since
reftable_new_record() doesn't cause heap allocation anymore, it should
be balanced with reftable_record_release() rather than
reftable_record_destroy().

Thanks to Peff for the suggestion.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Han-Wen Nienhuys
2022-01-20 15:12:13 +00:00
committed by Junio C Hamano
parent 9391b88dab
commit 66c0dabab5
12 changed files with 334 additions and 337 deletions

View File

@ -26,8 +26,9 @@ static void test_block_read_write(void)
struct block_writer bw = {
.last_key = STRBUF_INIT,
};
struct reftable_ref_record ref = { NULL };
struct reftable_record rec = { NULL };
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
};
int i = 0;
int n;
struct block_reader br = { 0 };
@ -40,7 +41,6 @@ static void test_block_read_write(void)
block.source = malloc_block_source();
block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
header_off, hash_size(GIT_SHA1_FORMAT_ID));
reftable_record_from_ref(&rec, &ref);
for (i = 0; i < N; i++) {
char name[100];
@ -48,14 +48,14 @@ static void test_block_read_write(void)
snprintf(name, sizeof(name), "branch%02d", i);
memset(hash, i, sizeof(hash));
ref.refname = name;
ref.value_type = REFTABLE_REF_VAL1;
ref.value.val1 = hash;
rec.u.ref.refname = name;
rec.u.ref.value_type = REFTABLE_REF_VAL1;
rec.u.ref.value.val1 = hash;
names[i] = xstrdup(name);
n = block_writer_add(&bw, &rec);
ref.refname = NULL;
ref.value_type = REFTABLE_REF_DELETION;
rec.u.ref.refname = NULL;
rec.u.ref.value_type = REFTABLE_REF_DELETION;
EXPECT(n == 0);
}
@ -74,7 +74,7 @@ static void test_block_read_write(void)
if (r > 0) {
break;
}
EXPECT_STREQ(names[j], ref.refname);
EXPECT_STREQ(names[j], rec.u.ref.refname);
j++;
}
@ -92,7 +92,7 @@ static void test_block_read_write(void)
n = block_iter_next(&it, &rec);
EXPECT(n == 0);
EXPECT_STREQ(names[i], ref.refname);
EXPECT_STREQ(names[i], rec.u.ref.refname);
want.len--;
n = block_reader_seek(&br, &it, &want);
@ -100,7 +100,7 @@ static void test_block_read_write(void)
n = block_iter_next(&it, &rec);
EXPECT(n == 0);
EXPECT_STREQ(names[10 * (i / 10)], ref.refname);
EXPECT_STREQ(names[10 * (i / 10)], rec.u.ref.refname);
block_iter_close(&it);
}