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

[PATCH 02/15] chunk-format: add API for writing table of contents

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 3, 2020, 16:16 UTC
Message-ID
<da7933cc59928c7995821dcc72ceae0c690ebaca.1607012215.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.804.git.1607012215.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

The commit-graph and multi-pack-index formats share a concept of "chunks" that are described by a table of contents near the beginning of the file.

The table of contents consists of rows of 12 bytes. Each row starts with a 4-byte ID that signals the type of data stored in the chunk. The row then continues with an 8-byte offset describing the position in the file where that data starts. The table of contents lists the chunks in position order so the length of a chunk can be determined by subtracting its start position from the start position of the next chunk.

The table of contents always ends with ID 0x0000 to assist finding the end of the last "real" chunk. Typically, this points to the trailing hash of a file.

Convert the chunk-writing loop in commit-graph.c to use the new write_table_of_contents() method in chunk-format.c.

The most subtle part of this conversion is the use of 'cur_offset' to allow the caller to specify how many bytes were written in the file's header before the table of contents. This may differ between formats.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Makefile       |  1 +
 chunk-format.c | 26 ++++++++++++++++++++++++++
 chunk-format.h | 36 ++++++++++++++++++++++++++++++++++++
 commit-graph.c | 23 ++---------------------
 4 files changed, 65 insertions(+), 21 deletions(-)
 create mode 100644 chunk-format.c
 create mode 100644 chunk-format.h
diff --git a/Makefile b/Makefile
index d3a531d3c6..cdbcadac14 100644
--- a/Makefile
+++ b/Makefile
@@ -854,6 +854,7 @@ LIB_OBJS += bundle.o
 LIB_OBJS += cache-tree.o
 LIB_OBJS += chdir-notify.o
 LIB_OBJS += checkout.o
+LIB_OBJS += chunk-format.o
 LIB_OBJS += color.o
 LIB_OBJS += column.o
 LIB_OBJS += combine-diff.o
diff --git a/chunk-format.c b/chunk-format.c
new file mode 100644
index 0000000000..771b6d98d0
--- /dev/null
+++ b/chunk-format.c
@@ -0,0 +1,26 @@
+#include "git-compat-util.h"
+#include "chunk-format.h"
+#include "csum-file.h"
+#define CHUNK_LOOKUP_WIDTH 12
+
+void write_table_of_contents(struct hashfile *f,
+			     uint64_t cur_offset,
+			     struct chunk_info *chunks,
+			     int nr)
+{
+	int i;
+
+	/* Add the table of contents to the current offset */
+	cur_offset += (nr + 1) * CHUNK_LOOKUP_WIDTH;
+
+	for (i = 0; i < nr; i++) {
+		hashwrite_be32(f, chunks[i].id);
+		hashwrite_be64(f, cur_offset);
+
+		cur_offset += chunks[i].size;
+	}
+
+	/* Trailing entry marks the end of the chunks */
+	hashwrite_be32(f, 0);
+	hashwrite_be64(f, cur_offset);
+}
diff --git a/chunk-format.h b/chunk-format.h
new file mode 100644
index 0000000000..4b9cbeb372
--- /dev/null
+++ b/chunk-format.h
@@ -0,0 +1,36 @@
+#ifndef CHUNK_FORMAT_H
+#define CHUNK_FORMAT_H
+
+#include "git-compat-util.h"
+
+struct hashfile;
+
+typedef int (*chunk_write_fn)(struct hashfile *f,
+			      void *data);
+
+/*
+ * When writing a chunk-based file format, collect the chunks in
+ * an array of chunk_info structs. The size stores the _expected_
+ * amount of data that will be written by write_fn.
+ */
+struct chunk_info {
+	uint32_t id;
+	uint64_t size;
+	chunk_write_fn write_fn;
+};
+
+/*
+ * Write the chunk data into the supplied hashfile.
+ *
+ * * 'cur_offset' indicates the number of bytes written to the hashfile
+ *   before the table of contents starts.
+ *
+ * * 'nr' is the number of chunks with non-zero IDs, so 'nr + 1'
+ *   chunks are written in total.
+ */
+void write_table_of_contents(struct hashfile *f,
+			     uint64_t cur_offset,
+			     struct chunk_info *chunks,
+			     int nr);
+
+#endif
diff --git a/commit-graph.c b/commit-graph.c
index 6b5bb8b6b8..5494fda1d3 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -19,6 +19,7 @@
 #include "shallow.h"
 #include "json-writer.h"
 #include "trace2.h"
