| 1 | package Net::Blossom::Server::Backend::Postgres; | ||||||||||||||||||||||||||||||||||||||||||
| 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 base_url _schema metadata_store blob_store); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use DBI (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Net::Blossom::BlobDescriptor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Net::Blossom::_URL; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Net::Blossom::Server::Backend::Postgres::BlobStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Net::Blossom::Server::Backend::Postgres::MetadataStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Net::Blossom::Server::BlobStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Net::Blossom::Server::BlobResult; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Net::Blossom::Server::MetadataStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | our $VERSION = '0.001004'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 25 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 25 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 25 | my %args = _constructor_args(@_); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 25 | my %known = map { $_ => 1 } qw(dsn username password dbh base_url connect_attrs); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 25 | my @unknown = grep { !exists $known{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 25 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | croak "dsn and dbh are mutually exclusive" | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 24 | ▶ | if defined $args{dsn} && defined $args{dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"dsn"} and defined $args{"dbh"}
MC/DC100%2/2
defined $args{"dsn"} and defined $args{"dbh"}
defined $args{"dsn"}
defined $args{"dbh"}
Truth table100%3/3
A and B
A= defined $args{"dsn"}B= defined $args{"dbh"}
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | croak "dsn or dbh is required" | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 23 | ▶ | unless defined $args{dsn} || defined $args{dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"dsn"} or defined $args{"dbh"}
MC/DC100%2/2
defined $args{"dsn"} or defined $args{"dbh"}
defined $args{"dsn"}
defined $args{"dbh"}
Truth table100%3/3
A or B
A= defined $args{"dsn"}B= defined $args{"dbh"}
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | croak "base_url is required" | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 22 | ▶ | unless defined $args{base_url}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | croak "connect_attrs must be a hash reference" | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | ▶ | if defined $args{connect_attrs} && ref($args{connect_attrs}) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"connect_attrs"} and ref $args{"connect_attrs"} ne "HASH"
MC/DC50%1/2
defined $args{"connect_attrs"} and ref $args{"connect_attrs"} ne "HASH"
defined $args{"connect_attrs"}
ref $args{"connect_attrs"} ne "HASH"
Truth table66%2/3
A and B
A= defined $args{"connect_attrs"}B= ref $args{"connect_attrs"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 20 | my $base_url = _normalize_base_url($args{base_url}); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | ▶ | my $dbh = defined $args{dbh} ? _validate_dbh($args{dbh}) : _connect(%args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | my ($schema) = $dbh->selectrow_array(q{SELECT current_schema()}); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | croak "Postgres connection has no current schema" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $schema and length $schema
MC/DC50%1/2
defined $schema and length $schema
defined $schema
length $schema
Truth table66%2/3
A and B
A= defined $schemaB= length $schema
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | unless defined $schema && length $schema; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | my $metadata_store = Net::Blossom::Server::Backend::Postgres::MetadataStore->new(dbh => $dbh); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | my $blob_store = Net::Blossom::Server::Backend::Postgres::BlobStore->new(dbh => $dbh); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | Net::Blossom::Server::MetadataStore->assert_implements($metadata_store); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | Net::Blossom::Server::BlobStore->assert_implements($blob_store); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | dbh => $dbh, | |||||||||||||||||||||||||||||||||||||||||
| 48 | base_url => $base_url, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | _schema => $schema, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | metadata_store => $metadata_store, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | blob_store => $blob_store, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub deploy_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deploy_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->_migrate_legacy_schema; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $self->blob_store->deploy_schema; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $self->metadata_store->deploy_schema; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub begin_upload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
begin_upload: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my ($self, %context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $blob_upload = $self->blob_store->begin_upload(%context); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | Net::Blossom::Server::BlobStore->assert_upload($blob_upload); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return Net::Blossom::Server::Backend::Postgres::_Upload->new( | |||||||||||||||||||||||||||||||||||||||||
| 69 | storage => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | blob_upload => $blob_upload, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub get_blob { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_blob: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $body = $self->blob_store->get_blob($row->{storage_key}); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return unless defined $body; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return Net::Blossom::Server::BlobResult->new( | |||||||||||||||||||||||||||||||||||||||||
| 82 | descriptor => $self->_descriptor($row), | ||||||||||||||||||||||||||||||||||||||||||
| 83 | body => $body, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub get_blob_range { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_blob_range: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my ($self, $sha256, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | return $self->blob_store->get_blob_range( | ||||||||||||||||||||||||||||||||||||||||||
| 92 | $row->{storage_key}, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | %opts, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | size => $row->{size}, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub head_blob { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
head_blob: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return $self->_descriptor($row); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub delete_blob { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_blob: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my ($self, $sha256, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $metadata = $self->metadata_store; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return $metadata->with_transaction(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $metadata->lock_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (defined $opts{pubkey}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return 0 unless $metadata->delete_owner($sha256, $opts{pubkey}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $self->_delete_blob_if_unowned($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $record = $metadata->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return 0 unless defined $record; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $metadata->delete_owners($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $self->blob_store->delete_blob($record->{storage_key}); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $metadata->delete_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub list_blobs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_blobs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my ($self, $pubkey, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my $rows = $self->metadata_store->list_blobs($pubkey, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return [map { $self->_descriptor($_) } @$rows]; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub _commit_upload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_commit_upload: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my ($self, $upload, %metadata) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $store = $self->metadata_store; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $created; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $store->with_transaction(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $store->lock_blob($metadata{sha256}); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $record = $store->find_blob($metadata{sha256}); | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (defined $record) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $created = 0; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $storage_key = $upload->_prepare(%metadata); | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | $created = $store->insert_blob(%metadata, storage_key => $storage_key) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (defined $metadata{pubkey}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | $store->upsert_owner(%metadata); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | eval { $upload->_cleanup($created) }; | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | descriptor => $self->_descriptor(\%metadata), | |||||||||||||||||||||||||||||||||||||||||
| 162 | created => $created, | ||||||||||||||||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _delete_blob_if_unowned { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_blob_if_unowned: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my $metadata = $self->metadata_store; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return if $metadata->owner_count($sha256); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $record = $metadata->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return unless defined $record; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $self->blob_store->delete_blob($record->{storage_key}); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $metadata->delete_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub _descriptor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_descriptor: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my ($self, $row) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | return Net::Blossom::BlobDescriptor->new( | ||||||||||||||||||||||||||||||||||||||||||
| 181 | url => $self->base_url . '/' . $row->{sha256}, | ||||||||||||||||||||||||||||||||||||||||||
| 182 | sha256 => $row->{sha256}, | ||||||||||||||||||||||||||||||||||||||||||
| 183 | size => 0 + $row->{size}, | ||||||||||||||||||||||||||||||||||||||||||
| 184 | type => $row->{type}, | ||||||||||||||||||||||||||||||||||||||||||
| 185 | uploaded => 0 + $row->{uploaded}, | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub _migrate_legacy_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_migrate_legacy_schema: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my $schema = $self->_schema; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $columns = $self->dbh->selectall_arrayref(q{ | |||||||||||||||||||||||||||||||||||||||||
| 193 | SELECT column_name, udt_name | ||||||||||||||||||||||||||||||||||||||||||
| 194 | FROM information_schema.columns | ||||||||||||||||||||||||||||||||||||||||||
| 195 | WHERE table_schema = ? | ||||||||||||||||||||||||||||||||||||||||||
| 196 | AND table_name = 'blossom_blobs' | ||||||||||||||||||||||||||||||||||||||||||
| 197 | }, undef, $schema); | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | return 1 unless @$columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my %columns = map { $_->[0] => $_->[1] } @$columns; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | return 1 if exists $columns{storage_key} && !exists $columns{body_oid}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $columns{"storage_key"} and not exists $columns{"body_oid"}
MC/DC0%0/2
exists $columns{"storage_key"} and not exists $columns{"body_oid"}
exists $columns{"storage_key"}
not exists $columns{"body_oid"}
Truth table0%0/3
A and not(B)
A= exists $columns{"storage_key"}B= not exists $columns{"body_oid"}
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | croak "incompatible blossom_blobs schema; recreate it for PostgreSQL large-object storage" | ||||||||||||||||||||||||||||||||||||||||||
| 203 | unless !exists $columns{body} | ||||||||||||||||||||||||||||||||||||||||||
| 204 | && defined $columns{body_oid} | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | && $columns{body_oid} eq 'oid'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not exists $columns{"body"} and defined $columns{"body_oid"} and $columns{"body_oid"} eq "oid"
not exists $columns{"body"} and defined $columns{"body_oid"}
MC/DC0%0/3
not exists $columns{"body"} and defined $columns{"body_oid"} and $columns{"body_oid"} eq "oid"
not exists $columns{"body"}
defined $columns{"body_oid"}
$columns{"body_oid"} eq "oid"
Truth table0%0/4
not(A) and B and C
A= not exists $columns{"body"}B= defined $columns{"body_oid"}C= $columns{"body_oid"} eq "oid"
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my $blobs = $self->_table('blossom_blobs'); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $data = $self->_table('blossom_blob_data'); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | $self->metadata_store->with_transaction(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $self->dbh->do(qq{ | |||||||||||||||||||||||||||||||||||||||||
| 211 | CREATE TABLE $data ( | ||||||||||||||||||||||||||||||||||||||||||
| 212 | storage_key text PRIMARY KEY NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | body_oid oid NOT NULL | ||||||||||||||||||||||||||||||||||||||||||
| 214 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 215 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $self->dbh->do(qq{ | |||||||||||||||||||||||||||||||||||||||||
| 217 | INSERT INTO $data (storage_key, body_oid) | ||||||||||||||||||||||||||||||||||||||||||
| 218 | SELECT sha256, body_oid FROM $blobs | ||||||||||||||||||||||||||||||||||||||||||
| 219 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $self->dbh->do(qq{ALTER TABLE $blobs ADD COLUMN storage_key text}); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $self->dbh->do(qq{UPDATE $blobs SET storage_key = sha256}); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $self->dbh->do(qq{ALTER TABLE $blobs ALTER COLUMN storage_key SET NOT NULL}); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | $self->dbh->do(qq{ALTER TABLE $blobs DROP COLUMN body_oid}); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | sub _table { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | return $self->dbh->quote_identifier($self->_schema, $name); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 4 | ▶ | sub _connect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=9 SCAR=27.3
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 4 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 4 | my $dsn = $args{dsn}; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 4 | ▶ | croak "dsn must be a scalar" if ref($dsn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 3 | ▶ | croak "dsn is required" unless length $dsn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | ▶ | croak "username must be a scalar" if defined $args{username} && ref($args{username}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"username"} and ref $args{"username"}
MC/DC50%1/2
defined $args{"username"} and ref $args{"username"}
defined $args{"username"}
ref $args{"username"}
Truth table66%2/3
A and B
A= defined $args{"username"}B= ref $args{"username"}
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | croak "password must be a scalar" if defined $args{password} && ref($args{password}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args{"password"} and ref $args{"password"}
MC/DC0%0/2
defined $args{"password"} and ref $args{"password"}
defined $args{"password"}
ref $args{"password"}
Truth table33%1/3
A and B
A= defined $args{"password"}B= ref $args{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | eval 'use DBD::Pg (); 1' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | or die $@; | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | my %attrs = ( | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | %{ $args{connect_attrs} || {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'connect_attrs'} || {}
MC/DC0%0/1
$args{'connect_attrs'} || {}
$args{'connect_attrs'}
Truth table0%0/2
A
A= $args{'connect_attrs'}
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 248 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 249 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | pg_enable_utf8 => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 251 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | return DBI->connect( | ||||||||||||||||||||||||||||||||||||||||||
| 254 | $dsn, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | $args{username}, | ||||||||||||||||||||||||||||||||||||||||||
| 256 | $args{password}, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | \%attrs, | |||||||||||||||||||||||||||||||||||||||||
| 258 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 6 | ▶ | sub _validate_dbh { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_dbh: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 6 | my ($dbh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | ▶ | croak "dbh must be a DBI database handle" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
builtin::blessed($dbh) and $dbh->can("do")
MC/DC33%1/3
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
builtin::blessed($dbh)
$dbh->can("do")
$dbh->can("selectrow_array")
Truth table50%2/4
A and B and C
A= builtin::blessed($dbh)B= $dbh->can("do")C= $dbh->can("selectrow_array")
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | unless blessed($dbh) && $dbh->can('do') && $dbh->can('selectrow_array'); | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 5 | my $driver = eval { $dbh->{Driver}{Name} }; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 5 | ▶ | croak "dbh must be a Postgres DBI handle" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $driver and $driver eq "Pg"
MC/DC50%1/2
defined $driver and $driver eq "Pg"
defined $driver
$driver eq "Pg"
Truth table66%2/3
A and B
A= defined $driverB= $driver eq "Pg"
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | unless defined $driver && $driver eq 'Pg'; | ||||||||||||||||||||||||||||||||||||||||||
| 268 | croak "dbh must have AutoCommit enabled" | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | unless $dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 3 | $dbh->{RaiseError} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 3 | $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 3 | eval { $dbh->{pg_enable_utf8} = 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 3 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 20 | ▶ | sub _normalize_base_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_base_url: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 20 | my ($base_url) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 20 | ▶ | croak "base_url must be a scalar" if ref($base_url); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 19 | ▶ | croak "base_url is required" unless length $base_url; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 19 | ▶ | croak "base_url must be a valid HTTP base URL" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | unless Net::Blossom::_URL::http_base_url($base_url); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 10 | $base_url =~ s{/+\z}{}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 10 | return $base_url; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 25 | ▶ | sub _constructor_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_constructor_args: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 25 | ▶ | return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 24 | ▶ | croak "constructor arguments must be name/value pairs" if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 24 | return @_; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | { | ||||||||||||||||||||||||||||||||||||||||||
| 295 | package Net::Blossom::Server::Backend::Postgres::_Upload; | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 6 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 6 | ▶ | use Class::Tiny qw(storage blob_upload), { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | committed => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 302 | aborted => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | $self->committed; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | $self->aborted; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | my ($self, $chunk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | return $self->{blob_upload}->write($chunk); | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | my ($self, %metadata) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | my $result = $self->{storage}->_commit_upload($self, %metadata); | |||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $self->{committed} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | sub abort { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abort: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 331 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | $self->{aborted} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | return $self->{blob_upload}->abort; | |||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub _prepare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | my ($self, %metadata) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | return $self->{blob_upload}->prepare(%metadata); | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | sub _cleanup { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cleanup: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my ($self, $created) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 343 | return $created | ||||||||||||||||||||||||||||||||||||||||||
| 344 | ? $self->{blob_upload}->commit | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | : $self->{blob_upload}->abort; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | eval { $self->abort }; | |||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | Net::Blossom::Server::Backend::Postgres - Postgres storage backend for Blossom servers | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | use Net::Blossom::Server; | ||||||||||||||||||||||||||||||||||||||||||
| 367 | use Net::Blossom::Server::Backend::Postgres; | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | my $storage = Net::Blossom::Server::Backend::Postgres->new( | ||||||||||||||||||||||||||||||||||||||||||
| 370 | dsn => 'dbi:Pg:dbname=blossom;host=/var/run/postgresql', | ||||||||||||||||||||||||||||||||||||||||||
| 371 | username => 'blossom', | ||||||||||||||||||||||||||||||||||||||||||
| 372 | password => $password, | ||||||||||||||||||||||||||||||||||||||||||
| 373 | base_url => 'https://cdn.example.com', | ||||||||||||||||||||||||||||||||||||||||||
| 374 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 375 | $storage->deploy_schema; | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | my $server = Net::Blossom::Server->new(storage => $storage); | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | C<Net::Blossom::Server::Backend::Postgres> is a Postgres storage backend for | ||||||||||||||||||||||||||||||||||||||||||
| 382 | L<Net::Blossom::Server>. It stores Blossom blob bytes and metadata in Postgres | ||||||||||||||||||||||||||||||||||||||||||
| 383 | and implements the L<Net::Blossom::Server::Storage> contract. | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | Postgres access is provided through L<DBI> and L<DBD::Pg>. | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | Blob bodies are stored as | ||||||||||||||||||||||||||||||||||||||||||
| 388 | L<PostgreSQL large objects|https://www.postgresql.org/docs/current/largeobjects.html>. | ||||||||||||||||||||||||||||||||||||||||||
| 389 | Uploads are written to a temporary file and imported only after the server has | ||||||||||||||||||||||||||||||||||||||||||
| 390 | validated the hash. Downloads are returned as streams, so blob bodies are not | ||||||||||||||||||||||||||||||||||||||||||
| 391 | loaded into Perl memory as a whole. | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | Each active download uses a cloned DBI connection and a read transaction until | ||||||||||||||||||||||||||||||||||||||||||
| 394 | the body reaches EOF or is closed. Deployments must allow enough PostgreSQL | ||||||||||||||||||||||||||||||||||||||||||
| 395 | connections for their concurrent downloads. Very large public media services | ||||||||||||||||||||||||||||||||||||||||||
| 396 | may still prefer a backend that stores blob bytes outside the metadata database. | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | This backend serializes uploads and deletes for the same hash with a | ||||||||||||||||||||||||||||||||||||||||||
| 399 | transaction-level PostgreSQL advisory lock. The lock is released when the | ||||||||||||||||||||||||||||||||||||||||||
| 400 | transaction commits or rolls back. Direct SQL writes to the backend tables do | ||||||||||||||||||||||||||||||||||||||||||
| 401 | not participate in this locking protocol. Operations for different hashes may | ||||||||||||||||||||||||||||||||||||||||||
| 402 | run concurrently. | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | The backend coordinates separate | ||||||||||||||||||||||||||||||||||||||||||
| 405 | L<Net::Blossom::Server::Backend::Postgres::MetadataStore> and | ||||||||||||||||||||||||||||||||||||||||||
| 406 | L<Net::Blossom::Server::Backend::Postgres::BlobStore> components on one DBI | ||||||||||||||||||||||||||||||||||||||||||
| 407 | handle. Applications normally use this top-level storage class. | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | =head1 UPGRADING FROM 0.001001 | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | C<deploy_schema> automatically separates the current large-object schema into | ||||||||||||||||||||||||||||||||||||||||||
| 412 | metadata and blob-data tables. Existing large-object identifiers, descriptors, | ||||||||||||||||||||||||||||||||||||||||||
| 413 | and owners are preserved. Back up the database before upgrading. | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | my $storage = Net::Blossom::Server::Backend::Postgres->new( | ||||||||||||||||||||||||||||||||||||||||||
| 420 | dsn => $dsn, | ||||||||||||||||||||||||||||||||||||||||||
| 421 | username => $username, | ||||||||||||||||||||||||||||||||||||||||||
| 422 | password => $password, | ||||||||||||||||||||||||||||||||||||||||||
| 423 | base_url => $url, | ||||||||||||||||||||||||||||||||||||||||||
| 424 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | Creates a storage object. C<dsn> is a DBI Postgres data source string. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | C<username> and C<password> are optional and are passed to C<DBI-E<gt>connect>. | ||||||||||||||||||||||||||||||||||||||||||
| 428 | C<base_url> is the public HTTP or HTTPS URL prefix used when descriptors are | ||||||||||||||||||||||||||||||||||||||||||
| 429 | created. It may include a path prefix, but not userinfo, query, or fragment | ||||||||||||||||||||||||||||||||||||||||||
| 430 | parts. Trailing slashes are removed. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | Instead of C<dsn>, callers may pass an existing DBI handle as C<dbh>. The handle | ||||||||||||||||||||||||||||||||||||||||||
| 433 | must be a Postgres handle with C<AutoCommit> enabled so the backend can manage | ||||||||||||||||||||||||||||||||||||||||||
| 434 | its transactions. The backend clones this handle for each active blob download. | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | The backend uses the connection's current schema at construction time. All | ||||||||||||||||||||||||||||||||||||||||||
| 437 | later operations, including cloned download connections, remain bound to that | ||||||||||||||||||||||||||||||||||||||||||
| 438 | schema. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | Optional C<connect_attrs> may be supplied with C<dsn>. The backend always forces | ||||||||||||||||||||||||||||||||||||||||||
| 441 | C<AutoCommit>, C<RaiseError>, C<PrintError>, and C<pg_enable_utf8> to values | ||||||||||||||||||||||||||||||||||||||||||
| 442 | needed by the storage implementation. | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | =head2 dbh | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | my $dbh = $storage->dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | Returns the DBI handle used by the backend. | ||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | =head2 base_url | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | my $url = $storage->base_url; | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | Returns the normalized descriptor URL prefix. | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | =head2 metadata_store | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | Returns the PostgreSQL metadata-store component. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | =head2 blob_store | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | Returns the PostgreSQL large-object store component. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | =head2 deploy_schema | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | $storage->deploy_schema; | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | Creates the required Postgres tables and indexes if they do not already exist. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | They are created in the schema captured by C<new>. This method is safe to call | ||||||||||||||||||||||||||||||||||||||||||
| 472 | more than once. Metadata indexes are built concurrently, so existing tables | ||||||||||||||||||||||||||||||||||||||||||
| 473 | remain writable while an index is added. The method requires C<AutoCommit> and | ||||||||||||||||||||||||||||||||||||||||||
| 474 | repairs an invalid index left by an interrupted build. It migrates the | ||||||||||||||||||||||||||||||||||||||||||
| 475 | C<0.001001> schema automatically. | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =head2 begin_upload | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | my $upload = $storage->begin_upload(%context); | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | Starts a blob upload and returns an upload writer. The server core writes bytes | ||||||||||||||||||||||||||||||||||||||||||
| 482 | to a temporary file and later calls C<commit> with validated blob metadata. A | ||||||||||||||||||||||||||||||||||||||||||
| 483 | new blob is imported as a PostgreSQL large object transactionally. | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =head2 get_blob | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | my $result = $storage->get_blob($sha256); | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | Returns a L<Net::Blossom::Server::BlobResult> for C<$sha256>, or C<undef> when | ||||||||||||||||||||||||||||||||||||||||||
| 490 | the blob is absent. Its body is a stream backed by a dedicated DBI connection. | ||||||||||||||||||||||||||||||||||||||||||
| 491 | Reading to EOF or calling C<close> releases that connection. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | =head2 get_blob_range | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | Returns one requested byte range as a positioned, bounded large-object stream, | ||||||||||||||||||||||||||||||||||||||||||
| 496 | or C<undef> when the blob is absent. Reading the range or calling C<close> | ||||||||||||||||||||||||||||||||||||||||||
| 497 | releases its dedicated DBI connection. | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | =head2 head_blob | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | my $descriptor = $storage->head_blob($sha256); | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | Returns a L<Net::Blossom::BlobDescriptor> without returning the blob body, or | ||||||||||||||||||||||||||||||||||||||||||
| 504 | C<undef> when the blob is absent. | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | =head2 delete_blob | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | my $deleted = $storage->delete_blob($sha256, pubkey => $pubkey); | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | Deletes one owner relationship when C<pubkey> is supplied. The blob bytes are | ||||||||||||||||||||||||||||||||||||||||||
| 511 | deleted with C<pg_lo_unlink> when the final owner is removed. Without C<pubkey>, | ||||||||||||||||||||||||||||||||||||||||||
| 512 | the blob and all owners are deleted. | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | =head2 list_blobs | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | my $descriptors = $storage->list_blobs($pubkey, limit => 100); | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | Returns descriptors owned by C<$pubkey>, sorted by C<uploaded> descending and | ||||||||||||||||||||||||||||||||||||||||||
| 519 | C<sha256> ascending. C<cursor> and C<limit> follow the | ||||||||||||||||||||||||||||||||||||||||||
| 520 | L<Net::Blossom::Server::Storage> contract. | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | L<PostgreSQL large objects|https://www.postgresql.org/docs/current/largeobjects.html>, | ||||||||||||||||||||||||||||||||||||||||||
| 525 | L<DBD::Pg/Large Objects> | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =head2 BUILDARGS | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | Normalizes constructor arguments for Class::Tiny. | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | =cut | ||||||||||||||||||||||||||||||||||||||||||