]> git.ipfire.org Git - thirdparty/git.git/commitdiff
bloom: prepare to discard incompatible Bloom filters
authorTaylor Blau <me@ttaylorr.com>
Tue, 25 Jun 2024 17:39:57 +0000 (13:39 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 Jun 2024 20:52:06 +0000 (13:52 -0700)
Callers use the inline `get_bloom_filter()` implementation as a thin
wrapper around `get_or_compute_bloom_filter()`. The former calls the
latter with a value of "0" for `compute_if_not_present`, making
`get_bloom_filter()` the default read-only path for fetching an existing
Bloom filter.

Callers expect the value returned from `get_bloom_filter()` is usable,
that is that it's compatible with the configured value corresponding to
`commitGraph.changedPathsVersion`.

This is OK, since the commit-graph machinery only initializes its BDAT
chunk (thereby enabling it to service Bloom filter queries) when the
Bloom filter hash_version is compatible with our settings. So any value
returned by `get_bloom_filter()` is trivially useable.

However, subsequent commits will load the BDAT chunk even when the Bloom
filters are built with incompatible hash versions. Prepare to handle
this by teaching `get_bloom_filter()` to discard filters that are
incompatible with the configured hash version.

Callers who wish to read incompatible filters (e.g., for upgrading
filters from v1 to v2) may use the lower level routine,
`get_or_compute_bloom_filter()`.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
bloom.c
bloom.h

diff --git a/bloom.c b/bloom.c
index e64e53bc4caf48cb56505935214ca87885e1e47a..c24489dbcf19537618ae499ad45cd116b78e077e 100644 (file)
--- a/bloom.c
+++ b/bloom.c
@@ -220,6 +220,23 @@ static void init_truncated_large_filter(struct bloom_filter *filter,
        filter->version = version;
 }
 
+struct bloom_filter *get_bloom_filter(struct repository *r, struct commit *c)
+{
+       struct bloom_filter *filter;
+       int hash_version;
+
+       filter = get_or_compute_bloom_filter(r, c, 0, NULL, NULL);
+       if (!filter)
+               return NULL;
+
+       prepare_repo_settings(r);
+       hash_version = r->settings.commit_graph_changed_paths_version;
+
+       if (!(hash_version == -1 || hash_version == filter->version))
+               return NULL; /* unusable filter */
+       return filter;
+}
+
 struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
                                                 struct commit *c,
                                                 int compute_if_not_present,
@@ -245,7 +262,8 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
                                                     filter, graph_pos);
        }
 
-       if (filter->data && filter->len)
+       if ((filter->data && filter->len) &&
+           (!settings || settings->hash_version == filter->version))
                return filter;
        if (!compute_if_not_present)
                return NULL;
diff --git a/bloom.h b/bloom.h
index c9dd7d402291c849bdb84b505498bb4cc43b3cc5..052a993aab1f562871e4592fa52bd41586172904 100644 (file)
--- a/bloom.h
+++ b/bloom.h
@@ -108,8 +108,24 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
                                                 const struct bloom_filter_settings *settings,
                                                 enum bloom_filter_computed *computed);
 
-#define get_bloom_filter(r, c) get_or_compute_bloom_filter( \
-       (r), (c), 0, NULL, NULL)
+/*
+ * Find the Bloom filter associated with the given commit "c".
+ *
+ * If any of the following are true
+ *
+ *   - the repository does not have a commit-graph, or
+ *   - the repository disables reading from the commit-graph, or
+ *   - the given commit does not have a Bloom filter computed, or
+ *   - there is a Bloom filter for commit "c", but it cannot be read
+ *     because the filter uses an incompatible version of murmur3
+ *
+ * , then `get_bloom_filter()` will return NULL. Otherwise, the corresponding
+ * Bloom filter will be returned.
+ *
+ * For callers who wish to inspect Bloom filters with incompatible hash
+ * versions, use get_or_compute_bloom_filter().
+ */
+struct bloom_filter *get_bloom_filter(struct repository *r, struct commit *c);
 
 int bloom_filter_contains(const struct bloom_filter *filter,
                          const struct bloom_key *key,