Postgres/MetadataStore.pm

filter: Statementstmt 18.6% 37 / 198 Branchbran 8.5% 7 / 82 Conditioncond 5.9% 5 / 84 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 25.9% 7 / 27 Podpod 100% 12 / 12 totaltotal 15.3% 68 / 442 CC 72 SCAR 45.7
CC
72
Cov
13%
_index_matches
272.0
_ensure_index
240.0
deploy_schema
72.0
CRAP 96.1
↑ Summary Postgres/BlobStore.pm »
1 package Net::Blossom::Server::Backend::Postgres::MetadataStore;
2  
3 7 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Class::Tiny qw(dbh schema);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 my $SCHEMA_LOCK_KEY = '-6335862357210207029';
10  
11 2 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=14.5
Pod documented
12 2 my $class = shift;
13 2 my %args = _constructor_args(@_);
14 2 my @unknown = grep { $_ ne 'dbh' } keys %args;
15 2 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 2 2
16 2 my $dbh = _validate_dbh($args{dbh});
17 2 my ($schema) = $dbh->selectrow_array(q{SELECT current_schema()});
18 2 ▶ croak "Postgres connection has no current schema" unless defined $schema && length $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema and length $schema 0 2 2
Condition33%1/3
defined $schema and length $schema
!ll&&!rl&&r
002
MC/DC0%0/2
defined $schema and length $schema
defined $schema length $schema
Truth table33%1/3
A and B
A= defined $schemaB= length $schema
ABresult
0X0
100
111
19 2 return { dbh => $dbh, schema => $schema };
20 }
21  
22 0 ▶sub deploy_schema {
Subroutine deploy_schema: not called CC=8 SCAR=42.8
Pod documented
23 0 my ($self) = @_;
24 0 my $dbh = $self->dbh;
25 0 ▶ croak "schema deployment requires AutoCommit" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 0 0
26  
27 # One-key advisory locks do not overlap the two-key per-blob lock space.
28 0 $self->_acquire_schema_lock;
29  
30 0 my $ok = eval {
31 0 $self->_deploy_schema;
32 0 1;
33 };
34 0 my $error = $@;
35 0 my $released;
36 0 my $unlock_ok = eval {
37 0 ($released) = $dbh->selectrow_array(
38 q{SELECT pg_advisory_unlock(CAST(? AS bigint))},
39 undef,
40 $SCHEMA_LOCK_KEY,
41 );
42 0 1;
43 };
44 0 my $unlock_error = $@;
45  
46 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
47 0 ▶ $error .= "unable to release PostgreSQL schema lock: $unlock_error"
Branch: 1 branch
BranchTrueFalseTotal
unless $unlock_ok 0 0 0
48 unless $unlock_ok;
49 0 ▶ $error .= "unable to release PostgreSQL schema lock\n"
Branch: 1 branch
BranchTrueFalseTotal
if $unlock_ok and not $released 0 0 0
Condition0%0/3
$unlock_ok and not $released
!ll&&!rl&&r
000
MC/DC0%0/2
$unlock_ok and not $released
$unlock_ok not $released
Truth table0%0/3
A and not(B)
A= $unlock_okB= not $released
ABresult
0X0
100
111
50 if $unlock_ok && !$released;
51 0 die $error;
52 }
53 0 ▶ die $unlock_error unless $unlock_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $unlock_ok 0 0 0
54 0 ▶ croak "unable to release PostgreSQL schema lock" unless $released;
Branch: 1 branch
BranchTrueFalseTotal
unless $released 0 0 0
55 0 return 1;
56 }
57  
58 0 ▶sub _acquire_schema_lock {
Subroutine _acquire_schema_lock: not called CC=2 SCAR=17.9
59 0 my ($self) = @_;
60 0 while (1) {
61 0 my ($acquired) = $self->dbh->selectrow_array(
62 q{SELECT pg_try_advisory_lock(CAST(? AS bigint))},
63 undef,
64 $SCHEMA_LOCK_KEY,
65 );
66 0 ▶ return 1 if $acquired;
Branch: 1 branch
BranchTrueFalseTotal
if $acquired 0 0 0
67 0 select undef, undef, undef, 0.05;
68 }
69 }
70  
71 0 ▶sub _deploy_schema {
Subroutine _deploy_schema: not called CC=1 SCAR=6.9
72 0 my ($self) = @_;
73 0 my $blobs = $self->_table('blossom_blobs');
74 0 my $owners = $self->_table('blossom_owners');
75  
76 0 $self->dbh->do(qq{
77 CREATE TABLE IF NOT EXISTS $blobs (
78 sha256 text PRIMARY KEY NOT NULL,
79 storage_key text NOT NULL,
80 size bigint NOT NULL,
81 type text NOT NULL,
82 uploaded bigint NOT NULL
83 )
84 });
85 0 $self->dbh->do(qq{
86 CREATE TABLE IF NOT EXISTS $owners (
87 pubkey text NOT NULL,
88 sha256 text NOT NULL,
89 type text NOT NULL,
90 uploaded bigint NOT NULL,
91 PRIMARY KEY (pubkey, sha256),
92 FOREIGN KEY (sha256) REFERENCES $blobs(sha256) ON DELETE CASCADE
93 )
94 });
95 0 $self->_ensure_index(
96 name => 'blossom_owners_pubkey_order',
97 definition => 'pubkey, uploaded DESC, sha256 ASC',
98 columns => [qw(pubkey uploaded sha256)],
99 options => '0 3 0',
100 );
101 0 $self->_ensure_index(
102 name => 'blossom_owners_sha256',
103 definition => 'sha256',
104 columns => ['sha256'],
105 options => '0',
106 );
107 0 return 1;
108 }
109  
110 0 ▶sub _ensure_index {
Subroutine _ensure_index: not called CC=15 SCAR=54.8
111 0 my ($self, %expected) = @_;
112 0 my $state = $self->_index_state($expected{name});
113  
114 0 ▶ if (defined $state) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state) 0 0 0
115 croak "incompatible PostgreSQL index $expected{name}"
116 unless $state->{is_index}
117 && $state->{table_schema} eq $self->schema
118 0 ▶ && $state->{table_name} eq 'blossom_owners';
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"is_index"} and $state->{"table_schema"} eq $self->schema and $state->{"table_name"} eq "blossom_owners" 0 0 0
Condition0%0/6
$state->{"is_index"} and $state->{"table_schema"} eq $self->schema and $state->{"table_name"} eq "blossom_owners"
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"table_schema"} eq $self->schema
!ll&&!rl&&r
000
MC/DC0%0/3
$state->{"is_index"} and $state->{"table_schema"} eq $self->schema and $state->{"table_name"} eq "blossom_owners"
$state->{"is_index"} $state->{"table_schema"} eq $self->schema $state->{"table_name"} eq "blossom_owners"
Truth table0%0/4
A and B and C
A= $state->{"is_index"}B= $state->{"table_schema"} eq $self->schemaC= $state->{"table_name"} eq "blossom_owners"
ABCresult
0XX0
10X0
1100
1111
119  
120 0 ▶ if (!$state->{valid}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $state->{'valid'}) { } 0 0 0
elsif (not $self->_index_matches($state, \%expected)) { } 0 0 0
121 0 $self->_drop_index_concurrently($expected{name});
122 0 $state = undef;
123 }
124 elsif (!$self->_index_matches($state, \%expected)) {
125 0 croak "incompatible PostgreSQL index $expected{name}";
126 }
127 else {
128 0 return 1;
129 }
130 }
131  
132 0 my $index = $self->dbh->quote_identifier($expected{name});
133 0 my $owners = $self->_table('blossom_owners');
134 0 my $created = eval {
135 0 $self->dbh->do(qq{
136 CREATE INDEX CONCURRENTLY $index
137 ON $owners ($expected{definition})
138 });
139 0 1;
140 };
141 0 my $error = $@;
142  
143 0 ▶ if (!$created) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($created) 0 0 0
144 0 eval {
145 0 my $failed = $self->_index_state($expected{name});
146 $self->_drop_index_concurrently($expected{name})
147 if defined $failed
148 && $failed->{is_index}
149 && $failed->{table_schema} eq $self->schema
150 && $failed->{table_name} eq 'blossom_owners'
151 0 ▶ && !$failed->{valid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $failed and $failed->{"is_index"} and $failed->{"table_schema"} eq $self->schema and $failed->{"table_name"} eq "blossom_owners" and not $failed->{"valid"} 0 0 0
Condition0%0/12
defined $failed and $failed->{"is_index"} and $failed->{"table_schema"} eq $self->schema and $failed->{"table_name"} eq "blossom_owners" and not $failed->{"valid"}
!ll&&!rl&&r
000
defined $failed and $failed->{"is_index"} and $failed->{"table_schema"} eq $self->schema and $failed->{"table_name"} eq "blossom_owners"
!ll&&!rl&&r
000
defined $failed and $failed->{"is_index"} and $failed->{"table_schema"} eq $self->schema
!ll&&!rl&&r
000
defined $failed and $failed->{"is_index"}
!ll&&!rl&&r
000
MC/DC0%0/5
defined $failed and $failed->{"is_index"} and $failed->{"table_schema"} eq $self->schema and $failed->{"table_name"} eq "blossom_owners" and not $failed->{"valid"}
defined $failed $failed->{"is_index"} $failed->{"table_schema"} eq $self->schema $failed->{"table_name"} eq "blossom_owners" not $failed->{"valid"}
Truth table0%0/6
A and B and C and D and not(E)
A= defined $failedB= $failed->{"is_index"}C= $failed->{"table_schema"} eq $self->schemaD= $failed->{"table_name"} eq "blossom_owners"E= not $failed->{"valid"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
152 };
153 0 die $error;
154 }
155  
156 0 $state = $self->_index_state($expected{name});
157 0 ▶ croak "PostgreSQL did not create valid index $expected{name}"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and $self->_index_matches($state, \%expected) 0 0 0
Condition0%0/3
defined $state and $self->_index_matches($state, \%expected)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $state and $self->_index_matches($state, \%expected)
defined $state $self->_index_matches($state, \%expected)
Truth table0%0/3
A and B
A= defined $stateB= $self->_index_matches($state, \%expected)
ABresult
0X0
100
111
158 unless defined $state && $self->_index_matches($state, \%expected);
159 0 return 1;
160 }
161  
162 0 ▶sub _index_state {
Subroutine _index_state: not called CC=6 SCAR=37.4
163 0 my ($self, $name) = @_;
164 0 my $row = $self->dbh->selectrow_hashref(q{
165 SELECT c.relkind,
166 i.indexrelid,
167 CASE WHEN i.indisvalid THEN 1 ELSE 0 END AS valid,
168 CASE WHEN i.indisready THEN 1 ELSE 0 END AS ready,
169 CASE WHEN i.indislive THEN 1 ELSE 0 END AS live,
170 CASE WHEN i.indisunique THEN 1 ELSE 0 END AS unique_index,
171 i.indnatts,
172 i.indoption::text AS options,
173 CASE WHEN i.indpred IS NULL THEN 1 ELSE 0 END AS unpredicated,
174 CASE WHEN i.indexprs IS NULL THEN 1 ELSE 0 END AS no_expressions,
175 CASE WHEN NOT EXISTS (
176 SELECT 1
177 FROM generate_series(0, i.indnatts - 1) AS p(position)
178 LEFT JOIN pg_attribute a
179 ON a.attrelid = i.indrelid
180 AND a.attnum = i.indkey[p.position]
181 LEFT JOIN pg_opclass opc
182 ON opc.oid = i.indclass[p.position]
183 WHERE a.attnum IS NULL
184 OR opc.oid IS NULL
185 OR NOT opc.opcdefault
186 OR opc.opcmethod <> c.relam
187 OR opc.opcintype <> a.atttypid
188 OR i.indcollation[p.position] <> a.attcollation
189 ) THEN 1 ELSE 0 END AS default_semantics,
190 am.amname,
191 tn.nspname AS table_schema,
192 t.relname AS table_name
193 FROM pg_class c
194 JOIN pg_namespace n ON n.oid = c.relnamespace
195 LEFT JOIN pg_index i ON i.indexrelid = c.oid
196 LEFT JOIN pg_class t ON t.oid = i.indrelid
197 LEFT JOIN pg_namespace tn ON tn.oid = t.relnamespace
198 LEFT JOIN pg_am am ON am.oid = c.relam
199 WHERE n.nspname = ?
200 AND c.relname = ?
201 }, undef, $self->schema, $name);
202 0 ▶ return unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
203  
204 0 ▶ $row->{is_index} = defined $row->{indexrelid} && $row->{relkind} eq 'i' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{'indexrelid'} && $row->{'relkind'} eq 'i' ? : 0 0 0
Condition0%0/3
defined $row->{"indexrelid"} and $row->{"relkind"} eq "i"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->{"indexrelid"} and $row->{"relkind"} eq "i"
defined $row->{"indexrelid"} $row->{"relkind"} eq "i"
Truth table0%0/3
A and B
A= defined $row->{"indexrelid"}B= $row->{"relkind"} eq "i"
ABresult
0X0
100
111
205 0 ▶ if ($row->{is_index}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"is_index"}) 0 0 0
206 0 my @columns;
207 0 for my $position (1 .. $row->{indnatts}) {
208 push @columns, scalar $self->dbh->selectrow_array(
209 q{SELECT pg_get_indexdef(CAST(? AS oid), ?, true)},
210 undef,
211 $row->{indexrelid},
212 0 $position,
213 );
214 }
215 0 $row->{columns} = \@columns;
216 }
217 0 return $row;
218 }
219  
220 0 ▶sub _index_matches {
Subroutine _index_matches: not called CC=16 SCAR=56.1
221 0 my ($self, $state, $expected) = @_;
222 return 0 unless $state->{is_index}
223 && $state->{valid}
224 && $state->{ready}
225 && $state->{live}
226 && !$state->{unique_index}
227 && $state->{unpredicated}
228 && $state->{no_expressions}
229 && $state->{default_semantics}
230 && defined $state->{amname}
231 && $state->{amname} eq 'btree'
232 && defined $state->{options}
233 && $state->{options} eq $expected->{options}
234 0 ▶ && @{$state->{columns}} == @{$expected->{columns}};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"} and $state->{"amname"} eq "btree" and defined $state->{"options"} and $state->{"options"} eq $expected->{"options"} and @{$state->{"columns"};} == @{$expected->{"columns"};} 0 0 0
Condition0%0/36
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"} and $state->{"amname"} eq "btree" and defined $state->{"options"} and $state->{"options"} eq $expected->{"options"} and @{$state->{"columns"};} == @{$expected->{"columns"};}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"} and $state->{"amname"} eq "btree" and defined $state->{"options"} and $state->{"options"} eq $expected->{"options"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"} and $state->{"amname"} eq "btree" and defined $state->{"options"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"} and $state->{"amname"} eq "btree"
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"}
!ll&&!rl&&r
000
$state->{"is_index"} and $state->{"valid"}
!ll&&!rl&&r
000
MC/DC0%0/13
$state->{"is_index"} and $state->{"valid"} and $state->{"ready"} and $state->{"live"} and not $state->{"unique_index"} and $state->{"unpredicated"} and $state->{"no_expressions"} and $state->{"default_semantics"} and defined $state->{"amname"} and $state->{"amname"} eq "btree" and defined $state->{"options"} and $state->{"options"} eq $expected->{"options"} and @{$state->{"columns"};} == @{$expected->{"columns"};}
$state->{"is_index"} $state->{"valid"} $state->{"ready"} $state->{"live"} not $state->{"unique_index"} $state->{"unpredicated"} $state->{"no_expressions"} $state->{"default_semantics"} defined $state->{"amname"} $state->{"amname"} eq "btree" defined $state->{"options"} $state->{"options"} eq $expected->{"options"} @{$state->{"columns"};} == @{$expected->{"columns"};}
Truth table0%0/14
A and B and C and D and not(E) and F and G and H and I and J and K and L and M
A= $state->{"is_index"}B= $state->{"valid"}C= $state->{"ready"}D= $state->{"live"}E= not $state->{"unique_index"}F= $state->{"unpredicated"}G= $state->{"no_expressions"}H= $state->{"default_semantics"}I= defined $state->{"amname"}J= $state->{"amname"} eq "btree"K= defined $state->{"options"}L= $state->{"options"} eq $expected->{"options"}M= @{$state->{"columns"};} == @{$expected->{"columns"};}
ABCDEFGHIJKLMresult
0XXXXXXXXXXXX0
10XXXXXXXXXXX0
110XXXXXXXXXX0
1110XXXXXXXXX0
11110XXXXXXXX0
111110XXXXXXX0
1111110XXXXXX0
11111110XXXXX0
111111110XXXX0
1111111110XXX0
11111111110XX0
111111111110X0
11111111111100
11111111111111
235  
236 0 for my $position (0 .. $#{$expected->{columns}}) {
237 0 ▶ return 0 unless $state->{columns}[$position] eq $expected->{columns}[$position];
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"columns"}[$position] eq $expected->{"columns"}[$position] 0 0 0
238 }
239 0 return 1;
240 }
241  
242 0 ▶sub _drop_index_concurrently {
Subroutine _drop_index_concurrently: not called CC=1 SCAR=6.9
243 0 my ($self, $name) = @_;
244 0 my $index = $self->dbh->quote_identifier($self->schema, $name);
245 0 $self->dbh->do(qq{DROP INDEX CONCURRENTLY $index});
246 0 return 1;
247 }
248  
249 0 ▶sub with_transaction {
Subroutine with_transaction: not called CC=6 SCAR=37.4
Pod documented
250 0 my ($self, $code) = @_;
251 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
252 0 my $dbh = $self->dbh;
253 0 my $wantarray = wantarray;
254 0 my (@result, $result);
255  
256 0 ▶ croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 0 0
257 0 $dbh->begin_work;
258 0 my $ok = eval {
259 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
260 0 @result = $code->();
261 }
262 else {
263 0 $result = $code->();
264 }
265 0 1;
266 };
267 0 my $error = $@;
268  
269 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
270 0 eval { $dbh->rollback };
271 0 die $error;
272 }
273  
274 0 $dbh->commit;
275 0 ▶ return $wantarray ? @result : $result;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
276 }
277  
278 0 ▶sub lock_blob {
Subroutine lock_blob: not called CC=2 SCAR=17.9
Pod documented
279 0 my ($self, $sha256) = @_;
280 0 $self->_require_transaction;
281 my @keys = map {
282 0 my $key = unpack 'N', pack 'H8', substr($sha256, $_, 8);
283 0 ▶ $key -= 4_294_967_296 if $key > 2_147_483_647;
Branch: 1 branch
BranchTrueFalseTotal
if $key > 2147483647 0 0 0
284 0 $key;
285 } (0, 8);
286 0 $self->dbh->selectrow_array(q{SELECT pg_advisory_xact_lock(?, ?)}, undef, @keys);
287 0 return 1;
288 }
289  
290 0 ▶sub find_blob {
Subroutine find_blob: not called CC=1 SCAR=6.9
Pod documented
291 0 my ($self, $sha256) = @_;
292 0 my $blobs = $self->_table('blossom_blobs');
293 0 return $self->dbh->selectrow_hashref(
294 qq{SELECT sha256, storage_key, size, type, uploaded FROM $blobs WHERE sha256 = ?},
295 undef,
296 $sha256,
297 );
298 }
299  
300 0 ▶sub insert_blob {
Subroutine insert_blob: not called CC=2 SCAR=17.9
Pod documented
301 0 my ($self, %record) = @_;
302 0 $self->_require_transaction;
303 0 my $blobs = $self->_table('blossom_blobs');
304 my $rows = $self->dbh->do(
305 qq{
306 INSERT INTO $blobs (sha256, storage_key, size, type, uploaded)
307 VALUES (?, ?, ?, ?, ?)
308 ON CONFLICT (sha256) DO NOTHING
309 },
310 undef,
311 0 @record{qw(sha256 storage_key size type uploaded)},
312 );
313 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
314 }
315  
316 0 ▶sub upsert_owner {
Subroutine upsert_owner: not called CC=1 SCAR=6.9
Pod documented
317 0 my ($self, %owner) = @_;
318 0 $self->_require_transaction;
319 0 my $owners = $self->_table('blossom_owners');
320 $self->dbh->do(
321 qq{
322 INSERT INTO $owners (pubkey, sha256, type, uploaded)
323 VALUES (?, ?, ?, ?)
324 ON CONFLICT (pubkey, sha256)
325 DO UPDATE SET type = EXCLUDED.type,
326 uploaded = EXCLUDED.uploaded
327 },
328 undef,
329 0 @owner{qw(pubkey sha256 type uploaded)},
330 );
331 0 return 1;
332 }
333  
334 0 ▶sub delete_owner {
Subroutine delete_owner: not called CC=2 SCAR=17.9
Pod documented
335 0 my ($self, $sha256, $pubkey) = @_;
336 0 $self->_require_transaction;
337 0 my $owners = $self->_table('blossom_owners');
338 0 my $rows = $self->dbh->do(
339 qq{DELETE FROM $owners WHERE sha256 = ? AND pubkey = ?},
340 undef,
341 $sha256,
342 $pubkey,
343 );
344 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
345 }
346  
347 0 ▶sub delete_owners {
Subroutine delete_owners: not called CC=2 SCAR=17.9
Pod documented
348 0 my ($self, $sha256) = @_;
349 0 $self->_require_transaction;
350 0 my $owners = $self->_table('blossom_owners');
351 0 my $rows = $self->dbh->do(qq{DELETE FROM $owners WHERE sha256 = ?}, undef, $sha256);
352 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
353 }
354  
355 0 ▶sub owner_count {
Subroutine owner_count: not called CC=1 SCAR=6.9
Pod documented
356 0 my ($self, $sha256) = @_;
357 0 my $owners = $self->_table('blossom_owners');
358 0 my ($count) = $self->dbh->selectrow_array(
359 qq{SELECT COUNT(*) FROM $owners WHERE sha256 = ?},
360 undef,
361 $sha256,
362 );
363 0 return 0 + $count;
364 }
365  
366 0 ▶sub delete_blob {
Subroutine delete_blob: not called CC=2 SCAR=17.9
Pod documented
367 0 my ($self, $sha256) = @_;
368 0 $self->_require_transaction;
369 0 my $blobs = $self->_table('blossom_blobs');
370 0 my $rows = $self->dbh->do(qq{DELETE FROM $blobs WHERE sha256 = ?}, undef, $sha256);
371 0 ▶ return _changed_rows($rows) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_changed_rows($rows) ? : 0 0 0
372 }
373  
374 0 ▶sub list_blobs {
Subroutine list_blobs: not called CC=5 SCAR=34.0
Pod documented
375 0 my ($self, $pubkey, %opts) = @_;
376 0 my $blobs = $self->_table('blossom_blobs');
377 0 my $owners = $self->_table('blossom_owners');
378 0 my @where = ('o.pubkey = ?');
379 0 my @bind = ($pubkey);
380  
381 0 ▶ if (defined $opts{cursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"cursor"}) 0 0 0
382 my $cursor = $self->dbh->selectrow_hashref(
383 qq{SELECT sha256, uploaded FROM $owners WHERE pubkey = ? AND sha256 = ?},
384 undef,
385 $pubkey,
386 $opts{cursor},
387 0 );
388 0 ▶ return [] unless defined $cursor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cursor 0 0 0
389 0 push @where, q{(o.uploaded < ? OR (o.uploaded = ? AND o.sha256 > ?))};
390 0 push @bind, $cursor->{uploaded}, $cursor->{uploaded}, $cursor->{sha256};
391 }
392  
393 0 my $sql = qq{
394 SELECT o.sha256, b.storage_key, b.size, o.type, o.uploaded
395 FROM $owners o
396 JOIN $blobs b ON b.sha256 = o.sha256
397 WHERE
398 } . join(' AND ', @where) . q{
399 ORDER BY o.uploaded DESC, o.sha256 ASC
400 };
401  
402 0 ▶ if (defined $opts{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"limit"}) 0 0 0
403 0 ▶ return [] if $opts{limit} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"limit"} <= 0 0 0 0
404 0 $sql .= q{ LIMIT ?};
405 0 push @bind, int($opts{limit});
406 }
407  
408 0 return $self->dbh->selectall_arrayref($sql, { Slice => {} }, @bind);
409 }
410  
411 0 ▶sub _table {
Subroutine _table: not called CC=1 SCAR=6.9
412 0 my ($self, $name) = @_;
413 0 return $self->dbh->quote_identifier($self->schema, $name);
414 }
415  
416 2 ▶sub _validate_dbh {
Subroutine _validate_dbh: called CC=7 SCAR=22.0
417 2 my ($dbh) = @_;
418 2 ▶ 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 2 2
Condition33%2/6
builtin::blessed($dbh) and $dbh->can("do") and $dbh->can("selectrow_array")
!ll&&!rl&&r
002
builtin::blessed($dbh) and $dbh->can("do")
!ll&&!rl&&r
002
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
419 unless blessed($dbh) && $dbh->can('do') && $dbh->can('selectrow_array');
420 2 my $driver = eval { $dbh->{Driver}{Name} };
421 2 ▶ croak "dbh must be a Postgres DBI handle" unless defined $driver && $driver eq 'Pg';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver and $driver eq "Pg" 0 2 2
Condition33%1/3
defined $driver and $driver eq "Pg"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $driver and $driver eq "Pg"
defined $driver $driver eq "Pg"
Truth table33%1/3
A and B
A= defined $driverB= $driver eq "Pg"
ABresult
0X0
100
111
422 2 ▶ croak "dbh must have AutoCommit enabled" unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 2 2
423 2 $dbh->{RaiseError} = 1;
424 2 $dbh->{PrintError} = 0;
425 2 eval { $dbh->{pg_enable_utf8} = 0 };
426 2 return $dbh;
427 }
428  
429 0 ▶sub _require_transaction {
Subroutine _require_transaction: not called CC=2 SCAR=17.9
430 0 my ($self) = @_;
431 croak "metadata change requires an active transaction"
432 0 ▶ if $self->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->dbh->{"AutoCommit"} 0 0 0
433 0 return;
434 }
435  
436 0 ▶sub _changed_rows {
Subroutine _changed_rows: not called CC=3 SCAR=24.8
437 0 my ($rows) = @_;
438 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
439 }
440  
441 2 ▶sub _constructor_args {
Subroutine _constructor_args: called CC=4 SCAR=17.1
442 0 ▶ return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 2 2
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
200
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
443 2 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 2 2
444 2 return @_;
445 }
446  
447 7 1;
448  
449 =pod
450  
451 =head1 NAME
452  
453 Net::Blossom::Server::Backend::Postgres::MetadataStore - PostgreSQL Blossom metadata
454  
455 =head1 DESCRIPTION
456  
457 This module implements L<Net::Blossom::Server::MetadataStore> using PostgreSQL.
458 It stores descriptors and owners but not blob bytes.
459  
460 Use the same DBI handle for the paired
461 L<Net::Blossom::Server::Backend::Postgres::BlobStore> so their changes share one
462 transaction.
463  
464 C<lock_blob> and all methods that change records require a transaction started
465 by C<with_transaction>.
466  
467 C<deploy_schema> requires C<AutoCommit>. It serializes concurrent deployments
468 and builds missing indexes without blocking ordinary metadata writes. An
469 invalid index left by an interrupted build is replaced automatically.
470  
471 =head1 CONSTRUCTOR
472  
473 =head2 new
474  
475 my $metadata = Net::Blossom::Server::Backend::Postgres::MetadataStore->new(
476 dbh => $dbh,
477 );
478  
479 Creates a metadata store in the current PostgreSQL schema using a PostgreSQL
480 DBI handle with C<AutoCommit> enabled.
481  
482 =head1 METHODS
483  
484 =head2 dbh
485  
486 Returns the PostgreSQL DBI handle.
487  
488 =head2 schema
489  
490 Returns the schema selected when the component was created.
491  
492 =head2 deploy_schema
493  
494 Creates storage-neutral metadata tables and the owner lookup indexes. Missing
495 indexes are built concurrently.
496  
497 =head2 with_transaction
498  
499 Runs a callback in a PostgreSQL transaction.
500  
501 =head2 lock_blob
502  
503 Takes a transaction-level advisory lock for one SHA-256 hash.
504  
505 =head2 find_blob
506  
507 Returns one storage-neutral blob record by SHA-256.
508  
509 =head2 insert_blob
510  
511 Inserts a blob record and reports whether it was created.
512  
513 =head2 upsert_owner
514  
515 Creates or updates one owner record.
516  
517 =head2 delete_owner
518  
519 Deletes one owner record and reports whether it existed.
520  
521 =head2 delete_owners
522  
523 Deletes every owner record for a blob.
524  
525 =head2 owner_count
526  
527 Returns the number of owners for a blob.
528  
529 =head2 delete_blob
530  
531 Deletes one blob metadata record and reports whether it existed.
532  
533 =head2 list_blobs
534  
535 Returns ordered storage-neutral blob records for an owner.
536  
537 =head2 BUILDARGS
538  
539 Validates and normalizes constructor arguments for Class::Tiny.
540  
541 =cut
↑ Summary Postgres/BlobStore.pm »