]>
Commit | Line | Data |
---|---|---|
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 | * | |
11 | * We build a hashtable of existing packs (pack_revindex), and keep reverse | |
12 | * index here -- pack index file is sorted by object name mapping to offset; | |
13 | * this pack_revindex[].revindex array is a list of offset/index_nr pairs | |
14 | * ordered by offset, so if you know the offset of an object, next offset | |
15 | * is where its packed representation ends and the index_nr can be used to | |
16 | * get the object sha1 from the main index. | |
17 | */ | |
18 | ||
3449f8c4 NP |
19 | static struct pack_revindex *pack_revindex; |
20 | static int pack_revindex_hashsz; | |
21 | ||
22 | static int pack_revindex_ix(struct packed_git *p) | |
23 | { | |
56a1a3ab | 24 | unsigned long ui = (unsigned long)(intptr_t)p; |
3449f8c4 NP |
25 | int i; |
26 | ||
27 | ui = ui ^ (ui >> 16); /* defeat structure alignment */ | |
28 | i = (int)(ui % pack_revindex_hashsz); | |
29 | while (pack_revindex[i].p) { | |
30 | if (pack_revindex[i].p == p) | |
31 | return i; | |
32 | if (++i == pack_revindex_hashsz) | |
33 | i = 0; | |
34 | } | |
35 | return -1 - i; | |
36 | } | |
37 | ||
1f5c74f6 | 38 | static void init_pack_revindex(void) |
3449f8c4 NP |
39 | { |
40 | int num; | |
41 | struct packed_git *p; | |
42 | ||
43 | for (num = 0, p = packed_git; p; p = p->next) | |
44 | num++; | |
45 | if (!num) | |
46 | return; | |
47 | pack_revindex_hashsz = num * 11; | |
48d547fb | 48 | pack_revindex = xcalloc(pack_revindex_hashsz, sizeof(*pack_revindex)); |
3449f8c4 NP |
49 | for (p = packed_git; p; p = p->next) { |
50 | num = pack_revindex_ix(p); | |
51 | num = - 1 - num; | |
52 | pack_revindex[num].p = p; | |
53 | } | |
54 | /* revindex elements are lazily initialized */ | |
55 | } | |
56 | ||
8b8dfd51 JK |
57 | /* |
58 | * This is a least-significant-digit radix sort. | |
59 | * | |
60 | * It sorts each of the "n" items in "entries" by its offset field. The "max" | |
61 | * parameter must be at least as large as the largest offset in the array, | |
62 | * and lets us quit the sort early. | |
63 | */ | |
64 | static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max) | |
3449f8c4 | 65 | { |
8b8dfd51 JK |
66 | /* |
67 | * We use a "digit" size of 16 bits. That keeps our memory | |
68 | * usage reasonable, and we can generally (for a 4G or smaller | |
69 | * packfile) quit after two rounds of radix-sorting. | |
70 | */ | |
71 | #define DIGIT_SIZE (16) | |
72 | #define BUCKETS (1 << DIGIT_SIZE) | |
73 | /* | |
74 | * We want to know the bucket that a[i] will go into when we are using | |
75 | * the digit that is N bits from the (least significant) end. | |
76 | */ | |
77 | #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1)) | |
78 | ||
79 | /* | |
80 | * We need O(n) temporary storage. Rather than do an extra copy of the | |
81 | * partial results into "entries", we sort back and forth between the | |
82 | * real array and temporary storage. In each iteration of the loop, we | |
83 | * keep track of them with alias pointers, always sorting from "from" | |
84 | * to "to". | |
85 | */ | |
b32fa95f | 86 | struct revindex_entry *tmp, *from, *to; |
8b8dfd51 | 87 | int bits; |
b32fa95f JK |
88 | unsigned *pos; |
89 | ||
90 | ALLOC_ARRAY(pos, BUCKETS); | |
91 | ALLOC_ARRAY(tmp, n); | |
92 | from = entries; | |
93 | to = tmp; | |
8b8dfd51 JK |
94 | |
95 | /* | |
96 | * If (max >> bits) is zero, then we know that the radix digit we are | |
97 | * on (and any higher) will be zero for all entries, and our loop will | |
98 | * be a no-op, as everybody lands in the same zero-th bucket. | |
99 | */ | |
100 | for (bits = 0; max >> bits; bits += DIGIT_SIZE) { | |
101 | struct revindex_entry *swap; | |
102 | unsigned i; | |
103 | ||
104 | memset(pos, 0, BUCKETS * sizeof(*pos)); | |
105 | ||
106 | /* | |
107 | * We want pos[i] to store the index of the last element that | |
108 | * will go in bucket "i" (actually one past the last element). | |
109 | * To do this, we first count the items that will go in each | |
110 | * bucket, which gives us a relative offset from the last | |
111 | * bucket. We can then cumulatively add the index from the | |
112 | * previous bucket to get the true index. | |
113 | */ | |
114 | for (i = 0; i < n; i++) | |
115 | pos[BUCKET_FOR(from, i, bits)]++; | |
116 | for (i = 1; i < BUCKETS; i++) | |
117 | pos[i] += pos[i-1]; | |
118 | ||
119 | /* | |
120 | * Now we can drop the elements into their correct buckets (in | |
121 | * our temporary array). We iterate the pos counter backwards | |
122 | * to avoid using an extra index to count up. And since we are | |
123 | * going backwards there, we must also go backwards through the | |
124 | * array itself, to keep the sort stable. | |
125 | * | |
126 | * Note that we use an unsigned iterator to make sure we can | |
127 | * handle 2^32-1 objects, even on a 32-bit system. But this | |
128 | * means we cannot use the more obvious "i >= 0" loop condition | |
129 | * for counting backwards, and must instead check for | |
130 | * wrap-around with UINT_MAX. | |
131 | */ | |
132 | for (i = n - 1; i != UINT_MAX; i--) | |
133 | to[--pos[BUCKET_FOR(from, i, bits)]] = from[i]; | |
134 | ||
135 | /* | |
136 | * Now "to" contains the most sorted list, so we swap "from" and | |
137 | * "to" for the next iteration. | |
138 | */ | |
139 | swap = from; | |
140 | from = to; | |
141 | to = swap; | |
142 | } | |
143 | ||
144 | /* | |
145 | * If we ended with our data in the original array, great. If not, | |
146 | * we have to move it back from the temporary storage. | |
147 | */ | |
148 | if (from != entries) | |
149 | memcpy(entries, tmp, n * sizeof(*entries)); | |
150 | free(tmp); | |
151 | free(pos); | |
152 | ||
153 | #undef BUCKET_FOR | |
154 | #undef BUCKETS | |
155 | #undef DIGIT_SIZE | |
3449f8c4 NP |
156 | } |
157 | ||
158 | /* | |
159 | * Ordered list of offsets of objects in the pack. | |
160 | */ | |
161 | static void create_pack_revindex(struct pack_revindex *rix) | |
162 | { | |
163 | struct packed_git *p = rix->p; | |
012b32bb JK |
164 | unsigned num_ent = p->num_objects; |
165 | unsigned i; | |
3449f8c4 NP |
166 | const char *index = p->index_data; |
167 | ||
b32fa95f | 168 | ALLOC_ARRAY(rix->revindex, num_ent + 1); |
3449f8c4 NP |
169 | index += 4 * 256; |
170 | ||
171 | if (p->index_version > 1) { | |
172 | const uint32_t *off_32 = | |
173 | (uint32_t *)(index + 8 + p->num_objects * (20 + 4)); | |
174 | const uint32_t *off_64 = off_32 + p->num_objects; | |
175 | for (i = 0; i < num_ent; i++) { | |
176 | uint32_t off = ntohl(*off_32++); | |
177 | if (!(off & 0x80000000)) { | |
178 | rix->revindex[i].offset = off; | |
179 | } else { | |
180 | rix->revindex[i].offset = | |
181 | ((uint64_t)ntohl(*off_64++)) << 32; | |
182 | rix->revindex[i].offset |= | |
183 | ntohl(*off_64++); | |
184 | } | |
185 | rix->revindex[i].nr = i; | |
186 | } | |
187 | } else { | |
188 | for (i = 0; i < num_ent; i++) { | |
189 | uint32_t hl = *((uint32_t *)(index + 24 * i)); | |
190 | rix->revindex[i].offset = ntohl(hl); | |
191 | rix->revindex[i].nr = i; | |
192 | } | |
193 | } | |
194 | ||
195 | /* This knows the pack format -- the 20-byte trailer | |
196 | * follows immediately after the last object data. | |
197 | */ | |
198 | rix->revindex[num_ent].offset = p->pack_size - 20; | |
199 | rix->revindex[num_ent].nr = -1; | |
8b8dfd51 | 200 | sort_revindex(rix->revindex, num_ent, p->pack_size); |
3449f8c4 NP |
201 | } |
202 | ||
92e5c77c | 203 | struct pack_revindex *revindex_for_pack(struct packed_git *p) |
3449f8c4 NP |
204 | { |
205 | int num; | |
3449f8c4 | 206 | struct pack_revindex *rix; |
3449f8c4 | 207 | |
1f5c74f6 NP |
208 | if (!pack_revindex_hashsz) |
209 | init_pack_revindex(); | |
92e5c77c | 210 | |
3449f8c4 NP |
211 | num = pack_revindex_ix(p); |
212 | if (num < 0) | |
1f5c74f6 | 213 | die("internal error: pack revindex fubar"); |
3449f8c4 NP |
214 | |
215 | rix = &pack_revindex[num]; | |
216 | if (!rix->revindex) | |
217 | create_pack_revindex(rix); | |
3449f8c4 | 218 | |
92e5c77c VM |
219 | return rix; |
220 | } | |
221 | ||
222 | int find_revindex_position(struct pack_revindex *pridx, off_t ofs) | |
223 | { | |
224 | int lo = 0; | |
225 | int hi = pridx->p->num_objects + 1; | |
226 | struct revindex_entry *revindex = pridx->revindex; | |
227 | ||
3449f8c4 | 228 | do { |
012b32bb | 229 | unsigned mi = lo + (hi - lo) / 2; |
3449f8c4 | 230 | if (revindex[mi].offset == ofs) { |
92e5c77c | 231 | return mi; |
3449f8c4 NP |
232 | } else if (ofs < revindex[mi].offset) |
233 | hi = mi; | |
234 | else | |
235 | lo = mi + 1; | |
236 | } while (lo < hi); | |
92e5c77c | 237 | |
08698b1e | 238 | error("bad offset for revindex"); |
92e5c77c VM |
239 | return -1; |
240 | } | |
241 | ||
242 | struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs) | |
243 | { | |
244 | struct pack_revindex *pridx = revindex_for_pack(p); | |
245 | int pos = find_revindex_position(pridx, ofs); | |
246 | ||
247 | if (pos < 0) | |
248 | return NULL; | |
249 | ||
250 | return pridx->revindex + pos; | |
3449f8c4 | 251 | } |