Object/Cached.pm

filter: Statementstmt 34.7% 75 / 216 Branchbran 15.3% 8 / 52 Conditioncond 7.6% 2 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 67.7% 21 / 31 Podpod 100% 10 / 10 totaltotal 33.0% 116 / 351 CC 41 SCAR 34.2
CC
41
Cov
29%
load
110.3
__xrdbopriv_get_object
49.4
remember
38.2
CRAP 30.5
« Object/Metadata/Auto/Oracle.pm ↑ Summary Object/Metadata/Column/Character.pm »
1 package Rose::DB::Object::Cached;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Rose::DB::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @ISA = qw(Rose::DB::Object);
9  
10 2 ▶use Rose::DB::Object::Constants qw(STATE_IN_DB);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.785';
13  
14 2 our $Debug = 0;
15  
16 # Anything that cannot be in a column name will work for these
17 2 ▶use constant PK_SEP => "\0\0";
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use constant UK_SEP => "\0\0";
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Try to pick a very unlikely value to stand in for undef in
21 # the stringified multi-column unique key value
22 2 ▶use constant UNDEF => "\1\2undef\2\1";
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub remember
25 0 ▶{
Subroutine remember: not called CC=7 SCAR=36.4
Pod documented
26 0 my($self) = shift;
27  
28 0 my $class = ref $self;
29 0 my $meta = $self->meta;
30 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $meta->primary_key_column_accessor_names);
31  
32 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 0 ▶ my $ttl_secs = $class->meta->cached_objects_expire_in || 0;
Condition0%0/2
$class->meta->cached_objects_expire_in || 0
l!l
00
MC/DC0%0/1
$class->meta->cached_objects_expire_in || 0
$class->meta->cached_objects_expire_in
Truth table0%0/2
A
A= $class->meta->cached_objects_expire_in
Aresult
11
00
35 0 ▶ my $loaded = $ttl_secs ? time : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ttl_secs ? : 0 0 0
36  
37 0 ${"${class}::Objects_By_Id"}{$pk} = $self;
38  
39 0 ▶ if($ttl_secs)
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl_secs) 0 0 0
40 {
41 0 ${"${class}::Objects_By_Id_Loaded"}{$pk} = $loaded;
42 }
43  
44 0 my $accessor = $meta->column_accessor_method_names_hash;
45  
46 0 foreach my $cols ($self->meta->unique_keys_column_names)
47 {
48 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 my $key_name = join(UK_SEP, @$cols);
50 0 ▶ my $key_value = join(UK_SEP, grep { defined($_) ? $_ : UNDEF }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
51 0 map { my $m = $accessor->{$_}; $self->$m() } @$cols);
52  
53 0 ${"${class}::Objects_By_Key"}{$key_name}{$key_value} = $self;
54 0 ${"${class}::Objects_Keys"}{$pk}{$key_name} = $key_value;
55  
56 0 ▶ if($ttl_secs)
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl_secs) 0 0 0
57 {
58 0 ${"${class}::Objects_By_Key_Loaded"}{$key_name}{$key_value} = $loaded;
59 }
60 }
61 };
62  
63 # This constant is not arbitrary. It must be defined and false.
64 # I'm playing games with return values, but this is all internal
65 # anyway and can change at any time.
66 2 ▶use constant CACHE_EXPIRED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 sub __xrdbopriv_get_object
69 0 ▶{
Subroutine __xrdbopriv_get_object: not called CC=9 SCAR=39.0
70 0 ▶ my($class) = ref $_[0] || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
71  
72 0 my $ttl_secs = $class->meta->cached_objects_expire_in;
73  
74 0 ▶ if(@_ == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
75 {
76 0 my($pk) = $_[1];
77  
78 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 0 ▶ if(${"${class}::Objects_By_Id"}{$pk})
Branch: 1 branch
BranchTrueFalseTotal
if (do { "${class}::Objects_By_Id" }->{$pk}) 0 0 0
82 {
83 0 ▶ if($ttl_secs && (time - ${"${class}::Objects_By_Id_Loaded"}{$pk}) >= $ttl_secs)
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl_secs and time - do { "${class}::Objects_By_Id_Loaded" }->{$pk} >= $ttl_secs) 0 0 0
Condition0%0/3
$ttl_secs and time - do { "${class}::Objects_By_Id_Loaded" }->{$pk} >= $ttl_secs
!ll&&!rl&&r
000
MC/DC0%0/2
$ttl_secs and time - do { "${class}::Objects_By_Id_Loaded" }->{$pk} >= $ttl_secs
$ttl_secs time - do { "${class}::Objects_By_Id_Loaded" }->{$pk} >= $ttl_secs
Truth table0%0/3
A and B
A= $ttl_secsB= time - do { "${class}::Objects_By_Id_Loaded" }->{$pk} >= $ttl_secs
ABresult
0X0
100
111
84 {
85 0 delete ${"${class}::Objects_By_Id"}{$pk};
86 0 return CACHE_EXPIRED;
87 }
88  
89 0 return ${"${class}::Objects_By_Id"}{$pk};
90 }
91  
92 0 return undef;
93 }
94 else
95 {
96 0 my($key_name, $key_value) = ($_[1], $_[2]);
97  
98 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 0 ▶ if(${"${class}::Objects_By_Key"}{$key_name}{$key_value})
Branch: 1 branch
BranchTrueFalseTotal
if (do { "${class}::Objects_By_Key" }->{$key_name}{$key_value}) 0 0 0
102 {
103 0 ▶ if($ttl_secs && (time - ${"${class}::Objects_By_Key_Loaded"}{$key_name}{$key_value}) >= $ttl_secs)
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl_secs and time - do { "${class}::Objects_By_Key_Loaded" }->{$key_name}{$key_value} >= $ttl_secs) 0 0 0
Condition0%0/3
$ttl_secs and time - do { "${class}::Objects_By_Key_Loaded" }->{$key_name}{$key_value} >= $ttl_secs
!ll&&!rl&&r
000
MC/DC0%0/2
$ttl_secs and time - do { "${class}::Objects_By_Key_Loaded" }->{$key_name}{$key_value} >= $ttl_secs
$ttl_secs time - do { "${class}::Objects_By_Key_Loaded" }->{$key_name}{$key_value} >= $ttl_secs
Truth table0%0/3
A and B
A= $ttl_secsB= time - do { "${class}::Objects_By_Key_Loaded" }->{$key_name}{$key_value} >= $ttl_secs
ABresult
0X0
100
111
104 {
105 0 delete ${"${class}::Objects_By_Key_Loaded"}{$key_name}{$key_value};
106 0 return undef; # cache expired
107 }
108  
109 0 ${"${class}::Objects_By_Key"}{$key_name}{$key_value}->remember();
110 0 return ${"${class}::Objects_By_Key"}{$key_name}{$key_value};
111 }
112  
113 0 return undef;
114 }
115 };
116  
117 sub load
118 0 ▶{
Subroutine load: not called CC=11 SCAR=47.0
Pod documented
119 # XXX: Must maintain alias to actual "self" object arg
120  
121 0 my %args = (self => @_); # faster than @_[1 .. $#_];
122  
123 0 ▶ unless(delete $args{'refresh'})
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $args{"refresh"}) 0 0 0
124 {
125 0 my $pk = join(PK_SEP, grep { defined } map { $_[0]->$_() } $_[0]->meta->primary_key_column_accessor_names);
126  
127 0 my $object = __xrdbopriv_get_object($_[0], $pk);
128  
129 0 ▶ if($object)
Branch: 2 branches
BranchTrueFalseTotal
if ($object) { } 0 0 0
elsif (not defined $object && $object == 0) { } 0 0 0
130 {
131 0 $_[0] = $object;
132 0 $_[0]->{STATE_IN_DB()} = 1;
133 0 ▶ return $_[0] || 1;
Condition0%0/2
$_[0] || 1
l!l
00
MC/DC0%0/1
$_[0] || 1
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
134 }
135 ▶ elsif(!(defined $object && $object == CACHE_EXPIRED))
Condition0%0/3
defined $object && $object == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $object && $object == 0
defined $object $object == 0
Truth table0%0/3
A && B
A= defined $objectB= $object == 0
ABresult
0X0
100
111
136 {
137 0 my $meta = $_[0]->meta;
138 0 my $accessor = $meta->column_accessor_method_names_hash;
139  
140 0 foreach my $cols ($meta->unique_keys_column_names)
141 {
142 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 0 my $key_name = join(UK_SEP, @$cols);
144 0 ▶ my $key_value = join(UK_SEP, grep { defined($_) ? $_ : UNDEF }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
145 0 map { my $m = $accessor->{$_}; $_[0]->$m() } @$cols);
146  
147 0 ▶ if(my $object = __xrdbopriv_get_object($_[0], $key_name, $key_value))
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = __xrdbopriv_get_object($_[0], $key_name, $key_value)) 0 0 0
148 {
149 0 $_[0] = $object;
150 0 $_[0]->{STATE_IN_DB()} = 1;
151 0 ▶ return $_[0] || 1;
Condition0%0/2
$_[0] || 1
l!l
00
MC/DC0%0/1
$_[0] || 1
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
152 }
153 }
154 }
155 }
156  
157 0 my $ret = $_[0]->SUPER::load(%args);
158 0 ▶ $_[0]->remember if($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
159  
160 0 return $ret;
161 }
162  
163 sub insert
164 0 ▶{
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
165 0 my($self) = shift;
166  
167 0 my $ret = $self->SUPER::insert(@_);
168 0 ▶ return $ret unless($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
169  
170 0 $self->remember;
171  
172 0 return $ret;
173 }
174  
175 sub update
176 0 ▶{
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
177 0 my($self) = shift;
178  
179 0 my $ret = $self->SUPER::update(@_);
180 0 ▶ return $ret unless($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
181  
182 0 $self->remember;
183  
184 0 return $ret;
185 }
186  
187 sub delete
188 0 ▶{
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
189 0 my($self) = shift;
190 0 my $ret = $self->SUPER::delete(@_);
191 0 ▶ $self->forget if($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
192 0 return $ret;
193 }
194  
195 sub forget
196 0 ▶{
Subroutine forget: not called CC=2 SCAR=12.6
Pod documented
197 0 my($self) = shift;
198  
199 0 my $class = ref $self;
200 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_accessor_names);
201  
202 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
203 0 delete ${"${class}::Objects_By_Id"}{$pk};
204  
205 0 foreach my $cols ($self->meta->unique_keys_column_names)
206 {
207 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 0 my $key_name = join(UK_SEP, @$cols);
209 0 my $key_value = ${"${class}::Objects_Keys"}{$pk}{$key_name};
210 0 delete ${"${class}::Objects_By_Key"}{$key_name}{$key_value};
211 }
212  
213 0 delete ${"${class}::Objects_Keys"}{$pk};
214  
215 0 return 1;
216 }
217  
218 sub remember_by_primary_key
219 0 ▶{
Subroutine remember_by_primary_key: not called CC=1 SCAR=2.9
Pod documented
220 0 my($self) = shift;
221  
222 0 my $class = ref $self;
223 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_accessor_names);
224  
225 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
226 0 ${"${class}::Objects_By_Id"}{$pk} = $self;
227 }
228  
229 sub remember_all
230 0 ▶{
Subroutine remember_all: not called CC=3 SCAR=24.8
Pod documented
231 0 my($class) = shift;
232  
233 0 require Rose::DB::Object::Manager;
234  
235 0 my(undef, %args) = Rose::DB::Object::Manager->normalize_get_objects_args(@_);
236  
237 0 my $objects =
238 Rose::DB::Object::Manager->get_objects(
239 object_class => $class,
240 share_db => 0,
241 %args);
242  
243 0 foreach my $object (@$objects)
244 {
245 0 $object->remember;
246 }
247  
248 0 ▶ return @$objects if(defined wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
249 }
250  
251 # Code borrowed from Cache::Cache
252 2 my %Expiration_Units =
253 (
254 map(($_, 1), qw(s sec secs second seconds)),
255 map(($_, 60), qw(m min mins minute minutes)),
256 map(($_, 60*60), qw(h hr hrs hour hours)),
257 map(($_, 60*60*24), qw(d day days)),
258 map(($_, 60*60*24*7), qw(w wk wks week weeks)),
259 map(($_, 60*60*24*365), qw(y yr yrs year years))
260 );
261  
262 sub clear_object_cache
263 0 ▶{
Subroutine clear_object_cache: not called CC=3 SCAR=21.6
Pod documented
264 0 ▶ my($class) = ref($_[0]) || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
265  
266 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
267 0 %{"${class}::Objects_By_Id"} = ();
268 0 %{"${class}::Objects_By_Key"} = ();
269 0 %{"${class}::Objects_Keys"} = ();
270  
271 0 ▶ if($class->cached_objects_expire_in)
Branch: 1 branch
BranchTrueFalseTotal
if ($class->cached_objects_expire_in) 0 0 0
272 {
273 0 %{"${class}::Objects_By_Key_Loaded"} = ();
274 0 %{"${class}::Objects_By_Id_Loaded"} = ();
275 }
276  
277 0 return 1;
278 }
279  
280 sub cached_objects_expire_in
281 196 ▶{
Subroutine cached_objects_expire_in: called CC=9 SCAR=25.1
Pod documented
282 196 my($class) = shift;
283  
284 196 ▶ $class = ref($class) if(ref($class));
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 28 168 196
285  
286 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
287 196 ▶ return ${"${class}::Cache_Expires"} ||= 0 unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 112 84 196
Condition50%1/2
${"${class}::Cache_Expires";} ||= 0
l!l
1120
MC/DC0%0/1
${"${class}::Cache_Expires";} ||= 0
${"${class}::Cache_Expires";}
Truth table50%1/2
A
A= ${"${class}::Cache_Expires";}
Aresult
11
00
288  
289 84 my $arg = shift;
290  
291 84 my $secs;
292  
293 84 ▶ if($arg =~ /^now$/i)
Branch: 4 branches
BranchTrueFalseTotal
if ($arg =~ /^now$/i) { } 0 84 84
elsif ($arg =~ /^never$/) { } 0 84 84
elsif ($arg =~ /^\s*([+-]?(?:\d+|\d*\.\d*))\s*$/) { } 0 84 84
elsif ($arg =~ /^\s*([+-]?(?:\d+(?:\.\d*)?|\d*\.\d+))\s*(\w*)\s*$/ and exists $Expiration_Units{$2}) { } 84 0 84
294 {
295 0 $class->forget_all;
296 0 $secs = 0;
297 }
298 elsif($arg =~ /^never$/)
299 {
300 0 $secs = 0;
301 }
302 elsif($arg =~ /^\s*([+-]?(?:\d+|\d*\.\d*))\s*$/)
303 {
304 0 $secs = $arg;
305 }
306 ▶ elsif($arg =~ /^\s*([+-]?(?:\d+(?:\.\d*)?|\d*\.\d+))\s*(\w*)\s*$/ && exists $Expiration_Units{$2})
Condition33%1/3
$arg =~ /^\s*([+-]?(?:\d+(?:\.\d*)?|\d*\.\d+))\s*(\w*)\s*$/ and exists $Expiration_Units{$2}
!ll&&!rl&&r
0084
MC/DC0%0/2
$arg =~ /^\s*([+-]?(?:\d+(?:\.\d*)?|\d*\.\d+))\s*(\w*)\s*$/ and exists $Expiration_Units{$2}
$arg =~ /^\s*([+-]?(?:\d+(?:\.\d*)?|\d*\.\d+))\s*(\w*)\s*$/ exists $Expiration_Units{$2}
Truth table33%1/3
A and B
A= $arg =~ /^\s*([+-]?(?:\d+(?:\.\d*)?|\d*\.\d+))\s*(\w*)\s*$/B= exists $Expiration_Units{$2}
ABresult
0X0
100
111
307 {
308 84 $secs = $Expiration_Units{$2} * $1;
309 }
310 else
311 {
312 0 Carp::croak("Invalid cache expiration time: '$arg'");
313 }
314  
315 84 return ${"${class}::Cache_Expires"} = $secs;
316 }
317  
318 2 1;
319  
320 __END__
« Object/Metadata/Auto/Oracle.pm ↑ Summary Object/Metadata/Column/Character.pm »