reftable/block: rename block to block_data

The `reftable_block` structure associates a byte slice with a block
source. As such it only holds the data of a reftable block without
actually encoding any of the details for how to access that data.

Rename the structure to instead be called `reftable_block_data`. Besides
clarifying that this really only holds data, it also allows us to rename
the `reftable_block_reader` to `reftable_block` in the next commit, as
this is the structure that actually encapsulates access to the reftable
blocks.

Rename the `struct reftable_block_reader::block` member accordingly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2025-04-07 15:16:18 +02:00
committed by Junio C Hamano
parent fd888311fb
commit 2b3362c10d
8 changed files with 73 additions and 73 deletions

View File

@@ -210,16 +210,16 @@ int block_writer_finish(struct block_writer *w)
} }
static int read_block(struct reftable_block_source *source, static int read_block(struct reftable_block_source *source,
struct reftable_block *dest, uint64_t off, struct reftable_block_data *dest, uint64_t off,
uint32_t sz) uint32_t sz)
{ {
size_t size = block_source_size(source); size_t size = block_source_size(source);
block_source_return_block(dest); block_source_release_data(dest);
if (off >= size) if (off >= size)
return 0; return 0;
if (off + sz > size) if (off + sz > size)
sz = size - off; sz = size - off;
return block_source_read_block(source, dest, off, sz); return block_source_read_data(source, dest, off, sz);
} }
int block_reader_init(struct block_reader *br, int block_reader_init(struct block_reader *br,
@@ -236,19 +236,19 @@ int block_reader_init(struct block_reader *br,
uint8_t block_type; uint8_t block_type;
int err; int err;
err = read_block(source, &br->block, offset, guess_block_size); err = read_block(source, &br->block_data, offset, guess_block_size);
if (err < 0) if (err < 0)
goto done; goto done;
block_type = br->block.data[header_size]; block_type = br->block_data.data[header_size];
if (!reftable_is_block_type(block_type)) { if (!reftable_is_block_type(block_type)) {
err = REFTABLE_FORMAT_ERROR; err = REFTABLE_FORMAT_ERROR;
goto done; goto done;
} }
block_size = reftable_get_be24(br->block.data + header_size + 1); block_size = reftable_get_be24(br->block_data.data + header_size + 1);
if (block_size > guess_block_size) { if (block_size > guess_block_size) {
err = read_block(source, &br->block, offset, block_size); err = read_block(source, &br->block_data, offset, block_size);
if (err < 0) if (err < 0)
goto done; goto done;
} }
@@ -256,7 +256,7 @@ int block_reader_init(struct block_reader *br,
if (block_type == BLOCK_TYPE_LOG) { if (block_type == BLOCK_TYPE_LOG) {
uint32_t block_header_skip = 4 + header_size; uint32_t block_header_skip = 4 + header_size;
uLong dst_len = block_size - block_header_skip; uLong dst_len = block_size - block_header_skip;
uLong src_len = br->block.len - block_header_skip; uLong src_len = br->block_data.len - block_header_skip;
/* Log blocks specify the *uncompressed* size in their header. */ /* Log blocks specify the *uncompressed* size in their header. */
REFTABLE_ALLOC_GROW_OR_NULL(br->uncompressed_data, block_size, REFTABLE_ALLOC_GROW_OR_NULL(br->uncompressed_data, block_size,
@@ -267,7 +267,7 @@ int block_reader_init(struct block_reader *br,
} }
/* Copy over the block header verbatim. It's not compressed. */ /* Copy over the block header verbatim. It's not compressed. */
memcpy(br->uncompressed_data, br->block.data, block_header_skip); memcpy(br->uncompressed_data, br->block_data.data, block_header_skip);
if (!br->zstream) { if (!br->zstream) {
REFTABLE_CALLOC_ARRAY(br->zstream, 1); REFTABLE_CALLOC_ARRAY(br->zstream, 1);
@@ -285,7 +285,7 @@ int block_reader_init(struct block_reader *br,
goto done; goto done;
} }
br->zstream->next_in = br->block.data + block_header_skip; br->zstream->next_in = br->block_data.data + block_header_skip;
br->zstream->avail_in = src_len; br->zstream->avail_in = src_len;
br->zstream->next_out = br->uncompressed_data + block_header_skip; br->zstream->next_out = br->uncompressed_data + block_header_skip;
br->zstream->avail_out = dst_len; br->zstream->avail_out = dst_len;
@@ -310,21 +310,21 @@ int block_reader_init(struct block_reader *br,
} }
/* We're done with the input data. */ /* We're done with the input data. */
block_source_return_block(&br->block); block_source_release_data(&br->block_data);
br->block.data = br->uncompressed_data; br->block_data.data = br->uncompressed_data;
br->block.len = block_size; br->block_data.len = block_size;
full_block_size = src_len + block_header_skip - br->zstream->avail_in; full_block_size = src_len + block_header_skip - br->zstream->avail_in;
} else if (full_block_size == 0) { } else if (full_block_size == 0) {
full_block_size = block_size; full_block_size = block_size;
} else if (block_size < full_block_size && block_size < br->block.len && } else if (block_size < full_block_size && block_size < br->block_data.len &&
br->block.data[block_size] != 0) { br->block_data.data[block_size] != 0) {
/* If the block is smaller than the full block size, it is /* If the block is smaller than the full block size, it is
padded (data followed by '\0') or the next block is padded (data followed by '\0') or the next block is
unaligned. */ unaligned. */
full_block_size = block_size; full_block_size = block_size;
} }
restart_count = reftable_get_be16(br->block.data + block_size - 2); restart_count = reftable_get_be16(br->block_data.data + block_size - 2);
restart_off = block_size - 2 - 3 * restart_count; restart_off = block_size - 2 - 3 * restart_count;
br->block_type = block_type; br->block_type = block_type;
@@ -347,20 +347,20 @@ void block_reader_release(struct block_reader *br)
inflateEnd(br->zstream); inflateEnd(br->zstream);
reftable_free(br->zstream); reftable_free(br->zstream);
reftable_free(br->uncompressed_data); reftable_free(br->uncompressed_data);
block_source_return_block(&br->block); block_source_release_data(&br->block_data);
memset(br, 0, sizeof(*br)); memset(br, 0, sizeof(*br));
} }
uint8_t block_reader_type(const struct block_reader *r) uint8_t block_reader_type(const struct block_reader *r)
{ {
return r->block.data[r->header_off]; return r->block_data.data[r->header_off];
} }
int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key) int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key)
{ {
int off = br->header_off + 4, n; int off = br->header_off + 4, n;
struct string_view in = { struct string_view in = {
.buf = br->block.data + off, .buf = br->block_data.data + off,
.len = br->restart_off - off, .len = br->restart_off - off,
}; };
uint8_t extra = 0; uint8_t extra = 0;
@@ -378,12 +378,12 @@ int block_reader_first_key(const struct block_reader *br, struct reftable_buf *k
static uint32_t block_reader_restart_offset(const struct block_reader *br, size_t idx) static uint32_t block_reader_restart_offset(const struct block_reader *br, size_t idx)
{ {
return reftable_get_be24(br->block.data + br->restart_off + 3 * idx); return reftable_get_be24(br->block_data.data + br->restart_off + 3 * idx);
} }
void block_iter_seek_start(struct block_iter *it, const struct block_reader *br) void block_iter_seek_start(struct block_iter *it, const struct block_reader *br)
{ {
it->block = br->block.data; it->block = br->block_data.data;
it->block_len = br->restart_off; it->block_len = br->restart_off;
it->hash_size = br->hash_size; it->hash_size = br->hash_size;
reftable_buf_reset(&it->last_key); reftable_buf_reset(&it->last_key);
@@ -401,7 +401,7 @@ static int restart_needle_less(size_t idx, void *_args)
struct restart_needle_less_args *args = _args; struct restart_needle_less_args *args = _args;
uint32_t off = block_reader_restart_offset(args->reader, idx); uint32_t off = block_reader_restart_offset(args->reader, idx);
struct string_view in = { struct string_view in = {
.buf = args->reader->block.data + off, .buf = args->reader->block_data.data + off,
.len = args->reader->restart_off - off, .len = args->reader->restart_off - off,
}; };
uint64_t prefix_len, suffix_len; uint64_t prefix_len, suffix_len;
@@ -528,7 +528,7 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
it->next_off = block_reader_restart_offset(br, i - 1); it->next_off = block_reader_restart_offset(br, i - 1);
else else
it->next_off = br->header_off + 4; it->next_off = br->header_off + 4;
it->block = br->block.data; it->block = br->block_data.data;
it->block_len = br->restart_off; it->block_len = br->restart_off;
it->hash_size = br->hash_size; it->hash_size = br->hash_size;

View File

@@ -71,7 +71,7 @@ struct block_reader {
uint32_t header_off; uint32_t header_off;
/* the memory block */ /* the memory block */
struct reftable_block block; struct reftable_block_data block_data;
uint32_t hash_size; uint32_t hash_size;
/* Uncompressed data for log entries. */ /* Uncompressed data for log entries. */

View File

@@ -13,15 +13,15 @@
#include "reftable-blocksource.h" #include "reftable-blocksource.h"
#include "reftable-error.h" #include "reftable-error.h"
void block_source_return_block(struct reftable_block *block) void block_source_release_data(struct reftable_block_data *data)
{ {
struct reftable_block_source source = block->source; struct reftable_block_source source = data->source;
if (block && source.ops) if (data && source.ops)
source.ops->return_block(source.arg, block); source.ops->release_data(source.arg, data);
block->data = NULL; data->data = NULL;
block->len = 0; data->len = 0;
block->source.ops = NULL; data->source.ops = NULL;
block->source.arg = NULL; data->source.arg = NULL;
} }
void block_source_close(struct reftable_block_source *source) void block_source_close(struct reftable_block_source *source)
@@ -34,11 +34,11 @@ void block_source_close(struct reftable_block_source *source)
source->ops = NULL; source->ops = NULL;
} }
ssize_t block_source_read_block(struct reftable_block_source *source, ssize_t block_source_read_data(struct reftable_block_source *source,
struct reftable_block *dest, uint64_t off, struct reftable_block_data *dest, uint64_t off,
uint32_t size) uint32_t size)
{ {
ssize_t result = source->ops->read_block(source->arg, dest, off, size); ssize_t result = source->ops->read_data(source->arg, dest, off, size);
dest->source = *source; dest->source = *source;
return result; return result;
} }
@@ -48,7 +48,7 @@ uint64_t block_source_size(struct reftable_block_source *source)
return source->ops->size(source->arg); return source->ops->size(source->arg);
} }
static void reftable_buf_return_block(void *b REFTABLE_UNUSED, struct reftable_block *dest) static void reftable_buf_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest)
{ {
if (dest->len) if (dest->len)
memset(dest->data, 0xff, dest->len); memset(dest->data, 0xff, dest->len);
@@ -59,8 +59,8 @@ static void reftable_buf_close(void *b REFTABLE_UNUSED)
{ {
} }
static ssize_t reftable_buf_read_block(void *v, struct reftable_block *dest, static ssize_t reftable_buf_read_data(void *v, struct reftable_block_data *dest,
uint64_t off, uint32_t size) uint64_t off, uint32_t size)
{ {
struct reftable_buf *b = v; struct reftable_buf *b = v;
assert(off + size <= b->len); assert(off + size <= b->len);
@@ -79,8 +79,8 @@ static uint64_t reftable_buf_size(void *b)
static struct reftable_block_source_vtable reftable_buf_vtable = { static struct reftable_block_source_vtable reftable_buf_vtable = {
.size = &reftable_buf_size, .size = &reftable_buf_size,
.read_block = &reftable_buf_read_block, .read_data = &reftable_buf_read_data,
.return_block = &reftable_buf_return_block, .release_data = &reftable_buf_release_data,
.close = &reftable_buf_close, .close = &reftable_buf_close,
}; };
@@ -102,7 +102,7 @@ static uint64_t file_size(void *b)
return ((struct file_block_source *)b)->size; return ((struct file_block_source *)b)->size;
} }
static void file_return_block(void *b REFTABLE_UNUSED, struct reftable_block *dest REFTABLE_UNUSED) static void file_release_data(void *b REFTABLE_UNUSED, struct reftable_block_data *dest REFTABLE_UNUSED)
{ {
} }
@@ -113,8 +113,8 @@ static void file_close(void *v)
reftable_free(b); reftable_free(b);
} }
static ssize_t file_read_block(void *v, struct reftable_block *dest, uint64_t off, static ssize_t file_read_data(void *v, struct reftable_block_data *dest, uint64_t off,
uint32_t size) uint32_t size)
{ {
struct file_block_source *b = v; struct file_block_source *b = v;
assert(off + size <= b->size); assert(off + size <= b->size);
@@ -125,8 +125,8 @@ static ssize_t file_read_block(void *v, struct reftable_block *dest, uint64_t of
static struct reftable_block_source_vtable file_vtable = { static struct reftable_block_source_vtable file_vtable = {
.size = &file_size, .size = &file_size,
.read_block = &file_read_block, .read_data = &file_read_data,
.return_block = &file_return_block, .release_data = &file_release_data,
.close = &file_close, .close = &file_close,
}; };

View File

@@ -12,7 +12,7 @@
#include "system.h" #include "system.h"
struct reftable_block_source; struct reftable_block_source;
struct reftable_block; struct reftable_block_data;
struct reftable_buf; struct reftable_buf;
/* /*
@@ -24,9 +24,9 @@ void block_source_close(struct reftable_block_source *source);
/* /*
* Read a block of length `size` from the source at the given `off`. * Read a block of length `size` from the source at the given `off`.
*/ */
ssize_t block_source_read_block(struct reftable_block_source *source, ssize_t block_source_read_data(struct reftable_block_source *source,
struct reftable_block *dest, uint64_t off, struct reftable_block_data *dest, uint64_t off,
uint32_t size); uint32_t size);
/* /*
* Return the total length of the underlying resource. * Return the total length of the underlying resource.
@@ -37,7 +37,7 @@ uint64_t block_source_size(struct reftable_block_source *source);
* Return a block to its original source, releasing any resources associated * Return a block to its original source, releasing any resources associated
* with it. * with it.
*/ */
void block_source_return_block(struct reftable_block *block); void block_source_release_data(struct reftable_block_data *data);
/* Create an in-memory block source for reading reftables. */ /* Create an in-memory block source for reading reftables. */
void block_source_from_buf(struct reftable_block_source *bs, void block_source_from_buf(struct reftable_block_source *bs,

View File

@@ -114,7 +114,7 @@ static void indexed_table_ref_iter_close(void *p)
{ {
struct indexed_table_ref_iter *it = p; struct indexed_table_ref_iter *it = p;
block_iter_close(&it->cur); block_iter_close(&it->cur);
block_source_return_block(&it->block_reader.block); block_source_release_data(&it->block_reader.block_data);
reftable_free(it->offsets); reftable_free(it->offsets);
reftable_buf_release(&it->oid); reftable_buf_release(&it->oid);
} }
@@ -128,7 +128,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
return 1; return 1;
} }
block_source_return_block(&it->block_reader.block); block_source_release_data(&it->block_reader.block_data);
off = it->offsets[it->offset_idx++]; off = it->offsets[it->offset_idx++];
err = table_init_block_reader(it->table, &it->block_reader, off, err = table_init_block_reader(it->table, &it->block_reader, off,

View File

@@ -21,7 +21,7 @@ struct reftable_block_source {
/* a contiguous segment of bytes. It keeps track of its generating block_source /* a contiguous segment of bytes. It keeps track of its generating block_source
* so it can return itself into the pool. */ * so it can return itself into the pool. */
struct reftable_block { struct reftable_block_data {
uint8_t *data; uint8_t *data;
size_t len; size_t len;
struct reftable_block_source source; struct reftable_block_source source;
@@ -29,20 +29,20 @@ struct reftable_block {
/* block_source_vtable are the operations that make up block_source */ /* block_source_vtable are the operations that make up block_source */
struct reftable_block_source_vtable { struct reftable_block_source_vtable {
/* returns the size of a block source */ /* Returns the size of a block source. */
uint64_t (*size)(void *source); uint64_t (*size)(void *source);
/* /*
* Reads a segment from the block source. It is an error to read beyond * Reads a segment from the block source. It is an error to read beyond
* the end of the block. * the end of the block.
*/ */
ssize_t (*read_block)(void *source, struct reftable_block *dest, ssize_t (*read_data)(void *source, struct reftable_block_data *dest,
uint64_t off, uint32_t size); uint64_t off, uint32_t size);
/* mark the block as read; may return the data back to malloc */ /* Mark the block as read; may release the data. */
void (*return_block)(void *source, struct reftable_block *blockp); void (*release_data)(void *source, struct reftable_block_data *data);
/* release all resources associated with the block source */ /* Release all resources associated with the block source. */
void (*close)(void *source); void (*close)(void *source);
}; };

View File

@@ -320,7 +320,7 @@ static int table_iter_seek_linear(struct table_iter *ti,
* as we have more than three blocks we would have an index, so * as we have more than three blocks we would have an index, so
* we would not do a linear search there anymore. * we would not do a linear search there anymore.
*/ */
memset(&next.br.block, 0, sizeof(next.br.block)); memset(&next.br.block_data, 0, sizeof(next.br.block_data));
next.br.zstream = NULL; next.br.zstream = NULL;
next.br.uncompressed_data = NULL; next.br.uncompressed_data = NULL;
next.br.uncompressed_cap = 0; next.br.uncompressed_cap = 0;
@@ -526,8 +526,8 @@ int reftable_table_init_log_iterator(struct reftable_table *t,
int reftable_table_new(struct reftable_table **out, int reftable_table_new(struct reftable_table **out,
struct reftable_block_source *source, char const *name) struct reftable_block_source *source, char const *name)
{ {
struct reftable_block footer = { 0 }; struct reftable_block_data footer = { 0 };
struct reftable_block header = { 0 }; struct reftable_block_data header = { 0 };
struct reftable_table *t; struct reftable_table *t;
uint64_t file_size = block_source_size(source); uint64_t file_size = block_source_size(source);
uint32_t read_size; uint32_t read_size;
@@ -550,7 +550,7 @@ int reftable_table_new(struct reftable_table **out,
goto done; goto done;
} }
bytes_read = block_source_read_block(source, &header, 0, read_size); bytes_read = block_source_read_data(source, &header, 0, read_size);
if (bytes_read < 0 || (size_t)bytes_read != read_size) { if (bytes_read < 0 || (size_t)bytes_read != read_size) {
err = REFTABLE_IO_ERROR; err = REFTABLE_IO_ERROR;
goto done; goto done;
@@ -576,8 +576,8 @@ int reftable_table_new(struct reftable_table **out,
t->hash_id = 0; t->hash_id = 0;
t->refcount = 1; t->refcount = 1;
bytes_read = block_source_read_block(source, &footer, t->size, bytes_read = block_source_read_data(source, &footer, t->size,
footer_size(t->version)); footer_size(t->version));
if (bytes_read < 0 || (size_t)bytes_read != footer_size(t->version)) { if (bytes_read < 0 || (size_t)bytes_read != footer_size(t->version)) {
err = REFTABLE_IO_ERROR; err = REFTABLE_IO_ERROR;
goto done; goto done;
@@ -590,8 +590,8 @@ int reftable_table_new(struct reftable_table **out,
*out = t; *out = t;
done: done:
block_source_return_block(&footer); block_source_release_data(&footer);
block_source_return_block(&header); block_source_release_data(&header);
if (err) { if (err) {
if (t) if (t)
reftable_free(t->name); reftable_free(t->name);

View File

@@ -23,22 +23,22 @@ static void t_buffer(void)
{ {
struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_buf buf = REFTABLE_BUF_INIT;
struct reftable_block_source source = { 0 }; struct reftable_block_source source = { 0 };
struct reftable_block out = { 0 }; struct reftable_block_data out = { 0 };
int n; int n;
uint8_t in[] = "hello"; uint8_t in[] = "hello";
check(!reftable_buf_add(&buf, in, sizeof(in))); check(!reftable_buf_add(&buf, in, sizeof(in)));
block_source_from_buf(&source, &buf); block_source_from_buf(&source, &buf);
check_int(block_source_size(&source), ==, 6); check_int(block_source_size(&source), ==, 6);
n = block_source_read_block(&source, &out, 0, sizeof(in)); n = block_source_read_data(&source, &out, 0, sizeof(in));
check_int(n, ==, sizeof(in)); check_int(n, ==, sizeof(in));
check(!memcmp(in, out.data, n)); check(!memcmp(in, out.data, n));
block_source_return_block(&out); block_source_release_data(&out);
n = block_source_read_block(&source, &out, 1, 2); n = block_source_read_data(&source, &out, 1, 2);
check_int(n, ==, 2); check_int(n, ==, 2);
check(!memcmp(out.data, "el", 2)); check(!memcmp(out.data, "el", 2));
block_source_return_block(&out); block_source_release_data(&out);
block_source_close(&source); block_source_close(&source);
reftable_buf_release(&buf); reftable_buf_release(&buf);
} }