2 #include "pack-revindex.h"
3 #include "object-store.h"
7 struct revindex_entry
{
13 * Pack index for existing packs give us easy access to the offsets into
14 * corresponding pack file where each object's data starts, but the entries
15 * do not store the size of the compressed representation (uncompressed
16 * size is easily available by examining the pack entry header). It is
17 * also rather expensive to find the sha1 for an object given its offset.
19 * The pack index file is sorted by object name mapping to offset;
20 * this revindex array is a list of offset/index_nr pairs
21 * ordered by offset, so if you know the offset of an object, next offset
22 * is where its packed representation ends and the index_nr can be used to
23 * get the object sha1 from the main index.
27 * This is a least-significant-digit radix sort.
29 * It sorts each of the "n" items in "entries" by its offset field. The "max"
30 * parameter must be at least as large as the largest offset in the array,
31 * and lets us quit the sort early.
33 static void sort_revindex(struct revindex_entry
*entries
, unsigned n
, off_t max
)
36 * We use a "digit" size of 16 bits. That keeps our memory
37 * usage reasonable, and we can generally (for a 4G or smaller
38 * packfile) quit after two rounds of radix-sorting.
40 #define DIGIT_SIZE (16)
41 #define BUCKETS (1 << DIGIT_SIZE)
43 * We want to know the bucket that a[i] will go into when we are using
44 * the digit that is N bits from the (least significant) end.
46 #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
49 * We need O(n) temporary storage. Rather than do an extra copy of the
50 * partial results into "entries", we sort back and forth between the
51 * real array and temporary storage. In each iteration of the loop, we
52 * keep track of them with alias pointers, always sorting from "from"
55 struct revindex_entry
*tmp
, *from
, *to
;
59 ALLOC_ARRAY(pos
, BUCKETS
);
65 * If (max >> bits) is zero, then we know that the radix digit we are
66 * on (and any higher) will be zero for all entries, and our loop will
67 * be a no-op, as everybody lands in the same zero-th bucket.
69 for (bits
= 0; max
>> bits
; bits
+= DIGIT_SIZE
) {
72 memset(pos
, 0, BUCKETS
* sizeof(*pos
));
75 * We want pos[i] to store the index of the last element that
76 * will go in bucket "i" (actually one past the last element).
77 * To do this, we first count the items that will go in each
78 * bucket, which gives us a relative offset from the last
79 * bucket. We can then cumulatively add the index from the
80 * previous bucket to get the true index.
82 for (i
= 0; i
< n
; i
++)
83 pos
[BUCKET_FOR(from
, i
, bits
)]++;
84 for (i
= 1; i
< BUCKETS
; i
++)
88 * Now we can drop the elements into their correct buckets (in
89 * our temporary array). We iterate the pos counter backwards
90 * to avoid using an extra index to count up. And since we are
91 * going backwards there, we must also go backwards through the
92 * array itself, to keep the sort stable.
94 * Note that we use an unsigned iterator to make sure we can
95 * handle 2^32-1 objects, even on a 32-bit system. But this
96 * means we cannot use the more obvious "i >= 0" loop condition
97 * for counting backwards, and must instead check for
98 * wrap-around with UINT_MAX.
100 for (i
= n
- 1; i
!= UINT_MAX
; i
--)
101 to
[--pos
[BUCKET_FOR(from
, i
, bits
)]] = from
[i
];
104 * Now "to" contains the most sorted list, so we swap "from" and
105 * "to" for the next iteration.
111 * If we ended with our data in the original array, great. If not,
112 * we have to move it back from the temporary storage.
115 COPY_ARRAY(entries
, tmp
, n
);
125 * Ordered list of offsets of objects in the pack.
127 static void create_pack_revindex(struct packed_git
*p
)
129 const unsigned num_ent
= p
->num_objects
;
131 const char *index
= p
->index_data
;
132 const unsigned hashsz
= the_hash_algo
->rawsz
;
134 ALLOC_ARRAY(p
->revindex
, num_ent
+ 1);
137 if (p
->index_version
> 1) {
138 const uint32_t *off_32
=
139 (uint32_t *)(index
+ 8 + (size_t)p
->num_objects
* (hashsz
+ 4));
140 const uint32_t *off_64
= off_32
+ p
->num_objects
;
141 for (i
= 0; i
< num_ent
; i
++) {
142 const uint32_t off
= ntohl(*off_32
++);
143 if (!(off
& 0x80000000)) {
144 p
->revindex
[i
].offset
= off
;
146 p
->revindex
[i
].offset
= get_be64(off_64
);
149 p
->revindex
[i
].nr
= i
;
152 for (i
= 0; i
< num_ent
; i
++) {
153 const uint32_t hl
= *((uint32_t *)(index
+ (hashsz
+ 4) * i
));
154 p
->revindex
[i
].offset
= ntohl(hl
);
155 p
->revindex
[i
].nr
= i
;
160 * This knows the pack format -- the hash trailer
161 * follows immediately after the last object data.
163 p
->revindex
[num_ent
].offset
= p
->pack_size
- hashsz
;
164 p
->revindex
[num_ent
].nr
= -1;
165 sort_revindex(p
->revindex
, num_ent
, p
->pack_size
);
168 static int create_pack_revindex_in_memory(struct packed_git
*p
)
170 if (git_env_bool(GIT_TEST_REV_INDEX_DIE_IN_MEMORY
, 0))
171 die("dying as requested by '%s'",
172 GIT_TEST_REV_INDEX_DIE_IN_MEMORY
);
173 if (open_pack_index(p
))
175 create_pack_revindex(p
);
179 static char *pack_revindex_filename(struct packed_git
*p
)
182 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
183 BUG("pack_name does not end in .pack");
184 return xstrfmt("%.*s.rev", (int)len
, p
->pack_name
);
187 #define RIDX_HEADER_SIZE (12)
188 #define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz))
190 struct revindex_header
{
196 static int load_revindex_from_disk(char *revindex_name
,
197 uint32_t num_objects
,
198 const uint32_t **data_p
, size_t *len_p
)
203 size_t revindex_size
;
204 struct revindex_header
*hdr
;
206 fd
= git_open(revindex_name
);
212 if (fstat(fd
, &st
)) {
213 ret
= error_errno(_("failed to read %s"), revindex_name
);
217 revindex_size
= xsize_t(st
.st_size
);
219 if (revindex_size
< RIDX_MIN_SIZE
) {
220 ret
= error(_("reverse-index file %s is too small"), revindex_name
);
224 if (revindex_size
- RIDX_MIN_SIZE
!= st_mult(sizeof(uint32_t), num_objects
)) {
225 ret
= error(_("reverse-index file %s is corrupt"), revindex_name
);
229 data
= xmmap(NULL
, revindex_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
232 if (ntohl(hdr
->signature
) != RIDX_SIGNATURE
) {
233 ret
= error(_("reverse-index file %s has unknown signature"), revindex_name
);
236 if (ntohl(hdr
->version
) != 1) {
237 ret
= error(_("reverse-index file %s has unsupported version %"PRIu32
),
238 revindex_name
, ntohl(hdr
->version
));
241 if (!(ntohl(hdr
->hash_id
) == 1 || ntohl(hdr
->hash_id
) == 2)) {
242 ret
= error(_("reverse-index file %s has unsupported hash id %"PRIu32
),
243 revindex_name
, ntohl(hdr
->hash_id
));
250 munmap(data
, revindex_size
);
252 *len_p
= revindex_size
;
253 *data_p
= (const uint32_t *)data
;
261 static int load_pack_revindex_from_disk(struct packed_git
*p
)
265 if (open_pack_index(p
))
268 revindex_name
= pack_revindex_filename(p
);
270 ret
= load_revindex_from_disk(revindex_name
,
277 p
->revindex_data
= (const uint32_t *)((const char *)p
->revindex_map
+ RIDX_HEADER_SIZE
);
284 int load_pack_revindex(struct packed_git
*p
)
286 if (p
->revindex
|| p
->revindex_data
)
289 if (!load_pack_revindex_from_disk(p
))
291 else if (!create_pack_revindex_in_memory(p
))
296 int offset_to_pack_pos(struct packed_git
*p
, off_t ofs
, uint32_t *pos
)
300 if (load_pack_revindex(p
) < 0)
304 hi
= p
->num_objects
+ 1;
307 const unsigned mi
= lo
+ (hi
- lo
) / 2;
308 off_t got
= pack_pos_to_offset(p
, mi
);
313 } else if (ofs
< got
)
319 error("bad offset for revindex");
323 uint32_t pack_pos_to_index(struct packed_git
*p
, uint32_t pos
)
325 if (!(p
->revindex
|| p
->revindex_data
))
326 BUG("pack_pos_to_index: reverse index not yet loaded");
327 if (p
->num_objects
<= pos
)
328 BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32
, pos
);
331 return p
->revindex
[pos
].nr
;
333 return get_be32(p
->revindex_data
+ pos
);
336 off_t
pack_pos_to_offset(struct packed_git
*p
, uint32_t pos
)
338 if (!(p
->revindex
|| p
->revindex_data
))
339 BUG("pack_pos_to_index: reverse index not yet loaded");
340 if (p
->num_objects
< pos
)
341 BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32
, pos
);
344 return p
->revindex
[pos
].offset
;
345 else if (pos
== p
->num_objects
)
346 return p
->pack_size
- the_hash_algo
->rawsz
;
348 return nth_packed_object_offset(p
, pack_pos_to_index(p
, pos
));