Commit abf5681c by Ramsay Jones

Change the interface of the pack index search function

In particular, on a successful search, we now return the index
entry number of the object rather than the '.pack' file offset.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
parent 238e54bc
...@@ -37,7 +37,7 @@ struct git_pack { ...@@ -37,7 +37,7 @@ struct git_pack {
/** Functions to access idx_map. */ /** Functions to access idx_map. */
int (*idx_search)( int (*idx_search)(
off_t *, uint32_t *,
struct git_pack *, struct git_pack *,
const git_oid *); const git_oid *);
...@@ -663,20 +663,20 @@ static int pack_openidx_map(git_pack *p) ...@@ -663,20 +663,20 @@ static int pack_openidx_map(git_pack *p)
return GIT_SUCCESS; return GIT_SUCCESS;
} }
static int idxv1_search(off_t *out, git_pack *p, const git_oid *id) static int idxv1_search(uint32_t *out, git_pack *p, const git_oid *id)
{ {
unsigned char *data = p->im_oid; unsigned char *data = p->im_oid;
size_t lo = id->id[0] ? p->im_fanout[id->id[0] - 1] : 0; uint32_t lo = id->id[0] ? p->im_fanout[id->id[0] - 1] : 0;
size_t hi = p->im_fanout[id->id[0]]; uint32_t hi = p->im_fanout[id->id[0]];
do { do {
size_t mid = (lo + hi) >> 1; uint32_t mid = (lo + hi) >> 1;
size_t pos = 24 * mid; uint32_t pos = 24 * mid;
int cmp = memcmp(id->id, data + pos + 4, 20); int cmp = memcmp(id->id, data + pos + 4, 20);
if (cmp < 0) if (cmp < 0)
hi = mid; hi = mid;
else if (!cmp) { else if (!cmp) {
*out = decode32(data + pos); *out = mid;
return GIT_SUCCESS; return GIT_SUCCESS;
} else } else
lo = mid + 1; lo = mid + 1;
...@@ -716,24 +716,20 @@ static int pack_openidx_v1(git_pack *p) ...@@ -716,24 +716,20 @@ static int pack_openidx_v1(git_pack *p)
return GIT_SUCCESS; return GIT_SUCCESS;
} }
static int idxv2_search(off_t *out, git_pack *p, const git_oid *id) static int idxv2_search(uint32_t *out, git_pack *p, const git_oid *id)
{ {
unsigned char *data = p->im_oid; unsigned char *data = p->im_oid;
size_t lo = id->id[0] ? p->im_fanout[id->id[0] - 1] : 0; uint32_t lo = id->id[0] ? p->im_fanout[id->id[0] - 1] : 0;
size_t hi = p->im_fanout[id->id[0]]; uint32_t hi = p->im_fanout[id->id[0]];
do { do {
size_t mid = (lo + hi) >> 1; uint32_t mid = (lo + hi) >> 1;
size_t pos = 20 * mid; uint32_t pos = 20 * mid;
int cmp = memcmp(id->id, data + pos, 20); int cmp = memcmp(id->id, data + pos, 20);
if (cmp < 0) if (cmp < 0)
hi = mid; hi = mid;
else if (!cmp) { else if (!cmp) {
uint32_t o32 = decode32(p->im_offset32 + mid); *out = mid;
if (o32 & 0x80000000)
*out = decode64(p->im_offset64 + 2*(o32 & ~0x80000000));
else
*out = o32;
return GIT_SUCCESS; return GIT_SUCCESS;
} else } else
lo = mid + 1; lo = mid + 1;
...@@ -977,7 +973,7 @@ static git_packlist *packlist_get(git_odb *db) ...@@ -977,7 +973,7 @@ static git_packlist *packlist_get(git_odb *db)
return pl; return pl;
} }
static int search_packs(git_pack **p, off_t *offset, git_odb *db, const git_oid *id) static int search_packs(git_pack **p, uint32_t *n, git_odb *db, const git_oid *id)
{ {
git_packlist *pl = packlist_get(db); git_packlist *pl = packlist_get(db);
size_t j; size_t j;
...@@ -988,7 +984,7 @@ static int search_packs(git_pack **p, off_t *offset, git_odb *db, const git_oid ...@@ -988,7 +984,7 @@ static int search_packs(git_pack **p, off_t *offset, git_odb *db, const git_oid
for (j = 0; j < pl->n_packs; j++) { for (j = 0; j < pl->n_packs; j++) {
git_pack *pack = pl->packs[j]; git_pack *pack = pl->packs[j];
off_t pos; uint32_t pos;
int res; int res;
if (pack_openidx(pack)) if (pack_openidx(pack))
...@@ -1000,8 +996,8 @@ static int search_packs(git_pack **p, off_t *offset, git_odb *db, const git_oid ...@@ -1000,8 +996,8 @@ static int search_packs(git_pack **p, off_t *offset, git_odb *db, const git_oid
packlist_dec(db, pl); packlist_dec(db, pl);
if (p) if (p)
*p = pack; *p = pack;
if (offset) if (n)
*offset = pos; *n = pos;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -1132,18 +1128,18 @@ int git_odb__read_loose(git_obj *out, git_odb *db, const git_oid *id) ...@@ -1132,18 +1128,18 @@ int git_odb__read_loose(git_obj *out, git_odb *db, const git_oid *id)
static int read_packed(git_obj *out, git_pack *p, const git_oid *id) static int read_packed(git_obj *out, git_pack *p, const git_oid *id)
{ {
off_t pos; uint32_t n;
int res; int res;
assert(out && p && id); assert(out && p && id);
if (pack_openidx(p)) if (pack_openidx(p))
return GIT_ERROR; return GIT_ERROR;
res = p->idx_search(&pos, p, id); res = p->idx_search(&n, p, id);
pack_decidx(p); pack_decidx(p);
if (!res) { if (!res) {
/* TODO unpack object at pos */ /* TODO unpack object */
res = GIT_ERROR; res = GIT_ERROR;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment