reftable/merged: adapt interface to allow reuse of iterators

Refactor the interfaces exposed by `struct reftable_merged_table` and
`struct merged_iter` such that they support iterator reuse. This is done
by separating initialization of the iterator and seeking on it.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2024-05-13 10:48:01 +02:00 committed by Junio C Hamano
parent 08efe69212
commit 369b84196e
4 changed files with 22 additions and 61 deletions

View file

@ -262,41 +262,6 @@ void merged_table_init_iter(struct reftable_merged_table *mt,
iterator_from_merged_iter(it, mi);
}
int reftable_merged_table_seek_ref(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name)
{
struct reftable_record rec = {
.type = BLOCK_TYPE_REF,
.u.ref = {
.refname = (char *)name,
},
};
merged_table_init_iter(mt, it, BLOCK_TYPE_REF);
return iterator_seek(it, &rec);
}
int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name, uint64_t update_index)
{
struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
.u.log = {
.refname = (char *)name,
.update_index = update_index,
} };
merged_table_init_iter(mt, it, BLOCK_TYPE_LOG);
return iterator_seek(it, &rec);
}
int reftable_merged_table_seek_log(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name)
{
uint64_t max = ~((uint64_t)0);
return reftable_merged_table_seek_log_at(mt, it, name, max);
}
uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *mt)
{
return mt->hash_id;

View file

@ -12,6 +12,7 @@ license that can be found in the LICENSE file or at
#include "basics.h"
#include "blocksource.h"
#include "constants.h"
#include "reader.h"
#include "record.h"
#include "test_framework.h"
@ -145,7 +146,10 @@ static void test_merged_between(void)
int i;
struct reftable_ref_record ref = { NULL };
struct reftable_iterator it = { NULL };
int err = reftable_merged_table_seek_ref(mt, &it, "a");
int err;
merged_table_init_iter(mt, &it, BLOCK_TYPE_REF);
err = reftable_iterator_seek_ref(&it, "a");
EXPECT_ERR(err);
err = reftable_iterator_next_ref(&it, &ref);
@ -217,14 +221,15 @@ static void test_merged(void)
struct reftable_reader **readers = NULL;
struct reftable_merged_table *mt =
merged_table_from_records(refs, &bs, &readers, sizes, bufs, 3);
struct reftable_iterator it = { NULL };
int err = reftable_merged_table_seek_ref(mt, &it, "a");
int err;
struct reftable_ref_record *out = NULL;
size_t len = 0;
size_t cap = 0;
int i = 0;
merged_table_init_iter(mt, &it, BLOCK_TYPE_REF);
err = reftable_iterator_seek_ref(&it, "a");
EXPECT_ERR(err);
EXPECT(reftable_merged_table_hash_id(mt) == GIT_SHA1_FORMAT_ID);
EXPECT(reftable_merged_table_min_update_index(mt) == 1);
@ -348,14 +353,15 @@ static void test_merged_logs(void)
struct reftable_reader **readers = NULL;
struct reftable_merged_table *mt = merged_table_from_log_records(
logs, &bs, &readers, sizes, bufs, 3);
struct reftable_iterator it = { NULL };
int err = reftable_merged_table_seek_log(mt, &it, "a");
int err;
struct reftable_log_record *out = NULL;
size_t len = 0;
size_t cap = 0;
int i = 0;
merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG);
err = reftable_iterator_seek_log(&it, "a");
EXPECT_ERR(err);
EXPECT(reftable_merged_table_hash_id(mt) == GIT_SHA1_FORMAT_ID);
EXPECT(reftable_merged_table_min_update_index(mt) == 1);
@ -377,7 +383,8 @@ static void test_merged_logs(void)
GIT_SHA1_RAWSZ));
}
err = reftable_merged_table_seek_log_at(mt, &it, "a", 2);
merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG);
err = reftable_iterator_seek_log_at(&it, "a", 2);
EXPECT_ERR(err);
reftable_log_record_release(&out[0]);
err = reftable_iterator_next_log(&it, &out[0]);

View file

@ -36,21 +36,6 @@ int reftable_new_merged_table(struct reftable_merged_table **dest,
struct reftable_table *stack, size_t n,
uint32_t hash_id);
/* returns an iterator positioned just before 'name' */
int reftable_merged_table_seek_ref(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name);
/* returns an iterator for log entry, at given update_index */
int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name, uint64_t update_index);
/* like reftable_merged_table_seek_log_at but look for the newest entry. */
int reftable_merged_table_seek_log(struct reftable_merged_table *mt,
struct reftable_iterator *it,
const char *name);
/* returns the max update_index covered by this merged table. */
uint64_t
reftable_merged_table_max_update_index(struct reftable_merged_table *mt);

View file

@ -925,7 +925,8 @@ static int stack_write_compact(struct reftable_stack *st,
goto done;
}
err = reftable_merged_table_seek_ref(mt, &it, "");
merged_table_init_iter(mt, &it, BLOCK_TYPE_REF);
err = reftable_iterator_seek_ref(&it, "");
if (err < 0)
goto done;
@ -949,7 +950,8 @@ static int stack_write_compact(struct reftable_stack *st,
}
reftable_iterator_destroy(&it);
err = reftable_merged_table_seek_log(mt, &it, "");
merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG);
err = reftable_iterator_seek_log(&it, "");
if (err < 0)
goto done;
@ -1340,9 +1342,11 @@ int reftable_stack_read_ref(struct reftable_stack *st, const char *refname,
int reftable_stack_read_log(struct reftable_stack *st, const char *refname,
struct reftable_log_record *log)
{
struct reftable_iterator it = { NULL };
struct reftable_merged_table *mt = reftable_stack_merged_table(st);
int err = reftable_merged_table_seek_log(mt, &it, refname);
struct reftable_iterator it = {0};
int err;
reftable_stack_init_log_iterator(st, &it);
err = reftable_iterator_seek_log(&it, refname);
if (err)
goto done;