midx.c: avoid adding preferred objects twice

The last commit changes the behavior of midx.c's `get_sorted_objects()` function to handle the case of writing a MIDX bitmap while reusing an existing MIDX and changing the identity of the preferred pack separately. As part of this change, all objects from the (new) preferred pack are added to the fanout table in a separate pass. Since these copies of the objects all have their preferred bits set, any duplicates will be resolved in their favor. Importantly, this includes any copies of those same objects that come from the existing MIDX. We know at the time of adding them that they'll be redundant if their source pack is the (new) preferred one, so we can avoid adding them to the list in this case. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Aug 22, 2022 at 15:50 UTC 99e4d084ffc4c6f8cb28ec61fdbb44facdd47ac7
1 file changed +13 -2
midx.c
+13 -2
@@ -595,7 +595,8 @@ static void midx_fanout_sort(struct midx_fanout *fanout)
595
596 static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
597 struct multi_pack_index *m,
598 - uint32_t cur_fanout)
598 + uint32_t cur_fanout,
599 + int preferred_pack)
600 {
601 uint32_t start = 0, end;
602 uint32_t cur_object;
@@ -605,6 +606,15 @@ static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
606 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
607
608 for (cur_object = start; cur_object < end; cur_object++) {
609 + if ((preferred_pack > -1) &&
610 + (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
611 + /*
612 + * Objects from preferred packs are added
613 + * separately.
614 + */
615 + continue;
616 + }
617 +
618 midx_fanout_grow(fanout, fanout->nr + 1);
619 nth_midxed_pack_midx_entry(m,
620 &fanout->entries[fanout->nr],
@@ -680,7 +690,8 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
690 fanout.nr = 0;
691
692 if (m)
683 - midx_fanout_add_midx_fanout(&fanout, m, cur_fanout);
693 + midx_fanout_add_midx_fanout(&fanout, m, cur_fanout,
694 + preferred_pack);
695
696 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
697 int preferred = cur_pack == preferred_pack;