2 #include "split-index.h"
5 struct split_index
*init_split_index(struct index_state
*istate
)
7 if (!istate
->split_index
) {
8 istate
->split_index
= xcalloc(1, sizeof(*istate
->split_index
));
9 istate
->split_index
->refcount
= 1;
11 return istate
->split_index
;
14 int read_link_extension(struct index_state
*istate
,
15 const void *data_
, unsigned long sz
)
17 const unsigned char *data
= data_
;
18 struct split_index
*si
;
21 if (sz
< the_hash_algo
->rawsz
)
22 return error("corrupt link extension (too short)");
23 si
= init_split_index(istate
);
24 hashcpy(si
->base_oid
.hash
, data
);
25 data
+= the_hash_algo
->rawsz
;
26 sz
-= the_hash_algo
->rawsz
;
29 si
->delete_bitmap
= ewah_new();
30 ret
= ewah_read_mmap(si
->delete_bitmap
, data
, sz
);
32 return error("corrupt delete bitmap in link extension");
35 si
->replace_bitmap
= ewah_new();
36 ret
= ewah_read_mmap(si
->replace_bitmap
, data
, sz
);
38 return error("corrupt replace bitmap in link extension");
40 return error("garbage at the end of link extension");
44 int write_link_extension(struct strbuf
*sb
,
45 struct index_state
*istate
)
47 struct split_index
*si
= istate
->split_index
;
48 strbuf_add(sb
, si
->base_oid
.hash
, the_hash_algo
->rawsz
);
49 if (!si
->delete_bitmap
&& !si
->replace_bitmap
)
51 ewah_serialize_strbuf(si
->delete_bitmap
, sb
);
52 ewah_serialize_strbuf(si
->replace_bitmap
, sb
);
56 static void mark_base_index_entries(struct index_state
*base
)
60 * To keep track of the shared entries between
61 * istate->base->cache[] and istate->cache[], base entry
62 * position is stored in each base entry. All positions start
63 * from 1 instead of 0, which is reserved to say "this is a new
66 for (i
= 0; i
< base
->cache_nr
; i
++)
67 base
->cache
[i
]->index
= i
+ 1;
70 void move_cache_to_base_index(struct index_state
*istate
)
72 struct split_index
*si
= istate
->split_index
;
76 * If there was a previous base index, then transfer ownership of allocated
77 * entries to the parent index.
80 si
->base
->ce_mem_pool
) {
82 if (!istate
->ce_mem_pool
)
83 mem_pool_init(&istate
->ce_mem_pool
, 0);
85 mem_pool_combine(istate
->ce_mem_pool
, istate
->split_index
->base
->ce_mem_pool
);
88 si
->base
= xcalloc(1, sizeof(*si
->base
));
89 si
->base
->version
= istate
->version
;
90 /* zero timestamp disables racy test in ce_write_index() */
91 si
->base
->timestamp
= istate
->timestamp
;
92 ALLOC_GROW(si
->base
->cache
, istate
->cache_nr
, si
->base
->cache_alloc
);
93 si
->base
->cache_nr
= istate
->cache_nr
;
96 * The mem_pool needs to move with the allocated entries.
98 si
->base
->ce_mem_pool
= istate
->ce_mem_pool
;
99 istate
->ce_mem_pool
= NULL
;
101 COPY_ARRAY(si
->base
->cache
, istate
->cache
, istate
->cache_nr
);
102 mark_base_index_entries(si
->base
);
103 for (i
= 0; i
< si
->base
->cache_nr
; i
++)
104 si
->base
->cache
[i
]->ce_flags
&= ~CE_UPDATE_IN_BASE
;
107 static void mark_entry_for_delete(size_t pos
, void *data
)
109 struct index_state
*istate
= data
;
110 if (pos
>= istate
->cache_nr
)
111 die("position for delete %d exceeds base index size %d",
112 (int)pos
, istate
->cache_nr
);
113 istate
->cache
[pos
]->ce_flags
|= CE_REMOVE
;
114 istate
->split_index
->nr_deletions
= 1;
117 static void replace_entry(size_t pos
, void *data
)
119 struct index_state
*istate
= data
;
120 struct split_index
*si
= istate
->split_index
;
121 struct cache_entry
*dst
, *src
;
123 if (pos
>= istate
->cache_nr
)
124 die("position for replacement %d exceeds base index size %d",
125 (int)pos
, istate
->cache_nr
);
126 if (si
->nr_replacements
>= si
->saved_cache_nr
)
127 die("too many replacements (%d vs %d)",
128 si
->nr_replacements
, si
->saved_cache_nr
);
129 dst
= istate
->cache
[pos
];
130 if (dst
->ce_flags
& CE_REMOVE
)
131 die("entry %d is marked as both replaced and deleted",
133 src
= si
->saved_cache
[si
->nr_replacements
];
135 die("corrupt link extension, entry %d should have "
136 "zero length name", (int)pos
);
137 src
->index
= pos
+ 1;
138 src
->ce_flags
|= CE_UPDATE_IN_BASE
;
139 src
->ce_namelen
= dst
->ce_namelen
;
140 copy_cache_entry(dst
, src
);
141 discard_cache_entry(src
);
142 si
->nr_replacements
++;
145 void merge_base_index(struct index_state
*istate
)
147 struct split_index
*si
= istate
->split_index
;
150 mark_base_index_entries(si
->base
);
152 si
->saved_cache
= istate
->cache
;
153 si
->saved_cache_nr
= istate
->cache_nr
;
154 istate
->cache_nr
= si
->base
->cache_nr
;
155 istate
->cache
= NULL
;
156 istate
->cache_alloc
= 0;
157 ALLOC_GROW(istate
->cache
, istate
->cache_nr
, istate
->cache_alloc
);
158 COPY_ARRAY(istate
->cache
, si
->base
->cache
, istate
->cache_nr
);
160 si
->nr_deletions
= 0;
161 si
->nr_replacements
= 0;
162 ewah_each_bit(si
->replace_bitmap
, replace_entry
, istate
);
163 ewah_each_bit(si
->delete_bitmap
, mark_entry_for_delete
, istate
);
164 if (si
->nr_deletions
)
165 remove_marked_cache_entries(istate
);
167 for (i
= si
->nr_replacements
; i
< si
->saved_cache_nr
; i
++) {
168 if (!ce_namelen(si
->saved_cache
[i
]))
169 die("corrupt link extension, entry %d should "
170 "have non-zero length name", i
);
171 add_index_entry(istate
, si
->saved_cache
[i
],
172 ADD_CACHE_OK_TO_ADD
|
173 ADD_CACHE_KEEP_CACHE_TREE
|
175 * we may have to replay what
176 * merge-recursive.c:update_stages()
177 * does, which has this flag on
179 ADD_CACHE_SKIP_DFCHECK
);
180 si
->saved_cache
[i
] = NULL
;
183 ewah_free(si
->delete_bitmap
);
184 ewah_free(si
->replace_bitmap
);
185 FREE_AND_NULL(si
->saved_cache
);
186 si
->delete_bitmap
= NULL
;
187 si
->replace_bitmap
= NULL
;
188 si
->saved_cache_nr
= 0;
191 void prepare_to_write_split_index(struct index_state
*istate
)
193 struct split_index
*si
= init_split_index(istate
);
194 struct cache_entry
**entries
= NULL
, *ce
;
195 int i
, nr_entries
= 0, nr_alloc
= 0;
197 si
->delete_bitmap
= ewah_new();
198 si
->replace_bitmap
= ewah_new();
201 /* Go through istate->cache[] and mark CE_MATCHED to
202 * entry with positive index. We'll go through
203 * base->cache[] later to delete all entries in base
204 * that are not marked with either CE_MATCHED or
205 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
206 * duplicate, deduplicate it.
208 for (i
= 0; i
< istate
->cache_nr
; i
++) {
209 struct cache_entry
*base
;
210 /* namelen is checked separately */
211 const unsigned int ondisk_flags
=
212 CE_STAGEMASK
| CE_VALID
| CE_EXTENDED_FLAGS
;
213 unsigned int ce_flags
, base_flags
, ret
;
214 ce
= istate
->cache
[i
];
217 if (ce
->index
> si
->base
->cache_nr
) {
221 ce
->ce_flags
|= CE_MATCHED
; /* or "shared" */
222 base
= si
->base
->cache
[ce
->index
- 1];
225 if (ce
->ce_namelen
!= base
->ce_namelen
||
226 strcmp(ce
->name
, base
->name
)) {
230 ce_flags
= ce
->ce_flags
;
231 base_flags
= base
->ce_flags
;
232 /* only on-disk flags matter */
233 ce
->ce_flags
&= ondisk_flags
;
234 base
->ce_flags
&= ondisk_flags
;
235 ret
= memcmp(&ce
->ce_stat_data
, &base
->ce_stat_data
,
236 offsetof(struct cache_entry
, name
) -
237 offsetof(struct cache_entry
, ce_stat_data
));
238 ce
->ce_flags
= ce_flags
;
239 base
->ce_flags
= base_flags
;
241 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
242 discard_cache_entry(base
);
243 si
->base
->cache
[ce
->index
- 1] = ce
;
245 for (i
= 0; i
< si
->base
->cache_nr
; i
++) {
246 ce
= si
->base
->cache
[i
];
247 if ((ce
->ce_flags
& CE_REMOVE
) ||
248 !(ce
->ce_flags
& CE_MATCHED
))
249 ewah_set(si
->delete_bitmap
, i
);
250 else if (ce
->ce_flags
& CE_UPDATE_IN_BASE
) {
251 ewah_set(si
->replace_bitmap
, i
);
252 ce
->ce_flags
|= CE_STRIP_NAME
;
253 ALLOC_GROW(entries
, nr_entries
+1, nr_alloc
);
254 entries
[nr_entries
++] = ce
;
256 if (is_null_oid(&ce
->oid
))
257 istate
->drop_cache_tree
= 1;
261 for (i
= 0; i
< istate
->cache_nr
; i
++) {
262 ce
= istate
->cache
[i
];
263 if ((!si
->base
|| !ce
->index
) && !(ce
->ce_flags
& CE_REMOVE
)) {
264 assert(!(ce
->ce_flags
& CE_STRIP_NAME
));
265 ALLOC_GROW(entries
, nr_entries
+1, nr_alloc
);
266 entries
[nr_entries
++] = ce
;
268 ce
->ce_flags
&= ~CE_MATCHED
;
272 * take cache[] out temporarily, put entries[] in its place
275 si
->saved_cache
= istate
->cache
;
276 si
->saved_cache_nr
= istate
->cache_nr
;
277 istate
->cache
= entries
;
278 istate
->cache_nr
= nr_entries
;
281 void finish_writing_split_index(struct index_state
*istate
)
283 struct split_index
*si
= init_split_index(istate
);
285 ewah_free(si
->delete_bitmap
);
286 ewah_free(si
->replace_bitmap
);
287 si
->delete_bitmap
= NULL
;
288 si
->replace_bitmap
= NULL
;
290 istate
->cache
= si
->saved_cache
;
291 istate
->cache_nr
= si
->saved_cache_nr
;
294 void discard_split_index(struct index_state
*istate
)
296 struct split_index
*si
= istate
->split_index
;
299 istate
->split_index
= NULL
;
304 discard_index(si
->base
);
310 void save_or_free_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
313 istate
->split_index
&&
314 istate
->split_index
->base
&&
315 ce
->index
<= istate
->split_index
->base
->cache_nr
&&
316 ce
== istate
->split_index
->base
->cache
[ce
->index
- 1])
317 ce
->ce_flags
|= CE_REMOVE
;
319 discard_cache_entry(ce
);
322 void replace_index_entry_in_base(struct index_state
*istate
,
323 struct cache_entry
*old_entry
,
324 struct cache_entry
*new_entry
)
326 if (old_entry
->index
&&
327 istate
->split_index
&&
328 istate
->split_index
->base
&&
329 old_entry
->index
<= istate
->split_index
->base
->cache_nr
) {
330 new_entry
->index
= old_entry
->index
;
331 if (old_entry
!= istate
->split_index
->base
->cache
[new_entry
->index
- 1])
332 discard_cache_entry(istate
->split_index
->base
->cache
[new_entry
->index
- 1]);
333 istate
->split_index
->base
->cache
[new_entry
->index
- 1] = new_entry
;
337 void add_split_index(struct index_state
*istate
)
339 if (!istate
->split_index
) {
340 init_split_index(istate
);
341 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
345 void remove_split_index(struct index_state
*istate
)
347 if (istate
->split_index
) {
349 * When removing the split index, we need to move
350 * ownership of the mem_pool associated with the
351 * base index to the main index. There may be cache entries
352 * allocated from the base's memory pool that are shared with
355 mem_pool_combine(istate
->ce_mem_pool
, istate
->split_index
->base
->ce_mem_pool
);
358 * The split index no longer owns the mem_pool backing
359 * its cache array. As we are discarding this index,
360 * mark the index as having no cache entries, so it
361 * will not attempt to clean up the cache entries or
364 if (istate
->split_index
->base
)
365 istate
->split_index
->base
->cache_nr
= 0;
368 * We can discard the split index because its
369 * memory pool has been incorporated into the
370 * memory pool associated with the the_index.
372 discard_split_index(istate
);
374 istate
->cache_changed
|= SOMETHING_CHANGED
;