Skip to content

Commit 3d475f4

Browse files
derrickstoleegitster
authored andcommitted
packfile: define and use bsearch_pack()
The method bsearch_hash() generalizes binary searches using a fanout table. The only consumer is currently find_pack_entry_one(). It requires a bit of pointer arithmetic to align the fanout table and the lookup table depending on the pack-index version. Extract the pack-index pointer arithmetic to a new method, bsearch_pack(), so this can be re-used in other code paths. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 626fd98 commit 3d475f4

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

packfile.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,29 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
16541654
return data;
16551655
}
16561656

1657+
int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
1658+
{
1659+
const unsigned char *index_fanout = p->index_data;
1660+
const unsigned char *index_lookup;
1661+
int index_lookup_width;
1662+
1663+
if (!index_fanout)
1664+
BUG("bsearch_pack called without a valid pack-index");
1665+
1666+
index_lookup = index_fanout + 4 * 256;
1667+
if (p->index_version == 1) {
1668+
index_lookup_width = 24;
1669+
index_lookup += 4;
1670+
} else {
1671+
index_lookup_width = 20;
1672+
index_fanout += 8;
1673+
index_lookup += 8;
1674+
}
1675+
1676+
return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
1677+
index_lookup, index_lookup_width, result);
1678+
}
1679+
16571680
const unsigned char *nth_packed_object_sha1(struct packed_git *p,
16581681
uint32_t n)
16591682
{
@@ -1720,30 +1743,17 @@ off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
17201743
off_t find_pack_entry_one(const unsigned char *sha1,
17211744
struct packed_git *p)
17221745
{
1723-
const uint32_t *level1_ofs = p->index_data;
17241746
const unsigned char *index = p->index_data;
1725-
unsigned stride;
1747+
struct object_id oid;
17261748
uint32_t result;
17271749

17281750
if (!index) {
17291751
if (open_pack_index(p))
17301752
return 0;
1731-
level1_ofs = p->index_data;
1732-
index = p->index_data;
1733-
}
1734-
if (p->index_version > 1) {
1735-
level1_ofs += 2;
1736-
index += 8;
1737-
}
1738-
index += 4 * 256;
1739-
if (p->index_version > 1) {
1740-
stride = 20;
1741-
} else {
1742-
stride = 24;
1743-
index += 4;
17441753
}
17451754

1746-
if (bsearch_hash(sha1, level1_ofs, index, stride, &result))
1755+
hashcpy(oid.hash, sha1);
1756+
if (bsearch_pack(&oid, p, &result))
17471757
return nth_packed_object_offset(p, result);
17481758
return 0;
17491759
}

packfile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ extern struct packed_git *add_packed_git(const char *path, size_t path_len, int
7878
*/
7979
extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
8080

81+
/*
82+
* Perform binary search on a pack-index for a given oid. Packfile is expected to
83+
* have a valid pack-index.
84+
*
85+
* See 'bsearch_hash' for more information.
86+
*/
87+
int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result);
88+
8189
/*
8290
* Return the SHA-1 of the nth object within the specified packfile.
8391
* Open the index if it is not already open. The return value points

0 commit comments

Comments
 (0)