lib/UR/Util.pm

filter: Statementstmt 26.3% 86 / 326 Branchbran 16.3% 18 / 110 Conditioncond 10.0% 3 / 30 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 30.6% 15 / 49 Podpod 23.0% 6 / 26 totaltotal 23.1% 130 / 561 CC 81 SCAR 37.2
CC
81
Cov
23%
mapreduce_grep
210.0
class_suffix_for_operator
90.0
used_libs
72.0
CRAP 41.1
« lib/UR/Namespace.pm ↑ Summary blib/lib/UR/DataSource/RDBMS/Operator/In.pm »
1  
2 package UR::Util;
3  
4 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 265 require UR;
7 265 our $VERSION = "0.47"; # UR $VERSION;
8 266 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 266 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 266 ▶use Clone::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 266 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 266 ▶use Module::Runtime v0.014 qw(module_notional_filename);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub on_destroy(&) {
Subroutine on_destroy: not called CC=2 SCAR=17.9
Pod undocumented
15 0 my $sub = shift;
16 0 ▶ unless ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 0 0 0
17 0 Carp::confess("expected an anonymous sub!")
18 }
19 0 return bless($sub, "UR::Util::CallOnDestroy");
20 }
21  
22 # used only by the above sub
23 # the local $@ ensures that we this does not stomp on thrown exceptions
24 0 ▶sub UR::Util::CallOnDestroy::DESTROY { local $@; shift->(); }
Subroutine DESTROY: not called CC=1 SCAR=6.9
25  
26 0 ▶sub d {
Subroutine d: not called CC=1 SCAR=6.9
Pod undocumented
27 0 Data::Dumper->new([@_])->Terse(1)->Indent(0)->Useqq(1)->Dump;
28 }
29  
30 0 ▶sub null_sub { }
Subroutine null_sub: not called CC=1 SCAR=0.0
Pod undocumented
31  
32 0 ▶sub used_libs {
Subroutine used_libs: not called CC=8 SCAR=42.8
Pod undocumented
33 0 my @extra;
34 0 my @compiled_inc = UR::Util::compiled_inc();
35 0 my @perl5lib = split(':', $ENV{PERL5LIB});
36 0 map { $_ =~ s/\/+$// } (@compiled_inc, @perl5lib); # remove trailing slashes
37 0 ▶ map { $_ = Cwd::abs_path($_) || $_ } (@compiled_inc, @perl5lib);
Condition0%0/3
Cwd::abs_path($_) || $_
l!l&&r!l&&!r
000
MC/DC0%0/2
Cwd::abs_path($_) || $_
Cwd::abs_path($_) $_
Truth table0%0/3
A || B
A= Cwd::abs_path($_)B= $_
ABresult
1X1
011
000
38 0 for my $inc (@INC) {
39 0 $inc =~ s/\/+$//;
40 0 ▶ my $abs_inc = Cwd::abs_path($inc) || $inc; # should already be expanded by UR.pm
Condition0%0/3
Cwd::abs_path($inc) || $inc
l!l&&r!l&&!r
000
MC/DC0%0/2
Cwd::abs_path($inc) || $inc
Cwd::abs_path($inc) $inc
Truth table0%0/3
A || B
A= Cwd::abs_path($inc)B= $inc
ABresult
1X1
011
000
41 0 ▶ next if (grep { $_ =~ /^\Q$abs_inc\E$/ } @compiled_inc);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ =~ /^\Q$abs_inc\E$/;} @compiled_inc 0 0 0
42 0 ▶ next if (grep { $_ =~ /^\Q$abs_inc\E$/ } @perl5lib);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ =~ /^\Q$abs_inc\E$/;} @perl5lib 0 0 0
43 0 ▶ next if ((File::Spec->splitdir($inc))[-1] eq $Config{archname});
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->splitdir($inc))[-1] eq $UR::Util::Config{"archname"} 0 0 0
44 0 push @extra, $inc;
45 }
46  
47 0 ▶ unshift @extra, ($ENV{PERL_USED_ABOVE} ? split(":", $ENV{PERL_USED_ABOVE}) : ());
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PERL_USED_ABOVE'} ? : 0 0 0
48  
49 0 map { $_ =~ s/\/+$// } @extra; # remove trailing slashes again
50 0 @extra = _unique_elements(@extra);
51  
52 0 return @extra;
53 }
54  
55 0 ▶sub _unique_elements {
Subroutine _unique_elements: not called CC=1 SCAR=6.9
56 0 my @list = @_;
57 0 my %seen = ();
58 0 my @unique = grep { ! $seen{$_} ++ } @list;
59 0 return @unique;
60 }
61  
62 0 ▶sub used_libs_perl5lib_prefix {
Subroutine used_libs_perl5lib_prefix: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my $prefix = "";
64 0 for my $i (used_libs()) {
65 0 $prefix .= "$i:";
66 }
67 0 return $prefix;
68 }
69  
70 0 ▶sub touch_file {
Subroutine touch_file: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my $filename = shift;
72 0 open(my $fh, '>>', $filename);
73 }
74  
75 265 my @compiled_inc;
76 266 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=17.9
77 266 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 266 my @var_list = (
80 'updatesarch', 'updateslib',
81 'archlib', 'privlib',
82 'sitearch', 'sitelib', 'sitelib_stem',
83 'vendorarch', 'vendorlib', 'vendorlib_stem',
84 'extrasarch', 'extraslib',
85 );
86  
87 266 for my $var_name (@var_list) {
88 3192 ▶ if ($var_name =~ /_stem$/ && $Config{$var_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_name =~ /_stem$/ and $UR::Util::Config{$var_name}) { } 266 2926 3192
Condition100%3/3
$var_name =~ /_stem$/ and $UR::Util::Config{$var_name}
!ll&&!rl&&r
2660266266
MC/DC100%2/2
$var_name =~ /_stem$/ and $UR::Util::Config{$var_name}
$var_name =~ /_stem$/ $UR::Util::Config{$var_name}
Truth table100%3/3
A and B
A= $var_name =~ /_stem$/B= $UR::Util::Config{$var_name}
ABresult
0X0
100
111
89 266 my @stem_list = (split(' ', $Config{'inc_version_list'}), '');
90 266 push @compiled_inc, map { $Config{$var_name} . "/$_" } @stem_list
91 } else {
92 2926 ▶ push @compiled_inc, $Config{$var_name} if $Config{$var_name};
Branch: 1 branch
BranchTrueFalseTotal
if $UR::Util::Config{$var_name} 1064 1862 2926
93 }
94 }
95  
96 # UR locks in relative paths when loaded so instead of adding '.' we add cwd
97 266 ▶ push @compiled_inc, Cwd::cwd() if (${^TAINT} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ${^TAINT} == 0 266 0 266
98  
99 266 map { $_ =~ s/\/+/\//g } @compiled_inc;
100 266 map { $_ =~ s/\/+$// } @compiled_inc;
101 }
102 0 ▶sub compiled_inc {
Subroutine compiled_inc: not called CC=1 SCAR=6.9
Pod undocumented
103 0 return @compiled_inc;
104 }
105  
106 0 ▶sub deep_copy {
Subroutine deep_copy: not called CC=1 SCAR=6.9
Pod undocumented
107 0 return Clone::PP::clone($_[0]);
108 }
109  
110 0 ▶sub value_positions_map {
Subroutine value_positions_map: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my ($array) = @_;
112 0 my %value_pos;
113 0 ▶ for (my $pos = 0; $pos < @$array; $pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < @$array) 0 0 0
114 0 my $value = $array->[$pos];
115 0 ▶ if (exists $value_pos{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value_pos{$value}) 0 0 0
116 0 die "Array has duplicate values, which cannot unambiguously be given value positions!"
117 . Data::Dumper::Dumper($array);
118 }
119 0 $value_pos{$value} = $pos;
120 }
121 0 return \%value_pos;
122 }
123  
124 0 ▶sub positions_of_values {
Subroutine positions_of_values: not called CC=2 SCAR=17.9
Pod undocumented
125 # my @pos = positions_of_values(\@unordered_crap, \@correct_order);
126 # my @fixed = @unordered_crap[@pos];
127 0 my ($unordered_array,$ordered_array) = @_;
128 0 my $map = value_positions_map($unordered_array);
129 0 my @translated_positions;
130 0 $#translated_positions = $#$ordered_array;
131 0 ▶ for (my $pos = 0; $pos < @$ordered_array; $pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < @$ordered_array) 0 0 0
132 0 my $value = $ordered_array->[$pos];
133 0 my $unordered_position = $map->{$value};
134 0 $translated_positions[$pos] = $unordered_position;
135 }
136 # self-test:
137 # my @now_ordered = @$unordered_array[@translated_positions];
138 # unless ("@now_ordered" eq "@$ordered_array") {
139 # Carp::confess()
140 # }
141 0 return @translated_positions;
142 }
143  
144  
145 # Get all combinations of values
146 # input is a list of listrefs of values
147 0 ▶sub combinations_of_values {
Subroutine combinations_of_values: not called CC=6 SCAR=37.4
Pod undocumented
148 0 ▶ return [] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
149  
150 0 my $first_values = shift;
151  
152 0 ▶ $first_values = [ $first_values ] unless (ref($first_values) and ref($first_values) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $first_values and ref $first_values eq "ARRAY" 0 0 0
Condition0%0/3
ref $first_values and ref $first_values eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $first_values and ref $first_values eq "ARRAY"
ref $first_values ref $first_values eq "ARRAY"
Truth table0%0/3
A and B
A= ref $first_valuesB= ref $first_values eq "ARRAY"
ABresult
0X0
100
111
153  
154 0 my @retval;
155 0 foreach my $sub_combination ( &combinations_of_values(@_) ) {
156 0 foreach my $value ( @$first_values ) {
157 0 push @retval, [$value, @$sub_combination];
158 }
159 }
160  
161 0 return @retval;
162 }
163  
164 # generate a method
165 2128 ▶sub _define_method {
Subroutine _define_method: called CC=7 SCAR=28.2
166 2128 my $class = shift;
167 2128 my (%opts) = @_;
168  
169 # create method name
170 2128 my $method = $opts{pkg} . '::' . $opts{property};
171  
172 # determine return value type
173 2128 my $retval;
174 2128 ▶ if (defined($opts{value}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'value'}) { } 266 1862 2128
175 {
176 266 my $refval = ref($opts{value});
177 266 ▶ $retval = ($refval) ? $refval : 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
$refval ? : 266 0 266
178 }
179 else
180 {
181 1862 $retval = 'SCALAR';
182 }
183  
184 # start defining method
185 2128 my $substr = "sub $method { my \$self = shift; ";
186  
187 # set default value
188 2128 $substr .= "\$self->{$opts{property}} = ";
189 2128 my $dd = Data::Dumper->new([ $opts{value} ]);
190 2128 $dd->Terse(1); # do not print ``$VAR1 =''
191 2128 $substr .= $dd->Dump;
192 2128 $substr .= " unless defined(\$self->{$opts{property}}); ";
193  
194 # array or scalar?
195 2128 ▶ if ($retval eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval eq 'ARRAY') { } 266 1862 2128
196 266 ▶ if ($opts{access} eq 'rw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"access"} eq "rw") 0 266 266
197 # allow setting of array
198 0 $substr .= "\$self->{$opts{property}} = [ \@_ ] if (\@_); ";
199 }
200  
201 # add return value
202 266 $substr .= "return \@{ \$self->{$opts{property}} }; ";
203 }
204 else { # scalar
205 1862 ▶ if ($opts{access} eq 'rw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"access"} eq "rw") 0 1862 1862
206 # allow setting of scalar
207 0 $substr .= "\$self->{$opts{property}} = \$_[0] if (\@_); ";
208 }
209  
210 # add return value
211 1862 $substr .= "return \$self->{$opts{property}}; ";
212 }
213  
214 # end the subroutine definition
215 2128 $substr .= "}";
216  
217 # actually define the method
218 266 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
219 0 ▶ eval($substr);
Subroutine call_stack: not called CC=2 SCAR=17.4
Subroutine level: not called CC=2 SCAR=17.4
Subroutine owner_class: not called CC=2 SCAR=17.4
Subroutine owner_id: not called CC=2 SCAR=17.4
Subroutine package_name: not called CC=2 SCAR=17.4
Subroutine text: not called CC=2 SCAR=17.4
Subroutine time_stamp: not called CC=2 SCAR=17.4
Subroutine type: not called CC=2 SCAR=17.4
Branch: 8 branches
BranchTrueFalseTotal
unless defined $self->{"call_stack"} 0 0 0
unless defined $self->{"level"} 0 0 0
unless defined $self->{"owner_class"} 0 0 0
unless defined $self->{"owner_id"} 0 0 0
unless defined $self->{"package_name"} 0 0 0
unless defined $self->{"text"} 0 0 0
unless defined $self->{"time_stamp"} 0 0 0
unless defined $self->{"type"} 0 0 0
220 2128 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2128 2128
221 # fatal error since this is like a failed compilation
222 0 die("failed to defined method $method {$substr}:$@");
223 }
224 2128 return 1;
225 }
226  
227 =pod
228  
229 =over
230  
231 =item path_relative_to
232  
233 $rel_path = UR::Util::path_relative_to($base, $target);
234  
235 Returns the pathname to $target relative to $base. If $base
236 and $target are the same, then it returns '.'. If $target is
237 a subdirectory of of $base, then it returns the portion of $target
238 that is unique compared to $base. If $target is not a subdirectory
239 of $base, then it returns a relative pathname starting with $base.
240  
241 =back
242  
243 =cut
244  
245 0 ▶sub path_relative_to {
Subroutine path_relative_to: not called CC=4 SCAR=30.0
Pod documented
246 0 my($base,$target) = @_;
247  
248 0 $base = Cwd::abs_path($base);
249 0 $target = Cwd::abs_path($target);
250  
251 0 my @base_path_parts = split('/', $base);
252 0 my @target_path_parts = split('/', $target);
253 0 my $i;
254 0 ▶ for ($i = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @base_path_parts and $base_path_parts[$i] eq $target_path_parts[$i]) 0 0 0
Condition0%0/3
$i < @base_path_parts and $base_path_parts[$i] eq $target_path_parts[$i]
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @base_path_parts and $base_path_parts[$i] eq $target_path_parts[$i]
$i < @base_path_parts $base_path_parts[$i] eq $target_path_parts[$i]
Truth table0%0/3
A and B
A= $i < @base_path_partsB= $base_path_parts[$i] eq $target_path_parts[$i]
ABresult
0X0
100
111
255 $i < @base_path_parts and $base_path_parts[$i] eq $target_path_parts[$i];
256 $i++
257 ) { ; }
258  
259 0 my $rel_path = '../' x (scalar(@base_path_parts) - $i)
260 .
261 join('/', @target_path_parts[$i .. $#target_path_parts]);
262 0 ▶ $rel_path = '.' unless length($rel_path);
Branch: 1 branch
BranchTrueFalseTotal
unless length $rel_path 0 0 0
263 0 return $rel_path;
264 }
265
266 =pod
267  
268 =over
269  
270 =item generate_readwrite_methods
271  
272 UR::Util->generate_readwrite_methods
273 (
274 some_scalar_property => 1,
275 some_array_property => []
276 );
277  
278 This method generates accessor/set methods named after the keys of its
279 hash argument. The type of function generated depends on the default
280 value provided as the hash key value. If the hash key is a scalar, a
281 scalar method is generated. If the hash key is a reference to an
282 array, an array method is generated.
283  
284 This method does not overwrite class methods that already exist.
285  
286 =back
287  
288 =cut
289  
290 sub generate_readwrite_methods
291 0 ▶{
Subroutine generate_readwrite_methods: not called CC=3 SCAR=24.8
Pod documented
292 0 my $class = shift;
293 0 my %properties = @_;
294  
295 # get package of caller
296 0 my $pkg = caller;
297  
298 # loop through properties
299 0 foreach my $property (keys(%properties)) {
300 # do not overwrite defined methods
301 0 ▶ next if $pkg->can($property);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->can($property) 0 0 0
302  
303 # create method
304 $class->_define_method
305 (
306 pkg => $pkg,
307 property => $property,
308 0 value => $properties{$property},
309 access => 'rw'
310 );
311 }
312  
313 0 return 1;
314 }
315  
316 =pod
317  
318 =over
319  
320 =item generate_readwrite_methods_override
321  
322 UR::Util->generate_readwrite_methods_override
323 (
324 some_scalar_property => 1,
325 some_array_property => []
326 );
327  
328 Same as generate_readwrite_function except that we force the functions
329 into the namespace even if the function is already defined
330  
331 =back
332  
333 =cut
334  
335 sub generate_readwrite_methods_override
336 0 ▶{
Subroutine generate_readwrite_methods_override: not called CC=2 SCAR=17.9
Pod documented
337 0 my $class = shift;
338 0 my %properties = @_;
339  
340 # get package of caller
341 0 my $pkg = caller;
342  
343 # generate the methods for each property
344 0 foreach my $property (keys(%properties)) {
345 # create method
346 $class->_define_method
347 (
348 pkg => $pkg,
349 property => $property,
350 0 value => $properties{$property},
351 access => 'rw'
352 );
353 }
354  
355 0 return 1;
356 }
357  
358 =pod
359  
360 =over
361  
362 =item generate_readonly_methods
363  
364 UR::Util->generate_readonly_methods
365 (
366 some_scalar_property => 1,
367 some_array_property => []
368 );
369  
370 This method generates accessor methods named after the keys of its
371 hash argument. The type of function generated depends on the default
372 value provided as the hash key value. If the hash key is a scalar, a
373 scalar method is generated. If the hash key is a reference to an
374 array, an array method is generated.
375  
376 This method does not overwrite class methods that already exist.
377  
378 =back
379  
380 =cut
381  
382 sub generate_readonly_methods
383 266 ▶{
Subroutine generate_readonly_methods: called CC=3 SCAR=11.0
Pod documented
384 266 my $class = shift;
385 266 my %properties = @_;
386  
387 # get package of caller
388 266 my ($pkg) = caller;
389  
390 # loop through properties
391 266 foreach my $property (keys(%properties)) {
392 # do no overwrite already defined methods
393 2128 ▶ next if $pkg->can($property);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->can($property) 0 2128 2128
394  
395 # create method
396 $class->_define_method
397 (
398 pkg => $pkg,
399 property => $property,
400 2128 value => $properties{$property},
401 access => 'ro'
402 );
403 }
404  
405 266 return 1;
406 }
407  
408 =pod
409  
410 =over
411  
412 =item object
413  
414 my $o = UR::Util::object($something);
415  
416 Return the object form of the supplied argument. For regular objects, it
417 returns the argument unchanged. For singleton class names, it returns the
418 instance of the Singleton. For other class names, it throws an exception.
419  
420 =back
421  
422 =cut
423  
424 0 ▶sub object {
Subroutine object: not called CC=3 SCAR=24.8
Pod documented
425 0 my $it = shift;
426  
427 0 ▶ unless (ref $it) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $it) 0 0 0
428 0 ▶ if ($it->isa('UR::Singleton')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($it->isa('UR::Singleton')) { } 0 0 0
429 0 $it = $it->_singleton_object();
430 } else {
431 0 Carp::croak("Expected an object instance or Singleton class name, but got '$it'");
432 }
433 }
434 0 return $it;
435 }
436  
437 =pod
438  
439 =over
440  
441 =item mapreduce_grep
442  
443 my @matches = UR::Util->map_reduce_grep { shift->some_test } @candidates;
444  
445 Works similar to the Perl C<grep> builtin, but in a possibly-parallel fashion.
446 If the environment variable UR_NR_CPU is set to a number greater than one, it
447 will fork off child processes to perform the test on slices of the input
448 list, collect the results, and return the matching items as a list.
449  
450 The test function is called with a single argument, an item from the list to
451 be tested, and should return a true of false value.
452  
453 =back
454  
455 =cut
456  
457 0 ▶sub mapreduce_grep($&@) {
Subroutine mapreduce_grep: not called CC=14 SCAR=53.5
Pod documented
458 0 my $class = shift;
459 0 my $subref = shift;
460 #$DB::single = 1;
461  
462  
463 # First check fast... should we do parallel at all?
464 0 ▶ if (!$ENV{'UR_NR_CPU'} or $ENV{'UR_NR_CPU'} < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"UR_NR_CPU"} or $ENV{"UR_NR_CPU"} < 2) 0 0 0
Condition0%0/3
not $ENV{"UR_NR_CPU"} or $ENV{"UR_NR_CPU"} < 2
l!l&&r!l&&!r
000
MC/DC0%0/2
not $ENV{"UR_NR_CPU"} or $ENV{"UR_NR_CPU"} < 2
not $ENV{"UR_NR_CPU"} $ENV{"UR_NR_CPU"} < 2
Truth table0%0/3
not(A) or B
A= not $ENV{"UR_NR_CPU"}B= $ENV{"UR_NR_CPU"} < 2
ABresult
1X1
011
000
465 #return grep { $subref->($_) } @_;
466 0 my @ret = grep { $subref->($_) } @_;
467 0 return @ret;
468 }
469  
470 0 my(@read_handles, @child_pids);
471 0 ▶ my $cleanup = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
472 0 foreach my $handle ( @read_handles ) {
473 0 $handle->close();
474 }
475  
476 0 kill 'TERM', @child_pids;
477  
478 0 foreach my $pid ( @child_pids ) {
479 0 waitpid($pid,0);
480 }
481 0 };
482  
483 0 my @things_to_check = @_;
484 0 my($children, $length,$parent_last);
485 0 ▶ if ($ENV{'UR_NR_CPU'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'UR_NR_CPU'}) { } 0 0 0
486 0 $length = POSIX::ceil(scalar(@things_to_check) / $ENV{'UR_NR_CPU'});
487 0 $children = $ENV{'UR_NR_CPU'} - 1;
488 } else {
489 0 $children = 0;
490 0 $parent_last = $#things_to_check;
491 }
492  
493 # FIXME - There needs to be some code in here to disconnect datasources
494 # Oracle in particular (maybe all DBs?), stops working right unless you
495 # disconnect before forking
496  
497 0 my $start = $length; # First child starts checking after parent's range
498 0 $parent_last = $length - 1;
499 0 ▶ while ($children-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children-- > 0) 0 0 0
500 0 my $pipe = IO::Pipe->new();
501 0 ▶ unless ($pipe) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pipe) 0 0 0
502 0 Carp::carp("pipe() failed: $!\nUnable to create pipes to communicate with child processes to verify transact+ion, falling back to serial verification");
503 0 $cleanup->();
504 0 $parent_last = $#things_to_check;
505 0 last;
506 }
507  
508 0 my $pid = fork();
509 0 ▶ if ($pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid) { } 0 0 0
elsif (defined $pid) { } 0 0 0
510 0 $pipe->reader();
511 0 push @read_handles, $pipe;
512 0 $start += $length;
513  
514 } elsif (defined $pid) {
515 0 $pipe->writer();
516 0 my $last = $start + $length;
517 0 ▶ $last = $#things_to_check if ($last > $#things_to_check);
Branch: 1 branch
BranchTrueFalseTotal
if $last > $#things_to_check 0 0 0
518  
519 #my @objects = grep { $subref->($_) } @things_to_check[$start .. $last];
520 0 my @matching;
521 0 ▶ for (my $i = $start; $i <= $last; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $last) 0 0 0
522 0 ▶ if ($subref->($things_to_check[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$subref($things_to_check[$i])) 0 0 0
523 0 push @matching, $i;
524 }
525 }
526 # FIXME - when there's a more general framework for passing objects between
527 # processes, use that instead
528 #$pipe->printf("%s\n%s\n",$_->class, $_->id) foreach @objects;
529 0 $pipe->print("$_\n") foreach @matching;
530  
531  
532 0 exit;
533  
534 } else {
535 0 Carp::carp("fork() failed: $!\nUnable to create child processes to ver+ify transaction, falling back to seri+al verification");
536 0 $cleanup->();
537 0 $parent_last = $#things_to_check;
538 }
539 }
540 0 my @matches = grep { $subref->($_) } @things_to_check[0 .. $parent_last];
541  
542 0 foreach my $handle ( @read_handles ) {
543 READ_FROM_CHILD:
544 0 while(1) {
545 0 my $match_idx = $handle->getline();
546 0 ▶ last READ_FROM_CHILD unless $match_idx;
Branch: 1 branch
BranchTrueFalseTotal
unless $match_idx 0 0 0
547 0 chomp $match_idx;
548  
549 0 push @matches, $things_to_check[$match_idx];
550 #my $match_class = $handle->getline();
551 #last READ_FROM_CHILD unless $match_class;
552 #chomp($match_class);
553  
554 #my $match_id = $handle->getline();
555 #unless (defined $match_id) {
556 # Carp::carp("Protocol error. Tried to get object ID for class $match_class while verifying transaction"+);
557 # last READ_FROM_CHILD;
558 #}
559 #chomp($match_id);
560  
561 #push @objects, $match_class->get($match_id);
562 }
563 0 $handle->close();
564 }
565  
566 0 $cleanup->();
567  
568 0 return @matches;
569 }
570  
571  
572 # Used in several places when printing out hash-like parameters
573 # to the user, such as in error messages
574 0 ▶sub display_string_for_params_list {
Subroutine display_string_for_params_list: not called CC=4 SCAR=30.0
Pod undocumented
575 0 my $class = shift;
576  
577 0 my %params;
578 0 ▶ if (ref($_[0]) =~ 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($_[0]) =~ /HASH/) { } 0 0 0
579 0 %params = %{$_[0]};
580 } else {
581 0 %params = @_;
582 }
583  
584 0 my @strings;
585 0 foreach my $key ( keys %params ) {
586 0 my $val = $params{$key};
587 0 ▶ $val = defined($val) ? "'$val'" : '(undef)';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
588 0 push @strings, "$key => $val";
589 }
590 0 return join(', ', @strings);
591 }
592  
593 # why isn't something like this in List::Util?
594 # Return a list of 3 listrefs:
595 # 0: items common to both lists
596 # 1: items in the first list only
597 # 2: items in the second list only
598 0 ▶sub intersect_lists {
Subroutine intersect_lists: not called CC=3 SCAR=24.8
Pod undocumented
599 0 my ($m,$n) = @_;
600 0 my %shared;
601 my %monly;
602 0 my %nonly;
603 0 @monly{@$m} = @$m;
604 0 for my $v (@$n) {
605 0 ▶ if ($monly{$v}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($monly{$v}) { } 0 0 0
606 0 $shared{$v} = delete $monly{$v};
607 }
608 else{
609 0 $nonly{$v} = $v;
610 }
611 }
612 return (
613 0 [ values %shared ],
614 [ values %monly ],
615 [ values %nonly ],
616 );
617 }
618  
619 48408 ▶sub is_valid_property_name {
Subroutine is_valid_property_name: called CC=1 SCAR=0.0
Pod undocumented
620 48408 my $property_name = shift;
621 48408 return $property_name =~ m/^[_[:alpha:]][_[:alnum:]]*$/;
622 }
623  
624 0 ▶sub is_valid_class_name {
Subroutine is_valid_class_name: not called CC=1 SCAR=6.9
Pod undocumented
625 0 my $class = shift;
626 0 return $class =~ m/^[[:alpha:]]\w*((::|')\w+)*$/;
627 }
628  
629 {
630 265 my %subclass_suffix_for_builtin_symbolic_operator = (
631 '=' => "Equals",
632 '<' => "LessThan",
633 '>' => "GreaterThan",
634 '[]' => "In",
635 'in []' => "In",
636 'ne' => "NotEquals",
637 '<=' => 'LessOrEqual',
638 '>=' => 'GreaterOrEqual',
639 );
640 265 my %subclass_suffix_for_builtin_symbolic_operator_negation = (
641 '<' => 'GreaterOrEqual', # 'not less than' is the same as GreaterOrEqual
642 '<=' => 'GreaterThan',
643 '>' => 'LessOrEqual',
644 '>=' => 'LessThan',
645 'ne' => 'Equals',
646 'false' => 'True',
647 'true' => 'False',
648 );
649  
650 0 ▶ sub class_suffix_for_operator {
Subroutine class_suffix_for_operator: not called CC=9 SCAR=45.0
Pod undocumented
651 0 my $comparison_operator = shift;
652 0 my $not = 0;
653 0 ▶ if ($comparison_operator and $comparison_operator =~ m/^(\!|not)\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comparison_operator and $comparison_operator =~ /^(\!|not)\s*(.*)/) 0 0 0
Condition0%0/3
$comparison_operator and $comparison_operator =~ /^(\!|not)\s*(.*)/
!ll&&!rl&&r
000
MC/DC0%0/2
$comparison_operator and $comparison_operator =~ /^(\!|not)\s*(.*)/
$comparison_operator $comparison_operator =~ /^(\!|not)\s*(.*)/
Truth table0%0/3
A and B
A= $comparison_operatorB= $comparison_operator =~ /^(\!|not)\s*(.*)/
ABresult
0X0
100
111
654 0 $not = 1;
655 0 $comparison_operator = $2;
656 }
657  
658 0 ▶ if (!defined($comparison_operator) or $comparison_operator eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $comparison_operator or $comparison_operator eq "") 0 0 0
Condition0%0/3
not defined $comparison_operator or $comparison_operator eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $comparison_operator or $comparison_operator eq ""
not defined $comparison_operator $comparison_operator eq ""
Truth table0%0/3
not(A) or B
A= not defined $comparison_operatorB= $comparison_operator eq ""
ABresult
1X1
011
000
659 0 $comparison_operator = '=';
660 }
661  
662 0 my $suffix;
663 0 ▶ if ($not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not) { } 0 0 0
664 0 $suffix = $subclass_suffix_for_builtin_symbolic_operator_negation{$comparison_operator};
665 0 ▶ unless ($suffix) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suffix) 0 0 0
666 0 ▶ $suffix = $subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst(lc($comparison_operator));
Condition0%0/3
$subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst lc $comparison_operator
l!l&&r!l&&!r
000
MC/DC0%0/2
$subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst lc $comparison_operator
$subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} ucfirst lc $comparison_operator
Truth table0%0/3
A || B
A= $subclass_suffix_for_builtin_symbolic_operator{$comparison_operator}B= ucfirst lc $comparison_operator
ABresult
1X1
011
000
667 0 $suffix = "Not$suffix";
668 }
669 } else {
670 0 ▶ $suffix = $subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst(lc($comparison_operator));
Condition0%0/3
$subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst lc $comparison_operator
l!l&&r!l&&!r
000
MC/DC0%0/2
$subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst lc $comparison_operator
$subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} ucfirst lc $comparison_operator
Truth table0%0/3
A || B
A= $subclass_suffix_for_builtin_symbolic_operator{$comparison_operator}B= ucfirst lc $comparison_operator
ABresult
1X1
011
000
671 }
672 0 return $suffix;
673 }
674 }
675  
676 # From DBI::quote()
677 # needed in a few places where we need to quote some SQL but don't
678 # have access to a database handle to call quote() on
679 265 ▶sub sql_quote {
Subroutine sql_quote: not called CC=2 SCAR=15.1
Pod undocumented
680 0 my $str = shift;
681 0 ▶ return "NULL" unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
682 0 $str =~ s/'/''/g; # ISO SQL2
683 0 return "'$str'";
684 }
685  
686 # Module::Runtime's use_package_optimistically will not throw an exception if
687 # the package cannot be found or if it fails to compile but will if the package
688 # has upstream exceptions, e.g. a missing dependency. We're a little less
689 # "optimistic" so we check if the package is in %INC so we can report whether
690 # it was believed to be loaded or not.
691 0 ▶sub use_package_optimistically {
Subroutine use_package_optimistically: not called CC=1 SCAR=6.9
Pod undocumented
692 0 my $name = Module::Runtime::use_package_optimistically(shift);
693 0 my $file = module_notional_filename($name);
694 0 return $INC{$file};
695 }
696  
697 # return a hashref of subroutine names => coderefs
698 0 ▶sub coderefs_for_package {
Subroutine coderefs_for_package: not called CC=3 SCAR=20.8
Pod undocumented
699 0 my $package = shift;
700  
701 0 my %stash = do {
702 266 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
703 0 my $stash_name = $package . '::';
704 0 %$stash_name;
705 };
706  
707 0 my %subs;
708 0 local $@;
709 0 foreach my $name ( keys %stash ) {
710 0 my $glob = $stash{$name};
711 0 ▶ next unless my $coderef = eval { *$glob{CODE} }; # constants are SCALAR refs, not typeglobs
Branch: 1 branch
BranchTrueFalseTotal
unless my $coderef = eval { do { *$glob{"CODE"} } } 0 0 0
712 0 $subs{$name} = $coderef;
713 }
714 0 return \%subs;
715 }
716  
717 # Given a key in a hashref, if the value is a scalar, wrap it in an arrayref
718 # used by the class initializer to allow some keys in a class definition to
719 # be specified as simple scalars that are normalized to be an arrayref.
720 # Returns false if the value isn't an arrayref or scalar.
721 23932 ▶sub ensure_arrayref {
Subroutine ensure_arrayref: called CC=4 SCAR=15.5
Pod undocumented
722 23932 my($new_class, $key) = @_;
723  
724 23932 ▶ if ($new_class->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_class->{$key}) { } 266 23666 23932
725 266 ▶ if (!ref($new_class->{$key})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $new_class->{$key}) { } 0 266 266
elsif (ref $new_class->{$key} ne 'ARRAY') { } 0 266 266
726 # If it's a plain string, wrap it into an arrayref
727 0 $new_class->{$key} = [ $new_class->{$key} ];
728 } elsif (ref($new_class->{$key}) ne 'ARRAY') {
729 0 my $class_name = $new_class->{class_name};
730 0 return 0;
731 }
732 } else {
733 23666 $new_class->{$key} = [];
734 }
735 23932 return 1;
736 }
737  
738  
739  
740 265 1;
741  
742 =pod
743  
744 =head1 NAME
745  
746 UR::Util - Collection of utility subroutines and methods
747  
748 =head1 DESCRIPTION
749  
750 This package contains subroutines and methods used by other parts of the
751 infrastructure. These subs are not likely to be useful to outside code.
752  
753 =cut
754  
« lib/UR/Namespace.pm ↑ Summary blib/lib/UR/DataSource/RDBMS/Operator/In.pm »