Start the 2.41 cycle
[git/debian.git] / split-index.c
blob95ecfa531954e92947bd13175b760d680f18d0be
1 #include "cache.h"
2 #include "alloc.h"
3 #include "split-index.h"
4 #include "ewah/ewok.h"
6 struct split_index *init_split_index(struct index_state *istate)
8 if (!istate->split_index) {
9 if (istate->sparse_index)
10 die(_("cannot use split index with a sparse index"));
12 CALLOC_ARRAY(istate->split_index, 1);
13 istate->split_index->refcount = 1;
15 return istate->split_index;
18 int read_link_extension(struct index_state *istate,
19 const void *data_, unsigned long sz)
21 const unsigned char *data = data_;
22 struct split_index *si;
23 int ret;
25 if (sz < the_hash_algo->rawsz)
26 return error("corrupt link extension (too short)");
27 si = init_split_index(istate);
28 oidread(&si->base_oid, data);
29 data += the_hash_algo->rawsz;
30 sz -= the_hash_algo->rawsz;
31 if (!sz)
32 return 0;
33 si->delete_bitmap = ewah_new();
34 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
35 if (ret < 0)
36 return error("corrupt delete bitmap in link extension");
37 data += ret;
38 sz -= ret;
39 si->replace_bitmap = ewah_new();
40 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
41 if (ret < 0)
42 return error("corrupt replace bitmap in link extension");
43 if (ret != sz)
44 return error("garbage at the end of link extension");
45 return 0;
48 int write_link_extension(struct strbuf *sb,
49 struct index_state *istate)
51 struct split_index *si = istate->split_index;
52 strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
53 if (!si->delete_bitmap && !si->replace_bitmap)
54 return 0;
55 ewah_serialize_strbuf(si->delete_bitmap, sb);
56 ewah_serialize_strbuf(si->replace_bitmap, sb);
57 return 0;
60 static void mark_base_index_entries(struct index_state *base)
62 int i;
64 * To keep track of the shared entries between
65 * istate->base->cache[] and istate->cache[], base entry
66 * position is stored in each base entry. All positions start
67 * from 1 instead of 0, which is reserved to say "this is a new
68 * entry".
70 for (i = 0; i < base->cache_nr; i++)
71 base->cache[i]->index = i + 1;
74 void move_cache_to_base_index(struct index_state *istate)
76 struct split_index *si = istate->split_index;
77 int i;
80 * If there was a previous base index, then transfer ownership of allocated
81 * entries to the parent index.
83 if (si->base &&
84 si->base->ce_mem_pool) {
86 if (!istate->ce_mem_pool) {
87 istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
88 mem_pool_init(istate->ce_mem_pool, 0);
91 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
94 ALLOC_ARRAY(si->base, 1);
95 index_state_init(si->base, istate->repo);
96 si->base->version = istate->version;
97 /* zero timestamp disables racy test in ce_write_index() */
98 si->base->timestamp = istate->timestamp;
99 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
100 si->base->cache_nr = istate->cache_nr;
103 * The mem_pool needs to move with the allocated entries.
105 si->base->ce_mem_pool = istate->ce_mem_pool;
106 istate->ce_mem_pool = NULL;
108 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
109 mark_base_index_entries(si->base);
110 for (i = 0; i < si->base->cache_nr; i++)
111 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
114 static void mark_entry_for_delete(size_t pos, void *data)
116 struct index_state *istate = data;
117 if (pos >= istate->cache_nr)
118 die("position for delete %d exceeds base index size %d",
119 (int)pos, istate->cache_nr);
120 istate->cache[pos]->ce_flags |= CE_REMOVE;
121 istate->split_index->nr_deletions++;
124 static void replace_entry(size_t pos, void *data)
126 struct index_state *istate = data;
127 struct split_index *si = istate->split_index;
128 struct cache_entry *dst, *src;
130 if (pos >= istate->cache_nr)
131 die("position for replacement %d exceeds base index size %d",
132 (int)pos, istate->cache_nr);
133 if (si->nr_replacements >= si->saved_cache_nr)
134 die("too many replacements (%d vs %d)",
135 si->nr_replacements, si->saved_cache_nr);
136 dst = istate->cache[pos];
137 if (dst->ce_flags & CE_REMOVE)
138 die("entry %d is marked as both replaced and deleted",
139 (int)pos);
140 src = si->saved_cache[si->nr_replacements];
141 if (ce_namelen(src))
142 die("corrupt link extension, entry %d should have "
143 "zero length name", (int)pos);
144 src->index = pos + 1;
145 src->ce_flags |= CE_UPDATE_IN_BASE;
146 src->ce_namelen = dst->ce_namelen;
147 copy_cache_entry(dst, src);
148 discard_cache_entry(src);
149 si->nr_replacements++;
152 void merge_base_index(struct index_state *istate)
154 struct split_index *si = istate->split_index;
155 unsigned int i;
157 mark_base_index_entries(si->base);
159 si->saved_cache = istate->cache;
160 si->saved_cache_nr = istate->cache_nr;
161 istate->cache_nr = si->base->cache_nr;
162 istate->cache = NULL;
163 istate->cache_alloc = 0;
164 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
165 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
167 si->nr_deletions = 0;
168 si->nr_replacements = 0;
169 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
170 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
171 if (si->nr_deletions)
172 remove_marked_cache_entries(istate, 0);
174 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
175 if (!ce_namelen(si->saved_cache[i]))
176 die("corrupt link extension, entry %d should "
177 "have non-zero length name", i);
178 add_index_entry(istate, si->saved_cache[i],
179 ADD_CACHE_OK_TO_ADD |
180 ADD_CACHE_KEEP_CACHE_TREE |
182 * we may have to replay what
183 * merge-recursive.c:update_stages()
184 * does, which has this flag on
186 ADD_CACHE_SKIP_DFCHECK);
187 si->saved_cache[i] = NULL;
190 ewah_free(si->delete_bitmap);
191 ewah_free(si->replace_bitmap);
192 FREE_AND_NULL(si->saved_cache);
193 si->delete_bitmap = NULL;
194 si->replace_bitmap = NULL;
195 si->saved_cache_nr = 0;
199 * Compare most of the fields in two cache entries, i.e. all except the
200 * hashmap_entry and the name.
202 static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
204 const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
205 CE_EXTENDED_FLAGS;
206 unsigned int ce_flags = a->ce_flags;
207 unsigned int base_flags = b->ce_flags;
208 int ret;
210 /* only on-disk flags matter */
211 a->ce_flags &= ondisk_flags;
212 b->ce_flags &= ondisk_flags;
213 ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
214 offsetof(struct cache_entry, name) -
215 offsetof(struct cache_entry, oid)) ||
216 !oideq(&a->oid, &b->oid);
217 a->ce_flags = ce_flags;
218 b->ce_flags = base_flags;
220 return ret;
223 void prepare_to_write_split_index(struct index_state *istate)
225 struct split_index *si = init_split_index(istate);
226 struct cache_entry **entries = NULL, *ce;
227 int i, nr_entries = 0, nr_alloc = 0;
229 si->delete_bitmap = ewah_new();
230 si->replace_bitmap = ewah_new();
232 if (si->base) {
233 /* Go through istate->cache[] and mark CE_MATCHED to
234 * entry with positive index. We'll go through
235 * base->cache[] later to delete all entries in base
236 * that are not marked with either CE_MATCHED or
237 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
238 * duplicate, deduplicate it.
240 for (i = 0; i < istate->cache_nr; i++) {
241 struct cache_entry *base;
242 ce = istate->cache[i];
243 if (!ce->index) {
245 * During simple update index operations this
246 * is a cache entry that is not present in
247 * the shared index. It will be added to the
248 * split index.
250 * However, it might also represent a file
251 * that already has a cache entry in the
252 * shared index, but a new index has just
253 * been constructed by unpack_trees(), and
254 * this entry now refers to different content
255 * than what was recorded in the original
256 * index, e.g. during 'read-tree -m HEAD^' or
257 * 'checkout HEAD^'. In this case the
258 * original entry in the shared index will be
259 * marked as deleted, and this entry will be
260 * added to the split index.
262 continue;
264 if (ce->index > si->base->cache_nr) {
265 BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
266 ce->index, si->base->cache_nr);
268 ce->ce_flags |= CE_MATCHED; /* or "shared" */
269 base = si->base->cache[ce->index - 1];
270 if (ce == base) {
271 /* The entry is present in the shared index. */
272 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
274 * Already marked for inclusion in
275 * the split index, either because
276 * the corresponding file was
277 * modified and the cached stat data
278 * was refreshed, or because there
279 * is already a replacement entry in
280 * the split index.
281 * Nothing more to do here.
283 } else if (!ce_uptodate(ce) &&
284 is_racy_timestamp(istate, ce)) {
286 * A racily clean cache entry stored
287 * only in the shared index: it must
288 * be added to the split index, so
289 * the subsequent do_write_index()
290 * can smudge its stat data.
292 ce->ce_flags |= CE_UPDATE_IN_BASE;
293 } else {
295 * The entry is only present in the
296 * shared index and it was not
297 * refreshed.
298 * Just leave it there.
301 continue;
303 if (ce->ce_namelen != base->ce_namelen ||
304 strcmp(ce->name, base->name)) {
305 ce->index = 0;
306 continue;
309 * This is the copy of a cache entry that is present
310 * in the shared index, created by unpack_trees()
311 * while it constructed a new index.
313 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
315 * Already marked for inclusion in the split
316 * index, either because the corresponding
317 * file was modified and the cached stat data
318 * was refreshed, or because the original
319 * entry already had a replacement entry in
320 * the split index.
321 * Nothing to do.
323 } else if (!ce_uptodate(ce) &&
324 is_racy_timestamp(istate, ce)) {
326 * A copy of a racily clean cache entry from
327 * the shared index. It must be added to
328 * the split index, so the subsequent
329 * do_write_index() can smudge its stat data.
331 ce->ce_flags |= CE_UPDATE_IN_BASE;
332 } else {
334 * Thoroughly compare the cached data to see
335 * whether it should be marked for inclusion
336 * in the split index.
338 * This comparison might be unnecessary, as
339 * code paths modifying the cached data do
340 * set CE_UPDATE_IN_BASE as well.
342 if (compare_ce_content(ce, base))
343 ce->ce_flags |= CE_UPDATE_IN_BASE;
345 discard_cache_entry(base);
346 si->base->cache[ce->index - 1] = ce;
348 for (i = 0; i < si->base->cache_nr; i++) {
349 ce = si->base->cache[i];
350 if ((ce->ce_flags & CE_REMOVE) ||
351 !(ce->ce_flags & CE_MATCHED))
352 ewah_set(si->delete_bitmap, i);
353 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
354 ewah_set(si->replace_bitmap, i);
355 ce->ce_flags |= CE_STRIP_NAME;
356 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
357 entries[nr_entries++] = ce;
359 if (is_null_oid(&ce->oid))
360 istate->drop_cache_tree = 1;
364 for (i = 0; i < istate->cache_nr; i++) {
365 ce = istate->cache[i];
366 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
367 assert(!(ce->ce_flags & CE_STRIP_NAME));
368 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
369 entries[nr_entries++] = ce;
371 ce->ce_flags &= ~CE_MATCHED;
375 * take cache[] out temporarily, put entries[] in its place
376 * for writing
378 si->saved_cache = istate->cache;
379 si->saved_cache_nr = istate->cache_nr;
380 istate->cache = entries;
381 istate->cache_nr = nr_entries;
384 void finish_writing_split_index(struct index_state *istate)
386 struct split_index *si = init_split_index(istate);
388 ewah_free(si->delete_bitmap);
389 ewah_free(si->replace_bitmap);
390 si->delete_bitmap = NULL;
391 si->replace_bitmap = NULL;
392 free(istate->cache);
393 istate->cache = si->saved_cache;
394 istate->cache_nr = si->saved_cache_nr;
397 void discard_split_index(struct index_state *istate)
399 struct split_index *si = istate->split_index;
400 if (!si)
401 return;
402 istate->split_index = NULL;
403 si->refcount--;
404 if (si->refcount)
405 return;
406 if (si->base) {
407 discard_index(si->base);
408 free(si->base);
410 free(si);
413 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
415 if (ce->index &&
416 istate->split_index &&
417 istate->split_index->base &&
418 ce->index <= istate->split_index->base->cache_nr &&
419 ce == istate->split_index->base->cache[ce->index - 1])
420 ce->ce_flags |= CE_REMOVE;
421 else
422 discard_cache_entry(ce);
425 void replace_index_entry_in_base(struct index_state *istate,
426 struct cache_entry *old_entry,
427 struct cache_entry *new_entry)
429 if (old_entry->index &&
430 istate->split_index &&
431 istate->split_index->base &&
432 old_entry->index <= istate->split_index->base->cache_nr) {
433 new_entry->index = old_entry->index;
434 if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
435 discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
436 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
440 void add_split_index(struct index_state *istate)
442 if (!istate->split_index) {
443 init_split_index(istate);
444 istate->cache_changed |= SPLIT_INDEX_ORDERED;
448 void remove_split_index(struct index_state *istate)
450 if (istate->split_index) {
451 if (istate->split_index->base) {
453 * When removing the split index, we need to move
454 * ownership of the mem_pool associated with the
455 * base index to the main index. There may be cache entries
456 * allocated from the base's memory pool that are shared with
457 * the_index.cache[].
459 mem_pool_combine(istate->ce_mem_pool,
460 istate->split_index->base->ce_mem_pool);
463 * The split index no longer owns the mem_pool backing
464 * its cache array. As we are discarding this index,
465 * mark the index as having no cache entries, so it
466 * will not attempt to clean up the cache entries or
467 * validate them.
469 istate->split_index->base->cache_nr = 0;
473 * We can discard the split index because its
474 * memory pool has been incorporated into the
475 * memory pool associated with the the_index.
477 discard_split_index(istate);
479 istate->cache_changed |= SOMETHING_CHANGED;