split-index: the reading part
[git/git.git] / split-index.c
CommitLineData
5fc2fc8f
NTND
1#include "cache.h"
2#include "split-index.h"
96a1d8d3 3#include "ewah/ewok.h"
5fc2fc8f
NTND
4
5struct split_index *init_split_index(struct index_state *istate)
6{
7 if (!istate->split_index) {
8 istate->split_index = xcalloc(1, sizeof(*istate->split_index));
9 istate->split_index->refcount = 1;
10 }
11 return istate->split_index;
12}
13
14int read_link_extension(struct index_state *istate,
15 const void *data_, unsigned long sz)
16{
17 const unsigned char *data = data_;
18 struct split_index *si;
76b07b37
NTND
19 int ret;
20
5fc2fc8f
NTND
21 if (sz < 20)
22 return error("corrupt link extension (too short)");
23 si = init_split_index(istate);
24 hashcpy(si->base_sha1, data);
25 data += 20;
26 sz -= 20;
76b07b37
NTND
27 if (!sz)
28 return 0;
29 si->delete_bitmap = ewah_new();
30 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
31 if (ret < 0)
32 return error("corrupt delete bitmap in link extension");
33 data += ret;
34 sz -= ret;
35 si->replace_bitmap = ewah_new();
36 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
37 if (ret < 0)
38 return error("corrupt replace bitmap in link extension");
39 if (ret != sz)
5fc2fc8f
NTND
40 return error("garbage at the end of link extension");
41 return 0;
42}
43
96a1d8d3
NTND
44static int write_strbuf(void *user_data, const void *data, size_t len)
45{
46 struct strbuf *sb = user_data;
47 strbuf_add(sb, data, len);
48 return len;
49}
50
5fc2fc8f
NTND
51int write_link_extension(struct strbuf *sb,
52 struct index_state *istate)
53{
54 struct split_index *si = istate->split_index;
55 strbuf_add(sb, si->base_sha1, 20);
96a1d8d3
NTND
56 if (!si->delete_bitmap && !si->replace_bitmap)
57 return 0;
58 ewah_serialize_to(si->delete_bitmap, write_strbuf, sb);
59 ewah_serialize_to(si->replace_bitmap, write_strbuf, sb);
5fc2fc8f
NTND
60 return 0;
61}
62
63static void mark_base_index_entries(struct index_state *base)
64{
65 int i;
66 /*
67 * To keep track of the shared entries between
68 * istate->base->cache[] and istate->cache[], base entry
69 * position is stored in each base entry. All positions start
70 * from 1 instead of 0, which is resrved to say "this is a new
71 * entry".
72 */
73 for (i = 0; i < base->cache_nr; i++)
74 base->cache[i]->index = i + 1;
75}
76
76b07b37
NTND
77static void mark_entry_for_delete(size_t pos, void *data)
78{
79 struct index_state *istate = data;
80 if (pos >= istate->cache_nr)
81 die("position for delete %d exceeds base index size %d",
82 (int)pos, istate->cache_nr);
83 istate->cache[pos]->ce_flags |= CE_REMOVE;
84 istate->split_index->nr_deletions = 1;
85}
86
87static void replace_entry(size_t pos, void *data)
88{
89 struct index_state *istate = data;
90 struct split_index *si = istate->split_index;
91 struct cache_entry *dst, *src;
92 if (pos >= istate->cache_nr)
93 die("position for replacement %d exceeds base index size %d",
94 (int)pos, istate->cache_nr);
95 if (si->nr_replacements >= si->saved_cache_nr)
96 die("too many replacements (%d vs %d)",
97 si->nr_replacements, si->saved_cache_nr);
98 dst = istate->cache[pos];
99 if (dst->ce_flags & CE_REMOVE)
100 die("entry %d is marked as both replaced and deleted",
101 (int)pos);
102 src = si->saved_cache[si->nr_replacements];
103 src->index = pos + 1;
104 src->ce_flags |= CE_UPDATE_IN_BASE;
105 free(dst);
106 dst = src;
107 si->nr_replacements++;
108}
109
5fc2fc8f
NTND
110void merge_base_index(struct index_state *istate)
111{
112 struct split_index *si = istate->split_index;
76b07b37 113 unsigned int i;
5fc2fc8f
NTND
114
115 mark_base_index_entries(si->base);
76b07b37
NTND
116
117 si->saved_cache = istate->cache;
118 si->saved_cache_nr = istate->cache_nr;
119 istate->cache_nr = si->base->cache_nr;
120 istate->cache = NULL;
121 istate->cache_alloc = 0;
5fc2fc8f
NTND
122 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
123 memcpy(istate->cache, si->base->cache,
124 sizeof(*istate->cache) * istate->cache_nr);
76b07b37
NTND
125
126 si->nr_deletions = 0;
127 si->nr_replacements = 0;
128 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
129 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
130 if (si->nr_deletions)
131 remove_marked_cache_entries(istate);
132
133 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
134 add_index_entry(istate, si->saved_cache[i],
135 ADD_CACHE_OK_TO_ADD |
136 /*
137 * we may have to replay what
138 * merge-recursive.c:update_stages()
139 * does, which has this flag on
140 */
141 ADD_CACHE_SKIP_DFCHECK);
142 si->saved_cache[i] = NULL;
143 }
144
145 ewah_free(si->delete_bitmap);
146 ewah_free(si->replace_bitmap);
147 free(si->saved_cache);
148 si->delete_bitmap = NULL;
149 si->replace_bitmap = NULL;
150 si->saved_cache = NULL;
151 si->saved_cache_nr = 0;
5fc2fc8f
NTND
152}
153
154void prepare_to_write_split_index(struct index_state *istate)
155{
156 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
157 struct cache_entry **entries = NULL, *ce;
158 int i, nr_entries = 0, nr_alloc = 0;
159
160 si->delete_bitmap = ewah_new();
161 si->replace_bitmap = ewah_new();
162
163 if (si->base) {
164 /* Go through istate->cache[] and mark CE_MATCHED to
165 * entry with positive index. We'll go through
166 * base->cache[] later to delete all entries in base
167 * that are not marked eith either CE_MATCHED or
168 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
169 * duplicate, deduplicate it.
170 */
171 for (i = 0; i < istate->cache_nr; i++) {
172 struct cache_entry *base;
173 /* namelen is checked separately */
174 const unsigned int ondisk_flags =
175 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
176 unsigned int ce_flags, base_flags, ret;
177 ce = istate->cache[i];
178 if (!ce->index)
179 continue;
180 if (ce->index > si->base->cache_nr) {
181 ce->index = 0;
182 continue;
183 }
184 ce->ce_flags |= CE_MATCHED; /* or "shared" */
185 base = si->base->cache[ce->index - 1];
186 if (ce == base)
187 continue;
188 if (ce->ce_namelen != base->ce_namelen ||
189 strcmp(ce->name, base->name)) {
190 ce->index = 0;
191 continue;
192 }
193 ce_flags = ce->ce_flags;
194 base_flags = base->ce_flags;
195 /* only on-disk flags matter */
196 ce->ce_flags &= ondisk_flags;
197 base->ce_flags &= ondisk_flags;
198 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
199 offsetof(struct cache_entry, name) -
200 offsetof(struct cache_entry, ce_stat_data));
201 ce->ce_flags = ce_flags;
202 base->ce_flags = base_flags;
203 if (ret)
204 ce->ce_flags |= CE_UPDATE_IN_BASE;
205 free(base);
206 si->base->cache[ce->index - 1] = ce;
207 }
208 for (i = 0; i < si->base->cache_nr; i++) {
209 ce = si->base->cache[i];
210 if ((ce->ce_flags & CE_REMOVE) ||
211 !(ce->ce_flags & CE_MATCHED))
212 ewah_set(si->delete_bitmap, i);
213 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
214 ewah_set(si->replace_bitmap, i);
215 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
216 entries[nr_entries++] = ce;
217 }
218 }
219 }
220
221 for (i = 0; i < istate->cache_nr; i++) {
222 ce = istate->cache[i];
223 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
224 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
225 entries[nr_entries++] = ce;
226 }
227 ce->ce_flags &= ~CE_MATCHED;
228 }
229
230 /*
231 * take cache[] out temporarily, put entries[] in its place
232 * for writing
233 */
234 si->saved_cache = istate->cache;
5fc2fc8f 235 si->saved_cache_nr = istate->cache_nr;
96a1d8d3
NTND
236 istate->cache = entries;
237 istate->cache_nr = nr_entries;
5fc2fc8f
NTND
238}
239
240void finish_writing_split_index(struct index_state *istate)
241{
242 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
243
244 ewah_free(si->delete_bitmap);
245 ewah_free(si->replace_bitmap);
246 si->delete_bitmap = NULL;
247 si->replace_bitmap = NULL;
248 free(istate->cache);
249 istate->cache = si->saved_cache;
5fc2fc8f
NTND
250 istate->cache_nr = si->saved_cache_nr;
251}
252
253void discard_split_index(struct index_state *istate)
254{
255 struct split_index *si = istate->split_index;
256 if (!si)
257 return;
258 istate->split_index = NULL;
259 si->refcount--;
260 if (si->refcount)
261 return;
262 if (si->base) {
263 discard_index(si->base);
264 free(si->base);
265 }
266 free(si);
267}
045113a5
NTND
268
269void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
270{
271 if (ce->index &&
272 istate->split_index &&
273 istate->split_index->base &&
274 ce->index <= istate->split_index->base->cache_nr &&
275 ce == istate->split_index->base->cache[ce->index - 1])
276 ce->ce_flags |= CE_REMOVE;
277 else
278 free(ce);
279}
078a58e8
NTND
280
281void replace_index_entry_in_base(struct index_state *istate,
282 struct cache_entry *old,
283 struct cache_entry *new)
284{
285 if (old->index &&
286 istate->split_index &&
287 istate->split_index->base &&
288 old->index <= istate->split_index->base->cache_nr) {
289 new->index = old->index;
290 if (old != istate->split_index->base->cache[new->index - 1])
291 free(istate->split_index->base->cache[new->index - 1]);
292 istate->split_index->base->cache[new->index - 1] = new;
293 }
294}