use COPY_ARRAY
[git/git.git] / pack-revindex.c
CommitLineData
3449f8c4
NP
1#include "cache.h"
2#include "pack-revindex.h"
3
4/*
5 * Pack index for existing packs give us easy access to the offsets into
6 * corresponding pack file where each object's data starts, but the entries
7 * do not store the size of the compressed representation (uncompressed
8 * size is easily available by examining the pack entry header). It is
9 * also rather expensive to find the sha1 for an object given its offset.
10 *
f4015337
JK
11 * The pack index file is sorted by object name mapping to offset;
12 * this revindex array is a list of offset/index_nr pairs
3449f8c4
NP
13 * ordered by offset, so if you know the offset of an object, next offset
14 * is where its packed representation ends and the index_nr can be used to
15 * get the object sha1 from the main index.
16 */
17
8b8dfd51
JK
18/*
19 * This is a least-significant-digit radix sort.
20 *
21 * It sorts each of the "n" items in "entries" by its offset field. The "max"
22 * parameter must be at least as large as the largest offset in the array,
23 * and lets us quit the sort early.
24 */
25static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
3449f8c4 26{
8b8dfd51
JK
27 /*
28 * We use a "digit" size of 16 bits. That keeps our memory
29 * usage reasonable, and we can generally (for a 4G or smaller
30 * packfile) quit after two rounds of radix-sorting.
31 */
32#define DIGIT_SIZE (16)
33#define BUCKETS (1 << DIGIT_SIZE)
34 /*
35 * We want to know the bucket that a[i] will go into when we are using
36 * the digit that is N bits from the (least significant) end.
37 */
38#define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
39
40 /*
41 * We need O(n) temporary storage. Rather than do an extra copy of the
42 * partial results into "entries", we sort back and forth between the
43 * real array and temporary storage. In each iteration of the loop, we
44 * keep track of them with alias pointers, always sorting from "from"
45 * to "to".
46 */
b32fa95f 47 struct revindex_entry *tmp, *from, *to;
8b8dfd51 48 int bits;
b32fa95f
JK
49 unsigned *pos;
50
51 ALLOC_ARRAY(pos, BUCKETS);
52 ALLOC_ARRAY(tmp, n);
53 from = entries;
54 to = tmp;
8b8dfd51
JK
55
56 /*
57 * If (max >> bits) is zero, then we know that the radix digit we are
58 * on (and any higher) will be zero for all entries, and our loop will
59 * be a no-op, as everybody lands in the same zero-th bucket.
60 */
61 for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
62 struct revindex_entry *swap;
63 unsigned i;
64
65 memset(pos, 0, BUCKETS * sizeof(*pos));
66
67 /*
68 * We want pos[i] to store the index of the last element that
69 * will go in bucket "i" (actually one past the last element).
70 * To do this, we first count the items that will go in each
71 * bucket, which gives us a relative offset from the last
72 * bucket. We can then cumulatively add the index from the
73 * previous bucket to get the true index.
74 */
75 for (i = 0; i < n; i++)
76 pos[BUCKET_FOR(from, i, bits)]++;
77 for (i = 1; i < BUCKETS; i++)
78 pos[i] += pos[i-1];
79
80 /*
81 * Now we can drop the elements into their correct buckets (in
82 * our temporary array). We iterate the pos counter backwards
83 * to avoid using an extra index to count up. And since we are
84 * going backwards there, we must also go backwards through the
85 * array itself, to keep the sort stable.
86 *
87 * Note that we use an unsigned iterator to make sure we can
88 * handle 2^32-1 objects, even on a 32-bit system. But this
89 * means we cannot use the more obvious "i >= 0" loop condition
90 * for counting backwards, and must instead check for
91 * wrap-around with UINT_MAX.
92 */
93 for (i = n - 1; i != UINT_MAX; i--)
94 to[--pos[BUCKET_FOR(from, i, bits)]] = from[i];
95
96 /*
97 * Now "to" contains the most sorted list, so we swap "from" and
98 * "to" for the next iteration.
99 */
100 swap = from;
101 from = to;
102 to = swap;
103 }
104
105 /*
106 * If we ended with our data in the original array, great. If not,
107 * we have to move it back from the temporary storage.
108 */
109 if (from != entries)
45ccef87 110 COPY_ARRAY(entries, tmp, n);
8b8dfd51
JK
111 free(tmp);
112 free(pos);
113
114#undef BUCKET_FOR
115#undef BUCKETS
116#undef DIGIT_SIZE
3449f8c4
NP
117}
118
119/*
120 * Ordered list of offsets of objects in the pack.
121 */
9d98bbf5 122static void create_pack_revindex(struct packed_git *p)
3449f8c4 123{
012b32bb
JK
124 unsigned num_ent = p->num_objects;
125 unsigned i;
3449f8c4
NP
126 const char *index = p->index_data;
127
11529ece 128 ALLOC_ARRAY(p->revindex, num_ent + 1);
3449f8c4
NP
129 index += 4 * 256;
130
131 if (p->index_version > 1) {
132 const uint32_t *off_32 =
133 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
134 const uint32_t *off_64 = off_32 + p->num_objects;
135 for (i = 0; i < num_ent; i++) {
136 uint32_t off = ntohl(*off_32++);
137 if (!(off & 0x80000000)) {
9d98bbf5 138 p->revindex[i].offset = off;
3449f8c4 139 } else {
9d98bbf5 140 p->revindex[i].offset =
3449f8c4 141 ((uint64_t)ntohl(*off_64++)) << 32;
9d98bbf5 142 p->revindex[i].offset |=
3449f8c4
NP
143 ntohl(*off_64++);
144 }
9d98bbf5 145 p->revindex[i].nr = i;
3449f8c4
NP
146 }
147 } else {
148 for (i = 0; i < num_ent; i++) {
149 uint32_t hl = *((uint32_t *)(index + 24 * i));
9d98bbf5
JK
150 p->revindex[i].offset = ntohl(hl);
151 p->revindex[i].nr = i;
3449f8c4
NP
152 }
153 }
154
155 /* This knows the pack format -- the 20-byte trailer
156 * follows immediately after the last object data.
157 */
9d98bbf5
JK
158 p->revindex[num_ent].offset = p->pack_size - 20;
159 p->revindex[num_ent].nr = -1;
160 sort_revindex(p->revindex, num_ent, p->pack_size);
3449f8c4
NP
161}
162
9d98bbf5 163void load_pack_revindex(struct packed_git *p)
3449f8c4 164{
9d98bbf5
JK
165 if (!p->revindex)
166 create_pack_revindex(p);
92e5c77c
VM
167}
168
9d98bbf5 169int find_revindex_position(struct packed_git *p, off_t ofs)
92e5c77c
VM
170{
171 int lo = 0;
9d98bbf5
JK
172 int hi = p->num_objects + 1;
173 struct revindex_entry *revindex = p->revindex;
92e5c77c 174
3449f8c4 175 do {
012b32bb 176 unsigned mi = lo + (hi - lo) / 2;
3449f8c4 177 if (revindex[mi].offset == ofs) {
92e5c77c 178 return mi;
3449f8c4
NP
179 } else if (ofs < revindex[mi].offset)
180 hi = mi;
181 else
182 lo = mi + 1;
183 } while (lo < hi);
92e5c77c 184
08698b1e 185 error("bad offset for revindex");
92e5c77c
VM
186 return -1;
187}
188
189struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
190{
9d98bbf5
JK
191 int pos;
192
193 load_pack_revindex(p);
194 pos = find_revindex_position(p, ofs);
92e5c77c
VM
195
196 if (pos < 0)
197 return NULL;
198
9d98bbf5 199 return p->revindex + pos;
3449f8c4 200}