git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 05/16] reftable/table: move reading block into block reader

From
Justin Tobler <jltobler@gmail.com>
Date
Apr 2, 2025, 20:13 UTC
Message-ID
<umaolgemnks4g4cgwgnurl7alic2a3pzrfx2ws3s7uxypiedia@7mwqxvhw2zxg>
In-Reply-To
<20250331-pks-reftable-polishing-v1-5-ebed5247434c@pks.im>
On 25/03/31 10:41AM, Patrick Steinhardt wrote:
Show 8 quoted lines
> 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 we're supposed to read a block. Like this, the ownership of the
s/we we're/we're/
Show 42 quoted lines
> 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>
> ---
>  reftable/block.c                | 87 ++++++++++++++++++++++++++---------------
>  reftable/block.h                |  8 ++--
>  reftable/table.c                | 65 +++---------------------------
>  t/unit-tests/t-reftable-block.c | 76 ++++++++++++++++++-----------------
>  4 files changed, 107 insertions(+), 129 deletions(-)
> 
> diff --git a/reftable/block.c b/reftable/block.c
> index f2567a8f0fd..2517108b8ef 100644
> --- a/reftable/block.c
> +++ b/reftable/block.c
> @@ -209,31 +209,57 @@ int block_writer_finish(struct block_writer *w)
>  	return w->next;
>  }
>  
> -int block_reader_init(struct block_reader *br, struct reftable_block *block,
> -		      uint32_t header_off, uint32_t table_block_size,
> -		      uint32_t hash_size)
> +static int read_block(struct reftable_block_source *source,
> +		      struct reftable_block *dest, uint64_t off,
> +		      uint32_t sz)
>  {
> +	size_t size = block_source_size(source);
> +	block_source_return_block(dest);
> +	if (off >= size)
> +		return 0;
> +	if (off + sz > size)
> +		sz = size - off;
> +	return block_source_read_block(source, dest, off, sz);
> +}
> +
> +int block_reader_init(struct block_reader *br,
> +		      struct reftable_block_source *source,
> +		      uint32_t offset, uint32_t header_size,
> +		      uint32_t table_block_size, uint32_t hash_size)
> +{
> +	uint32_t guess_block_size = table_block_size ?
> +		table_block_size : DEFAULT_BLOCK_SIZE;

Out of curiousity, in what scenarios would the table not know the block size and we have to rely on the guess?

Show 13 quoted lines
>  	uint32_t full_block_size = table_block_size;
> -	uint8_t typ = block->data[header_off];
> -	uint32_t sz = reftable_get_be24(block->data + header_off + 1);
>  	uint16_t restart_count;
>  	uint32_t restart_off;
> +	uint32_t block_size;
> +	uint8_t block_type;
>  	int err;
>  
> -	block_source_return_block(&br->block);
> +	err = read_block(source, &br->block, offset, guess_block_size);
> +	if (err < 0)
> +		goto done;

Ok, so now `block_reader_init()` handles reading the block itself and no longer relies on the read block being provided to it. This makes block ownership more self-contained and clear.

Show 18 quoted lines
> -	if (!reftable_is_block_type(typ)) {
> -		err =  REFTABLE_FORMAT_ERROR;
> +	block_type = br->block.data[header_size];
> +	if (!reftable_is_block_type(block_type)) {
> +		err = REFTABLE_FORMAT_ERROR;
>  		goto done;
>  	}
>  
> -	if (typ == BLOCK_TYPE_LOG) {
> -		uint32_t block_header_skip = 4 + header_off;
> -		uLong dst_len = sz - block_header_skip;
> -		uLong src_len = block->len - block_header_skip;
> +	block_size = reftable_get_be24(br->block.data + header_size + 1);
> +	if (block_size > guess_block_size) {
> +		err = read_block(source, &br->block, offset, block_size);
> +		if (err < 0)
> +			goto done;
> +	}

Instead of relying on `table_init_block_reader()` to determine if `guess_block_size` was correct and reread the block, this is now handled as part of the initial `block_reader_init()`. Make sense.

[snip]
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 12 of 55 in “reftable: overhaul the API to expose access to blocks”
  1. 00/16 reftable: overhaul the API to expose access to blocksPatrick Steinhardt, Mar 31, 2025
  2. 01/16 reftable: fix formatting of the license headerPatrick Steinhardt, Mar 31, 2025
  3. 03/16 reftable/blocksource: consolidate code into a single filePatrick Steinhardt, Mar 31, 2025
  4. Justin ToblerApr 2, 2025
  5. Karthik NayakApr 3, 2025
  6. 02/16 reftable/reader: rename data structure to "table"Patrick Steinhardt, Mar 31, 2025
  7. 04/16 reftable/block: simplify how we track restart pointsPatrick Steinhardt, Mar 31, 2025
  8. Justin ToblerApr 2, 2025
  9. Karthik NayakApr 3, 2025
  10. Patrick SteinhardtApr 7, 2025
  11. 05/16 reftable/table: move reading block into block readerPatrick Steinhardt, Mar 31, 2025
  12. Justin ToblerApr 2, 2025
  13. Patrick SteinhardtApr 7, 2025
  14. 06/16 reftable/block: rename `block` to `block_data`Patrick Steinhardt, Mar 31, 2025
  15. Justin ToblerApr 2, 2025
  16. Patrick SteinhardtApr 7, 2025
  17. 07/16 reftable/block: rename `block_reader` to `reftable_block`Patrick Steinhardt, Mar 31, 2025
  18. Justin ToblerApr 2, 2025
  19. Patrick SteinhardtApr 7, 2025
  20. 08/16 git-zlib: use `struct z_stream_s` instead of typedefPatrick Steinhardt, Mar 31, 2025
  21. 10/16 reftable/block: store block pointer in the block iteratorPatrick Steinhardt, Mar 31, 2025
  22. Justin ToblerApr 2, 2025
  23. Patrick SteinhardtApr 7, 2025
  24. 09/16 reftable/block: create public interface for reading blocksPatrick Steinhardt, Mar 31, 2025
  25. 11/16 reftable/block: make block iterators reseekablePatrick Steinhardt, Mar 31, 2025
  26. Justin ToblerApr 2, 2025
  27. Patrick SteinhardtApr 7, 2025
  28. 12/16 reftable/block: expose a generic iterator over reftable recordsPatrick Steinhardt, Mar 31, 2025
  29. 13/16 reftable/table: add `reftable_table` to the public interfacePatrick Steinhardt, Mar 31, 2025
  30. 14/16 reftable/table: introduce iterator for table blocksPatrick Steinhardt, Mar 31, 2025
  31. Junio C HamanoApr 1, 2025
  32. Patrick SteinhardtApr 2, 2025
  33. Justin ToblerApr 2, 2025
  34. Patrick SteinhardtApr 7, 2025
  35. 15/16 reftable/constants: make block types part of the public interfacePatrick Steinhardt, Mar 31, 2025
  36. 16/16 reftable/table: move printing logic into test helperPatrick Steinhardt, Mar 31, 2025
  37. Justin ToblerApr 2, 2025
  38. 00/16 reftable: overhaul the API to expose access to blocksPatrick Steinhardt, Apr 7, 2025
  39. 01/16 reftable: fix formatting of the license headerPatrick Steinhardt, Apr 7, 2025
  40. 03/16 reftable/blocksource: consolidate code into a single filePatrick Steinhardt, Apr 7, 2025
  41. 02/16 reftable/reader: rename data structure to "table"Patrick Steinhardt, Apr 7, 2025
  42. 04/16 reftable/block: simplify how we track restart pointsPatrick Steinhardt, Apr 7, 2025
  43. 06/16 reftable/block: rename `block` to `block_data`Patrick Steinhardt, Apr 7, 2025
  44. 05/16 reftable/table: move reading block into block readerPatrick Steinhardt, Apr 7, 2025
  45. 07/16 reftable/block: rename `block_reader` to `reftable_block`Patrick Steinhardt, Apr 7, 2025
  46. 08/16 git-zlib: use `struct z_stream_s` instead of typedefPatrick Steinhardt, Apr 7, 2025
  47. 09/16 reftable/block: create public interface for reading blocksPatrick Steinhardt, Apr 7, 2025
  48. 10/16 reftable/block: store block pointer in the block iteratorPatrick Steinhardt, Apr 7, 2025
  49. 11/16 reftable/block: make block iterators reseekablePatrick Steinhardt, Apr 7, 2025
  50. 12/16 reftable/block: expose a generic iterator over reftable recordsPatrick Steinhardt, Apr 7, 2025
  51. 13/16 reftable/table: add `reftable_table` to the public interfacePatrick Steinhardt, Apr 7, 2025
  52. 14/16 reftable/table: introduce iterator for table blocksPatrick Steinhardt, Apr 7, 2025
  53. 15/16 reftable/constants: make block types part of the public interfacePatrick Steinhardt, Apr 7, 2025
  54. 16/16 reftable/table: move printing logic into test helperPatrick Steinhardt, Apr 7, 2025
  55. Justin ToblerApr 14, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.