| 1 | package Brackup::CompositeChunk; | ||||||||||||
| 2 | |||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 13 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 13 | ▶ | use Fcntl qw(SEEK_SET); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 13 | ▶ | use Brackup::Util qw(tempfile_obj io_sha1 io_print_to_fh); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 13 | ▶ | use fields ( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 13 | 'used_up', | |||||||||||
| 11 | 'max_size', | ||||||||||||
| 12 | 'target', | ||||||||||||
| 13 | 'digest', # memoized | ||||||||||||
| 14 | 'finalized', # if we've written ourselves to the target yet | ||||||||||||
| 15 | 'subchunks', # the chunks this composite chunk is made of | ||||||||||||
| 16 | 'sha1', # Digest::SHA1 object | ||||||||||||
| 17 | '_chunk_fh', # tempfile file containing the whole composite chunk | ||||||||||||
| 18 | 13 | ); | |||||||||||
| 19 | |||||||||||||
| 20 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 21 | 1 | my ($class, $root, $target) = @_; | |||||||||||
| 22 | 1 | ▶ | my $self = ref $class ? $class : fields::new($class); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 1 | $self->{used_up} = 0; # bytes | |||||||||||
| 24 | 1 | $self->{finalized} = 0; # false | |||||||||||
| 25 | 1 | $self->{max_size} = $root->max_composite_size; | |||||||||||
| 26 | 1 | $self->{target} = $target; | |||||||||||
| 27 | 1 | $self->{subchunks} = []; | |||||||||||
| 28 | 1 | $self->{sha1} = Digest::SHA1->new; | |||||||||||
| 29 | 1 | $self->{_chunk_fh} = tempfile_obj(); | |||||||||||
| 30 | 1 | return $self; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 5 | ▶ | sub append_little_chunk { | ||||||||||
|
Subroutine
append_little_chunk: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 34 | 5 | my ($self, $schunk) = @_; | |||||||||||
| 35 | 5 | ▶ | die "ASSERT" if $self->{digest}; # its digest was already requested? | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | |||||||||||||
| 37 | 5 | my $from = $self->{used_up}; | |||||||||||
| 38 | 5 | $self->{used_up} += $schunk->backup_length; | |||||||||||
| 39 | 5 | io_print_to_fh($schunk->chunkref, $self->{_chunk_fh}, $self->{sha1}); | |||||||||||
| 40 | 5 | my $to = $self->{used_up}; | |||||||||||
| 41 | |||||||||||||
| 42 | 5 | $schunk->set_composite_chunk($self, $from, $to); | |||||||||||
| 43 | 5 | push @{$self->{subchunks}}, $schunk; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 8 | ▶ | sub digest { | ||||||||||
|
Subroutine
digest: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 47 | 8 | my $self = shift; | |||||||||||
| 48 | 8 | ▶ | return $self->{digest} ||= "sha1:" . $self->{sha1}->hexdigest; | ||||||||||
|
Condition100%2/2
$self->{"digest"} ||= 'sha1:' . $self->{"sha1"}->hexdigest
MC/DC100%1/1
$self->{"digest"} ||= 'sha1:' . $self->{"sha1"}->hexdigest
$self->{"digest"}
Truth table100%2/2
A
A= $self->{"digest"}
| |||||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 4 | ▶ | sub can_fit { | ||||||||||
|
Subroutine
can_fit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 52 | 4 | my ($self, $len) = @_; | |||||||||||
| 53 | 4 | return $len <= ($self->{max_size} - $self->{used_up}); | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | # return on success; die on any failure | ||||||||||||
| 57 | 1 | ▶ | sub finalize { | ||||||||||
|
Subroutine
finalize: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||
| 58 | 1 | my $self = shift; | |||||||||||
| 59 | 1 | ▶ | die "ASSERT" if $self->{finalized}++; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | |||||||||||||
| 61 | 1 | ▶ | $self->{target}->store_chunk($self) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | or die "chunk storage of composite chunk failed.\n"; | ||||||||||||
| 63 | |||||||||||||
| 64 | 1 | foreach my $schunk (@{$self->{subchunks}}) { | |||||||||||
| 65 | 5 | $self->{target}->add_to_inventory($schunk->pchunk => $schunk); | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 1 | $self->forget_chunkref; | |||||||||||
| 69 | |||||||||||||
| 70 | 1 | return 1; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | 11 | ▶ | sub stored_chunk_from_dup_internal_raw { | ||||||||||
|
Subroutine
stored_chunk_from_dup_internal_raw: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 74 | 11 | my ($self, $pchunk) = @_; | |||||||||||
| 75 | 11 | my $ikey = $pchunk->inventory_key; | |||||||||||
| 76 | 11 | foreach my $schunk (@{$self->{subchunks}}) { | |||||||||||
| 77 | 28 | ▶ | next unless $schunk->pchunk->inventory_key eq $ikey; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | # match! found a duplicate within ourselves | ||||||||||||
| 79 | 2 | return $schunk->clone_but_for_pchunk($pchunk); | |||||||||||
| 80 | } | ||||||||||||
| 81 | 9 | return undef; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | # <duck-typing> | ||||||||||||
| 85 | # make this duck-typed like a StoredChunk, so targets can store it | ||||||||||||
| 86 | 13 | *backup_digest = \&digest; | |||||||||||
| 87 | 9 | ▶ | sub backup_length { | ||||||||||
|
Subroutine
backup_length: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 88 | 9 | my $self = shift; | |||||||||||
| 89 | 9 | return $self->{used_up}; | |||||||||||
| 90 | } | ||||||||||||
| 91 | # return handle to data | ||||||||||||
| 92 | 1 | ▶ | sub chunkref { | ||||||||||
|
Subroutine
chunkref: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 93 | 1 | my $self = shift; | |||||||||||
| 94 | 1 | ▶ | croak "ASSERT: _chunk_fh not opened" unless $self->{_chunk_fh}->opened; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 1 | seek($self->{_chunk_fh}, 0, SEEK_SET); | |||||||||||
| 96 | 1 | return $self->{_chunk_fh}; | |||||||||||
| 97 | } | ||||||||||||
| 98 | 0 | ▶ | sub inventory_value { | ||||||||||
|
Subroutine
inventory_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 99 | 0 | die "ASSERT: don't expect this to be called"; | |||||||||||
| 100 | } | ||||||||||||
| 101 | # called when chunk data not needed anymore | ||||||||||||
| 102 | 1 | ▶ | sub forget_chunkref { | ||||||||||
|
Subroutine
forget_chunkref: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||
| 103 | 1 | my $self = shift; | |||||||||||
| 104 | 1 | ▶ | if ($self->{_chunk_fh}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | die "ASSERT: used_up: $self->{used_up}, size: " . -s $self->{_chunk_fh}->filename | ||||||||||||
| 106 | 1 | ▶ | unless -s $self->{_chunk_fh}->filename == $self->{used_up}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 1 | close $self->{_chunk_fh}; | |||||||||||
| 108 | 1 | delete $self->{_chunk_fh}; # also deletes the temp file | |||||||||||
| 109 | } | ||||||||||||
| 110 | } | ||||||||||||
| 111 | # </duck-typing> | ||||||||||||
| 112 | |||||||||||||
| 113 | 13 | 1; | |||||||||||