diff mbox series

[v2,12/19] midx: teach `midx_contains_pack()` about incremental MIDXs

Message ID fb60f2b022d1dc8c0e6df53253dc924f8d00a382.1721250704.git.me@ttaylorr.com (mailing list archive)
State Superseded
Headers show
Series midx: incremental multi-pack indexes, part one | expand

Commit Message

Taylor Blau July 17, 2024, 9:12 p.m. UTC
Now that the `midx_contains_pack()` versus `midx_locate_pack()` debacle
has been cleaned up, teach the former about how to operate in an
incremental MIDX-aware world in a similar fashion as in previous
commits.

Instead of using either of the two `midx_for_object()` or
`midx_for_pack()` helpers, this function is split into two: one that
determines whether a pack is contained in a single MIDX, and another
which calls the former in a loop over all MIDXs.

This approach does not require that we change any of the implementation
in what is now `midx_contains_pack_1()` as it still operates over a
single MIDX.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 midx.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Jeff King Aug. 1, 2024, 10:17 a.m. UTC | #1
On Wed, Jul 17, 2024 at 05:12:32PM -0400, Taylor Blau wrote:

> Now that the `midx_contains_pack()` versus `midx_locate_pack()` debacle
> has been cleaned up, teach the former about how to operate in an
> incremental MIDX-aware world in a similar fashion as in previous
> commits.
> 
> Instead of using either of the two `midx_for_object()` or
> `midx_for_pack()` helpers, this function is split into two: one that
> determines whether a pack is contained in a single MIDX, and another
> which calls the former in a loop over all MIDXs.
> 
> This approach does not require that we change any of the implementation
> in what is now `midx_contains_pack_1()` as it still operates over a
> single MIDX.

Makes sense. There is no ordering or relationship for which packs might
be in which midx, so we have to just walk them linearly and check each
part of the chain.

-Peff
diff mbox series

Patch

diff --git a/midx.c b/midx.c
index 59097808a9..0fa8febb9d 100644
--- a/midx.c
+++ b/midx.c
@@ -465,7 +465,8 @@  int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 	return strcmp(idx_or_pack_name, idx_name);
 }
 
-int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
+static int midx_contains_pack_1(struct multi_pack_index *m,
+				const char *idx_or_pack_name)
 {
 	uint32_t first = 0, last = m->num_packs;
 
@@ -488,6 +489,14 @@  int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
 	return 0;
 }
 
+int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
+{
+	for (; m; m = m->base_midx)
+		if (midx_contains_pack_1(m, idx_or_pack_name))
+			return 1;
+	return 0;
+}
+
 int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
 {
 	if (m->preferred_pack_idx == -1) {