| 1 | package Net::Blossom::Server::Backend::SQLite; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Class::Tiny qw(dbh base_url metadata_store blob_store); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use DBI (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Net::Blossom::BlobDescriptor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Net::Blossom::_URL; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Net::Blossom::Server::Backend::SQLite::BlobStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Net::Blossom::Server::Backend::SQLite::MetadataStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Net::Blossom::Server::BlobStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Net::Blossom::Server::BlobResult; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Net::Blossom::Server::MetadataStore; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = '0.001004'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 25 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 25 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 25 | my %args = _constructor_args(@_); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 25 | my %known = map { $_ => 1 } qw(database dbh base_url); | |||||||||||||||||||||||||||||||||||||||||
| 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 "database and dbh are mutually exclusive" | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 24 | ▶ | if defined $args{database} && defined $args{dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"database"} and defined $args{"dbh"}
MC/DC100%2/2
defined $args{"database"} and defined $args{"dbh"}
defined $args{"database"}
defined $args{"dbh"}
Truth table100%3/3
A and B
A= defined $args{"database"}B= defined $args{"dbh"}
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | croak "database or dbh is required" | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 23 | ▶ | unless defined $args{database} || defined $args{dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"database"} or defined $args{"dbh"}
MC/DC100%2/2
defined $args{"database"} or defined $args{"dbh"}
defined $args{"database"}
defined $args{"dbh"}
Truth table100%3/3
A or B
A= defined $args{"database"}B= defined $args{"dbh"}
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | croak "base_url is required" | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 22 | ▶ | unless defined $args{base_url}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | my $base_url = _normalize_base_url($args{base_url}); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | ▶ | my $dbh = defined $args{dbh} ? _validate_dbh($args{dbh}) : _connect($args{database}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | $dbh->do('PRAGMA foreign_keys = ON'); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | my $metadata_store = Net::Blossom::Server::Backend::SQLite::MetadataStore->new(dbh => $dbh); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | my $blob_store = Net::Blossom::Server::Backend::SQLite::BlobStore->new(dbh => $dbh); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | Net::Blossom::Server::MetadataStore->assert_implements($metadata_store); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | Net::Blossom::Server::BlobStore->assert_implements($blob_store); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 6 | dbh => $dbh, | |||||||||||||||||||||||||||||||||||||||||
| 44 | base_url => $base_url, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | metadata_store => $metadata_store, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | blob_store => $blob_store, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | sub deploy_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deploy_schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | $self->_migrate_legacy_schema; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | $self->blob_store->deploy_schema; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | $self->metadata_store->deploy_schema; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub begin_upload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
begin_upload: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my ($self, %context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $blob_upload = $self->blob_store->begin_upload(%context); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | Net::Blossom::Server::BlobStore->assert_upload($blob_upload); | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return Net::Blossom::Server::Backend::SQLite::_Upload->new( | |||||||||||||||||||||||||||||||||||||||||
| 64 | storage => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | blob_upload => $blob_upload, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub get_blob { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_blob: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | my $body = $self->blob_store->get_blob($row->{storage_key}); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | return unless defined $body; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | return Net::Blossom::Server::BlobResult->new( | |||||||||||||||||||||||||||||||||||||||||
| 77 | descriptor => $self->_descriptor($row), | ||||||||||||||||||||||||||||||||||||||||||
| 78 | body => $body, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub get_blob_range { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_blob_range: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my ($self, $sha256, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | return $self->blob_store->get_blob_range( | ||||||||||||||||||||||||||||||||||||||||||
| 87 | $row->{storage_key}, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | %opts, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | size => $row->{size}, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub head_blob { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
head_blob: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $row = $self->metadata_store->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return unless defined $row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | return $self->_descriptor($row); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub delete_blob { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_blob: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my ($self, $sha256, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $metadata = $self->metadata_store; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return $metadata->with_transaction(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $metadata->lock_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (defined $opts{pubkey}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return 0 unless $metadata->delete_owner($sha256, $opts{pubkey}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->_delete_blob_if_unowned($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $record = $metadata->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return 0 unless defined $record; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $metadata->delete_owners($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->blob_store->delete_blob($record->{storage_key}); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $metadata->delete_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | sub list_blobs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_blobs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | my ($self, $pubkey, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | my $rows = $self->metadata_store->list_blobs($pubkey, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | return [map { $self->_descriptor($_) } @$rows]; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub _commit_upload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_commit_upload: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my ($self, $upload, %metadata) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my $store = $self->metadata_store; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $created; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $store->with_transaction(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $store->lock_blob($metadata{sha256}); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my $record = $store->find_blob($metadata{sha256}); | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if (defined $record) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $created = 0; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $storage_key = $upload->_prepare(%metadata); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $created = $store->insert_blob(%metadata, storage_key => $storage_key) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (defined $metadata{pubkey}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $store->upsert_owner(%metadata); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | eval { $upload->_cleanup($created) }; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | descriptor => $self->_descriptor(\%metadata), | |||||||||||||||||||||||||||||||||||||||||
| 156 | created => $created, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub _delete_blob_if_unowned { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_blob_if_unowned: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my ($self, $sha256) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $metadata = $self->metadata_store; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | return if $metadata->owner_count($sha256); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $record = $metadata->find_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | return unless defined $record; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $self->blob_store->delete_blob($record->{storage_key}); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $metadata->delete_blob($sha256); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 2 | ▶ | sub _descriptor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_descriptor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | my ($self, $row) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | return Net::Blossom::BlobDescriptor->new( | ||||||||||||||||||||||||||||||||||||||||||
| 175 | url => $self->base_url . '/' . $row->{sha256}, | ||||||||||||||||||||||||||||||||||||||||||
| 176 | sha256 => $row->{sha256}, | ||||||||||||||||||||||||||||||||||||||||||
| 177 | size => 0 + $row->{size}, | ||||||||||||||||||||||||||||||||||||||||||
| 178 | type => $row->{type}, | ||||||||||||||||||||||||||||||||||||||||||
| 179 | uploaded => 0 + $row->{uploaded}, | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | ▶ | sub _migrate_legacy_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_migrate_legacy_schema: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 5 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 5 | my %columns = map { $_->[1] => 1 } @{$dbh->selectall_arrayref( | |||||||||||||||||||||||||||||||||||||||||
| 187 | q{PRAGMA table_info(blossom_blobs)}, | ||||||||||||||||||||||||||||||||||||||||||
| 188 | )}; | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 5 | ▶ | return 1 unless %columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | ▶ | return 1 if $columns{storage_key} && !$columns{body}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$columns{"storage_key"} and not $columns{"body"}
MC/DC50%1/2
$columns{"storage_key"} and not $columns{"body"}
$columns{"storage_key"}
not $columns{"body"}
Truth table66%2/3
A and not(B)
A= $columns{"storage_key"}B= not $columns{"body"}
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | croak "incompatible blossom_blobs schema" | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | ▶ | unless $columns{body} && !$columns{storage_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$columns{"body"} and not $columns{"storage_key"}
MC/DC0%0/2
$columns{"body"} and not $columns{"storage_key"}
$columns{"body"}
not $columns{"storage_key"}
Truth table33%1/3
A and not(B)
A= $columns{"body"}B= not $columns{"storage_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | my ($foreign_keys) = $dbh->selectrow_array('PRAGMA foreign_keys'); | |||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | $dbh->do('PRAGMA foreign_keys = OFF'); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | $dbh->begin_work; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | $dbh->do(q{ALTER TABLE blossom_owners RENAME TO blossom_owners_legacy}); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 2 | $dbh->do(q{ALTER TABLE blossom_blobs RENAME TO blossom_blobs_legacy}); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||
| 201 | CREATE TABLE blossom_blob_data ( | ||||||||||||||||||||||||||||||||||||||||||
| 202 | storage_key TEXT PRIMARY KEY NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 203 | body BLOB NOT NULL | ||||||||||||||||||||||||||||||||||||||||||
| 204 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 205 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 2 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||
| 207 | INSERT INTO blossom_blob_data (storage_key, body) | ||||||||||||||||||||||||||||||||||||||||||
| 208 | SELECT sha256, CAST(body AS BLOB) FROM blossom_blobs_legacy | ||||||||||||||||||||||||||||||||||||||||||
| 209 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||
| 211 | CREATE TABLE blossom_blobs ( | ||||||||||||||||||||||||||||||||||||||||||
| 212 | sha256 TEXT PRIMARY KEY NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | storage_key TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 214 | size INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 215 | type TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 216 | uploaded INTEGER NOT NULL | ||||||||||||||||||||||||||||||||||||||||||
| 217 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 218 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||
| 220 | INSERT INTO blossom_blobs (sha256, storage_key, size, type, uploaded) | ||||||||||||||||||||||||||||||||||||||||||
| 221 | SELECT sha256, sha256, size, type, uploaded FROM blossom_blobs_legacy | ||||||||||||||||||||||||||||||||||||||||||
| 222 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||
| 224 | CREATE TABLE blossom_owners ( | ||||||||||||||||||||||||||||||||||||||||||
| 225 | pubkey TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 226 | sha256 TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 227 | type TEXT NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | uploaded INTEGER NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | PRIMARY KEY (pubkey, sha256), | ||||||||||||||||||||||||||||||||||||||||||
| 230 | FOREIGN KEY (sha256) REFERENCES blossom_blobs(sha256) ON DELETE CASCADE | ||||||||||||||||||||||||||||||||||||||||||
| 231 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 232 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | $dbh->do(q{ | |||||||||||||||||||||||||||||||||||||||||
| 234 | INSERT INTO blossom_owners (pubkey, sha256, type, uploaded) | ||||||||||||||||||||||||||||||||||||||||||
| 235 | SELECT pubkey, sha256, type, uploaded FROM blossom_owners_legacy | ||||||||||||||||||||||||||||||||||||||||||
| 236 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 2 | $dbh->do(q{DROP TABLE blossom_owners_legacy}); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | $dbh->do(q{DROP TABLE blossom_blobs_legacy}); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | my $violations = $dbh->selectall_arrayref('PRAGMA foreign_key_check'); | |||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | ▶ | croak "SQLite schema migration left invalid foreign keys" if @$violations; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 242 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | eval { $dbh->rollback }; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | ▶ | $dbh->do('PRAGMA foreign_keys = ON') if $foreign_keys; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | die $error; | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | $dbh->commit; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | ▶ | $dbh->do('PRAGMA foreign_keys = ON') if $foreign_keys; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 5 | ▶ | sub _connect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 5 | my ($database) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 5 | ▶ | croak "database must be a scalar" if ref($database); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 4 | ▶ | croak "database is required" unless length $database; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 3 | return DBI->connect( | |||||||||||||||||||||||||||||||||||||||||
| 262 | "dbi:SQLite:dbname=$database", | ||||||||||||||||||||||||||||||||||||||||||
| 263 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 264 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 265 | { | ||||||||||||||||||||||||||||||||||||||||||
| 266 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 268 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | sqlite_unicode => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 270 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 271 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 6 | ▶ | sub _validate_dbh { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_dbh: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 6 | my ($dbh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 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")
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | unless blessed($dbh) && $dbh->can('do') && $dbh->can('selectrow_array'); | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 5 | my $driver = eval { $dbh->{Driver}{Name} }; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 5 | ▶ | croak "dbh must be a SQLite DBI handle" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $driver and $driver eq "SQLite"
MC/DC50%1/2
defined $driver and $driver eq "SQLite"
defined $driver
$driver eq "SQLite"
Truth table66%2/3
A and B
A= defined $driverB= $driver eq "SQLite"
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | unless defined $driver && $driver eq 'SQLite'; | ||||||||||||||||||||||||||||||||||||||||||
| 281 | croak "dbh must have AutoCommit enabled" | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 4 | ▶ | unless $dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 3 | $dbh->{RaiseError} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 3 | $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 3 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 21 | ▶ | sub _normalize_base_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_base_url: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 21 | my ($base_url) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 21 | ▶ | croak "base_url must be a scalar" if ref($base_url); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 20 | ▶ | croak "base_url is required" unless length $base_url; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 20 | ▶ | croak "base_url must be a valid HTTP base URL" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | unless Net::Blossom::_URL::http_base_url($base_url); | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 11 | $base_url =~ s{/+\z}{}; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 11 | return $base_url; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 25 | ▶ | sub _constructor_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_constructor_args: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 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"
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 24 | ▶ | croak "constructor arguments must be name/value pairs" if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 24 | return @_; | |||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||||||||||||||||
| 307 | package Net::Blossom::Server::Backend::SQLite::_Upload; | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 4 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 4 | ▶ | use Class::Tiny qw(storage blob_upload), { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | committed => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 314 | aborted => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $self->committed; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | $self->aborted; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my ($self, $chunk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | return $self->{blob_upload}->write($chunk); | |||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | my ($self, %metadata) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | croak "upload is already committed" if $self->{committed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | croak "upload is aborted" if $self->{aborted}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | my $result = $self->{storage}->_commit_upload($self, %metadata); | |||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | $self->{committed} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | sub abort { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abort: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | $self->{aborted} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | return $self->{blob_upload}->abort; | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | sub _prepare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | my ($self, %metadata) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | return $self->{blob_upload}->prepare(%metadata); | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | sub _cleanup { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cleanup: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | my ($self, $created) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 355 | return $created | ||||||||||||||||||||||||||||||||||||||||||
| 356 | ? $self->{blob_upload}->commit | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | : $self->{blob_upload}->abort; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | eval { $self->abort }; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | Net::Blossom::Server::Backend::SQLite - SQLite storage backend for Blossom servers | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | use Net::Blossom::Server; | ||||||||||||||||||||||||||||||||||||||||||
| 379 | use Net::Blossom::Server::Backend::SQLite; | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | my $storage = Net::Blossom::Server::Backend::SQLite->new( | ||||||||||||||||||||||||||||||||||||||||||
| 382 | database => '/var/lib/blossom/blossom.sqlite', | ||||||||||||||||||||||||||||||||||||||||||
| 383 | base_url => 'https://cdn.example.com', | ||||||||||||||||||||||||||||||||||||||||||
| 384 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 385 | $storage->deploy_schema; | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | my $server = Net::Blossom::Server->new(storage => $storage); | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | C<Net::Blossom::Server::Backend::SQLite> is a SQLite storage backend for | ||||||||||||||||||||||||||||||||||||||||||
| 392 | L<Net::Blossom::Server>. It stores Blossom blob bytes and metadata in a SQLite | ||||||||||||||||||||||||||||||||||||||||||
| 393 | database and implements the L<Net::Blossom::Server::Storage> contract. | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | SQLite access is provided through L<DBI> and L<DBD::SQLite>. | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | This backend is intended for self-contained single-node deployments, local | ||||||||||||||||||||||||||||||||||||||||||
| 398 | development, and tests. It can be a reasonable production choice when blob sizes, | ||||||||||||||||||||||||||||||||||||||||||
| 399 | traffic, and write concurrency are controlled. | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | Blob bodies are stored in SQLite C<BLOB> values. This keeps storage simple, but | ||||||||||||||||||||||||||||||||||||||||||
| 402 | large media archives or high-traffic public servers should usually use Postgres | ||||||||||||||||||||||||||||||||||||||||||
| 403 | or a backend that stores blob bytes outside the metadata database. | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | The backend coordinates separate | ||||||||||||||||||||||||||||||||||||||||||
| 406 | L<Net::Blossom::Server::Backend::SQLite::MetadataStore> and | ||||||||||||||||||||||||||||||||||||||||||
| 407 | L<Net::Blossom::Server::Backend::SQLite::BlobStore> components on one DBI | ||||||||||||||||||||||||||||||||||||||||||
| 408 | handle. Applications normally use this top-level storage class. | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | =head1 UPGRADING FROM 0.001000 OR 0.001001 | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | C<deploy_schema> automatically moves blob bodies from the C<blossom_blobs> | ||||||||||||||||||||||||||||||||||||||||||
| 413 | table into C<blossom_blob_data>. Existing descriptors and owners are preserved. | ||||||||||||||||||||||||||||||||||||||||||
| 414 | Back up the database before upgrading. | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | my $storage = Net::Blossom::Server::Backend::SQLite->new( | ||||||||||||||||||||||||||||||||||||||||||
| 421 | database => $path, | ||||||||||||||||||||||||||||||||||||||||||
| 422 | base_url => $url, | ||||||||||||||||||||||||||||||||||||||||||
| 423 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | Creates a storage object. C<database> is the SQLite database file path. | ||||||||||||||||||||||||||||||||||||||||||
| 426 | C<base_url> is the public HTTP or HTTPS URL prefix used when descriptors are | ||||||||||||||||||||||||||||||||||||||||||
| 427 | created. It may include a path prefix, but not userinfo, query, or fragment | ||||||||||||||||||||||||||||||||||||||||||
| 428 | parts. Trailing slashes are removed. | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | Instead of C<database>, callers may pass an existing DBI handle as C<dbh>. The | ||||||||||||||||||||||||||||||||||||||||||
| 431 | handle must be a SQLite handle with C<AutoCommit> enabled. | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =head2 dbh | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | my $dbh = $storage->dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | Returns the DBI handle used by the backend. | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | =head2 base_url | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | my $url = $storage->base_url; | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | Returns the normalized descriptor URL prefix. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =head2 metadata_store | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | Returns the SQLite metadata-store component. | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | =head2 blob_store | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | Returns the SQLite blob-store component. | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | =head2 deploy_schema | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | $storage->deploy_schema; | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | Creates the required SQLite tables and indexes if they do not already exist. | ||||||||||||||||||||||||||||||||||||||||||
| 460 | This method is safe to call more than once and migrates the earlier combined | ||||||||||||||||||||||||||||||||||||||||||
| 461 | schema when needed. | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | =head2 begin_upload | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | my $upload = $storage->begin_upload(%context); | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | Starts a blob upload and returns an upload writer. The server core writes bytes | ||||||||||||||||||||||||||||||||||||||||||
| 468 | to the writer and later calls C<commit> with validated blob metadata. | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | =head2 get_blob | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | my $result = $storage->get_blob($sha256); | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | Returns a L<Net::Blossom::Server::BlobResult> for C<$sha256>, or C<undef> when | ||||||||||||||||||||||||||||||||||||||||||
| 475 | the blob is absent. | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =head2 get_blob_range | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | Returns one requested byte range as a scalar, or C<undef> when the blob is | ||||||||||||||||||||||||||||||||||||||||||
| 480 | absent. SQLite extracts the range without returning the complete BLOB to Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | =head2 head_blob | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | my $descriptor = $storage->head_blob($sha256); | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | Returns a L<Net::Blossom::BlobDescriptor> without returning the blob body, or | ||||||||||||||||||||||||||||||||||||||||||
| 487 | C<undef> when the blob is absent. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =head2 delete_blob | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | my $deleted = $storage->delete_blob($sha256, pubkey => $pubkey); | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | Deletes one owner relationship when C<pubkey> is supplied. The blob bytes are | ||||||||||||||||||||||||||||||||||||||||||
| 494 | deleted when the final owner is removed. Without C<pubkey>, the blob and all | ||||||||||||||||||||||||||||||||||||||||||
| 495 | owners are deleted. | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | =head2 list_blobs | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | my $descriptors = $storage->list_blobs($pubkey, limit => 100); | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | Returns descriptors owned by C<$pubkey>, sorted by C<uploaded> descending and | ||||||||||||||||||||||||||||||||||||||||||
| 502 | C<sha256> ascending. C<cursor> and C<limit> follow the | ||||||||||||||||||||||||||||||||||||||||||
| 503 | L<Net::Blossom::Server::Storage> contract. | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | =head2 BUILDARGS | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | Normalizes constructor arguments for Class::Tiny. | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | =cut | ||||||||||||||||||||||||||||||||||||||||||