Skip to content

Commit 76ffbca

Browse files
garimasi514gitster
authored andcommitted
commit-graph: write Bloom filters to commit graph file
Update the technical documentation for commit-graph-format with the formats for the Bloom filter index (BIDX) and Bloom filter data (BDAT) chunks. Write the computed Bloom filters information to the commit graph file using this format. Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Garima Singh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d11275 commit 76ffbca

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

Documentation/technical/commit-graph-format.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ metadata, including:
1717
- The parents of the commit, stored using positional references within
1818
the graph file.
1919

20+
- The Bloom filter of the commit carrying the paths that were changed between
21+
the commit and its first parent, if requested.
22+
2023
These positional references are stored as unsigned 32-bit integers
2124
corresponding to the array position within the list of commit OIDs. Due
2225
to some special constants we use to track parents, we can store at most
@@ -93,6 +96,33 @@ CHUNK DATA:
9396
positions for the parents until reaching a value with the most-significant
9497
bit on. The other bits correspond to the position of the last parent.
9598

99+
Bloom Filter Index (ID: {'B', 'I', 'D', 'X'}) (N * 4 bytes) [Optional]
100+
* The ith entry, BIDX[i], stores the number of 8-byte word blocks in all
101+
Bloom filters from commit 0 to commit i (inclusive) in lexicographic
102+
order. The Bloom filter for the i-th commit spans from BIDX[i-1] to
103+
BIDX[i] (plus header length), where BIDX[-1] is 0.
104+
* The BIDX chunk is ignored if the BDAT chunk is not present.
105+
106+
Bloom Filter Data (ID: {'B', 'D', 'A', 'T'}) [Optional]
107+
* It starts with header consisting of three unsigned 32-bit integers:
108+
- Version of the hash algorithm being used. We currently only support
109+
value 1 which corresponds to the 32-bit version of the murmur3 hash
110+
implemented exactly as described in
111+
https://en.wikipedia.org/wiki/MurmurHash#Algorithm and the double
112+
hashing technique using seed values 0x293ae76f and 0x7e646e2 as
113+
described in https://doi.org/10.1007/978-3-540-30494-4_26 "Bloom Filters
114+
in Probabilistic Verification"
115+
- The number of times a path is hashed and hence the number of bit positions
116+
that cumulatively determine whether a file is present in the commit.
117+
- The minimum number of bits 'b' per entry in the Bloom filter. If the filter
118+
contains 'n' entries, then the filter size is the minimum number of 64-bit
119+
words that contain n*b bits.
120+
* The rest of the chunk is the concatenation of all the computed Bloom
121+
filters for the commits in lexicographic order.
122+
* Note: Commits with no changes or more than 512 changes have Bloom filters
123+
of length zero.
124+
* The BDAT chunk is present if and only if BIDX is present.
125+
96126
Base Graphs List (ID: {'B', 'A', 'S', 'E'}) [Optional]
97127
This list of H-byte hashes describe a set of B commit-graph files that
98128
form a commit-graph chain. The graph position for the ith commit in this

commit-graph.c

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
2525
#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
2626
#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
27+
#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
28+
#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
2729
#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
28-
#define MAX_NUM_CHUNKS 5
30+
#define MAX_NUM_CHUNKS 7
2931

3032
#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
3133

@@ -319,6 +321,32 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
319321
chunk_repeated = 1;
320322
else
321323
graph->chunk_base_graphs = data + chunk_offset;
324+
break;
325+
326+
case GRAPH_CHUNKID_BLOOMINDEXES:
327+
if (graph->chunk_bloom_indexes)
328+
chunk_repeated = 1;
329+
else
330+
graph->chunk_bloom_indexes = data + chunk_offset;
331+
break;
332+
333+
case GRAPH_CHUNKID_BLOOMDATA:
334+
if (graph->chunk_bloom_data)
335+
chunk_repeated = 1;
336+
else {
337+
uint32_t hash_version;
338+
graph->chunk_bloom_data = data + chunk_offset;
339+
hash_version = get_be32(data + chunk_offset);
340+
341+
if (hash_version != 1)
342+
break;
343+
344+
graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
345+
graph->bloom_filter_settings->hash_version = hash_version;
346+
graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
347+
graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
348+
}
349+
break;
322350
}
323351

324352
if (chunk_repeated) {
@@ -337,6 +365,15 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
337365
last_chunk_offset = chunk_offset;
338366
}
339367

368+
if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
369+
init_bloom_filters();
370+
} else {
371+
/* We need both the bloom chunks to exist together. Else ignore the data */
372+
graph->chunk_bloom_indexes = NULL;
373+
graph->chunk_bloom_data = NULL;
374+
graph->bloom_filter_settings = NULL;
375+
}
376+
340377
hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
341378

342379
if (verify_commit_graph_lite(graph)) {
@@ -1034,6 +1071,59 @@ static void write_graph_chunk_extra_edges(struct hashfile *f,
10341071
}
10351072
}
10361073

