Skip to content

Commit e001118

Browse files
pks-tgitster
authored andcommitted
reftable/table: move printing logic into test helper
The logic to print individual blocks in a table is hosted in the reftable library. This is only the case due to historical reasons though because users of the library had no interfaces to read blocks one by one. Otherwise, printing individual blocks has no place in the reftable library given that the format will not be generic in the first place. We have now grown a public interface to iterate through blocks contained in a table, and thus we can finally move the logic to print them into the test helper. Move over the logic and refactor it accordingly. Note that the iterator also trivially allows us to access index sections, which we previously didn't print at all. This omission wasn't intentional though, so start dumping those sections as well so that we can assert that indices are written as expected. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0f8ee94 commit e001118

File tree

4 files changed

+77
-69
lines changed

4 files changed

+77
-69
lines changed

reftable/reftable-table.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ uint64_t reftable_table_max_update_index(struct reftable_table *t);
9797
/* return the min_update_index for a table */
9898
uint64_t reftable_table_min_update_index(struct reftable_table *t);
9999

100-
/* print blocks onto stdout for debugging. */
101-
int reftable_table_print_blocks(const char *tablename);
102-
103100
/*
104101
* An iterator that iterates through the blocks contained in a given table.
105102
*/

reftable/table.c

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -740,71 +740,6 @@ uint64_t reftable_table_min_update_index(struct reftable_table *t)
740740
return t->min_update_index;
741741
}
742742

743-
int reftable_table_print_blocks(const char *tablename)
744-
{
745-
struct {
746-
const char *name;
747-
int type;
748-
} sections[] = {
749-
{
750-
.name = "ref",
751-
.type = REFTABLE_BLOCK_TYPE_REF,
752-
},
753-
{
754-
.name = "obj",
755-
.type = REFTABLE_BLOCK_TYPE_OBJ,
756-
},
757-
{
758-
.name = "log",
759-
.type = REFTABLE_BLOCK_TYPE_LOG,
760-
},
761-
};
762-
struct reftable_block_source src = { 0 };
763-
struct reftable_table *table = NULL;
764-
struct table_iter ti = { 0 };
765-
size_t i;
766-
int err;
767-
768-
err = reftable_block_source_from_file(&src, tablename);
769-
if (err < 0)
770-
goto done;
771-
772-
err = reftable_table_new(&table, &src, tablename);
773-
if (err < 0)
774-
goto done;
775-
776-
table_iter_init(&ti, table);
777-
778-
printf("header:\n");
779-
printf(" block_size: %d\n", table->block_size);
780-
781-
for (i = 0; i < sizeof(sections) / sizeof(*sections); i++) {
782-
err = table_iter_seek_start(&ti, sections[i].type, 0);
783-
if (err < 0)
784-
goto done;
785-
if (err > 0)
786-
continue;
787-
788-
printf("%s:\n", sections[i].name);
789-
790-
while (1) {
791-
printf(" - length: %u\n", ti.block.restart_off);
792-
printf(" restarts: %u\n", ti.block.restart_count);
793-
794-
err = table_iter_next_block(&ti);
795-
if (err < 0)
796-
goto done;
797-
if (err > 0)
798-
break;
799-
}
800-
}
801-
802-
done:
803-
reftable_table_decref(table);
804-
table_iter_close(&ti);
805-
return err;
806-
}
807-
808743
int reftable_table_iterator_init(struct reftable_table_iterator *it,
809744
struct reftable_table *t)
810745
{

t/helper/test-reftable.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "hash.h"
33
#include "hex.h"
44
#include "reftable/system.h"
5+
#include "reftable/reftable-constants.h"
56
#include "reftable/reftable-error.h"
67
#include "reftable/reftable-merged.h"
78
#include "reftable/reftable-stack.h"
@@ -20,6 +21,72 @@ static void print_help(void)
2021
"\n");
2122
}
2223

24+
static int dump_blocks(const char *tablename)
25+
{
26+
struct reftable_table_iterator ti = { 0 };
27+
struct reftable_block_source src = { 0 };
28+
struct reftable_table *table = NULL;
29+
uint8_t section_type = 0;
30+
int err;
31+
32+
err = reftable_block_source_from_file(&src, tablename);
33+
if (err < 0)
34+
goto done;
35+
36+
err = reftable_table_new(&table, &src, tablename);
37+
if (err < 0)
38+
goto done;
39+
40+
err = reftable_table_iterator_init(&ti, table);
41+
if (err < 0)
42+
goto done;
43+
44+
printf("header:\n");
45+
printf(" block_size: %d\n", table->block_size);
46+
47+
while (1) {
48+
const struct reftable_block *block;
49+
50+
err = reftable_table_iterator_next(&ti, &block);
51+
if (err < 0)
52+
goto done;
53+
if (err > 0)
54+
break;
55+
56+
if (block->block_type != section_type) {
57+
const char *section;
58+
switch (block->block_type) {
59+
case REFTABLE_BLOCK_TYPE_LOG:
60+
section = "log";
61+
break;
62+
case REFTABLE_BLOCK_TYPE_REF:
63+
section = "ref";
64+
break;
65+
case REFTABLE_BLOCK_TYPE_OBJ:
66+
section = "obj";
67+
break;
68+
case REFTABLE_BLOCK_TYPE_INDEX:
69+
section = "idx";
70+
break;
71+
default:
72+
err = -1;
73+
goto done;
74+
}
75+
76+
section_type = block->block_type;
77+
printf("%s:\n", section);
78+
}
79+
80+
printf(" - length: %u\n", block->restart_off);
81+
printf(" restarts: %u\n", block->restart_count);
82+
}
83+
84+
done:
85+
reftable_table_iterator_release(&ti);
86+
reftable_table_decref(table);
87+
return err;
88+
}
89+
2390
static int dump_table(struct reftable_merged_table *mt)
2491
{
2592
struct reftable_iterator it = { NULL };
@@ -184,7 +251,7 @@ int cmd__dump_reftable(int argc, const char **argv)
184251
arg = argv[1];
185252

186253
if (opt_dump_blocks) {
187-
err = reftable_table_print_blocks(arg);
254+
err = dump_blocks(arg);
188255
} else if (opt_dump_table) {
189256
err = dump_reftable(arg);
190257
} else if (opt_dump_stack) {

t/t0613-reftable-write-options.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ test_expect_success 'many refs results in multiple blocks' '
9393
restarts: 3
9494
- length: 3289
9595
restarts: 3
96+
idx:
97+
- length: 103
98+
restarts: 1
9699
EOF
97100
test-tool dump-reftable -b .git/reftable/*.ref >actual &&
98101
test_cmp expect actual
@@ -241,6 +244,9 @@ test_expect_success 'object index gets written by default with ref index' '
241244
restarts: 1
242245
- length: 80
243246
restarts: 1
247+
idx:
248+
- length: 55
249+
restarts: 2
244250
obj:
245251
- length: 11
246252
restarts: 1
@@ -277,6 +283,9 @@ test_expect_success 'object index can be disabled' '
277283
restarts: 1
278284
- length: 80
279285
restarts: 1
286+
idx:
287+
- length: 55
288+
restarts: 2
280289
EOF
281290
test-tool dump-reftable -b .git/reftable/*.ref >actual &&
282291
test_cmp expect actual

0 commit comments

Comments
 (0)