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