reftable/block: make block iterators reseekable

Refactor the block iterators so that initialization and seeking are
different from one another. This makes the iterator trivially reseekable
by storing the pointer to the block at initialization time, which we can
then reuse on every seek.

This refactoring prepares the code for exposing a `reftable_iterator`
interface for blocks in a subsequent commit. Callsites are adjusted
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:23 +02:00
committed by Junio C Hamano
parent 156d79cef0
commit 6da48a5e00
5 changed files with 48 additions and 35 deletions

View File

@@ -79,12 +79,23 @@ struct block_iter {
.scratch = REFTABLE_BUF_INIT, \
}
/* Position `it` at start of the block */
void block_iter_seek_start(struct block_iter *it, const struct reftable_block *block);
/*
* Initialize the block iterator with the given block. The iterator will be
* positioned at the first record contained in the block. The block must remain
* valid until the end of the iterator's lifetime. It is valid to re-initialize
* iterators multiple times.
*/
void block_iter_init(struct block_iter *it, const struct reftable_block *block);
/* Position `it` to the `want` key in the block */
int block_iter_seek_key(struct block_iter *it, const struct reftable_block *block,
struct reftable_buf *want);
/* Position the initialized iterator at the first record of its block. */
void block_iter_seek_start(struct block_iter *it);
/*
* Position the initialized iterator at the desired record key. It is not an
* error in case the record cannot be found. If so, a subsequent call to
* `block_iter_next()` will indicate that the iterator is exhausted.
*/
int block_iter_seek_key(struct block_iter *it, struct reftable_buf *want);
/* return < 0 for error, 0 for OK, > 0 for EOF. */
int block_iter_next(struct block_iter *it, struct reftable_record *rec);