Git fork

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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
e0011188 0f8ee94b

+77 -69
-3
reftable/reftable-table.h
··· 97 97 /* return the min_update_index for a table */ 98 98 uint64_t reftable_table_min_update_index(struct reftable_table *t); 99 99 100 - /* print blocks onto stdout for debugging. */ 101 - int reftable_table_print_blocks(const char *tablename); 102 - 103 100 /* 104 101 * An iterator that iterates through the blocks contained in a given table. 105 102 */
-65
reftable/table.c
··· 740 740 return t->min_update_index; 741 741 } 742 742 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 - 808 743 int reftable_table_iterator_init(struct reftable_table_iterator *it, 809 744 struct reftable_table *t) 810 745 {
+68 -1
t/helper/test-reftable.c
··· 2 2 #include "hash.h" 3 3 #include "hex.h" 4 4 #include "reftable/system.h" 5 + #include "reftable/reftable-constants.h" 5 6 #include "reftable/reftable-error.h" 6 7 #include "reftable/reftable-merged.h" 7 8 #include "reftable/reftable-stack.h" ··· 18 19 " -6 sha256 hash format\n" 19 20 " -h this help\n" 20 21 "\n"); 22 + } 23 + 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; 21 88 } 22 89 23 90 static int dump_table(struct reftable_merged_table *mt) ··· 184 251 arg = argv[1]; 185 252 186 253 if (opt_dump_blocks) { 187 - err = reftable_table_print_blocks(arg); 254 + err = dump_blocks(arg); 188 255 } else if (opt_dump_table) { 189 256 err = dump_reftable(arg); 190 257 } else if (opt_dump_stack) {
+9
t/t0613-reftable-write-options.sh
··· 93 93 restarts: 3 94 94 - length: 3289 95 95 restarts: 3 96 + idx: 97 + - length: 103 98 + restarts: 1 96 99 EOF 97 100 test-tool dump-reftable -b .git/reftable/*.ref >actual && 98 101 test_cmp expect actual ··· 241 244 restarts: 1 242 245 - length: 80 243 246 restarts: 1 247 + idx: 248 + - length: 55 249 + restarts: 2 244 250 obj: 245 251 - length: 11 246 252 restarts: 1 ··· 277 283 restarts: 1 278 284 - length: 80 279 285 restarts: 1 286 + idx: 287 + - length: 55 288 + restarts: 2 280 289 EOF 281 290 test-tool dump-reftable -b .git/reftable/*.ref >actual && 282 291 test_cmp expect actual