SQLite.pm

filter: Statementstmt 64.5% 140 / 217 Branchbran 50.0% 41 / 82 Conditioncond 56.6% 17 / 30 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 62.5% 25 / 40 Podpod 100% 8 / 8 totaltotal 60.3% 239 / 396 CC 52 SCAR 20.0
CC
52
Cov
60%
__ANON__
20.0
__ANON__
20.0
_delete_blob_if_unowned
12.0
CRAP 7.4
« SQLite/MetadataStore.pm ↑ Summary
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
BranchTrueFalseTotal
if @unknown 1 24 25
25  
26 croak "database and dbh are mutually exclusive"
27 24 ▶ if defined $args{database} && defined $args{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"database"} and defined $args{"dbh"} 1 23 24
Condition100%3/3
defined $args{"database"} and defined $args{"dbh"}
!ll&&!rl&&r
7161
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"}
ABresult
0X0
100
111
28 croak "database or dbh is required"
29 23 ▶ unless defined $args{database} || defined $args{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"database"} or defined $args{"dbh"} 1 22 23
Condition100%3/3
defined $args{"database"} or defined $args{"dbh"}
l!l&&r!l&&!r
1661
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"}
ABresult
1X1
011
000
30 croak "base_url is required"
31 22 ▶ unless defined $args{base_url};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"base_url"} 1 21 22
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
BranchTrueFalseTotal
defined $args{'dbh'} ? : 6 5 11
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
BranchTrueFalseTotal
unless defined $row 0 1 1
73 1 my $body = $self->blob_store->get_blob($row->{storage_key});
74 1 ▶ return unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 1 1
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
BranchTrueFalseTotal
unless defined $row 0 0 0
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
BranchTrueFalseTotal
unless defined $row 0 0 0
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
BranchTrueFalseTotal
if (defined $opts{"pubkey"}) 0 0 0
107 0 ▶ return 0 unless $metadata->delete_owner($sha256, $opts{pubkey});
Branch: 1 branch
BranchTrueFalseTotal
unless $metadata->delete_owner($sha256, $opts{"pubkey"}) 0 0 0
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
BranchTrueFalseTotal
unless defined $record 0 0 0
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
BranchTrueFalseTotal
if (defined $record) { } 0 0 0
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
BranchTrueFalseTotal
$store->insert_blob(%metadata, 'storage_key', $storage_key) ? : 0 0 0
143 }
144  
145 0 ▶ if (defined $metadata{pubkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metadata{"pubkey"}) 0 0 0
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
BranchTrueFalseTotal
if $metadata->owner_count($sha256) 0 0 0
164 0 my $record = $metadata->find_blob($sha256);
165 0 ▶ return unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 0 0 0
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
BranchTrueFalseTotal
unless %columns 1 4 5
190 4 ▶ return 1 if $columns{storage_key} && !$columns{body};
Branch: 1 branch
BranchTrueFalseTotal
if $columns{"storage_key"} and not $columns{"body"} 2 2 4
Condition66%2/3
$columns{"storage_key"} and not $columns{"body"}
!ll&&!rl&&r
202
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"}
ABresult
0X0
100
111
191 croak "incompatible blossom_blobs schema"
192 2 ▶ unless $columns{body} && !$columns{storage_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $columns{"body"} and not $columns{"storage_key"} 0 2 2
Condition33%1/3
$columns{"body"} and not $columns{"storage_key"}
!ll&&!rl&&r
002
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if @$violations 1 1 2
241 1 1;
242 };
243 2 my $error = $@;
244  
245 2 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1 2
246 1 eval { $dbh->rollback };
247 1 ▶ $dbh->do('PRAGMA foreign_keys = ON') if $foreign_keys;
Branch: 1 branch
BranchTrueFalseTotal
if $foreign_keys 1 0 1
248 1 die $error;
249 }
250  
251 1 $dbh->commit;
252 1 ▶ $dbh->do('PRAGMA foreign_keys = ON') if $foreign_keys;
Branch: 1 branch
BranchTrueFalseTotal
if $foreign_keys 1 0 1
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
BranchTrueFalseTotal
if ref $database 1 4 5
259 4 ▶ croak "database is required" unless length $database;
Branch: 1 branch
BranchTrueFalseTotal
unless length $database 1 3 4
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
BranchTrueFalseTotal
unless builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array") 1 5 6
Condition66%4/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
!ll&&!rl&&r
105
builtin::blessed($dbh) and $dbh->can("do")
!ll&&!rl&&r
105
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")
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
unless defined $driver and $driver eq "SQLite" 1 4 5
Condition66%2/3
defined $driver and $driver eq "SQLite"
!ll&&!rl&&r
104
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"
ABresult
0X0
100
111
280 unless defined $driver && $driver eq 'SQLite';
281 croak "dbh must have AutoCommit enabled"
282 4 ▶ unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 1 3 4
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
BranchTrueFalseTotal
if ref $base_url 1 20 21
291 20 ▶ croak "base_url is required" unless length $base_url;
Branch: 1 branch
BranchTrueFalseTotal
unless length $base_url 0 20 20
292  
293 20 ▶ croak "base_url must be a valid HTTP base URL"
Branch: 1 branch
BranchTrueFalseTotal
unless Net::Blossom::_URL::http_base_url($base_url) 9 11 20
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
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 1 24 25
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2401
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"
ABresult
0X0
100
111
302 24 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 24 24
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
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
327 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
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
BranchTrueFalseTotal
if $self->{"committed"} 0 0 0
334 0 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 0 0
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
BranchTrueFalseTotal
if $self->{"aborted"} or $self->{"committed"} 0 0 0
Condition0%0/3
$self->{"aborted"} or $self->{"committed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"aborted"} or $self->{"committed"}
$self->{"aborted"} $self->{"committed"}
Truth table0%0/3
A or B
A= $self->{"aborted"}B= $self->{"committed"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
$created ? : 0 0 0
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
BranchTrueFalseTotal
if $self->{"committed"} or $self->{"aborted"} 0 0 0
Condition0%0/3
$self->{"committed"} or $self->{"aborted"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"committed"} or $self->{"aborted"}
$self->{"committed"} $self->{"aborted"}
Truth table0%0/3
A or B
A= $self->{"committed"}B= $self->{"aborted"}
ABresult
1X1
011
000
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
« SQLite/MetadataStore.pm ↑ Summary