+#include "chunk-format.h"
 
 void git_test_write_commit_graph_or_die(void)
 {
@@ -1696,15 +1697,6 @@ static int write_graph_chunk_base(struct hashfile *f,
 	return 0;
 }
 
-typedef int (*chunk_write_fn)(struct hashfile *f,
-			      void *data);
-
-struct chunk_info {
-	uint32_t id;
-	uint64_t size;
-	chunk_write_fn write_fn;
-};
-
 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 {
 	uint32_t i;
@@ -1715,7 +1707,6 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	const unsigned hashsz = the_hash_algo->rawsz;
 	struct strbuf progress_title = STRBUF_INIT;
 	int num_chunks = 3;
-	uint64_t chunk_offset;
 	struct object_id file_hash;
 
 	if (ctx->split) {
@@ -1805,17 +1796,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	hashwrite_u8(f, num_chunks);
 	hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
 
-	chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
-	for (i = 0; i <= num_chunks; i++) {
-		uint32_t chunk_write[3];
-
-		chunk_write[0] = htonl(chunks[i].id);
-		chunk_write[1] = htonl(chunk_offset >> 32);
-		chunk_write[2] = htonl(chunk_offset & 0xffffffff);
-		hashwrite(f, chunk_write, 12);
-
-		chunk_offset += chunks[i].size;
-	}
+	write_table_of_contents(f, /* cur_offset */ 8, chunks, num_chunks);
 
 	if (ctx->report_progress) {
 		strbuf_addf(&progress_title,
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Taylor Blau
Message 2 of 40 in “Refactor chunk-format into an API”
  1. 00/15 Refactor chunk-format into an APIDerrick Stolee via GitGitGadget, Dec 3, 2020
  2. 02/15 chunk-format: add API for writing table of contentsDerrick Stolee via GitGitGadget, Dec 3, 2020
  3. Taylor BlauDec 8, 2020
  4. 01/15 commit-graph: anonymize data in chunk_write_fnDerrick Stolee via GitGitGadget, Dec 3, 2020
  5. 04/15 midx: use context in write_midx_pack_names()Derrick Stolee via GitGitGadget, Dec 3, 2020
  6. 03/15 midx: rename pack_info to write_midx_contextDerrick Stolee via GitGitGadget, Dec 3, 2020
  7. 05/15 midx: add entries to write_midx_contextDerrick Stolee via GitGitGadget, Dec 3, 2020
  8. Junio C HamanoDec 3, 2020
  9. Derrick StoleeDec 4, 2020
  10. Taylor BlauDec 8, 2020
  11. 07/15 midx: add num_large_offsets to write_midx_contextDerrick Stolee via GitGitGadget, Dec 3, 2020
  12. 06/15 midx: add pack_perm to write_midx_contextDerrick Stolee via GitGitGadget, Dec 3, 2020
  13. 09/15 midx: drop chunk progress during writeDerrick Stolee via GitGitGadget, Dec 3, 2020
  14. 08/15 midx: convert chunk write methods to return intDerrick Stolee via GitGitGadget, Dec 3, 2020
  15. Junio C HamanoDec 3, 2020
  16. Derrick StoleeDec 4, 2020
  17. 11/15 midx: use 64-bit multiplication for chunk sizesDerrick Stolee via GitGitGadget, Dec 3, 2020
  18. Junio C HamanoDec 3, 2020
  19. Taylor BlauDec 8, 2020
  20. 12/15 chunk-format: create write_chunks()Derrick Stolee via GitGitGadget, Dec 3, 2020
  21. Taylor BlauDec 8, 2020
  22. 13/15 chunk-format: create chunk reading APIDerrick Stolee via GitGitGadget, Dec 3, 2020
  23. Junio C HamanoDec 3, 2020
  24. Derrick StoleeDec 4, 2020
  25. Junio C HamanoDec 4, 2020
  26. Junio C HamanoDec 3, 2020
  27. Derrick StoleeDec 4, 2020
  28. 15/15 chunk-format: add technical docsDerrick Stolee via GitGitGadget, Dec 3, 2020
  29. 14/15 commit-graph: restore duplicate chunk checksDerrick Stolee via GitGitGadget, Dec 3, 2020
  30. Derrick StoleeDec 7, 2020
  31. 10/15 midx: use chunk-format API in write_midx_internal()Derrick Stolee via GitGitGadget, Dec 3, 2020
  32. Taylor BlauDec 8, 2020
  33. Derrick StoleeDec 10, 2020
  34. René ScharfeDec 4, 2020
  35. Derrick StoleeDec 4, 2020
  36. Junio C HamanoDec 4, 2020
  37. Taylor BlauDec 8, 2020
  38. René ScharfeDec 9, 2020
  39. Taylor BlauDec 10, 2020
  40. Derrick StoleeDec 10, 2020

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.