midx: write object ids in a chunk

Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 12, 2018 at 15:39 UTC 0d5b3a5ef72383f3b6fe93793be3bbd107a88eaa
5 files changed +53 -5
Documentation/technical/pack-format.txt
+4
@@ -302,6 +302,10 @@ CHUNK DATA:
302 name. This is the only chunk not guaranteed to be a multiple of four
303 bytes in length, so should be the last chunk for alignment reasons.
304
305 + OID Lookup (ID: {'O', 'I', 'D', 'L'})
306 + The OIDs for all objects in the MIDX are stored in lexicographic
307 + order in this chunk.
308 +
309 (This section intentionally left incomplete.)
310
311 TRAILER:
midx.c
+44 -3
@@ -18,9 +18,10 @@
18 #define MIDX_HASH_LEN 20
19 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN)
20
21 -#define MIDX_MAX_CHUNKS 1
21 +#define MIDX_MAX_CHUNKS 2
22 #define MIDX_CHUNK_ALIGNMENT 4
23 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
24 +#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
25 #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
26
27 static char *get_midx_filename(const char *object_dir)
@@ -101,6 +102,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir)
102 m->chunk_pack_names = m->data + chunk_offset;
103 break;
104
105 + case MIDX_CHUNKID_OIDLOOKUP:
106 + m->chunk_oid_lookup = m->data + chunk_offset;
107 + break;
108 +
109 case 0:
110 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
111 break;
@@ -116,6 +121,8 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir)
121
122 if (!m->chunk_pack_names)
123 die(_("multi-pack-index missing required pack-name chunk"));
124 + if (!m->chunk_oid_lookup)
125 + die(_("multi-pack-index missing required OID lookup chunk"));
126
127 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
128
@@ -382,6 +389,32 @@ static size_t write_midx_pack_names(struct hashfile *f,
389 return written;
390 }
391
392 +static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
393 + struct pack_midx_entry *objects,
394 + uint32_t nr_objects)
395 +{
396 + struct pack_midx_entry *list = objects;
397 + uint32_t i;
398 + size_t written = 0;
399 +
400 + for (i = 0; i < nr_objects; i++) {
401 + struct pack_midx_entry *obj = list++;
402 +
403 + if (i < nr_objects - 1) {
404 + struct pack_midx_entry *next = list;
405 + if (oidcmp(&obj->oid, &next->oid) >= 0)
406 + BUG("OIDs not in order: %s >= %s",
407 + oid_to_hex(&obj->oid),
408 + oid_to_hex(&next->oid));
409 + }
410 +
411 + hashwrite(f, obj->oid.hash, (int)hash_len);
412 + written += hash_len;
413 + }
414 +
415 + return written;
416 +}
417 +
418 int write_midx_file(const char *object_dir)
419 {
420 unsigned char cur_chunk, num_chunks = 0;
@@ -428,7 +461,7 @@ int write_midx_file(const char *object_dir)
461 FREE_AND_NULL(midx_name);
462
463 cur_chunk = 0;
431 - num_chunks = 1;
464 + num_chunks = 2;
465
466 written = write_midx_header(f, num_chunks, packs.nr);
467
@@ -436,9 +469,13 @@ int write_midx_file(const char *object_dir)
469 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
470
471 cur_chunk++;
439 - chunk_ids[cur_chunk] = 0;
472 + chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
473 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len;
474
475 + cur_chunk++;
476 + chunk_ids[cur_chunk] = 0;
477 + chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN;
478 +
479 for (i = 0; i <= num_chunks; i++) {
480 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
481 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
@@ -468,6 +505,10 @@ int write_midx_file(const char *object_dir)
505 written += write_midx_pack_names(f, packs.names, packs.nr);
506 break;
507
508 + case MIDX_CHUNKID_OIDLOOKUP:
509 + written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries);
510 + break;
511 +
512 default:
513 BUG("trying to write unknown chunk id %"PRIx32,
514 chunk_ids[i]);
midx.h
+1
@@ -15,6 +15,7 @@ struct multi_pack_index {
15 uint32_t num_objects;
16
17 const unsigned char *chunk_pack_names;
18 + const unsigned char *chunk_oid_lookup;
19
20 const char **pack_names;
21 char object_dir[FLEX_ARRAY];
t/helper/test-read-midx.c
+2
@@ -22,6 +22,8 @@ static int read_midx_file(const char *object_dir)
22
23 if (m->chunk_pack_names)
24 printf(" pack-names");
25 + if (m->chunk_oid_lookup)
26 + printf(" oid-lookup");
27
28 printf("\n");
29
t/t5319-multi-pack-index.sh
+2 -2
@@ -7,8 +7,8 @@ midx_read_expect () {
7 NUM_PACKS=$1
8 {
9 cat <<-EOF &&
10 - header: 4d494458 1 1 $NUM_PACKS
11 - chunks: pack-names
10 + header: 4d494458 1 2 $NUM_PACKS
11 + chunks: pack-names oid-lookup
12 packs:
13 EOF
14 if test $NUM_PACKS -ge 1