pager.h: move declarations for pager.c functions from cache.h
[alt-git.git] / split-index.c
blob5602b74994b0cb84c109ecc04dbe2e692c064eaa
1 #include "cache.h"
2 #include "alloc.h"
3 #include "gettext.h"
4 #include "mem-pool.h"
5 #include "split-index.h"
6 #include "ewah/ewok.h"
8 struct split_index *init_split_index(struct index_state *istate)
10 if (!istate->split_index) {
11 if (istate->sparse_index)
12 die(_("cannot use split index with a sparse index"));
14 CALLOC_ARRAY(istate->split_index, 1);
15 istate->split_index->refcount = 1;
17 return istate->split_index;
20 int read_link_extension(struct index_state *istate,
21 const void *data_, unsigned long sz)
23 const unsigned char *data = data_;
24 struct split_index *si;
25 int ret;
27 if (sz < the_hash_algo->rawsz)
28 return error("corrupt link extension (too short)");
29 si = init_split_index(istate);
30 oidread(&si->base_oid, data);
31 data += the_hash_algo->rawsz;
32 sz -= the_hash_algo->rawsz;
33 if (!sz)
34 return 0;
35 si->delete_bitmap = ewah_new();
36 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
37 if (ret < 0)
38 return error("corrupt delete bitmap in link extension");
39 data += ret;
40 sz -= ret;
41 si->replace_bitmap = ewah_new();
42 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
43 if (ret < 0)
44 return error("corrupt replace bitmap in link extension");
45 if (ret != sz)
46 return error("garbage at the end of link extension");
47 return 0;
50 int write_link_extension(struct strbuf *sb,
51 struct index_state *istate)
53 struct split_index *si = istate->split_index;
54 strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
55 if (!si->delete_bitmap && !si->replace_bitmap)
56 return 0;
57 ewah_serialize_strbuf(si->delete_bitmap, sb);
58 ewah_serialize_strbuf(si->replace_bitmap, sb);
59 return 0;
62 static void mark_base_index_entries(struct index_state *base)
64 int i;
66 * To keep track of the shared entries between
67 * istate->base->cache[] and istate->cache[], base entry
68 * position is stored in each base entry. All positions start
69 * from 1 instead of 0, which is reserved to say "this is a new
70 * entry".
72 for (i = 0; i < base->cache_nr; i++)
73 base->cache[i]->index = i + 1;
76 void move_cache_to_base_index(struct index_state *istate)
78 struct split_index *si = istate->split_index;
79 int i;
82 * If there was a previous base index, then transfer ownership of allocated
83 * entries to the parent index.
85 if (si->base &&
86 si->base->ce_mem_pool) {
88 if (!istate->ce_mem_pool) {
89 istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
90 mem_pool_init(istate->ce_mem_pool, 0);
93 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
96 ALLOC_ARRAY(si->base, 1);
97 index_state_init(si->base, istate->repo);
98 si->base->version = istate->version;
99 /* zero timestamp disables racy test in ce_write_index() */
100 si->base->timestamp = istate->timestamp;
101 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
102 si->base->cache_nr = istate->cache_nr;
105 * The mem_pool needs to move with the allocated entries.
107 si->base->ce_mem_pool = istate->ce_mem_pool;
108 istate->ce_mem_pool = NULL;
110 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
111 mark_base_index_entries(si->base);
112 for (i = 0; i < si->base->cache_nr; i++)
113 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
116 static void mark_entry_for_delete(size_t pos, void *data)
118 struct index_state *istate = data;
119 if (pos >= istate->cache_nr)
120 die("position for delete %d exceeds base index size %d",
121 (int)pos, istate->cache_nr);
122 istate->cache[pos]->ce_flags |= CE_REMOVE;
123 istate->split_index->nr_deletions++;
126 static void replace_entry(size_t pos, void *data)
128 struct index_state *istate = data;
129 struct split_index *si = istate->split_index;
130 struct cache_entry *dst, *src;
132 if (pos >= istate->cache_nr)
133 die("position for replacement %d exceeds base index size %d",
134 (int)pos, istate->cache_nr);
135 if (si->nr_replacements >= si->saved_cache_nr)
136 die("too many replacements (%d vs %d)",
137 si->nr_replacements, si->saved_cache_nr);
138 dst = istate->cache[pos];
139 if (dst->ce_flags & CE_REMOVE)
140 die("entry %d is marked as both replaced and deleted",
141 (int)pos);
142 src = si->saved_cache[si->nr_replacements];
143 if (ce_namelen(src))
144 die("corrupt link extension, entry %d should have "
145 "zero length name", (int)pos);
146 src->index = pos + 1;
147 src->ce_flags |= CE_UPDATE_IN_BASE;
148 src->ce_namelen = dst->ce_namelen;
149 copy_cache_entry(dst, src);
150 discard_cache_entry(src);
151 si->nr_replacements++;
154 void merge_base_index(struct index_state *istate)
156 struct split_index *si = istate->split_index;
157 unsigned int i;
159 mark_base_index_entries(si->base);
161 si->saved_cache = istate->cache;
162 si->saved_cache_nr = istate->cache_nr;
163 istate->cache_nr = si->base->cache_nr;
164 istate->cache = NULL;
165 istate->cache_alloc = 0;
166 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
167 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
169 si->nr_deletions = 0;
170 si->nr_replacements = 0;
171 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
172 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
173 if (si->nr_deletions)
174 remove_marked_cache_entries(istate, 0);
176 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
177 if (!ce_namelen(si->saved_cache[i]))
178 die("corrupt link extension, entry %d should "
179 "have non-zero length name", i);
180 add_index_entry(istate, si->saved_cache[i],
181 ADD_CACHE_OK_TO_ADD |
182 ADD_CACHE_KEEP_CACHE_TREE |
184 * we may have to replay what
185 * merge-recursive.c:update_stages()
186 * does, which has this flag on
188 ADD_CACHE_SKIP_DFCHECK);
189 si->saved_cache[i] = NULL;
192 ewah_free(si->delete_bitmap);
193 ewah_free(si->replace_bitmap);
194 FREE_AND_NULL(si->saved_cache);
195 si->delete_bitmap = NULL;
196 si->replace_bitmap = NULL;
197 si->saved_cache_nr = 0;
201 * Compare most of the fields in two cache entries, i.e. all except the
202 * hashmap_entry and the name.
204 static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
206 const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
207 CE_EXTENDED_FLAGS;
208 unsigned int ce_flags = a->ce_flags;
209 unsigned int base_flags = b->ce_flags;
210 int ret;
212 /* only on-disk flags matter */
213 a->ce_flags &= ondisk_flags;
214 b->ce_flags &= ondisk_flags;
215 ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
216 offsetof(struct cache_entry, name) -
217 offsetof(struct cache_entry, oid)) ||
218 !oideq(&a->oid, &b->oid);
219 a->ce_flags = ce_flags;
220 b->ce_flags = base_flags;
222 return ret;
225 void prepare_to_write_split_index(struct index_state *istate)
227 struct split_index *si = init_split_index(istate);
228 struct cache_entry **entries = NULL, *ce;
229 int i, nr_entries = 0, nr_alloc = 0;
231 si->delete_bitmap = ewah_new();
232 si->replace_bitmap = ewah_new();
234 if (si->base) {
235 /* Go through istate->cache[] and mark CE_MATCHED to
236 * entry with positive index. We'll go through
237 * base->cache[] later to delete all entries in base
238 * that are not marked with either CE_MATCHED or
239 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
240 * duplicate, deduplicate it.
242 for (i = 0; i < istate->cache_nr; i++) {
243 struct cache_entry *base;
244 ce = istate->cache[i];
245 if (!ce->index) {
247 * During simple update index operations this
248 * is a cache entry that is not present in
249 * the shared index. It will be added to the
250 * split index.
252 * However, it might also represent a file
253 * that already has a cache entry in the
254 * shared index, but a new index has just
255 * been constructed by unpack_trees(), and
256 * this entry now refers to different content
257 * than what was recorded in the original
258 * index, e.g. during 'read-tree -m HEAD^' or
259 * 'checkout HEAD^'. In this case the
260 * original entry in the shared index will be
261 * marked as deleted, and this entry will be
262 * added to the split index.
264 continue;
266 if (ce->index > si->base->cache_nr) {
267 BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
268 ce->index, si->base->cache_nr);
270 ce->ce_flags |= CE_MATCHED; /* or "shared" */
271 base = si->base->cache[ce->index - 1];
272 if (ce == base) {
273 /* The entry is present in the shared index. */
274 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
276 * Already marked for inclusion in
277 * the split index, either because
278 * the corresponding file was
279 * modified and the cached stat data
280 * was refreshed, or because there
281 * is already a replacement entry in
282 * the split index.
283 * Nothing more to do here.
285 } else if (!ce_uptodate(ce) &&
286 is_racy_timestamp(istate, ce)) {
288 * A racily clean cache entry stored
289 * only in the shared index: it must
290 * be added to the split index, so
291 * the subsequent do_write_index()
292 * can smudge its stat data.
294 ce->ce_flags |= CE_UPDATE_IN_BASE;
295 } else {
297 * The entry is only present in the
298 * shared index and it was not
299 * refreshed.
300 * Just leave it there.
303 continue;
305 if (ce->ce_namelen != base->ce_namelen ||
306 strcmp(ce->name, base->name)) {
307 ce->index = 0;
308 continue;
311 * This is the copy of a cache entry that is present
312 * in the shared index, created by unpack_trees()
313 * while it constructed a new index.
315 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
317 * Already marked for inclusion in the split
318 * index, either because the corresponding
319 * file was modified and the cached stat data
320 * was refreshed, or because the original
321 * entry already had a replacement entry in
322 * the split index.
323 * Nothing to do.
325 } else if (!ce_uptodate(ce) &&
326 is_racy_timestamp(istate, ce)) {
328 * A copy of a racily clean cache entry from
329 * the shared index. It must be added to
330 * the split index, so the subsequent
331 * do_write_index() can smudge its stat data.
333 ce->ce_flags |= CE_UPDATE_IN_BASE;
334 } else {
336 * Thoroughly compare the cached data to see
337 * whether it should be marked for inclusion
338 * in the split index.
340 * This comparison might be unnecessary, as
341 * code paths modifying the cached data do
342 * set CE_UPDATE_IN_BASE as well.
344 if (compare_ce_content(ce, base))
345 ce->ce_flags |= CE_UPDATE_IN_BASE;
347 discard_cache_entry(base);
348 si->base->cache[ce->index - 1] = ce;
350 for (i = 0; i < si->base->cache_nr; i++) {
351 ce = si->base->cache[i];
352 if ((ce->ce_flags & CE_REMOVE) ||
353 !(ce->ce_flags & CE_MATCHED))
354 ewah_set(si->delete_bitmap, i);
355 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
356 ewah_set(si->replace_bitmap, i);
357 ce->ce_flags |= CE_STRIP_NAME;
358 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
359 entries[nr_entries++] = ce;
361 if (is_null_oid(&ce->oid))
362 istate->drop_cache_tree = 1;
366 for (i = 0; i < istate->cache_nr; i++) {
367 ce = istate->cache[i];
368 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
369 assert(!(ce->ce_flags & CE_STRIP_NAME));
370 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
371 entries[nr_entries++] = ce;
373 ce->ce_flags &= ~CE_MATCHED;
377 * take cache[] out temporarily, put entries[] in its place
378 * for writing
380 si->saved_cache = istate->cache;
381 si->saved_cache_nr = istate->cache_nr;
382 istate->cache = entries;
383 istate->cache_nr = nr_entries;
386 void finish_writing_split_index(struct index_state *istate)
388 struct split_index *si = init_split_index(istate);
390 ewah_free(si->delete_bitmap);
391 ewah_free(si->replace_bitmap);
392 si->delete_bitmap = NULL;
393 si->replace_bitmap = NULL;
394 free(istate->cache);
395 istate->cache = si->saved_cache;
396 istate->cache_nr = si->saved_cache_nr;
399 void discard_split_index(struct index_state *istate)
401 struct split_index *si = istate->split_index;
402 if (!si)
403 return;
404 istate->split_index = NULL;
405 si->refcount--;
406 if (si->refcount)
407 return;
408 if (si->base) {
409 discard_index(si->base);
410 free(si->base);
412 free(si);
415 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
417 if (ce->index &&
418 istate->split_index &&
419 istate->split_index->base &&
420 ce->index <= istate->split_index->base->cache_nr &&
421 ce == istate->split_index->base->cache[ce->index - 1])
422 ce->ce_flags |= CE_REMOVE;
423 else
424 discard_cache_entry(ce);
427 void replace_index_entry_in_base(struct index_state *istate,
428 struct cache_entry *old_entry,
429 struct cache_entry *new_entry)
431 if (old_entry->index &&
432 istate->split_index &&
433 istate->split_index->base &&
434 old_entry->index <= istate->split_index->base->cache_nr) {
435 new_entry->index = old_entry->index;
436 if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
437 discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
438 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
442 void add_split_index(struct index_state *istate)
444 if (!istate->split_index) {
445 init_split_index(istate);
446 istate->cache_changed |= SPLIT_INDEX_ORDERED;
450 void remove_split_index(struct index_state *istate)
452 if (istate->split_index) {
453 if (istate->split_index->base) {
455 * When removing the split index, we need to move
456 * ownership of the mem_pool associated with the
457 * base index to the main index. There may be cache entries
458 * allocated from the base's memory pool that are shared with
459 * the_index.cache[].
461 mem_pool_combine(istate->ce_mem_pool,
462 istate->split_index->base->ce_mem_pool);
465 * The split index no longer owns the mem_pool backing
466 * its cache array. As we are discarding this index,
467 * mark the index as having no cache entries, so it
468 * will not attempt to clean up the cache entries or
469 * validate them.
471 istate->split_index->base->cache_nr = 0;
475 * We can discard the split index because its
476 * memory pool has been incorporated into the
477 * memory pool associated with the the_index.
479 discard_split_index(istate);
481 istate->cache_changed |= SOMETHING_CHANGED;