forked from msysgit/git
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reftable/reader: make table iterator reseekable
In 67ce50b (Merge branch 'ps/reftable-reusable-iterator', 2024-05-30) we have refactored the interface of reftable iterators such that they can be reused in theory. This patch series only landed the required changes on the interface level, but didn't yet implement the actual logic to make iterators reusable. As it turns out almost all of the infrastructure already does support re-seeking. The only exception is the table iterator, which does not reset its `is_finished` bit. Do so and add a couple of tests that verify that we can re-seek iterators. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
- Loading branch information
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "test-lib.h" | ||
#include "lib-reftable.h" | ||
#include "reftable/blocksource.h" | ||
#include "reftable/reader.h" | ||
|
||
static int t_reader_seek_once(void) | ||
{ | ||
struct reftable_ref_record records[] = { | ||
{ | ||
.refname = (char *) "refs/heads/main", | ||
.value_type = REFTABLE_REF_VAL1, | ||
.value.val1 = { 42 }, | ||
}, | ||
}; | ||
struct reftable_block_source source = { 0 }; | ||
struct reftable_ref_record ref = { 0 }; | ||
struct reftable_iterator it = { 0 }; | ||
struct reftable_reader *reader; | ||
struct strbuf buf = STRBUF_INIT; | ||
int ret; | ||
|
||
t_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); | ||
block_source_from_strbuf(&source, &buf); | ||
|
||
ret = reftable_reader_new(&reader, &source, "name"); | ||
check(!ret); | ||
|
||
reftable_reader_init_ref_iterator(reader, &it); | ||
ret = reftable_iterator_seek_ref(&it, ""); | ||
check(!ret); | ||
ret = reftable_iterator_next_ref(&it, &ref); | ||
check(!ret); | ||
|
||
ret = reftable_ref_record_equal(&ref, &records[0], GIT_SHA1_RAWSZ); | ||
check_int(ret, ==, 1); | ||
|
||
ret = reftable_iterator_next_ref(&it, &ref); | ||
check_int(ret, ==, 1); | ||
|
||
reftable_ref_record_release(&ref); | ||
reftable_iterator_destroy(&it); | ||
reftable_reader_decref(reader); | ||
strbuf_release(&buf); | ||
return 0; | ||
} | ||
|
||
static int t_reader_reseek(void) | ||
{ | ||
struct reftable_ref_record records[] = { | ||
{ | ||
.refname = (char *) "refs/heads/main", | ||
.value_type = REFTABLE_REF_VAL1, | ||
.value.val1 = { 42 }, | ||
}, | ||
}; | ||
struct reftable_block_source source = { 0 }; | ||
struct reftable_ref_record ref = { 0 }; | ||
struct reftable_iterator it = { 0 }; | ||
struct reftable_reader *reader; | ||
struct strbuf buf = STRBUF_INIT; | ||
int ret; | ||
|
||
t_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); | ||
block_source_from_strbuf(&source, &buf); | ||
|
||
ret = reftable_reader_new(&reader, &source, "name"); | ||
check(!ret); | ||
|
||
reftable_reader_init_ref_iterator(reader, &it); | ||
|
||
for (size_t i = 0; i < 5; i++) { | ||
ret = reftable_iterator_seek_ref(&it, ""); | ||
check(!ret); | ||
ret = reftable_iterator_next_ref(&it, &ref); | ||
check(!ret); | ||
|
||
ret = reftable_ref_record_equal(&ref, &records[0], GIT_SHA1_RAWSZ); | ||
check_int(ret, ==, 1); | ||
|
||
ret = reftable_iterator_next_ref(&it, &ref); | ||
check_int(ret, ==, 1); | ||
} | ||
|
||
reftable_ref_record_release(&ref); | ||
reftable_iterator_destroy(&it); | ||
reftable_reader_decref(reader); | ||
strbuf_release(&buf); | ||
return 0; | ||
} | ||
|
||
int cmd_main(int argc UNUSED, const char *argv[] UNUSED) | ||
{ | ||
TEST(t_reader_seek_once(), "reader can seek once"); | ||
TEST(t_reader_reseek(), "reader can reseek multiple times"); | ||
return test_done(); | ||
} |