lib/UR/Object/Index.pm

filter: Statementstmt 11.7% 36 / 307 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 45.8% 11 / 24 Podpod 16.6% 1 / 6 totaltotal 7.4% 48 / 643 CC 141 SCAR 87.6
CC
141
Cov
6%
get_objects_matching
10169.6
_build_data_tree
72.3
__ANON__
56.0
CRAP 6396.5
↑ Summary blib/lib/UR/Namespace/Command/Update/ClassesFromDb.pm »
1 # Index for cached objects.
2  
3 package UR::Object::Index;
4 266 our $VERSION = "0.47"; # UR $VERSION;
5 266 ▶use base qw(UR::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 266 require UR;
10  
11 266 ▶use List::MoreUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # wrapper for one of the ID properties to make it less ugly
14  
15 sub indexed_property_names
16 0 ▶{
Subroutine indexed_property_names: not called CC=2 SCAR=12.2
Pod undocumented
17 0 my $self = shift;
18 0 ▶ unless (exists $self->{indexed_property_names}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"indexed_property_names"}) 0 0 0
19 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 0 $self->{indexed_property_names} = [ split(/,/,$self->{indexed_property_string}) ];
21 }
22 0 return @{$self->{indexed_property_names}};
23 }
24  
25 0 ▶sub indexed_property_numericness {
Subroutine indexed_property_numericness: not called CC=3 SCAR=24.8
Pod undocumented
26 0 my $self = shift;
27 0 ▶ unless (exists $self->{indexed_property_numericness}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"indexed_property_numericness"}) 0 0 0
28 0 my $class_meta = $self->indexed_class_name->__meta__;
29 my @is_numeric = map {
30 0 my @props = $class_meta->_concrete_property_meta_for_class_and_name($_);
31 0 ▶ @props == 1
Branch: 1 branch
BranchTrueFalseTotal
@props == 1 ? : 0 0 0
32 ? $props[0]->is_numeric
33 : 0 # multiple ID properties are treated as a string
34 }
35 $self->indexed_property_names;
36  
37 0 $self->{indexed_property_numericness} = \@is_numeric;
38 }
39 0 return @{ $self->{indexed_property_numericness} };
40 }
41  
42  
43 # the only non-id property has an accessor...
44  
45 sub data_tree
46 0 ▶{
Subroutine data_tree: not called CC=3 SCAR=24.8
Pod undocumented
47 0 ▶ if (@_ > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
48 {
49 0 my $old = $_[0]->{data_tree};
50 0 my $new = $_[1];
51 0 ▶ if ($old ne $new)
Branch: 1 branch
BranchTrueFalseTotal
if ($old ne $new) 0 0 0
52 {
53 0 $_[0]->{data_tree} = $new;
54 0 $_[0]->__signal_change__('data_tree', $old, $new);
55 }
56 0 return $new;
57 }
58 0 return $_[0]->{data_tree};
59 }
60  
61 # override create to initilize the index
62  
63 0 ▶sub create {
Subroutine create: not called CC=3 SCAR=24.8
Pod documented
64 0 my $class = shift;
65
66 # NOTE: This is called from one location in UR::Context and relies
67 # on all properties including the ID being specifically defined.
68
69 0 my $self = $UR::Context::current->_construct_object($class, @_);
70 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
71 0 ▶ $self->{data_tree} ||= {};
Condition0%0/2
$self->{"data_tree"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"data_tree"} ||= {}
$self->{"data_tree"}
Truth table0%0/2
A
A= $self->{"data_tree"}
Aresult
11
00
72
73 0 $self->_build_data_tree;
74 0 $self->_setup_change_subscription;
75
76 0 $self->__signal_change__("create");
77 0 return $self;
78 }
79  
80 # this does a lookup as efficiently as possible
81  
82 sub get_objects_matching
83 0 ▶{
Subroutine get_objects_matching: not called CC=103 SCAR=92.3
Pod undocumented
84 0 my $self = shift;
85 0 my @values = @_;
86  
87 # The hash access below generates warnings
88 # where undef is a value. Ignore these.
89 266 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 0 my @hr = ($self->{data_tree});
92 0 my @is_numeric = $self->indexed_property_numericness;
93  
94 0 my $iter = List::MoreUtils::each_array(@values, @is_numeric);
95 0 ▶ while(my($value, $is_numeric) = $iter->())
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 0 0 0
96 {
97 0 my $value_ref = ref($value);
98 0 ▶ if($value_ref eq "HASH")
Branch: 3 branches
BranchTrueFalseTotal
if ($value_ref eq 'HASH') { } 0 0 0
elsif (not $value_ref) { } 0 0 0
elsif ($value_ref eq 'ARRAY') { } 0 0 0
99 {
100 # property => { operator => "not like", value => "H~_WGS%", escape "~" }
101 0 ▶ if (my $op = $value->{operator})
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = $value->{'operator'}) { } 0 0 0
102 {
103 0 $op = lc($op);
104 0 my $not = 0;
105 0 ▶ if ($op =~ m/^(!|not\s*)(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /^(!|not\s*)(.*)/) 0 0 0
106 0 $not = 1;
107 0 $op = $2;
108 }
109  
110 0 my $result;
111  
112 0 ▶ if ($op eq '=' and !$not) {
Branch: 14 branches
BranchTrueFalseTotal
if ($op eq '=' and not $not) { } 0 0 0
elsif ($op eq 'like') { } 0 0 0
elsif ($op eq 'in' and not $not) { } 0 0 0
elsif ($op eq 'in' and $not) { } 0 0 0
elsif ($op eq 'isa') { } 0 0 0
elsif ($op eq 'true' or $op eq 'false') { } 0 0 0
elsif ($not and $op eq '=' || !$op) { } 0 0 0
elsif ($op eq '>') { } 0 0 0
elsif ($op eq '<') { } 0 0 0
elsif ($op eq '>=') { } 0 0 0
elsif ($op eq '<=') { } 0 0 0
elsif ($op eq 'ne') { } 0 0 0
elsif ($op eq '<>') { } 0 0 0
elsif ($op eq 'between') { } 0 0 0
Condition0%0/3
$op eq "=" and not $not
!ll&&!rl&&r
000
MC/DC0%0/2
$op eq "=" and not $not
$op eq "=" not $not
Truth table0%0/3
A and not(B)
A= $op eq "="B= not $not
ABresult
0X0
100
111
113 0 @hr = grep { $_ } map { $_->{$value->{'value'}} } @hr;
114 }
115 elsif ($op eq 'like')
116 {
117 0 my $comparison_value = $value->{value};
118 0 my $escape = $value->{escape};
119
120 0 my $regex =
121 UR::BoolExpr::Template::PropertyComparison::Like->
122 comparison_value_and_escape_character_to_regex(
123 $comparison_value,
124 $escape
125 );
126
127 0 my @thr;
128 0 ▶ if ($not)
Branch: 1 branch
BranchTrueFalseTotal
if ($not) { } 0 0 0
129 {
130 # Get the values using the regular or negative match op.
131 0 foreach my $h (@hr) {
132 0 foreach my $k (sort keys %$h) {
133 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
134 0 ▶ if($k !~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ /$regex/) 0 0 0
135 0 push @thr, $h->{$k};
136 }
137 }
138 }
139 }
140 else
141 {
142 # Standard positive match
143 0 for my $h (@hr) {
144 0 for my $k (sort keys %$h) {
145 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
146 0 ▶ if ($k =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /$regex/) 0 0 0
147 0 push @thr, $h->{$k};
148 }
149 }
150 }
151 }
152 0 @hr = grep { $_ } @thr;
153 }
154 ▶ elsif ($op eq 'in' and !$not)
Condition0%0/3
$op eq "in" and not $not
!ll&&!rl&&r
000
MC/DC0%0/2
$op eq "in" and not $not
$op eq "in" not $not
Truth table0%0/3
A and not(B)
A= $op eq "in"B= not $not
ABresult
0X0
100
111
155 {
156 0 $value = $value->{value};
157 0 ▶ my $has_null = ( (grep { length($_) == 0 } @$value) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
grep({length $_ == 0;} @$value) ? : 0 0 0
158 0 ▶ if ($has_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_null) { } 0 0 0
159 0 @hr = grep { $_ } map { @$_{@$value} } @hr;
160 } else {
161 0 my @value = grep { length($_) > 0 } @$value;
162 0 @hr = grep { $_ } map { @$_{@value} } @hr;
163 }
164 }
165 ▶ elsif ($op eq 'in' and $not)
Condition0%0/3
$op eq "in" and $not
!ll&&!rl&&r
000
MC/DC0%0/2
$op eq "in" and $not
$op eq "in" $not
Truth table0%0/3
A and B
A= $op eq "in"B= $not
ABresult
0X0
100
111
166 {
167 0 $value = $value->{value};
168
169 # make a hash if we got an array as a value
170 #die ">@$value<" if ref($value) eq "ARRAY";
171 0 ▶ $value = { map { $_ => 1 } @$value } if ref($value) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 0 0
172
173 # if there is a single null, the not in clause will be false
174 0 ▶ if ($value->{""}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{''}) { } 0 0 0
175 0 @hr = ();
176 }
177 else {
178 # return everything NOT in the hash
179 0 my @thr;
180 0 for my $h (@hr) {
181 0 for my $k (sort keys %$h) {
182 0 ▶ next unless length($k);
Branch: 1 branch
BranchTrueFalseTotal
unless length $k 0 0 0
183 0 ▶ unless ($value->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->{$k}) 0 0 0
184 0 push @thr, $h->{$k};
185 }
186 }
187 }
188 0 @hr = grep { $_ } @thr;
189 }
190  
191 } elsif ($op eq 'isa') {
192 0 my @thr;
193 0 foreach my $h ( @hr ) {
194 0 foreach my $k ( keys %$h) {
195 0 ▶ if ($k->isa($value->{value}) xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->isa($value->{"value"}) xor $not) 0 0 0
Condition0%0/4
$k->isa($value->{"value"}) xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$k->isa($value->{"value"}) xor $not
$k->isa($value->{"value"}) $not
Truth table0%0/4
A xor B
A= $k->isa($value->{"value"})B= $not
ABresult
110
101
011
000
196 0 push @thr, $h->{$k};
197 }
198 }
199 }
200 0 @hr = grep { $_ } @thr;
201  
202 ▶ } elsif ($op eq 'true' or $op eq 'false') {
Condition0%0/3
$op eq "true" or $op eq "false"
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "true" or $op eq "false"
$op eq "true" $op eq "false"
Truth table0%0/3
A or B
A= $op eq "true"B= $op eq "false"
ABresult
1X1
011
000
203 0 ▶ $not = (( $op eq 'true' && $not) or ($op eq 'false' && !$not));
Condition0%0/9
$op eq 'true' && $not || $op eq 'false' && !$not
l!l&&r!l&&!r
000
$op eq "true" and $not
!ll&&!rl&&r
000
$op eq "false" and not $not
!ll&&!rl&&r
000
MC/DC0%0/4
$op eq 'true' && $not || $op eq 'false' && !$not
$op eq "true" $not $op eq "false" not $not
Truth table0%0/7
A and B || C and not(D)
A= $op eq "true"B= $notC= $op eq "false"D= not $not
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
204 0 my @thr;
205 0 foreach my $h ( @hr ) {
206 0 foreach my $k ( keys %$h ) {
207 0 ▶ if ($k xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k xor $not) 0 0 0
Condition0%0/4
$k xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$k xor $not
$k $not
Truth table0%0/4
A xor B
A= $kB= $not
ABresult
110
101
011
000
208 0 push @thr, $h->{$k};
209 }
210 }
211 }
212 0 @hr = grep { $_ } @thr;
213  
214 ▶ } elsif ($not and ($op eq '=' or !$op)) {
Condition0%0/6
$not and $op eq "=" || !$op
!ll&&!rl&&r
000
$op eq "=" or not $op
l!l&&r!l&&!r
000
MC/DC0%0/3
$not and $op eq "=" || !$op
$not $op eq "=" not $op
Truth table0%0/4
A and B or not(C)
A= $notB= $op eq "="C= not $op
ABCresult
0XX0
1000
11X1
1011
215 0 my @thr;
216 0 foreach my $h (@hr) {
217 0 foreach my $k (sort keys %$h) {
218 # An empty string for $k means the object's value was loaded as NULL
219 # and we want things like 0 != NULL to be true to match the SQL that
220 # gets generated for the same rule
221 my $t = ($k eq '')
222 ||
223 ($is_numeric
224 ? $k != $value->{value}
225 0 ▶ : $k ne $value->{value});
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
Condition0%0/3
$k eq '' || ($is_numeric ? $k != $value->{'value'} : $k ne $value->{'value'})
l!l&&r!l&&!r
000
MC/DC0%0/2
$k eq '' || ($is_numeric ? $k != $value->{'value'} : $k ne $value->{'value'})
$k eq '' ($is_numeric ? $k != $value->{'value'} : $k ne $value->{'value'})
Truth table0%0/3
A || B
A= $k eq ''B= ($is_numeric ? $k != $value->{'value'} : $k ne $value->{'value'})
ABresult
1X1
011
000
226 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 0 0
227 0 push @thr, $h->{$k};
228 }
229 }
230 }
231 0 @hr = grep { $_ } @thr;
232 } elsif($op eq '>') {
233 0 my @thr;
234 0 foreach my $h (@hr) {
235 0 foreach my $k (keys %$h) {
236 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
237 my $t = $is_numeric
238 ? $k > $value->{value}
239 0 ▶ : $k gt $value->{value};
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
240 0 ▶ if ($t xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t xor $not) 0 0 0
Condition0%0/4
$t xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$t xor $not
$t $not
Truth table0%0/4
A xor B
A= $tB= $not
ABresult
110
101
011
000
241 0 push @thr, $h->{$k};
242 }
243 }
244 }
245 0 @hr = grep { $_ } @thr;
246 } elsif($op eq '<') {
247 0 my @thr;
248 0 foreach my $h (@hr) {
249 0 foreach my $k (keys %$h) {
250 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
251 my $t = $is_numeric
252 ? $k < $value->{value}
253 0 ▶ : $k lt $value->{value};
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
254 0 ▶ if ($t xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t xor $not) 0 0 0
Condition0%0/4
$t xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$t xor $not
$t $not
Truth table0%0/4
A xor B
A= $tB= $not
ABresult
110
101
011
000
255 0 push @thr, $h->{$k};
256 }
257 }
258 }
259 0 @hr = grep { $_ } @thr;
260 } elsif($op eq '>=') {
261 0 my @thr;
262 0 foreach my $h (@hr) {
263 0 foreach my $k (keys %$h) {
264 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
265 my $t = $is_numeric
266 ? $k >= $value->{value}
267 0 ▶ : $k ge $value->{value};
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
268 0 ▶ if ($t xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t xor $not) 0 0 0
Condition0%0/4
$t xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$t xor $not
$t $not
Truth table0%0/4
A xor B
A= $tB= $not
ABresult
110
101
011
000
269 0 push @thr, $h->{$k};
270 }
271 }
272 }
273 0 @hr = grep { $_ } @thr;
274 } elsif($op eq '<=') {
275 0 my @thr;
276 0 foreach my $h (@hr) {
277 0 foreach my $k (keys %$h) {
278 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
279 my $t = $is_numeric
280 ? $k <= $value->{value}
281 0 ▶ : $k le $value->{value};
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
282 0 ▶ if ($t xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t xor $not) 0 0 0
Condition0%0/4
$t xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$t xor $not
$t $not
Truth table0%0/4
A xor B
A= $tB= $not
ABresult
110
101
011
000
283 0 push @thr, $h->{$k};
284 }
285 }
286 }
287 0 @hr = grep { $_ } @thr;
288 } elsif($op eq 'ne') {
289 0 my @thr;
290 0 foreach my $h (@hr) {
291 0 foreach my $k (sort keys %$h) {
292 0 ▶ next if $k eq ''; # an earlier undef value got saved as an empty string here
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
293 0 ▶ if($k ne $value->{value} xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k ne $value->{"value"} xor $not) 0 0 0
Condition0%0/4
$k ne $value->{"value"} xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$k ne $value->{"value"} xor $not
$k ne $value->{"value"} $not
Truth table0%0/4
A xor B
A= $k ne $value->{"value"}B= $not
ABresult
110
101
011
000
294 0 push @thr, $h->{$k};
295 }
296 }
297 }
298 0 @hr = grep { $_ } @thr;
299 } elsif($op eq '<>') {
300 0 my @thr;
301 0 foreach my $h (@hr) {
302 0 foreach my $k (sort keys %$h) {
303 0 ▶ if((length($k) and length($value->{value}) and $k ne $value->{value}) xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $k and length $value->{"value"} and $k ne $value->{"value"} xor $not) 0 0 0
Condition0%0/10
length $k and length $value->{"value"} and $k ne $value->{"value"} xor $not
l&&rl&&!r!l&&r!l&&!r
0000
length $k && length $value->{'value'} && $k ne $value->{'value'}
!ll&&!rl&&r
000
length $k and length $value->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/4
length $k and length $value->{"value"} and $k ne $value->{"value"} xor $not
length $k length $value->{"value"} $k ne $value->{'value'} $not
Truth table0%0/8
A and B && C xor D
A= length $kB= length $value->{"value"}C= $k ne $value->{'value'}D= $not
ABCDresult
11110
11101
0XX11
10X11
11011
0XX00
10X00
11000
304 0 push @thr, $h->{$k};
305 }
306 }
307 }
308 0 @hr = grep { $_ } @thr;
309 } elsif($op eq 'between') {
310 0 my @thr;
311 0 my ($min,$max) = @{ $value->{value} };
312 0 foreach my $h (@hr) {
313 0 foreach my $k (sort keys %$h) {
314 0 ▶ next if $k eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 0 0 0
315 0 ▶ my $t = $is_numeric
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
Condition0%0/6
$k >= $min and $k <= $max
!ll&&!rl&&r
000
$k ge $min and $k le $max
!ll&&!rl&&r
000
MC/DC0%0/2
$k >= $min and $k <= $max
$k >= $min $k <= $max
MC/DC0%0/2
$k ge $min and $k le $max
$k ge $min $k le $max
Truth table0%0/3
A and B
A= $k >= $minB= $k <= $max
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $k ge $minB= $k le $max
ABresult
0X0
100
111
316 ? ( $k >= $min and $k <= $max )
317 : ( $k ge $min and $k le $max );
318 0 ▶ if ($t xor $not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t xor $not) 0 0 0
Condition0%0/4
$t xor $not
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$t xor $not
$t $not
Truth table0%0/4
A xor B
A= $tB= $not
ABresult
110
101
011
000
319 0 push @thr, $h->{$k};
320 }
321 }
322 }
323 0 @hr = grep { $_ } @thr;
324 } else {
325 266 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
326 0 Carp::confess("Unknown operator in key-value pair used in index lookup for index " . Dumper($value));
327 }
328 }
329 else
330 {
331 0 Carp::confess("No operator specified in hashref value!" . Dumper($value));
332 }
333 }
334 elsif (not $value_ref)
335 {
336 # property => value
337 0 @hr = grep { $_ } map { $_->{$value} } @hr;
338 }
339 elsif ($value_ref eq "ARRAY")
340 {
341 # property => [ v1, v2, v3]
342 0 @hr = grep { $_ } map { @$_{@$value} } @hr;
343 }
344 }
345 0 return (map { values(%$_) } @hr);
346 }
347  
348  
349 # private methods
350  
351 sub _build_data_tree
352 0 ▶{
Subroutine _build_data_tree: not called CC=9 SCAR=42.8
353 0 my $self = $_[0];
354
355 0 my @indexed_property_names = $self->indexed_property_names;
356 0 my $hr_base = $self->{data_tree};
357
358 # _remove_object in bulk.
359 0 %$hr_base = ();
360 0 my $indexed_class_name = $self->indexed_class_name;
361
362 0 ▶ if (my @bad_properties =
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_properties) = grep({not $indexed_class_name->can($_);} @indexed_property_names)) 0 0 0
363 0 grep { not $indexed_class_name->can($_) }
364 @indexed_property_names
365 ) {
366 0 Carp::confess(
367 "Attempt to index $indexed_class_name by properties which "
368 . "do not function: @bad_properties"
369 );
370 }
371
372 # _add_object in bulk.
373 0 for my $object ($UR::Context::current->all_objects_loaded($indexed_class_name)) {
374 0 my(@values, $hr);
375 0 ▶ if (@indexed_property_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@indexed_property_names) 0 0 0
376 0 ▶ @values = map { my $val = $object->$_; defined $val ? $val : undef } @indexed_property_names;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
377 0 ▶ @values = (undef) unless(@values);
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 0 0
378 }
379 0 $hr = $hr_base;
380 0 for my $value (@values)
381 {
382 266 ▶ no warnings 'uninitialized'; # in case $value is undef
Subroutine BEGIN: called CC=1 SCAR=0.0
383 0 ▶ $hr->{$value} ||= {};
Condition0%0/2
$hr->{$value} ||= {}
l!l
00
MC/DC0%0/1
$hr->{$value} ||= {}
$hr->{$value}
Truth table0%0/2
A
A= $hr->{$value}
Aresult
11
00
384 0 $hr = $hr->{$value};
385 }
386 0 my $obj_id = $object->id;
387 0 $hr->{$obj_id} = $object;
388 0 ▶ if (Scalar::Util::isweak($UR::Context::all_objects_loaded->{$indexed_class_name}->{$obj_id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::is_weak($UR::Context::all_objects_loaded->{$indexed_class_name}{$obj_id})) 0 0 0
389 0 Scalar::Util::weaken($hr->{$obj_id});
390 }
391 }
392 }
393  
394 # FIXME maybe objects in an index should always be weakend?
395 0 ▶sub weaken_reference_for_object {
Subroutine weaken_reference_for_object: not called CC=5 SCAR=30.5
Pod undocumented
396 0 my $self = shift;
397 0 my $object = shift;
398 0 my $overrides = shift; # FIXME copied from _remove_object - what's this for?
399  
400 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
401 0 my @indexed_property_names = $self->indexed_property_names;
402 my @values =
403 map
404 {
405 0 ($overrides && exists($overrides->{$_}))
406 ?
407 0 ▶ $overrides->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$overrides && exists $overrides->{$_} ? : 0 0 0
Condition0%0/3
$overrides and exists $overrides->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$overrides and exists $overrides->{$_}
$overrides exists $overrides->{$_}
Truth table0%0/3
A and B
A= $overridesB= exists $overrides->{$_}
ABresult
0X0
100
111
408 :
409 $object->$_
410 }
411 @indexed_property_names;
412  
413 0 my $hr = $self->{data_tree};
414 0 my $value;
415 0 for $value (@values)
416 {
417 0 $hr = $hr->{$value};
418 0 ▶ return unless $hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hr 0 0 0
419 }
420 0 Scalar::Util::weaken($hr->{$object->id});
421 }
422
423
424 sub _setup_change_subscription
425 0 ▶{
Subroutine _setup_change_subscription: not called CC=4 SCAR=30.0
426
427 0 my $self = shift;
428
429
430 0 my $indexed_class_name = $self->indexed_class_name;
431 0 my @indexed_property_names = $self->indexed_property_names;
432
433 0 if (1) {
434 # This is a new indexing strategy which pays at index creation time instead of use.
435
436 0 my @properties_to_watch = (@indexed_property_names, qw/create delete load unload/);
437 #print "making index $self->{id}\n";
438 0 for my $class ($indexed_class_name, @{ $UR::Object::Type::_init_subclasses_loaded{$indexed_class_name} }) {
439 0 for my $property (@properties_to_watch) {
440 0 ▶ my $index_list = $UR::Object::Index::all_by_class_name_and_property_name{$class}{$property} ||= [];
Condition0%0/2
$UR::Object::Index::all_by_class_name_and_property_name{$class}{$property} ||= []
l!l
00
MC/DC0%0/1
$UR::Object::Index::all_by_class_name_and_property_name{$class}{$property} ||= []
$UR::Object::Index::all_by_class_name_and_property_name{$class}{$property}
Truth table0%0/2
A
A= $UR::Object::Index::all_by_class_name_and_property_name{$class}{$property}
Aresult
11
00
441 #print " adding to $class\n";
442 0 push @$index_list, $self;
443 }
444 }
445
446 0 return 1;
447 }
448
449 # This will be ignored for now.
450 # If the __signal_change__/subscription system is improved, it may be better to go back?
451
452 0 my %properties_to_watch = map { $_ => 1 } (@indexed_property_names, qw/create delete load unload/);
453
454 $self->{_get_change_subscription} = $indexed_class_name->create_subscription(
455 callback =>
456 sub
457 0 ▶ {
Subroutine __ANON__: not called CC=7 SCAR=40.3
458 0 my ($changed_object, $changed_property, $old_value, $new_value) = @_;
459
460 #print "got change $changed_property for $indexed_class_name: $changed_object->{id}: @_\n";
461
462 # ensure we don't track changes for subclasses
463 #return() unless ref($changed_object) eq $indexed_class_name;
464
465 # ensure we only add/remove for selected method calls
466 0 ▶ return() unless $properties_to_watch{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless $properties_to_watch{$_[1]} 0 0 0
467
468 #print "changing @_\n";
469
470 0 ▶ $self->_remove_object(
Branch: 1 branch
BranchTrueFalseTotal
if $changed_property ne "create" and $changed_property ne "load" and $changed_property ne "__define__" 0 0 0
Condition0%0/6
$changed_property ne "create" and $changed_property ne "load" and $changed_property ne "__define__"
!ll&&!rl&&r
000
$changed_property ne "create" and $changed_property ne "load"
!ll&&!rl&&r
000
MC/DC0%0/3
$changed_property ne "create" and $changed_property ne "load" and $changed_property ne "__define__"
$changed_property ne "create" $changed_property ne "load" $changed_property ne "__define__"
Truth table0%0/4
A and B and C
A= $changed_property ne "create"B= $changed_property ne "load"C= $changed_property ne "__define__"
ABCresult
0XX0
10X0
1100
1111
471 $changed_object,
472 { $changed_property => $old_value }
473 ) if ($changed_property ne 'create'
474 and $changed_property ne 'load'
475 and $changed_property ne '__define__');
476
477 0 ▶ $self->_add_object($changed_object) if ($changed_property ne 'delete' and $changed_property ne 'unload');
Branch: 1 branch
BranchTrueFalseTotal
if $changed_property ne "delete" and $changed_property ne "unload" 0 0 0
Condition0%0/3
$changed_property ne "delete" and $changed_property ne "unload"
!ll&&!rl&&r
000
MC/DC0%0/2
$changed_property ne "delete" and $changed_property ne "unload"
$changed_property ne "delete" $changed_property ne "unload"
Truth table0%0/3
A and B
A= $changed_property ne "delete"B= $changed_property ne "unload"
ABresult
0X0
100
111
478 },
479 0 note => "index monitor " . $self->id,
480 priority => 0,
481 );
482 }
483  
484 sub _get_change_subscription
485 0 ▶{
Subroutine _get_change_subscription: not called CC=2 SCAR=17.9
486 # accessor for the change subscription
487 0 ▶ $_[0]->{_get_change_subscription} = $_[1] if (@_ > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
488 0 return $_[0]->{_get_change_subscription};
489 }
490  
491 sub _remove_object($$)
492 0 ▶{
Subroutine _remove_object: not called CC=4 SCAR=25.6
493 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
494
495 0 my ($self, $object, $overrides) = @_;
496 0 my @indexed_property_names = $self->indexed_property_names;
497 my @values =
498 map
499 {
500 0 ($overrides && exists($overrides->{$_}))
501 ?
502 0 ▶ $overrides->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$overrides && exists $overrides->{$_} ? : 0 0 0
Condition0%0/3
$overrides and exists $overrides->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$overrides and exists $overrides->{$_}
$overrides exists $overrides->{$_}
Truth table0%0/3
A and B
A= $overridesB= exists $overrides->{$_}
ABresult
0X0
100
111
503 :
504 $object->$_
505 }
506 @indexed_property_names;
507
508 0 my $hr = $self->{data_tree};
509 0 my $value;
510 0 for $value (@values)
511 {
512 0 $hr = $hr->{$value};
513 }
514 0 delete $hr->{$object->id};
515 }
516  
517 sub _add_object($$)
518 0 ▶{
Subroutine _add_object: not called CC=5 SCAR=30.5
519 # We get warnings when undef converts into an empty string.
520 # For efficiency, we turn warnings off in this method.
521 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
522
523 0 my ($self, $object) = @_;
524 0 my @indexed_property_names = $self->indexed_property_names;
525 0 my @values = map { $object->$_ } @indexed_property_names;
526 0 my $hr = $self->{data_tree};
527 0 my $value;
528 0 for $value (@values)
529 {
530 0 ▶ $hr->{$value} ||= {};
Condition0%0/2
$hr->{$value} ||= {}
l!l
00
MC/DC0%0/1
$hr->{$value} ||= {}
$hr->{$value}
Truth table0%0/2
A
A= $hr->{$value}
Aresult
11
00
531 0 $hr = $hr->{$value};
532 }
533 0 $hr->{$object->id} = $object;
534
535 # This is the exact formula used elsewhere. TODO: refactor, base on class meta
536 0 ▶ if ($UR::Context::light_cache and substr($self->indexed_class_name,0,5) ne 'App::') {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::Context::light_cache and substr($self->indexed_class_name, 0, 5) ne "App::") 0 0 0
Condition0%0/3
$UR::Context::light_cache and substr($self->indexed_class_name, 0, 5) ne "App::"
!ll&&!rl&&r
000
MC/DC0%0/2
$UR::Context::light_cache and substr($self->indexed_class_name, 0, 5) ne "App::"
$UR::Context::light_cache substr($self->indexed_class_name, 0, 5) ne "App::"
Truth table0%0/3
A and B
A= $UR::Context::light_cacheB= substr($self->indexed_class_name, 0, 5) ne "App::"
ABresult
0X0
100
111
537 0 Scalar::Util::weaken($hr->{$object->id});
538 }
539 }
540  
541 0 ▶sub _all_objects_indexed {
Subroutine _all_objects_indexed: not called CC=3 SCAR=24.8
542 0 my $self = shift;
543  
544 0 my @object_hashes = ( $self->{data_tree} );
545  
546 # Recurse one level deep for each indexed property name
547 # and collect the hashes at that level
548 0 foreach ( $self->indexed_property_names ) {
549 0 my @new_object_hashes;
550 0 ▶ while (my $hr = shift @object_hashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hr = shift @object_hashes) 0 0 0
551 0 push @new_object_hashes, values(%$hr);
552 }
553 0 @object_hashes = @new_object_hashes;
554 }
555  
556 # The final level's values are all the objects
557 0 return map { values %$_ } @object_hashes;
558 }
559  
560 266 1;
561  
562 =pod
563  
564 =head1 NAME
565  
566 UR::Object::Index - Indexing system for retrieving objects by non-id properties
567  
568 =head1 DESCRIPTION
569  
570 This class implements an indexing system for objects to retrieve them quickly
571 by properties other than their ID properties. Their existence and use is
572 managed by the Context as needed, and end-users should never need to interact
573 with UR::Object::Index instances.
574  
575 Internally, they are a container for objects of the same class and a set of
576 properties used to look them up. Each time a get() is performed on a new set
577 of non-id properties, a new Index is created to handle the request for
578 objects which may already exist in the object cache,
579  
580 The data_tree inside the Index is a multi-level hash. The levels are in the
581 same order as the properties in the get request. At each level, the hash
582 keys are the values that target property has. For that level and key, all the
583 objects inside have the same value for that property. A get() by three non-id
584 properties will have a 3-level hash.
585  
586 =cut
↑ Summary blib/lib/UR/Namespace/Command/Update/ClassesFromDb.pm »