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