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