blib/lib/DBIx/QuickDB/Pool.pm

filter: Statementstmt 32.8% 99 / 301 Branchbran 25.8% 32 / 124 Conditioncond 15.3% 12 / 78 MC/DCmcdc 4.3% 2 / 46 Subroutinesub 47.3% 18 / 38 Podpod 90.0% 18 / 20 totaltotal 29.8% 181 / 607 CC 94 SCAR 41.7
CC
94
Cov
28%
cache_check
306.0
add_db
110.0
add_driver
110.0
CRAP 64.8
« blib/lib/DBIx/QuickDB/Driver/MySQLCom.pm ↑ Summary lib/DBIx/QuickDB/Watcher.pm »
1 package DBIx::QuickDB::Pool;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.000066';
6  
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Path qw/make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Digest::SHA qw/sha1_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use DBIx::QuickDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use DBIx::QuickDB::Util qw/remove_tree_robust remove_tree_or_quarantine disconnect_dbi_handles/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use DBIx::QuickDB::Util::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
18 +cache_dir
19 +instance_dir
20  
21 <library
22  
23 +databases
24  
25 update_checksums
26 purge_old
27 verbose
28 show_diag
29 2 };
30  
31 0 ▶sub import {
Subroutine import: not called CC=2 SCAR=17.9
32 0 my $class = shift;
33 0 my %params = @_;
34  
35 0 ▶ $params{library} ||= caller;
Condition0%0/3
$params{"library"} ||= caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"library"} ||= caller
$params{"library"} caller
Truth table0%0/3
A ||= B
A= $params{"library"}B= caller
ABresult
1X1
011
000
36  
37 0 my $inst = $class->new(%params);
38  
39 0 $inst->export();
40 }
41  
42 2 ▶sub init {
Subroutine init: called CC=10 SCAR=27.4
Pod undocumented
43 2 my $self = shift;
44  
45 2 ▶ croak "'cache_dir' is a required_attribute" unless $self->{+CACHE_DIR};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cache_dir"} 0 2 2
46  
47 croak "'cache_dir' must point to an existing directory"
48 2 ▶ unless -d $self->{+CACHE_DIR};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"cache_dir"} 0 2 2
49  
50 croak "'$self->{+INSTANCE_DIR}' must be an existing directory"
51 2 ▶ if $self->{+INSTANCE_DIR} && !-d $self->{+INSTANCE_DIR};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"instance_dir"} and not -d $self->{"instance_dir"} 0 2 2
Condition33%1/3
$self->{"instance_dir"} and not -d $self->{"instance_dir"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"instance_dir"} and not -d $self->{"instance_dir"}
$self->{"instance_dir"} not -d $self->{"instance_dir"}
Truth table33%1/3
A and not(B)
A= $self->{"instance_dir"}B= not -d $self->{"instance_dir"}
ABresult
0X0
100
111
52  
53 2 ▶ $self->{+LIBRARY} //= caller(1);
Condition33%1/3
$self->{"library"} //= caller 1
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"library"} //= caller 1
$self->{"library"} caller 1
Truth table33%1/3
A //= B
A= $self->{"library"}B= caller 1
ABresult
1X1
011
000
54  
55 2 ▶ $self->{+VERBOSE} //= 0;
Condition50%1/2
$self->{"verbose"} //= 0
l!l
02
MC/DC0%0/1
$self->{"verbose"} //= 0
$self->{"verbose"}
Truth table50%1/2
A
A= $self->{"verbose"}
Aresult
11
00
56 2 ▶ $self->{+PURGE_OLD} //= 0;
Condition50%1/2
$self->{"purge_old"} //= 0
l!l
02
MC/DC0%0/1
$self->{"purge_old"} //= 0
$self->{"purge_old"}
Truth table50%1/2
A
A= $self->{"purge_old"}
Aresult
11
00
57 2 ▶ $self->{+UPDATE_CHECKSUMS} //= 1;
Condition50%1/2
$self->{"update_checksums"} //= 1
l!l
02
MC/DC0%0/1
$self->{"update_checksums"} //= 1
$self->{"update_checksums"}
Truth table50%1/2
A
A= $self->{"update_checksums"}
Aresult
11
00
58  
59 2 ▶ $self->{+DATABASES} //= {};
Condition50%1/2
$self->{"databases"} //= {}
l!l
02
MC/DC0%0/1
$self->{"databases"} //= {}
$self->{"databases"}
Truth table50%1/2
A
A= $self->{"databases"}
Aresult
11
00
60 }
61  
62 # Remove a disposable database tree, or move it out of the canonical cache path
63 # when Windows still refuses the removal after retries. Returning true means the
64 # canonical path is clear and safe to rebuild; clear_old_cache() reclaims the
65 # quarantine on a later pass, and it can never be mistaken for a valid entry.
66 3 ▶sub _remove_or_quarantine {
Subroutine _remove_or_quarantine: called CC=3 SCAR=11.0
67 3 my ($self, $dir) = @_;
68  
69 3 my $stale = remove_tree_or_quarantine($dir);
70 3 ▶ return 0 unless defined $stale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stale 0 3 3
71  
72 3 ▶ $self->diag("$$ Quarantined stale database directory '$dir' as '$stale'")
Branch: 1 branch
BranchTrueFalseTotal
if length $stale 1 2 3
73 if length $stale;
74 3 return 1;
75 }
76  
77 2 ▶sub clear_old_cache {
Subroutine clear_old_cache: called CC=15 SCAR=27.6
Pod documented
78 2 my $self = shift;
79 2 my ($age) = @_;
80  
81 2 my $dir = $self->{+CACHE_DIR};
82  
83 2 ▶ opendir(my $dh, $dir) or die "Could not open cache dir '$dir': $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Could not open cache dir '${dir}': $!" 2 0 2
84 2 for my $name (readdir($dh)) {
85 8 my $full = "$dir/$name";
86  
87 # Quarantines sit outside the canonical cache path (see
88 # _remove_or_quarantine). Retry their removal on every pass: the
89 # transient Windows file lock that blocked deletion may have cleared.
90 8 ▶ if ($name =~ m/\.STALE-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\.STALE-/) 1 7 8
91 1 ▶ remove_tree_robust($full) if -d $full;
Branch: 1 branch
BranchTrueFalseTotal
if -d $full 1 0 1
92 1 next;
93 }
94  
95 # A live cache entry is a bare directory name. Its marker files
96 # ("$name.lock", "$name.READY") do contain dots and are handled
97 # through their entry, never scanned directly.
98 7 ▶ next if $name =~ m/\./;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\./ 4 3 7
99  
100 3 ▶ next unless -d $full;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $full 0 3 3
101  
102 3 my $file = "$full/cloned";
103 3 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 2 3
104  
105 2 ▶ open(my $fh, '<', $file) or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 2 0 2
106 2 my $stamp = <$fh>;
107 2 close($fh);
108 2 ▶ $stamp = '' unless defined $stamp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stamp 1 1 2
109 2 chomp($stamp);
110  
111 # Time::HiRes values written by older releases can contain a decimal
112 # comma under the Windows process locale (for example 1785633742,27439).
113 # Cache expiry needs only whole seconds, so accept either historical
114 # fractional spelling and reduce it to a locale-independent integer. A
115 # damaged stamp is treated as infinitely old so it is reclaimed rather
116 # than pinning an unusable clone in the cache forever.
117 2 $stamp =~ s/[.,]\d+\z//;
118 2 ▶ $stamp = 0 unless $stamp =~ /^\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stamp =~ /^\d+\z/ 1 1 2
119  
120 2 ▶ next unless $age <= (time - $stamp);
Branch: 1 branch
BranchTrueFalseTotal
unless $age <= &DBIx::QuickDB::Pool::time() - $stamp 0 2 2
121  
122 2 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { disconnect_dbi_handles($full); $self->_remove_or_quarantine($full); unlink "$full.lock" if -e "$full.lock"; unlink "$full.READY" if -e "$full.READY"; 1 } } or warn $@ 2 0 2
123 # An expired SQLite source may still have DBI handles retained by
124 # application/test code. On Windows those handles prevent unlinking
125 # the database file. Close every matching handle before removal.
126 2 disconnect_dbi_handles($full);
127  
128 # If removal remains blocked, moving the tree out of its canonical
129 # path keeps a later build from seeing partially-removed SQLite
130 # schema/data. Either way, invalidate the external markers: expiry
131 # was explicitly requested, and build_db() will refuse to build on
132 # any stubborn tree that could not be quarantined.
133 2 $self->_remove_or_quarantine($full);
134 2 ▶ unlink("$full.lock") if -e "$full.lock";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$full.lock" 0 2 2
135 2 ▶ unlink("$full.READY") if -e "$full.READY";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$full.READY" 0 2 2
136 2 1;
137 } or warn $@;
138 }
139 }
140  
141 0 ▶sub export {
Subroutine export: not called CC=1 SCAR=5.1
Pod documented
142 0 my $self = shift;
143  
144 0 my $library = $self->{+LIBRARY};
145  
146 0 ▶ my $qdb = sub { $self };
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 ▶ my $build = sub { $self->add_db(@_, caller => [caller()]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 ▶ my $db = sub { $self->fetch_db(@_, caller => [caller()]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 ▶ my $driver = sub { $self->add_driver(@_, caller => [caller()]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
150  
151 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 0 *{"$library\::QDB_POOL"} = $qdb;
153 0 *{"$library\::driver"} = $driver;
154 0 *{"$library\::build"} = $build;
155 0 *{"$library\::db"} = $db;
156  
157 0 push @{"$library\::EXPORT_OK"} => 'db';
158 }
159  
160 0 ▶sub throw {
Subroutine throw: not called CC=2 SCAR=17.9
Pod documented
161 0 my $self = shift;
162 0 my ($msg, %params) = @_;
163 0 ▶ my $caller = $params{caller} || [caller(1)];
Condition0%0/2
$params{'caller'} || [caller 1]
l!l
00
MC/DC0%0/1
$params{'caller'} || [caller 1]
$params{'caller'}
Truth table0%0/2
A
A= $params{'caller'}
Aresult
11
00
164 0 die "$msg at $caller->[1] line $caller->[2].\n";
165 }
166  
167 0 ▶sub alert {
Subroutine alert: not called CC=2 SCAR=17.9
Pod documented
168 0 my $self = shift;
169 0 my ($msg, %params) = @_;
170 0 ▶ my $caller = $params{caller} || [caller(1)];
Condition0%0/2
$params{'caller'} || [caller 1]
l!l
00
MC/DC0%0/1
$params{'caller'} || [caller 1]
$params{'caller'}
Truth table0%0/2
A
A= $params{'caller'}
Aresult
11
00
171 0 warn "$msg at $caller->[1] line $caller->[2].\n";
172 }
173  
174 1 ▶sub diag {
Subroutine diag: called CC=5 SCAR=25.7
Pod documented
175 1 my $self = shift;
176 1 my ($msg, %params) = @_;
177 1 ▶ my $show = $self->{+VERBOSE} || $self->{+SHOW_DIAG};
Condition33%1/3
$self->{'verbose'} || $self->{'show_diag'}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{'verbose'} || $self->{'show_diag'}
$self->{'verbose'} $self->{'show_diag'}
Truth table33%1/3
A || B
A= $self->{'verbose'}B= $self->{'show_diag'}
ABresult
1X1
011
000
178 1 ▶ return unless $show;
Branch: 1 branch
BranchTrueFalseTotal
unless $show 1 0 1
179  
180 # Only append caller info when asked
181 0 ▶ if (my $caller = $params{caller}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = $params{"caller"}) 0 0 0
182 0 $msg .= " at $caller->[1] line $caller->[2].";
183 }
184  
185 0 ▶ if ($show > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show > 1) { } 0 0 0
186 0 print STDERR "$msg\n";
187 }
188 else {
189 0 print STDOUT "$msg\n";
190 }
191  
192 0 return;
193 }
194  
195 0 ▶sub add_db {
Subroutine add_db: not called CC=10 SCAR=47.0
Pod documented
196 0 my $self = shift;
197 0 my ($name, %params) = @_;
198  
199 0 ▶ my $from = $params{from} or $self->throw("You must specify a 'from' database", %params);
Branch: 1 branch
BranchTrueFalseTotal
my $from = $params{"from"} or $self->throw("You must specify a 'from' database", %params) 0 0 0
200 0 ▶ my $from_spec = $self->{+DATABASES}->{$from} or $self->throw("Cannot build '$name' from '$from' which has not been defined");
Branch: 1 branch
BranchTrueFalseTotal
my $from_spec = $self->{"databases"}{$from} or $self->throw("Cannot build '${name}' from '${from}' which has not been defined") 0 0 0
201  
202 $self->throw("Database '$name' is already defined", %params)
203 0 ▶ if $self->{+DATABASES}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"databases"}{$name} 0 0 0
204  
205 $self->throw("'build' is a required argument", %params)
206 0 ▶ unless $params{build};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"build"} 0 0 0
207  
208 $self->throw("'$params{build}' is not a valid builder, it must be either a coderef, or a class method on '$self->{+LIBRARY}'", %params)
209 0 ▶ unless ref($params{build}) eq 'CODE' || $self->{+LIBRARY}->can($params{build});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"build"} eq "CODE" or $self->{"library"}->can($params{"build"}) 0 0 0
Condition0%0/3
ref $params{"build"} eq "CODE" or $self->{"library"}->can($params{"build"})
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $params{"build"} eq "CODE" or $self->{"library"}->can($params{"build"})
ref $params{"build"} eq "CODE" $self->{"library"}->can($params{"build"})
Truth table0%0/3
A or B
A= ref $params{"build"} eq "CODE"B= $self->{"library"}->can($params{"build"})
ABresult
1X1
011
000
210  
211 $self->throw("A checksum sub or method name is required")
212 0 ▶ unless $params{checksum};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"checksum"} 0 0 0
213  
214 0 ▶ unless (ref($params{checksum}) eq 'CODE' || $self->{+LIBRARY}->can($params{checksum})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params{"checksum"} eq "CODE" or $self->{"library"}->can($params{"checksum"})) 0 0 0
Condition0%0/3
ref $params{"checksum"} eq "CODE" or $self->{"library"}->can($params{"checksum"})
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $params{"checksum"} eq "CODE" or $self->{"library"}->can($params{"checksum"})
ref $params{"checksum"} eq "CODE" $self->{"library"}->can($params{"checksum"})
Truth table0%0/3
A or B
A= ref $params{"checksum"} eq "CODE"B= $self->{"library"}->can($params{"checksum"})
ABresult
1X1
011
000
215 0 $self->throw(
216 "'$params{checksum}' is not a valid checksum calculator, it must be either a coderef or a class method on '$self->{+LIBRARY}'",
217 %params,
218 );
219 }
220  
221 0 $self->{+DATABASES}->{$name} = {%params, name => $name, driver => $from_spec->{driver}};
222  
223 0 return;
224 }
225  
226 0 ▶sub add_driver {
Subroutine add_driver: not called CC=10 SCAR=47.0
Pod documented
227 0 my $self = shift;
228 0 my ($driver, %params) = @_;
229  
230 0 my $name = $params{name};
231 0 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
232 0 $name = $driver;
233 0 $name =~ s/^.*:://g;
234 0 $params{name} = $name;
235 }
236  
237 $self->throw("Database '$name' is already defined", %params)
238 0 ▶ if $self->{+DATABASES}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"databases"}{$name} 0 0 0
239  
240 0 ▶ $driver = "DBIx::QuickDB::Driver::$driver"
Branch: 1 branch
BranchTrueFalseTotal
unless $driver =~ s/^\+// or $driver =~ /^DBIx::QuickDB::Driver::/ 0 0 0
Condition0%0/3
$driver =~ s/^\+// or $driver =~ /^DBIx::QuickDB::Driver::/
l!l&&r!l&&!r
000
MC/DC0%0/2
$driver =~ s/^\+// or $driver =~ /^DBIx::QuickDB::Driver::/
$driver =~ s/^\+// $driver =~ /^DBIx::QuickDB::Driver::/
Truth table0%0/3
A or B
A= $driver =~ s/^\+//B= $driver =~ /^DBIx::QuickDB::Driver::/
ABresult
1X1
011
000
241 unless $driver =~ s/^\+// || $driver =~ m/^DBIx::QuickDB::Driver::/;
242  
243 0 my $file = $driver;
244 0 $file =~ s{::}{/}g;
245 0 $file .= ".pm";
246 0 ▶ eval { require $file; 1 } or $self->throw("Could not load driver '$driver': $@", %params);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file; 1 } } or $self->throw("Could not load driver '${driver}': $@", %params) 0 0 0
247  
248 0 my ($viable, $why_not) = $driver->viable({bootstrap => 1, autostart => 1, load_sql => 1});
249 0 ▶ $self->throw("Driver '$driver' is not viable:\n$why_not\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $viable 0 0 0
250 unless $viable;
251  
252 0 $params{driver} = $driver;
253 0 ▶ $params{checksum} //= \&driver_checksum;
Condition0%0/2
$params{"checksum"} //= \&DBIx::QuickDB::Pool::driver_checksum
l!l
00
MC/DC0%0/1
$params{"checksum"} //= \&DBIx::QuickDB::Pool::driver_checksum
$params{"checksum"}
Truth table0%0/2
A
A= $params{"checksum"}
Aresult
11
00
254  
255 0 ▶ unless (ref($params{checksum}) eq 'CODE' || $self->{+LIBRARY}->can($params{checksum})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params{"checksum"} eq "CODE" or $self->{"library"}->can($params{"checksum"})) 0 0 0
Condition0%0/3
ref $params{"checksum"} eq "CODE" or $self->{"library"}->can($params{"checksum"})
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $params{"checksum"} eq "CODE" or $self->{"library"}->can($params{"checksum"})
ref $params{"checksum"} eq "CODE" $self->{"library"}->can($params{"checksum"})
Truth table0%0/3
A or B
A= ref $params{"checksum"} eq "CODE"B= $self->{"library"}->can($params{"checksum"})
ABresult
1X1
011
000
256 0 $self->throw(
257 "'$params{checksum}' is not a valid checksum calculator, it must be either a coderef or a class method on '$self->{+LIBRARY}'",
258 %params,
259 );
260 }
261  
262 0 $self->{+DATABASES}->{$name} = \%params;
263  
264 0 return;
265 }
266  
267 25 ▶sub instance_dir_user {
Subroutine instance_dir_user: called CC=5 SCAR=16.1
Pod documented
268 25 my $self = shift;
269  
270 25 ▶ my $user = $ENV{USER} // $ENV{USERNAME} // 'quickdb';
Condition100%5/5
($ENV{"USER"} // $ENV{"USERNAME"}) // "quickdb"
l!l
241
$ENV{"USER"} // $ENV{"USERNAME"}
l!l&&r!l&&!r
2311
MC/DC100%2/2
($ENV{"USER"} // $ENV{"USERNAME"}) // "quickdb"
$ENV{"USER"} $ENV{"USERNAME"}
Truth table100%3/3
A // B
A= $ENV{"USER"}B= $ENV{"USERNAME"}
ABresult
1X1
011
000
271  
272 # A '/' would send tempdir looking for a subdir that does not exist. Real
273 # logins that hit this: 'jdoe@corp.example.com', 'DOMAIN\jdoe'.
274 25 $user =~ s/[^\w.-]+/_/g;
275 25 ▶ $user = 'quickdb' unless length $user;
Branch: 1 branch
BranchTrueFalseTotal
unless length $user 2 23 25
276  
277 # The instance dir must hold a unix socket, and sun_path caps at 107 bytes.
278 25 ▶ $user = substr($user, 0, 12) if length($user) > 12;
Branch: 1 branch
BranchTrueFalseTotal
if length $user > 12 3 22 25
279  
280 25 return $user;
281 }
282  
283 0 ▶sub fetch_db {
Subroutine fetch_db: not called CC=4 SCAR=30.0
Pod documented
284 0 my $self = shift;
285 0 my ($name, %params) = @_;
286  
287 0 my $spec = $self->{+DATABASES}->{$name};
288  
289 0 ▶ $self->throw("Invalid database name: $name", %params)
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 0 0
290 unless $spec;
291  
292 0 delete $params{caller};
293  
294 0 my $from = $self->vivify_db($spec);
295  
296 0 my %add_args;
297 0 ▶ if (my $dir = $self->{+INSTANCE_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $self->{"instance_dir"}) 0 0 0
298 0 require File::Temp;
299 0 my $user = $self->instance_dir_user;
300  
301 # $$ because File::Temp draws names from rand(): identically-seeded
302 # processes walk the same sequence and recycle freed names, and two of
303 # them copying into one dir corrupts the clone. Only 6 X's -- the pid
304 # does the work, and the name competes for the 107-byte socket budget.
305 0 $add_args{dir} = File::Temp::tempdir("$user-$$-XXXXXX", CLEANUP => 0, DIR => $dir);
306 }
307  
308 0 ▶ return $from->clone(autostart => 1, autostop => 1, cleanup => 1, %add_args, %{$spec->{clone_args} || {}}, %params);
Condition0%0/2
$spec->{'clone_args'} || {}
l!l
00
MC/DC0%0/1
$spec->{'clone_args'} || {}
$spec->{'clone_args'}
Truth table0%0/2
A
A= $spec->{'clone_args'}
Aresult
11
00
309 }
310  
311 0 ▶sub vivify_db {
Subroutine vivify_db: not called CC=5 SCAR=34.0
Pod documented
312 0 my $self = shift;
313 0 my ($spec) = @_;
314  
315 0 my ($db, $csum) = $self->cache_check($spec);
316  
317 0 ▶ return $db if $db;
Branch: 1 branch
BranchTrueFalseTotal
if $db 0 0 0
318  
319 0 $spec->{built_checksum} = $csum;
320 0 my $dir = "$self->{+CACHE_DIR}/$spec->{name}-$csum";
321 0 $spec->{dir} = $dir;
322  
323 # Already built, we can use it.
324 0 ▶ if (-e "$dir.READY") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir.READY") 0 0 0
325 0 $self->diag("$$ Found existing '$spec->{name}' database at '$dir'...");
326 0 return $spec->{db} = $self->reclaim($dir => $spec);
327 }
328  
329 0 my $lock = "$dir.lock";
330 0 my $lock_fh = $self->lock($lock);
331  
332 # Another process got the lock first and built the db, start the loop over to build it.
333 0 ▶ if (-e "$dir.READY") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir.READY") { } 0 0 0
334 # Unlock immedietly to unblock other processes
335 0 $self->unlock($lock_fh, $lock);
336 0 $self->diag("$$ Previous lock holder built the '$spec->{name}' db...");
337 0 $db = $self->reclaim($dir => $spec);
338 }
339 else {
340 0 $db = $self->build_db($dir => $spec, stop => 1);
341 # Unlock AFTER building
342 0 $self->unlock($lock_fh, $lock);
343 }
344  
345 0 ▶ $db->stop() if $db->started;
Branch: 1 branch
BranchTrueFalseTotal
if $db->started 0 0 0
346  
347 0 return $spec->{db} = $db;
348 }
349  
350 0 ▶sub cache_check {
Subroutine cache_check: not called CC=17 SCAR=57.2
Pod documented
351 0 my $self = shift;
352 0 my ($spec) = @_;
353  
354 # If we do not have 'update_checksums' set then we do not re-check every time.
355 return ($spec->{db}, $spec->{built_checksum})
356 0 ▶ if $spec->{db} && $spec->{built_checksum} && !$self->{+UPDATE_CHECKSUMS};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"db"} and $spec->{"built_checksum"} and not $self->{"update_checksums"} 0 0 0
Condition0%0/6
$spec->{"db"} and $spec->{"built_checksum"} and not $self->{"update_checksums"}
!ll&&!rl&&r
000
$spec->{"db"} and $spec->{"built_checksum"}
!ll&&!rl&&r
000
MC/DC0%0/3
$spec->{"db"} and $spec->{"built_checksum"} and not $self->{"update_checksums"}
$spec->{"db"} $spec->{"built_checksum"} not $self->{"update_checksums"}
Truth table0%0/4
A and B and not(C)
A= $spec->{"db"}B= $spec->{"built_checksum"}C= not $self->{"update_checksums"}
ABCresult
0XX0
10X0
1100
1111
357  
358 0 my $valid = 1;
359  
360 0 ▶ my $meth = $spec->{checksum} or die "Database '$spec->{name}' has no checksum method";
Branch: 1 branch
BranchTrueFalseTotal
my $meth = $spec->{"checksum"} or die qq[Database '$spec->{"name"}' has no checksum method] 0 0 0
361 0 my $csum = $self->{+LIBRARY}->$meth(name => $spec->{name}, qdb => $self, spec => $spec);
362  
363 0 my $parent_changed = 0;
364 0 ▶ if (my $from = $spec->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $from = $spec->{"from"}) 0 0 0
365 0 my ($parent, $psum) = $self->cache_check($self->{+DATABASES}->{$from});
366  
367 0 ▶ unless ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 0 0 0
368 0 $parent_changed = 1;
369 0 $valid = 0;
370 }
371  
372 # Composite checksum
373  
374 0 $self->diag("$$ Combining checksums from '$spec->{name}': '$csum' and parent '$from': '$psum'...");
375 0 $csum = sha1_hex("$psum$csum");
376 0 $self->diag("$$ Combined checksum from '$spec->{name}' and parent '$from': '$csum'.");
377 }
378  
379 # shorten the checksum to 10 characters for shorter dir names
380 # This is mainly because unix sockets have a max filename of 107 bites.
381 # Sometimes the temp dir + other temp dirs before our socket make the
382 # length too much.
383 0 $csum = substr($csum, 0, 10);
384  
385 0 my $old = $spec->{built_checksum};
386  
387 0 ▶ $valid &&= $old;
Condition0%0/3
$valid &&= $old
!ll&&!rl&&r
000
MC/DC0%0/2
$valid &&= $old
$valid $old
Truth table0%0/3
A &&= B
A= $validB= $old
ABresult
0X0
100
111
388 0 ▶ $valid &&= $spec->{db} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'db'} ? : 0 0 0
Condition0%0/3
$valid &&= $spec->{"db"} ? 1 : 0
!ll&&!rl&&r
000
MC/DC0%0/2
$valid &&= $spec->{"db"} ? 1 : 0
$valid $spec->{"db"} ? 1 : 0
Truth table0%0/3
A &&= B
A= $validB= $spec->{"db"} ? 1 : 0
ABresult
0X0
100
111
389 0 ▶ $valid &&= $old eq $csum;
Condition0%0/3
$valid &&= $old eq $csum
!ll&&!rl&&r
000
MC/DC0%0/2
$valid &&= $old eq $csum
$valid $old eq $csum
Truth table0%0/3
A &&= B
A= $validB= $old eq $csum
ABresult
0X0
100
111
390 0 ▶ $valid &&= !$parent_changed;
Condition0%0/3
$valid &&= !$parent_changed
!ll&&!rl&&r
000
MC/DC0%0/2
$valid &&= !$parent_changed
$valid !$parent_changed
Truth table0%0/3
A &&= not(B)
A= $validB= !$parent_changed
ABresult
0X0
100
111
391  
392 0 ▶ return ($spec->{db}, $csum) if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 0 0 0
393  
394 $self->diag("$$ Invalidated '$spec->{name}' due to " . ($parent_changed ? 'parent' : 'checksum') . " change...")
395 0 ▶ if $old || $spec->{db};
Branch: 2 branches
BranchTrueFalseTotal
if $old or $spec->{"db"} 0 0 0
$parent_changed ? : 0 0 0
Condition0%0/3
$old or $spec->{"db"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$old or $spec->{"db"}
$old $spec->{"db"}
Truth table0%0/3
A or B
A= $oldB= $spec->{"db"}
ABresult
1X1
011
000
396  
397 0 delete $spec->{db};
398 0 delete $spec->{built_checksum};
399  
400 0 ▶ remove_tree_robust($spec->{dir}) if $self->{+PURGE_OLD};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"purge_old"} 0 0 0
401  
402 0 delete $spec->{dir};
403  
404 0 return (undef, $csum);
405 }
406  
407 1 ▶sub build_db {
Subroutine build_db: called CC=7 SCAR=28.5
Pod documented
408 1 my $self = shift;
409 1 my ($dir, $spec, %params) = @_;
410  
411 # Create the dir, deleting it if it exists. Close retained DBI handles first
412 # (essential for SQLite on Windows). If removal still fails, quarantine the
413 # entire stale tree before rebuilding. We must never continue in the old
414 # directory: doing so feeds stale schema/data to the builder and turns the
415 # cleanup problem into misleading errors such as "table already exists".
416 1 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 1 0 1
417 1 disconnect_dbi_handles($dir);
418  
419 1 ▶ unless ($self->_remove_or_quarantine($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_remove_or_quarantine($dir)) 0 1 1
420 0 my $error = "$!";
421 0 ▶ $error = 'unknown operating-system error' unless length $error;
Branch: 1 branch
BranchTrueFalseTotal
unless length $error 0 0 0
422 0 $self->throw("Could not remove or quarantine stale database directory '$dir': $error");
423 }
424 }
425  
426 1 make_path($dir);
427 1 $spec->{dir} = $dir;
428  
429 1 my $db;
430 1 ▶ if ($spec->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'from'}) { } 0 1 1
431 0 $db = $self->build_via_clone($dir, $spec);
432 }
433 else {
434 1 $db = $self->build_via_driver($dir, $spec);
435 }
436  
437 0 ▶ $db->stop if $params{stop};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"stop"} 0 0 0
438  
439 0 $self->diag("$$ Built new db '$spec->{name}', marking as ready!");
440 0 ▶ open(my $ready, '>', "$dir.READY") or die "Could not create ready file '$dir.READY': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ready, ">", "$dir.READY" or die "Could not create ready file '$dir.READY': $!" 0 0 0
441 0 print $ready "1\n";
442 0 close($ready);
443  
444 0 return $db;
445 }
446  
447 0 ▶sub build_via_clone {
Subroutine build_via_clone: not called CC=4 SCAR=30.0
Pod documented
448 0 my $self = shift;
449 0 my ($dir, $spec) = @_;
450  
451 0 $self->diag("$$ Building new db '$spec->{name}' from existing db '$spec->{from}'...");
452  
453 0 my $from_spec = $self->{+DATABASES}->{$spec->{from}};
454 0 my $from = $self->vivify_db($from_spec);
455  
456 0 $self->write_clone_stamp($from);
457 0 $spec->{driver} = $from_spec->{driver};
458 0 ▶ $spec->{driver_args} //= $from_spec->{driver_args};
Condition0%0/3
$spec->{"driver_args"} //= $from_spec->{"driver_args"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec->{"driver_args"} //= $from_spec->{"driver_args"}
$spec->{"driver_args"} $from_spec->{"driver_args"}
Truth table0%0/3
A //= B
A= $spec->{"driver_args"}B= $from_spec->{"driver_args"}
ABresult
1X1
011
000
459 0 ▶ $spec->{clone_args} //= $from_spec->{clone_args};
Condition0%0/3
$spec->{"clone_args"} //= $from_spec->{"clone_args"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec->{"clone_args"} //= $from_spec->{"clone_args"}
$spec->{"clone_args"} $from_spec->{"clone_args"}
Truth table0%0/3
A //= B
A= $spec->{"clone_args"}B= $from_spec->{"clone_args"}
ABresult
1X1
011
000
460  
461 0 ▶ my $db = $from->clone(%{$spec->{clone_args} || {}}, dir => $dir, autostart => 0, autostop => 1, verbose => $self->{+VERBOSE});
Condition0%0/2
$spec->{'clone_args'} || {}
l!l
00
MC/DC0%0/1
$spec->{'clone_args'} || {}
$spec->{'clone_args'}
Truth table0%0/2
A
A= $spec->{'clone_args'}
Aresult
11
00
462  
463 0 $db->start();
464 0 my $builder = $spec->{build};
465 0 $self->{+LIBRARY}->$builder($db, name => $spec->{name}, dir => $dir, qdb => $self);
466  
467 0 return $db;
468 }
469  
470 0 ▶sub build_via_driver {
Subroutine build_via_driver: not called CC=3 SCAR=24.8
Pod documented
471 0 my $self = shift;
472 0 my ($dir, $spec) = @_;
473  
474 0 $self->diag("$$ Building new base db '$spec->{name}'...");
475  
476 my $db = $spec->{driver}->new(
477 0 ▶ %{$spec->{driver_args} || {}},
Condition0%0/2
$spec->{'driver_args'} || {}
l!l
00
MC/DC0%0/1
$spec->{'driver_args'} || {}
$spec->{'driver_args'}
Truth table0%0/2
A
A= $spec->{'driver_args'}
Aresult
11
00
478 dir => $dir,
479 0 verbose => $self->{+VERBOSE},
480 autostart => 0,
481 autostop => 1,
482 );
483  
484 0 $db->bootstrap();
485  
486 0 ▶ if (my $builder = $spec->{build}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $builder = $spec->{"build"}) 0 0 0
487 0 $db->start();
488 0 $self->{+LIBRARY}->$builder($db, name => $spec->{name}, dir => $dir, qdb => $self);
489 }
490  
491 0 $self->write_clone_stamp($db);
492  
493 0 return $db;
494 }
495  
496 0 ▶sub write_clone_stamp {
Subroutine write_clone_stamp: not called CC=2 SCAR=17.9
Pod undocumented
497 0 my $self = shift;
498 0 my ($db) = @_;
499  
500 # Preserve subsecond resolution (callers use it to detect rapid successive
501 # clones), but make the serialized decimal separator locale-independent.
502 # Stringifying Time::HiRes::time on Windows may otherwise produce a comma.
503 0 my $stamp = '' . time();
504 0 $stamp =~ tr/,/./;
505 0 ▶ open(my $fh, '>', $db->dir . '/cloned') or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $db->dir . '/cloned' or die "$!" 0 0 0
506 0 print $fh $stamp, "\n";
507 0 close($fh);
508  
509 0 return $stamp;
510 }
511  
512 0 ▶sub unlock {
Subroutine unlock: not called CC=2 SCAR=17.9
Pod documented
513 0 my $self = shift;
514 0 my ($lock_fh, $lock) = @_;
515  
516 0 $self->diag("$$ Releasing lock '$lock'...");
517 0 ▶ flock($lock_fh, LOCK_UN) or die "Could not unlock '$lock': $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $lock_fh, 8 or die "Could not unlock '${lock}': $!" 0 0 0
518 0 close($lock_fh);
519  
520 0 return 1;
521 }
522  
523 0 ▶sub lock {
Subroutine lock: not called CC=3 SCAR=24.8
Pod documented
524 0 my $self = shift;
525 0 my ($lock) = @_;
526  
527 0 ▶ open(my $lh, '>>', $lock) or die "Could not open lock file '$lock': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $lh, ">>", $lock or die "Could not open lock file '${lock}': $!" 0 0 0
528  
529 0 $self->diag("$$ Acquiring lock '$lock'...");
530 0 ▶ flock($lh, LOCK_EX) or die "Could not get lock '$lock': $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $lh, 2 or die "Could not get lock '${lock}': $!" 0 0 0
531 0 $self->diag("$$ Lock acquired '$lock'...");
532  
533 0 seek($lh, 0, 0);
534 0 return $lh;
535 }
536  
537 0 ▶sub reclaim {
Subroutine reclaim: not called CC=2 SCAR=17.9
Pod documented
538 0 my $self = shift;
539 0 my ($dir, $spec) = @_;
540  
541 return $spec->{driver}->new(
542 0 ▶ %{$spec->{driver_args} || {}},
Condition0%0/2
$spec->{'driver_args'} || {}
l!l
00
MC/DC0%0/1
$spec->{'driver_args'} || {}
$spec->{'driver_args'}
Truth table0%0/2
A
A= $spec->{'driver_args'}
Aresult
11
00
543 dir => $dir,
544 0 verbose => $self->{+VERBOSE},
545 autostart => 0,
546 autostop => 1,
547 );
548 }
549  
550 0 ▶sub driver_checksum {
Subroutine driver_checksum: not called CC=1 SCAR=6.9
Pod documented
551 0 my $library = shift;
552 0 my %params = @_;
553  
554 0 my $spec = $params{spec};
555 0 my $version = $spec->{driver}->version_string($spec->{driver_args});
556  
557 0 return sha1_hex($version);
558 }
559  
560 2 1;
561  
562 __END__
« blib/lib/DBIx/QuickDB/Driver/MySQLCom.pm ↑ Summary lib/DBIx/QuickDB/Watcher.pm »