git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 05/11] reftable/dump: support dumping a table's block structure

From
Patrick Steinhardt <ps@pks.im>
Date
May 2, 2024, 06:51 UTC
Message-ID
<4d4407d4a468a81f7775595c1ea4c611c1ca2482.1714630191.git.ps@pks.im>
In-Reply-To
<cover.1714630191.git.ps@pks.im>

We're about to introduce new configs that will allow users to have more control over how exactly reftables are written. To verify that these configs are effective we will need to take a peak into the actual blocks written by the reftable backend.

Introduce a new mode to the dumping logic that prints out the block structure. This logic can be invoked via `test-tool dump-reftables -b`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/dump.c                   |   8 ++-
 reftable/reader.c                 |  63 ++++++++++++++++++
 reftable/reftable-reader.h        |   2 +
 t/t0613-reftable-write-options.sh | 102 ++++++++++++++++++++++++++++++
 4 files changed, 174 insertions(+), 1 deletion(-)
 create mode 100755 t/t0613-reftable-write-options.sh
diff --git a/reftable/dump.c b/reftable/dump.c
index 9c770a10cc..24476cc2a9 100644
--- a/reftable/dump.c
+++ b/reftable/dump.c
@@ -48,6 +48,7 @@ static void print_help(void)
 	printf("usage: dump [-cst] arg\n\n"
 	       "options: \n"
 	       "  -c compact\n"
+	       "  -b dump blocks\n"
 	       "  -t dump table\n"
 	       "  -s dump stack\n"
 	       "  -6 sha256 hash format\n"
@@ -58,6 +59,7 @@ static void print_help(void)
 int reftable_dump_main(int argc, char *const *argv)
 {
 	int err = 0;
+	int opt_dump_blocks = 0;
 	int opt_dump_table = 0;
 	int opt_dump_stack = 0;
 	int opt_compact = 0;
@@ -67,6 +69,8 @@ int reftable_dump_main(int argc, char *const *argv)
 	for (; argc > 1; argv++, argc--)
 		if (*argv[1] != '-')
 			break;
+		else if (!strcmp("-b", argv[1]))
+			opt_dump_blocks = 1;
 		else if (!strcmp("-t", argv[1]))
 			opt_dump_table = 1;
 		else if (!strcmp("-6", argv[1]))
@@ -88,7 +92,9 @@ int reftable_dump_main(int argc, char *const *argv)
 
 	arg = argv[1];
 
-	if (opt_dump_table) {
+	if (opt_dump_blocks) {
+		err = reftable_reader_print_blocks(arg);
+	} else if (opt_dump_table) {
 		err = reftable_reader_print_file(arg);
 	} else if (opt_dump_stack) {
 		err = reftable_stack_print_directory(arg, opt_hash_id);
diff --git a/reftable/reader.c b/reftable/reader.c
index 481dff10d4..f23c8523db 100644
--- a/reftable/reader.c
+++ b/reftable/reader.c
@@ -856,3 +856,66 @@ int reftable_reader_print_file(const char *tablename)
 	reftable_reader_free(r);
 	return err;
 }
+
+int reftable_reader_print_blocks(const char *tablename)
+{
+	struct {
+		const char *name;
+		int type;
+	} sections[] = {
+		{
+			.name = "ref",
+			.type = BLOCK_TYPE_REF,
+		},
+		{
+			.name = "obj",
+			.type = BLOCK_TYPE_OBJ,
+		},
+		{
+			.name = "log",
+			.type = BLOCK_TYPE_LOG,
+		},
+	};
+	struct reftable_block_source src = { 0 };
+	struct table_iter ti = TABLE_ITER_INIT;
+	struct reftable_reader *r = NULL;
+	size_t i;
+	int err;
+
+	err = reftable_block_source_from_file(&src, tablename);
+	if (err < 0)
+		goto done;
+
+	err = reftable_new_reader(&r, &src, tablename);
+	if (err < 0)
+		goto done;
+
+	printf("header:\n");
+	printf("  block_size: %d\n", r->block_size);
+
+	for (i = 0; i < ARRAY_SIZE(sections); i++) {
+		err = reader_start(r, &ti, sections[i].type, 0);
+		if (err < 0)
+			goto done;
+		if (err > 0)
+			continue;
+
+		printf("%s:\n", sections[i].name);
+
+		while (1) {
+			printf("  - length: %u\n", ti.br.block_len);
+			printf("    restarts: %u\n", ti.br.restart_count);
+
+			err = table_iter_next_block(&ti);
+			if (err < 0)
+				goto done;
+			if (err > 0)
+				break;
+		}
+	}
+
+done:
+	reftable_reader_free(r);
+	table_iter_close(&ti);
+	return err;
+}
diff --git a/reftable/reftable-reader.h b/reftable/reftable-reader.h
index 4a4bc2fdf8..4a04857773 100644
--- a/reftable/reftable-reader.h
+++ b/reftable/reftable-reader.h
@@ -97,5 +97,7 @@ void reftable_table_from_reader(struct reftable_table *tab,
 
 /* print table onto stdout for debugging. */
 int reftable_reader_print_file(const char *tablename);
+/* print blocks onto stdout for debugging. */
+int reftable_reader_print_blocks(const char *tablename);
 
 #endif
diff --git a/t/t0613-reftable-write-options.sh b/t/t0613-reftable-write-options.sh
new file mode 100755
index 0000000000..462980c37c
--- /dev/null
+++ b/t/t0613-reftable-write-options.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='reftable write options'
+
+GIT_TEST_DEFAULT_REF_FORMAT=reftable
+export GIT_TEST_DEFAULT_REF_FORMAT
+# Disable auto-compaction for all tests as we explicitly control repacking of
+# refs.
+GIT_TEST_REFTABLE_AUTOCOMPACTION=false
+export GIT_TEST_REFTABLE_AUTOCOMPACTION
+# Block sizes depend on the hash function, so we force SHA1 here.
+GIT_TEST_DEFAULT_HASH=sha1
+export GIT_TEST_DEFAULT_HASH
+# Block sizes also depend on the actual refs we write, so we force "master" to
+# be the default initial branch name.
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'default write options' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit initial &&
+		git pack-refs &&
+		cat >expect <<-EOF &&
+		header:
+		  block_size: 4096
+		ref:
+		  - length: 129
+		    restarts: 2
+		log:
+		  - length: 262
+		    restarts: 2
+		EOF
+		test-tool dump-reftable -b .git/reftable/*.ref >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'disabled reflog writes no log blocks' '
+	test_config_global core.logAllRefUpdates false &&
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit initial &&
+		git pack-refs &&
+		cat >expect <<-EOF &&
+		header:
+		  block_size: 4096
+		ref:
+		  - length: 129
+		    restarts: 2
+		EOF
+		test-tool dump-reftable -b .git/reftable/*.ref >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'many refs results in multiple blocks' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit initial &&
+		for i in $(test_seq 200)
+		do
+			printf "update refs/heads/branch-%d HEAD\n" "$i" ||
+			return 1
+		done >input &&
+		git update-ref --stdin <input &&
+		git pack-refs &&
+
+		cat >expect <<-EOF &&
+		header:
+		  block_size: 4096
+		ref:
+		  - length: 4049
+		    restarts: 11
+		  - length: 1136
+		    restarts: 3
+		log:
+		  - length: 4041
+		    restarts: 4
+		  - length: 4015
+		    restarts: 3
+		  - length: 4014
+		    restarts: 3
+		  - length: 4012
+		    restarts: 3
+		  - length: 3289
+		    restarts: 3
+		EOF
+		test-tool dump-reftable -b .git/reftable/*.ref >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_done
-- 
2.45.0
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 8 of 78 in “reftable: expose write options as config”
  1. 00/11 reftable: expose write options as configPatrick Steinhardt, May 2, 2024
  2. 01/11 reftable: consistently refer to `reftable_write_options` as `opts`Patrick Steinhardt, May 2, 2024
  3. Karthik NayakMay 10, 2024
  4. Patrick SteinhardtMay 10, 2024
  5. 02/11 reftable: consistently pass write opts as valuePatrick Steinhardt, May 2, 2024
  6. 03/11 reftable/writer: drop static variable used to initialize strbufPatrick Steinhardt, May 2, 2024
  7. 04/11 reftable/writer: improve error when passed an invalid block sizePatrick Steinhardt, May 2, 2024
  8. 05/11 reftable/dump: support dumping a table's block structurePatrick Steinhardt, May 2, 2024
  9. 06/11 refs/reftable: allow configuring block sizePatrick Steinhardt, May 2, 2024
  10. Karthik NayakMay 10, 2024
  11. Patrick SteinhardtMay 10, 2024
  12. 07/11 reftable: use `uint16_t` to track restart intervalPatrick Steinhardt, May 2, 2024
  13. 08/11 refs/reftable: allow configuring restart intervalPatrick Steinhardt, May 2, 2024
  14. 09/11 refs/reftable: allow disabling writing the object indexPatrick Steinhardt, May 2, 2024
  15. 10/11 reftable: make the compaction factor configurablePatrick Steinhardt, May 2, 2024
  16. Karthik NayakMay 10, 2024
  17. Patrick SteinhardtMay 10, 2024
  18. 11/11 refs/reftable: allow configuring geometric factorPatrick Steinhardt, May 2, 2024
  19. Karthik NayakMay 10, 2024
  20. Patrick SteinhardtMay 10, 2024
  21. Patrick SteinhardtMay 2, 2024
  22. Junio C HamanoMay 3, 2024
  23. Patrick SteinhardtMay 6, 2024
  24. Justin ToblerMay 6, 2024
  25. Karthik NayakMay 10, 2024
  26. Patrick SteinhardtMay 10, 2024
  27. 00/11 reftable: expose write options as configPatrick Steinhardt, May 10, 2024
  28. 01/11 reftable: consistently refer to `reftable_write_options` as `opts`Patrick Steinhardt, May 10, 2024
  29. Junio C HamanoMay 10, 2024
  30. 02/11 reftable: consistently pass write opts as valuePatrick Steinhardt, May 10, 2024
  31. Junio C HamanoMay 10, 2024
  32. Patrick SteinhardtMay 13, 2024
  33. 03/11 reftable/writer: drop static variable used to initialize strbufPatrick Steinhardt, May 10, 2024
  34. Junio C HamanoMay 10, 2024
  35. 04/11 reftable/writer: improve error when passed an invalid block sizePatrick Steinhardt, May 10, 2024
  36. Junio C HamanoMay 10, 2024
  37. Patrick SteinhardtMay 13, 2024
  38. 05/11 reftable/dump: support dumping a table's block structurePatrick Steinhardt, May 10, 2024
  39. Junio C HamanoMay 13, 2024
  40. 06/11 refs/reftable: allow configuring block sizePatrick Steinhardt, May 10, 2024
  41. 07/11 reftable: use `uint16_t` to track restart intervalPatrick Steinhardt, May 10, 2024
  42. Junio C HamanoMay 13, 2024
  43. Patrick SteinhardtMay 14, 2024
  44. 08/11 refs/reftable: allow configuring restart intervalPatrick Steinhardt, May 10, 2024
  45. Junio C HamanoMay 10, 2024
  46. Patrick SteinhardtMay 13, 2024
  47. 09/11 refs/reftable: allow disabling writing the object indexPatrick Steinhardt, May 10, 2024
  48. 10/11 reftable: make the compaction factor configurablePatrick Steinhardt, May 10, 2024
  49. Junio C HamanoMay 10, 2024
  50. Patrick SteinhardtMay 13, 2024
  51. Junio C HamanoMay 13, 2024
  52. Patrick SteinhardtMay 14, 2024
  53. 11/11 refs/reftable: allow configuring geometric factorPatrick Steinhardt, May 10, 2024
  54. Karthik NayakMay 10, 2024
  55. 00/11 reftable: expose write options as configPatrick Steinhardt, May 13, 2024
  56. 01/11 reftable: consistently refer to `reftable_write_options` as `opts`Patrick Steinhardt, May 13, 2024
  57. 02/11 reftable: pass opts as constant pointerPatrick Steinhardt, May 13, 2024
  58. Karthik NayakMay 17, 2024
  59. Justin ToblerMay 21, 2024
  60. Patrick SteinhardtMay 22, 2024
  61. 03/11 reftable/writer: drop static variable used to initialize strbufPatrick Steinhardt, May 13, 2024
  62. 04/11 reftable/writer: improve error when passed an invalid block sizePatrick Steinhardt, May 13, 2024
  63. 05/11 reftable/dump: support dumping a table's block structurePatrick Steinhardt, May 13, 2024
  64. Justin ToblerMay 21, 2024
  65. Patrick SteinhardtMay 22, 2024
  66. 06/11 refs/reftable: allow configuring block sizePatrick Steinhardt, May 13, 2024
  67. Karthik NayakMay 17, 2024
  68. 07/11 reftable: use `uint16_t` to track restart intervalPatrick Steinhardt, May 13, 2024
  69. 08/11 refs/reftable: allow configuring restart intervalPatrick Steinhardt, May 13, 2024
  70. Justin ToblerMay 21, 2024
  71. Patrick SteinhardtMay 22, 2024
  72. 09/11 refs/reftable: allow disabling writing the object indexPatrick Steinhardt, May 13, 2024
  73. 10/11 reftable: make the compaction factor configurablePatrick Steinhardt, May 13, 2024
  74. 11/11 refs/reftable: allow configuring geometric factorPatrick Steinhardt, May 13, 2024
  75. Karthik NayakMay 17, 2024
  76. Patrick SteinhardtMay 17, 2024
  77. Justin ToblerMay 21, 2024
  78. Patrick SteinhardtMay 22, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.