File Coverage

lib/Archive/CAR/Indexer.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1 2     2   25 use v5.40;
  2         6  
2 2     2   10 use feature 'class';
  2         2  
  2         297  
3 2     2   12 no warnings 'experimental::class';
  2         1  
  2         248  
4             #
5             class Archive::CAR::Indexer v0.0.4 {
6 2     2   48 use Archive::CAR::Utils;
  2         4  
  2         991  
7              
8             # Simple index format (Format 0x0400: Multihash -> Offset)
9             method generate_index ($blocks) {
10              
11             # Index Header
12             # codec (varint)
13             my $index_codec = 0x0400; # IndexSorted
14             my $data = Archive::CAR::Utils::encode_varint($index_codec);
15              
16             # Format 0x0400:
17             # bucket_count (uint32 LE)
18             # For each bucket:
19             # digest_length (uint32 LE)
20             # entry_count (uint32 LE)
21             # entries: [digest (digest_length bytes), offset (uint64 LE)]
22             # Group blocks by digest length
23             my %buckets;
24             for my $block (@$blocks) {
25             my $digest = $block->{cid}->digest;
26             push @{ $buckets{ length($digest) } }, $block;
27             }
28             my @sorted_lengths = sort { $a <=> $b } keys %buckets;
29             $data .= pack( 'V', scalar @sorted_lengths );
30             for my $len (@sorted_lengths) {
31             my @sorted_blocks = sort { $a->{cid}->digest cmp $b->{cid}->digest } @{ $buckets{$len} };
32             $data .= pack( 'V V', $len, scalar @sorted_blocks );
33             for my $block (@sorted_blocks) {
34             $data .= $block->{cid}->digest;
35             $data .= pack( 'Q<', $block->{offset} );
36             }
37             }
38             return $data;
39             }
40             };
41             #
42             1;