S3/BlobStore.pm

filter: Statementstmt 93.9% 202 / 215 Branchbran 69.3% 86 / 124 Conditioncond 45.0% 32 / 71 MC/DCmcdc 18.4% 7 / 38 Subroutinesub 94.8% 37 / 39 Podpod 100% 6 / 6 totaltotal 75.0% 370 / 493 CC 87 SCAR 23.6
CC
87
Cov
78%
BUILDARGS
32.2
read
13.2
_range_options
13.1
CRAP 10.6
« S3.pm ↑ Summary
1 package Net::Blossom::Server::Backend::S3::BlobStore;
2  
3 7 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Class::Tiny qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 client temp_dir prefix range_size multipart_threshold multipart_part_size
8 generation
9 7 );
10 7 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Net::Blossom::Server::Backend::S3::_Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 my $MIB = 1024 * 1024;
17 7 my $GIB = 1024 * $MIB;
18  
19 7 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=22 SCAR=34.7
Pod documented
20 7 my $class = shift;
21 7 my %args = _constructor_args(@_);
22 7 my %known = map { $_ => 1 } qw(
23 client bucket endpoint region access_key_id secret_access_key
24 session_token path_style s3 timeout retry temp_dir prefix range_size
25 multipart_threshold multipart_part_size generation
26 );
27 7 my @unknown = grep { !$known{$_} } keys %args;
28 7 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 7 7
29  
30 7 my $client = delete $args{client};
31 7 ▶ if (defined $client) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client) { } 7 0 7
32 7 for my $method (qw(upload_file head get_range delete)) {
33 28 ▶ croak "client must provide $method"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($client) and $client->can($method) 0 28 28
Condition33%1/3
builtin::blessed($client) and $client->can($method)
!ll&&!rl&&r
0028
MC/DC0%0/2
builtin::blessed($client) and $client->can($method)
builtin::blessed($client) $client->can($method)
Truth table33%1/3
A and B
A= builtin::blessed($client)B= $client->can($method)
ABresult
0X0
100
111
34 unless blessed($client) && $client->can($method);
35 }
36 my @client_args = grep {
37 7 exists $args{$_}
38 } qw(bucket endpoint region access_key_id secret_access_key session_token path_style s3 timeout retry);
39 7 ▶ croak "client cannot be combined with S3 connection arguments"
Branch: 1 branch
BranchTrueFalseTotal
if @client_args 0 7 7
40 if @client_args;
41 }
42 else {
43 my %client_args = map {
44 0 ▶ exists $args{$_} ? ($_ => delete $args{$_}) : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} ? : 0 0 0
45 } qw(bucket endpoint region access_key_id secret_access_key session_token path_style s3 timeout retry);
46 0 $client = Net::Blossom::Server::Backend::S3::_Client->new(%client_args);
47 }
48  
49 7 ▶ my $temp_dir = defined $args{temp_dir} ? $args{temp_dir} : File::Spec->tmpdir;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'temp_dir'} ? : 1 6 7
50 7 ▶ croak "temp_dir must be a writable directory"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $temp_dir and -d $temp_dir and -w $temp_dir 0 7 7
Condition33%2/6
not ref $temp_dir and -d $temp_dir and -w $temp_dir
!ll&&!rl&&r
007
not ref $temp_dir and -d $temp_dir
!ll&&!rl&&r
007
MC/DC0%0/3
not ref $temp_dir and -d $temp_dir and -w $temp_dir
not ref $temp_dir -d $temp_dir -w $temp_dir
Truth table25%1/4
not(A) and B and C
A= not ref $temp_dirB= -d $temp_dirC= -w $temp_dir
ABCresult
0XX0
10X0
1100
1111
51 unless !ref($temp_dir) && -d $temp_dir && -w $temp_dir;
52  
53 7 ▶ my $prefix = defined $args{prefix} ? $args{prefix} : 'blossom';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'prefix'} ? : 1 6 7
54 7 ▶ croak "prefix must be a scalar" if ref($prefix);
Branch: 1 branch
BranchTrueFalseTotal
if ref $prefix 0 7 7
55 7 $prefix =~ s{\A/+}{};
56 7 $prefix =~ s{/+\z}{};
57 7 ▶ croak "prefix contains an unsafe path segment"
Branch: 1 branch
BranchTrueFalseTotal
if $prefix =~ m[(?:\A|/)\.\.?\z|(?:\A|/)\.\.?/|[\x00-\x1f\x7f]] 0 7 7
58 if $prefix =~ m{(?:\A|/)\.\.?\z|(?:\A|/)\.\.?/|[\x00-\x1f\x7f]};
59  
60 my $range_size = _positive_integer(
61 7 ▶ defined $args{range_size} ? $args{range_size} : 8 * $MIB,
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'range_size'} ? : 3 4 7
62 'range_size',
63 );
64 my $multipart_threshold = _positive_integer(
65 7 ▶ defined $args{multipart_threshold} ? $args{multipart_threshold} : 100 * $MIB,
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'multipart_threshold'} ? : 0 7 7
66 'multipart_threshold',
67 );
68 my $multipart_part_size = _positive_integer(
69 7 ▶ defined $args{multipart_part_size} ? $args{multipart_part_size} : 16 * $MIB,
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'multipart_part_size'} ? : 1 6 7
70 'multipart_part_size',
71 );
72 7 ▶ croak "multipart_part_size must be at least 5 MiB"
Branch: 1 branch
BranchTrueFalseTotal
if $multipart_part_size < 5 * $MIB 0 7 7
73 if $multipart_part_size < 5 * $MIB;
74 7 ▶ croak "multipart_part_size must be at most 5 GiB"
Branch: 1 branch
BranchTrueFalseTotal
if $multipart_part_size > 5 * $GIB 1 6 7
75 if $multipart_part_size > 5 * $GIB;
76  
77 my $generation = defined $args{generation}
78 ? $args{generation}
79 0 ▶ : sub { return unpack 'H*', random_bytes(16) };
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'generation'} ? : 2 4 6
80 6 ▶ croak "generation must be a code reference" unless ref($generation) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $generation eq "CODE" 1 5 6
81  
82 return {
83 5 client => $client,
84 temp_dir => $temp_dir,
85 prefix => $prefix,
86 range_size => $range_size,
87 multipart_threshold => $multipart_threshold,
88 multipart_part_size => $multipart_part_size,
89 generation => $generation,
90 };
91 }
92  
93 1 ▶sub deploy_schema {
Subroutine deploy_schema: called CC=1 SCAR=0.0
Pod documented
94 1 return 1;
95 }
96  
97 6 ▶sub begin_upload {
Subroutine begin_upload: called CC=2 SCAR=7.0
Pod documented
98 6 my ($self) = @_;
99 6 my ($fh, $path) = tempfile(
100 'net-blossom-s3-upload-XXXXXX',
101 DIR => $self->temp_dir,
102 UNLINK => 0,
103 );
104 6 ▶ binmode $fh or croak "unable to binmode upload temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("unable to binmode upload temp file: $!") 6 0 6
105 6 return Net::Blossom::Server::Backend::S3::BlobStore::_Upload->new(
106 store => $self,
107 fh => $fh,
108 path => $path,
109 );
110 }
111  
112 7 ▶sub get_blob {
Subroutine get_blob: called CC=3 SCAR=11.0
Pod documented
113 7 my ($self, $storage_key, %opts) = @_;
114 7 my $size = $self->_object_size($storage_key, $opts{size});
115 6 ▶ return unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 1 5 6
116 5 ▶ return '' unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 1 4 5
117  
118 4 return Net::Blossom::Server::Backend::S3::BlobStore::_Stream->new(
119 client => $self->client,
120 storage_key => $storage_key,
121 size => $size,
122 range_size => $self->range_size,
123 );
124 }
125  
126 2 ▶sub get_blob_range {
Subroutine get_blob_range: called CC=4 SCAR=13.9
Pod documented
127 2 my ($self, $storage_key, %opts) = @_;
128 2 my %known = map { $_ => 1 } qw(offset length size);
129 2 my @unknown = grep { !$known{$_} } keys %opts;
130 2 ▶ croak "unknown option(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 2 2
131 2 _range_options(\%opts);
132  
133 2 my $size = $self->_object_size($storage_key, $opts{size});
134 2 ▶ return unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 2 2
135 croak "range exceeds object size"
136 2 ▶ if $opts{offset} + $opts{length} > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"offset"} + $opts{"length"} > $size 1 1 2
137  
138 return Net::Blossom::Server::Backend::S3::BlobStore::_Stream->new(
139 client => $self->client,
140 storage_key => $storage_key,
141 size => $opts{offset} + $opts{length},
142 range_size => $self->range_size,
143 offset => $opts{offset},
144 1 );
145 }
146  
147 9 ▶sub _object_size {
Subroutine _object_size: called CC=5 SCAR=16.1
148 9 my ($self, $storage_key, $expected) = @_;
149 9 my $head = $self->client->head($storage_key);
150 9 ▶ return unless defined $head;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $head 1 8 9
151 8 ▶ croak "object head response has no size" unless defined $head->{size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $head->{"size"} 0 8 8
152 8 my $size = 0 + $head->{size};
153 8 ▶ croak "object size does not match metadata"
Branch: 1 branch
BranchTrueFalseTotal
if defined $expected and $size != $expected 1 7 8
Condition100%3/3
defined $expected and $size != $expected
!ll&&!rl&&r
521
MC/DC100%2/2
defined $expected and $size != $expected
defined $expected $size != $expected
Truth table100%3/3
A and B
A= defined $expectedB= $size != $expected
ABresult
0X0
100
111
154 if defined $expected && $size != $expected;
155 7 return $size;
156 }
157  
158 2 ▶sub delete_blob {
Subroutine delete_blob: called CC=2 SCAR=6.9
Pod documented
159 2 my ($self, $storage_key) = @_;
160 2 ▶ return $self->client->delete($storage_key) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->client->delete($storage_key) ? : 1 1 2
161 }
162  
163 4 ▶sub _object_key {
Subroutine _object_key: called CC=7 SCAR=22.4
164 4 my ($self, $sha256) = @_;
165 4 ▶ croak "sha256 must be 64 lowercase hexadecimal characters"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sha256 and not ref $sha256 and $sha256 =~ /\A[0-9a-f]{64}\z/ 1 3 4
Condition50%3/6
defined $sha256 and not ref $sha256 and $sha256 =~ /\A[0-9a-f]{64}\z/
!ll&&!rl&&r
013
defined $sha256 and not ref $sha256
!ll&&!rl&&r
004
MC/DC33%1/3
defined $sha256 and not ref $sha256 and $sha256 =~ /\A[0-9a-f]{64}\z/
defined $sha256 not ref $sha256 $sha256 =~ /\A[0-9a-f]{64}\z/
Truth table50%2/4
A and not(B) and C
A= defined $sha256B= not ref $sha256C= $sha256 =~ /\A[0-9a-f]{64}\z/
ABCresult
0XX0
10X0
1100
1111
166 unless defined $sha256 && !ref($sha256) && $sha256 =~ /\A[0-9a-f]{64}\z/;
167 3 my $generation = $self->generation->();
168 3 ▶ croak "generation returned an unsafe object-key segment"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $generation and not ref $generation and $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/ 0 3 3
Condition33%2/6
defined $generation and not ref $generation and $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
!ll&&!rl&&r
003
defined $generation and not ref $generation
!ll&&!rl&&r
003
MC/DC0%0/3
defined $generation and not ref $generation and $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
defined $generation not ref $generation $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
Truth table25%1/4
A and not(B) and C
A= defined $generationB= not ref $generationC= $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
ABCresult
0XX0
10X0
1100
1111
169 unless defined $generation
170 && !ref($generation)
171 && $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/;
172 3 return join '/', grep { length } $self->prefix, $sha256, $generation;
173 }
174  
175 21 ▶sub _positive_integer {
Subroutine _positive_integer: called CC=4 SCAR=17.1
176 21 my ($value, $name) = @_;
177 21 ▶ croak "$name must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or not $value =~ /\A[0-9]+\z/ or $value < 1 0 21 21
Condition33%2/6
ref $value or not $value =~ /\A[0-9]+\z/ or $value < 1
l!l&&r!l&&!r
0021
ref $value or not $value =~ /\A[0-9]+\z/
l!l&&r!l&&!r
0021
MC/DC0%0/3
ref $value or not $value =~ /\A[0-9]+\z/ or $value < 1
ref $value not $value =~ /\A[0-9]+\z/ $value < 1
Truth table25%1/4
A or not(B) or C
A= ref $valueB= not $value =~ /\A[0-9]+\z/C= $value < 1
ABCresult
1XX1
01X1
0011
0000
178 if ref($value) || $value !~ /\A[0-9]+\z/ || $value < 1;
179 21 return 0 + $value;
180 }
181  
182 7 ▶sub _constructor_args {
Subroutine _constructor_args: called CC=4 SCAR=17.1
183 0 ▶ return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 7 7
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
700
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
184 7 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 7 7
185 7 return @_;
186 }
187  
188 2 ▶sub _range_options {
Subroutine _range_options: called CC=7 SCAR=25.7
189 2 my ($opts) = @_;
190 croak "offset must be a non-negative integer"
191 unless defined $opts->{offset}
192 && !ref($opts->{offset})
193 2 ▶ && $opts->{offset} =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/ 0 2 2
Condition33%2/6
defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
002
defined $opts->{"offset"} and not ref $opts->{"offset"}
!ll&&!rl&&r
002
MC/DC0%0/3
defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/
defined $opts->{"offset"} not ref $opts->{"offset"} $opts->{"offset"} =~ /\A[0-9]+\z/
Truth table25%1/4
A and not(B) and C
A= defined $opts->{"offset"}B= not ref $opts->{"offset"}C= $opts->{"offset"} =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
194 croak "length must be a positive integer"
195 unless defined $opts->{length}
196 && !ref($opts->{length})
197 2 ▶ && $opts->{length} =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/ 0 2 2
Condition33%2/6
defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
002
defined $opts->{"length"} and not ref $opts->{"length"}
!ll&&!rl&&r
002
MC/DC0%0/3
defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/
defined $opts->{"length"} not ref $opts->{"length"} $opts->{"length"} =~ /\A[1-9][0-9]*\z/
Truth table25%1/4
A and not(B) and C
A= defined $opts->{"length"}B= not ref $opts->{"length"}C= $opts->{"length"} =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
10X0
1100
1111
198 2 return;
199 }
200  
201 {
202 package Net::Blossom::Server::Backend::S3::BlobStore::_Upload;
203  
204 7 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
205  
206 7 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
207 7 ▶ use Class::Tiny qw(store fh path storage_key), {
Subroutine BEGIN: called CC=1 SCAR=0.0
208 prepared => 0,
209 committed => 0,
210 aborted => 0,
211 7 };
212  
213 6 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
214 6 my ($self) = @_;
215 6 $self->prepared;
216 6 $self->committed;
217 6 $self->aborted;
218 6 return;
219 }
220  
221 7 ▶ sub write {
Subroutine write: called CC=5 SCAR=17.0
222 7 my ($self, $chunk) = @_;
223 7 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 7 7
224 7 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 7 7
225 7 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 7 7
226 7 ▶ print {$self->{fh}} $chunk or croak "storage write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"fh"};} $chunk or croak("storage write failed: $!") 7 0 7
227 7 return length $chunk;
228 }
229  
230 5 ▶ sub prepare {
Subroutine prepare: called CC=7 SCAR=19.9
231 5 my ($self, %metadata) = @_;
232 5 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 5 5
233 5 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 5 5
234 5 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 5 5
235 5 $self->_close;
236  
237 5 my $size = -s $self->{path};
238 5 ▶ croak "unable to stat upload temp file: $!" unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 5 5
239 croak "upload size does not match metadata"
240 5 ▶ unless defined $metadata{size} && $metadata{size} == $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $metadata{"size"} and $metadata{"size"} == $size 1 4 5
Condition66%2/3
defined $metadata{"size"} and $metadata{"size"} == $size
!ll&&!rl&&r
014
MC/DC50%1/2
defined $metadata{"size"} and $metadata{"size"} == $size
defined $metadata{"size"} $metadata{"size"} == $size
Truth table66%2/3
A and B
A= defined $metadata{"size"}B= $metadata{"size"} == $size
ABresult
0X0
100
111
241 4 my $storage_key = $self->{store}->_object_key($metadata{sha256});
242  
243 $self->{store}->client->upload_file(
244 key => $storage_key,
245 path => $self->{path},
246 size => $size,
247 content_type => $metadata{type},
248 sha256 => $metadata{sha256},
249 multipart_threshold => $self->{store}->multipart_threshold,
250 multipart_part_size => $self->{store}->multipart_part_size,
251 3 );
252  
253 2 $self->{storage_key} = $storage_key;
254 2 $self->{prepared} = 1;
255 2 return $storage_key;
256 }
257  
258 1 ▶ sub commit {
Subroutine commit: called CC=4 SCAR=14.3
259 1 my ($self) = @_;
260 1 ▶ return 1 if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 1 1
261 1 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 1 1
262 1 ▶ croak "upload is not prepared" unless $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"prepared"} 0 1 1
263 1 $self->{committed} = 1;
264 1 $self->_cleanup;
265 1 return 1;
266 }
267  
268 6 ▶ sub abort {
Subroutine abort: called CC=3 SCAR=11.0
269 6 my ($self) = @_;
270 6 ▶ return 1 if $self->{aborted} || $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} or $self->{"committed"} 1 5 6
Condition66%2/3
$self->{"aborted"} or $self->{"committed"}
l!l&&r!l&&!r
105
MC/DC50%1/2
$self->{"aborted"} or $self->{"committed"}
$self->{"aborted"} $self->{"committed"}
Truth table66%2/3
A or B
A= $self->{"aborted"}B= $self->{"committed"}
ABresult
1X1
011
000
271 5 $self->{aborted} = 1;
272 5 $self->_cleanup;
273 5 return 1;
274 }
275  
276 6 ▶ sub _cleanup {
Subroutine _cleanup: called CC=4 SCAR=16.1
277 6 my ($self) = @_;
278 6 $self->_close;
279 unlink $self->{path}
280 or croak "unable to remove upload temp file: $!"
281 6 ▶ if defined $self->{path} && -e $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"path"} and -e $self->{"path"} 6 0 6
Condition37%3/8
defined $self->{'path'} && -e $self->{'path'} && (unlink($self->{'path'}) || croak("unable to remove upload temp file: $!"))
!ll&&!rl&&r
060
defined $self->{"path"} and -e $self->{"path"}
!ll&&!rl&&r
006
unlink $self->{"path"} or croak("unable to remove upload temp file: $!")
l!l
60
MC/DC0%0/3
defined $self->{'path'} && -e $self->{'path'} && (unlink($self->{'path'}) || croak("unable to remove upload temp file: $!"))
defined $self->{"path"} -e $self->{"path"} unlink $self->{"path"}
Truth table0%0/4
A and B && C
A= defined $self->{"path"}B= -e $self->{"path"}C= unlink $self->{"path"}
ABCresult
0XX0
10X0
1100
1111
282 6 $self->{path} = undef;
283 6 return 1;
284 }
285  
286 11 ▶ sub _close {
Subroutine _close: called CC=3 SCAR=11.0
287 11 my ($self) = @_;
288 11 ▶ return 1 unless defined $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fh"} 5 6 11
289 6 ▶ close $self->{fh} or croak "unable to close upload temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"fh"} or croak("unable to close upload temp file: $!") 6 0 6
290 6 $self->{fh} = undef;
291 6 return 1;
292 }
293  
294 6 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=14.2
295 6 my ($self) = @_;
296 6 ▶ return if $self->{committed} || $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} or $self->{"aborted"} 6 0 6
Condition66%2/3
$self->{"committed"} or $self->{"aborted"}
l!l&&r!l&&!r
150
MC/DC0%0/2
$self->{"committed"} or $self->{"aborted"}
$self->{"committed"} $self->{"aborted"}
Truth table66%2/3
A or B
A= $self->{"committed"}B= $self->{"aborted"}
ABresult
1X1
011
000
297 0 eval { $self->abort };
298 0 return;
299 }
300 }
301  
302 {
303 7 package Net::Blossom::Server::Backend::S3::BlobStore::_Stream;
304  
305 7 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 7 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
308 7 ▶ use Class::Tiny qw(client storage_key size range_size), {
Subroutine BEGIN: called CC=1 SCAR=0.0
309 offset => 0,
310 buffer => '',
311 closed => 0,
312 7 };
313  
314 5 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
315 5 my ($self) = @_;
316 5 $self->offset;
317 5 $self->buffer;
318 5 $self->closed;
319 5 return;
320 }
321  
322 13 ▶ sub read {
Subroutine read: called CC=13 SCAR=25.8
323 13 my ($self, undef, $length) = @_;
324 13 ▶ croak "stream is closed" if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 12 13
325 12 ▶ croak "read length must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $length or ref $length or not $length =~ /\A[0-9]+\z/ 1 11 12
Condition50%3/6
not defined $length or ref $length or not $length =~ /\A[0-9]+\z/
l!l&&r!l&&!r
0111
not defined $length or ref $length
l!l&&r!l&&!r
0012
MC/DC33%1/3
not defined $length or ref $length or not $length =~ /\A[0-9]+\z/
not defined $length ref $length not $length =~ /\A[0-9]+\z/
Truth table50%2/4
not(A) or B or not(C)
A= not defined $lengthB= ref $lengthC= not $length =~ /\A[0-9]+\z/
ABCresult
1XX1
01X1
0011
0000
326 if !defined $length || ref($length) || $length !~ /\A[0-9]+\z/;
327 11 $_[1] = '';
328 11 ▶ return 0 unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 1 10 11
329 10 ▶ return 0 if $self->{offset} >= $self->{size};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"offset"} >= $self->{"size"} 2 8 10
330  
331 8 ▶ if (!length($self->{buffer}) && $self->{offset} < $self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $self->{"buffer"} and $self->{"offset"} < $self->{"size"}) 7 1 8
Condition66%2/3
not length $self->{"buffer"} and $self->{"offset"} < $self->{"size"}
!ll&&!rl&&r
107
MC/DC50%1/2
not length $self->{"buffer"} and $self->{"offset"} < $self->{"size"}
not length $self->{"buffer"} $self->{"offset"} < $self->{"size"}
Truth table66%2/3
not(A) and B
A= not length $self->{"buffer"}B= $self->{"offset"} < $self->{"size"}
ABresult
0X0
100
111
332 7 my $start = $self->{offset};
333 7 my $end = $start + $self->{range_size} - 1;
334 7 ▶ $end = $self->{size} - 1 if $end >= $self->{size};
Branch: 1 branch
BranchTrueFalseTotal
if $end >= $self->{"size"} 2 5 7
335 7 my $bytes = $self->{client}->get_range($self->{storage_key}, $start, $end);
336 7 ▶ croak "object disappeared during ranged read" unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 7 7
337 7 my $expected = $end - $start + 1;
338 7 ▶ croak "object range length does not match response"
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes == $expected 1 6 7
339 unless length($bytes) == $expected;
340 6 $self->{buffer} = $bytes;
341 }
342  
343 my $take = length($self->{buffer}) < $length
344 ? length($self->{buffer})
345 7 ▶ : $length;
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'buffer'} < $length ? : 6 1 7
346 7 $_[1] = substr($self->{buffer}, 0, $take, '');
347 7 $self->{offset} += $take;
348 7 return $take;
349 }
350  
351 0 ▶ sub getline {
Subroutine getline: not called CC=2 SCAR=17.9
352 0 my ($self) = @_;
353 0 my $chunk = '';
354 0 my $read = $self->read($chunk, $self->{range_size});
355 0 ▶ return unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 0 0
356 0 return $chunk;
357 }
358  
359 6 ▶ sub close {
Subroutine close: called CC=1 SCAR=0.0
360 6 my ($self) = @_;
361 6 $self->{closed} = 1;
362 6 $self->{buffer} = '';
363 6 return 1;
364 }
365  
366 5 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
367 5 my ($self) = @_;
368 5 ▶ $self->close unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 4 1 5
369 5 return;
370 }
371 }
372  
373 7 1;
374  
375 =pod
376  
377 =head1 NAME
378  
379 Net::Blossom::Server::Backend::S3::BlobStore - S3-compatible Blossom blob bytes
380  
381 =head1 DESCRIPTION
382  
383 This module implements L<Net::Blossom::Server::BlobStore> with an
384 S3-compatible object service. It is normally constructed by
385 L<Net::Blossom::Server::Backend::S3>.
386  
387 Uploads use local temporary files and generation-specific object keys.
388 Downloads are returned as bounded range streams. A read may return fewer bytes
389 than requested so memory remains bounded by the configured range size.
390  
391 =head1 CONSTRUCTOR
392  
393 =head2 new
394  
395 Accepts the S3 connection, staging, multipart, and range options documented by
396 L<Net::Blossom::Server::Backend::S3/new>.
397  
398 C<client> may provide a custom object client. It cannot be combined with S3
399 connection options.
400  
401 =head1 METHODS
402  
403 =head2 BUILDARGS
404  
405 Normalizes and validates constructor arguments for C<Class::Tiny>.
406  
407 =head2 client
408  
409 Returns the object client used for S3 requests.
410  
411 =head2 temp_dir
412  
413 Returns the upload staging directory.
414  
415 =head2 prefix
416  
417 Returns the object key prefix.
418  
419 =head2 range_size
420  
421 Returns the ranged-download chunk size in bytes.
422  
423 =head2 multipart_threshold
424  
425 Returns the configured multipart-upload threshold.
426  
427 =head2 multipart_part_size
428  
429 Returns the preferred multipart part size.
430  
431 =head2 generation
432  
433 Returns the callback used to create object-key generation segments.
434  
435 =head2 deploy_schema
436  
437 Returns true; object storage has no database schema.
438  
439 =head2 begin_upload
440  
441 Returns a file-backed blob upload writer.
442  
443 =head2 get_blob
444  
445 Returns C<''> for an empty object, a ranged stream for a nonempty object, or
446 C<undef> when the object is absent. When C<size> is supplied, a size mismatch
447 is an error.
448  
449 =head2 get_blob_range
450  
451 Returns a stream for the requested zero-based C<offset> and positive C<length>,
452 or C<undef> when the object is absent. Only the requested S3 byte ranges are
453 fetched. When C<size> is supplied, a size mismatch is an error.
454  
455 =head2 delete_blob
456  
457 Deletes an object and reports whether it existed.
458  
459 =cut
« S3.pm ↑ Summary