]> git.ipfire.org Git - thirdparty/git.git/blame - pack-bitmap-write.c
Merge branch 'js/empty-index-fixes'
[thirdparty/git.git] / pack-bitmap-write.c
CommitLineData
d812c3b6 1#include "git-compat-util.h"
36bf1958 2#include "alloc.h"
32a8f510 3#include "environment.h"
f394e093 4#include "gettext.h"
41771fa4 5#include "hex.h"
a034e910 6#include "object-store-ll.h"
7cc8f971
VM
7#include "commit.h"
8#include "tag.h"
9#include "diff.h"
10#include "revision.h"
11#include "list-objects.h"
12#include "progress.h"
13#include "pack-revindex.h"
14#include "pack.h"
15#include "pack-bitmap.h"
bc626927 16#include "hash-lookup.h"
7cc8f971 17#include "pack-objects.h"
c339932b 18#include "path.h"
64043556 19#include "commit-reach.h"
6dc5ef75 20#include "prio-queue.h"
ec2f0269 21#include "trace2.h"
d4a4f929 22#include "tree.h"
0e312eaa 23#include "tree-walk.h"
7cc8f971
VM
24
25struct bitmapped_commit {
26 struct commit *commit;
27 struct ewah_bitmap *bitmap;
28 struct ewah_bitmap *write_as;
29 int flags;
30 int xor_offset;
31 uint32_t commit_pos;
32};
33
34struct bitmap_writer {
35 struct ewah_bitmap *commits;
36 struct ewah_bitmap *trees;
37 struct ewah_bitmap *blobs;
38 struct ewah_bitmap *tags;
39
d2bc62b1 40 kh_oid_map_t *bitmaps;
7cc8f971
VM
41 struct packing_data *to_pack;
42
43 struct bitmapped_commit *selected;
44 unsigned int selected_nr, selected_alloc;
45
46 struct progress *progress;
47 int show_progress;
825544a3 48 unsigned char pack_checksum[GIT_MAX_RAWSZ];
7cc8f971
VM
49};
50
51static struct bitmap_writer writer;
52
53void bitmap_writer_show_progress(int show)
54{
55 writer.show_progress = show;
56}
57
58/**
0f533c72 59 * Build the initial type index for the packfile or multi-pack-index
7cc8f971 60 */
06af3bba
NTND
61void bitmap_writer_build_type_index(struct packing_data *to_pack,
62 struct pack_idx_entry **index,
7cc8f971
VM
63 uint32_t index_nr)
64{
65 uint32_t i;
66
67 writer.commits = ewah_new();
68 writer.trees = ewah_new();
69 writer.blobs = ewah_new();
70 writer.tags = ewah_new();
06af3bba 71 ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
7cc8f971
VM
72
73 for (i = 0; i < index_nr; ++i) {
74 struct object_entry *entry = (struct object_entry *)index[i];
75 enum object_type real_type;
76
06af3bba 77 oe_set_in_pack_pos(to_pack, entry, i);
7cc8f971 78
fd9b1bae 79 switch (oe_type(entry)) {
7cc8f971
VM
80 case OBJ_COMMIT:
81 case OBJ_TREE:
82 case OBJ_BLOB:
83 case OBJ_TAG:
fd9b1bae 84 real_type = oe_type(entry);
7cc8f971
VM
85 break;
86
87 default:
7c141127 88 real_type = oid_object_info(to_pack->repo,
0df8e965 89 &entry->idx.oid, NULL);
7cc8f971
VM
90 break;
91 }
92
93 switch (real_type) {
94 case OBJ_COMMIT:
95 ewah_set(writer.commits, i);
96 break;
97
98 case OBJ_TREE:
99 ewah_set(writer.trees, i);
100 break;
101
102 case OBJ_BLOB:
103 ewah_set(writer.blobs, i);
104 break;
105
106 case OBJ_TAG:
107 ewah_set(writer.tags, i);
108 break;
109
110 default:
111 die("Missing type information for %s (%d/%d)",
e6a492b7 112 oid_to_hex(&entry->idx.oid), real_type,
fd9b1bae 113 oe_type(entry));
7cc8f971
VM
114 }
115 }
116}
117
118/**
119 * Compute the actual bitmaps
120 */
7cc8f971 121
449fa5ee 122static inline void push_bitmapped_commit(struct commit *commit)
7cc8f971
VM
123{
124 if (writer.selected_nr >= writer.selected_alloc) {
125 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
2756ca43 126 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
7cc8f971
VM
127 }
128
129 writer.selected[writer.selected_nr].commit = commit;
449fa5ee 130 writer.selected[writer.selected_nr].bitmap = NULL;
7cc8f971
VM
131 writer.selected[writer.selected_nr].flags = 0;
132
133 writer.selected_nr++;
134}
135
3ba3d062 136static uint32_t find_object_pos(const struct object_id *oid, int *found)
7cc8f971 137{
3a37876b 138 struct object_entry *entry = packlist_find(writer.to_pack, oid);
7cc8f971
VM
139
140 if (!entry) {
3ba3d062
TB
141 if (found)
142 *found = 0;
143 warning("Failed to write bitmap index. Packfile doesn't have full closure "
05805d74 144 "(object %s is missing)", oid_to_hex(oid));
3ba3d062 145 return 0;
7cc8f971
VM
146 }
147
3ba3d062
TB
148 if (found)
149 *found = 1;
06af3bba 150 return oe_in_pack_pos(writer.to_pack, entry);
7cc8f971
VM
151}
152
7cc8f971
VM
153static void compute_xor_offsets(void)
154{
155 static const int MAX_XOR_OFFSET_SEARCH = 10;
156
157 int i, next = 0;
158
159 while (next < writer.selected_nr) {
160 struct bitmapped_commit *stored = &writer.selected[next];
161
162 int best_offset = 0;
163 struct ewah_bitmap *best_bitmap = stored->bitmap;
164 struct ewah_bitmap *test_xor;
165
166 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
167 int curr = next - i;
168
169 if (curr < 0)
170 break;
171
172 test_xor = ewah_pool_new();
173 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
174
175 if (test_xor->buffer_size < best_bitmap->buffer_size) {
176 if (best_bitmap != stored->bitmap)
177 ewah_pool_free(best_bitmap);
178
179 best_bitmap = test_xor;
180 best_offset = i;
181 } else {
182 ewah_pool_free(test_xor);
183 }
184 }
185
186 stored->xor_offset = best_offset;
187 stored->write_as = best_bitmap;
188
189 next++;
190 }
191}
192
4a9c5817 193struct bb_commit {
928e3f42 194 struct commit_list *reverse_edges;
089f7513 195 struct bitmap *commit_mask;
4a9c5817 196 struct bitmap *bitmap;
089f7513
DS
197 unsigned selected:1,
198 maximal:1;
4a9c5817
JK
199 unsigned idx; /* within selected array */
200};
7cc8f971 201
4a9c5817 202define_commit_slab(bb_data, struct bb_commit);
7cc8f971 203
4a9c5817
JK
204struct bitmap_builder {
205 struct bb_data data;
206 struct commit **commits;
207 size_t commits_nr, commits_alloc;
208};
7cc8f971 209
4a9c5817 210static void bitmap_builder_init(struct bitmap_builder *bb,
f077b0a9
DS
211 struct bitmap_writer *writer,
212 struct bitmap_index *old_bitmap)
4a9c5817
JK
213{
214 struct rev_info revs;
215 struct commit *commit;
f077b0a9
DS
216 struct commit_list *reusable = NULL;
217 struct commit_list *r;
45f4eeb2 218 unsigned int i, num_maximal = 0;
7cc8f971 219
4a9c5817
JK
220 memset(bb, 0, sizeof(*bb));
221 init_bb_data(&bb->data);
7cc8f971 222
7cc8f971 223 reset_revision_walk();
4a9c5817
JK
224 repo_init_revisions(writer->to_pack->repo, &revs, NULL);
225 revs.topo_order = 1;
45f4eeb2 226 revs.first_parent_only = 1;
4a9c5817
JK
227
228 for (i = 0; i < writer->selected_nr; i++) {
229 struct commit *c = writer->selected[i].commit;
230 struct bb_commit *ent = bb_data_at(&bb->data, c);
089f7513 231
4a9c5817 232 ent->selected = 1;
089f7513 233 ent->maximal = 1;
4a9c5817 234 ent->idx = i;
089f7513
DS
235
236 ent->commit_mask = bitmap_new();
237 bitmap_set(ent->commit_mask, i);
238
4a9c5817
JK
239 add_pending_object(&revs, &c->object, "");
240 }
7cc8f971 241
4a9c5817
JK
242 if (prepare_revision_walk(&revs))
243 die("revision walk setup failed");
7cc8f971 244
4a9c5817 245 while ((commit = get_revision(&revs))) {
45f4eeb2 246 struct commit_list *p = commit->parents;
089f7513 247 struct bb_commit *c_ent;
7cc8f971 248
4a9c5817 249 parse_commit_or_die(commit);
7cc8f971 250
089f7513
DS
251 c_ent = bb_data_at(&bb->data, commit);
252
f077b0a9
DS
253 /*
254 * If there is no commit_mask, there is no reason to iterate
255 * over this commit; it is not selected (if it were, it would
256 * not have a blank commit mask) and all its children have
257 * existing bitmaps (see the comment starting with "This commit
258 * has an existing bitmap" below), so it does not contribute
259 * anything to the final bitmap file or its descendants.
260 */
261 if (!c_ent->commit_mask)
262 continue;
263
264 if (old_bitmap && bitmap_for_commit(old_bitmap, commit)) {
265 /*
266 * This commit has an existing bitmap, so we can
267 * get its bits immediately without an object
268 * walk. That is, it is reusable as-is and there is no
269 * need to continue walking beyond it.
270 *
271 * Mark it as such and add it to bb->commits separately
272 * to avoid allocating a position in the commit mask.
273 */
274 commit_list_insert(commit, &reusable);
275 goto next;
276 }
277
089f7513 278 if (c_ent->maximal) {
45f4eeb2 279 num_maximal++;
089f7513
DS
280 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
281 bb->commits[bb->commits_nr++] = commit;
282 }
7cc8f971 283
45f4eeb2 284 if (p) {
089f7513
DS
285 struct bb_commit *p_ent = bb_data_at(&bb->data, p->item);
286 int c_not_p, p_not_c;
287
288 if (!p_ent->commit_mask) {
289 p_ent->commit_mask = bitmap_new();
290 c_not_p = 1;
291 p_not_c = 0;
292 } else {
293 c_not_p = bitmap_is_subset(c_ent->commit_mask, p_ent->commit_mask);
294 p_not_c = bitmap_is_subset(p_ent->commit_mask, c_ent->commit_mask);
295 }
296
297 if (!c_not_p)
298 continue;
299
300 bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
301
302 if (p_not_c)
303 p_ent->maximal = 1;
304 else {
305 p_ent->maximal = 0;
306 free_commit_list(p_ent->reverse_edges);
307 p_ent->reverse_edges = NULL;
308 }
309
310 if (c_ent->maximal) {
311 commit_list_insert(commit, &p_ent->reverse_edges);
312 } else {
313 struct commit_list *cc = c_ent->reverse_edges;
314
315 for (; cc; cc = cc->next) {
316 if (!commit_list_contains(cc->item, p_ent->reverse_edges))
317 commit_list_insert(cc->item, &p_ent->reverse_edges);
318 }
319 }
4a9c5817 320 }
089f7513 321
f077b0a9 322next:
089f7513
DS
323 bitmap_free(c_ent->commit_mask);
324 c_ent->commit_mask = NULL;
4a9c5817 325 }
089f7513 326
f077b0a9
DS
327 for (r = reusable; r; r = r->next) {
328 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
329 bb->commits[bb->commits_nr++] = r->item;
330 }
331
089f7513
DS
332 trace2_data_intmax("pack-bitmap-write", the_repository,
333 "num_selected_commits", writer->selected_nr);
334 trace2_data_intmax("pack-bitmap-write", the_repository,
335 "num_maximal_commits", num_maximal);
f077b0a9 336
2108fe4a 337 release_revisions(&revs);
f077b0a9 338 free_commit_list(reusable);
4a9c5817
JK
339}
340
341static void bitmap_builder_clear(struct bitmap_builder *bb)
342{
343 clear_bb_data(&bb->data);
344 free(bb->commits);
345 bb->commits_nr = bb->commits_alloc = 0;
346}
347
3ba3d062
TB
348static int fill_bitmap_tree(struct bitmap *bitmap,
349 struct tree *tree)
4a9c5817 350{
3ba3d062 351 int found;
4a9c5817
JK
352 uint32_t pos;
353 struct tree_desc desc;
354 struct name_entry entry;
355
356 /*
357 * If our bit is already set, then there is nothing to do. Both this
358 * tree and all of its children will be set.
359 */
3ba3d062
TB
360 pos = find_object_pos(&tree->object.oid, &found);
361 if (!found)
362 return -1;
4a9c5817 363 if (bitmap_get(bitmap, pos))
3ba3d062 364 return 0;
4a9c5817
JK
365 bitmap_set(bitmap, pos);
366
367 if (parse_tree(tree) < 0)
368 die("unable to load tree object %s",
369 oid_to_hex(&tree->object.oid));
370 init_tree_desc(&desc, tree->buffer, tree->size);
371
372 while (tree_entry(&desc, &entry)) {
373 switch (object_type(entry.mode)) {
374 case OBJ_TREE:
3ba3d062
TB
375 if (fill_bitmap_tree(bitmap,
376 lookup_tree(the_repository, &entry.oid)) < 0)
377 return -1;
4a9c5817
JK
378 break;
379 case OBJ_BLOB:
3ba3d062
TB
380 pos = find_object_pos(&entry.oid, &found);
381 if (!found)
382 return -1;
383 bitmap_set(bitmap, pos);
4a9c5817
JK
384 break;
385 default:
386 /* Gitlink, etc; not reachable */
387 break;
388 }
389 }
7cc8f971 390
4a9c5817 391 free_tree_buffer(tree);
3ba3d062 392 return 0;
4a9c5817 393}
7cc8f971 394
e9c38399
TB
395static int reused_bitmaps_nr;
396
3ba3d062
TB
397static int fill_bitmap_commit(struct bb_commit *ent,
398 struct commit *commit,
399 struct prio_queue *queue,
400 struct prio_queue *tree_queue,
401 struct bitmap_index *old_bitmap,
402 const uint32_t *mapping)
4a9c5817 403{
3ba3d062
TB
404 int found;
405 uint32_t pos;
4a9c5817
JK
406 if (!ent->bitmap)
407 ent->bitmap = bitmap_new();
408
6dc5ef75
DS
409 prio_queue_put(queue, commit);
410
411 while (queue->nr) {
412 struct commit_list *p;
413 struct commit *c = prio_queue_get(queue);
414
341fa348
DS
415 if (old_bitmap && mapping) {
416 struct ewah_bitmap *old = bitmap_for_commit(old_bitmap, c);
417 /*
418 * If this commit has an old bitmap, then translate that
419 * bitmap and add its bits to this one. No need to walk
420 * parents or the tree for this commit.
421 */
e9c38399
TB
422 if (old && !rebuild_bitmap(mapping, old, ent->bitmap)) {
423 reused_bitmaps_nr++;
341fa348 424 continue;
e9c38399 425 }
341fa348
DS
426 }
427
428 /*
429 * Mark ourselves and queue our tree. The commit
430 * walk ensures we cover all parents.
431 */
3ba3d062
TB
432 pos = find_object_pos(&c->object.oid, &found);
433 if (!found)
434 return -1;
435 bitmap_set(ent->bitmap, pos);
ecb5091f
ÆAB
436 prio_queue_put(tree_queue,
437 repo_get_commit_tree(the_repository, c));
6dc5ef75
DS
438
439 for (p = c->parents; p; p = p->next) {
3ba3d062
TB
440 pos = find_object_pos(&p->item->object.oid, &found);
441 if (!found)
442 return -1;
6dc5ef75
DS
443 if (!bitmap_get(ent->bitmap, pos)) {
444 bitmap_set(ent->bitmap, pos);
445 prio_queue_put(queue, p->item);
446 }
447 }
448 }
341fa348 449
3ba3d062
TB
450 while (tree_queue->nr) {
451 if (fill_bitmap_tree(ent->bitmap,
452 prio_queue_get(tree_queue)) < 0)
453 return -1;
454 }
455 return 0;
4a9c5817 456}
7cc8f971 457
4a9c5817
JK
458static void store_selected(struct bb_commit *ent, struct commit *commit)
459{
460 struct bitmapped_commit *stored = &writer.selected[ent->idx];
461 khiter_t hash_pos;
462 int hash_ret;
463
4a9c5817
JK
464 stored->bitmap = bitmap_to_ewah(ent->bitmap);
465
466 hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
467 if (hash_ret == 0)
468 die("Duplicate entry when writing index: %s",
469 oid_to_hex(&commit->object.oid));
470 kh_value(writer.bitmaps, hash_pos) = stored;
471}
7cc8f971 472
3ba3d062 473int bitmap_writer_build(struct packing_data *to_pack)
4a9c5817
JK
474{
475 struct bitmap_builder bb;
476 size_t i;
477 int nr_stored = 0; /* for progress */
6dc5ef75 478 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
341fa348
DS
479 struct prio_queue tree_queue = { NULL };
480 struct bitmap_index *old_bitmap;
481 uint32_t *mapping;
3ba3d062 482 int closed = 1; /* until proven otherwise */
7cc8f971 483
4a9c5817
JK
484 writer.bitmaps = kh_init_oid_map();
485 writer.to_pack = to_pack;
486
487 if (writer.show_progress)
488 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
489 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
490 the_repository);
7cc8f971 491
341fa348
DS
492 old_bitmap = prepare_bitmap_git(to_pack->repo);
493 if (old_bitmap)
494 mapping = create_bitmap_mapping(old_bitmap, to_pack);
495 else
496 mapping = NULL;
497
f077b0a9 498 bitmap_builder_init(&bb, &writer, old_bitmap);
4a9c5817
JK
499 for (i = bb.commits_nr; i > 0; i--) {
500 struct commit *commit = bb.commits[i-1];
501 struct bb_commit *ent = bb_data_at(&bb.data, commit);
502 struct commit *child;
010e5eac 503 int reused = 0;
7cc8f971 504
3ba3d062
TB
505 if (fill_bitmap_commit(ent, commit, &queue, &tree_queue,
506 old_bitmap, mapping) < 0) {
507 closed = 0;
508 break;
509 }
7cc8f971 510
4a9c5817
JK
511 if (ent->selected) {
512 store_selected(ent, commit);
513 nr_stored++;
514 display_progress(writer.progress, nr_stored);
515 }
516
928e3f42 517 while ((child = pop_commit(&ent->reverse_edges))) {
4a9c5817
JK
518 struct bb_commit *child_ent =
519 bb_data_at(&bb.data, child);
520
521 if (child_ent->bitmap)
522 bitmap_or(child_ent->bitmap, ent->bitmap);
010e5eac 523 else if (reused)
4a9c5817 524 child_ent->bitmap = bitmap_dup(ent->bitmap);
010e5eac
JK
525 else {
526 child_ent->bitmap = ent->bitmap;
527 reused = 1;
528 }
4a9c5817 529 }
010e5eac
JK
530 if (!reused)
531 bitmap_free(ent->bitmap);
4a9c5817 532 ent->bitmap = NULL;
7cc8f971 533 }
6dc5ef75 534 clear_prio_queue(&queue);
341fa348 535 clear_prio_queue(&tree_queue);
4a9c5817 536 bitmap_builder_clear(&bb);
1d7f7f24 537 free_bitmap_index(old_bitmap);
341fa348 538 free(mapping);
4a9c5817
JK
539
540 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
541 the_repository);
e9c38399
TB
542 trace2_data_intmax("pack-bitmap-write", the_repository,
543 "building_bitmaps_reused", reused_bitmaps_nr);
7cc8f971 544
7cc8f971
VM
545 stop_progress(&writer.progress);
546
3ba3d062
TB
547 if (closed)
548 compute_xor_offsets();
549 return closed ? 0 : -1;
7cc8f971
VM
550}
551
552/**
553 * Select the commits that will be bitmapped
554 */
555static inline unsigned int next_commit_index(unsigned int idx)
556{
557 static const unsigned int MIN_COMMITS = 100;
558 static const unsigned int MAX_COMMITS = 5000;
559
560 static const unsigned int MUST_REGION = 100;
561 static const unsigned int MIN_REGION = 20000;
562
563 unsigned int offset, next;
564
565 if (idx <= MUST_REGION)
566 return 0;
567
568 if (idx <= MIN_REGION) {
569 offset = idx - MUST_REGION;
570 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
571 }
572
573 offset = idx - MIN_REGION;
574 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
575
576 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
577}
578
579static int date_compare(const void *_a, const void *_b)
580{
581 struct commit *a = *(struct commit **)_a;
582 struct commit *b = *(struct commit **)_b;
583 return (long)b->date - (long)a->date;
584}
585
7cc8f971
VM
586void bitmap_writer_select_commits(struct commit **indexed_commits,
587 unsigned int indexed_commits_nr,
588 int max_bitmaps)
589{
590 unsigned int i = 0, j, next;
591
9ed0d8d6 592 QSORT(indexed_commits, indexed_commits_nr, date_compare);
7cc8f971 593
7cc8f971
VM
594 if (indexed_commits_nr < 100) {
595 for (i = 0; i < indexed_commits_nr; ++i)
449fa5ee 596 push_bitmapped_commit(indexed_commits[i]);
7cc8f971
VM
597 return;
598 }
599
b3118a56
ÆAB
600 if (writer.show_progress)
601 writer.progress = start_progress("Selecting bitmap commits", 0);
602
7cc8f971 603 for (;;) {
7cc8f971
VM
604 struct commit *chosen = NULL;
605
606 next = next_commit_index(i);
607
608 if (i + next >= indexed_commits_nr)
609 break;
610
611 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
612 writer.selected_nr = max_bitmaps;
613 break;
614 }
615
616 if (next == 0) {
617 chosen = indexed_commits[i];
7cc8f971
VM
618 } else {
619 chosen = indexed_commits[i + next];
620
621 for (j = 0; j <= next; ++j) {
622 struct commit *cm = indexed_commits[i + j];
623
449fa5ee 624 if ((cm->object.flags & NEEDS_BITMAP) != 0) {
7cc8f971
VM
625 chosen = cm;
626 break;
627 }
628
629 if (cm->parents && cm->parents->next)
630 chosen = cm;
631 }
632 }
633
449fa5ee 634 push_bitmapped_commit(chosen);
7cc8f971
VM
635
636 i += next + 1;
637 display_progress(writer.progress, i);
638 }
639
640 stop_progress(&writer.progress);
641}
642
643
98a3beab 644static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
7cc8f971 645{
98a3beab 646 /* hashwrite will die on error */
647 hashwrite(f, buf, len);
7cc8f971
VM
648 return len;
649}
650
651/**
652 * Write the bitmap index to disk
653 */
98a3beab 654static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
7cc8f971 655{
98a3beab 656 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
7cc8f971
VM
657 die("Failed to write bitmap index");
658}
659
8380dcd7 660static const struct object_id *oid_access(size_t pos, const void *table)
7cc8f971 661{
8380dcd7 662 const struct pack_idx_entry * const *index = table;
45ee13b9 663 return &index[pos]->oid;
7cc8f971
VM
664}
665
98a3beab 666static void write_selected_commits_v1(struct hashfile *f,
93eb41e2
AC
667 uint32_t *commit_positions,
668 off_t *offsets)
7cc8f971
VM
669{
670 int i;
671
672 for (i = 0; i < writer.selected_nr; ++i) {
673 struct bitmapped_commit *stored = &writer.selected[i];
7cc8f971 674
93eb41e2
AC
675 if (offsets)
676 offsets[i] = hashfile_total(f);
677
aa301625 678 hashwrite_be32(f, commit_positions[i]);
98a3beab 679 hashwrite_u8(f, stored->xor_offset);
680 hashwrite_u8(f, stored->flags);
7cc8f971 681
7cc8f971
VM
682 dump_bitmap(f, stored->write_as);
683 }
684}
685
93eb41e2
AC
686static int table_cmp(const void *_va, const void *_vb, void *_data)
687{
688 uint32_t *commit_positions = _data;
689 uint32_t a = commit_positions[*(uint32_t *)_va];
690 uint32_t b = commit_positions[*(uint32_t *)_vb];
691
692 if (a > b)
693 return 1;
694 else if (a < b)
695 return -1;
696
697 return 0;
698}
699
700static void write_lookup_table(struct hashfile *f,
93eb41e2
AC
701 uint32_t *commit_positions,
702 off_t *offsets)
703{
704 uint32_t i;
705 uint32_t *table, *table_inv;
706
707 ALLOC_ARRAY(table, writer.selected_nr);
708 ALLOC_ARRAY(table_inv, writer.selected_nr);
709
710 for (i = 0; i < writer.selected_nr; i++)
711 table[i] = i;
712
713 /*
714 * At the end of this sort table[j] = i means that the i'th
715 * bitmap corresponds to j'th bitmapped commit (among the selected
716 * commits) in lex order of OIDs.
717 */
718 QSORT_S(table, writer.selected_nr, table_cmp, commit_positions);
719
720 /* table_inv helps us discover that relationship (i'th bitmap
721 * to j'th commit by j = table_inv[i])
722 */
723 for (i = 0; i < writer.selected_nr; i++)
724 table_inv[table[i]] = i;
725
726 trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository);
727 for (i = 0; i < writer.selected_nr; i++) {
728 struct bitmapped_commit *selected = &writer.selected[table[i]];
729 uint32_t xor_offset = selected->xor_offset;
730 uint32_t xor_row;
731
732 if (xor_offset) {
733 /*
734 * xor_index stores the index (in the bitmap entries)
735 * of the corresponding xor bitmap. But we need to convert
736 * this index into lookup table's index. So, table_inv[xor_index]
737 * gives us the index position w.r.t. the lookup table.
738 *
739 * If "k = table[i] - xor_offset" then the xor base is the k'th
740 * bitmap. `table_inv[k]` gives us the position of that bitmap
741 * in the lookup table.
742 */
743 uint32_t xor_index = table[i] - xor_offset;
744 xor_row = table_inv[xor_index];
745 } else {
746 xor_row = 0xffffffff;
747 }
748
749 hashwrite_be32(f, commit_positions[table[i]]);
750 hashwrite_be64(f, (uint64_t)offsets[table[i]]);
751 hashwrite_be32(f, xor_row);
752 }
753 trace2_region_leave("pack-bitmap-write", "writing_lookup_table", the_repository);
754
755 free(table);
756 free(table_inv);
757}
758
98a3beab 759static void write_hash_cache(struct hashfile *f,
ae4f07fb
VM
760 struct pack_idx_entry **index,
761 uint32_t index_nr)
762{
763 uint32_t i;
764
765 for (i = 0; i < index_nr; ++i) {
766 struct object_entry *entry = (struct object_entry *)index[i];
7744a5d6 767 hashwrite_be32(f, entry->hash);
ae4f07fb
VM
768 }
769}
770
57665249 771void bitmap_writer_set_checksum(const unsigned char *sha1)
7cc8f971
VM
772{
773 hashcpy(writer.pack_checksum, sha1);
774}
775
776void bitmap_writer_finish(struct pack_idx_entry **index,
777 uint32_t index_nr,
ae4f07fb
VM
778 const char *filename,
779 uint16_t options)
7cc8f971 780{
7cc8f971
VM
781 static uint16_t default_version = 1;
782 static uint16_t flags = BITMAP_OPT_FULL_DAG;
594fa999 783 struct strbuf tmp_file = STRBUF_INIT;
98a3beab 784 struct hashfile *f;
aa301625 785 uint32_t *commit_positions = NULL;
93eb41e2 786 off_t *offsets = NULL;
aa301625 787 uint32_t i;
7cc8f971
VM
788
789 struct bitmap_disk_header header;
790
594fa999 791 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
7cc8f971 792
98a3beab 793 f = hashfd(fd, tmp_file.buf);
7cc8f971
VM
794
795 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
796 header.version = htons(default_version);
ae4f07fb 797 header.options = htons(flags | options);
7cc8f971 798 header.entry_count = htonl(writer.selected_nr);
50546b15 799 hashcpy(header.checksum, writer.pack_checksum);
7cc8f971 800
0f4d6cad 801 hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
7cc8f971
VM
802 dump_bitmap(f, writer.commits);
803 dump_bitmap(f, writer.trees);
804 dump_bitmap(f, writer.blobs);
805 dump_bitmap(f, writer.tags);
aa301625 806
93eb41e2
AC
807 if (options & BITMAP_OPT_LOOKUP_TABLE)
808 CALLOC_ARRAY(offsets, index_nr);
809
aa301625
AC
810 ALLOC_ARRAY(commit_positions, writer.selected_nr);
811
812 for (i = 0; i < writer.selected_nr; i++) {
813 struct bitmapped_commit *stored = &writer.selected[i];
814 int commit_pos = oid_pos(&stored->commit->object.oid, index, index_nr, oid_access);
815
816 if (commit_pos < 0)
817 BUG(_("trying to write commit not in index"));
818
819 commit_positions[i] = commit_pos;
820 }
821
969a5645 822 write_selected_commits_v1(f, commit_positions, offsets);
93eb41e2
AC
823
824 if (options & BITMAP_OPT_LOOKUP_TABLE)
969a5645 825 write_lookup_table(f, commit_positions, offsets);
7cc8f971 826
ae4f07fb
VM
827 if (options & BITMAP_OPT_HASH_CACHE)
828 write_hash_cache(f, index, index_nr);
829
020406ea
NS
830 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
831 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
7cc8f971 832
594fa999 833 if (adjust_shared_perm(tmp_file.buf))
7cc8f971
VM
834 die_errno("unable to make temporary bitmap file readable");
835
594fa999 836 if (rename(tmp_file.buf, filename))
7cc8f971 837 die_errno("unable to rename temporary bitmap file to '%s'", filename);
594fa999
JK
838
839 strbuf_release(&tmp_file);
aa301625 840 free(commit_positions);
93eb41e2 841 free(offsets);
7cc8f971 842}