aboutsummaryrefslogtreecommitdiff
path: root/reftable/table.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-04-07 15:16:17 +0200
committerJunio C Hamano <gitster@pobox.com>2025-04-07 14:53:10 -0700
commitfd888311fbc95b0cbb3c9e580dc6f7277bb7bf7f (patch)
treeb9a91396cb5f926f6360f3445b42986d6b4311f8 /reftable/table.c
parentba620d296ab7bcd93fcedfe13b265f84df1ed1eb (diff)
downloadgit-fd888311fbc95b0cbb3c9e580dc6f7277bb7bf7f.tar.xz
reftable/table: move reading block into block reader
The logic to read blocks from a reftable is scattered across both the table and the block subsystems. Besides causing somewhat fuzzy responsibilities, it also means that we have to awkwardly pass around the ownership of blocks between the subsystems. Refactor the code so that we stop passing the block when initializing a reader, but instead by passing in the block source plus the offset at which we're supposed to read a block. Like this, the ownership of the block itself doesn't need to get handed over as the block reader is the one owning the block right from the start. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'reftable/table.c')
-rw-r--r--reftable/table.c65
1 files changed, 6 insertions, 59 deletions
diff --git a/reftable/table.c b/reftable/table.c
index ec84545707..7c0f1c9e6e 100644
--- a/reftable/table.c
+++ b/reftable/table.c
@@ -30,23 +30,6 @@ table_offsets_for(struct reftable_table *t, uint8_t typ)
abort();
}
-static int table_get_block(struct reftable_table *t,
- struct reftable_block *dest, uint64_t off,
- uint32_t sz)
-{
- ssize_t bytes_read;
- if (off >= t->size)
- return 0;
- if (off + sz > t->size)
- sz = t->size - off;
-
- bytes_read = block_source_read_block(&t->source, dest, off, sz);
- if (bytes_read < 0)
- return (int)bytes_read;
-
- return 0;
-}
-
enum reftable_hash reftable_table_hash_id(struct reftable_table *t)
{
return t->hash_id;
@@ -180,64 +163,28 @@ static void table_iter_block_done(struct table_iter *ti)
block_iter_reset(&ti->bi);
}
-static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off,
- int version)
-{
- int32_t result = 0;
-
- if (off == 0) {
- data += header_size(version);
- }
-
- *typ = data[0];
- if (reftable_is_block_type(*typ)) {
- result = reftable_get_be24(data + 1);
- }
- return result;
-}
-
int table_init_block_reader(struct reftable_table *t, struct block_reader *br,
uint64_t next_off, uint8_t want_typ)
{
- int32_t guess_block_size = t->block_size ? t->block_size :
- DEFAULT_BLOCK_SIZE;
- struct reftable_block block = { NULL };
- uint8_t block_typ = 0;
- int err = 0;
uint32_t header_off = next_off ? 0 : header_size(t->version);
- int32_t block_size = 0;
+ int err;
if (next_off >= t->size)
return 1;
- err = table_get_block(t, &block, next_off, guess_block_size);
+ err = block_reader_init(br, &t->source, next_off, header_off,
+ t->block_size, hash_size(t->hash_id));
if (err < 0)
goto done;
- block_size = extract_block_size(block.data, &block_typ, next_off,
- t->version);
- if (block_size < 0) {
- err = block_size;
- goto done;
- }
- if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) {
+ if (want_typ != BLOCK_TYPE_ANY && br->block_type != want_typ) {
err = 1;
goto done;
}
- if (block_size > guess_block_size) {
- block_source_return_block(&block);
- err = table_get_block(t, &block, next_off, block_size);
- if (err < 0) {
- goto done;
- }
- }
-
- err = block_reader_init(br, &block, header_off, t->block_size,
- hash_size(t->hash_id));
done:
- block_source_return_block(&block);
-
+ if (err)
+ block_reader_release(br);
return err;
}