1 #include "git-compat-util.h"
3 #include "pack-revindex.h"
4 #include "object-file.h"
5 #include "object-store.h"
11 struct revindex_entry
{
17 * Pack index for existing packs give us easy access to the offsets into
18 * corresponding pack file where each object's data starts, but the entries
19 * do not store the size of the compressed representation (uncompressed
20 * size is easily available by examining the pack entry header). It is
21 * also rather expensive to find the sha1 for an object given its offset.
23 * The pack index file is sorted by object name mapping to offset;
24 * this revindex array is a list of offset/index_nr pairs
25 * ordered by offset, so if you know the offset of an object, next offset
26 * is where its packed representation ends and the index_nr can be used to
27 * get the object sha1 from the main index.
31 * This is a least-significant-digit radix sort.
33 * It sorts each of the "n" items in "entries" by its offset field. The "max"
34 * parameter must be at least as large as the largest offset in the array,
35 * and lets us quit the sort early.
37 static void sort_revindex(struct revindex_entry
*entries
, unsigned n
, off_t max
)
40 * We use a "digit" size of 16 bits. That keeps our memory
41 * usage reasonable, and we can generally (for a 4G or smaller
42 * packfile) quit after two rounds of radix-sorting.
44 #define DIGIT_SIZE (16)
45 #define BUCKETS (1 << DIGIT_SIZE)
47 * We want to know the bucket that a[i] will go into when we are using
48 * the digit that is N bits from the (least significant) end.
50 #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
53 * We need O(n) temporary storage. Rather than do an extra copy of the
54 * partial results into "entries", we sort back and forth between the
55 * real array and temporary storage. In each iteration of the loop, we
56 * keep track of them with alias pointers, always sorting from "from"
59 struct revindex_entry
*tmp
, *from
, *to
;
63 ALLOC_ARRAY(pos
, BUCKETS
);
69 * If (max >> bits) is zero, then we know that the radix digit we are
70 * on (and any higher) will be zero for all entries, and our loop will
71 * be a no-op, as everybody lands in the same zero-th bucket.
73 for (bits
= 0; max
>> bits
; bits
+= DIGIT_SIZE
) {
76 memset(pos
, 0, BUCKETS
* sizeof(*pos
));
79 * We want pos[i] to store the index of the last element that
80 * will go in bucket "i" (actually one past the last element).
81 * To do this, we first count the items that will go in each
82 * bucket, which gives us a relative offset from the last
83 * bucket. We can then cumulatively add the index from the
84 * previous bucket to get the true index.
86 for (i
= 0; i
< n
; i
++)
87 pos
[BUCKET_FOR(from
, i
, bits
)]++;
88 for (i
= 1; i
< BUCKETS
; i
++)
92 * Now we can drop the elements into their correct buckets (in
93 * our temporary array). We iterate the pos counter backwards
94 * to avoid using an extra index to count up. And since we are
95 * going backwards there, we must also go backwards through the
96 * array itself, to keep the sort stable.
98 * Note that we use an unsigned iterator to make sure we can
99 * handle 2^32-1 objects, even on a 32-bit system. But this
100 * means we cannot use the more obvious "i >= 0" loop condition
101 * for counting backwards, and must instead check for
102 * wrap-around with UINT_MAX.
104 for (i
= n
- 1; i
!= UINT_MAX
; i
--)
105 to
[--pos
[BUCKET_FOR(from
, i
, bits
)]] = from
[i
];
108 * Now "to" contains the most sorted list, so we swap "from" and
109 * "to" for the next iteration.
115 * If we ended with our data in the original array, great. If not,
116 * we have to move it back from the temporary storage.
119 COPY_ARRAY(entries
, tmp
, n
);
129 * Ordered list of offsets of objects in the pack.
131 static void create_pack_revindex(struct packed_git
*p
)
133 const unsigned num_ent
= p
->num_objects
;
135 const char *index
= p
->index_data
;
136 const unsigned hashsz
= the_hash_algo
->rawsz
;
138 ALLOC_ARRAY(p
->revindex
, num_ent
+ 1);
141 if (p
->index_version
> 1) {
142 const uint32_t *off_32
=
143 (uint32_t *)(index
+ 8 + (size_t)p
->num_objects
* (hashsz
+ 4));
144 const uint32_t *off_64
= off_32
+ p
->num_objects
;
145 for (i
= 0; i
< num_ent
; i
++) {
146 const uint32_t off
= ntohl(*off_32
++);
147 if (!(off
& 0x80000000)) {
148 p
->revindex
[i
].offset
= off
;
150 p
->revindex
[i
].offset
= get_be64(off_64
);
153 p
->revindex
[i
].nr
= i
;
156 for (i
= 0; i
< num_ent
; i
++) {
157 const uint32_t hl
= *((uint32_t *)(index
+ (hashsz
+ 4) * i
));
158 p
->revindex
[i
].offset
= ntohl(hl
);
159 p
->revindex
[i
].nr
= i
;
164 * This knows the pack format -- the hash trailer
165 * follows immediately after the last object data.
167 p
->revindex
[num_ent
].offset
= p
->pack_size
- hashsz
;
168 p
->revindex
[num_ent
].nr
= -1;
169 sort_revindex(p
->revindex
, num_ent
, p
->pack_size
);
172 static int create_pack_revindex_in_memory(struct packed_git
*p
)
174 if (git_env_bool(GIT_TEST_REV_INDEX_DIE_IN_MEMORY
, 0))
175 die("dying as requested by '%s'",
176 GIT_TEST_REV_INDEX_DIE_IN_MEMORY
);
177 if (open_pack_index(p
))
179 create_pack_revindex(p
);
183 static char *pack_revindex_filename(struct packed_git
*p
)
186 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
187 BUG("pack_name does not end in .pack");
188 return xstrfmt("%.*s.rev", (int)len
, p
->pack_name
);
191 #define RIDX_HEADER_SIZE (12)
192 #define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz))
194 struct revindex_header
{
200 static int load_revindex_from_disk(char *revindex_name
,
201 uint32_t num_objects
,
202 const uint32_t **data_p
, size_t *len_p
)
207 size_t revindex_size
;
208 struct revindex_header
*hdr
;
210 fd
= git_open(revindex_name
);
216 if (fstat(fd
, &st
)) {
217 ret
= error_errno(_("failed to read %s"), revindex_name
);
221 revindex_size
= xsize_t(st
.st_size
);
223 if (revindex_size
< RIDX_MIN_SIZE
) {
224 ret
= error(_("reverse-index file %s is too small"), revindex_name
);
228 if (revindex_size
- RIDX_MIN_SIZE
!= st_mult(sizeof(uint32_t), num_objects
)) {
229 ret
= error(_("reverse-index file %s is corrupt"), revindex_name
);
233 data
= xmmap(NULL
, revindex_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
236 if (ntohl(hdr
->signature
) != RIDX_SIGNATURE
) {
237 ret
= error(_("reverse-index file %s has unknown signature"), revindex_name
);
240 if (ntohl(hdr
->version
) != 1) {
241 ret
= error(_("reverse-index file %s has unsupported version %"PRIu32
),
242 revindex_name
, ntohl(hdr
->version
));
245 if (!(ntohl(hdr
->hash_id
) == 1 || ntohl(hdr
->hash_id
) == 2)) {
246 ret
= error(_("reverse-index file %s has unsupported hash id %"PRIu32
),
247 revindex_name
, ntohl(hdr
->hash_id
));
254 munmap(data
, revindex_size
);
256 *len_p
= revindex_size
;
257 *data_p
= (const uint32_t *)data
;
265 static int load_pack_revindex_from_disk(struct packed_git
*p
)
269 if (open_pack_index(p
))
272 revindex_name
= pack_revindex_filename(p
);
274 ret
= load_revindex_from_disk(revindex_name
,
281 p
->revindex_data
= (const uint32_t *)((const char *)p
->revindex_map
+ RIDX_HEADER_SIZE
);
288 int load_pack_revindex(struct packed_git
*p
)
290 if (p
->revindex
|| p
->revindex_data
)
293 if (!load_pack_revindex_from_disk(p
))
295 else if (!create_pack_revindex_in_memory(p
))
300 int load_midx_revindex(struct multi_pack_index
*m
)
302 struct strbuf revindex_name
= STRBUF_INIT
;
305 if (m
->revindex_data
)
308 if (m
->chunk_revindex
) {
310 * If the MIDX `m` has a `RIDX` chunk, then use its contents for
311 * the reverse index instead of trying to load a separate `.rev`
314 * Note that we do *not* set `m->revindex_map` here, since we do
315 * not want to accidentally call munmap() in the middle of the
318 trace2_data_string("load_midx_revindex", the_repository
,
320 m
->revindex_data
= (const uint32_t *)m
->chunk_revindex
;
324 trace2_data_string("load_midx_revindex", the_repository
,
327 get_midx_rev_filename(&revindex_name
, m
);
329 ret
= load_revindex_from_disk(revindex_name
.buf
,
336 m
->revindex_data
= (const uint32_t *)((const char *)m
->revindex_map
+ RIDX_HEADER_SIZE
);
339 strbuf_release(&revindex_name
);
343 int close_midx_revindex(struct multi_pack_index
*m
)
345 if (!m
|| !m
->revindex_map
)
348 munmap((void*)m
->revindex_map
, m
->revindex_len
);
350 m
->revindex_map
= NULL
;
351 m
->revindex_data
= NULL
;
357 int offset_to_pack_pos(struct packed_git
*p
, off_t ofs
, uint32_t *pos
)
361 if (load_pack_revindex(p
) < 0)
365 hi
= p
->num_objects
+ 1;
368 const unsigned mi
= lo
+ (hi
- lo
) / 2;
369 off_t got
= pack_pos_to_offset(p
, mi
);
374 } else if (ofs
< got
)
380 error("bad offset for revindex");
384 uint32_t pack_pos_to_index(struct packed_git
*p
, uint32_t pos
)
386 if (!(p
->revindex
|| p
->revindex_data
))
387 BUG("pack_pos_to_index: reverse index not yet loaded");
388 if (p
->num_objects
<= pos
)
389 BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32
, pos
);
392 return p
->revindex
[pos
].nr
;
394 return get_be32(p
->revindex_data
+ pos
);
397 off_t
pack_pos_to_offset(struct packed_git
*p
, uint32_t pos
)
399 if (!(p
->revindex
|| p
->revindex_data
))
400 BUG("pack_pos_to_index: reverse index not yet loaded");
401 if (p
->num_objects
< pos
)
402 BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32
, pos
);
405 return p
->revindex
[pos
].offset
;
406 else if (pos
== p
->num_objects
)
407 return p
->pack_size
- the_hash_algo
->rawsz
;
409 return nth_packed_object_offset(p
, pack_pos_to_index(p
, pos
));
412 uint32_t pack_pos_to_midx(struct multi_pack_index
*m
, uint32_t pos
)
414 if (!m
->revindex_data
)
415 BUG("pack_pos_to_midx: reverse index not yet loaded");
416 if (m
->num_objects
<= pos
)
417 BUG("pack_pos_to_midx: out-of-bounds object at %"PRIu32
, pos
);
418 return get_be32(m
->revindex_data
+ pos
);
421 struct midx_pack_key
{
425 uint32_t preferred_pack
;
426 struct multi_pack_index
*midx
;
429 static int midx_pack_order_cmp(const void *va
, const void *vb
)
431 const struct midx_pack_key
*key
= va
;
432 struct multi_pack_index
*midx
= key
->midx
;
434 uint32_t versus
= pack_pos_to_midx(midx
, (uint32_t*)vb
- (const uint32_t *)midx
->revindex_data
);
435 uint32_t versus_pack
= nth_midxed_pack_int_id(midx
, versus
);
438 uint32_t key_preferred
= key
->pack
== key
->preferred_pack
;
439 uint32_t versus_preferred
= versus_pack
== key
->preferred_pack
;
442 * First, compare the preferred-ness, noting that the preferred pack
445 if (key_preferred
&& !versus_preferred
)
447 else if (!key_preferred
&& versus_preferred
)
450 /* Then, break ties first by comparing the pack IDs. */
451 if (key
->pack
< versus_pack
)
453 else if (key
->pack
> versus_pack
)
456 /* Finally, break ties by comparing offsets within a pack. */
457 versus_offset
= nth_midxed_offset(midx
, versus
);
458 if (key
->offset
< versus_offset
)
460 else if (key
->offset
> versus_offset
)
466 int midx_to_pack_pos(struct multi_pack_index
*m
, uint32_t at
, uint32_t *pos
)
468 struct midx_pack_key key
;
471 if (!m
->revindex_data
)
472 BUG("midx_to_pack_pos: reverse index not yet loaded");
473 if (m
->num_objects
<= at
)
474 BUG("midx_to_pack_pos: out-of-bounds object at %"PRIu32
, at
);
476 key
.pack
= nth_midxed_pack_int_id(m
, at
);
477 key
.offset
= nth_midxed_offset(m
, at
);
480 * The preferred pack sorts first, so determine its identifier by
481 * looking at the first object in pseudo-pack order.
483 * Note that if no --preferred-pack is explicitly given when writing a
484 * multi-pack index, then whichever pack has the lowest identifier
485 * implicitly is preferred (and includes all its objects, since ties are
486 * broken first by pack identifier).
488 key
.preferred_pack
= nth_midxed_pack_int_id(m
, pack_pos_to_midx(m
, 0));
490 found
= bsearch(&key
, m
->revindex_data
, m
->num_objects
,
491 sizeof(*m
->revindex_data
), midx_pack_order_cmp
);
494 return error("bad offset for revindex");
496 *pos
= found
- m
->revindex_data
;