SQLite/BlobStore.pm

filter: Statementstmt 38.4% 50 / 130 Branchbran 9.6% 6 / 62 Conditioncond 8.5% 4 / 47 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 50.0% 13 / 26 Podpod 100% 6 / 6 totaltotal 26.6% 79 / 296 CC 47 SCAR 30.2
CC
47
Cov
25%
_range_options
56.0
prepare
56.0
write
30.0
CRAP 20.4
↑ Summary SQLite/MetadataStore.pm »
1 package Net::Blossom::Server::Backend::SQLite::BlobStore;
2  
3 4 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Class::Tiny qw(dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod documented
11 6 my $class = shift;
12 6 my %args = _constructor_args(@_);
13 6 my @unknown = grep { $_ ne 'dbh' } keys %args;
14 6 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 6 6
15 6 return { dbh => _validate_dbh($args{dbh}) };
16 }
17  
18 4 ▶sub deploy_schema {
Subroutine deploy_schema: called CC=1 SCAR=0.0
Pod documented
19 4 my ($self) = @_;
20 4 $self->dbh->do(q{
21 CREATE TABLE IF NOT EXISTS blossom_blob_data (
22 storage_key TEXT PRIMARY KEY NOT NULL,
23 body BLOB NOT NULL
24 )
25 });
26 4 return 1;
27 }
28  
29 0 ▶sub begin_upload {
Subroutine begin_upload: not called CC=2 SCAR=17.9
Pod documented
30 0 my ($self, %context) = @_;
31 0 my ($fh, $path) = tempfile('net-blossom-sqlite-upload-XXXXXX', TMPDIR => 1, UNLINK => 0);
32 0 ▶ binmode $fh or croak "unable to binmode upload temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("unable to binmode upload temp file: $!") 0 0 0
33  
34 0 return Net::Blossom::Server::Backend::SQLite::BlobStore::_Upload->new(
35 store => $self,
36 fh => $fh,
37 path => $path,
38 );
39 }
40  
41 1 ▶sub get_blob {
Subroutine get_blob: called CC=1 SCAR=0.0
Pod documented
42 1 my ($self, $storage_key) = @_;
43 1 my ($body) = $self->dbh->selectrow_array(
44 q{SELECT body FROM blossom_blob_data WHERE storage_key = ?},
45 undef,
46 $storage_key,
47 );
48 1 return $body;
49 }
50  
51 0 ▶sub get_blob_range {
Subroutine get_blob_range: not called CC=4 SCAR=30.0
Pod documented
52 0 my ($self, $storage_key, %opts) = @_;
53 0 my %known = map { $_ => 1 } qw(offset length size);
54 0 my @unknown = grep { !$known{$_} } keys %opts;
55 0 ▶ croak "unknown option(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
56 0 _range_options(\%opts);
57 croak "range exceeds BLOB size"
58 if defined $opts{size}
59 0 ▶ && $opts{offset} + $opts{length} > $opts{size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"size"} and $opts{"offset"} + $opts{"length"} > $opts{"size"} 0 0 0
Condition0%0/3
defined $opts{"size"} and $opts{"offset"} + $opts{"length"} > $opts{"size"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"size"} and $opts{"offset"} + $opts{"length"} > $opts{"size"}
defined $opts{"size"} $opts{"offset"} + $opts{"length"} > $opts{"size"}
Truth table0%0/3
A and B
A= defined $opts{"size"}B= $opts{"offset"} + $opts{"length"} > $opts{"size"}
ABresult
0X0
100
111
60  
61 my ($body) = $self->dbh->selectrow_array(
62 q{SELECT substr(CAST(body AS BLOB), ?, ?) FROM blossom_blob_data WHERE storage_key = ?},
63 undef,
64 $opts{offset} + 1,
65 $opts{length},
66 0 $storage_key,
67 );
68 0 return $body;
69 }
70  
71 0 ▶sub delete_blob {
Subroutine delete_blob: not called CC=3 SCAR=24.8
Pod documented
72 0 my ($self, $storage_key) = @_;
73 croak "blob deletion requires an active transaction"
74 0 ▶ if $self->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->dbh->{"AutoCommit"} 0 0 0
75 0 my $rows = $self->dbh->do(
76 q{DELETE FROM blossom_blob_data WHERE storage_key = ?},
77 undef,
78 $storage_key,
79 );
80 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
81 }
82  
83 6 ▶sub _validate_dbh {
Subroutine _validate_dbh: called CC=7 SCAR=22.6
84 6 my ($dbh) = @_;
85 6 ▶ croak "dbh must be a DBI database handle"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array") 0 6 6
Condition33%2/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
!ll&&!rl&&r
006
builtin::blessed($dbh) and $dbh->can("do")
!ll&&!rl&&r
006
MC/DC0%0/3
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
builtin::blessed($dbh) $dbh->can("do") $dbh->can("selectrow_array")
Truth table25%1/4
A and B and C
A= builtin::blessed($dbh)B= $dbh->can("do")C= $dbh->can("selectrow_array")
ABCresult
0XX0
10X0
1100
1111
86 unless blessed($dbh) && $dbh->can('do') && $dbh->can('selectrow_array');
87 6 my $driver = eval { $dbh->{Driver}{Name} };
88 6 ▶ croak "dbh must be a SQLite DBI handle"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver and $driver eq "SQLite" 0 6 6
Condition33%1/3
defined $driver and $driver eq "SQLite"
!ll&&!rl&&r
006
MC/DC0%0/2
defined $driver and $driver eq "SQLite"
defined $driver $driver eq "SQLite"
Truth table33%1/3
A and B
A= defined $driverB= $driver eq "SQLite"
ABresult
0X0
100
111
89 unless defined $driver && $driver eq 'SQLite';
90 6 ▶ croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 6 6
91 6 $dbh->{RaiseError} = 1;
92 6 $dbh->{PrintError} = 0;
93 6 return $dbh;
94 }
95  
96 0 ▶sub _changed_rows {
Subroutine _changed_rows: not called CC=3 SCAR=24.8
97 0 my ($rows) = @_;
98 0 ▶ return defined $rows && $rows ne '0E0' && $rows > 0;
Condition0%0/6
defined $rows && $rows ne '0E0' && $rows > 0
!ll&&!rl&&r
000
defined $rows and $rows ne "0E0"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $rows && $rows ne '0E0' && $rows > 0
defined $rows $rows ne "0E0" $rows > 0
Truth table0%0/4
A and B && C
A= defined $rowsB= $rows ne "0E0"C= $rows > 0
ABCresult
0XX0
10X0
1100
1111
99 }
100  
101 6 ▶sub _constructor_args {
Subroutine _constructor_args: called CC=4 SCAR=17.1
102 0 ▶ return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 6 6
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
600
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
103 6 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 6 6
104 6 return @_;
105 }
106  
107 0 ▶sub _range_options {
Subroutine _range_options: not called CC=7 SCAR=40.3
108 0 my ($opts) = @_;
109 croak "offset must be a non-negative integer"
110 unless defined $opts->{offset}
111 && !ref($opts->{offset})
112 0 ▶ && $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 0 0
Condition0%0/6
defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
000
defined $opts->{"offset"} and not ref $opts->{"offset"}
!ll&&!rl&&r
000
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 table0%0/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
113 croak "length must be a positive integer"
114 unless defined $opts->{length}
115 && !ref($opts->{length})
116 0 ▶ && $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 0 0
Condition0%0/6
defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
000
defined $opts->{"length"} and not ref $opts->{"length"}
!ll&&!rl&&r
000
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 table0%0/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
117 0 return;
118 }
119  
120 {
121 package Net::Blossom::Server::Backend::SQLite::BlobStore::_Upload;
122  
123 4 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 4 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
126 4 ▶ use Class::Tiny qw(store fh path storage_key), {
Subroutine BEGIN: called CC=1 SCAR=0.0
127 prepared => 0,
128 committed => 0,
129 aborted => 0,
130 4 };
131  
132 0 ▶ sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
133 0 my ($self) = @_;
134 0 $self->prepared;
135 0 $self->committed;
136 0 $self->aborted;
137 0 return;
138 }
139  
140 0 ▶ sub write {
Subroutine write: not called CC=5 SCAR=34.0
141 0 my ($self, $chunk) = @_;
142 0 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 0 0
143 0 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
144 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
145 0 ▶ print {$self->{fh}} $chunk or croak "storage write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"fh"};} $chunk or croak("storage write failed: $!") 0 0 0
146 0 return length $chunk;
147 }
148  
149 0 ▶ sub prepare {
Subroutine prepare: not called CC=7 SCAR=40.3
150 0 my ($self, %metadata) = @_;
151 0 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 0 0
152 0 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
153 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
154 croak "blob preparation requires an active transaction"
155 0 ▶ if $self->{store}->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"store"}->dbh->{"AutoCommit"} 0 0 0
156  
157 0 $self->_close;
158 open my $fh, '<:raw', $self->{path}
159 0 ▶ or croak "unable to read upload temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $self->{"path"} or croak("unable to read upload temp file: $!") 0 0 0
160 0 my $body = do { local $/; <$fh> };
161 0 ▶ close $fh or croak "unable to close upload temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("unable to close upload temp file: $!") 0 0 0
162  
163 0 my $storage_key = $metadata{sha256};
164 $self->{store}->dbh->do(
165 0 q{INSERT OR IGNORE INTO blossom_blob_data (storage_key, body) VALUES (?, CAST(? AS BLOB))},
166 undef,
167 $storage_key,
168 $body,
169 );
170 0 $self->{storage_key} = $storage_key;
171 0 $self->{prepared} = 1;
172 0 return $storage_key;
173 }
174  
175 0 ▶ sub commit {
Subroutine commit: not called CC=4 SCAR=30.0
176 0 my ($self) = @_;
177 0 ▶ return 1 if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
178 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
179 0 ▶ croak "upload is not prepared" unless $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"prepared"} 0 0 0
180 0 $self->{committed} = 1;
181 0 $self->_cleanup;
182 0 return 1;
183 }
184  
185 0 ▶ sub abort {
Subroutine abort: not called CC=3 SCAR=24.8
186 0 my ($self) = @_;
187 0 ▶ return 1 if $self->{aborted} || $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} or $self->{"committed"} 0 0 0
Condition0%0/3
$self->{"aborted"} or $self->{"committed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"aborted"} or $self->{"committed"}
$self->{"aborted"} $self->{"committed"}
Truth table0%0/3
A or B
A= $self->{"aborted"}B= $self->{"committed"}
ABresult
1X1
011
000
188 0 $self->{aborted} = 1;
189 0 $self->_cleanup;
190 0 return 1;
191 }
192  
193 0 ▶ sub _cleanup {
Subroutine _cleanup: not called CC=4 SCAR=30.0
194 0 my ($self) = @_;
195 0 $self->_close;
196 unlink $self->{path}
197 or croak "unable to remove upload temp file: $!"
198 0 ▶ if defined $self->{path} && -e $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"path"} and -e $self->{"path"} 0 0 0
Condition0%0/8
defined $self->{'path'} && -e $self->{'path'} && (unlink($self->{'path'}) || croak("unable to remove upload temp file: $!"))
!ll&&!rl&&r
000
defined $self->{"path"} and -e $self->{"path"}
!ll&&!rl&&r
000
unlink $self->{"path"} or croak("unable to remove upload temp file: $!")
l!l
00
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
199 0 $self->{path} = undef;
200 0 return 1;
201 }
202  
203 0 ▶ sub _close {
Subroutine _close: not called CC=3 SCAR=24.8
204 0 my ($self) = @_;
205 0 ▶ return 1 unless defined $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fh"} 0 0 0
206 0 ▶ 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: $!") 0 0 0
207 0 $self->{fh} = undef;
208 0 return 1;
209 }
210  
211 0 ▶ sub DEMOLISH {
Subroutine DEMOLISH: not called CC=3 SCAR=24.8
212 0 my ($self) = @_;
213 0 ▶ return if $self->{committed} || $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} or $self->{"aborted"} 0 0 0
Condition0%0/3
$self->{"committed"} or $self->{"aborted"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"committed"} or $self->{"aborted"}
$self->{"committed"} $self->{"aborted"}
Truth table0%0/3
A or B
A= $self->{"committed"}B= $self->{"aborted"}
ABresult
1X1
011
000
214 0 eval { $self->abort };
215 0 return;
216 }
217 }
218  
219 4 1;
220  
221 =pod
222  
223 =head1 NAME
224  
225 Net::Blossom::Server::Backend::SQLite::BlobStore - SQLite Blossom blob bytes
226  
227 =head1 DESCRIPTION
228  
229 This module implements L<Net::Blossom::Server::BlobStore> using SQLite C<BLOB>
230 values. It stores bytes by opaque storage key and contains no descriptor or
231 owner logic.
232  
233 Use the same DBI handle for the paired
234 L<Net::Blossom::Server::Backend::SQLite::MetadataStore> so their changes share
235 one transaction.
236  
237 =head1 CONSTRUCTOR
238  
239 =head2 new
240  
241 my $blobs = Net::Blossom::Server::Backend::SQLite::BlobStore->new(
242 dbh => $dbh,
243 );
244  
245 Creates a blob store using a SQLite DBI handle with C<AutoCommit> enabled.
246  
247 =head1 METHODS
248  
249 =head2 dbh
250  
251 Returns the SQLite DBI handle.
252  
253 =head2 deploy_schema
254  
255 Creates the blob byte table.
256  
257 =head2 begin_upload
258  
259 Starts a staged upload implementing the L<Net::Blossom::Server::BlobStore>
260 upload contract. Its C<prepare> method requires an active transaction on C<dbh>;
261 the top-level SQLite backend manages this transaction.
262  
263 =head2 get_blob
264  
265 Returns bytes by storage key or C<undef>.
266  
267 =head2 get_blob_range
268  
269 Uses SQLite C<substr> to return the requested zero-based C<offset> and positive
270 C<length> without materializing the complete BLOB in Perl. Returns C<undef> when
271 the storage key is absent. When C<size> is supplied, a range beyond it is
272 rejected.
273  
274 =head2 delete_blob
275  
276 Deletes bytes by storage key and reports whether they existed. It requires an
277 active transaction on C<dbh>.
278  
279 =head2 BUILDARGS
280  
281 Validates and normalizes constructor arguments for Class::Tiny.
282  
283 =cut
↑ Summary SQLite/MetadataStore.pm »