Indexer/Format/SQLite.pm

filter: Statementstmt 86.1% 112 / 130 Branchbran 46.1% 24 / 52 Conditioncond 35.1% 13 / 37 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 5 totaltotal 64.1% 168 / 262 CC 41 SCAR 27.3
CC
41
Cov
68%
load_index
32.4
_load_from_fh
17.4
create_index
15.0
CRAP 15.3
« Indexer/Storage/S3.pm ↑ Summary Indexer/Format.pm »
1 package DarkPAN::Indexer::Format::SQLite;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use CLI::Simple::Constants qw(:booleans);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use CLI::Simple::Utils qw(choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 with 'DarkPAN::Indexer::Format';
18  
19 ########################################################################
20 # Indexers must implement:
21 ########################################################################
22 # create_index
23 # delete_from_index
24 # load_index
25 # update_index
26 # new
27 #######################################################################
28  
29 ########################################################################
30 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
31 ########################################################################
32 5 my ( $class, $config ) = @_;
33  
34 5 ▶ $config //= {};
Condition50%1/2
$config //= {}
l!l
50
MC/DC0%0/1
$config //= {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
35  
36 5 return bless { config => $config }, $class;
37 }
38  
39 ########################################################################
40 4 ▶sub create_index {
Subroutine create_index: called CC=8 SCAR=27.1
Pod undocumented
41 ########################################################################
42 4 my ( $self, %args ) = @_;
43  
44 4 my ( $output, $distributions, $fetch ) = @args{qw(output distributions fetch)};
45  
46 4 ▶ croak "create_index requires a 'fetch' callback\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $fetch 0 4 4
47 if !$fetch;
48  
49 4 ▶ my $output_fh = choose {
Subroutine __ANON__: called CC=4 SCAR=25.0
50 4 ▶ return $output
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle($output) 4 0 4
51 if openhandle($output);
52  
53 0 ▶ return *STDOUT
Branch: 1 branch
BranchTrueFalseTotal
unless $output 0 0 0
54 if !$output;
55  
56 0 ▶ open my $fh, '>', $output
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $output or die sprintf("unable to open '%s' for writing: %s\n", $output, $DarkPAN::Indexer::Format::SQLite::OS_ERROR) 0 0 0
57 or die sprintf "unable to open '%s' for writing: %s\n", $output, $OS_ERROR,;
58  
59 0 return $fh;
60 4 };
61  
62 4 my %stats = (
63 distributions_seen => 0,
64 distributions_indexed => 0,
65 distributions_failed => 0,
66 modules_written => 0,
67 );
68  
69 4 ▶ foreach my $key ( @{ $distributions || [] } ) {
Condition50%1/2
$distributions || []
l!l
40
MC/DC0%0/1
$distributions || []
$distributions
Truth table50%1/2
A
A= $distributions
Aresult
11
00
70 6 $stats{distributions_seen}++;
71  
72 my $module_count
73 6 = eval { return $self->index_distribution( distribution => $key, output_fh => $output_fh, fetch => $fetch ); };
74  
75 6 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DarkPAN::Indexer::Format::SQLite::EVAL_ERROR) 0 6 6
76 0 my $error = $EVAL_ERROR;
77 0 chomp $error;
78  
79 0 warn sprintf "failed to index %s\n%s", $key, $error;
80  
81 0 $stats{distributions_failed}++;
82 0 next;
83 }
84  
85 6 $stats{distributions_indexed}++;
86 6 $stats{modules_written} += $module_count;
87 }
88  
89 4 ▶ if ( $output && !openhandle($output) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output and not &openhandle($output)) 0 4 4
Condition33%1/3
$output and not &openhandle($output)
!ll&&!rl&&r
040
MC/DC0%0/2
$output and not &openhandle($output)
$output not &openhandle($output)
Truth table33%1/3
A and not(B)
A= $outputB= not &openhandle($output)
ABresult
0X0
100
111
90 0 ▶ close $output_fh
Branch: 1 branch
BranchTrueFalseTotal
close $output_fh or die sprintf("unable to close '%s': %s\n", $output, $DarkPAN::Indexer::Format::SQLite::OS_ERROR) 0 0 0
91 or die sprintf "unable to close '%s': %s\n", $output, $OS_ERROR,;
92 }
93  
94 4 printf {*STDERR} "distributions seen: %d, indexed: %d, failed: %d, modules written: %d\n", @stats{
95 4 qw(
96 distributions_seen
97 distributions_indexed
98 distributions_failed
99 modules_written
100 )
101 };
102  
103 4 return \%stats;
104 }
105  
106 ########################################################################
107 0 ▶sub delete_from_index {
Subroutine delete_from_index: not called CC=1 SCAR=6.9
Pod undocumented
108 ########################################################################
109 0 my ( $self, %args ) = @_;
110  
111 0 $self->update_index( %args, delete_only => 1 );
112 0 return;
113 }
114  
115 ########################################################################
116 4 ▶sub load_index {
Subroutine load_index: called CC=16 SCAR=34.8
Pod undocumented
117 ########################################################################
118 4 my ( $self, %args ) = @_;
119  
120 4 my ( $input, $database, $create, $distribution ) = @args{qw(input database create distribution)};
121  
122 4 ▶ croak "input is required\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $distribution and !defined($input) || $input eq "" 0 4 4
Condition33%2/6
not $distribution and !defined($input) || $input eq ""
!ll&&!rl&&r
040
not defined $input or $input eq ""
l!l&&r!l&&!r
004
MC/DC0%0/3
not $distribution and !defined($input) || $input eq ""
not $distribution not defined $input $input eq ""
Truth table25%1/4
not(A) and not(B) or C
A= not $distributionB= not defined $inputC= $input eq ""
ABCresult
0XX0
1000
11X1
1011
123 if !$distribution && ( !defined $input || $input eq q{} );
124  
125 4 ▶ croak sprintf "input file '%s' does not exist\n", $input
Branch: 1 branch
BranchTrueFalseTotal
if $input and not -e $input 0 4 4
Condition33%1/3
$input and not -e $input
!ll&&!rl&&r
040
MC/DC0%0/2
$input and not -e $input
$input not -e $input
Truth table33%1/3
A and not(B)
A= $inputB= not -e $input
ABresult
0X0
100
111
126 if $input && !-e $input;
127  
128 4 ▶ if ($create) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create) 4 0 4
129 4 ▶ if ( -e $database ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $database) 4 0 4
130 4 my $timestamp = strftime '%Y%m%d-%H%M%S', localtime;
131 4 my $backup = sprintf '%s.%s.bak', $database, $timestamp;
132 4 my $sequence = 0;
133  
134 4 ▶ while ( -e $backup ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $backup) 0 4 4
135 0 $sequence++;
136 0 $backup = sprintf '%s.%s.%d.bak', $database, $timestamp, $sequence;
137 }
138  
139 4 ▶ rename $database, $backup
Branch: 1 branch
BranchTrueFalseTotal
rename $database, $backup or croak(sprintf("unable to back up '%s' to '%s': %s\n", $database, $backup, $DarkPAN::Indexer::Format::SQLite::OS_ERROR)) 4 0 4
140 or croak sprintf "unable to back up '%s' to '%s': %s\n", $database, $backup, $OS_ERROR;
141 }
142 }
143  
144 4 my $dbh = DBI->connect(
145 sprintf( 'dbi:SQLite:dbname=%s', $database ),
146 q{}, q{},
147 { AutoCommit => $FALSE,
148 RaiseError => $TRUE,
149 PrintError => $FALSE,
150 },
151 );
152  
153 4 ▶ if ($create) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create) 4 0 4
154 4 $dbh->do(
155 q{
156 CREATE TABLE modules (
157 distribution TEXT NOT NULL,
158 module TEXT NOT NULL,
159 version TEXT NOT NULL
160 )
161 }
162 );
163  
164 4 $dbh->do('CREATE INDEX modules_module_idx ON modules (module)');
165 4 $dbh->do('CREATE INDEX modules_distribution_idx ON modules (distribution)');
166 }
167  
168 4 ▶ croak "ERROR: no input or distribution\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $input or $distribution 0 4 4
Condition33%1/3
$input or $distribution
l!l&&r!l&&!r
400
MC/DC0%0/2
$input or $distribution
$input $distribution
Truth table33%1/3
A or B
A= $inputB= $distribution
ABresult
1X1
011
000
169 if !$input && !$distribution;
170  
171 4 my $row_count = eval {
172 4 ▶ if ($input) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input) { } 4 0 4
elsif ($distribution) { } 0 0 0
173 4 return $self->_load_from_file( $input, $dbh );
174 }
175 elsif ($distribution) {
176 0 return $self->update_index(
177 distribution => $distribution,
178 dbh => $dbh,
179 database => $database,
180 );
181 }
182 };
183  
184 4 my $err = $EVAL_ERROR;
185  
186 4 ▶ if ( !$err ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 4 0 4
187 4 $dbh->commit;
188 4 $dbh->disconnect;
189 4 return $row_count;
190 }
191  
192 0 $dbh->rollback;
193 0 $dbh->disconnect;
194  
195 0 croak $err;
196 }
197  
198 ########################################################################
199 4 ▶sub update_index {
Subroutine update_index: called CC=4 SCAR=13.9
Pod undocumented
200 ########################################################################
201 4 my ( $self, %args ) = @_;
202  
203 4 my ( $distribution, $database, $delete_only, $fetch ) = @args{qw(distribution database delete_only fetch)};
204  
205 4 my $dbh = DBI->connect( "dbi:SQLite:dbname=$database", q{}, q{}, { AutoCommit => 1, RaiseError => 1, PrintError => 0 } );
206  
207 4 $dbh->do( 'delete from modules where distribution = ?', undef, $distribution );
208 3 ▶ return if $delete_only;
Branch: 1 branch
BranchTrueFalseTotal
if $delete_only 1 2 3
209  
210 2 my $index = q{};
211  
212 2 ▶ open my $output_fh, '>', \$index
Branch: 1 branch
BranchTrueFalseTotal
open my $output_fh, ">", \$index or croak("ERROR: could open scalar \$index for writing\n$DarkPAN::Indexer::Format::SQLite::OS_ERROR") 2 0 2
213 or croak "ERROR: could open scalar \$index for writing\n$OS_ERROR";
214  
215 2 $self->index_distribution(
216 fetch => $fetch,
217 distribution => $distribution,
218 output_fh => $output_fh,
219 );
220  
221 1 close $output_fh;
222  
223 1 ▶ open my $input_fh, '<', \$index ## no critic
Branch: 1 branch
BranchTrueFalseTotal
open my $input_fh, "<", \$index or croak("ERROR: could not open scalar \$index for reading\n$DarkPAN::Indexer::Format::SQLite::OS_ERROR") 1 0 1
224 or croak "ERROR: could not open scalar \$index for reading\n$OS_ERROR";
225  
226 1 return $self->_load_from_fh( $input_fh, $dbh );
227 }
228  
229 ########################################################################
230 4 ▶sub _load_from_file {
Subroutine _load_from_file: called CC=2 SCAR=7.1
231 ########################################################################
232 4 my ( $self, $input, $dbh ) = @_;
233  
234 4 ▶ open my $input_fh, '<', $input ## no critic
Branch: 1 branch
BranchTrueFalseTotal
open my $input_fh, "<", $input or die sprintf("unable to open '%s' for reading: %s\n", $input, $DarkPAN::Indexer::Format::SQLite::OS_ERROR) 4 0 4
235 or die sprintf "unable to open '%s' for reading: %s\n", $input, $OS_ERROR;
236  
237 4 return $self->_load_from_fh( $input_fh, $dbh );
238 }
239  
240 ########################################################################
241 5 ▶sub _load_from_fh {
Subroutine _load_from_fh: called CC=11 SCAR=28.6
242 ########################################################################
243 5 my ( $self, $input_fh, $dbh ) = @_;
244  
245 5 my $insert_sth = $dbh->prepare(
246 q{
247 INSERT INTO modules (distribution, module, version)
248 VALUES (?, ?, ?)
249 }
250 );
251  
252 5 my $rows_loaded = 0;
253 5 my $line_number = 0;
254  
255 5 ▶ while ( my $line = <$input_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input_fh)) 7 5 12
256 7 $line_number++;
257 7 chomp $line;
258  
259 next
260 7 ▶ if $line eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 7 7
261  
262 7 my ( $distribution, $module, $version, @extra ) = split /\t/x, $line, -1;
263  
264 7 ▶ croak sprintf "invalid input at line %d: expected three tab-separated fields\n", $line_number
Branch: 1 branch
BranchTrueFalseTotal
if @extra or not defined $distribution or not defined $module or not defined $version or $distribution eq "" or $module eq "" or $version eq "" 0 7 7
Condition33%6/18
@extra or not defined $distribution or not defined $module or not defined $version or $distribution eq "" or $module eq "" or $version eq ""
l!l&&r!l&&!r
007
@extra or not defined $distribution or not defined $module or not defined $version or $distribution eq "" or $module eq ""
l!l&&r!l&&!r
007
@extra or not defined $distribution or not defined $module or not defined $version or $distribution eq ""
l!l&&r!l&&!r
007
@extra or not defined $distribution or not defined $module or not defined $version
l!l&&r!l&&!r
007
@extra or not defined $distribution or not defined $module
l!l&&r!l&&!r
007
@extra or not defined $distribution
l!l&&r!l&&!r
007
MC/DC0%0/7
@extra or not defined $distribution or not defined $module or not defined $version or $distribution eq "" or $module eq "" or $version eq ""
@extra not defined $distribution not defined $module not defined $version $distribution eq "" $module eq "" $version eq ""
Truth table12%1/8
A or not(B) or not(C) or not(D) or E or F or G
A= @extraB= not defined $distributionC= not defined $moduleD= not defined $versionE= $distribution eq ""F= $module eq ""G= $version eq ""
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
265 if @extra
266 || !defined $distribution
267 || !defined $module
268 || !defined $version
269 || $distribution eq q{}
270 || $module eq q{}
271 || $version eq q{};
272  
273 7 $insert_sth->execute( $distribution, $module, $version );
274 7 $rows_loaded++;
275 }
276  
277 5 ▶ close $input_fh
Branch: 1 branch
BranchTrueFalseTotal
close $input_fh or croak(sprintf("unable to close handle: %s\n", $DarkPAN::Indexer::Format::SQLite::OS_ERROR)) 5 0 5
278 or croak sprintf "unable to close handle: %s\n", $OS_ERROR;
279  
280 5 return $rows_loaded;
281 }
282  
283 5 1;
« Indexer/Storage/S3.pm ↑ Summary Indexer/Format.pm »