Metadata.pm

filter: Statementstmt 80.4% 103 / 128 Branchbran 44.2% 23 / 52 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 68.5% 146 / 213 CC 36 SCAR 27.5
CC
36
Cov
69%
add_single_metadata
28.6
new
14.7
add_multi_metadata
6.1
CRAP 15.6
« Metadata/Query/default.pm ↑ Summary Metadata/Query.pm »
1 package Tapper::Metadata;
2 # git description: v5.0.0-1-g7c8654f
3  
4 2 our $AUTHORITY = 'cpan:TAPPER';
5 2 $Tapper::Metadata::VERSION = '5.0.1';
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my $hr_default_config = {
11 debug => 0,
12 max_redo_count => 25,
13 select_cache => 1,
14 tables => {
15 additional_type_table => {
16 name => 'bench_additional_types',
17 primary => 'bench_additional_type_id',
18 },
19 additional_value_table => {
20 name => 'bench_additional_values',
21 primary => 'bench_additional_value_id',
22 foreign_key => {
23 additional_type_table => 'bench_additional_type_id',
24 },
25 },
26 },
27 };
28  
29 1 ▶sub new {
Subroutine new: called CC=10 SCAR=26.9
Pod documented
30  
31 1 my ( $s_self, $hr_atts ) = @_;
32  
33 1 my $or_self = bless {}, $s_self;
34  
35 1 for my $s_key (qw/ dbh /) {
36 1 ▶ if (! $hr_atts->{$s_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hr_atts->{$s_key}) 0 1 1
37 0 require Carp;
38 0 Carp::confess("missing '$s_key' parameter");
39 0 return;
40 }
41 }
42  
43 # get tapper benchmark configuration
44 1 $or_self->{config} = { %{$hr_default_config} };
45  
46 1 ▶ if ( $or_self->can('get_default_config') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_self->can("get_default_config")) 1 0 1
47 $or_self->{config} =
48 Hash::Merge
49 ->new('LEFT_PRECEDENT')
50 ->merge(
51 $or_self->get_default_config(),
52 $or_self->{config},
53 )
54 1 ;
55 }
56 1 ▶ if ( $hr_atts->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_atts->{"config"}) 0 1 1
57 $or_self->{config} =
58 Hash::Merge
59 ->new('LEFT_PRECEDENT')
60 ->merge(
61 $hr_atts->{config},
62 $or_self->{config},
63 )
64 0 ;
65 }
66  
67 1 require CHI;
68 1 ▶ if ( $or_self->{config}{select_cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_self->{"config"}{"select_cache"}) 1 0 1
69 1 $or_self->{cache} = CHI->new( driver => 'RawMemory', global => 1 );
70 }
71  
72 1 my $s_module = "Tapper::Metadata::Query::$hr_atts->{dbh}{Driver}{Name}";
73  
74 1 my $fn_new_sub;
75 1 eval {
76 1 require Module::Load;
77 1 Module::Load::load( $s_module );
78 1 $fn_new_sub = $s_module->can('new');
79 };
80  
81 1 ▶ if ( $@ || !$fn_new_sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $fn_new_sub) { } 0 1 1
Condition33%1/3
$@ or not $fn_new_sub
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or not $fn_new_sub
$@ not $fn_new_sub
Truth table33%1/3
A or not(B)
A= $@B= not $fn_new_sub
ABresult
1X1
011
000
82 0 require Carp;
83 0 Carp::confess("database engine '$hr_atts->{dbh}{Driver}{Name}' not supported $@");
84 0 return;
85 }
86 else {
87 1 require DateTime;
88 $or_self->{query} = $s_module->new({
89 now => $hr_atts->{now} || DateTime->now->strftime('%F %T'),
90 dbh => $hr_atts->{dbh},
91 cache => $or_self->{cache},
92 debug => $hr_atts->{debug} || 0,
93 config => $or_self->{config},
94 1 ▶ });
Condition40%2/5
$hr_atts->{'now'} || 'DateTime'->now->strftime('%F %T')
l!l&&r!l&&!r
010
$hr_atts->{'debug'} || 0
l!l
01
MC/DC0%0/2
$hr_atts->{'now'} || 'DateTime'->now->strftime('%F %T')
$hr_atts->{'now'} 'DateTime'->now->strftime('%F %T')
MC/DC0%0/1
$hr_atts->{'debug'} || 0
$hr_atts->{'debug'}
Truth table33%1/3
A || B
A= $hr_atts->{'now'}B= 'DateTime'->now->strftime('%F %T')
ABresult
1X1
011
000
Truth table50%1/2
A
A= $hr_atts->{'debug'}
Aresult
11
00
95 }
96  
97 1 return $or_self;
98  
99 }
100  
101 5 ▶sub add_single_metadata {
Subroutine add_single_metadata: called CC=16 SCAR=33.5
Pod documented
102  
103 5 my ( $or_self, $hr_data, $hr_options ) = @_;
104  
105 5 my $i_redo_count = 0;
106 TRANSACTION: {
107  
108 5 my $b_transaction_started = $or_self->{query}->start_transaction();
109  
110 5 eval {
111  
112 5 ▶ if (! $hr_data->{TESTRUN} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hr_data->{"TESTRUN"}) 0 5 5
113 0 die "Testrun ID not found";
114 }
115  
116 # check for already existing metadata-set
117 5 my $ar_exists;
118 5 eval {
119 $ar_exists = $or_self->search_array({
120 select => [ 'TESTRUN', ],
121 exclusive => 1,
122 where => [
123 map {
124 {
125 operator => '=',
126 column => $_,
127 14 values => $hr_data->{$_},
128 },
129 5 } keys %{$hr_data}
130 ],
131 });
132 };
133  
134 5 ▶ if ( !$ar_exists || @{$ar_exists} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ar_exists or @{$ar_exists;} < 1) 5 0 5
Condition66%2/3
not $ar_exists or @{$ar_exists;} < 1
l!l&&r!l&&!r
140
MC/DC0%0/2
not $ar_exists or @{$ar_exists;} < 1
not $ar_exists @{$ar_exists;} < 1
Truth table66%2/3
not(A) or B
A= not $ar_existsB= @{$ar_exists;} < 1
ABresult
1X1
011
000
135  
136 # add metadata header
137 my $i_header_id = $or_self->{query}->insert_metadata_header(
138 delete $hr_data->{TESTRUN}
139 5 );
140 5 ▶ if (! $i_header_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i_header_id) 0 5 5
141 0 die "cannot insert metadata header";
142 }
143  
144 # WARNING: Don't add all three entries inside the same loop. This
145 # leads to deadlocks. Add all entries for one table,
146 # after that - go to the next table.
147  
148 # add metadata lines
149 5 my %h_deadlock_free_inserts;
150  
151 # additional type
152 5 for my $s_add_type ( keys %{$hr_data} ) {
153  
154 9 my $i_add_type_id = $or_self->{query}->select_addtype_by_name( $s_add_type );
155 9 ▶ if (! $i_add_type_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i_add_type_id) 2 7 9
156 2 $i_add_type_id = $or_self->{query}->insert_addtype( $s_add_type );
157 }
158 9 ▶ if (! $i_add_type_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i_add_type_id) 0 9 9
159 0 die "cannot find or insert bench additional type id for '$s_add_type'";
160 }
161  
162 9 $h_deadlock_free_inserts{$i_add_type_id} = $hr_data->{$s_add_type};
163  
164 }
165  
166 # additional value
167 5 for my $i_add_type ( keys %h_deadlock_free_inserts ) {
168  
169 9 my $i_add_value_id = $or_self->{query}->select_addvalue_id( $i_add_type, $h_deadlock_free_inserts{$i_add_type} );
170 9 ▶ if (! $i_add_value_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i_add_value_id) 9 0 9
171 9 $i_add_value_id = $or_self->{query}->insert_addvalue( $i_add_type, $h_deadlock_free_inserts{$i_add_type}, );
172 }
173 9 ▶ if (! $i_add_value_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i_add_value_id) 0 9 9
174 0 die "cannot find or insert bench additional value id for '$i_add_type', '$h_deadlock_free_inserts{$i_add_type}'";
175 }
176  
177 9 $h_deadlock_free_inserts{$i_add_type} = $i_add_value_id;
178  
179 }
180  
181 # additional metadata line
182 5 for my $i_add_value_id ( values %h_deadlock_free_inserts ) {
183 $or_self->{query}->insert_metadata_line(
184 9 $i_header_id, $i_add_value_id,
185 );
186 }
187  
188 }
189  
190 };
191  
192 5 my $b_success = $or_self->{query}->finish_transaction( $b_transaction_started, $@ );
193  
194 5 ▶ if (! $b_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $b_success) { } 0 5 5
195 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
196 0 ▶ if ( $@ =~ /try restarting transaction/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /try restarting transaction/) 0 0 0
197 0 ▶ if ( ++$i_redo_count <= $or_self->{config}{max_redo_count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i_redo_count <= $or_self->{"config"}{"max_redo_count"}) 0 0 0
198 0 redo TRANSACTION;
199 }
200 }
201 0 print STDERR $@;
202 0 return $@;
203 }
204 else {
205 0 print STDERR 'unknown error occured';
206 0 return 'unknown error occured';
207 }
208 }
209 else {
210 5 return;
211 }
212  
213 } # TRANSACTION
214  
215 }
216  
217 1 ▶sub add_multi_metadata {
Subroutine add_multi_metadata: called CC=5 SCAR=18.1
Pod documented
218  
219 1 my ( $or_self, $ar_data, $hr_options ) = @_;
220  
221 1 my ( $i_counter, @a_error ) = ( 0 );
222 1 for my $hr_data ( @{$ar_data} ) {
223 4 ▶ if ( my $s_error = $or_self->add_single_metadata( $hr_data, $hr_options ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s_error = $or_self->add_single_metadata($hr_data, $hr_options)) 0 4 4
224 0 push @a_error, {
225 'index' => $i_counter,
226 'error' => $s_error,
227 };
228 }
229 4 $i_counter++;
230 }
231  
232 1 ▶ return ( wantarray ? @a_error : ( @a_error ? 0 : 1 ));
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 1 0 1
@a_error ? : 0 0 0
233  
234 }
235  
236 8 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
237  
238 8 my ( $or_self, $hr_search ) = @_;
239  
240 return $or_self->{query}->select_benchmark_values(
241 8 $hr_search
242 );
243  
244 }
245  
246 6 ▶sub search_array {
Subroutine search_array: called CC=4 SCAR=14.2
Pod documented
247  
248 6 my ( $or_self, $hr_search ) = @_;
249  
250 6 my $s_key;
251 6 ▶ if ( $or_self->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_self->{"cache"}) 6 0 6
252 6 require JSON::XS;
253 6 $s_key = JSON::XS::encode_json($hr_search);
254 6 ▶ if ( my $ar_search_data = $or_self->{cache}->get("search_array||$s_key") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ar_search_data = $or_self->{"cache"}->get("search_array||$s_key")) 0 6 6
255 0 return $ar_search_data;
256 }
257 }
258  
259 6 my ( $or_prepared, undef ) = $or_self->search( $hr_search );
260 5 my $ar_result = $or_prepared->fetchall_arrayref({});
261 5 $or_prepared->finish();
262  
263 5 ▶ if ( $or_self->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_self->{"cache"}) 5 0 5
264 5 $or_self->{cache}->set( "search_array||$s_key" => $ar_result );
265 }
266  
267 5 return $ar_result;
268  
269 }
270  
271 1 ▶sub search_hash {
Subroutine search_hash: called CC=5 SCAR=17.6
Pod documented
272  
273 1 my ( $or_self, $hr_search ) = @_;
274  
275 1 my $s_key;
276 1 ▶ if ( $or_self->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_self->{"cache"}) 1 0 1
277 1 require JSON::XS;
278 1 $s_key = JSON::XS::encode_json($hr_search);
279 1 ▶ if ( my $hr_search_data = $or_self->{cache}->get( "search_hash||$s_key" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hr_search_data = $or_self->{"cache"}->get("search_hash||$s_key")) 0 1 1
280 0 return $hr_search_data;
281 }
282 }
283  
284 1 ▶ if (! $hr_search->{keys} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hr_search->{"keys"}) 0 1 1
285 0 require Carp;
286 0 Carp::confess(q#cannot get hash search result without 'keys'#);
287 0 return;
288 }
289  
290 1 my ( $or_prepared, undef ) = $or_self->search( $hr_search );
291 1 my $hr_result = $or_prepared->fetchall_hashref($hr_search->{keys});
292 1 $or_prepared->finish();
293  
294 1 ▶ if ( $or_self->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($or_self->{"cache"}) 1 0 1
295 1 $or_self->{cache}->set( "search_hash||$s_key" => $hr_result )
296 }
297  
298 1 return $hr_result;
299  
300 }
301  
302 2 1;
303  
304 __END__
« Metadata/Query/default.pm ↑ Summary Metadata/Query.pm »