Distlinks/DBI.pm

filter: Statementstmt 85.8% 151 / 176 Branchbran 59.3% 19 / 32 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.6% 29 / 32 Podpod n/a totaltotal 80.8% 207 / 256 CC 22 SCAR 11.9
CC
22
Cov
81%
read_page
8.1
recheck
6.0
__ANON__
5.1
CRAP 3.3
« Distlinks/URIterator.pm ↑ Summary
1 # Copyright 2009, 2010, 2011, 2012, 2013 Kevin Ryde
2  
3 # This file is part of Distlinks.
4 #
5 # Distlinks is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Distlinks is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Distlinks. If not, see <http://www.gnu.org/licenses/>.
17  
18 package App::Distlinks::DBI;
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use List::MoreUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use base 'Class::Singleton';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use base 'DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 our $VERSION = 11;
28  
29 1 my $verbose = 0;
30  
31 1 ▶sub _new_instance {
Subroutine _new_instance: called CC=4 SCAR=14.1
32 1 my ($class) = @_;
33  
34 1 ▶ my $home_directory = File::HomeDir->my_home
Condition50%1/2
'File::HomeDir'->my_home || die("No home directory found by File::HomeDir\n")
l!l
10
MC/DC0%0/1
'File::HomeDir'->my_home || die("No home directory found by File::HomeDir\n")
'File::HomeDir'->my_home
Truth table50%1/2
A
A= 'File::HomeDir'->my_home
Aresult
11
00
35 || die "No home directory found by File::HomeDir\n";
36  
37 1 my $db_filename = File::Spec->catfile ($home_directory, '.distlinks.sqdb');
38 0 ▶ if ($verbose) { print "database $db_filename\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 1 1
39  
40 1 my $dbh = $class->connect ("dbi:SQLite:dbname=$db_filename",
41 '', '', {RaiseError=>1});
42 1 $dbh->func(90_000, 'busy_timeout'); # 90 seconds
43 1 $dbh->{'private_App_Distlinks'}->{'filename'} = $db_filename;
44 1 $dbh->do ('PRAGMA foreign_keys = 1');
45  
46 1 ▶ if (! $dbh->table_exists ('page')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->table_exists("page")) 1 0 1
47 1 print "Create $db_filename\n";
48  
49 1 $dbh->do (<<'HERE');
50 CREATE TABLE page (
51 url TEXT NOT NULL PRIMARY KEY,
52 timestamp TEXT NOT NULL,
53 is_success BOOLEAN NOT NULL,
54 status_code INTEGER NOT NULL,
55 status_line TEXT NOT NULL,
56 etag TEXT DEFAULT NULL,
57 last_modified TEXT DEFAULT NULL,
58 redir_location TEXT DEFAULT NULL,
59 anchors BOOLEAN NOT NULL DEFAULT FALSE)
60 HERE
61 # index for faster expiry checking
62 1 $dbh->do (<<'HERE');
63 CREATE INDEX page_timestamp_index ON page (timestamp)
64 HERE
65  
66 1 $dbh->do (<<'HERE');
67 CREATE TABLE anchor (
68 url TEXT NOT NULL,
69 anchor TEXT NOT NULL,
70 PRIMARY KEY (url, anchor),
71 FOREIGN KEY(url) REFERENCES page(url) ON DELETE CASCADE)
72 HERE
73 }
74  
75 1 return $dbh;
76  
77 # if (0) {
78 # my $count = 0;
79 # foreach my $url (keys %nodb_urls) {
80 # $count += $dbh->do ('DELETE FROM page WHERE url LIKE ?',
81 # undef,
82 # "$url%");
83 # }
84 # if ($count != 0) {
85 # print "$count no-db urls deleted\n";
86 # }
87 # }
88 }
89  
90 package App::Distlinks::DBI::db;
91 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 1 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 ▶use base 'DBI::db';
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 #------------------------------------------------------------------------------
98 # page records
99  
100 5 ▶sub read_page {
Subroutine read_page: called CC=8 SCAR=20.9
101 5 my ($dbh, $url, $anchor) = @_;
102 5 $url = URI->new($url)->canonical;
103  
104 5 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) 3 2 5
105 3 my $sth = $dbh->prepare_cached
106 ('SELECT * FROM anchor WHERE url=? and anchor=?');
107 3 my $info = $dbh->selectrow_hashref($sth,undef,$url,$anchor);
108 3 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 1 2 3
109 1 $info->{'is_success'} = 1;
110 1 $info->{'status_code'} = 200;
111 1 $info->{'anchors'} = 1;
112 1 return $info;
113 }
114 }
115 4 my $info;
116 4 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) { } 2 2 4
117 2 my $sth = $dbh->prepare_cached
118 ('SELECT * FROM page LEFT JOIN anchor ON page.url = anchor.url
119 WHERE page.url=?');
120 2 $sth->execute($url);
121 2 $info = $sth->fetchrow_hashref;
122 2 $info->{'have_anchors'} = [];
123 2 ▶ if ($info && defined $info->{'anchor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info and defined $info->{"anchor"}) 1 1 2
Condition66%2/3
$info and defined $info->{"anchor"}
!ll&&!rl&&r
011
MC/DC50%1/2
$info and defined $info->{"anchor"}
$info defined $info->{"anchor"}
Truth table66%2/3
A and B
A= $infoB= defined $info->{"anchor"}
ABresult
0X0
100
111
124 1 push @{$info->{'have_anchors'}}, delete $info->{'anchor'};
125 1 ▶ while (my $more = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $more = $sth->fetchrow_hashref) 0 1 1
126 0 push @{$info->{'have_anchors'}}, $more->{'anchor'};
127 }
128 }
129 2 $sth->finish;
130 2 $info->{'anchor_not_found'} = 1;
131  
132 } else {
133 2 my $sth = $dbh->prepare_cached ('SELECT * FROM page WHERE url=?');
134 2 $info = $dbh->selectrow_hashref($sth,undef,$url);
135 }
136 4 ▶ return ($info || {});
Condition100%2/2
$info || {}
l!l
31
MC/DC100%1/1
$info || {}
$info
Truth table100%2/2
A
A= $info
Aresult
11
00
137 }
138  
139 0 ▶sub read_anchors {
Subroutine read_anchors: not called CC=1 SCAR=6.9
140 0 my ($dbh, $url) = @_;
141 0 my $sth = $dbh->prepare_cached
142 ('SELECT anchor FROM anchor WHERE url=?');
143 0 return $dbh->selectcol_arrayref($sth,undef,$url);
144 }
145  
146  
147 1 ▶sub write_page {
Subroutine write_page: called CC=1 SCAR=0.0
148 1 my ($dbh, $info) = @_;
149 $dbh->call_with_transaction
150 1 ▶ (sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
151 1 my $url = $info->{'url'};
152 1 $url = URI->new($url)->canonical;
153  
154 1 { my $sth = $dbh->prepare_cached ('DELETE FROM anchor WHERE url = ?');
155 1 $sth->execute ($url);
156 }
157 1 { my $sth = $dbh->prepare_cached ('DELETE FROM page WHERE url = ?');
158 1 $sth->execute ($url);
159 }
160  
161 {
162 1 my $sth = $dbh->prepare_cached
163 ('INSERT INTO page (url,timestamp,
164 is_success,status_code,status_line,
165 etag,last_modified,
166 redir_location,anchors)
167 VALUES(?,?, ?,?,?, ?,?, ?,?)');
168 $sth->execute ($url, timestamp_now(),
169 $info->{'is_success'} ? 1 : 0,
170 $info->{'status_code'},
171 $info->{'status_line'},
172 $info->{'etag'},
173 $info->{'last_modified'},
174 $info->{'redir_location'},
175 1 ▶ $info->{'anchors'} ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$info->{'is_success'} ? : 1 0 1
$info->{'anchors'} ? : 1 0 1
176 }
177 1 ▶ if (my $anchors = $info->{'anchors'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $anchors = $info->{"anchors"}) 1 0 1
178 1 my $sth = $dbh->prepare_cached
179 ('INSERT INTO anchor (url,anchor) VALUES(?,?)');
180 1 foreach my $anchor (List::MoreUtils::uniq (@$anchors)) {
181 1 $sth->execute ($url, $anchor);
182 }
183 }
184 1 });
185 }
186  
187 1 my $expire_days = 30;
188  
189 1 ▶sub expire {
Subroutine expire: called CC=3 SCAR=13.1
190 1 my ($dbh, $always_print) = @_;
191 # always expire file:// urls so as to recheck on next run
192 1 $dbh->do ('DELETE FROM page WHERE url LIKE \'file://%\'');
193  
194 1 my $count = $dbh->do
195 ('DELETE FROM page WHERE timestamp < ? OR timestamp > ?',
196 undef,
197 timestamp_range ($expire_days * 86400));
198 1 $count += 0; # numize '0E0' return when none deleted
199  
200 1 ▶ if ($count || $always_print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count or $always_print) 0 1 1
Condition33%1/3
$count or $always_print
l!l&&r!l&&!r
001
MC/DC0%0/2
$count or $always_print
$count $always_print
Truth table33%1/3
A or B
A= $countB= $always_print
ABresult
1X1
011
000
201 0 my ($kept) = $dbh->selectrow_array('SELECT COUNT(*) FROM page');
202 0 my ($anchors) = $dbh->selectrow_array('SELECT COUNT(*) FROM anchor');
203 0 print "expired $count links, kept $kept (with $anchors anchors)\n";
204 }
205 1 return $count;
206 }
207  
208 1 ▶sub vacuum {
Subroutine vacuum: called CC=1 SCAR=0.0
209 1 my ($dbh) = @_;
210 1 my $db_filename = $dbh->{'private_App_Distlinks'}->{'filename'};
211 1 my $old_size = -s $db_filename;
212 1 my $old_kbytes = int (($old_size + 1023) / 1024);
213 1 $dbh->do ('VACUUM');
214 1 my $new_size = -s $db_filename;
215 1 my $new_kbytes = int (($new_size + 1023) / 1024);
216 1 print "vacuum database to now ${new_kbytes}k (was ${old_kbytes}k)\n";
217 }
218  
219 0 ▶sub recheck {
Subroutine recheck: not called CC=2 SCAR=17.9
220 0 my ($dbh, $url) = @_;
221 0 my $count = 0;
222 0 ▶ if ($url =~ /^$RE{net}{domain}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^$App::Distlinks::DBI::db::RE{'net'}{'domain'}$/) { } 0 0 0
223 0 my $pattern = "%$url/%";
224 0 $count = $dbh->do ('DELETE FROM page WHERE url LIKE ?', undef, $pattern);
225 } else {
226 0 $count = $dbh->do ('DELETE FROM page WHERE url = ?', undef, $url);
227 }
228 0 $count += 0; # numize '0E0' return when none deleted
229 0 print "recheck $count links\n";
230 }
231  
232  
233 #------------------------------------------------------------------------------
234 # transaction
235  
236 # rollback() can get errors too, like database gone away. They end up
237 # thrown in preference to the original error.
238 #
239 1 ▶sub call_with_transaction {
Subroutine call_with_transaction: called CC=3 SCAR=12.2
240 1 my $dbh = shift;
241 1 my $subr = shift;
242  
243 1 ▶ if ($dbh->{'AutoCommit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{'AutoCommit'}) { } 1 0 1
244 1 my $commit = App::Distlinks::DBI::Commit->new ($dbh);
245 1 $dbh->begin_work;
246 1 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 1 1
247 0 my @ret = $subr->(@_);
248 0 $commit->ok;
249 0 return @ret;
250 } else {
251 1 my $ret = $subr->(@_);
252 1 $commit->ok;
253 1 return $ret;
254 }
255  
256 } else {
257 0 return $subr->(@_);
258 }
259 }
260 {
261 package App::Distlinks::DBI::Commit;
262 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
263 1 my ($class, $dbh) = @_;
264 1 return bless { dbh => $dbh,
265 method => 'rollback',
266 }, $class;
267 }
268 1 ▶ sub ok {
Subroutine ok: called CC=1 SCAR=0.0
269 1 my ($self) = @_;
270 1 $self->{'method'} = 'commit';
271 }
272 1 ▶ sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
273 1 my ($self) = @_;
274 1 my $method = $self->{'method'};
275 1 $self->{'dbh'}->$method;
276 }
277 }
278  
279 # return true if $dbh contains a table called $table
280 1 ▶sub table_exists {
Subroutine table_exists: called CC=2 SCAR=7.0
281 1 my ($dbh, $table) = @_;
282 1 my $sth = $dbh->table_info (undef, undef, $table, undef);
283 1 ▶ my $exists = $sth->fetchrow_arrayref ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetchrow_arrayref ? : 0 1 1
284 1 $sth->finish;
285 1 return $exists;
286 }
287  
288 #------------------------------------------------------------------------------
289 # timestamps
290  
291 # return strings ($lo, $hi)
292 1 ▶sub timestamp_range {
Subroutine timestamp_range: called CC=1 SCAR=0.0
293 1 my ($seconds) = @_;
294 1 my $t = time();
295 1 my $lo = $t - $seconds;
296 1 my $hi = $t + 6*3600; # 2 hours future
297 1 return (timet_to_timestamp($lo),
298 timet_to_timestamp($hi));
299 }
300 1 ▶sub timestamp_now {
Subroutine timestamp_now: called CC=1 SCAR=0.0
301 1 return timet_to_timestamp(time());
302 }
303 3 ▶sub timet_to_timestamp {
Subroutine timet_to_timestamp: called CC=2 SCAR=7.0
304 3 my ($t) = @_;
305 3 ▶ my @gmtime = gmtime($t) or die "Oops, gmtime($t) not supported";
Branch: 1 branch
BranchTrueFalseTotal
my(@gmtime) = gmtime $t or die "Oops, gmtime($t) not supported" 3 0 3
306 3 require POSIX;
307 3 return POSIX::strftime ('%Y-%m-%d %H:%M:%S+00:00', @gmtime);
308 }
309 0 ▶sub timestamp_to_timet {
Subroutine timestamp_to_timet: not called CC=1 SCAR=6.9
310 0 my ($timestamp) = @_;
311 0 my ($year, $month, $day, $hour, $minute, $second)
312 = split /[- :+]/, $timestamp;
313 0 require Time::Local;
314 0 return Time::Local::timegm
315 ($second, $minute, $hour, $day, $month-1, $year-1900);
316 }
317  
318 package App::Distlinks::DBI::st;
319 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
320 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
321 1 ▶use base 'DBI::st';
Subroutine BEGIN: called CC=1 SCAR=0.0
322  
323 1 1;
324 __END__
« Distlinks/URIterator.pm ↑ Summary