Skip to content

Commit e1bfe30

Browse files
ttaylorrgitster
authored andcommitted
pack-revindex: factor out midx_key_to_pack_pos() helper
The `midx_to_pack_pos()` function implements a binary search over objects in the MIDX between lexical and pseudo-pack order. It does this by taking in an index into the lexical order (i.e. the same argument you'd use for `nth_midxed_object_id()` and similar) and spits out a position in the pseudo-pack order. This works for all callers, since they currently all are translating from lexical order to pseudo-pack order. But future callers may want to translate a known (offset, pack_id) tuple into an index into the psuedo-pack order, without knowing where that (offset, pack_id) tuple appears in lexical order. Prepare for implementing a function that translates between a (offset, pack_id) tuple into an index into the psuedo-pack order by extracting a helper function which does just that, and then reimplementing midx_to_pack_pos() in terms of it. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1e3333 commit e1bfe30

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

pack-revindex.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -520,19 +520,12 @@ static int midx_pack_order_cmp(const void *va, const void *vb)
520520
return 0;
521521
}
522522

523-
int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos)
523+
static int midx_key_to_pack_pos(struct multi_pack_index *m,
524+
struct midx_pack_key *key,
525+
uint32_t *pos)
524526
{
525-
struct midx_pack_key key;
526527
uint32_t *found;
527528

528-
if (!m->revindex_data)
529-
BUG("midx_to_pack_pos: reverse index not yet loaded");
530-
if (m->num_objects <= at)
531-
BUG("midx_to_pack_pos: out-of-bounds object at %"PRIu32, at);
532-
533-
key.pack = nth_midxed_pack_int_id(m, at);
534-
key.offset = nth_midxed_offset(m, at);
535-
key.midx = m;
536529
/*
537530
* The preferred pack sorts first, so determine its identifier by
538531
* looking at the first object in pseudo-pack order.
@@ -542,16 +535,32 @@ int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos)
542535
* implicitly is preferred (and includes all its objects, since ties are
543536
* broken first by pack identifier).
544537
*/
545-
if (midx_preferred_pack(key.midx, &key.preferred_pack) < 0)
538+
if (midx_preferred_pack(key->midx, &key->preferred_pack) < 0)
546539
return error(_("could not determine preferred pack"));
547540

548-
549-
found = bsearch(&key, m->revindex_data, m->num_objects,
550-
sizeof(*m->revindex_data), midx_pack_order_cmp);
541+
found = bsearch(key, m->revindex_data, m->num_objects,
542+
sizeof(*m->revindex_data),
543+
midx_pack_order_cmp);
551544

552545
if (!found)
553-
return error("bad offset for revindex");
546+
return -1;
554547

555548
*pos = found - m->revindex_data;
556549
return 0;
557550
}
551+
552+
int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos)
553+
{
554+
struct midx_pack_key key;
555+
556+
if (!m->revindex_data)
557+
BUG("midx_to_pack_pos: reverse index not yet loaded");
558+
if (m->num_objects <= at)
559+
BUG("midx_to_pack_pos: out-of-bounds object at %"PRIu32, at);
560+
561+
key.pack = nth_midxed_pack_int_id(m, at);
562+
key.offset = nth_midxed_offset(m, at);
563+
key.midx = m;
564+
565+
return midx_key_to_pack_pos(m, &key, pos);
566+
}

0 commit comments

Comments
 (0)