| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $schema and length $schema
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $opts{"size"} and $opts{"offset"} + $opts{"length"} > $opts{"size"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | croak "unable to open PostgreSQL large object" if defined $body_oid && !defined $fd; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $body_oid and not defined $fd
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (!$ok || !defined $body_oid) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ok and defined $body_oid
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | eval { $dbh->rollback unless $dbh->{AutoCommit} }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | eval { $dbh->disconnect }; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | die $error unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
builtin::blessed($dbh) and $dbh->can("do")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $driver and $driver eq "Pg"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | ▶ | croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
defined $rows and $rows ne "0E0"
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 2 | ▶ | croak "constructor arguments must be name/value pairs" if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/
defined $opts->{"offset"} and not ref $opts->{"offset"}
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/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/
defined $opts->{"length"} and not ref $opts->{"length"}
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/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | print {$self->{fh}} $chunk or croak "storage write failed: $!"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | croak "blob preparation requires an active transaction" | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if $self->{store}->dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | croak "upload is not prepared" unless $self->{prepared}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->{"aborted"} or $self->{"committed"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/8
defined $self->{'path'} && -e $self->{'path'} && (unlink($self->{'path'}) || croak("unable to remove upload temp file: $!"))
defined $self->{"path"} and -e $self->{"path"}
unlink $self->{"path"} or croak("unable to remove upload temp file: $!")
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | close $self->{fh} or croak "unable to close upload temp file: $!"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->{"committed"} or $self->{"aborted"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $position and $position == $self->{"offset"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 307 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 3 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | $_[1] = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 8 | ▶ | croak "stream is closed" if $self->{closed}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 8 | ▶ | croak "read length must be a non-negative integer" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $length and not ref $length and $length =~ /\A[0-9]+\z/
defined $length and not ref $length
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/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | unless defined $length && !ref($length) && $length =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 8 | ▶ | if ($length == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 1 | $_[1] = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | $length = $self->{remaining} | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 7 | ▶ | if defined $self->{remaining} && $length > $self->{remaining}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"remaining"} and $length > $self->{"remaining"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 7 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $self->{"remaining"} and $read == 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 389 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 8 | ▶ | push @errors, $@ unless $object_closed; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 8 | ▶ | if ($commit && $object_closed) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$commit and $object_closed
MC/DC100%2/2
$commit and $object_closed
$commit
$object_closed
Truth table100%3/3
A and B
A= $commitB= $object_closed
| |||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 4 | my $committed = eval { $self->{dbh}->commit; 1 }; | |||||||||||||||||||||||||||||||||||||||||||||
| 394 | 4 | ▶ | if (!$committed) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 1 | push @errors, $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 396 | 1 | eval { $self->{dbh}->rollback }; | |||||||||||||||||||||||||||||||||||||||||||||
| 397 | 1 | ▶ | push @errors, $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 4 | eval { $self->{dbh}->rollback }; | |||||||||||||||||||||||||||||||||||||||||||||
| 402 | 4 | ▶ | push @errors, $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 8 | eval { $self->{dbh}->disconnect }; | |||||||||||||||||||||||||||||||||||||||||||||
| 406 | 8 | ▶ | push @errors, $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 8 | ▶ | die $errors[0] if @errors; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||