blib/lib/SeeAlso/Source/BeaconAggregator/Maintenance.pm

filter: Statementstmt 78.2% 638 / 815 Branchbran 60.1% 455 / 756 Conditioncond 50.5% 137 / 271 MC/DCmcdc 24.2% 38 / 157 Subroutinesub 100% 34 / 34 Podpod 86.9% 20 / 23 totaltotal 64.3% 1322 / 2056 SCAR 91.1
CC
482
Cov
67%
loadFile
892.1
processbeaconheader
429.1
update
414.6
CRAP 9002.8
« blib/lib/SeeAlso/Source/BeaconAggregator/Publisher.pm ↑ Summary blib/script/sasbactrl »
1 package SeeAlso::Source::BeaconAggregator::Maintenance;
2 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 BEGIN {
6 37 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶ $VERSION = '0.2_90';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 @ISA = qw(Exporter);
10 #Give a hoot don't pollute, do not export more than needed by default
11 37 @EXPORT = qw();
12 37 @EXPORT_OK = qw();
13 37 %EXPORT_TAGS = ();
14 }
15  
16 37 ▶use base ("SeeAlso::Source::BeaconAggregator");
Subroutine BEGIN: called CC=1 SCAR=0.0
17 37 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 37 ▶use HTTP::Date; # not perfect, but the module is commonly installed...
Subroutine BEGIN: called CC=1 SCAR=0.0
19 37 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 37 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 37 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 sasbactrl.pl - command line interface to SeeAlso::Source::BeaconAggregator and
26 auxiliary classes
27  
28 =head1 SYNOPSIS
29  
30  
31 =head1 DESCRIPTION
32  
33 This Module allows a collection of BEACON files (cf. http://de.wikipedia.org/wiki/Wikipedia:BEACON)
34 to be used as SeeAlso::Source (probably in the context of an SeeAlso::Server application).
35 Therefore it implements the four methods documented in SeeAlso::Source
36  
37 The BEACON files (lists of non-local identifiers of a certain type documenting the coverage of a given
38 online database plus means for access) are imported by the methods provided by
39 SeeAlso::Source::BeaconAggregator::Maintenance.pm, usually by employing the script sasbactrl.pl
40 as command line client.
41  
42 Serving other formats than SeeAlso or providing a BEACON file with respect to this
43 SeeAlso service is achieved by using SeeAlso::Source::BeaconAggregator::Publisher.
44  
45  
46 =head1 USAGE
47  
48 Use the C<new()> method inherited from C<SeeAlso::Source::BeaconAggregator> to
49 access an existing database or create a new one.
50  
51  
52 =head2 Database Methods
53  
54 =head3 init( [ %options] )
55  
56 Sets up and initializes the database structure for the object.
57 This has to be done once after creating a new database and after
58 upgrading this module.
59  
60 Valid options include:
61  
62 =over 8
63  
64 =item verbose
65  
66 =item prepareRedirs
67  
68 =item identifierClass
69  
70 =back
71  
72  
73 The I<repos> table contains as columns all valid beacon fields plus
74 the following administrative fields which have to be prefixed with
75 "_" in the interface:
76  
77 =over 8
78  
79 =item seqno
80  
81 Sequence number: Is incremented on any successfull load
82  
83 =item alias
84  
85 Unique key: On update older seqences with the same alias are
86 automatically discarded. Most methods take an alias as
87 argument thus obliterating the need to determine the sequence
88 number.
89  
90 =item sort
91  
92 optional sort key
93  
94  
95 =item uri
96  
97 Overrides the #FEED header for updates
98  
99 =item ruri
100  
101 Real uri from which the last instance was loaded
102  
103  
104 =item ftime
105  
106 Fetch time: Timestamp as to when this instance was loaded
107  
108 Clear this or mtime to force automatic reload.
109  
110 =item fstat
111  
112 Short statistics line of last successful reload on update.
113  
114  
115 =item mtime
116  
117 Modification time: Timestamp of the file / HTTP object from which this instance was loaded.
118 Identical to ftime if no timestamp is provided
119  
120 Clear this or ftime to force automatic reload on update.
121  
122  
123 =item utime
124  
125 Timestamp of last update attempt
126  
127 =item ustat
128  
129 Short status line of last update attempt.
130  
131  
132 =item counti
133  
134 Identifier count
135  
136 =item countu
137  
138 Unique identifier count
139  
140  
141 =item admin
142  
143 Just to store some remarks.
144  
145 =back
146  
147 The I<beacons> table stores the individual beacon entries from the input files.
148 Its columns are:
149  
150 =over 8
151  
152 =item hash
153  
154 Identifier. If a (subclass of) C<SeeAlso::Source::Identifier> instance is provided,
155 this will be transformed by the C<hash()> method.
156  
157 =item seqno
158  
159 Sequence number of the beacon file in the database
160  
161 =item altid
162  
163 optional identifier from an alternative identifier system for use
164 with ALTTARGET templates.
165  
166 =item hits
167  
168 optional number of hits for this identifier in the given resource
169  
170 =item info
171  
172 optional information text
173  
174 =item link
175  
176 optional explicit URL
177  
178 =back
179  
180  
181 The I<osd> table contains C<key>, C<val> pairs for various metadata
182 concerning the collection as such, notably the values needed for
183 the Open Search Description and the Header fields needed in case
184 of publishing a beacon file for this collection.
185  
186 The I<admin> table stores (unique) C<key>, C<val> pairs for
187 general persistent data. Currently the following keys are defined:
188  
189 =over 8
190  
191 =item DATA_VERSION
192  
193 Integer version number to migrate database layout.
194  
195 =item IDENTIFIER_CLASS
196  
197 Name of the Identifier class to be used.
198  
199 =item REDIRECTION_INDEX
200  
201 Control creation of an additional index for the I<altid> column
202 (facialiates reverse lookups as needed for clustering).
203  
204 =back
205  
206  
207 =cut
208  
209 sub init {
210 9 ▶ my ($self, %options) = @_;
Subroutine init: called CC=51 SCAR=53.6
Pod documented
211 9 ▶ $options{'verbose'} = $self->{'verbose'} unless exists $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"verbose"} 9 0 9
212  
213 9 my @fieldlist = SeeAlso::Source::BeaconAggregator->beaconfields();
214 9 my @bf = map{ join(" ", @{[SeeAlso::Source::BeaconAggregator->beaconfields($_)]}[0..1]) } @fieldlist;
215 9 ▶ my $hdl = $self->{dbh} or croak("no database handle?");
Branch: 1 branch
BranchTrueFalseTotal
my $hdl = $self->{"dbh"} or croak("no database handle?") 0 9 9
216  
217 9 local($") = ",\n";
218 9 ▶ $hdl->do(<<"XxX"
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE TABLE IF NOT EXISTS repos (\n seqno INTEGER PRIMARY KEY AUTOINCREMENT,\n alias TEXT,\n sort TEXT,\n uri VARCHAR(512),\n ruri VARCHAR(512),\n mtime INTEGER,\n utime INTEGER,\n ftime INTEGER,\n counti INTEGER DEFAULT 0,\n countu INTEGER DEFAULT 0,\n fstat TEXT,\n ustat TEXT,\n admin VARCHAR(512),\n @bf\n);\n") or croak('Setup error: ' . $hdl->errstr) 0 9 9
219 CREATE TABLE IF NOT EXISTS repos (
220 seqno INTEGER PRIMARY KEY AUTOINCREMENT,
221 alias TEXT,
222 sort TEXT,
223 uri VARCHAR(512),
224 ruri VARCHAR(512),
225 mtime INTEGER,
226 utime INTEGER,
227 ftime INTEGER,
228 counti INTEGER DEFAULT 0,
229 countu INTEGER DEFAULT 0,
230 fstat TEXT,
231 ustat TEXT,
232 admin VARCHAR(512),
233 @bf
234 );
235 XxX
236 ) or croak("Setup error: ".$hdl->errstr);
237  
238 9 ▶ $hdl->do("CREATE UNIQUE INDEX IF NOT EXISTS seqnos ON repos(seqno);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE UNIQUE INDEX IF NOT EXISTS seqnos ON repos(seqno);") or croak('Setup error: ' . $hdl->errstr) 0 9 9
239 9 ▶ $hdl->do("CREATE INDEX IF NOT EXISTS aliases ON repos(alias);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE INDEX IF NOT EXISTS aliases ON repos(alias);") or croak('Setup error: ' . $hdl->errstr) 0 9 9
240  
241 9 ▶ $hdl->do(<<"XxX"
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE TABLE IF NOT EXISTS beacons (\n hash CHARACTER(64) NOT NULL,\n seqno INTEGER REFERENCES repos(seqno) ON DELETE CASCADE,\n altid TEXT,\n hits INTEGER,\n info VARCHAR(255),\n link VARCHAR(1024) \n);\n") or croak('Setup error: ' . $hdl->errstr) 0 9 9
242 CREATE TABLE IF NOT EXISTS beacons (
243 hash CHARACTER(64) NOT NULL,
244 seqno INTEGER REFERENCES repos(seqno) ON DELETE CASCADE,
245 altid TEXT,
246 hits INTEGER,
247 info VARCHAR(255),
248 link VARCHAR(1024)
249 );
250 XxX
251 ) or croak("Setup error: ".$hdl->errstr);
252  
253  
254 # Faciliate lookups
255 9 ▶ $hdl->do("CREATE INDEX IF NOT EXISTS lookup ON beacons(hash);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE INDEX IF NOT EXISTS lookup ON beacons(hash);") or croak('Setup error: ' . $hdl->errstr) 0 9 9
256 # maintenance and enforce constraints
257 # (Problem: Dupes w/o altid but differing in link *and* info fields should be legitimate, too)
258 9 ▶ $hdl->do("CREATE UNIQUE INDEX IF NOT EXISTS mntnce ON beacons(seqno, hash, altid);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE UNIQUE INDEX IF NOT EXISTS mntnce ON beacons(seqno, hash, altid);") or croak('Setup error: ' . $hdl->errstr) 0 9 9
259  
260 # foreign key on cascade does not work?
261  
262 9 ▶ $hdl->do(<<"XxX"
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE TRIGGER IF NOT EXISTS on_delete_seqno BEFORE DELETE ON repos FOR EACH ROW\n BEGIN\n DELETE FROM beacons WHERE seqno=OLD.seqno;\n END;\n") or croak('Setup error: ' . $hdl->errstr) 0 9 9
263 CREATE TRIGGER IF NOT EXISTS on_delete_seqno BEFORE DELETE ON repos FOR EACH ROW
264 BEGIN
265 DELETE FROM beacons WHERE seqno=OLD.seqno;
266 END;
267 XxX
268 ) or croak("Setup error: ".$hdl->errstr);
269  
270 # OpenSearchDescription
271 9 ▶ $hdl->do(<<"XxX"
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE TABLE IF NOT EXISTS osd (\n key CHAR(20) NOT NULL,\n val VARCHAR(1024)\n);\n") or croak('Setup error: ' . $hdl->errstr) 0 9 9
272 CREATE TABLE IF NOT EXISTS osd (
273 key CHAR(20) NOT NULL,
274 val VARCHAR(1024)
275 );
276 XxX
277 ) or croak("Setup error: ".$hdl->errstr);
278 9 ▶ $hdl->do("CREATE INDEX IF NOT EXISTS OSDKeys ON osd(key);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE INDEX IF NOT EXISTS OSDKeys ON osd(key);") or croak('Setup error: ' . $hdl->errstr) 0 9 9
279  
280 # Admin Stuff
281 9 ▶ $hdl->do(<<"XxX"
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE TABLE IF NOT EXISTS admin (\n key CHAR(20) PRIMARY KEY NOT NULL,\n val VARCHAR(1024)\n);\n") or croak('Setup error: ' . $hdl->errstr) 0 9 9
282 CREATE TABLE IF NOT EXISTS admin (
283 key CHAR(20) PRIMARY KEY NOT NULL,
284 val VARCHAR(1024)
285 );
286 XxX
287 ) or croak("Setup error: ".$hdl->errstr);
288  
289 9 ▶ $hdl->do("CREATE UNIQUE INDEX IF NOT EXISTS ADMKeys ON admin(key);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE UNIQUE INDEX IF NOT EXISTS ADMKeys ON admin(key);") or croak('Setup error: ' . $hdl->errstr) 0 9 9
290  
291 9 my $admref = $self->admhash();
292  
293 9 my $verkey = "DATA_VERSION";
294 9 my $goalver = $SeeAlso::Source::BeaconAggregator::DATA_VERSION;
295 9 ▶ my $dbver = $admref->{$verkey} || 0;
Condition100%2/2
$admref->{$verkey} || 0
l!l
81
MC/DC100%1/1
$admref->{$verkey} || 0
$admref->{$verkey}
Truth table100%2/2
A
A= $admref->{$verkey}
Aresult
11
00
296 9 ▶ if ( $dbver != $goalver ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbver != $goalver) { } 1 8 9
elsif ($options{'verbose'}) { } 0 8 8
297 1 print "NOTICE: Database version $dbver: Upgrading to $goalver\n";
298 # alter tables here
299 1 ▶ if ( $dbver < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbver < 2) 1 0 1
300 # my ($at, $type) = SeeAlso::Source::BeaconAggregator->beaconfields("COUNT");
301 # $hdl->do("ALTER TABLE repos ADD COLUMN $at $type;");
302 # ($at, $type) = SeeAlso::Source::BeaconAggregator->beaconfields("REMARK");
303 # $hdl->do("ALTER TABLE repos ADD COLUMN $at $type;");
304 };
305 }
306 elsif ( $options{'verbose'} ) {
307 0 print "INFO: Database version $dbver is current\n"};
308  
309 9 ▶ unless ( $dbver == $goalver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbver == $goalver) 1 8 9
310 1 my $verh = $self->stmtHdl("INSERT OR REPLACE INTO admin VALUES (?, ?);", "update version statement");
311 1 ▶ $verh->execute($verkey, $goalver)
Branch: 1 branch
BranchTrueFalseTotal
$verh->execute($verkey, $goalver) or croak('Could not execute update version statement: ' . $verh->errstr) 0 1 1
312 or croak("Could not execute update version statement: ".$verh->errstr);
313 };
314  
315 9 ▶ unless ( exists $options{'identifierClass'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"identifierClass"}) 3 6 9
316 3 ▶ $options{'identifierClass'} = $self->{'identifierClass'} if exists $self->{'identifierClass'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"identifierClass"} 1 2 3
317 };
318  
319 9 my $ickey = "IDENTIFIER_CLASS";
320 9 ▶ if ( (exists $options{identifierClass}) and (my $wanttype = ref($options{'identifierClass'})) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options{'identifierClass'} and my $wanttype = ref $options{'identifierClass'}) { } 5 4 9
elsif (exists $options{'identifierClass'} and not $options{'identifierClass'}) { } 2 4 6
Condition100%3/3
exists $options{"identifierClass"} and my $wanttype = ref $options{"identifierClass"}
!ll&&!rl&&r
225
MC/DC100%2/2
exists $options{"identifierClass"} and my $wanttype = ref $options{"identifierClass"}
exists $options{"identifierClass"} my $wanttype = ref $options{"identifierClass"}
Truth table100%3/3
A and B
A= exists $options{"identifierClass"}B= my $wanttype = ref $options{"identifierClass"}
ABresult
0X0
100
111
321 5 ▶ if ( (exists $self->{identifierClass}) && (ref($self->{identifierClass}) ne $wanttype) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"identifierClass"} and ref $self->{"identifierClass"} ne $wanttype) 1 4 5
Condition100%3/3
exists $self->{"identifierClass"} and ref $self->{"identifierClass"} ne $wanttype
!ll&&!rl&&r
221
MC/DC100%2/2
exists $self->{"identifierClass"} and ref $self->{"identifierClass"} ne $wanttype
exists $self->{"identifierClass"} ref $self->{"identifierClass"} ne $wanttype
Truth table100%3/3
A and B
A= exists $self->{"identifierClass"}B= ref $self->{"identifierClass"} ne $wanttype
ABresult
0X0
100
111
322 1 croak("Cannot override identifierClass set on new()")};
323 4 ▶ if ( my $oldtype = $admref->{$ickey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $oldtype = $admref->{$ickey}) { } 2 2 4
324 2 ▶ croak ("Identifier mismatch: Cannot set to $wanttype since database already branded to $oldtype")
Branch: 1 branch
BranchTrueFalseTotal
unless $oldtype eq $wanttype 0 2 2
325 unless($oldtype eq $wanttype);
326 }
327 else {
328 2 ▶ print "fixing identifierClass as $wanttype\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 2 2
329 2 my $ichdl = $self->stmtHdl("INSERT INTO admin VALUES (?, ?);", "fix identifier class statement");
330 2 ▶ $ichdl->execute($ickey, $wanttype)
Branch: 1 branch
BranchTrueFalseTotal
$ichdl->execute($ickey, $wanttype) or croak('Could not execute fix identifier class statement: ' . $ichdl->errstr) 0 2 2
331 or croak("Could not execute fix identifier class statement: ".$ichdl->errstr);
332 2 $self->{identifierClass} = $options{identifierClass};
333 };
334 }
335 ▶ elsif ( (exists $options{identifierClass}) and (not $options{identifierClass}) ) {
Condition66%2/3
exists $options{"identifierClass"} and not $options{"identifierClass"}
!ll&&!rl&&r
202
MC/DC50%1/2
exists $options{"identifierClass"} and not $options{"identifierClass"}
exists $options{"identifierClass"} not $options{"identifierClass"}
Truth table66%2/3
A and not(B)
A= exists $options{"identifierClass"}B= not $options{"identifierClass"}
ABresult
0X0
100
111
336 2 ▶ print "removing fixed identifierClass from admin table\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 2 2
337 2 my $ichdl = $self->stmtHdl("DELETE FROM admin WHERE key=?;", "identifier class statement");
338 2 ▶ $ichdl->execute($ickey)
Branch: 1 branch
BranchTrueFalseTotal
$ichdl->execute($ickey) or croak('Could not execute remove identifier class statement: ' . $ichdl->errstr) 0 2 2
339 or croak("Could not execute remove identifier class statement: ".$ichdl->errstr);
340 2 delete $self->{identifierClass};
341 };
342  
343 8 my $rikey = "REDIRECTION_INDEX";
344 8 ▶ if ( exists $options{prepareRedirs} or exists $admref->{$rikey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"prepareRedirs"} or exists $admref->{$rikey}) 0 8 8
Condition33%1/3
exists $options{"prepareRedirs"} or exists $admref->{$rikey}
l!l&&r!l&&!r
008
MC/DC0%0/2
exists $options{"prepareRedirs"} or exists $admref->{$rikey}
exists $options{"prepareRedirs"} exists $admref->{$rikey}
Truth table33%1/3
A or B
A= exists $options{"prepareRedirs"}B= exists $admref->{$rikey}
ABresult
1X1
011
000
345 0 my $rihdl = $self->stmtHdl("INSERT OR REPLACE INTO admin VALUES (?, ?);", "fix redirection index statement");
346 0 ▶ if ( $options{prepareRedirs} or ( $admref->{$rikey} and not exists $options{prepareRedirs} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'prepareRedirs'} or $admref->{$rikey} and not exists $options{'prepareRedirs'}) { } 0 0 0
elsif (not $admref->{$rikey} && ($options{'prepareRedirs'} || !exists $options{'prepareRedirs'})) { } 0 0 0
Condition0%0/6
$options{"prepareRedirs"} or $admref->{$rikey} and not exists $options{"prepareRedirs"}
l!l&&r!l&&!r
000
$admref->{$rikey} and not exists $options{"prepareRedirs"}
!ll&&!rl&&r
000
MC/DC0%0/3
$options{"prepareRedirs"} or $admref->{$rikey} and not exists $options{"prepareRedirs"}
$options{"prepareRedirs"} $admref->{$rikey} not exists $options{"prepareRedirs"}
Truth table0%0/4
A or B and not(C)
A= $options{"prepareRedirs"}B= $admref->{$rikey}C= not exists $options{"prepareRedirs"}
ABCresult
1XX1
0111
00X0
0100
347 0 ▶ print "creating redirection index\n" if $options{prepareRedirs} and $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"prepareRedirs"} and $options{"verbose"} 0 0 0
Condition0%0/3
$options{"prepareRedirs"} and $options{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"prepareRedirs"} and $options{"verbose"}
$options{"prepareRedirs"} $options{"verbose"}
Truth table0%0/3
A and B
A= $options{"prepareRedirs"}B= $options{"verbose"}
ABresult
0X0
100
111
348 0 ▶ $hdl->do("CREATE INDEX IF NOT EXISTS redir ON beacons(altid,seqno);") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("CREATE INDEX IF NOT EXISTS redir ON beacons(altid,seqno);") or croak('Setup error: ' . $hdl->errstr) 0 0 0
349 0 ▶ $rihdl->execute($rikey, 1)
Branch: 1 branch
BranchTrueFalseTotal
$rihdl->execute($rikey, 1) or croak('Could not execute fix redirection index: ' . $rihdl->errstr) 0 0 0
350 or croak("Could not execute fix redirection index: ".$rihdl->errstr);
351 }
352 ▶ elsif ( not( $admref->{$rikey} and ($options{prepareRedirs} or (not exists $options{prepareRedirs})) ) ) {
Condition0%0/6
$admref->{$rikey} && ($options{'prepareRedirs'} || !exists $options{'prepareRedirs'})
!ll&&!rl&&r
000
$options{"prepareRedirs"} or not exists $options{"prepareRedirs"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$admref->{$rikey} && ($options{'prepareRedirs'} || !exists $options{'prepareRedirs'})
$admref->{$rikey} $options{"prepareRedirs"} not exists $options{"prepareRedirs"}
Truth table0%0/4
A && B or not(C)
A= $admref->{$rikey}B= $options{"prepareRedirs"}C= not exists $options{"prepareRedirs"}
ABCresult
0XX0
1000
11X1
1011
353 0 ▶ print "dropping redirection index\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
354 0 ▶ $hdl->do("DROP INDEX IF EXISTS redir;") or croak("Setup error: ".$hdl->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("DROP INDEX IF EXISTS redir;") or croak('Setup error: ' . $hdl->errstr) 0 0 0
355 0 ▶ $rihdl->execute($rikey, 0)
Branch: 1 branch
BranchTrueFalseTotal
$rihdl->execute($rikey, 0) or croak('Could not execute fix redirection index: ' . $rihdl->errstr) 0 0 0
356 or croak("Could not execute fix redirection index: ".$rihdl->errstr);
357 };
358 # $admref = $self->admhash();
359 }
360  
361 8 ▶ print "[ANALYZE ..." if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 8 8
362 8 $hdl->do("ANALYZE;");
363 8 ▶ print "]\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 8 8
364 8 return 1; # o.k.
365 };
366  
367  
368 =head3 deflate()
369  
370 Maintenance action: performs VACCUUM, REINDEX and ANALYZE on the database
371  
372 =cut
373  
374 sub deflate {
375 1 ▶ my ($self) = @_;
Subroutine deflate: called CC=5 SCAR=16.6
Pod documented
376 1 ▶ my $hdl = $self->{dbh} or croak("no handle?");
Branch: 1 branch
BranchTrueFalseTotal
my $hdl = $self->{"dbh"} or croak("no handle?") 0 1 1
377 1 local $hdl->{AutoCommit} = 1;
378 1 print "VACUUM\n";
379 1 ▶ $hdl->do("VACUUM") or carp("could not VACUUM: Skipping...");
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("VACUUM") or carp("could not VACUUM: Skipping...") 0 1 1
380 1 print "REINDEX\n";
381 1 ▶ $hdl->do("REINDEX") or croak("could not REINDEX: Abort");
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("REINDEX") or croak("could not REINDEX: Abort") 0 1 1
382 1 print "ANALYZE\n";
383 1 ▶ $hdl->do("ANALYZE;") or croak("could not ANALYZE: Abort");
Branch: 1 branch
BranchTrueFalseTotal
$hdl->do("ANALYZE;") or croak("could not ANALYZE: Abort") 0 1 1
384 1 return 1;
385 }
386  
387  
388 =head2 Handling of beacon files
389  
390 =head3 loadFile ( $file, $fields, %options )
391  
392 Reads a physical beacon file and stores it with a new Sequence number in the
393 database.
394  
395 Returns a triple:
396  
397 my ($seqno, $rec_ok, $message) = loadFile ( $file, $fields, %options )
398  
399 $seqno is undef on error
400  
401 $seqno and $rec_ok are zero with $message containing an explanation in case
402 of no action taken.
403  
404 $seqno is an positive integer if something was loaded: The L<Sequence Number>
405 (internal unique identifier) for the representation of the beacon file in
406 the database.
407  
408 =over 8
409  
410 =item $file
411  
412 File to read: Must be a beacon file
413  
414 =item $fields
415  
416 Hashref with additional meta and admin fields to store
417  
418 =item Supported options:
419  
420 verbose => (0|1)
421 force => (0|1) process unconditionally without timestamp comparison
422 nostat => (0|1) don't refresh global identifier counters
423  
424 =back
425  
426 If the file does not contain a minimal correct header (eg. is an empty file
427 or an HTML error page accidentaly caught) no action is performed.
428  
429 Otherwise, a fresh SeqNo (sequence number) is generated and meta and
430 BEACON-Lines are stored in the appropriate tables in the database.
431  
432 If the _alias field is provided, existing database entries for this
433 Alias are updated, identifiers not accounted for any more are
434 eventually discarded.
435  
436 =cut
437  
438 sub loadFile {
439 14 ▶ my ($self, $file, $fields, %options) = @_;
Subroutine loadFile: called CC=130 SCAR=67.9
Pod documented
440 14 ▶ $options{'verbose'} = $self->{'verbose'} unless exists $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"verbose"} 1 13 14
441 14 ▶ $options{'verbose'} ||= 0;
Condition100%2/2
$options{"verbose"} ||= 0
l!l
95
MC/DC100%1/1
$options{"verbose"} ||= 0
$options{"verbose"}
Truth table100%2/2
A
A= $options{"verbose"}
Aresult
11
00
442  
443 14 ▶ if ( ! $file ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $file) { } 0 14 14
elsif (not -e $file) { } 0 14 14
elsif (not -r _) { } 0 14 14
elsif (-z _) { } 0 14 14
444 0 croak("Missing file argument")}
445 elsif ( ! -e $file ) {
446 0 ▶ print "ERROR: no such file $file\n" && return undef}
Condition0%0/2
"ERROR: no such file $file\n" && (return undef)
!ll
00
MC/DC0%0/1
"ERROR: no such file $file\n" && (return undef)
"ERROR: no such file $file\n"
Truth table0%0/2
A
A= "ERROR: no such file $file\n"
Aresult
00
11
447 elsif ( ! -r _ ) {
448 0 ▶ print "ERROR: no read permissions for $file\n" && return undef}
Condition0%0/2
"ERROR: no read permissions for $file\n" && (return undef)
!ll
00
MC/DC0%0/1
"ERROR: no read permissions for $file\n" && (return undef)
"ERROR: no read permissions for $file\n"
Truth table0%0/2
A
A= "ERROR: no read permissions for $file\n"
Aresult
00
11
449 elsif ( -z _ ) {
450 0 print "WARNING: empty file $file\n";
451 0 return (0,0, "empty file: Will not process");
452 }
453 14 my $mtime = (stat(_))[9];
454 14 ▶ open(BKN, "<:utf8", $file) or (print "ERROR: cannot read $file\n", return undef);
Branch: 1 branch
BranchTrueFalseTotal
open BKN, "<:utf8", $file or print "ERROR: cannot read $file\n", (return undef) 0 14 14
455 14 local($.) = 0;
456  
457 14 ▶ unless ( defined $self->{identifierClass} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"identifierClass"}) 14 0 14
458 14 my $package = $self->autoIdentifier();
459 14 ▶ $options{'verbose'} && ref($package) && print "Assuming identifiers of type ".ref($package)."\n";
Branch: 1 branch
BranchTrueFalseTotal
$options{"verbose"} and ref $package and print 'Assuming identifiers of type ' . ref($package) . "\n" 0 14 14
Condition66%2/3
$options{"verbose"} and ref $package
!ll&&!rl&&r
590
MC/DC0%0/2
$options{"verbose"} and ref $package
$options{"verbose"} ref $package
Truth table66%2/3
A and B
A= $options{"verbose"}B= ref $package
ABresult
0X0
100
111
460 };
461  
462 14 ▶ $fields = {} unless $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 0 14 14
463 14 ▶ $fields->{'_ftime'} ||= time();
Condition33%1/3
$fields->{"_ftime"} ||= time
l!l&&r!l&&!r
0140
MC/DC0%0/2
$fields->{"_ftime"} ||= time
$fields->{"_ftime"} time
Truth table33%1/3
A ||= B
A= $fields->{"_ftime"}B= time
ABresult
1X1
011
000
464 14 ▶ $fields->{'_mtime'} ||= $mtime;
Condition66%2/3
$fields->{"_mtime"} ||= $mtime
l!l&&r!l&&!r
1220
MC/DC0%0/2
$fields->{"_mtime"} ||= $mtime
$fields->{"_mtime"} $mtime
Truth table66%2/3
A ||= B
A= $fields->{"_mtime"}B= $mtime
ABresult
1X1
011
000
465 14 ▶ delete $fields->{_uri} unless $fields->{_uri};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{"_uri"} 1 13 14
466 14 ▶ delete $fields->{_alias} unless $fields->{_alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{"_alias"} 0 14 14
467 14 ▶ my $autopurge = $fields->{_alias} || "";
Condition50%1/2
$fields->{'_alias'} || ''
l!l
140
MC/DC0%0/1
$fields->{'_alias'} || ''
$fields->{'_alias'}
Truth table50%1/2
A
A= $fields->{'_alias'}
Aresult
11
00
468 14 ▶ my $showme = $fields->{_alias} || $fields->{_uri} || $file;
Condition33%2/6
$fields->{'_alias'} || $fields->{'_uri'} || $file
l!l&&r!l&&!r
1400
$fields->{"_alias"} or $fields->{"_uri"}
l!l&&r!l&&!r
1400
MC/DC0%0/3
$fields->{'_alias'} || $fields->{'_uri'} || $file
$fields->{"_alias"} $fields->{"_uri"} $file
Truth table25%1/4
A or B || C
A= $fields->{"_alias"}B= $fields->{"_uri"}C= $file
ABCresult
1XX1
01X1
0011
0000
469  
470 14 ▶ if ( $options{'verbose'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"verbose"}) 9 5 14
471 9 ▶ printf("* Loading %s from URI %s\n", $fields->{_alias} || "<no alias>", $fields->{_uri} || "<direct file>");
Condition50%2/4
$fields->{'_alias'} || '<no alias>'
l!l
90
$fields->{'_uri'} || '<direct file>'
l!l
90
MC/DC0%0/1
$fields->{'_alias'} || '<no alias>'
$fields->{'_alias'}
MC/DC0%0/1
$fields->{'_uri'} || '<direct file>'
$fields->{'_uri'}
Truth table50%1/2
A
A= $fields->{'_alias'}
Aresult
11
00
Truth table50%1/2
A
A= $fields->{'_uri'}
Aresult
11
00
472 9 printf("* local input %s (%s)\n", $file, SeeAlso::Source::BeaconAggregator::tToISO($mtime));
473 };
474  
475 14 my ($collno, $inserthandle, $replacehandle, $err, $format);
476 14 my ($linecount, $headerseen, $oseq) = (0, 0, 0);
477 14 my ($reccount, $recill, $recign, $recnil, $recupd, $recnew, $recdupl, $recdel) = (0, 0, 0, 0, 0, 0, 0, 0);
478 14 local($_);
479 lines:
480 14 ▶ while ( <BKN> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline BKN)) 174 14 188
481 188 s/[ \x0d\x0a]+$//;
482 188 ▶ unless ( $linecount++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($linecount++) 14 174 188
483 14 ▶ if ( s/^\x{FEFF}// ) { # BOM-Character
Branch: 2 branches
BranchTrueFalseTotal
if (s/^\x{FEFF}//) { } 0 14 14
elsif (s/^\xef\xbb\xbf//) { } 0 14 14
484 }
485 elsif ( s/^\xef\xbb\xbf// ) { # BOM-Bytes
486 0 print "ERROR: cannot cope with doubly UTF-8 encoded $file\n";
487 0 return (undef, undef, "encoding trouble")};
488 14 ▶ if ( /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/) 0 14 14
489 0 print "WARNING: Discarding blank line before beacon header [$showme l.$.]\n";
490 0 next;
491 };
492 };
493 188 ▶ if ( not defined $collno ) { # $collno used as flag: "still in header"
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $collno) { } 103 85 188
494 103 ▶ if ( /^#\s*([A-Z][\w-]*):\s*(.*)$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^#\s*([A-Z][\w-]*):\s*(.*)$/) { } 87 16 103
elsif (/^(#[^:\s]+)/) { } 0 16 16
elsif (/^\s*$/) { } 2 14 16
elsif (not $headerseen) { } 0 14 14
495 87 $headerseen++;
496 87 my ($field, $data) = ($1, $2);
497 87 ▶ $field =~ s/^DATE$/TIMESTAMP/ && print "WARNING: corrected DATE to TIMESTAMP in Beacon-Header [$showme l.$.]\n";
Branch: 1 branch
BranchTrueFalseTotal
$field =~ s/^DATE$/TIMESTAMP/ and print "WARNING: corrected DATE to TIMESTAMP in Beacon-Header [$showme l.$.]\n" 0 87 87
498 87 $data =~ s/\s+$//;
499 87 ▶ next if $data =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /^\s*$/ 0 87 87
500 87 ▶ if ( SeeAlso::Source::BeaconAggregator->beaconfields($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('SeeAlso::Source::BeaconAggregator'->beaconfields($field)) { } 81 6 87
501 81 ▶ if ( $fields->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$field}) { } 0 81 81
502 0 print "WARNING: Skipping already set $field [$showme l.$.]\n"}
503 else {
504 81 $fields->{$field} = $data}
505 }
506 else {
507 6 print "WARNING: Ignoring unknown $field [$data] [$showme l.$.]\n";
508 };
509 }
510 elsif ( /^(#[^:\s]+)/ ) {
511 0 print "WARNING: Discarding unparseable line >$1...< in beacon header context [$showme l.$.]\n"}
512 elsif ( /^\s*$/ ) {
513 2 ▶ print "NOTICE: Discarding blank line in beacon header context [$showme l.$.]\n" if $options{'verbose'}}
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 2 0 2
514 elsif ( ! $headerseen ) {
515 0 print "ERROR: no header fields [$showme l.$.]\n";
516 0 return (0, 0, "no header fields: Will not proceed");
517 }
518 else {
519 14 ($collno, $err, $format, $inserthandle, $replacehandle, $oseq) = $self->processbeaconheader($fields, %options);
520 14 ▶ unless ( $collno ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($collno) 0 14 14
521 0 print "ERROR: metadata error [$showme l.$.]\n";
522 0 return (0, 0, "metadata error: $err");
523 };
524 14 $self->{dbh}->{AutoCommit} = 0;
525 14 $linecount --;
526 14 redo lines;
527 }
528 }
529 else {
530 85 s/^\s+//; s/\s+$//;
531 85 my ($id, $altid, @rest);
532 85 ($id, @rest) = split(/\s*\|\s*/, $_, 4);
533 85 ▶ ($id, $altid) = split(/\s*=\s*/, $id, 2) if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 85 0 85
534 85 ▶ $id || ($recnil++, next);
Branch: 1 branch
BranchTrueFalseTotal
$id or ++$recnil, next 0 85 85
535  
536 85 ▶ if ( $options{'filter'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"filter"}) 28 57 85
537 28 ($id, $altid) = &{$options{'filter'}}($id, $altid, @rest);
538 28 ▶ unless ( $id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 14 14 28
539 14 $recign ++;
540 14 ▶ unless ( ++$reccount % 10000 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (++$reccount % 10000) 0 14 14
541 0 $self->{dbh}->{AutoCommit} = 1;
542 0 ▶ print "$reccount\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
543 0 $self->{dbh}->{AutoCommit} = 0;
544 };
545 14 next lines;
546 };
547 };
548 71 ▶ $altid ||= "";
Condition100%2/2
$altid ||= ""
l!l
2744
MC/DC100%1/1
$altid ||= ""
$altid
Truth table100%2/2
A
A= $altid
Aresult
11
00
549  
550 71 my($hits, $info, $link);
551 71 ▶ if ( @rest && ($rest[$#rest] =~ m!^\S+://\S+$!) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest and $rest[$#rest] =~ m[^\S+://\S+$]) { } 27 44 71
elsif (defined $rest[2]) { } 0 44 44
Condition100%3/3
@rest and $rest[$#rest] =~ m[^\S+://\S+$]
!ll&&!rl&&r
113327
MC/DC100%2/2
@rest and $rest[$#rest] =~ m[^\S+://\S+$]
@rest $rest[$#rest] =~ m[^\S+://\S+$]
Truth table100%3/3
A and B
A= @restB= $rest[$#rest] =~ m[^\S+://\S+$]
ABresult
0X0
100
111
552 27 $link = pop @rest}
553 elsif ( defined $rest[2] ) {
554 0 print "WARNING: unparseable link content >$rest[2]< [$showme l.$.]"};
555  
556 71 ▶ if ( @rest && ($rest[0] =~ /^\d*$/) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@rest and $rest[0] =~ /^\d*$/) { } 13 58 71
elsif (defined $rest[1]) { } 2 56 58
elsif (defined $rest[0]) { } 45 11 56
Condition100%3/3
@rest and $rest[0] =~ /^\d*$/
!ll&&!rl&&r
114713
MC/DC100%2/2
@rest and $rest[0] =~ /^\d*$/
@rest $rest[0] =~ /^\d*$/
Truth table100%3/3
A and B
A= @restB= $rest[0] =~ /^\d*$/
ABresult
0X0
100
111
557 13 $hits = shift @rest;
558 # really disregard hits with explicit 0?
559 13 ▶ if ( (!$altid) and (!$link) and ($format =~ /\baltTARGET\b/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $altid || $link and $format =~ /\baltTARGET\b/) { } 2 11 13
Condition83%5/6
not $altid || $link and $format =~ /\baltTARGET\b/
!ll&&!rl&&r
922
$altid || $link
l!l&&r!l&&!r
094
MC/DC66%2/3
not $altid || $link and $format =~ /\baltTARGET\b/
$altid $link $format =~ /\baltTARGET\b/
Truth table75%3/4
not(A || B) and C
A= $altidB= $linkC= $format =~ /\baltTARGET\b/
ABCresult
1XX0
01X0
0000
0011
560 2 ▶ $altid = shift @rest || "";
Condition50%1/2
shift @rest || ''
l!l
20
MC/DC0%0/1
shift @rest || ''
shift @rest
Truth table50%1/2
A
A= shift @rest
Aresult
11
00
561 }
562 else {
563 11 ▶ $info = shift @rest || "";
Condition100%2/2
shift @rest || ''
l!l
92
MC/DC100%1/1
shift @rest || ''
shift @rest
Truth table100%2/2
A
A= shift @rest
Aresult
11
00
564 };
565 }
566 elsif ( defined $rest[1] ) {
567 2 $hits = "";
568 2 ▶ if ( (!$altid) and (!$link) and ($format =~ /\baltTARGET\b/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $altid || $link and $format =~ /\baltTARGET\b/) { } 2 0 2
Condition33%2/6
not $altid || $link and $format =~ /\baltTARGET\b/
!ll&&!rl&&r
002
$altid || $link
l!l&&r!l&&!r
002
MC/DC0%0/3
not $altid || $link and $format =~ /\baltTARGET\b/
$altid $link $format =~ /\baltTARGET\b/
Truth table25%1/4
not(A || B) and C
A= $altidB= $linkC= $format =~ /\baltTARGET\b/
ABCresult
1XX0
01X0
0000
0011
569 2 $info = shift @rest;
570 2 $altid = shift @rest;
571 }
572 else {
573 0 shift @rest;
574 0 $info = shift @rest;
575 };
576 }
577 elsif ( defined $rest[0] ) {
578 45 $hits = "";
579 45 $info = shift @rest;
580 };
581 71 ▶ if ( @rest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 0 71 71
582 0 print "WARNING: unparseable content >$_< [$showme l.$.]"};
583  
584 71 ▶ unless ( $link ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link) 44 27 71
585 44 ▶ if ( ($format =~ /\bhasTARGET\b/) ) { # ok
Branch: 3 branches
BranchTrueFalseTotal
if ($format =~ /\bhasTARGET\b/) { } 40 4 44
elsif ($altid and $format =~ /\baltTARGET\b/) { } 4 0 4
elsif ($format =~ /\bnoTARGET\b/) { } 0 0 0
586 }
587 ▶ elsif ( $altid && ($format =~ /\baltTARGET\b/) ) { # also ok
Condition33%1/3
$altid and $format =~ /\baltTARGET\b/
!ll&&!rl&&r
004
MC/DC0%0/2
$altid and $format =~ /\baltTARGET\b/
$altid $format =~ /\baltTARGET\b/
Truth table33%1/3
A and B
A= $altidB= $format =~ /\baltTARGET\b/
ABresult
0X0
100
111
588 }
589 elsif ( $format =~ /\bnoTARGET\b/ ) {
590 0 ▶ print "NOTICE: discarding >$id<".(defined $hits ? " ($hits)" : "")." without link [$showme l.$.]\n" if $options{'verbose'} > 1;
Branch: 2 branches
BranchTrueFalseTotal
if $options{"verbose"} > 1 0 0 0
defined $hits ? : 0 0 0
591 0 $recill++;
592 0 next lines;
593 }
594 else {
595 0 ▶ print "WARNING: discarding >$id<".(defined $hits ? " ($hits)" : "")." without link [$showme l.$.] (assertion failed)\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $hits ? : 0 0 0
596 0 $recill++;
597 0 next lines;
598 }
599 };
600  
601 71 ▶ if ( $format !~ /\baltTARGET\b/ ) { # Allow certain duplicates (force disambiguisation)
Branch: 1 branch
BranchTrueFalseTotal
unless ($format =~ /\baltTARGET\b/) 4 67 71
602 4 ▶ $altid ||= $info || $link}
Condition33%2/6
$altid ||= $info || $link
l!l&&r!l&&!r
004
$info || $link
l!l&&r!l&&!r
004
MC/DC0%0/3
$altid ||= $info || $link
$altid $info $link
Truth table0%0/4
A ||= B || C
A= $altidB= $infoC= $link
ABCresult
1XX1
01X1
0011
0000
603  
604 71 ▶ $hits = "" unless defined $hits;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hits 11 60 71
605 71 ▶ ($hits =~ /^0+/) && ($recnil++, next); # Explizit "0" => raus
Branch: 1 branch
BranchTrueFalseTotal
$hits =~ /^0+/ and ++$recnil, next 0 71 71
606 71 ▶ $hits = 0 if $hits eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $hits eq "" 60 11 71
607 71 ▶ $altid ||= "";
Condition100%2/2
$altid ||= ""
l!l
3140
MC/DC100%1/1
$altid ||= ""
$altid
Truth table100%2/2
A
A= $altid
Aresult
11
00
608 71 my $hash;
609 71 ▶ if ( defined $self->{identifierClass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'identifierClass'}) { } 0 71 71
610 0 $self->{identifierClass}->value($id);
611 0 ▶ unless ( $self->{identifierClass}->valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"identifierClass"}->valid) 0 0 0
612 0 ▶ print "NOTICE: invalid identifier >$id< ($hits) [$showme l.$.]\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
613 0 $recill++;
614 0 next lines;
615 };
616 0 $hash = $self->{identifierClass}->hash();
617 }
618 else {
619 71 $hash = $id};
620 71 my $did;
621 71 ▶ if ( $replacehandle && ($did = $replacehandle->execute($hits, $info, $link, $hash, $altid)) ) { # UPDATE OR FAIL old record
Branch: 3 branches
BranchTrueFalseTotal
if ($replacehandle and $did = $replacehandle->execute($hits, $info, $link, $hash, $altid)) { } 40 31 71
elsif ($did = $inserthandle->execute($hash, $altid, $hits, $info, $link)) { } 31 0 31
elsif ($inserthandle->errstr =~ /constraint/) { } 0 0 0
Condition66%2/3
$replacehandle and $did = $replacehandle->execute($hits, $info, $link, $hash, $altid)
!ll&&!rl&&r
31040
MC/DC50%1/2
$replacehandle and $did = $replacehandle->execute($hits, $info, $link, $hash, $altid)
$replacehandle $did = $replacehandle->execute($hits, $info, $link, $hash, $altid)
Truth table66%2/3
A and B
A= $replacehandleB= $did = $replacehandle->execute($hits, $info, $link, $hash, $altid)
ABresult
0X0
100
111
622 40 ▶ if ( $replacehandle->err ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($replacehandle->err) { } 0 40 40
elsif ($did eq '0E0') { } 28 12 40
623 0 carp("update in trouble: $replacehandle->errstring [$showme l.$.]");
624 0 $recdupl++;
625 }
626 elsif ( $did eq "0E0" ) { # not found, try insert
627 28 $did = $inserthandle->execute($hash, $altid, $hits, $info, $link);
628 28 ▶ if ( $did eq "0E0" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did eq '0E0') { } 10 18 28
629 10 $recdupl++;
630 10 ▶ if ( $altid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altid) { } 0 10 10
631 0 ▶ print "INFO: did not insert duplicate Id >$id< = >$altid< ($hits) [$showme l.$.]\n" if $options{'verbose'}}
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
632 else {
633 10 ▶ print "INFO: did not insert duplicate Id >$id< ($hits) [$showme l.$.]\n" if $options{'verbose'} > 1};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} > 1 0 10 10
634 }
635 else {
636 18 $recnew++};
637 }
638 else {
639 12 $recupd++};
640 }
641 elsif ( $did = $inserthandle->execute($hash, $altid, $hits, $info, $link) ) { # INSERT OR IGNORE new record
642 31 ▶ if ( $did eq "0E0" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did eq '0E0') { } 9 22 31
643 9 $recdupl++;
644 9 ▶ print "INFO: did not insert duplicate Id $id ($hits) [$showme l.$.]\n" if $options{'verbose'} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} > 1 0 9 9
645 }
646 else {
647 22 $recnew++};
648 }
649 elsif ( $inserthandle->errstr =~ /constraint/ ) {
650 0 $recdupl++;
651 0 ▶ print "INFO: duplicate Id $id ($hits): not inserting [$showme l.$.]\n" if $options{'verbose'} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} > 1 0 0 0
652 }
653 else {
654 0 croak("Could not insert: ($id, $hits, $info, $link): ".$inserthandle->errstr)};
655  
656 71 ▶ unless ( ++$reccount % 10000 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (++$reccount % 10000) 0 71 71
657 0 $self->{dbh}->{AutoCommit} = 1;
658 0 ▶ print "$reccount\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
659 0 $self->{dbh}->{AutoCommit} = 0;
660 };
661 }
662 };
663 14 ▶ if ( not defined $collno ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $collno) 0 14 14
664 0 ▶ if ( $headerseen ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($headerseen) { } 0 0 0
elsif ($.) { } 0 0 0
665 0 ($collno, $err, $format, $inserthandle, $replacehandle, $oseq) = $self->processbeaconheader($fields, %options);
666 0 ▶ if ( $collno ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collno) { } 0 0 0
667 0 print "WARNING: no idn content in file [$showme l.$.]\n"}
668 else {
669 0 print "ERROR: metadata error [$showme l.$.]\n";
670 0 return (0,0, "metadata error: $err");
671 };
672 }
673 elsif ( $. ) {
674 0 print "ERROR: no header fields [$showme l.$.]\n";
675 0 return (0, 0, "no header fields: Will not proceed");
676 }
677 else {
678 0 print "WARNING: empty file [$showme]\n";
679 0 return (0,0, "empty file");
680 };
681 }
682 14 $self->{dbh}->{AutoCommit} = 1;
683  
684 14 ▶ if ( $autopurge ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autopurge) 14 0 14
685 14 $self->{dbh}->{AutoCommit} = 0;
686 14 ▶ if ( $oseq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oseq) 8 6 14
687 8 my ($bcdelh, $bcdelexpl) = $self->stmtHdl("DELETE FROM beacons WHERE seqno==?");
688 8 ▶ $self->stmtExplain($bcdelexpl, $oseq) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 8 8
689 8 ▶ my $rows = $bcdelh->execute($oseq) or croak("Could not execute >".$bcdelh->{Statement}."<: ".$bcdelh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $bcdelh->execute($oseq) or croak('Could not execute >' . $bcdelh->{"Statement"} . '<: ' . $bcdelh->errstr) 0 8 8
690 8 $self->{dbh}->{AutoCommit} = 1;
691 8 ▶ printf("INFO: Purged %s surplus identifiers from old sequence %u\n", $rows, $oseq) if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 4 4 8
692 8 ▶ $rows = "0" if $rows eq "0E0";
Branch: 1 branch
BranchTrueFalseTotal
if $rows eq "0E0" 5 3 8
693 8 $recdel += $rows;
694 };
695  
696 14 $self->{dbh}->{AutoCommit} = 0;
697 14 my ($rpdelh, $rpdelexpl) = $self->stmtHdl("DELETE FROM repos WHERE (alias=?) AND (seqno<?);");
698 14 ▶ $self->stmtExplain($rpdelexpl, $autopurge, $collno) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 14 14
699 14 ▶ my $rows = $rpdelh->execute($autopurge, $collno) or croak("Could not execute >".$rpdelh->{Statement}."<: ".$rpdelh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $rpdelh->execute($autopurge, $collno) or croak('Could not execute >' . $rpdelh->{"Statement"} . '<: ' . $rpdelh->errstr) 0 14 14
700 14 $self->{dbh}->{AutoCommit} = 1;
701 14 ▶ $rows = "0" if $rows eq "0E0";
Branch: 1 branch
BranchTrueFalseTotal
if $rows eq "0E0" 6 8 14
702 14 ▶ printf("INFO: %u old sequences discarded\n", $rows) if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 9 5 14
703 }
704  
705 14 ▶ printf "NOTICE: New sequence %u for %s: processed %u Records from %u lines\n",
Condition50%1/2
$autopurge || '???'
l!l
140
MC/DC0%0/1
$autopurge || '???'
$autopurge
Truth table50%1/2
A
A= $autopurge
Aresult
11
00
706 $collno, $autopurge || "???", $reccount, $linecount;
707 14 my $statline = sprintf "%u replaced, %u new, %u deleted, %u duplicate, %u nil, %u invalid, %u ignored",
708 $recupd, $recnew, $recdel, $recdupl, $recnil, $recill, $recign;
709 14 print " ($statline)\n";
710  
711 14 my $recok = $recupd + $recnew;
712 14 ▶ my $numchg = ($recnew or $recdel) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$recnew || $recdel ? : 12 2 14
Condition100%3/3
$recnew or $recdel
l!l&&r!l&&!r
1112
MC/DC100%2/2
$recnew or $recdel
$recnew $recdel
Truth table100%3/3
A or B
A= $recnewB= $recdel
ABresult
1X1
011
000
713  
714 # my $ct1hdl = $self->stmtHdl("SELECT COUNT(*) FROM beacons WHERE seqno==? LIMIT 1;");
715 # $ct1hdl->execute($collno) or croak("could not execute live count: ".$ct1hdl->errstr);
716 # my $ct1ref = $ct1hdl->fetchrow_arrayref();
717 # my $counti = $ct1ref->[0] || 0;
718  
719 # my $ct2hdl = $self->stmtHdl("SELECT COUNT(DISTINCT hash) FROM beacons WHERE seqno==?");
720 # using subquery to trick SQLite into using indices
721 # my $ct2hdl = $self->stmtHdl("SELECT COUNT(*) FROM (SELECT DISTINCT hash FROM beacons WHERE seqno==?) LIMIT 1;");
722 # $ct2hdl->execute($collno) or croak("could not execute live count: ".$ct2hdl->errstr);
723 # my $ct2ref = $ct2hdl->fetchrow_arrayref();
724 # my $countu = $ct2ref->[0] || 0;
725  
726 # combined query turned out as not as efficient
727 # my $ct0hdl = $self->stmtHdl("SELECT COUNT(*), COUNT(DISTINCT hash) FROM beacons WHERE seqno==? LIMIT 1;");
728 # $ct0hdl->execute($collno) or croak("could not execute live count: ".$ct0hdl->errstr);
729 # my $ct0ref = $ct0hdl->fetchrow_arrayref();
730 # my ($counti, $countu) = ($ct0ref->[0] || 0, $ct0ref->[1] || 0);
731  
732 14 my ($updh, $updexpl) = $self->stmtHdl(<<"XxX");
733 UPDATE OR FAIL repos SET counti=?,countu=?,fstat=?,utime=?,ustat=?,sort=? WHERE seqno==?;
734 XxX
735  
736 14 ▶ my $counti = $self->idStat($collno, 'distinct' => 0) || 0;
Condition50%1/2
$self->idStat($collno, 'distinct', 0) || 0
l!l
140
MC/DC0%0/1
$self->idStat($collno, 'distinct', 0) || 0
$self->idStat($collno, 'distinct', 0)
Truth table50%1/2
A
A= $self->idStat($collno, 'distinct', 0)
Aresult
11
00
737 14 ▶ printf("WARNING: expected %u valid records, counted %u\n", $recok, $counti) if $recok != $counti;
Branch: 1 branch
BranchTrueFalseTotal
if $recok != $counti 0 14 14
738 14 ▶ unless ( $numchg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($numchg) 2 12 14
739 2 ▶ $fields->{'_counti'} ||= 0;
Condition50%1/2
$fields->{"_counti"} ||= 0
l!l
20
MC/DC0%0/1
$fields->{"_counti"} ||= 0
$fields->{"_counti"}
Truth table50%1/2
A
A= $fields->{"_counti"}
Aresult
11
00
740 2 ▶ printf("WARNING: expected unchanged number %u valid records, counted %u\n", $fields->{'_counti'}, $counti) if $fields->{'_counti'} != $counti;
Branch: 1 branch
BranchTrueFalseTotal
if $fields->{"_counti"} != $counti 0 2 2
741 };
742  
743 14 ▶ my $sort = $fields->{'_sort'} || "";
Condition50%1/2
$fields->{'_sort'} || ''
l!l
014
MC/DC0%0/1
$fields->{'_sort'} || ''
$fields->{'_sort'}
Truth table50%1/2
A
A= $fields->{'_sort'}
Aresult
11
00
744 my $countu = $numchg ? ( $self->idStat($collno, 'distinct' => 1) || 0 )
745 14 ▶ : ( $fields->{'_countu'} || $self->idStat($collno, 'distinct' => 1) || 0 );
Branch: 1 branch
BranchTrueFalseTotal
$numchg ? : 12 2 14
Condition42%3/7
$self->idStat($collno, "distinct", 1) or 0
l!l
120
$fields->{"_countu"} or $self->idStat($collno, "distinct", 1) or 0
l!l
20
$fields->{"_countu"} or $self->idStat($collno, "distinct", 1)
l!l&&r!l&&!r
200
MC/DC0%0/1
$self->idStat($collno, "distinct", 1) or 0
$self->idStat($collno, "distinct", 1)
MC/DC0%0/2
$fields->{"_countu"} or $self->idStat($collno, "distinct", 1) or 0
$fields->{"_countu"} $self->idStat($collno, "distinct", 1)
Truth table50%1/2
A
A= $self->idStat($collno, "distinct", 1)
Aresult
11
00
Truth table33%1/3
A or B
A= $fields->{"_countu"}B= $self->idStat($collno, "distinct", 1)
ABresult
1X1
011
000
746 14 ▶ $self->stmtExplain($updexpl, $counti, $countu, $statline, time(), "successfully loaded", $sort, $collno) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 14 14
747 $updh->execute($counti, $countu, $statline, time(), "successfully loaded", $sort, $collno)
748 14 ▶ or croak("Could not execute >".$updh->{Statement}."<: ".$updh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$updh->execute($counti, $countu, $statline, time, "successfully loaded", $sort, $collno) or croak('Could not execute >' . $updh->{"Statement"} . '<: ' . $updh->errstr) 0 14 14
749 14 close(BKN);
750  
751 14 ▶ if ( $numchg or $options{'force'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numchg or $options{"force"}) 14 0 14
Condition66%2/3
$numchg or $options{"force"}
l!l&&r!l&&!r
1220
MC/DC0%0/2
$numchg or $options{"force"}
$numchg $options{"force"}
Truth table66%2/3
A or B
A= $numchgB= $options{"force"}
ABresult
1X1
011
000
752 # if ( $options{'force'} ) {
753 # print "[ANALYZE ..." if $options{'verbose'};
754 # $self->{dbh}->do("ANALYZE;");
755 # print "]\n" if $options{'verbose'};
756 # };
757  
758 14 ▶ if ( $options{'nostat'} ) { # invalidate since they might have changed
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'nostat'}) { } 0 14 14
759 0 $self->admin('gcounti', undef);
760 0 $self->admin('gcountu', undef);
761 }
762 else {
763 14 ▶ $self->admin('gcounti', $self->idStat(undef, 'distinct' => 0) || 0);
Condition50%1/2
$self->idStat(undef, 'distinct', 0) || 0
l!l
140
MC/DC0%0/1
$self->idStat(undef, 'distinct', 0) || 0
$self->idStat(undef, 'distinct', 0)
Truth table50%1/2
A
A= $self->idStat(undef, 'distinct', 0)
Aresult
11
00
764 14 ▶ $self->admin('gcountu', $self->idStat(undef, 'distinct' => 1) || 0);
Condition50%1/2
$self->idStat(undef, 'distinct', 1) || 0
l!l
140
MC/DC0%0/1
$self->idStat(undef, 'distinct', 1) || 0
$self->idStat(undef, 'distinct', 1)
Truth table50%1/2
A
A= $self->idStat(undef, 'distinct', 1)
Aresult
11
00
765 }
766 };
767  
768 14 return ($collno, $recok, undef);
769 }
770  
771  
772 =head4 processbeaconheader($self, $fieldref, [ %options] )
773  
774 Internal subroutine used by C<loadFile()>.
775  
776 =over 8
777  
778 =item $fieldref
779  
780 Hash with raw fields.
781  
782 =item Supported options:
783  
784 verbose => (0|1)
785  
786 Show seqnos of old instances which are met by the alias
787  
788 =back
789  
790  
791 =cut
792  
793 sub processbeaconheader {
794 14 ▶ my ($self, $fieldref, %options) = @_;
Subroutine processbeaconheader: called CC=76 SCAR=60.6
Pod documented
795 14 my $osq = 0;
796 14 my @carp;
797  
798 14 ▶ if ( my $alias = $fieldref->{_alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = $fieldref->{"_alias"}) 14 0 14
799 14 my $stampfield = SeeAlso::Source::BeaconAggregator->beaconfields("TIMESTAMP");
800 14 my ($listh, $listexpl) = $self->stmtHdl("SELECT seqno, $stampfield, mtime, counti, countu FROM repos WHERE alias=?;");
801 14 ▶ $self->stmtExplain($listexpl, $alias) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 14 14
802 14 ▶ $listh->execute($alias) or croak("Could not execute >".$listh->{Statement}."<: ".$listh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$listh->execute($alias) or croak('Could not execute >' . $listh->{"Statement"} . '<: ' . $listh->errstr) 0 14 14
803 14 my ($rowcnt, $ocounti, $ocountu);
804 14 ▶ while ( my($row) = $listh->fetchrow_arrayref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($row) = $listh->fetchrow_arrayref) 22 0 22
805 22 ▶ last unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 14 8 22
806 8 $rowcnt ++;
807 8 ($ocounti, $ocountu) = ($row->[3], $row->[4]);
808 8 ▶ if ( $options{'verbose'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'verbose'}) { } 4 4 8
809 4 ▶ print "* Old Instances for $alias:\n" unless $osq;
Branch: 1 branch
BranchTrueFalseTotal
unless $osq 4 0 4
810 4 $osq = $row->[0];
811 4 ▶ print "+\t#$osq ", SeeAlso::Source::BeaconAggregator::tToISO($row->[1] || $row->[2]), " (", $row->[3] || "???", ")\n";
Condition60%3/5
$row->[1] || $row->[2]
l!l&&r!l&&!r
130
$row->[3] || '???'
l!l
40
MC/DC0%0/2
$row->[1] || $row->[2]
$row->[1] $row->[2]
MC/DC0%0/1
$row->[3] || '???'
$row->[3]
Truth table66%2/3
A || B
A= $row->[1]B= $row->[2]
ABresult
1X1
011
000
Truth table50%1/2
A
A= $row->[3]
Aresult
11
00
812 }
813 else {
814 4 $osq = $row->[0]};
815 }
816 14 ▶ if ( $rowcnt && ($rowcnt == 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowcnt and $rowcnt == 1) 8 6 14
Condition66%2/3
$rowcnt and $rowcnt == 1
!ll&&!rl&&r
608
MC/DC50%1/2
$rowcnt and $rowcnt == 1
$rowcnt $rowcnt == 1
Truth table66%2/3
A and B
A= $rowcntB= $rowcnt == 1
ABresult
0X0
100
111
817 8 ▶ $fieldref->{_counti} ||= $ocounti if $ocounti;
Branch: 1 branch
BranchTrueFalseTotal
if $ocounti 6 2 8
Condition33%1/3
$fieldref->{"_counti"} ||= $ocounti
l!l&&r!l&&!r
060
MC/DC0%0/2
$fieldref->{"_counti"} ||= $ocounti
$fieldref->{"_counti"} $ocounti
Truth table33%1/3
A ||= B
A= $fieldref->{"_counti"}B= $ocounti
ABresult
1X1
011
000
818 8 ▶ $fieldref->{_countu} ||= $ocountu if $ocountu;
Branch: 1 branch
BranchTrueFalseTotal
if $ocountu 6 2 8
Condition33%1/3
$fieldref->{"_countu"} ||= $ocountu
l!l&&r!l&&!r
060
MC/DC0%0/2
$fieldref->{"_countu"} ||= $ocountu
$fieldref->{"_countu"} $ocountu
Truth table33%1/3
A ||= B
A= $fieldref->{"_countu"}B= $ocountu
ABresult
1X1
011
000
819 }
820 };
821  
822 14 my $format = "";
823 14 ▶ if ( $fieldref->{'FORMAT'} && $self->{accept}->{'FORMAT'} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($fieldref->{'FORMAT'} and $self->{'accept'}{'FORMAT'}) { } 4 10 14
elsif ($fieldref->{'FORMAT'}) { } 8 2 10
elsif ($fieldref->{'VERSION'} or $fieldref->{'TARGET'} or $fieldref->{'PREFIX'} or $fieldref->{'MESSAGE'}) { } 2 0 2
elsif ($self->{'accept'}{'FORMAT'}) { } 0 0 0
Condition100%3/3
$fieldref->{"FORMAT"} and $self->{"accept"}{"FORMAT"}
!ll&&!rl&&r
284
MC/DC100%2/2
$fieldref->{"FORMAT"} and $self->{"accept"}{"FORMAT"}
$fieldref->{"FORMAT"} $self->{"accept"}{"FORMAT"}
Truth table100%3/3
A and B
A= $fieldref->{"FORMAT"}B= $self->{"accept"}{"FORMAT"}
ABresult
0X0
100
111
824 4 ▶ if ( $fieldref->{'FORMAT'} =~ $self->{accept}->{'FORMAT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldref->{'FORMAT'} =~ /$self->{'accept'}{'FORMAT'}/) { } 4 0 4
825 4 $format = $fieldref->{'FORMAT'}}
826 else {
827 0 push(@carp, "ERROR: only FORMAT '".$self->{accept}->{'FORMAT'}."' are supported, this is ".$fieldref->{'FORMAT'})}
828 }
829 elsif ( $fieldref->{'FORMAT'} ) {
830 8 $format = $fieldref->{'FORMAT'}}
831 ▶ elsif ( $fieldref->{'VERSION'} or $fieldref->{'TARGET'} or $fieldref->{'PREFIX'} or $fieldref->{'MESSAGE'} ) {
Condition33%3/9
$fieldref->{"VERSION"} or $fieldref->{"TARGET"} or $fieldref->{"PREFIX"} or $fieldref->{"MESSAGE"}
l!l&&r!l&&!r
200
$fieldref->{"VERSION"} or $fieldref->{"TARGET"} or $fieldref->{"PREFIX"}
l!l&&r!l&&!r
200
$fieldref->{"VERSION"} or $fieldref->{"TARGET"}
l!l&&r!l&&!r
020
MC/DC0%0/4
$fieldref->{"VERSION"} or $fieldref->{"TARGET"} or $fieldref->{"PREFIX"} or $fieldref->{"MESSAGE"}
$fieldref->{"VERSION"} $fieldref->{"TARGET"} $fieldref->{"PREFIX"} $fieldref->{"MESSAGE"}
Truth table20%1/5
A or B or C or D
A= $fieldref->{"VERSION"}B= $fieldref->{"TARGET"}C= $fieldref->{"PREFIX"}D= $fieldref->{"MESSAGE"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
832 2 push(@carp, "WARNING: header line #FORMAT: BEACON should be supplied")}
833 elsif ( $self->{accept}->{'FORMAT'} ) {
834 0 push(@carp, "ERROR: header line #FORMAT is missing")}
835 else {
836 0 push(@carp, "WARNING: header line #FORMAT: BEACON should be supplied")};
837  
838 14 ▶ if ( $fieldref->{'FORMAT'} && ($fieldref->{'FORMAT'} =~ /v(?:ersion)?\s*(\d+(?:\.\d*)?)/i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldref->{"FORMAT"} and $fieldref->{"FORMAT"} =~ /v(?:ersion)?\s*(\d+(?:\.\d*)?)/i) 0 14 14
Condition66%2/3
$fieldref->{"FORMAT"} and $fieldref->{"FORMAT"} =~ /v(?:ersion)?\s*(\d+(?:\.\d*)?)/i
!ll&&!rl&&r
2120
MC/DC0%0/2
$fieldref->{"FORMAT"} and $fieldref->{"FORMAT"} =~ /v(?:ersion)?\s*(\d+(?:\.\d*)?)/i
$fieldref->{"FORMAT"} $fieldref->{"FORMAT"} =~ /v(?:ersion)?\s*(\d+(?:\.\d*)?)/i
Truth table66%2/3
A and B
A= $fieldref->{"FORMAT"}B= $fieldref->{"FORMAT"} =~ /v(?:ersion)?\s*(\d+(?:\.\d*)?)/i
ABresult
0X0
100
111
839 0 ▶ $fieldref->{'VERSION'} ||= $1};
Condition0%0/3
$fieldref->{"VERSION"} ||= $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$fieldref->{"VERSION"} ||= $1
$fieldref->{"VERSION"} $1
Truth table0%0/3
A ||= B
A= $fieldref->{"VERSION"}B= $1
ABresult
1X1
011
000
840 14 ▶ unless ( $fieldref->{'VERSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fieldref->{"VERSION"}) 3 11 14
841 3 ▶ $fieldref->{'VERSION'} = $fieldref->{'FORMAT'} ? "0.1" : "1.0";
Branch: 1 branch
BranchTrueFalseTotal
$fieldref->{'FORMAT'} ? : 1 2 3
842 3 push(@carp, "NOTICE: added header field #VERSION as '".$fieldref->{'VERSION'}."'");
843 };
844 14 ▶ if ( $self->{accept}->{'VERSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"accept"}{"VERSION"}) 5 9 14
845 ($fieldref->{'VERSION'} =~ $self->{accept}->{'VERSION'})
846 5 ▶ || push(@carp, "ERROR: only VERSION '".$self->{accept}->{'VERSION'}."' is supported, this is ".$fieldref->{'VERSION'});
Branch: 1 branch
BranchTrueFalseTotal
$fieldref->{"VERSION"} =~ /$self->{"accept"}{"VERSION"}/ or push @carp, q[ERROR: only VERSION '] . $self->{"accept"}{"VERSION"} . q[' is supported, this is ] . $fieldref->{"VERSION"} 0 5 5
847 };
848  
849 14 ▶ if ( $fieldref->{'ALTTARGET'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldref->{"ALTTARGET"}) 11 3 14
850 11 ▶ $fieldref->{'ALTTARGET'} = "" unless defined $fieldref->{'ALTTARGET'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fieldref->{"ALTTARGET"} 0 11 11
851 11 my $parsed = hDecode($fieldref, 'ALTTARGET');
852 11 ▶ if ( $parsed && ($parsed =~ /(^|[^%])(%.)*%\d\$s/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed and $parsed =~ /(^|[^%])(%.)*%\d\$s/) { } 11 0 11
elsif ($parsed) { } 0 0 0
Condition33%1/3
$parsed and $parsed =~ /(^|[^%])(%.)*%\d\$s/
!ll&&!rl&&r
0011
MC/DC0%0/2
$parsed and $parsed =~ /(^|[^%])(%.)*%\d\$s/
$parsed $parsed =~ /(^|[^%])(%.)*%\d\$s/
Truth table33%1/3
A and B
A= $parsedB= $parsed =~ /(^|[^%])(%.)*%\d\$s/
ABresult
0X0
100
111
853 11 $fieldref->{'ALTTARGET'} = $parsed;
854 11 $format =~ s/\s*-altTARGET//;
855 11 $format .= " -altTARGET";
856 11 ▶ ($parsed =~ /(^|[^%])(%.)*%2\$s/) or
Branch: 1 branch
BranchTrueFalseTotal
$parsed =~ /(^|[^%])(%.)*%2\$s/ or push @carp, "WARNING: header field #ALTTARGET should contain placeholder {ALTID}" 0 11 11
857 push(@carp, "WARNING: header field #ALTTARGET should contain placeholder {ALTID}");
858 }
859 elsif ( $parsed ) {
860 0 push(@carp, "ERROR: header field #ALTTARGET must contain placeholder {ALTID} (or {ID})");
861 0 delete $fieldref->{'ALTTARGET'};
862 }
863 else {
864 0 push(@carp, "ERROR: could not parse header field #ALTTARGET: '".$fieldref->{'ALTTARGET'}."'");
865 0 delete $fieldref->{'ALTTARGET'};
866 }
867 };
868  
869 14 ▶ if ( $fieldref->{'IMGTARGET'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldref->{"IMGTARGET"}) 0 14 14
870 0 ▶ $fieldref->{'IMGTARGET'} = "" unless defined $fieldref->{'IMGTARGET'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fieldref->{"IMGTARGET"} 0 0 0
871 0 my $parsed = hDecode($fieldref, 'IMGTARGET');
872 0 ▶ if ( $parsed && ($parsed =~ /(^|[^%])(%.)*%\d\$s/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed and $parsed =~ /(^|[^%])(%.)*%\d\$s/) { } 0 0 0
elsif ($parsed) { } 0 0 0
Condition0%0/3
$parsed and $parsed =~ /(^|[^%])(%.)*%\d\$s/
!ll&&!rl&&r
000
MC/DC0%0/2
$parsed and $parsed =~ /(^|[^%])(%.)*%\d\$s/
$parsed $parsed =~ /(^|[^%])(%.)*%\d\$s/
Truth table0%0/3
A and B
A= $parsedB= $parsed =~ /(^|[^%])(%.)*%\d\$s/
ABresult
0X0
100
111
873 0 $fieldref->{'IMGTARGET'} = $parsed;
874 0 $format =~ s/\s*-imgTARGET//;
875 0 $format .= " -imgTARGET";
876 }
877 elsif ( $parsed ) {
878 0 push(@carp, "WARNING: header field #IMGTARGET should contain placeholders {ID} or {ALTID}")}
879 else {
880 0 push(@carp, "ERROR: could not parse header field #IMGTARGET: '".$fieldref->{'IMGTARGET'}."'");
881 0 delete $fieldref->{'IMGTARGET'};
882 }
883 };
884  
885 14 ▶ if ( exists $fieldref->{'TARGET'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $fieldref->{'TARGET'}) { } 14 0 14
elsif ($format =~ /^BEACON/) { } 0 0 0
886 14 ▶ $fieldref->{'TARGET'} = "" unless defined $fieldref->{'TARGET'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fieldref->{"TARGET"} 0 14 14
887 14 my $parsed = hDecode($fieldref, 'TARGET');
888 14 ▶ if ( $parsed && ($parsed =~ /(^|[^%])(%.)*%1\$s/) && ($parsed !~ /(^|[^%])(%.)*%[2-9]\$s/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed and $parsed =~ /(^|[^%])(%.)*%1\$s/ and not $parsed =~ /(^|[^%])(%.)*%[2-9]\$s/) { } 12 2 14
elsif ($parsed) { } 2 0 2
Condition66%4/6
$parsed and $parsed =~ /(^|[^%])(%.)*%1\$s/ and not $parsed =~ /(^|[^%])(%.)*%[2-9]\$s/
!ll&&!rl&&r
2012
$parsed and $parsed =~ /(^|[^%])(%.)*%1\$s/
!ll&&!rl&&r
0212
MC/DC33%1/3
$parsed and $parsed =~ /(^|[^%])(%.)*%1\$s/ and not $parsed =~ /(^|[^%])(%.)*%[2-9]\$s/
$parsed $parsed =~ /(^|[^%])(%.)*%1\$s/ not $parsed =~ /(^|[^%])(%.)*%[2-9]\$s/
Truth table50%2/4
A and B and not(C)
A= $parsedB= $parsed =~ /(^|[^%])(%.)*%1\$s/C= not $parsed =~ /(^|[^%])(%.)*%[2-9]\$s/
ABCresult
0XX0
10X0
1100
1111
889 12 $fieldref->{'TARGET'} = $parsed;
890 12 $format .= " -hasTARGET";
891 }
892 elsif ( $parsed ) {
893 2 ▶ if ( exists $fieldref->{'ALTTARGET'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fieldref->{'ALTTARGET'}) { } 0 2 2
894 0 push(@carp, "ERROR: header field #TARGET must contain placeholder {ID} only");
895 }
896 else {
897 2 push(@carp, "WARNING: Adding implicit {ID} to #TARGET as #ALTTARGET");
898 2 $fieldref->{'ALTTARGET'} = $parsed."%2\$s";
899 2 $format .= " -altTARGET";
900 }
901 2 delete $fieldref->{'TARGET'};
902 }
903 else {
904 0 push(@carp, "ERROR: could not parse header field #TARGET: '".$fieldref->{'TARGET'}."'");
905 0 delete $fieldref->{'TARGET'};
906 }
907 }
908 elsif ( $format =~ /^BEACON/ ) {
909 0 push(@carp, "WARNING: header field #TARGET not set: ALL beacon lines will have to provide their link by other means!");
910 0 $format =~ s/\s*-noTARGET//;
911 0 $format .= " -noTARGET";
912 }
913 else {
914 0 push(@carp, "ERROR: header field #TARGET is mandatory")};
915  
916  
917 14 ▶ $fieldref->{'MESSAGE'} = hDecode($fieldref, 'MESSAGE') if $fieldref->{'MESSAGE'};
Branch: 1 branch
BranchTrueFalseTotal
if $fieldref->{"MESSAGE"} 0 14 14
918  
919 14 ▶ if ( $fieldref->{'TIMESTAMP'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldref->{'TIMESTAMP'}) { } 2 12 14
920 2 ▶ if ( my $parsed = hDecode($fieldref, 'TIMESTAMP') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parsed = hDecode($fieldref, 'TIMESTAMP')) { } 2 0 2
921 2 ▶ printf("* %-30s %s\n", "Beacon Timestamp:", hEncode($parsed, 'TIMESTAMP')) if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 2 0 2
922 2 $fieldref->{'TIMESTAMP'} = $parsed;
923 }
924 else { # unparseable => use current
925 0 push(@carp, "WARNING: cannot parse TIMESTAMP '".$fieldref->{'TIMESTAMP'}."', using current time");
926 0 $fieldref->{'TIMESTAMP'} = $^T;
927 };
928 }
929 else {
930 # $fieldref->{'TIMESTAMP'} = $fieldref->{'_mtime'} || $^T;
931 12 push(@carp, "NOTICE: no header field #TIMESTAMP detected");
932 };
933  
934 14 ▶ if ( $fieldref->{'REVISIT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldref->{'REVISIT'}) { } 0 14 14
935 0 ▶ if ( my $parsed = hDecode($fieldref, 'REVISIT') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parsed = hDecode($fieldref, 'REVISIT')) { } 0 0 0
936 0 ▶ if ( $parsed < $^T ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed < $^T) { } 0 0 0
937 0 printf("* %-30s %s [%s]\n", "STALE Revisit hint parsed as", hEncode($parsed, 'REVISIT'), $fieldref->{'REVISIT'})} # if $options{'verbose'}
938 else {
939 0 ▶ printf("* %-30s %s\n", "Revisit hint parsed as", hEncode($parsed, 'REVISIT')) if $options{'verbose'}};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
940 0 $fieldref->{'REVISIT'} = $parsed;
941 }
942 else { # unparseable => discard
943 0 push(@carp, "WARNING: cannot parse #REVISIT '".$fieldref->{'REVISIT'}."', discarding");
944 0 delete $fieldref->{'REVISIT'};
945 };
946 }
947 else {
948 14 push(@carp, "INFO: no header field #REVISIT detected");
949 };
950  
951 14 my $cancontinue = 1;
952 14 my $err = "";
953 14 foreach ( @carp ) {
954 33 print "$_\n";
955 33 ▶ if ( s/^ERROR: // ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^ERROR: //) 0 33 33
956 0 $cancontinue = 0;
957 0 ▶ $err .= " | " if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
958 0 $err .= $_;
959 };
960 }
961 14 ▶ unless ( $cancontinue or $options{'ignore-header-errors'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cancontinue or $options{"ignore-header-errors"}) 0 14 14
Condition33%1/3
$cancontinue or $options{"ignore-header-errors"}
l!l&&r!l&&!r
1400
MC/DC0%0/2
$cancontinue or $options{"ignore-header-errors"}
$cancontinue $options{"ignore-header-errors"}
Truth table33%1/3
A or B
A= $cancontinueB= $options{"ignore-header-errors"}
ABresult
1X1
011
000
962 0 print "CRITICAL: Aborting because of Header Errors\n";
963 0 return (undef, $err, $format);
964 };
965  
966 14 ▶ $fieldref->{'_uri'} ||= $fieldref->{'FEED'};
Condition66%2/3
$fieldref->{"_uri"} ||= $fieldref->{"FEED"}
l!l&&r!l&&!r
1301
MC/DC50%1/2
$fieldref->{"_uri"} ||= $fieldref->{"FEED"}
$fieldref->{"_uri"} $fieldref->{"FEED"}
Truth table66%2/3
A ||= B
A= $fieldref->{"_uri"}B= $fieldref->{"FEED"}
ABresult
1X1
011
000
967 14 ▶ delete $fieldref->{'_uri'} unless $fieldref->{'_uri'};
Branch: 1 branch
BranchTrueFalseTotal
unless $fieldref->{"_uri"} 1 13 14
968  
969 14 ▶ $fieldref->{'_alias'} ||= $fieldref->{'FEED'} || $fieldref->{'TARGET'};
Condition16%1/6
$fieldref->{"_alias"} ||= $fieldref->{"FEED"} || $fieldref->{"TARGET"}
l!l&&r!l&&!r
1400
$fieldref->{'FEED'} || $fieldref->{'TARGET'}
l!l&&r!l&&!r
000
MC/DC0%0/3
$fieldref->{"_alias"} ||= $fieldref->{"FEED"} || $fieldref->{"TARGET"}
$fieldref->{"_alias"} $fieldref->{'FEED'} $fieldref->{'TARGET'}
Truth table0%0/4
A ||= B || C
A= $fieldref->{"_alias"}B= $fieldref->{'FEED'}C= $fieldref->{'TARGET'}
ABCresult
1XX1
01X1
0011
0000
970  
971 14 my (@fn, @fd);
972 14 ▶ while ( my ($key, $val) = each %$fieldref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fieldref) 175 14 189
973 175 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 4 171 175
974 171 my $dbkey = "";
975 171 ▶ if ( $dbkey = SeeAlso::Source::BeaconAggregator->beaconfields($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbkey = 'SeeAlso::Source::BeaconAggregator'->beaconfields($key)) { } 84 87 171
elsif ($key =~ /_(\w+)$/) { } 87 0 87
976 84 push(@fn, $dbkey)}
977 elsif ( $key =~ /_(\w+)$/ ) {
978 87 push(@fn, $1)}
979 else {
980 0 next};
981 171 my $myval = $val;
982 171 ▶ unless ( $myval =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($myval =~ /^\d+$/) 129 42 171
983 129 $myval =~ s/'/''/g;
984 129 $myval = "'".$myval."'";
985 };
986 171 push(@fd, $myval);
987 };
988 14 local($") = ",\n";
989 14 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
990 INSERT INTO repos ( seqno, @fn ) VALUES ( NULL, @fd );
991 XxX
992 14 ▶ $self->stmtExplain($sthexpl) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 14 14
993 14 ▶ $sth->execute() or croak("Could not execute >".$sth->{Statement}."<:".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak('Could not execute >' . $sth->{"Statement"} . '<:' . $sth->errstr) 0 14 14
994 14 my $collno = $self->{dbh}->last_insert_id("", "", "", "");
995  
996 14 my $rhandle;
997 14 ▶ if ( $osq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($osq) 8 6 14
998 8 $rhandle = $self->stmtHdl(<<"XxX");
999 UPDATE OR FAIL beacons SET seqno=$collno, hits=?, info=?, link=? WHERE hash=? AND seqno==$osq AND altid=?;
1000 XxX
1001 };
1002 14 my $ihandle = $self->stmtHdl(<<"XxX");
1003 INSERT OR IGNORE INTO beacons ( hash, seqno, altid, hits, info, link ) VALUES (?, $collno, ?, ?, ?, ?);
1004 XxX
1005 14 return ($collno, "", $format, $ihandle, $rhandle, $osq);
1006 }
1007  
1008  
1009  
1010  
1011 37 my ($lwpcarp817, $lwpcarp827);
1012  
1013 =head3 update ($sq_or_alias, $params, %options)
1014  
1015 Loads a beacon file into the database, possibly replacing a previous instance.
1016  
1017 Some magic is employed to autoconvert ISO-8859-1 or doubly UTF-8 encoded files
1018 back to UTF-8.
1019  
1020 Returns undef, if something goes wrong, or the file was not modified since,
1021 otherwise returns a pair (new seqence number, number of lines imported).
1022  
1023  
1024 =over 8
1025  
1026  
1027 =item $sq_or_alias
1028  
1029 Sequence number or alias: Used to determine an existing instance.
1030  
1031  
1032 =item $params
1033  
1034 Hashref, containing
1035  
1036 agent => LWP::UserAgent to use
1037 _uri => Feed URL to load from
1038  
1039 =item %options
1040  
1041 Hash, propagated to C<loadFile()>
1042  
1043 verbose => (0|1)
1044 force => (0|1) process unconditionally without timestamp comparison
1045 nostat => (0|1) don't refresh global identifier counters
1046  
1047 =back
1048  
1049 Incorporates a new beacon source from a URI in the database or updates an existing one.
1050 For HTTP URIs care is taken not to reload an unmodified BEACON feed (unless the 'force'
1051 option is provided).
1052  
1053 If the feed appears to be newer than the previously loaded version it is fetched,
1054 some UTF-8 adjustments are performed if necessary, then it is stored to a temporary file
1055 and from there finally processed by the C<loadFile()> method above.
1056  
1057 The URI to load is determined by the following order of precedence:
1058  
1059 =over 8
1060  
1061 =item 1
1062  
1063 _uri Option
1064  
1065 =item 2
1066  
1067 admin field uri stored in the database
1068  
1069 =item 3
1070  
1071 meta field #FEED taken from the database
1072  
1073 =back
1074  
1075 Typical use is with an alias, not with a sequence number:
1076  
1077 $db->update('whatever');
1078  
1079 Can be used to initially load beacon files from URIs:
1080  
1081 $db->update("new_alias", {_uri => $file_uri} );
1082  
1083 =cut
1084  
1085 sub update {
1086 13 ▶ my ($self, $sq_or_alias, $params, %options) = @_;
Subroutine update: called CC=67 SCAR=60.3
Pod documented
1087 13 ▶ $params = {} unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 2 11 13
1088 13 ▶ $options{'verbose'} = $self->{'verbose'} unless exists $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"verbose"} 5 8 13
1089  
1090 13 my $ua = $params->{'agent'};
1091 13 ▶ unless ( $ua ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 13 0 13
1092 13 require LWP::UserAgent;
1093 13 ▶ $ua = LWP::UserAgent->new(agent => "SA-S-BeaconAggregator ", # end with space to get default agent appended
Branch: 1 branch
BranchTrueFalseTotal
$ua = "LWP::UserAgent"->new("agent", "SA-S-BeaconAggregator ", "env_proxy", 1, "timeout", 300) or croak("cannot create UserAgent") 0 13 13
1094 env_proxy => 1,
1095 timeout => 300,
1096 ) or croak("cannot create UserAgent");
1097 };
1098  
1099 13 my ($cond, @cval) = SeeAlso::Source::BeaconAggregator::mkConstraint($sq_or_alias);
1100 13 ▶ my $alias = ($sq_or_alias =~ /^\d+$/) ? "" : $sq_or_alias;
Branch: 1 branch
BranchTrueFalseTotal
$sq_or_alias =~ /^\d+$/ ? : 1 12 13
1101 13 my $feedname = SeeAlso::Source::BeaconAggregator->beaconfields("FEED");
1102 13 my ($ssth, $ssthexpl) = $self->stmtHdl(<<"XxX");
1103 SELECT seqno, uri, alias, $feedname, ftime, mtime, sort FROM repos $cond;
1104 XxX
1105 13 ▶ $self->stmtExplain($ssthexpl, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 13 13
1106 13 ▶ $ssth->execute(@cval) or croak("Could not execute >".$ssth->{Statement}."<: ".$ssth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$ssth->execute(@cval) or croak('Could not execute >' . $ssth->{"Statement"} . '<: ' . $ssth->errstr) 0 13 13
1107 13 ▶ croak("Select old instance error: ".$ssth->errstr) if $ssth->err;
Branch: 1 branch
BranchTrueFalseTotal
if $ssth->err 0 13 13
1108 13 my $aryref = $ssth->fetchrow_arrayref;
1109 13 ▶ my ($osq, $ouri, $oalias, $feed, $fetchtime, $modtime, $osort) = $aryref ? @$aryref : ();
Branch: 1 branch
BranchTrueFalseTotal
$aryref ? : 9 4 13
1110  
1111 13 ▶ my $uri = $params->{'_uri'} || $ouri || $feed;
Condition50%3/6
$params->{'_uri'} || $ouri || $feed
l!l&&r!l&&!r
1300
$params->{"_uri"} or $ouri
l!l&&r!l&&!r
580
MC/DC0%0/3
$params->{'_uri'} || $ouri || $feed
$params->{"_uri"} $ouri $feed
Truth table50%2/4
A or B || C
A= $params->{"_uri"}B= $ouriC= $feed
ABCresult
1XX1
01X1
0011
0000
1112 13 ▶ croak("Cannot update $sq_or_alias: URI not given nor determinable from previous content") unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 13 13
1113 13 $uri =~ s/\s$//;
1114 13 ▶ $alias ||= $oalias || "";
Condition60%3/5
$alias ||= $oalias || ""
l!l&&r!l&&!r
1210
$oalias || ''
l!l
10
MC/DC0%0/2
$alias ||= $oalias || ""
$alias $oalias
Truth table66%2/3
A ||= B
A= $aliasB= $oalias
ABresult
1X1
011
000
1115  
1116 13 ▶ print "Requesting $uri\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 8 5 13
1117 13 ▶ my $rq = HTTP::Request->new('GET', $uri, ['Accept' => 'text/*']) or croak("could not construct request from $uri");
Branch: 1 branch
BranchTrueFalseTotal
my $rq = "HTTP::Request"->new("GET", $uri, ["Accept", "text/*"]) or croak("could not construct request from $uri") 0 13 13
1118 13 ▶ if ( $fetchtime && $modtime && !$options{'force'} ) { # allow force-reload by deleting _ftime or _mtime
Branch: 1 branch
BranchTrueFalseTotal
if ($fetchtime and $modtime and not $options{"force"}) 1 12 13
Condition100%6/6
$fetchtime and $modtime and not $options{"force"}
!ll&&!rl&&r
571
$fetchtime and $modtime
!ll&&!rl&&r
418
MC/DC100%3/3
$fetchtime and $modtime and not $options{"force"}
$fetchtime $modtime not $options{"force"}
Truth table100%4/4
A and B and not(C)
A= $fetchtimeB= $modtimeC= not $options{"force"}
ABCresult
0XX0
10X0
1100
1111
1119 1 ▶ printf(" %-30s %s\n", "Old instance stamped", scalar localtime($modtime)) if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 1 1
1120 1 $rq->header('If-Modified-Since', HTTP::Date::time2str($modtime));
1121 };
1122 13 ▶ if ( $rq->can("accept_decodable") ) { # LWP 5.817 and newer
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->can('accept_decodable')) { } 13 0 13
1123 13 $rq->accept_decodable}
1124 else {
1125 0 ▶ carp("please upgrade to LWP >= 5.817 for compression negotiation") if $options{'verbose'} && (!$lwpcarp817++)};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} and not $lwpcarp817++ 0 0 0
Condition0%0/3
$options{"verbose"} and not $lwpcarp817++
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"verbose"} and not $lwpcarp817++
$options{"verbose"} not $lwpcarp817++
Truth table0%0/3
A and not(B)
A= $options{"verbose"}B= not $lwpcarp817++
ABresult
0X0
100
111
1126  
1127 13 my $response = $ua->request($rq); # Well, we hoggishly slurp everything into memory,
1128 # however explicit decompression of an already dumped result would be PITA
1129 13 my $nuri = ($response->request)->uri;
1130 13 ▶ print "NOTICE: Differing result URI: $nuri\n" if $uri ne $nuri;
Branch: 1 branch
BranchTrueFalseTotal
if $uri ne $nuri 0 13 13
1131 13 ▶ if ( $response->is_success ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->is_success) { } 12 1 13
elsif ($response->code == 304) { } 1 0 1
1132 12 ▶ print $osq ? "INFO: refreshing $alias sq $osq from $uri\n"
Branch: 1 branch
BranchTrueFalseTotal
$osq ? : 8 4 12
1133 : "INFO: importing previously unseen $alias from $uri\n";
1134 12 my $charset;
1135 12 ▶ if ( $response->can("content_charset") ) { # LWP 5.827 and above
Branch: 1 branch
BranchTrueFalseTotal
if ($response->can('content_charset')) { } 12 0 12
1136 12 $charset = $response->content_charset;
1137 12 ▶ print "DEBUG: Content charset is $charset\n" if $charset && $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $charset and $options{"verbose"} 8 4 12
Condition66%2/3
$charset and $options{"verbose"}
!ll&&!rl&&r
048
MC/DC50%1/2
$charset and $options{"verbose"}
$charset $options{"verbose"}
Truth table66%2/3
A and B
A= $charsetB= $options{"verbose"}
ABresult
0X0
100
111
1138 }
1139 else {
1140 0 ▶ carp("please upgrade to LWP >= 5.827 for better detection of content_charset") if $options{'verbose'} && (!$lwpcarp827++)};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} and not $lwpcarp827++ 0 0 0
Condition0%0/3
$options{"verbose"} and not $lwpcarp827++
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"verbose"} and not $lwpcarp827++
$options{"verbose"} not $lwpcarp827++
Truth table0%0/3
A and not(B)
A= $options{"verbose"}B= not $lwpcarp827++
ABresult
0X0
100
111
1141 12 ▶ $charset ||= "UTF-8";
Condition50%1/2
$charset ||= "UTF-8"
l!l
120
MC/DC0%0/1
$charset ||= "UTF-8"
$charset
Truth table50%1/2
A
A= $charset
Aresult
11
00
1142  
1143 12 my $lm = $response->last_modified;
1144 12 ▶ printf(" %-30s %s\n", "Last modified", scalar localtime($lm)) if $lm && $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $lm and $options{"verbose"} 8 4 12
Condition66%2/3
$lm and $options{"verbose"}
!ll&&!rl&&r
048
MC/DC50%1/2
$lm and $options{"verbose"}
$lm $options{"verbose"}
Truth table66%2/3
A and B
A= $lmB= $options{"verbose"}
ABresult
0X0
100
111
1145 12 ▶ $lm ||= $^T;
Condition33%1/3
$lm ||= $^T
l!l&&r!l&&!r
1200
MC/DC0%0/2
$lm ||= $^T
$lm $^T
Truth table33%1/3
A ||= B
A= $lmB= $^T
ABresult
1X1
011
000
1146  
1147 12 my $vt = $response->fresh_until(h_min => 1800, h_max => 30 * 86400);
1148 12 ▶ printf(" %-30s %s\n", "Should be valid until", scalar localtime($vt)) if $vt && $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $vt and $options{"verbose"} 8 4 12
Condition66%2/3
$vt and $options{"verbose"}
!ll&&!rl&&r
048
MC/DC50%1/2
$vt and $options{"verbose"}
$vt $options{"verbose"}
Truth table66%2/3
A and B
A= $vtB= $options{"verbose"}
ABresult
0X0
100
111
1149 12 ▶ $vt ||= 0;
Condition50%1/2
$vt ||= 0
l!l
120
MC/DC0%0/1
$vt ||= 0
$vt
Truth table50%1/2
A
A= $vt
Aresult
11
00
1150  
1151 # temporary file for dumped contents
1152 12 ▶ my ($tmpfh, $tmpfile) = File::Temp::tempfile("BeaconAggregator-XXXXXXXX", SUFFIX => ".txt", TMPDIR => 1) or croak("Could not acquire temporary file for storage");
Branch: 1 branch
BranchTrueFalseTotal
() = File::Temp::tempfile("BeaconAggregator-XXXXXXXX", "SUFFIX", ".txt", "TMPDIR", 1) or croak("Could not acquire temporary file for storage") 0 12 12
1153 12 my $contref; # reference to content buffer
1154 12 ▶ if ( ! $response->content_is_text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->content_is_text) 0 12 12
1155 0 my $ct = $response->content_type;
1156 0 print "WARNING: Response content is $ct, not text/*\n";
1157 0 ▶ if ( my $ce = $response->content_encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ce = $response->content_encoding) 0 0 0
1158 0 print "NOTICE: Response is also Content-encoded: $ce\n"}
1159 0 my $ctt = join("|", $response->decodable());
1160 0 ▶ if ( $ct =~ s!^(.+\/)?($ctt)$!$2! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ s[^(.+\/)?($ctt)$][$2]) 0 0 0
1161 # yes: decode anyway since it could be a gzip-encoded .txt.gz file!
1162 0 my $cr = $response->decoded_content( raise_error => 1, ref => 1); # method exists since LWP 5.802 (2004-11-30)
1163 0 $response->remove_content_headers;
1164 0 my $newresp = HTTP::Response->new($response->code, $response->message, $response->headers);
1165 0 $newresp->content_type("text/plain; charset: $charset");
1166 0 $newresp->content_encoding($ct);
1167 0 $newresp->content_ref($cr);
1168 0 $response = $newresp;
1169 }
1170 };
1171 12 $contref = $response->decoded_content( raise_error => 1, ref => 1); # method exists since LWP 5.802 (2004-11-30)
1172  
1173 12 ▶ if ( $$contref =~ /^\x{FFEF}/ ) { # properly encoded BOM => put Characters to file
Branch: 4 branches
BranchTrueFalseTotal
if ($$contref =~ /^\x{FFEF}/) { } 0 12 12
elsif ($$contref =~ s/^\xef\xbb\xbf//) { } 0 12 12
elsif (utf8::is_utf8($$contref)) { } 10 2 12
elsif (utf8::decode($$contref)) { } 2 0 2
1174 0 binmode($tmpfh, ":utf8");
1175 0 print "INFO: properly encoded BOM detected: Groked UTF8\n"; # if $options{'verbose'};
1176 }
1177 elsif ( $$contref =~ s/^\xef\xbb\xbf// ) { # BOM Bytes => put Bytes to file, re-read as UTF-8
1178 0 print "INFO: Byte coded BOM detected: trying to restitute character semantics\n"; # if $options{'verbose'};
1179 0 ▶ print "INFO: Length is ", length($$contref), " ", (utf8::is_utf8($$contref) ? "characters" : "bytes"), "\n";
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($$contref) ? : 0 0 0
1180 0 binmode($tmpfh, ":bytes");
1181 0 ▶ utf_deduplicate($contref) && binmode($tmpfh, ":utf8");
Branch: 1 branch
BranchTrueFalseTotal
utf_deduplicate($contref) and binmode $tmpfh, ":utf8" 0 0 0
1182 }
1183 elsif ( utf8::is_utf8($$contref) ) { # already Upgraded strings should be written as utf-8
1184 10 ▶ print "INFO: UTF8-ness already established\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 6 4 10
1185 10 binmode($tmpfh, ":utf8");
1186 10 utf_deduplicate($contref); # but don't trust it (older LWP with file URLs, ...)
1187 }
1188 elsif ( utf8::decode($$contref) ) { # everything in character semantics now
1189 2 ▶ print "INFO: Could decode bytes to UTF8-characters\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 2 0 2
1190 2 binmode($tmpfh, ":utf8");
1191 }
1192 else { # leave it alone
1193 0 print "WARNING: No clue about character encoding: Assume ISO 8859-1\n"; # if $options{'verbose'};
1194 0 binmode($tmpfh, ":utf8");
1195 };
1196 12 print $tmpfh $$contref;
1197 12 close($tmpfh);
1198 # early cleanup since everything might be huge....
1199 12 $contref = $response = undef;
1200  
1201 12 my ($collno, $count, $statref) = $self->loadFile($tmpfile, {_alias => $alias, _uri => $uri, _ruri => $nuri, _mtime => $lm, _sort => $osort}, %options);
1202 12 ▶ if ( ! $collno && $osq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $collno and $osq) 0 12 12
Condition33%1/3
not $collno and $osq
!ll&&!rl&&r
1200
MC/DC0%0/2
not $collno and $osq
not $collno $osq
Truth table33%1/3
not(A) and B
A= not $collnoB= $osq
ABresult
0X0
100
111
1203 0 my ($usth, $usthexpl) = $self->stmtHdl(<<"XxX");
1204 UPDATE OR FAIL repos SET utime=?,ustat=? WHERE seqno==?;
1205 XxX
1206 0 ▶ $self->stmtExplain($usthexpl, time(), ($statref ? "load error: $statref" : "internal error"), $osq) if $ENV{'DBI_PROFILE'};
Branch: 2 branches
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 0 0
$statref ? : 0 0 0
1207 $usth->execute(time(), ($statref ? "load error: $statref" : "internal error"), $osq)
1208 0 ▶ or croak("Could not execute >".$usth->{Statement}."<: ".$usth->errstr);
Branch: 2 branches
BranchTrueFalseTotal
$usth->execute(time, $statref ? "load error: $statref" : "internal error", $osq) or croak('Could not execute >' . $usth->{"Statement"} . '<: ' . $usth->errstr) 0 0 0
$statref ? : 0 0 0
1209 };
1210  
1211 12 ▶ unlink($tmpfile) if -f $tmpfile;
Branch: 1 branch
BranchTrueFalseTotal
if -f $tmpfile 12 0 12
1212 12 ▶ return $collno ? ($collno, $count) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$collno ? : 12 0 12
1213 }
1214 elsif ( $response->code == 304 ) {
1215 1 ▶ print "INFO: $alias not modified since ".HTTP::Date::time2str($modtime)."\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 1 1
1216 1 my $vt = $response->fresh_until(h_min => 1800, h_max => 6 * 86400);
1217 1 ▶ printf(" %-30s %s\n", "Will not try again before", scalar localtime($vt)) if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 1 1
1218 1 my ($usth, $usthexpl) = $self->stmtHdl(<<"XxX");
1219 UPDATE OR FAIL repos SET utime=?,ustat=?,ruri=? WHERE seqno==?;
1220 XxX
1221 1 ▶ $self->stmtExplain($usthexpl, time(), $response->status_line, $nuri, $osq) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 1 1
1222 $usth->execute(time(), $response->status_line, $nuri, $osq)
1223 1 ▶ or croak("Could not execute >".$usth->{Statement}."<: ".$usth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$usth->execute(time, $response->status_line, $nuri, $osq) or croak('Could not execute >' . $usth->{"Statement"} . '<: ' . $usth->errstr) 0 1 1
1224 1 return undef;
1225 }
1226 else {
1227 0 print "WARNING: No access to $uri for $alias [".$response->status_line."]\n";
1228 0 print $response->headers_as_string, "\n";
1229 0 ▶ return undef unless $osq;
Branch: 1 branch
BranchTrueFalseTotal
unless $osq 0 0 0
1230 0 my ($usth, $usthexpl) = $self->stmtHdl(<<"XxX");
1231 UPDATE OR FAIL repos SET utime=?,ustat=?,ruri=? WHERE seqno==?;
1232 XxX
1233 0 ▶ $self->stmtExplain($usthexpl, time(), $response->status_line, $nuri, $osq) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 0 0
1234 $usth->execute(time(), $response->status_line, $nuri, $osq)
1235 0 ▶ or croak("Could not execute >".$usth->{Statement}."<: ".$usth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$usth->execute(time, $response->status_line, $nuri, $osq) or croak('Could not execute >' . $usth->{"Statement"} . '<: ' . $usth->errstr) 0 0 0
1236 0 return undef;
1237 };
1238 }
1239  
1240  
1241  
1242 sub utf_deduplicate {
1243 10 ▶ my ($success, $stringref) = (0, @_);
Subroutine utf_deduplicate: called CC=6 SCAR=26.0
Pod undocumented
1244 10 ▶ if ( utf8::downgrade($$stringref, 1) ) { # 1 = FAIL_OK
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::downgrade($$stringref, 1)) { } 10 0 10
1245 10 my $prevlength = length($$stringref);
1246 10 ▶ print "INFO: Downgrade was possible, length now $prevlength ", (utf8::is_utf8($$stringref) ? "characters" : "bytes"), "\n";
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($$stringref) ? : 0 10 10
1247 10 ▶ while ( utf8::decode($$stringref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::decode($$stringref)) 0 10 10
1248 0 $success ++;
1249 0 my $newlength = length($$stringref);
1250 0 ▶ print "DEBUG: Reassembling as UTF-8 succeeded, length now $newlength ", (utf8::is_utf8($$stringref) ? "characters" : "bytes"), "\n";
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($$stringref) ? : 0 0 0
1251 0 ▶ last if $newlength == $prevlength;
Branch: 1 branch
BranchTrueFalseTotal
if $newlength == $prevlength 0 0 0
1252 0 $prevlength = $newlength;
1253 # last unless utf8::downgrade($$stringref, 1);
1254 }
1255 }
1256 else {
1257 0 print "WARNING: no downgrade possible, proceed with byte semantics";
1258 };
1259 10 return $success;
1260 }
1261  
1262 =head3 unload ( [ $seqno_or_alias, %options ] )
1263  
1264 Deletes the sequence(s).
1265  
1266 =over 8
1267  
1268 =item $seqno_or_alias
1269  
1270 numeric sequence number, Alias or SQL pattern.
1271  
1272 =item Supported options:
1273  
1274 force => (0|1)
1275  
1276 Needed to purge the complete database ($seqno_or_alias empty) or to purge
1277 more than one sequence ($seqno_or_alias yields more than one seqno).
1278  
1279 =back
1280  
1281  
1282 =cut
1283  
1284 sub unload {
1285 3 ▶ my ($self, $seqno_or_alias, %options) = @_;
Subroutine unload: called CC=23 SCAR=46.6
Pod documented
1286 3 ▶ $options{'verbose'} = $self->{'verbose'} unless exists $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"verbose"} 3 0 3
1287  
1288 3 my @seqnos = ();
1289 3 ▶ if ( $seqno_or_alias && ($seqno_or_alias =~ /^\d+$/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($seqno_or_alias and $seqno_or_alias =~ /^\d+$/) { } 0 3 3
elsif ($seqno_or_alias or $options{'force'}) { } 3 0 3
Condition33%1/3
$seqno_or_alias and $seqno_or_alias =~ /^\d+$/
!ll&&!rl&&r
030
MC/DC0%0/2
$seqno_or_alias and $seqno_or_alias =~ /^\d+$/
$seqno_or_alias $seqno_or_alias =~ /^\d+$/
Truth table33%1/3
A and B
A= $seqno_or_aliasB= $seqno_or_alias =~ /^\d+$/
ABresult
0X0
100
111
1290 0 @seqnos = ($seqno_or_alias)}
1291 ▶ elsif ( $seqno_or_alias || $options{'force'} ) {
Condition33%1/3
$seqno_or_alias or $options{"force"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$seqno_or_alias or $options{"force"}
$seqno_or_alias $options{"force"}
Truth table33%1/3
A or B
A= $seqno_or_aliasB= $options{"force"}
ABresult
1X1
011
000
1292 3 ▶ @seqnos = $self->Seqnos('_alias', $seqno_or_alias ? ($seqno_or_alias) : ());
Branch: 1 branch
BranchTrueFalseTotal
$seqno_or_alias ? : 3 0 3
1293 3 ▶ unless ( @seqnos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@seqnos) 0 3 3
1294 0 carp("no Seqnos selected by $seqno_or_alias");
1295 0 return 0;
1296 };
1297 3 ▶ unless ( $options{'force'} or (@seqnos == 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"force"} or @seqnos == 1) 0 3 3
Condition33%1/3
$options{"force"} or @seqnos == 1
l!l&&r!l&&!r
030
MC/DC0%0/2
$options{"force"} or @seqnos == 1
$options{"force"} @seqnos == 1
Truth table33%1/3
A or B
A= $options{"force"}B= @seqnos == 1
ABresult
1X1
011
000
1298 0 carp("Use --force to purge more than one sequence (@seqnos)");
1299 0 return 0;
1300 };
1301 }
1302 else {
1303 0 carp("Use --force to purge the complete database");
1304 0 return 0;
1305 };
1306  
1307 3 ▶ if ( $options{'force'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"force"}) 0 3 3
1308 0 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1309 DELETE FROM beacons WHERE seqno==?;
1310 XxX
1311 0 foreach my $seqno ( @seqnos ) {
1312 0 ▶ $self->stmtExplain($sthexpl, $seqno_or_alias) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 0 0
1313 0 ▶ my $rows = $sth->execute($seqno_or_alias) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $sth->execute($seqno_or_alias) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 0 0
1314 0 ▶ print "INFO: $rows forced for $seqno\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 0 0
1315 };
1316 };
1317  
1318 3 my ($cond, @cval) = SeeAlso::Source::BeaconAggregator::mkConstraint($seqno_or_alias);
1319 3 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1320 DELETE FROM repos $cond;
1321 XxX
1322 3 ▶ $self->stmtExplain($sthexpl, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 3 3
1323 3 ▶ my $rows = $sth->execute(@cval) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $sth->execute(@cval) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 3 3
1324 3 ▶ $rows = 0 if $rows eq "0E0";
Branch: 1 branch
BranchTrueFalseTotal
if $rows eq "0E0" 0 3 3
1325  
1326 3 ▶ if ( $rows or $options{'force'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows or $options{"force"}) 3 0 3
Condition33%1/3
$rows or $options{"force"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$rows or $options{"force"}
$rows $options{"force"}
Truth table33%1/3
A or B
A= $rowsB= $options{"force"}
ABresult
1X1
011
000
1327 # if ( $options{'force'} ) {
1328 # print "[ANALYZE ..." if $options{'verbose'};
1329 # $self->{dbh}->do("ANALYZE;");
1330 # print "]\n" if $options{'verbose'};
1331 # };
1332  
1333 3 ▶ if ( $options{'nostat'} ) { # invalidate since they might have changed
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'nostat'}) { } 0 3 3
1334 0 $self->admin('gcounti', undef);
1335 0 $self->admin('gcountu', undef);
1336 }
1337 else {
1338 3 ▶ $self->admin('gcounti', $self->idStat(undef, 'distinct' => 0) || 0);
Condition50%1/2
$self->idStat(undef, 'distinct', 0) || 0
l!l
30
MC/DC0%0/1
$self->idStat(undef, 'distinct', 0) || 0
$self->idStat(undef, 'distinct', 0)
Truth table50%1/2
A
A= $self->idStat(undef, 'distinct', 0)
Aresult
11
00
1339 3 ▶ $self->admin('gcountu', $self->idStat(undef, 'distinct' => 1) || 0);
Condition50%1/2
$self->idStat(undef, 'distinct', 1) || 0
l!l
30
MC/DC0%0/1
$self->idStat(undef, 'distinct', 1) || 0
$self->idStat(undef, 'distinct', 1)
Truth table50%1/2
A
A= $self->idStat(undef, 'distinct', 1)
Aresult
11
00
1340 }
1341 };
1342  
1343 3 return $rows;
1344 }
1345  
1346  
1347 =head3 purge ( $seqno_or_alias[, %options ] )
1348  
1349 Deletes all identifiers from the database to the given pattern,
1350 but leaves the stored header information intact, such that it
1351 can be updated automatically.
1352  
1353 =over 8
1354  
1355 =item $seqno_or_alias
1356  
1357 Pattern
1358  
1359 =item Supported options:
1360  
1361 force => (0|1)
1362  
1363 Allow purging of more than one sequence.
1364  
1365 =back
1366  
1367  
1368 =cut
1369  
1370 sub purge {
1371 2 ▶ my ($self, $seqno_or_alias, %options) = @_;
Subroutine purge: called CC=22 SCAR=41.2
Pod documented
1372 2 ▶ $options{'verbose'} = $self->{'verbose'} unless exists $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"verbose"} 2 0 2
1373 2 my @seqnos;
1374 2 ▶ if ( $seqno_or_alias && ($seqno_or_alias =~ /^\d+$/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($seqno_or_alias and $seqno_or_alias =~ /^\d+$/) { } 0 2 2
elsif ($seqno_or_alias or $options{'force'}) { } 2 0 2
Condition33%1/3
$seqno_or_alias and $seqno_or_alias =~ /^\d+$/
!ll&&!rl&&r
020
MC/DC0%0/2
$seqno_or_alias and $seqno_or_alias =~ /^\d+$/
$seqno_or_alias $seqno_or_alias =~ /^\d+$/
Truth table33%1/3
A and B
A= $seqno_or_aliasB= $seqno_or_alias =~ /^\d+$/
ABresult
0X0
100
111
1375 0 @seqnos = ($seqno_or_alias)}
1376 ▶ elsif ( $seqno_or_alias || $options{'force'} ) {
Condition33%1/3
$seqno_or_alias or $options{"force"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$seqno_or_alias or $options{"force"}
$seqno_or_alias $options{"force"}
Truth table33%1/3
A or B
A= $seqno_or_aliasB= $options{"force"}
ABresult
1X1
011
000
1377 2 ▶ @seqnos = $self->Seqnos('_alias', $seqno_or_alias ? ($seqno_or_alias) : ());
Branch: 1 branch
BranchTrueFalseTotal
$seqno_or_alias ? : 2 0 2
1378 2 ▶ unless ( @seqnos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@seqnos) 0 2 2
1379 0 carp("no Seqnos selected by $seqno_or_alias");
1380 0 return 0;
1381 };
1382 2 ▶ unless ( $options{'force'} or (@seqnos == 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"force"} or @seqnos == 1) 0 2 2
Condition33%1/3
$options{"force"} or @seqnos == 1
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{"force"} or @seqnos == 1
$options{"force"} @seqnos == 1
Truth table33%1/3
A or B
A= $options{"force"}B= @seqnos == 1
ABresult
1X1
011
000
1383 0 carp("Use --force to purge more than one sequence (@seqnos)");
1384 0 return 0;
1385 };
1386 }
1387 else {
1388 0 carp("Use --force to purge the complete database");
1389 0 return 0;
1390 };
1391 2 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1392 DELETE FROM beacons WHERE seqno==?;
1393 XxX
1394 2 my ($usth, $usthexpl) = $self->stmtHdl(<<"XxX");
1395 UPDATE OR FAIL repos SET counti=?,countu=?,utime=?,ustat=? WHERE seqno==?;
1396 XxX
1397 2 my $trows = 0;
1398 2 foreach my $seqno ( @seqnos ) {
1399 2 ▶ $self->stmtExplain($sthexpl, $seqno) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 2 2
1400 2 ▶ my $rows = $sth->execute($seqno) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $sth->execute($seqno) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 2 2
1401 2 ▶ $rows = "0" if $rows eq "0E0";
Branch: 1 branch
BranchTrueFalseTotal
if $rows eq "0E0" 0 2 2
1402 2 ▶ print "INFO: $rows purged for $seqno\n" if $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 0 2 2
1403 2 $trows += $rows;
1404 2 ▶ $self->stmtExplain($usthexpl, 0, 0, time, "purged", $seqno) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 2 2
1405 $usth->execute(0, 0, time, "purged", $seqno)
1406 2 ▶ or croak("Could not execute >".$usth->{Statement}."<: ".$usth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$usth->execute(0, 0, time, "purged", $seqno) or croak('Could not execute >' . $usth->{"Statement"} . '<: ' . $usth->errstr) 0 2 2
1407 };
1408  
1409 2 ▶ if ( $trows or $options{'force'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trows or $options{"force"}) 2 0 2
Condition33%1/3
$trows or $options{"force"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$trows or $options{"force"}
$trows $options{"force"}
Truth table33%1/3
A or B
A= $trowsB= $options{"force"}
ABresult
1X1
011
000
1410 # if ( $options{'force'} ) {
1411 # print "[ANALYZE ..." if $options{'verbose'};
1412 # $self->{dbh}->do("ANALYZE;");
1413 # print "]\n" if $options{'verbose'};
1414 # };
1415  
1416 2 ▶ if ( $options{'nostat'} ) { # invalidate since they might have changed
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'nostat'}) { } 0 2 2
1417 0 $self->admin('gcounti', undef);
1418 0 $self->admin('gcountu', undef);
1419 }
1420 else {
1421 2 ▶ $self->admin('gcounti', $self->idStat(undef, 'distinct' => 0) || 0);
Condition50%1/2
$self->idStat(undef, 'distinct', 0) || 0
l!l
20
MC/DC0%0/1
$self->idStat(undef, 'distinct', 0) || 0
$self->idStat(undef, 'distinct', 0)
Truth table50%1/2
A
A= $self->idStat(undef, 'distinct', 0)
Aresult
11
00
1422 2 ▶ $self->admin('gcountu', $self->idStat(undef, 'distinct' => 1) || 0);
Condition50%1/2
$self->idStat(undef, 'distinct', 1) || 0
l!l
20
MC/DC0%0/1
$self->idStat(undef, 'distinct', 1) || 0
$self->idStat(undef, 'distinct', 1)
Truth table50%1/2
A
A= $self->idStat(undef, 'distinct', 1)
Aresult
11
00
1423 }
1424 };
1425  
1426 2 return $trows;
1427 }
1428  
1429  
1430 =head2 Methods for headers
1431  
1432 =head3 ($rows, @oldvalues) = headerfield ( $sq_or_alias, $key [, $value] )
1433  
1434 Gets or sets an meta or admin Entry for the constituent file indicated by $sq_or_alias
1435  
1436 =cut
1437  
1438 sub headerfield {
1439 9 ▶ my ($self, $sq_or_alias, $key, $value) = @_;
Subroutine headerfield: called CC=14 SCAR=30.0
Pod documented
1440  
1441 9 my $dbkey = "";
1442 9 ▶ if ( $dbkey = SeeAlso::Source::BeaconAggregator->beaconfields($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbkey = 'SeeAlso::Source::BeaconAggregator'->beaconfields($key)) { } 4 5 9
elsif ($key =~ /_(\w+)$/) { } 5 0 5
1443 }
1444 elsif ( $key =~ /_(\w+)$/ ) {
1445 5 $dbkey = $1}
1446 else {
1447 0 carp "Field $key not known";
1448 0 return undef;
1449 };
1450  
1451 9 my ($cond, @cval) = SeeAlso::Source::BeaconAggregator::mkConstraint($sq_or_alias);
1452  
1453 9 my ($osth, $osthexpl) = $self->stmtHdl(<<"XxX");
1454 SELECT $dbkey FROM repos $cond;
1455 XxX
1456 9 ▶ $self->stmtExplain($osthexpl, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 9 9
1457 9 ▶ $osth->execute(@cval) or croak("Could not execute >".$osth->{Statement}."<:".$osth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$osth->execute(@cval) or croak('Could not execute >' . $osth->{"Statement"} . '<:' . $osth->errstr) 0 9 9
1458 9 my $tmpval = $osth->fetchall_arrayref();
1459 9 ▶ my @oval = map { hEncode($_, $key) } map { (defined $_->[0]) ? ($_->[0]) : () } @$tmpval;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[0] ? : 7 2 9
1460 9 my $rows = scalar @oval;
1461  
1462 9 ▶ if ( (defined $value) and ($value ne "") ) { # set
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and $value ne '') { } 3 6 9
elsif (defined $value) { } 0 6 6
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
603
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
1463 3 my ($usth, $usthexpl) = $self->stmtHdl(<<"XxX");
1464 UPDATE OR FAIL repos SET $dbkey=? $cond;
1465 XxX
1466 3 ▶ $value = hDecode($value, $key) || "";
Condition100%2/2
hDecode($value, $key) || ''
l!l
21
MC/DC100%1/1
hDecode($value, $key) || ''
hDecode($value, $key)
Truth table100%2/2
A
A= hDecode($value, $key)
Aresult
11
00
1467 3 ▶ $self->stmtExplain($usthexpl, $value, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 3 3
1468 3 ▶ $rows = $usth->execute($value, @cval) or croak("Could not execute >".$usth->{Statement}."<:".$usth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$rows = $usth->execute($value, @cval) or croak('Could not execute >' . $usth->{"Statement"} . '<:' . $usth->errstr) 0 3 3
1469 }
1470 elsif ( defined $value ) { # clear
1471 0 my ($dsth, $dsthexpl) = $self->stmtHdl(<<"XxX");
1472 UPDATE OR FAIL repos SET $dbkey=? $cond;
1473 XxX
1474 0 ▶ $self->stmtExplain($dsthexpl, undef, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 0 0
1475 0 ▶ $rows = $dsth->execute(undef, @cval) or croak("Could not execute >".$dsth->{Statement}."<:".$dsth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$rows = $dsth->execute(undef, @cval) or croak('Could not execute >' . $dsth->{"Statement"} . '<:' . $dsth->errstr) 0 0 0
1476 }
1477 else { # read
1478 }
1479  
1480 9 return ($rows, @oval);
1481 }
1482  
1483 =head3 ($resultref, $metaref) = headers ( [ $seqno_or_alias ] )
1484  
1485 Iterates over all
1486  
1487 For each iteration returns two hash references:
1488  
1489 =over 8
1490  
1491 =item 1
1492 all official beacon fields
1493  
1494 =item 2
1495 all administrative fields (_alias, ...)
1496  
1497 =back
1498  
1499 =cut
1500  
1501 sub headers {
1502 5 ▶ my ($self, $seqno_or_alias) = @_;
Subroutine headers: called CC=10 SCAR=23.2
Pod documented
1503  
1504 5 ▶ unless ( $self->{_iterator_info} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_iterator_info"}) 1 4 5
1505 1 my ($constraint, @cval) = SeeAlso::Source::BeaconAggregator::mkConstraint($seqno_or_alias);
1506 1 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1507 SELECT * FROM repos $constraint;
1508 XxX
1509 1 ▶ $self->stmtExplain($sthexpl, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 1 1
1510 1 ▶ $sth->execute(@cval) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@cval) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 1 1
1511 1 $self->{_iterator_info} = $sth;
1512 };
1513  
1514 5 my $info = $self->{_iterator_info}->fetchrow_hashref;
1515 5 ▶ unless ( defined $info ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info) 1 4 5
1516 1 ▶ croak("Error listing Collections: $self->{_iterator_info}->errstr") if $self->{_iterator_info}->err;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_iterator_info"}->err 0 1 1
1517 1 delete $self->{_iterator_info};
1518 1 return undef;
1519 }
1520  
1521 4 ▶ my $collno = $info->{seqno} || $seqno_or_alias;
Condition33%1/3
$info->{'seqno'} || $seqno_or_alias
l!l&&r!l&&!r
400
MC/DC0%0/2
$info->{'seqno'} || $seqno_or_alias
$info->{'seqno'} $seqno_or_alias
Truth table33%1/3
A || B
A= $info->{'seqno'}B= $seqno_or_alias
ABresult
1X1
011
000
1522 4 my %meta = (_seqno => $collno);
1523 4 my %result = ();
1524 4 ▶ while ( my($key, $val) = each %$info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$info) 136 4 140
1525 136 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 65 71 136
1526 71 my $pval = hEncode($val, $key);
1527  
1528 71 ▶ if ( $key =~ /^bc(\w+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^bc(\w+)$/) { } 25 46 71
1529 25 $result{$1} = $pval}
1530 else {
1531 46 $meta{"_$key"} = $pval};
1532 }
1533 4 return \%result, \%meta;
1534 }
1535  
1536 =head3 listCollections ( [ $seqno_or_alias ] )
1537  
1538 Iterates over all Sequences and returns on each call an array of
1539  
1540 Seqno, Alias, Uri, Modification time, Identifier Count and Unique identifier count
1541  
1542 Returns undef if done.
1543  
1544 =cut
1545  
1546 sub listCollections {
1547 31 ▶ my ($self, $seqno_or_alias) = @_;
Subroutine listCollections: called CC=6 SCAR=18.0
Pod documented
1548  
1549 31 ▶ unless ( $self->{_iterator_listCollections} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_iterator_listCollections"}) 8 23 31
1550 8 my ($constraint, @cval) = SeeAlso::Source::BeaconAggregator::mkConstraint($seqno_or_alias);
1551 8 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1552 SELECT seqno, alias, uri, mtime, counti, countu FROM repos $constraint;
1553 XxX
1554 8 ▶ $self->stmtExplain($sthexpl, @cval) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 8 8
1555 8 ▶ $sth->execute(@cval) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@cval) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 8 8
1556 8 $self->{_iterator_listCollections} = $sth;
1557 };
1558 31 my $onerow = $self->{_iterator_listCollections}->fetchrow_arrayref;
1559 31 ▶ unless ( $onerow ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($onerow) 8 23 31
1560 8 ▶ croak("Error listing Collections: $self->{_iterator_listCollections}->errstr") if $self->{_iterator_listCollections}->err;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_iterator_listCollections"}->err 0 8 8
1561 8 delete $self->{_iterator_listCollections};
1562 8 return ();
1563 };
1564 23 return @$onerow;
1565 }
1566  
1567 =head2 Statistics
1568  
1569 =head3 idStat ( [ $seqno_or_alias, %options ] )
1570  
1571 Count identifiers for the given pattern.
1572  
1573 =over 8
1574  
1575 =item Supported options:
1576  
1577 distinct => (0|1)
1578  
1579 Count multiple occurences only once
1580  
1581 verbose => (0|1)
1582  
1583 =back
1584  
1585  
1586 =cut
1587  
1588 sub idStat {
1589 82 ▶ my ($self, $seqno_or_alias, %options) = @_;
Subroutine idStat: called CC=10 SCAR=23.5
Pod documented
1590 82 ▶ $options{'verbose'} = $self->{'verbose'} unless exists $options{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"verbose"} 80 2 82
1591 82 my $cond = "";
1592 82 ▶ if ( $seqno_or_alias && ($seqno_or_alias =~ /^\d+$/) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($seqno_or_alias and $seqno_or_alias =~ /^\d+$/) { } 26 56 82
elsif ($seqno_or_alias) { } 5 51 56
Condition100%3/3
$seqno_or_alias and $seqno_or_alias =~ /^\d+$/
!ll&&!rl&&r
51526
MC/DC100%2/2
$seqno_or_alias and $seqno_or_alias =~ /^\d+$/
$seqno_or_alias $seqno_or_alias =~ /^\d+$/
Truth table100%3/3
A and B
A= $seqno_or_aliasB= $seqno_or_alias =~ /^\d+$/
ABresult
0X0
100
111
1593 26 $cond = "WHERE seqno==$seqno_or_alias"}
1594 elsif ( $seqno_or_alias ) {
1595 5 my @seqnos = $self->Seqnos('_alias', $seqno_or_alias);
1596 5 ▶ if ( @seqnos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@seqnos) { } 5 0 5
1597 5 $cond = "WHERE seqno IN (".join(",", @seqnos).")"}
1598 else {
1599 0 carp("no Seqnos selected by $seqno_or_alias");
1600 0 return 0;
1601 };
1602 };
1603 # my $count_what = $options{'distinct'} ? "DISTINCT hash" : "*";
1604 # will not be optimized by SQLite or mySQL: SELECT COUNT($count_what) FROM beacons $cond;
1605 # my $sth= $self->stmtHdl("SELECT COUNT($count_what) FROM beacons $cond LIMIT 1;");
1606 82 ▶ my $from = $options{'distinct'} ? "(SELECT DISTINCT hash FROM beacons $cond)"
Branch: 1 branch
BranchTrueFalseTotal
$options{'distinct'} ? : 38 44 82
1607 : "beacons $cond";
1608 82 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1609 SELECT COUNT(*) FROM $from LIMIT 1;
1610 XxX
1611 82 ▶ $self->stmtExplain($sthexpl) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 82 82
1612 82 ▶ $sth->execute() or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 82 82
1613 82 my $hits = $sth->fetchrow_arrayref;
1614  
1615 82 ▶ return $hits->[0] || 0;
Condition50%1/2
$hits->[0] || 0
l!l
820
MC/DC0%0/1
$hits->[0] || 0
$hits->[0]
Truth table50%1/2
A
A= $hits->[0]
Aresult
11
00
1616 };
1617  
1618  
1619 =head3 idCounts ( [ $pattern, %options ] )
1620  
1621 Iterates through the entries according to the optional id filter expression.
1622  
1623 For each iteration the call returns a triple consisting of (identifier,
1624 number of rows, and sum of all individual counts).
1625  
1626 =over 8
1627  
1628 =item Supported options:
1629  
1630 distinct => (0|1)
1631  
1632 Count multiple occurences in one beacon file only once.
1633  
1634 =back
1635  
1636 =cut
1637  
1638 sub idCounts {
1639 76 ▶ my ($self, $pattern, %options) = @_;
Subroutine idCounts: called CC=14 SCAR=26.5
Pod documented
1640 76 ▶ my $cond = $pattern ? qq!WHERE hash LIKE "$pattern"! : "";
Branch: 1 branch
BranchTrueFalseTotal
$pattern ? : 4 72 76
1641 76 ▶ my $count_what = $options{'distinct'} ? "DISTINCT seqno" : "seqno";
Branch: 1 branch
BranchTrueFalseTotal
$options{'distinct'} ? : 40 36 76
1642 76 ▶ unless ( $self->{_iterator_idCounts} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_iterator_idCounts"}) 10 66 76
1643 10 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1644 SELECT hash, COUNT($count_what), SUM(hits) FROM beacons $cond GROUP BY hash ORDER BY hash;
1645 XxX
1646 10 ▶ $self->stmtExplain($sthexpl) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 10 10
1647 10 ▶ $sth->execute() or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 10 10
1648 10 $self->{_iterator_idCounts} = $sth;
1649 10 ▶ unless ( defined $self->{identifierClass} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"identifierClass"}) 4 6 10
1650 4 my $package = $self->autoIdentifier();
1651 4 ▶ $options{'verbose'} && ref($package) && carp "Assuming identifiers of type ".ref($package)."\n";
Branch: 1 branch
BranchTrueFalseTotal
$options{"verbose"} and ref $package and carp('Assuming identifiers of type ' . ref($package) . "\n") 0 4 4
Condition66%2/3
$options{"verbose"} and ref $package
!ll&&!rl&&r
220
MC/DC0%0/2
$options{"verbose"} and ref $package
$options{"verbose"} ref $package
Truth table66%2/3
A and B
A= $options{"verbose"}B= ref $package
ABresult
0X0
100
111
1652 }
1653 };
1654 76 my $onerow = $self->{_iterator_idCounts}->fetchrow_arrayref;
1655 76 ▶ unless ( $onerow ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($onerow) 10 66 76
1656 10 ▶ croak("Error listing Collections: $self->{_iterator_idCounts}->errstr") if $self->{_iterator_idCounts}->err;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_iterator_idCounts"}->err 0 10 10
1657 10 delete $self->{_iterator_idCounts};
1658 10 return ();
1659 };
1660 66 ▶ if ( defined $self->{identifierClass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"identifierClass"}) 42 24 66
1661 42 my $c = $self->{identifierClass};
1662 # compat: hash might not take an argument, must resort to value, has to be cleared before...
1663 42 $c->value("");
1664 42 ▶ my $did = $c->hash($onerow->[0]) || $c->value($onerow->[0]);
Condition100%3/3
$c->hash($onerow->[0]) || $c->value($onerow->[0])
l!l&&r!l&&!r
12282
MC/DC100%2/2
$c->hash($onerow->[0]) || $c->value($onerow->[0])
$c->hash($onerow->[0]) $c->value($onerow->[0])
Truth table100%3/3
A || B
A= $c->hash($onerow->[0])B= $c->value($onerow->[0])
ABresult
1X1
011
000
1665 42 ▶ $onerow->[0] = $c->can("pretty") ? $c->pretty() : $c->value();
Branch: 1 branch
BranchTrueFalseTotal
$c->can('pretty') ? : 14 28 42
1666 };
1667 66 return @$onerow;
1668 };
1669  
1670  
1671 =head3 idList ( [ $pattern ] )
1672  
1673 Iterates through the entries according to the optional selection.
1674  
1675 For each iteration the call returns a tuple consisting of identifier and an
1676 list of array references (Seqno, Hits, Info, explicit Link, AltId) or the emtpy list
1677 if finished.
1678  
1679 Hits, Info, Link and AltId are normalized to the empty string if undefined (or < 2 for hits).
1680  
1681 It is important to finish all iterations before calling this method for "new" arguments:
1682  
1683 1 while $db->idList(); # flush pending results
1684  
1685 =cut
1686  
1687 sub idList {
1688 62 ▶ my ($self, $pattern) = @_;
Subroutine idList: called CC=22 SCAR=31.2
Pod documented
1689 62 ▶ my $cond = $pattern ? ($pattern =~ /%/ ? "WHERE hash LIKE ?" : qq"WHERE hash=?")
Branch: 2 branches
BranchTrueFalseTotal
$pattern ? : 22 40 62
$pattern =~ /%/ ? : 22 0 22
1690 : "";
1691 62 ▶ unless ( $self->{_iterator_idList_handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_iterator_idList_handle"}) 11 51 62
1692 11 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1693 SELECT hash, seqno, hits, info, link, altid FROM beacons $cond ORDER BY hash, seqno, altid;
1694 XxX
1695 11 ▶ $self->stmtExplain($sthexpl, ($pattern ? ($pattern) : () )) if $ENV{'DBI_PROFILE'};
Branch: 2 branches
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 11 11
$pattern ? : 0 0 0
1696 11 ▶ $sth->execute(($pattern ? ($pattern) : () )) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 2 branches
BranchTrueFalseTotal
$sth->execute($pattern ? $pattern : ()) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 11 11
$pattern ? : 6 5 11
1697 11 $self->{_iterator_idList_handle} = $sth;
1698 11 $self->{_iterator_idList_crosscheck} = $self->RepoCols("ALTTARGET");
1699 11 $self->{_iterator_idList_prefetch} = undef;
1700 11 ▶ $self->autoIdentifier() unless defined $self->{identifierClass};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"identifierClass"} 5 6 11
1701 };
1702 62 ▶ unless ( exists $self->{_iterator_idList_prefetch} ) { # deferred exit
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_iterator_idList_prefetch"}) 11 51 62
1703 11 delete $self->{_iterator_idList_handle};
1704 11 delete $self->{_iterator_idList_crosscheck};
1705 11 return ();
1706 };
1707 51 my $pf = $self->{_iterator_idList_prefetch};
1708 51 ▶ while ( my $onerow = $self->{_iterator_idList_handle}->fetchrow_arrayref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $onerow = $self->{"_iterator_idList_handle"}->fetchrow_arrayref) 71 11 82
1709 # $onerow->[2] = "" unless $self->{_iterator_idList_crosscheck}->{$onerow->[1]}; # kill artefacts
1710 71 ▶ $onerow->[2] = "" unless $onerow->[2]; # kill artefacts
Branch: 1 branch
BranchTrueFalseTotal
unless $onerow->[2] 51 20 71
1711 71 ▶ $onerow->[3] = "" unless defined $onerow->[3]; # kill artefacts
Branch: 1 branch
BranchTrueFalseTotal
unless defined $onerow->[3] 18 53 71
1712 71 ▶ $onerow->[4] = "" unless defined $onerow->[4]; # kill artefacts
Branch: 1 branch
BranchTrueFalseTotal
unless defined $onerow->[4] 65 6 71
1713 71 ▶ $onerow->[5] = "" unless defined $onerow->[5]; # kill artefacts
Branch: 1 branch
BranchTrueFalseTotal
unless defined $onerow->[5] 0 71 71
1714 71 ▶ if ( defined $self->{identifierClass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"identifierClass"}) 36 35 71
1715 36 my $c = $self->{identifierClass};
1716 # compat: hash might not take an argument, must resort to value, has to be cleared before...
1717 36 $c->value("");
1718 36 ▶ my $did = $c->hash($onerow->[0]) || $c->value($onerow->[0]);
Condition100%3/3
$c->hash($onerow->[0]) || $c->value($onerow->[0])
l!l&&r!l&&!r
11241
MC/DC100%2/2
$c->hash($onerow->[0]) || $c->value($onerow->[0])
$c->hash($onerow->[0]) $c->value($onerow->[0])
Truth table100%3/3
A || B
A= $c->hash($onerow->[0])B= $c->value($onerow->[0])
ABresult
1X1
011
000
1719 36 ▶ $onerow->[0] = $c->can("pretty") ? $c->pretty() : $c->value();
Branch: 1 branch
BranchTrueFalseTotal
$c->can('pretty') ? : 12 24 36
1720 };
1721 71 ▶ if ( $pf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pf) { } 60 11 71
1722 60 ▶ if ( $pf->[0] eq $onerow->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pf->[0] eq $onerow->[0]) { } 20 40 60
1723 20 push(@$pf, [@$onerow[1..@$onerow-1]]);
1724 20 next;
1725 }
1726 else {
1727 40 $self->{_iterator_idList_prefetch} = [$onerow->[0], [@$onerow[1..@$onerow-1]]];
1728 40 return @$pf;
1729 }
1730 }
1731 else {
1732 11 $pf = [$onerow->[0], [@$onerow[1..@$onerow-1]]]};
1733 };
1734
1735 11 ▶ if ( $self->{_iterator_idList_handle}->err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_iterator_idList_handle"}->err) 0 11 11
1736 0 croak("Error listing Collections: $self->{_iterator_idList_handle}->errstr");
1737 };
1738 11 delete $self->{_iterator_idList_prefetch};
1739 11 ▶ return $pf ? @$pf : ();
Branch: 1 branch
BranchTrueFalseTotal
$pf ? : 11 0 11
1740 };
1741  
1742  
1743 =head2 Manipulation of global metadata: Open Search Description
1744  
1745 =head3 setOSD ( $field, @values }
1746  
1747 Sets the field $field of the OpenSearchDescription to @value(s).
1748  
1749 =cut
1750  
1751 sub setOSD {
1752 20 ▶ my ($self) = shift;
Subroutine setOSD: called CC=3 SCAR=11.0
Pod documented
1753 20 ▶ $self->clearOSD($_[0]) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->clearOSD($_[0]) or return undef 2 18 20
1754 18 ▶ return (defined $_[1]) ? $self->addOSD(@_) : 0; # value(s) to set
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 12 6 18
1755 };
1756  
1757 =head3 clearOSD ( $field }
1758  
1759 Clears the field $field of the OpenSearchDescription.
1760  
1761 =cut
1762  
1763 sub clearOSD {
1764 27 ▶ my ($self, $field) = @_;
Subroutine clearOSD: called CC=5 SCAR=16.2
Pod documented
1765 27 ▶ $field || (carp("no OSD field name provided"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
$field or carp("no OSD field name provided"), (return undef) 2 25 27
1766 25 ▶ defined $self->osdKeys($field) || (carp("no valid OSD field '$field'"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->osdKeys($field) or carp("no valid OSD field '${field}'"), (return undef) 2 23 25
1767 23 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1768 DELETE FROM osd WHERE key=?;
1769 XxX
1770 23 ▶ $self->stmtExplain($sthexpl, $field) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 23 23
1771 23 ▶ $sth->execute($field) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($field) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 23 23
1772 23 return 1;
1773 }
1774  
1775 =head3 addOSD ( $field, @values }
1776  
1777 Adds more @value(s) as (repeatable) field $field of the OpenSearchDescription.
1778  
1779 =cut
1780  
1781 sub addOSD {
1782 24 ▶ my ($self, $field, @values) = @_;
Subroutine addOSD: called CC=6 SCAR=18.0
Pod documented
1783 24 ▶ $field || (carp("no OSD field name provided"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
$field or carp("no OSD field name provided"), (return undef) 1 23 24
1784 23 ▶ return 0 unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1 22 23
1785 22 ▶ defined $self->osdKeys($field) || (carp("no valid OSD field '$field'"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->osdKeys($field) or carp("no valid OSD field '${field}'"), (return undef) 1 21 22
1786 21 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1787 INSERT INTO osd ( key, val ) VALUES ( ?, ? );
1788 XxX
1789 21 ▶ $self->stmtExplain($sthexpl, $field, $values[0]) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 21 21
1790 21 my $tstatus = [];
1791 21 ▶ my $tuples = $sth->execute_array({ArrayTupleStatus => $tstatus}, $field, \@values) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $tuples = $sth->execute_array({"ArrayTupleStatus", $tstatus}, $field, \@values) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 21 21
1792 21 return $tuples;
1793 }
1794  
1795 =head2 Manipulation of global metadata: Beacon Metadata
1796  
1797 These headers are used when you will be publishing a beacon file for the collection.
1798  
1799 =head3 setBeaconMeta ( $field, $value )
1800  
1801 Sets the field $field of the Beacon meta table (used to generate a BEACON file for this
1802 service) to $value.
1803  
1804 =cut
1805  
1806 sub setBeaconMeta {
1807 4 ▶ my ($self) = shift;
Subroutine setBeaconMeta: called CC=3 SCAR=11.0
Pod documented
1808 4 ▶ $self->clearBeaconMeta(@_) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->clearBeaconMeta(@_) or return undef 2 2 4
1809 2 ▶ return (defined $_[1]) ? $self->addBeaconMeta(@_) : 0; # value to set
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 1 1 2
1810 };
1811  
1812 =head3 clearBeaconMeta ( $field }
1813  
1814 Deletes the field $field of the Beacon meta table.
1815  
1816 =cut
1817  
1818 sub clearBeaconMeta {
1819 7 ▶ my ($self, $rfield) = @_;
Subroutine clearBeaconMeta: called CC=5 SCAR=16.2
Pod documented
1820 7 ▶ $rfield || (carp("no Beacon field name provided"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
$rfield or carp("no Beacon field name provided"), (return undef) 2 5 7
1821 5 ▶ my $field = $self->beaconfields($rfield) or (carp("no valid Beacon field '$rfield'"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->beaconfields($rfield) or carp("no valid Beacon field '${rfield}'"), (return undef) 2 3 5
1822 3 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1823 DELETE FROM osd WHERE key=?;
1824 XxX
1825 3 ▶ $self->stmtExplain($sthexpl, $field) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 3 3
1826 3 ▶ $sth->execute($field) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($field) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 3 3
1827 3 return 1;
1828 }
1829  
1830 =head3 addBeaconMeta ( $field, $value )
1831  
1832 Appends $value to the field $field of the BEACON meta table
1833  
1834 =cut
1835 sub addBeaconMeta {
1836 7 ▶ my ($self, $rfield, $value) = @_;
Subroutine addBeaconMeta: called CC=5 SCAR=16.2
Pod documented
1837 7 ▶ $rfield || (carp("no Beacon field name provided"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
$rfield or carp("no Beacon field name provided"), (return undef) 1 6 7
1838 6 ▶ my $field = $self->beaconfields($rfield) or (carp("no valid Beacon field '$rfield'"), return undef);
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->beaconfields($rfield) or carp("no valid Beacon field '${rfield}'"), (return undef) 1 5 6
1839 5 my ($sth, $sthexpl) = $self->stmtHdl(<<"XxX");
1840 INSERT INTO osd ( key, val ) VALUES ( ?, ? );
1841 XxX
1842 5 ▶ $self->stmtExplain($sthexpl, $field, $value) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 5 5
1843 5 ▶ $sth->execute($field, $value) or croak("Could not execute >".$sth->{Statement}."<: ".$sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($field, $value) or croak('Could not execute >' . $sth->{"Statement"} . '<: ' . $sth->errstr) 0 5 5
1844 5 return 1;
1845 }
1846  
1847 =head3 admin ( [$field, [$value]] )
1848  
1849 Manipulates the admin table.
1850  
1851 Yields a hashref to the admin table if called without arguments.
1852  
1853 If called with $field, returns the current value, and sets the
1854 table entry to $value if defined.
1855  
1856  
1857 =cut
1858  
1859 sub admin {
1860 54 ▶ my ($self, $field, $value) = @_;
Subroutine admin: called CC=6 SCAR=18.0
Pod documented
1861 54 my $admref = $self->admhash();
1862 54 ▶ return $admref unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 3 51 54
1863 51 my $retval = $admref->{$field};
1864 51 ▶ return $retval unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 11 40 51
1865  
1866 40 my ($admh, $admexpl) = $self->stmtHdl("INSERT OR REPLACE INTO admin VALUES (?, ?);");
1867 40 ▶ $self->stmtExplain($admexpl, $field, $value) if $ENV{'DBI_PROFILE'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBI_PROFILE"} 0 40 40
1868 40 ▶ $admh->execute($field, $value)
Branch: 1 branch
BranchTrueFalseTotal
$admh->execute($field, $value) or croak('Could not execute update to admin table: ' . $admh->errstr) 0 40 40
1869 or croak("Could not execute update to admin table: ".$admh->errstr);
1870 40 ▶ return defined($retval) ? $retval : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $retval ? : 37 3 40
1871 }
1872  
1873  
1874 # on-the-fly conversions
1875  
1876 sub hDecode { # external time to numeric timestamp, printf placeholders
1877 30 ▶ my ($val, $fnam) = @_;
Subroutine hDecode: called CC=9 SCAR=23.4
Pod undocumented
1878 30 ▶ return $val unless $fnam;
Branch: 1 branch
BranchTrueFalseTotal
unless $fnam 0 30 30
1879 30 ▶ local($_) = (ref $val) ? $val->{$fnam} : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 27 3 30
1880 30 ▶ return undef unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 30 30
1881  
1882 30 ▶ if ( $fnam =~ /target$/i ) { s/%/%%/g; s/(\{id\}|\$PND)/%1\$s/gi; s/(\{altid\}|\$PND)/%2\$s/gi; }
Branch: 3 branches
BranchTrueFalseTotal
if ($fnam =~ /target$/i) { } 25 5 30
elsif ($fnam =~ /message$/i) { } 0 5 5
elsif ($fnam =~ /time|revisit/i) { } 3 2 5
1883 0 elsif ( $fnam =~ /message$/i ) { s/%/%%/g; s/\{hits?\}/%s/gi; }
1884 elsif ( $fnam =~ /time|revisit/i ) {
1885 3 ▶ if ( /^\d+$/ ) { # legacy UNIX timestamp
Branch: 2 branches
BranchTrueFalseTotal
if (/^\d+$/) { } 1 2 3
elsif (my $p = &HTTP::Date::str2time($_, 'GMT')) { } 2 0 2
1886 }
1887 elsif ( my $p = HTTP::Date::str2time($_, "GMT") ) { # all unqualified times are GMT
1888 2 $_ = $p}
1889 else {
1890 0 carp("could not parse value '$_' as time in field $fnam");
1891 0 return undef;
1892 };
1893 }
1894 30 return $_;
1895 }
1896  
1897 sub hEncode { # timestamp to beacon format
1898 80 ▶ my ($val, $fnam) = @_;
Subroutine hEncode: called CC=6 SCAR=18.0
Pod undocumented
1899 80 ▶ local($_) = (ref $val) ? $val->{$fnam} : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 80 80
1900 80 ▶ return undef unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 80 80
1901 80 ▶ if ( $fnam =~ /time|revisit/i ) { $_ = SeeAlso::Source::BeaconAggregator::tToISO($_) }
Branch: 3 branches
BranchTrueFalseTotal
if ($fnam =~ /time|revisit/i) { } 16 64 80
elsif ($fnam =~ /message/i) { } 2 62 64
elsif ($fnam =~ /target/i) { } 6 56 62
1902 2 elsif ( $fnam =~ /message/i ) { s/%s/{hits}/; s/%%/%/g; }
1903 6 elsif ( $fnam =~ /target/i ) { s/%s/{ID}/; s/%1\$s/{ID}/; s/%2\$s/{ALTID}/; s/%%/%/g; };
1904 80 return $_;
1905 }
1906  
1907 =head1 AUTHOR
1908  
1909 Thomas Berger
1910 CPAN ID: THB
1911 gymel.com
1912 THB@cpan.org
1913  
1914 =head1 COPYRIGHT
1915  
1916 This program is free software; you can redistribute
1917 it and/or modify it under the same terms as Perl itself.
1918  
1919 The full text of the license can be found in the
1920 LICENSE file included with this module.
1921  
1922 =cut
1923  
1924 37 1;
1925  
« blib/lib/SeeAlso/Source/BeaconAggregator/Publisher.pm ↑ Summary blib/script/sasbactrl »