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