1074+
static void write_graph_chunk_bloom_indexes(struct hashfile *f,
1075+
struct write_commit_graph_context *ctx)
1076+
{
1077+
struct commit **list = ctx->commits.list;
1078+
struct commit **last = ctx->commits.list + ctx->commits.nr;
1079+
uint32_t cur_pos = 0;
1080+
struct progress *progress = NULL;
1081+
int i = 0;
1082+
1083+
if (ctx->report_progress)
1084+
progress = start_delayed_progress(
1085+
_("Writing changed paths Bloom filters index"),
1086+
ctx->commits.nr);
1087+
1088+
while (list < last) {
1089+
struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1090+
cur_pos += filter->len;
1091+
display_progress(progress, ++i);
1092+
hashwrite_be32(f, cur_pos);
1093+
list++;
1094+
}
1095+
1096+
stop_progress(&progress);
1097+
}
1098+
1099+
static void write_graph_chunk_bloom_data(struct hashfile *f,
1100+
struct write_commit_graph_context *ctx,
1101+
const struct bloom_filter_settings *settings)
1102+
{
1103+
struct commit **list = ctx->commits.list;
1104+
struct commit **last = ctx->commits.list + ctx->commits.nr;
1105+
struct progress *progress = NULL;
1106+
int i = 0;
1107+
1108+
if (ctx->report_progress)
1109+
progress = start_delayed_progress(
1110+
_("Writing changed paths Bloom filters data"),
1111+
ctx->commits.nr);
1112+
1113+
hashwrite_be32(f, settings->hash_version);
1114+
hashwrite_be32(f, settings->num_hashes);
1115+
hashwrite_be32(f, settings->bits_per_entry);
1116+
1117+
while (list < last) {
1118+
struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1119+
display_progress(progress, ++i);
1120+
hashwrite(f, filter->data, filter->len * sizeof(unsigned char));
1121+
list++;
1122+
}
1123+
1124+
stop_progress(&progress);
1125+
}
1126+
10371127
static int oid_compare(const void *_a, const void *_b)
10381128
{
10391129
const struct object_id *a = (const struct object_id *)_a;
@@ -1438,6 +1528,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
14381528
struct strbuf progress_title = STRBUF_INIT;
14391529
int num_chunks = 3;
14401530
struct object_id file_hash;
1531+
const struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
14411532

14421533
if (ctx->split) {
14431534
struct strbuf tmp_file = STRBUF_INIT;
@@ -1482,6 +1573,12 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
14821573
chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
14831574
num_chunks++;
14841575
}
1576+
if (ctx->changed_paths) {
1577+
chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMINDEXES;
1578+
num_chunks++;
1579+
chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMDATA;
1580+
num_chunks++;
1581+
}
14851582
if (ctx->num_commit_graphs_after > 1) {
14861583
chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
14871584
num_chunks++;
@@ -1500,6 +1597,15 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
15001597
4 * ctx->num_extra_edges;
15011598
num_chunks++;
15021599
}
1600+
if (ctx->changed_paths) {
1601+
chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1602+
sizeof(uint32_t) * ctx->commits.nr;
1603+
num_chunks++;
1604+
1605+
chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1606+
sizeof(uint32_t) * 3 + ctx->total_bloom_filter_data_size;
1607+
num_chunks++;
1608+
}
15031609
if (ctx->num_commit_graphs_after > 1) {
15041610
chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
15051611
hashsz * (ctx->num_commit_graphs_after - 1);
@@ -1537,6 +1643,10 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
15371643
write_graph_chunk_data(f, hashsz, ctx);
15381644
if (ctx->num_extra_edges)
15391645
write_graph_chunk_extra_edges(f, ctx);
1646+
if (ctx->changed_paths) {
1647+
write_graph_chunk_bloom_indexes(f, ctx);
1648+
write_graph_chunk_bloom_data(f, ctx, &bloom_settings);
1649+
}
15401650
if (ctx->num_commit_graphs_after > 1 &&
15411651
write_graph_chunk_base(f, ctx)) {
15421652
return -1;
@@ -2184,6 +2294,7 @@ void free_commit_graph(struct commit_graph *g)
21842294
close(g->graph_fd);
21852295
}
21862296
free(g->filename);
2297+
free(g->bloom_filter_settings);
21872298
free(g);
21882299
}
21892300

commit-graph.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD "GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD"
1212

1313
struct commit;
14+
struct bloom_filter_settings;
1415

1516
char *get_commit_graph_filename(struct object_directory *odb);
1617
int open_commit_graph(const char *graph_file, int *fd, struct stat *st);
@@ -59,6 +60,10 @@ struct commit_graph {
5960
const unsigned char *chunk_commit_data;
6061
const unsigned char *chunk_extra_edges;
6162
const unsigned char *chunk_base_graphs;
63+
const unsigned char *chunk_bloom_indexes;
64+
const unsigned char *chunk_bloom_data;
65+
66+
struct bloom_filter_settings *bloom_filter_settings;
6267
};
6368

6469
struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,

0 commit comments

Comments
 (0)