object.h: move some inline functions and defines from cache.h
[alt-git.git] / pack-revindex.c
blob4d9bb41b4dbcda342ebfced2e15e05f27d1e12a7
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "pack-revindex.h"
4 #include "object-file.h"
5 #include "object-store.h"
6 #include "packfile.h"
7 #include "trace2.h"
8 #include "config.h"
9 #include "midx.h"
11 struct revindex_entry {
12 off_t offset;
13 unsigned int nr;
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"
57 * to "to".
59 struct revindex_entry *tmp, *from, *to;
60 int bits;
61 unsigned *pos;
63 ALLOC_ARRAY(pos, BUCKETS);
64 ALLOC_ARRAY(tmp, n);
65 from = entries;
66 to = tmp;
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) {
74 unsigned i;
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++)
89 pos[i] += pos[i-1];
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.
111 SWAP(from, to);
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.
118 if (from != entries)
119 COPY_ARRAY(entries, tmp, n);
120 free(tmp);
121 free(pos);
123 #undef BUCKET_FOR
124 #undef BUCKETS
125 #undef DIGIT_SIZE
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;
134 unsigned i;
135 const char *index = p->index_data;
136 const unsigned hashsz = the_hash_algo->rawsz;
138 ALLOC_ARRAY(p->revindex, num_ent + 1);
139 index += 4 * 256;
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;
149 } else {
150 p->revindex[i].offset = get_be64(off_64);
151 off_64 += 2;
153 p->revindex[i].nr = i;
155 } else {
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))
178 return -1;
179 create_pack_revindex(p);
180 return 0;
183 static char *pack_revindex_filename(struct packed_git *p)
185 size_t len;
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 {
195 uint32_t signature;
196 uint32_t version;
197 uint32_t hash_id;
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)
204 int fd, ret = 0;
205 struct stat st;
206 void *data = NULL;
207 size_t revindex_size;
208 struct revindex_header *hdr;
210 fd = git_open(revindex_name);
212 if (fd < 0) {
213 ret = -1;
214 goto cleanup;
216 if (fstat(fd, &st)) {
217 ret = error_errno(_("failed to read %s"), revindex_name);
218 goto cleanup;
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);
225 goto cleanup;
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);
230 goto cleanup;
233 data = xmmap(NULL, revindex_size, PROT_READ, MAP_PRIVATE, fd, 0);
234 hdr = data;
236 if (ntohl(hdr->signature) != RIDX_SIGNATURE) {
237 ret = error(_("reverse-index file %s has unknown signature"), revindex_name);
238 goto cleanup;
240 if (ntohl(hdr->version) != 1) {
241 ret = error(_("reverse-index file %s has unsupported version %"PRIu32),
242 revindex_name, ntohl(hdr->version));
243 goto cleanup;
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));
248 goto cleanup;
251 cleanup:
252 if (ret) {
253 if (data)
254 munmap(data, revindex_size);
255 } else {
256 *len_p = revindex_size;
257 *data_p = (const uint32_t *)data;
260 if (fd >= 0)
261 close(fd);
262 return ret;
265 static int load_pack_revindex_from_disk(struct packed_git *p)
267 char *revindex_name;
268 int ret;
269 if (open_pack_index(p))
270 return -1;
272 revindex_name = pack_revindex_filename(p);
274 ret = load_revindex_from_disk(revindex_name,
275 p->num_objects,
276 &p->revindex_map,
277 &p->revindex_size);
278 if (ret)
279 goto cleanup;
281 p->revindex_data = (const uint32_t *)((const char *)p->revindex_map + RIDX_HEADER_SIZE);
283 cleanup:
284 free(revindex_name);
285 return ret;
288 int load_pack_revindex(struct packed_git *p)
290 if (p->revindex || p->revindex_data)
291 return 0;
293 if (!load_pack_revindex_from_disk(p))
294 return 0;
295 else if (!create_pack_revindex_in_memory(p))
296 return 0;
297 return -1;
300 int load_midx_revindex(struct multi_pack_index *m)
302 struct strbuf revindex_name = STRBUF_INIT;
303 int ret;
305 if (m->revindex_data)
306 return 0;
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`
312 * file.
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
316 * MIDX.
318 trace2_data_string("load_midx_revindex", the_repository,
319 "source", "midx");
320 m->revindex_data = (const uint32_t *)m->chunk_revindex;
321 return 0;
324 trace2_data_string("load_midx_revindex", the_repository,
325 "source", "rev");
327 get_midx_rev_filename(&revindex_name, m);
329 ret = load_revindex_from_disk(revindex_name.buf,
330 m->num_objects,
331 &m->revindex_map,
332 &m->revindex_len);
333 if (ret)
334 goto cleanup;
336 m->revindex_data = (const uint32_t *)((const char *)m->revindex_map + RIDX_HEADER_SIZE);
338 cleanup:
339 strbuf_release(&revindex_name);
340 return ret;
343 int close_midx_revindex(struct multi_pack_index *m)
345 if (!m || !m->revindex_map)
346 return 0;
348 munmap((void*)m->revindex_map, m->revindex_len);
350 m->revindex_map = NULL;
351 m->revindex_data = NULL;
352 m->revindex_len = 0;
354 return 0;
357 int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos)
359 unsigned lo, hi;
361 if (load_pack_revindex(p) < 0)
362 return -1;
364 lo = 0;
365 hi = p->num_objects + 1;
367 do {
368 const unsigned mi = lo + (hi - lo) / 2;
369 off_t got = pack_pos_to_offset(p, mi);
371 if (got == ofs) {
372 *pos = mi;
373 return 0;
374 } else if (ofs < got)
375 hi = mi;
376 else
377 lo = mi + 1;
378 } while (lo < hi);
380 error("bad offset for revindex");
381 return -1;
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);
391 if (p->revindex)
392 return p->revindex[pos].nr;
393 else
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);
404 if (p->revindex)
405 return p->revindex[pos].offset;
406 else if (pos == p->num_objects)
407 return p->pack_size - the_hash_algo->rawsz;
408 else
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 {
422 uint32_t pack;
423 off_t offset;
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);
436 off_t versus_offset;
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
443 * comes first.
445 if (key_preferred && !versus_preferred)
446 return -1;
447 else if (!key_preferred && versus_preferred)
448 return 1;
450 /* Then, break ties first by comparing the pack IDs. */
451 if (key->pack < versus_pack)
452 return -1;
453 else if (key->pack > versus_pack)
454 return 1;
456 /* Finally, break ties by comparing offsets within a pack. */
457 versus_offset = nth_midxed_offset(midx, versus);
458 if (key->offset < versus_offset)
459 return -1;
460 else if (key->offset > versus_offset)
461 return 1;
463 return 0;
466 int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos)
468 struct midx_pack_key key;
469 uint32_t *found;
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);
478 key.midx = m;
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);
493 if (!found)
494 return error("bad offset for revindex");
496 *pos = found - m->revindex_data;
497 return 0;