Skip to content

Commit f33fb6e

Browse files
ttaylorrgitster
authored andcommitted
pack-revindex: introduce a new API
In the next several patches, we will prepare for loading a reverse index either in memory (mapping the inverse of the .idx's contents in-core), or directly from a yet-to-be-introduced on-disk format. To prepare for that, we'll introduce an API that avoids the caller explicitly indexing the revindex pointer in the packed_git structure. There are four ways to interact with the reverse index. Accordingly, four functions will be exported from 'pack-revindex.h' by the time that the existing API is removed. A caller may: 1. Load the pack's reverse index. This involves opening up the index, generating an array, and then sorting it. Since opening the index can fail, this function ('load_pack_revindex()') returns an int. Accordingly, it takes only a single argument: the 'struct packed_git' the caller wants to build a reverse index for. This function is well-suited for both the current and new API. Callers will have to continue to open the reverse index explicitly, but this function will eventually learn how to detect and load a reverse index from the on-disk format, if one exists. Otherwise, it will fallback to generating one in memory from scratch. 2. Convert a pack position into an offset. This operation is now called `pack_pos_to_offset()`. It takes a pack and a position, and returns the corresponding off_t. Any error simply calls BUG(), since the callers are not well-suited to handle a failure and keep going. 3. Convert a pack position into an index position. Same as above; this takes a pack and a position, and returns a uint32_t. This operation is known as `pack_pos_to_index()`. The same thinking about error conditions applies here as well. 4. Find the pack position for a given offset. This operation is now known as `offset_to_pack_pos()`. It takes a pack, an offset, and a pointer to a uint32_t where the position is written, if an object exists at that offset. Otherwise, -1 is returned to indicate failure. Unlike some of the callers that used to access '->offset' and '->nr' directly, the error checking around this call is somewhat more robust. This is important since callers should always pass an offset which points at the boundary of two objects. The API, unlike direct access, enforces that that is the case. This will become important in a subsequent patch where a caller which does not but could check the return value treats the signed `-1` from `find_revindex_position()` as an index into the 'revindex' array. Two design warts are carried over into the new API: - Asking for the index position of an out-of-bounds object will result in a BUG() (since no such object exists), but asking for the offset of the non-existent object at the end of the pack returns the total size of the pack. This makes it convenient for callers who always want to take the difference of two adjacent object's offsets (to compute the on-disk size) but don't want to worry about boundaries at the end of the pack. - offset_to_pack_pos() lazily loads the reverse index, but pack_pos_to_index() doesn't (callers of the former are well-suited to handle errors, but callers of the latter are not). Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 72c4083 commit f33fb6e

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

pack-revindex.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,35 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
203203

204204
return p->revindex + pos;
205205
}
206+
207+
int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos)
208+
{
209+
int ret;
210+
211+
if (load_pack_revindex(p) < 0)
212+
return -1;
213+
214+
ret = find_revindex_position(p, ofs);
215+
if (ret < 0)
216+
return ret;
217+
*pos = ret;
218+
return 0;
219+
}
220+
221+
uint32_t pack_pos_to_index(struct packed_git *p, uint32_t pos)
222+
{
223+
if (!p->revindex)
224+
BUG("pack_pos_to_index: reverse index not yet loaded");
225+
if (p->num_objects <= pos)
226+
BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32, pos);
227+
return p->revindex[pos].nr;
228+
}
229+
230+
off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos)
231+
{
232+
if (!p->revindex)
233+
BUG("pack_pos_to_index: reverse index not yet loaded");
234+
if (p->num_objects < pos)
235+
BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32, pos);
236+
return p->revindex[pos].offset;
237+
}

pack-revindex.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,70 @@
11
#ifndef PACK_REVINDEX_H
22
#define PACK_REVINDEX_H
33

4+
/**
5+
* A revindex allows converting efficiently between three properties
6+
* of an object within a pack:
7+
*
8+
* - index position: the numeric position within the list of sorted object ids
9+
* found in the .idx file
10+
*
11+
* - pack position: the numeric position within the list of objects in their
12+
* order within the actual .pack file (i.e., 0 is the first object in the
13+
* .pack, 1 is the second, and so on)
14+
*
15+
* - offset: the byte offset within the .pack file at which the object contents
16+
* can be found
17+
*/
18+
419
struct packed_git;
520

621
struct revindex_entry {
722
off_t offset;
823
unsigned int nr;
924
};
1025

26+
/*
27+
* load_pack_revindex populates the revindex's internal data-structures for the
28+
* given pack, returning zero on success and a negative value otherwise.
29+
*/
1130
int load_pack_revindex(struct packed_git *p);
1231
int find_revindex_position(struct packed_git *p, off_t ofs);
1332

1433
struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs);
1534

35+
/*
36+
* offset_to_pack_pos converts an object offset to a pack position. This
37+
* function returns zero on success, and a negative number otherwise. The
38+
* parameter 'pos' is usable only on success.
39+
*
40+
* If the reverse index has not yet been loaded, this function loads it lazily,
41+
* and returns an negative number if an error was encountered.
42+
*
43+
* This function runs in time O(log N) with the number of objects in the pack.
44+
*/
45+
int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos);
46+
47+
/*
48+
* pack_pos_to_index converts the given pack-relative position 'pos' by
49+
* returning an index-relative position.
50+
*
51+
* If the reverse index has not yet been loaded, or the position is out of
52+
* bounds, this function aborts.
53+
*
54+
* This function runs in constant time.
55+
*/
56+
uint32_t pack_pos_to_index(struct packed_git *p, uint32_t pos);
57+
58+
/*
59+
* pack_pos_to_offset converts the given pack-relative position 'pos' into a
60+
* pack offset. For a pack with 'N' objects, asking for position 'N' will return
61+
* the total size (in bytes) of the pack.
62+
*
63+
* If the reverse index has not yet been loaded, or the position is out of
64+
* bounds, this function aborts.
65+
*
66+
* This function runs in constant time.
67+
*/
68+
off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos);
69+
1670
#endif

0 commit comments

Comments
 (0)