SQLite/MetadataStore.pm

filter: Statementstmt 43.9% 47 / 107 Branchbran 19.0% 8 / 42 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 50.0% 10 / 20 Podpod 100% 12 / 12 totaltotal 38.7% 81 / 209 CC 28 SCAR 23.9
CC
28
Cov
35%
with_transaction
42.0
_changed_rows
12.0
list_blobs
10.2
CRAP 10.9
« SQLite/BlobStore.pm ↑ Summary SQLite.pm »
1 package Net::Blossom::Server::Backend::SQLite::MetadataStore;
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);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod documented
10 6 my $class = shift;
11 6 my %args = _constructor_args(@_);
12 6 my @unknown = grep { $_ ne 'dbh' } keys %args;
13 6 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 6 6
14 6 return { dbh => _validate_dbh($args{dbh}) };
15 }
16  
17 4 ▶sub deploy_schema {
Subroutine deploy_schema: called CC=1 SCAR=0.0
Pod documented
18 4 my ($self) = @_;
19 4 my $dbh = $self->dbh;
20  
21 4 $dbh->do(q{
22 CREATE TABLE IF NOT EXISTS blossom_blobs (
23 sha256 TEXT PRIMARY KEY NOT NULL,
24 storage_key TEXT NOT NULL,
25 size INTEGER NOT NULL,
26 type TEXT NOT NULL,
27 uploaded INTEGER NOT NULL
28 )
29 });
30 4 $dbh->do(q{
31 CREATE TABLE IF NOT EXISTS blossom_owners (
32 pubkey TEXT NOT NULL,
33 sha256 TEXT NOT NULL,
34 type TEXT NOT NULL,
35 uploaded INTEGER NOT NULL,
36 PRIMARY KEY (pubkey, sha256),
37 FOREIGN KEY (sha256) REFERENCES blossom_blobs(sha256) ON DELETE CASCADE
38 )
39 });
40 4 $dbh->do(q{
41 CREATE INDEX IF NOT EXISTS blossom_owners_pubkey_order
42 ON blossom_owners (pubkey, uploaded DESC, sha256 ASC)
43 });
44 4 $dbh->do(q{
45 CREATE INDEX IF NOT EXISTS blossom_owners_sha256
46 ON blossom_owners (sha256)
47 });
48  
49 4 return 1;
50 }
51  
52 0 ▶sub with_transaction {
Subroutine with_transaction: not called CC=6 SCAR=37.4
Pod documented
53 0 my ($self, $code) = @_;
54 0 ▶ croak "transaction callback must be a code reference" unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
55  
56 0 my $dbh = $self->dbh;
57 0 my $wantarray = wantarray;
58 0 my (@result, $result);
59  
60 0 ▶ croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 0 0
61 0 $dbh->begin_work;
62 0 my $ok = eval {
63 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
64 0 @result = $code->();
65 }
66 else {
67 0 $result = $code->();
68 }
69 0 1;
70 };
71 0 my $error = $@;
72  
73 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
74 0 eval { $dbh->rollback };
75 0 die $error;
76 }
77  
78 0 $dbh->commit;
79 0 ▶ return $wantarray ? @result : $result;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
80 }
81  
82 0 ▶sub lock_blob {
Subroutine lock_blob: not called CC=1 SCAR=6.9
Pod documented
83 0 my ($self) = @_;
84 0 $self->_require_transaction;
85 0 return 1;
86 }
87  
88 1 ▶sub find_blob {
Subroutine find_blob: called CC=1 SCAR=0.0
Pod documented
89 1 my ($self, $sha256) = @_;
90 1 return $self->dbh->selectrow_hashref(
91 q{SELECT sha256, storage_key, size, type, uploaded FROM blossom_blobs WHERE sha256 = ?},
92 undef,
93 $sha256,
94 );
95 }
96  
97 0 ▶sub insert_blob {
Subroutine insert_blob: not called CC=2 SCAR=17.9
Pod documented
98 0 my ($self, %record) = @_;
99 0 $self->_require_transaction;
100 my $rows = $self->dbh->do(
101 q{
102 INSERT OR IGNORE INTO blossom_blobs
103 (sha256, storage_key, size, type, uploaded)
104 VALUES (?, ?, ?, ?, ?)
105 },
106 undef,
107 0 @record{qw(sha256 storage_key size type uploaded)},
108 );
109 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
110 }
111  
112 0 ▶sub upsert_owner {
Subroutine upsert_owner: not called CC=2 SCAR=17.9
Pod documented
113 0 my ($self, %owner) = @_;
114 0 $self->_require_transaction;
115 my $rows = $self->dbh->do(
116 q{
117 UPDATE blossom_owners
118 SET type = ?, uploaded = ?
119 WHERE pubkey = ? AND sha256 = ?
120 },
121 undef,
122 0 @owner{qw(type uploaded pubkey sha256)},
123 );
124 0 ▶ if (!_changed_rows($rows)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_changed_rows($rows)) 0 0 0
125 $self->dbh->do(
126 q{
127 INSERT INTO blossom_owners
128 (pubkey, sha256, type, uploaded)
129 VALUES (?, ?, ?, ?)
130 },
131 undef,
132 0 @owner{qw(pubkey sha256 type uploaded)},
133 );
134 }
135 0 return 1;
136 }
137  
138 0 ▶sub delete_owner {
Subroutine delete_owner: not called CC=2 SCAR=17.9
Pod documented
139 0 my ($self, $sha256, $pubkey) = @_;
140 0 $self->_require_transaction;
141 0 my $rows = $self->dbh->do(
142 q{DELETE FROM blossom_owners WHERE sha256 = ? AND pubkey = ?},
143 undef,
144 $sha256,
145 $pubkey,
146 );
147 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
148 }
149  
150 0 ▶sub delete_owners {
Subroutine delete_owners: not called CC=2 SCAR=17.9
Pod documented
151 0 my ($self, $sha256) = @_;
152 0 $self->_require_transaction;
153 0 my $rows = $self->dbh->do(
154 q{DELETE FROM blossom_owners WHERE sha256 = ?},
155 undef,
156 $sha256,
157 );
158 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
159 }
160  
161 0 ▶sub owner_count {
Subroutine owner_count: not called CC=1 SCAR=6.9
Pod documented
162 0 my ($self, $sha256) = @_;
163 0 my ($count) = $self->dbh->selectrow_array(
164 q{SELECT COUNT(*) FROM blossom_owners WHERE sha256 = ?},
165 undef,
166 $sha256,
167 );
168 0 return 0 + $count;
169 }
170  
171 0 ▶sub delete_blob {
Subroutine delete_blob: not called CC=2 SCAR=17.9
Pod documented
172 0 my ($self, $sha256) = @_;
173 0 $self->_require_transaction;
174 0 my $rows = $self->dbh->do(
175 q{DELETE FROM blossom_blobs WHERE sha256 = ?},
176 undef,
177 $sha256,
178 );
179 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
180 }
181  
182 1 ▶sub list_blobs {
Subroutine list_blobs: called CC=5 SCAR=23.2
Pod documented
183 1 my ($self, $pubkey, %opts) = @_;
184 1 my @where = ('o.pubkey = ?');
185 1 my @bind = ($pubkey);
186  
187 1 ▶ if (defined $opts{cursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"cursor"}) 0 1 1
188 my $cursor = $self->dbh->selectrow_hashref(
189 q{SELECT sha256, uploaded FROM blossom_owners WHERE pubkey = ? AND sha256 = ?},
190 undef,
191 $pubkey,
192 $opts{cursor},
193 0 );
194 0 ▶ return [] unless defined $cursor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cursor 0 0 0
195 0 push @where, q{(o.uploaded < ? OR (o.uploaded = ? AND o.sha256 > ?))};
196 0 push @bind, $cursor->{uploaded}, $cursor->{uploaded}, $cursor->{sha256};
197 }
198  
199 1 my $sql = q{
200 SELECT o.sha256, b.storage_key, b.size, o.type, o.uploaded
201 FROM blossom_owners o
202 JOIN blossom_blobs b ON b.sha256 = o.sha256
203 WHERE
204 } . join(' AND ', @where) . q{
205 ORDER BY o.uploaded DESC, o.sha256 ASC
206 };
207  
208 1 ▶ if (defined $opts{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"limit"}) 0 1 1
209 0 ▶ return [] if $opts{limit} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"limit"} <= 0 0 0 0
210 0 $sql .= q{ LIMIT ?};
211 0 push @bind, int($opts{limit});
212 }
213  
214 1 return $self->dbh->selectall_arrayref($sql, { Slice => {} }, @bind);
215 }
216  
217 6 ▶sub _validate_dbh {
Subroutine _validate_dbh: called CC=7 SCAR=22.6
218 6 my ($dbh) = @_;
219 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") 0 6 6
Condition33%2/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
!ll&&!rl&&r
006
builtin::blessed($dbh) and $dbh->can("do")
!ll&&!rl&&r
006
MC/DC0%0/3
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
builtin::blessed($dbh) $dbh->can("do") $dbh->can("selectrow_array")
Truth table25%1/4
A and B and C
A= builtin::blessed($dbh)B= $dbh->can("do")C= $dbh->can("selectrow_array")
ABCresult
0XX0
10X0
1100
1111
220 unless blessed($dbh) && $dbh->can('do') && $dbh->can('selectrow_array');
221 6 my $driver = eval { $dbh->{Driver}{Name} };
222 6 ▶ croak "dbh must be a SQLite DBI handle"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver and $driver eq "SQLite" 0 6 6
Condition33%1/3
defined $driver and $driver eq "SQLite"
!ll&&!rl&&r
006
MC/DC0%0/2
defined $driver and $driver eq "SQLite"
defined $driver $driver eq "SQLite"
Truth table33%1/3
A and B
A= defined $driverB= $driver eq "SQLite"
ABresult
0X0
100
111
223 unless defined $driver && $driver eq 'SQLite';
224 6 ▶ croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 6 6
225 6 $dbh->{RaiseError} = 1;
226 6 $dbh->{PrintError} = 0;
227 6 return $dbh;
228 }
229  
230 0 ▶sub _require_transaction {
Subroutine _require_transaction: not called CC=2 SCAR=17.9
231 0 my ($self) = @_;
232 croak "metadata change requires an active transaction"
233 0 ▶ if $self->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->dbh->{"AutoCommit"} 0 0 0
234 0 return;
235 }
236  
237 0 ▶sub _changed_rows {
Subroutine _changed_rows: not called CC=3 SCAR=24.8
238 0 my ($rows) = @_;
239 0 ▶ return defined $rows && $rows ne '0E0' && $rows > 0;
Condition0%0/6
defined $rows && $rows ne '0E0' && $rows > 0
!ll&&!rl&&r
000
defined $rows and $rows ne "0E0"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $rows && $rows ne '0E0' && $rows > 0
defined $rows $rows ne "0E0" $rows > 0
Truth table0%0/4
A and B && C
A= defined $rowsB= $rows ne "0E0"C= $rows > 0
ABCresult
0XX0
10X0
1100
1111
240 }
241  
242 6 ▶sub _constructor_args {
Subroutine _constructor_args: called CC=4 SCAR=17.1
243 0 ▶ return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 6 6
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
600
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
244 6 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 6 6
245 6 return @_;
246 }
247  
248 4 1;
249  
250 =pod
251  
252 =head1 NAME
253  
254 Net::Blossom::Server::Backend::SQLite::MetadataStore - SQLite Blossom metadata
255  
256 =head1 DESCRIPTION
257  
258 This module implements L<Net::Blossom::Server::MetadataStore> using SQLite. It
259 stores descriptors and owners but not blob bytes.
260  
261 Use the same DBI handle for the paired
262 L<Net::Blossom::Server::Backend::SQLite::BlobStore> so their changes share one
263 transaction.
264  
265 C<lock_blob> and all methods that change records require a transaction started
266 by C<with_transaction>.
267  
268 =head1 CONSTRUCTOR
269  
270 =head2 new
271  
272 my $metadata = Net::Blossom::Server::Backend::SQLite::MetadataStore->new(
273 dbh => $dbh,
274 );
275  
276 Creates a metadata store using a SQLite DBI handle with C<AutoCommit> enabled.
277  
278 =head1 METHODS
279  
280 =head2 dbh
281  
282 Returns the SQLite DBI handle.
283  
284 =head2 deploy_schema
285  
286 Creates the metadata tables and owner lookup indexes.
287  
288 =head2 with_transaction
289  
290 Runs a callback in a SQLite transaction.
291  
292 =head2 lock_blob
293  
294 Provides the contract's per-blob lock point. SQLite serializes writes for the
295 whole database, so this method is a no-op. Conflicting writers may receive a
296 SQLite busy or locked error.
297  
298 =head2 find_blob
299  
300 Returns one storage-neutral blob record by SHA-256.
301  
302 =head2 insert_blob
303  
304 Inserts a blob record and reports whether it was created.
305  
306 =head2 upsert_owner
307  
308 Creates or updates one owner record.
309  
310 =head2 delete_owner
311  
312 Deletes one owner record and reports whether it existed.
313  
314 =head2 delete_owners
315  
316 Deletes every owner record for a blob.
317  
318 =head2 owner_count
319  
320 Returns the number of owners for a blob.
321  
322 =head2 delete_blob
323  
324 Deletes one blob metadata record and reports whether it existed.
325  
326 =head2 list_blobs
327  
328 Returns ordered storage-neutral blob records for an owner.
329  
330 =head2 BUILDARGS
331  
332 Validates and normalizes constructor arguments for Class::Tiny.
333  
334 =cut
« SQLite/BlobStore.pm ↑ Summary SQLite.pm »