]> git.ipfire.org Git - thirdparty/git.git/blame - split-index.c
Merge branch 'en/header-split-cache-h-part-3'
[thirdparty/git.git] / split-index.c
CommitLineData
bc5c5ec0 1#include "git-compat-util.h"
36bf1958 2#include "alloc.h"
f394e093 3#include "gettext.h"
d1cbe1e6 4#include "hash.h"
5bc07225 5#include "mem-pool.h"
08c46a49 6#include "read-cache-ll.h"
5fc2fc8f 7#include "split-index.h"
69a63fe6 8#include "strbuf.h"
96a1d8d3 9#include "ewah/ewok.h"
5fc2fc8f
NTND
10
11struct split_index *init_split_index(struct index_state *istate)
12{
13 if (!istate->split_index) {
451b66c5
JS
14 if (istate->sparse_index)
15 die(_("cannot use split index with a sparse index"));
16
ca56dadb 17 CALLOC_ARRAY(istate->split_index, 1);
5fc2fc8f
NTND
18 istate->split_index->refcount = 1;
19 }
20 return istate->split_index;
21}
22
23int read_link_extension(struct index_state *istate,
24 const void *data_, unsigned long sz)
25{
26 const unsigned char *data = data_;
27 struct split_index *si;
76b07b37
NTND
28 int ret;
29
2182abd9 30 if (sz < the_hash_algo->rawsz)
5fc2fc8f
NTND
31 return error("corrupt link extension (too short)");
32 si = init_split_index(istate);
92e2cab9 33 oidread(&si->base_oid, data);
2182abd9 34 data += the_hash_algo->rawsz;
35 sz -= the_hash_algo->rawsz;
76b07b37
NTND
36 if (!sz)
37 return 0;
38 si->delete_bitmap = ewah_new();
39 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
40 if (ret < 0)
41 return error("corrupt delete bitmap in link extension");
42 data += ret;
43 sz -= ret;
44 si->replace_bitmap = ewah_new();
45 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
46 if (ret < 0)
47 return error("corrupt replace bitmap in link extension");
48 if (ret != sz)
5fc2fc8f
NTND
49 return error("garbage at the end of link extension");
50 return 0;
51}
52
53int write_link_extension(struct strbuf *sb,
54 struct index_state *istate)
55{
56 struct split_index *si = istate->split_index;
2182abd9 57 strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
96a1d8d3
NTND
58 if (!si->delete_bitmap && !si->replace_bitmap)
59 return 0;
be0d9d53
NTND
60 ewah_serialize_strbuf(si->delete_bitmap, sb);
61 ewah_serialize_strbuf(si->replace_bitmap, sb);
5fc2fc8f
NTND
62 return 0;
63}
64
65static void mark_base_index_entries(struct index_state *base)
66{
67 int i;
68 /*
69 * To keep track of the shared entries between
70 * istate->base->cache[] and istate->cache[], base entry
71 * position is stored in each base entry. All positions start
832c0e5e 72 * from 1 instead of 0, which is reserved to say "this is a new
5fc2fc8f
NTND
73 * entry".
74 */
75 for (i = 0; i < base->cache_nr; i++)
76 base->cache[i]->index = i + 1;
77}
78
c18b80a0
NTND
79void move_cache_to_base_index(struct index_state *istate)
80{
81 struct split_index *si = istate->split_index;
82 int i;
83
84 /*
8e72d675
JM
85 * If there was a previous base index, then transfer ownership of allocated
86 * entries to the parent index.
c18b80a0 87 */
8e72d675
JM
88 if (si->base &&
89 si->base->ce_mem_pool) {
90
44c7e1a7
EN
91 if (!istate->ce_mem_pool) {
92 istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
93 mem_pool_init(istate->ce_mem_pool, 0);
94 }
8e72d675
JM
95
96 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
97 }
98
2f6b1eb7 99 ALLOC_ARRAY(si->base, 1);
6269f8ea 100 index_state_init(si->base, istate->repo);
c18b80a0
NTND
101 si->base->version = istate->version;
102 /* zero timestamp disables racy test in ce_write_index() */
103 si->base->timestamp = istate->timestamp;
104 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
105 si->base->cache_nr = istate->cache_nr;
8e72d675
JM
106
107 /*
108 * The mem_pool needs to move with the allocated entries.
109 */
110 si->base->ce_mem_pool = istate->ce_mem_pool;
111 istate->ce_mem_pool = NULL;
112
45ccef87 113 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
c18b80a0
NTND
114 mark_base_index_entries(si->base);
115 for (i = 0; i < si->base->cache_nr; i++)
116 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
117}
118
76b07b37
NTND
119static void mark_entry_for_delete(size_t pos, void *data)
120{
121 struct index_state *istate = data;
122 if (pos >= istate->cache_nr)
123 die("position for delete %d exceeds base index size %d",
124 (int)pos, istate->cache_nr);
125 istate->cache[pos]->ce_flags |= CE_REMOVE;
2034e848 126 istate->split_index->nr_deletions++;
76b07b37
NTND
127}
128
129static void replace_entry(size_t pos, void *data)
130{
131 struct index_state *istate = data;
132 struct split_index *si = istate->split_index;
133 struct cache_entry *dst, *src;
b3c96fb1 134
76b07b37
NTND
135 if (pos >= istate->cache_nr)
136 die("position for replacement %d exceeds base index size %d",
137 (int)pos, istate->cache_nr);
138 if (si->nr_replacements >= si->saved_cache_nr)
139 die("too many replacements (%d vs %d)",
140 si->nr_replacements, si->saved_cache_nr);
141 dst = istate->cache[pos];
142 if (dst->ce_flags & CE_REMOVE)
143 die("entry %d is marked as both replaced and deleted",
144 (int)pos);
145 src = si->saved_cache[si->nr_replacements];
b3c96fb1
NTND
146 if (ce_namelen(src))
147 die("corrupt link extension, entry %d should have "
148 "zero length name", (int)pos);
76b07b37
NTND
149 src->index = pos + 1;
150 src->ce_flags |= CE_UPDATE_IN_BASE;
b3c96fb1
NTND
151 src->ce_namelen = dst->ce_namelen;
152 copy_cache_entry(dst, src);
a849735b 153 discard_cache_entry(src);
76b07b37
NTND
154 si->nr_replacements++;
155}
156
5fc2fc8f
NTND
157void merge_base_index(struct index_state *istate)
158{
159 struct split_index *si = istate->split_index;
76b07b37 160 unsigned int i;
5fc2fc8f
NTND
161
162 mark_base_index_entries(si->base);
76b07b37
NTND
163
164 si->saved_cache = istate->cache;
165 si->saved_cache_nr = istate->cache_nr;
166 istate->cache_nr = si->base->cache_nr;
167 istate->cache = NULL;
168 istate->cache_alloc = 0;
5fc2fc8f 169 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
45ccef87 170 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
76b07b37
NTND
171
172 si->nr_deletions = 0;
173 si->nr_replacements = 0;
174 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
175 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
176 if (si->nr_deletions)
6fdc2057 177 remove_marked_cache_entries(istate, 0);
76b07b37
NTND
178
179 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
b3c96fb1
NTND
180 if (!ce_namelen(si->saved_cache[i]))
181 die("corrupt link extension, entry %d should "
182 "have non-zero length name", i);
76b07b37
NTND
183 add_index_entry(istate, si->saved_cache[i],
184 ADD_CACHE_OK_TO_ADD |
ce7c614b 185 ADD_CACHE_KEEP_CACHE_TREE |
76b07b37
NTND
186 /*
187 * we may have to replay what
188 * merge-recursive.c:update_stages()
189 * does, which has this flag on
190 */
191 ADD_CACHE_SKIP_DFCHECK);
192 si->saved_cache[i] = NULL;
193 }
194
195 ewah_free(si->delete_bitmap);
196 ewah_free(si->replace_bitmap);
88ce3ef6 197 FREE_AND_NULL(si->saved_cache);
76b07b37
NTND
198 si->delete_bitmap = NULL;
199 si->replace_bitmap = NULL;
76b07b37 200 si->saved_cache_nr = 0;
5fc2fc8f
NTND
201}
202
e3d83798
SG
203/*
204 * Compare most of the fields in two cache entries, i.e. all except the
205 * hashmap_entry and the name.
206 */
207static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
208{
209 const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
210 CE_EXTENDED_FLAGS;
211 unsigned int ce_flags = a->ce_flags;
212 unsigned int base_flags = b->ce_flags;
213 int ret;
214
215 /* only on-disk flags matter */
216 a->ce_flags &= ondisk_flags;
217 b->ce_flags &= ondisk_flags;
218 ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
219 offsetof(struct cache_entry, name) -
09751bf1
AH
220 offsetof(struct cache_entry, oid)) ||
221 !oideq(&a->oid, &b->oid);
e3d83798
SG
222 a->ce_flags = ce_flags;
223 b->ce_flags = base_flags;
224
225 return ret;
226}
227
5fc2fc8f
NTND
228void prepare_to_write_split_index(struct index_state *istate)
229{
230 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
231 struct cache_entry **entries = NULL, *ce;
232 int i, nr_entries = 0, nr_alloc = 0;
233
234 si->delete_bitmap = ewah_new();
235 si->replace_bitmap = ewah_new();
236
237 if (si->base) {
238 /* Go through istate->cache[] and mark CE_MATCHED to
239 * entry with positive index. We'll go through
240 * base->cache[] later to delete all entries in base
753c4515 241 * that are not marked with either CE_MATCHED or
96a1d8d3
NTND
242 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
243 * duplicate, deduplicate it.
244 */
245 for (i = 0; i < istate->cache_nr; i++) {
246 struct cache_entry *base;
96a1d8d3 247 ce = istate->cache[i];
e3d83798
SG
248 if (!ce->index) {
249 /*
250 * During simple update index operations this
251 * is a cache entry that is not present in
252 * the shared index. It will be added to the
253 * split index.
254 *
255 * However, it might also represent a file
256 * that already has a cache entry in the
257 * shared index, but a new index has just
258 * been constructed by unpack_trees(), and
259 * this entry now refers to different content
260 * than what was recorded in the original
261 * index, e.g. during 'read-tree -m HEAD^' or
262 * 'checkout HEAD^'. In this case the
263 * original entry in the shared index will be
264 * marked as deleted, and this entry will be
265 * added to the split index.
266 */
96a1d8d3 267 continue;
e3d83798 268 }
96a1d8d3 269 if (ce->index > si->base->cache_nr) {
4c490f3d
SG
270 BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
271 ce->index, si->base->cache_nr);
96a1d8d3
NTND
272 }
273 ce->ce_flags |= CE_MATCHED; /* or "shared" */
274 base = si->base->cache[ce->index - 1];
5581a019
SG
275 if (ce == base) {
276 /* The entry is present in the shared index. */
277 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
278 /*
279 * Already marked for inclusion in
280 * the split index, either because
281 * the corresponding file was
282 * modified and the cached stat data
283 * was refreshed, or because there
284 * is already a replacement entry in
285 * the split index.
286 * Nothing more to do here.
287 */
288 } else if (!ce_uptodate(ce) &&
289 is_racy_timestamp(istate, ce)) {
290 /*
291 * A racily clean cache entry stored
292 * only in the shared index: it must
293 * be added to the split index, so
294 * the subsequent do_write_index()
295 * can smudge its stat data.
296 */
297 ce->ce_flags |= CE_UPDATE_IN_BASE;
298 } else {
299 /*
300 * The entry is only present in the
301 * shared index and it was not
302 * refreshed.
303 * Just leave it there.
304 */
305 }
96a1d8d3 306 continue;
5581a019 307 }
96a1d8d3
NTND
308 if (ce->ce_namelen != base->ce_namelen ||
309 strcmp(ce->name, base->name)) {
310 ce->index = 0;
311 continue;
312 }
e3d83798
SG
313 /*
314 * This is the copy of a cache entry that is present
315 * in the shared index, created by unpack_trees()
316 * while it constructed a new index.
317 */
318 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
319 /*
320 * Already marked for inclusion in the split
321 * index, either because the corresponding
322 * file was modified and the cached stat data
323 * was refreshed, or because the original
324 * entry already had a replacement entry in
325 * the split index.
326 * Nothing to do.
327 */
5581a019
SG
328 } else if (!ce_uptodate(ce) &&
329 is_racy_timestamp(istate, ce)) {
330 /*
331 * A copy of a racily clean cache entry from
332 * the shared index. It must be added to
333 * the split index, so the subsequent
334 * do_write_index() can smudge its stat data.
335 */
336 ce->ce_flags |= CE_UPDATE_IN_BASE;
e3d83798
SG
337 } else {
338 /*
339 * Thoroughly compare the cached data to see
340 * whether it should be marked for inclusion
341 * in the split index.
342 *
343 * This comparison might be unnecessary, as
344 * code paths modifying the cached data do
345 * set CE_UPDATE_IN_BASE as well.
346 */
347 if (compare_ce_content(ce, base))
348 ce->ce_flags |= CE_UPDATE_IN_BASE;
349 }
a849735b 350 discard_cache_entry(base);
96a1d8d3
NTND
351 si->base->cache[ce->index - 1] = ce;
352 }
353 for (i = 0; i < si->base->cache_nr; i++) {
354 ce = si->base->cache[i];
355 if ((ce->ce_flags & CE_REMOVE) ||
356 !(ce->ce_flags & CE_MATCHED))
357 ewah_set(si->delete_bitmap, i);
358 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
359 ewah_set(si->replace_bitmap, i);
b3c96fb1 360 ce->ce_flags |= CE_STRIP_NAME;
96a1d8d3
NTND
361 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
362 entries[nr_entries++] = ce;
363 }
4bddd983
TG
364 if (is_null_oid(&ce->oid))
365 istate->drop_cache_tree = 1;
96a1d8d3
NTND
366 }
367 }
368
369 for (i = 0; i < istate->cache_nr; i++) {
370 ce = istate->cache[i];
371 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
b3c96fb1 372 assert(!(ce->ce_flags & CE_STRIP_NAME));
96a1d8d3
NTND
373 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
374 entries[nr_entries++] = ce;
375 }
376 ce->ce_flags &= ~CE_MATCHED;
377 }
378
379 /*
380 * take cache[] out temporarily, put entries[] in its place
381 * for writing
382 */
383 si->saved_cache = istate->cache;
5fc2fc8f 384 si->saved_cache_nr = istate->cache_nr;
96a1d8d3
NTND
385 istate->cache = entries;
386 istate->cache_nr = nr_entries;
5fc2fc8f
NTND
387}
388
389void finish_writing_split_index(struct index_state *istate)
390{
391 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
392
393 ewah_free(si->delete_bitmap);
394 ewah_free(si->replace_bitmap);
395 si->delete_bitmap = NULL;
396 si->replace_bitmap = NULL;
397 free(istate->cache);
398 istate->cache = si->saved_cache;
5fc2fc8f
NTND
399 istate->cache_nr = si->saved_cache_nr;
400}
401
402void discard_split_index(struct index_state *istate)
403{
404 struct split_index *si = istate->split_index;
405 if (!si)
406 return;
407 istate->split_index = NULL;
408 si->refcount--;
409 if (si->refcount)
410 return;
411 if (si->base) {
412 discard_index(si->base);
413 free(si->base);
414 }
415 free(si);
416}
045113a5
NTND
417
418void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
419{
420 if (ce->index &&
421 istate->split_index &&
422 istate->split_index->base &&
423 ce->index <= istate->split_index->base->cache_nr &&
424 ce == istate->split_index->base->cache[ce->index - 1])
425 ce->ce_flags |= CE_REMOVE;
426 else
a849735b 427 discard_cache_entry(ce);
045113a5 428}
078a58e8
NTND
429
430void replace_index_entry_in_base(struct index_state *istate,
75b7b971
BW
431 struct cache_entry *old_entry,
432 struct cache_entry *new_entry)
078a58e8 433{
75b7b971 434 if (old_entry->index &&
078a58e8
NTND
435 istate->split_index &&
436 istate->split_index->base &&
75b7b971
BW
437 old_entry->index <= istate->split_index->base->cache_nr) {
438 new_entry->index = old_entry->index;
439 if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
a849735b 440 discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
75b7b971 441 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
078a58e8
NTND
442 }
443}
cef4fc7e
CC
444
445void add_split_index(struct index_state *istate)
446{
447 if (!istate->split_index) {
448 init_split_index(istate);
449 istate->cache_changed |= SPLIT_INDEX_ORDERED;
450 }
451}
452
453void remove_split_index(struct index_state *istate)
454{
64719b11 455 if (istate->split_index) {
6e37c8ed
NTND
456 if (istate->split_index->base) {
457 /*
458 * When removing the split index, we need to move
459 * ownership of the mem_pool associated with the
460 * base index to the main index. There may be cache entries
461 * allocated from the base's memory pool that are shared with
462 * the_index.cache[].
463 */
464 mem_pool_combine(istate->ce_mem_pool,
465 istate->split_index->base->ce_mem_pool);
8e72d675 466
6e37c8ed
NTND
467 /*
468 * The split index no longer owns the mem_pool backing
469 * its cache array. As we are discarding this index,
470 * mark the index as having no cache entries, so it
471 * will not attempt to clean up the cache entries or
472 * validate them.
473 */
8e72d675 474 istate->split_index->base->cache_nr = 0;
6e37c8ed 475 }
8e72d675
JM
476
477 /*
478 * We can discard the split index because its
479 * memory pool has been incorporated into the
480 * memory pool associated with the the_index.
481 */
482 discard_split_index(istate);
483
64719b11
JH
484 istate->cache_changed |= SOMETHING_CHANGED;
485 }
cef4fc7e 486}