Postgres/BlobStore.pm

filter: Statementstmt 54.9% 144 / 262 Branchbran 36.9% 48 / 130 Conditioncond 21.6% 16 / 74 MC/DCmcdc 11.9% 5 / 42 Subroutinesub 51.2% 20 / 39 Podpod 100% 6 / 6 totaltotal 43.2% 239 / 553 CC 90 SCAR 29.8
CC
90
Cov
45%
_open_blob_stream
72.0
_range_options
56.0
prepare
56.0
CRAP 19.6
« Postgres/MetadataStore.pm ↑ Summary Postgres.pm »
1 package Net::Blossom::Server::Backend::Postgres::BlobStore;
2  
3 6 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Class::Tiny qw(dbh schema);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=14.5
Pod documented
11 2 my $class = shift;
12 2 my %args = _constructor_args(@_);
13 2 my @unknown = grep { $_ ne 'dbh' } keys %args;
14 2 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 2 2
15 2 my $dbh = _validate_dbh($args{dbh});
16 2 my ($schema) = $dbh->selectrow_array(q{SELECT current_schema()});
17 2 ▶ croak "Postgres connection has no current schema" unless defined $schema && length $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema and length $schema 0 2 2
Condition33%1/3
defined $schema and length $schema
!ll&&!rl&&r
002
MC/DC0%0/2
defined $schema and length $schema
defined $schema length $schema
Truth table33%1/3
A and B
A= defined $schemaB= length $schema
ABresult
0X0
100
111
18 2 return { dbh => $dbh, schema => $schema };
19 }
20  
21 0 ▶sub deploy_schema {
Subroutine deploy_schema: not called CC=1 SCAR=6.9
Pod documented
22 0 my ($self) = @_;
23 0 my $data = $self->_table('blossom_blob_data');
24 0 $self->dbh->do(qq{
25 CREATE TABLE IF NOT EXISTS $data (
26 storage_key text PRIMARY KEY NOT NULL,
27 body_oid oid NOT NULL
28 )
29 });
30 0 return 1;
31 }
32  
33 0 ▶sub begin_upload {
Subroutine begin_upload: not called CC=2 SCAR=17.9
Pod documented
34 0 my ($self, %context) = @_;
35 0 my ($fh, $path) = tempfile('net-blossom-postgres-upload-XXXXXX', TMPDIR => 1, UNLINK => 0);
36 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
37 0 return Net::Blossom::Server::Backend::Postgres::BlobStore::_Upload->new(
38 store => $self,
39 fh => $fh,
40 path => $path,
41 );
42 }
43  
44 0 ▶sub get_blob {
Subroutine get_blob: not called CC=1 SCAR=6.9
Pod documented
45 0 my ($self, $storage_key) = @_;
46 0 return $self->_open_blob_stream($storage_key);
47 }
48  
49 0 ▶sub get_blob_range {
Subroutine get_blob_range: not called CC=4 SCAR=30.0
Pod documented
50 0 my ($self, $storage_key, %opts) = @_;
51 0 my %known = map { $_ => 1 } qw(offset length size);
52 0 my @unknown = grep { !$known{$_} } keys %opts;
53 0 ▶ croak "unknown option(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
54 0 _range_options(\%opts);
55 croak "range exceeds large-object size"
56 if defined $opts{size}
57 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
58  
59 return $self->_open_blob_stream(
60 $storage_key,
61 offset => $opts{offset},
62 remaining => $opts{length},
63 0 );
64 }
65  
66 0 ▶sub _open_blob_stream {
Subroutine _open_blob_stream: not called CC=8 SCAR=42.8
67 0 my ($self, $storage_key, %stream_opts) = @_;
68 0 my $dbh = $self->_reader_dbh;
69 0 my $data = $dbh->quote_identifier($self->schema, 'blossom_blob_data');
70 0 my ($body_oid, $fd);
71  
72 0 my $ok = eval {
73 0 $dbh->begin_work;
74 0 ($body_oid) = $dbh->selectrow_array(
75 qq{SELECT body_oid FROM $data WHERE storage_key = ?},
76 undef,
77 $storage_key,
78 );
79 0 ▶ $fd = $dbh->pg_lo_open($body_oid, $dbh->{pg_INV_READ}) if defined $body_oid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body_oid 0 0 0
80 0 ▶ croak "unable to open PostgreSQL large object" if defined $body_oid && !defined $fd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body_oid and not defined $fd 0 0 0
Condition0%0/3
defined $body_oid and not defined $fd
!ll&&!rl&&r
000
MC/DC0%0/2
defined $body_oid and not defined $fd
defined $body_oid not defined $fd
Truth table0%0/3
A and not(B)
A= defined $body_oidB= not defined $fd
ABresult
0X0
100
111
81 0 1;
82 };
83  
84 0 ▶ if (!$ok || !defined $body_oid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and defined $body_oid) 0 0 0
Condition0%0/3
$ok and defined $body_oid
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and defined $body_oid
$ok defined $body_oid
Truth table0%0/3
A and B
A= $okB= defined $body_oid
ABresult
0X0
100
111
85 0 my $error = $@;
86 0 ▶ eval { $dbh->rollback unless $dbh->{AutoCommit} };
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 0 0
87 0 eval { $dbh->disconnect };
88 0 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
89 0 return;
90 }
91  
92 0 return Net::Blossom::Server::Backend::Postgres::BlobStore::_Stream->new(
93 dbh => $dbh,
94 fd => $fd,
95 %stream_opts,
96 );
97 }
98  
99 0 ▶sub delete_blob {
Subroutine delete_blob: not called CC=3 SCAR=24.8
Pod documented
100 0 my ($self, $storage_key) = @_;
101 croak "blob deletion requires an active transaction"
102 0 ▶ if $self->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->dbh->{"AutoCommit"} 0 0 0
103 0 my $data = $self->_table('blossom_blob_data');
104 0 my ($body_oid) = $self->dbh->selectrow_array(
105 qq{SELECT body_oid FROM $data WHERE storage_key = ?},
106 undef,
107 $storage_key,
108 );
109 0 ▶ return 0 unless defined $body_oid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body_oid 0 0 0
110 0 $self->_unlink_body($body_oid);
111 0 $self->dbh->do(qq{DELETE FROM $data WHERE storage_key = ?}, undef, $storage_key);
112 0 return 1;
113 }
114  
115 0 ▶sub _unlink_body {
Subroutine _unlink_body: not called CC=2 SCAR=17.9
116 0 my ($self, $body_oid) = @_;
117 0 my $unlinked = $self->dbh->pg_lo_unlink($body_oid);
118 0 ▶ croak "unable to unlink PostgreSQL large object" unless $unlinked;
Branch: 1 branch
BranchTrueFalseTotal
unless $unlinked 0 0 0
119 0 return 1;
120 }
121  
122 0 ▶sub _reader_dbh {
Subroutine _reader_dbh: not called CC=2 SCAR=17.9
123 0 my ($self) = @_;
124 0 my $dbh = $self->dbh->clone({
125 AutoCommit => 1,
126 RaiseError => 1,
127 PrintError => 0,
128 pg_enable_utf8 => 0,
129 });
130 0 ▶ croak "unable to clone Postgres DBI handle for blob stream" unless defined $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh 0 0 0
131 0 return $dbh;
132 }
133  
134 0 ▶sub _table {
Subroutine _table: not called CC=1 SCAR=6.9
135 0 my ($self, $name) = @_;
136 0 return $self->dbh->quote_identifier($self->schema, $name);
137 }
138  
139 2 ▶sub _validate_dbh {
Subroutine _validate_dbh: called CC=7 SCAR=22.0
140 2 my ($dbh) = @_;
141 2 ▶ 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 2 2
Condition33%2/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
!ll&&!rl&&r
002
builtin::blessed($dbh) and $dbh->can("do")
!ll&&!rl&&r
002
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
142 unless blessed($dbh) && $dbh->can('do') && $dbh->can('selectrow_array');
143 2 my $driver = eval { $dbh->{Driver}{Name} };
144 2 ▶ croak "dbh must be a Postgres DBI handle" unless defined $driver && $driver eq 'Pg';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver and $driver eq "Pg" 0 2 2
Condition33%1/3
defined $driver and $driver eq "Pg"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $driver and $driver eq "Pg"
defined $driver $driver eq "Pg"
Truth table33%1/3
A and B
A= defined $driverB= $driver eq "Pg"
ABresult
0X0
100
111
145 2 ▶ croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 2 2
146 2 $dbh->{RaiseError} = 1;
147 2 $dbh->{PrintError} = 0;
148 2 eval { $dbh->{pg_enable_utf8} = 0 };
149 2 return $dbh;
150 }
151  
152 0 ▶sub _changed_rows {
Subroutine _changed_rows: not called CC=3 SCAR=24.8
153 0 my ($rows) = @_;
154 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
155 }
156  
157 2 ▶sub _constructor_args {
Subroutine _constructor_args: called CC=4 SCAR=17.1
158 0 ▶ return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 2 2
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
200
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
159 2 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 2 2
160 2 return @_;
161 }
162  
163 0 ▶sub _range_options {
Subroutine _range_options: not called CC=7 SCAR=40.3
164 0 my ($opts) = @_;
165 croak "offset must be a non-negative integer"
166 unless defined $opts->{offset}
167 && !ref($opts->{offset})
168 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
169 croak "length must be a positive integer"
170 unless defined $opts->{length}
171 && !ref($opts->{length})
172 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
173 0 return;
174 }
175  
176 {
177 package Net::Blossom::Server::Backend::Postgres::BlobStore::_Upload;
178  
179 6 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 6 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
182 6 ▶ use Class::Tiny qw(store fh path storage_key), {
Subroutine BEGIN: called CC=1 SCAR=0.0
183 prepared => 0,
184 committed => 0,
185 aborted => 0,
186 6 };
187  
188 0 ▶ sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
189 0 my ($self) = @_;
190 0 $self->prepared;
191 0 $self->committed;
192 0 $self->aborted;
193 0 return;
194 }
195  
196 0 ▶ sub write {
Subroutine write: not called CC=5 SCAR=34.0
197 0 my ($self, $chunk) = @_;
198 0 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 0 0
199 0 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
200 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
201 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
202 0 return length $chunk;
203 }
204  
205 0 ▶ sub prepare {
Subroutine prepare: not called CC=7 SCAR=40.3
206 0 my ($self, %metadata) = @_;
207 0 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 0 0
208 0 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
209 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
210 croak "blob preparation requires an active transaction"
211 0 ▶ if $self->{store}->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"store"}->dbh->{"AutoCommit"} 0 0 0
212 0 $self->_close;
213  
214 0 my $body_oid = $self->{store}->dbh->pg_lo_import($self->{path});
215 0 ▶ croak "unable to import PostgreSQL large object" unless defined $body_oid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body_oid 0 0 0
216 0 my $storage_key = $metadata{sha256};
217 0 my $data = $self->{store}->_table('blossom_blob_data');
218 my $rows = $self->{store}->dbh->do(
219 0 qq{
220 INSERT INTO $data (storage_key, body_oid)
221 VALUES (?, ?)
222 ON CONFLICT (storage_key) DO NOTHING
223 },
224 undef,
225 $storage_key,
226 $body_oid,
227 );
228 0 ▶ $self->{store}->_unlink_body($body_oid)
Branch: 1 branch
BranchTrueFalseTotal
unless Net::Blossom::Server::Backend::Postgres::BlobStore::_changed_rows($rows) 0 0 0
229 unless Net::Blossom::Server::Backend::Postgres::BlobStore::_changed_rows($rows);
230  
231 0 $self->{storage_key} = $storage_key;
232 0 $self->{prepared} = 1;
233 0 return $storage_key;
234 }
235  
236 0 ▶ sub commit {
Subroutine commit: not called CC=4 SCAR=30.0
237 0 my ($self) = @_;
238 0 ▶ return 1 if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
239 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
240 0 ▶ croak "upload is not prepared" unless $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"prepared"} 0 0 0
241 0 $self->{committed} = 1;
242 0 $self->_cleanup;
243 0 return 1;
244 }
245  
246 0 ▶ sub abort {
Subroutine abort: not called CC=3 SCAR=24.8
247 0 my ($self) = @_;
248 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
249 0 $self->{aborted} = 1;
250 0 $self->_cleanup;
251 0 return 1;
252 }
253  
254 0 ▶ sub _cleanup {
Subroutine _cleanup: not called CC=4 SCAR=30.0
255 0 my ($self) = @_;
256 0 $self->_close;
257 unlink $self->{path}
258 or croak "unable to remove upload temp file: $!"
259 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
260 0 $self->{path} = undef;
261 0 return 1;
262 }
263  
264 0 ▶ sub _close {
Subroutine _close: not called CC=3 SCAR=24.8
265 0 my ($self) = @_;
266 0 ▶ return 1 unless defined $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fh"} 0 0 0
267 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
268 0 $self->{fh} = undef;
269 0 return 1;
270 }
271  
272 0 ▶ sub DEMOLISH {
Subroutine DEMOLISH: not called CC=3 SCAR=24.8
273 0 my ($self) = @_;
274 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
275 0 eval { $self->abort };
276 0 return;
277 }
278 }
279  
280 {
281 6 package Net::Blossom::Server::Backend::Postgres::BlobStore::_Stream;
282  
283 6 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
284  
285 6 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
286 6 ▶ use Class::Tiny qw(dbh fd offset remaining), {
Subroutine BEGIN: called CC=1 SCAR=0.0
287 closed => 0,
288 eof => 0,
289 6 };
290  
291 6 my $READ_SIZE = 65536;
292  
293 8 ▶ sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.2
294 8 my ($self) = @_;
295 8 $self->closed;
296 8 $self->eof;
297 8 ▶ if (defined $self->{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"offset"}) 3 5 8
298 3 my $ok = eval {
299 my $position = $self->{dbh}->pg_lo_lseek(
300 $self->{fd},
301 $self->{offset},
302 3 0,
303 );
304 croak "PostgreSQL large-object seek failed"
305 2 ▶ unless defined $position && $position == $self->{offset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $position and $position == $self->{"offset"} 0 2 2
Condition33%1/3
defined $position and $position == $self->{"offset"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $position and $position == $self->{"offset"}
defined $position $position == $self->{"offset"}
Truth table33%1/3
A and B
A= defined $positionB= $position == $self->{"offset"}
ABresult
0X0
100
111
306 2 1;
307 };
308 3 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 2 3
309 1 my $error = $@;
310 1 eval { $self->_finish(0) };
311 1 die $error;
312 }
313 }
314 7 return;
315 }
316  
317 11 ▶ sub read {
Subroutine read: called CC=16 SCAR=28.0
318 11 my ($self, undef, $length) = @_;
319 11 ▶ if ($self->{eof}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"eof"}) 3 8 11
320 3 $_[1] = '';
321 3 return 0;
322 }
323 8 ▶ croak "stream is closed" if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 8 8
324 8 ▶ croak "read length must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length and not ref $length and $length =~ /\A[0-9]+\z/ 0 8 8
Condition33%2/6
defined $length and not ref $length and $length =~ /\A[0-9]+\z/
!ll&&!rl&&r
008
defined $length and not ref $length
!ll&&!rl&&r
008
MC/DC0%0/3
defined $length and not ref $length and $length =~ /\A[0-9]+\z/
defined $length not ref $length $length =~ /\A[0-9]+\z/
Truth table25%1/4
A and not(B) and C
A= defined $lengthB= not ref $lengthC= $length =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
325 unless defined $length && !ref($length) && $length =~ /\A[0-9]+\z/;
326 8 ▶ if ($length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 0) 1 7 8
327 1 $_[1] = '';
328 1 return 0;
329 }
330 $length = $self->{remaining}
331 7 ▶ if defined $self->{remaining} && $length > $self->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"remaining"} and $length > $self->{"remaining"} 3 4 7
Condition66%2/3
defined $self->{"remaining"} and $length > $self->{"remaining"}
!ll&&!rl&&r
403
MC/DC50%1/2
defined $self->{"remaining"} and $length > $self->{"remaining"}
defined $self->{"remaining"} $length > $self->{"remaining"}
Truth table66%2/3
A and B
A= defined $self->{"remaining"}B= $length > $self->{"remaining"}
ABresult
0X0
100
111
332  
333 7 my $chunk = '';
334 7 my $read;
335 7 my $ok = eval {
336 7 $read = $self->{dbh}->pg_lo_read($self->{fd}, $chunk, $length);
337 6 ▶ croak "PostgreSQL large-object read failed" unless defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 6 6
338 6 1;
339 };
340 7 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 6 7
341 1 my $error = $@;
342 1 eval { $self->_finish(0) };
343 1 die $error;
344 }
345  
346 6 $_[1] = $chunk;
347 6 ▶ if (defined $self->{remaining} && $read == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"remaining"} and $read == 0) 1 5 6
Condition100%3/3
defined $self->{"remaining"} and $read == 0
!ll&&!rl&&r
321
MC/DC100%2/2
defined $self->{"remaining"} and $read == 0
defined $self->{"remaining"} $read == 0
Truth table100%3/3
A and B
A= defined $self->{"remaining"}B= $read == 0
ABresult
0X0
100
111
348 1 eval { $self->_finish(0) };
349 1 croak "PostgreSQL large-object range ended before requested length";
350 }
351 5 ▶ if ($read == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($read == 0) { } 1 4 5
elsif (defined $self->{'remaining'}) { } 2 2 4
352 1 $self->{eof} = 1;
353 1 $self->_finish(1);
354 }
355 elsif (defined $self->{remaining}) {
356 2 $self->{remaining} -= $read;
357 2 ▶ if (!$self->{remaining}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"remaining"}) 1 1 2
358 1 $self->{eof} = 1;
359 1 $self->_finish(1);
360 }
361 }
362 5 return $read;
363 }
364  
365 4 ▶ sub getline {
Subroutine getline: called CC=2 SCAR=6.9
366 4 my ($self) = @_;
367 4 my $chunk = '';
368 4 my $read = $self->read($chunk, $READ_SIZE);
369 4 ▶ return if $read == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 2 2 4
370 2 return $chunk;
371 }
372  
373 3 ▶ sub close {
Subroutine close: called CC=2 SCAR=7.1
374 3 my ($self) = @_;
375 3 ▶ return 1 if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 3 3
376 3 return $self->_finish(1);
377 }
378  
379 8 ▶ sub _finish {
Subroutine _finish: called CC=11 SCAR=24.1
380 8 my ($self, $commit) = @_;
381 8 ▶ return 1 if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 8 8
382 8 $self->{closed} = 1;
383  
384 8 my @errors;
385 8 my $object_closed = eval {
386 $self->{dbh}->pg_lo_close($self->{fd})
387 8 ▶ or die "unable to close PostgreSQL large object\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->pg_lo_close($self->{"fd"}) or die "unable to close PostgreSQL large object\n" 7 0 7
388 7 1;
389 };
390 8 ▶ push @errors, $@ unless $object_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $object_closed 1 7 8
391  
392 8 ▶ if ($commit && $object_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit and $object_closed) { } 4 4 8
Condition100%3/3
$commit and $object_closed
!ll&&!rl&&r
314
MC/DC100%2/2
$commit and $object_closed
$commit $object_closed
Truth table100%3/3
A and B
A= $commitB= $object_closed
ABresult
0X0
100
111
393 4 my $committed = eval { $self->{dbh}->commit; 1 };
394 4 ▶ if (!$committed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($committed) 1 3 4
395 1 push @errors, $@;
396 1 eval { $self->{dbh}->rollback };
397 1 ▶ push @errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
398 }
399 }
400 else {
401 4 eval { $self->{dbh}->rollback };
402 4 ▶ push @errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
403 }
404  
405 8 eval { $self->{dbh}->disconnect };
406 8 ▶ push @errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
407 8 ▶ die $errors[0] if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 2 6 8
408 6 return 1;
409 }
410  
411 8 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.6
412 8 my ($self) = @_;
413 0 ▶ eval { $self->_finish(0) } unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 0 8 8
414 8 return;
415 }
416 }
417  
418 6 1;
419  
420 =pod
421  
422 =head1 NAME
423  
424 Net::Blossom::Server::Backend::Postgres::BlobStore - PostgreSQL large-object bytes
425  
426 =head1 DESCRIPTION
427  
428 This module implements L<Net::Blossom::Server::BlobStore> using PostgreSQL
429 large objects. It stores bytes by opaque storage key and contains no descriptor
430 or owner logic.
431  
432 Use the same DBI handle for the paired
433 L<Net::Blossom::Server::Backend::Postgres::MetadataStore> so their changes share
434 one transaction.
435  
436 =head1 CONSTRUCTOR
437  
438 =head2 new
439  
440 my $blobs = Net::Blossom::Server::Backend::Postgres::BlobStore->new(
441 dbh => $dbh,
442 );
443  
444 Creates a blob store in the current PostgreSQL schema using a PostgreSQL DBI
445 handle with C<AutoCommit> enabled.
446  
447 =head1 METHODS
448  
449 =head2 dbh
450  
451 Returns the PostgreSQL DBI handle.
452  
453 =head2 schema
454  
455 Returns the schema selected when the component was created.
456  
457 =head2 deploy_schema
458  
459 Creates the storage-key to large-object table.
460  
461 =head2 begin_upload
462  
463 Starts a staged upload implementing the L<Net::Blossom::Server::BlobStore>
464 upload contract. Its C<prepare> method requires an active transaction on C<dbh>;
465 the top-level PostgreSQL backend manages this transaction.
466  
467 =head2 get_blob
468  
469 Returns a streaming large-object body by storage key, or C<undef>.
470  
471 =head2 get_blob_range
472  
473 Seeks within the PostgreSQL large object and returns a stream bounded to the
474 requested zero-based C<offset> and positive C<length>, or C<undef> when absent.
475 When C<size> is supplied, a range beyond that size is rejected.
476  
477 =head2 delete_blob
478  
479 Unlinks and removes a large object by storage key. It requires an active
480 transaction on C<dbh>.
481  
482 =head2 BUILDARGS
483  
484 Validates and normalizes constructor arguments for Class::Tiny.
485  
486 =cut
« Postgres/MetadataStore.pm ↑ Summary Postgres.pm »