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