blib/lib/Rose/DBx/Object/Cached/CHI.pm

filter: Statementstmt 24.3% 57 / 234 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 51.5% 17 / 33 Podpod 53.8% 7 / 13 totaltotal 21.0% 81 / 385 CC 47 SCAR 41.8
CC
47
Cov
18%
remember
182.0
load
155.8
__xrdbopriv_get_object
56.0
CRAP 65.2
↑ Summary
1 package Rose::DBx::Object::Cached::CHI;
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 CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Rose::DB::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Rose::DB::Object::Helpers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Rose::DB::Object::Cached;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our @ISA = qw(Rose::DB::Object);
16  
17 2 ▶use Rose::DB::Object::Constants qw(STATE_IN_DB MODIFIED_COLUMNS);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = '0.22';
20 2 our $SETTINGS = undef;
21 2 our $Debug = 0;
22 2 our $USE_IN_SYNC = 0;
23  
24 # Use same expiration units from Rose::DB::Object::Cached;
25 2 my %Expiration_Units = %Rose::DB::Object::Cached::Expiration_Units;
26  
27  
28 # Anything that cannot be in a column name will work for these
29 # Changed to '&' for compatibility with Memcached
30 2 ▶use constant PK_SEP => "&";
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use constant UK_SEP => "&";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use constant LEVEL_SEP => "&";
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Try to pick a very unlikely value to stand in for undef in
35 # the stringified multi-column unique key value
36 2 ▶use constant UNDEF => "\1\2undef\2\1";
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38  
39 sub remember
40 0 ▶{
Subroutine remember: not called CC=13 SCAR=52.0
Pod undocumented
41 0 my($self) = shift;
42 0 my $class = ref $self;
43 0 my $meta = $self->meta;
44  
45 0 local $Storable::Deparse = 1;
46  
47 0 my $cache = $class->__xrdbopriv_get_cache_handle;
48  
49 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $meta->primary_key_column_accessor_names);
50  
51 0 my $safe_obj = $self->__xrdbopriv_clone->__xrdbopriv_strip; ## Strip has been fixed so that all CODE REFs are gone
52 ## but keeping the clone here so we don't mess with the
53 ## object the developer is currently working with
54  
55 0 ▶ my %orig_modified = $self->{MODIFIED_COLUMNS()} ? %{$self->{MODIFIED_COLUMNS()}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__xrdbopriv_modified_columns'} ? : 0 0 0
56 0 $safe_obj->{MODIFIED_COLUMNS()} = \%orig_modified;
57  
58 0 ▶ my $expire_in = ($self->meta->cached_objects_expire_in || $class->cached_objects_settings->{expires_in} || 'never');
Condition0%0/5
$self->meta->cached_objects_expire_in || $class->cached_objects_settings->{'expires_in'} || 'never'
l!l
00
$self->meta->cached_objects_expire_in or $class->cached_objects_settings->{"expires_in"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->meta->cached_objects_expire_in || $class->cached_objects_settings->{'expires_in'} || 'never'
$self->meta->cached_objects_expire_in $class->cached_objects_settings->{"expires_in"}
Truth table0%0/3
A or B
A= $self->meta->cached_objects_expire_inB= $class->cached_objects_settings->{"expires_in"}
ABresult
1X1
011
000
59  
60 0 my $pk_cache_key = "${class}::Objects_By_Id" . LEVEL_SEP . $pk;
61 0 my $successful_pk_set = $cache->set($pk_cache_key, $safe_obj,$expire_in);
62 0 ▶ $cache->remove($pk_cache_key) unless $successful_pk_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $successful_pk_set 0 0 0
63  
64 0 my $accessor = $meta->column_accessor_method_names_hash;
65  
66 0 foreach my $cols ($self->meta->unique_keys_column_names)
67 {
68 0 my $values_defined = 0;
69  
70 0 my $key_name = join(UK_SEP, @$cols);
71 0 ▶ my $key_value = join(UK_SEP, grep { defined($_) ? $_ : UNDEF }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
72 0 ▶ map { my $m = $accessor->{$_}; my $colval = $safe_obj->$m();$values_defined++ if defined($colval);$colval } @$cols);
Branch: 1 branch
BranchTrueFalseTotal
if defined $colval 0 0 0
73  
74 0 ▶ next unless ($values_defined == @$cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $values_defined == @$cols 0 0 0
75  
76 0 my $ObKey_cache_key = "${class}::Objects_By_Key" . LEVEL_SEP . $key_name . LEVEL_SEP . $key_value;
77 0 my $ObKeys_cache_key = "${class}::Objects_Keys" . LEVEL_SEP . $pk . LEVEL_SEP . $key_name;
78  
79 0 my $successful_set = $cache->set($ObKey_cache_key, $safe_obj, $expire_in);
80 0 ▶ $cache->remove($ObKey_cache_key) unless $successful_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $successful_set 0 0 0
81  
82 0 $successful_set = $cache->set("${class}::Objects_Keys" . LEVEL_SEP . $pk . LEVEL_SEP . $key_name, $key_value, $expire_in);
83 0 ▶ $cache->remove($ObKeys_cache_key) unless $successful_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $successful_set 0 0 0
84  
85 }
86  
87 0 ▶ if ($Rose::DBx::Object::Cached::CHI::USE_IN_SYNC && $successful_pk_set ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rose::DBx::Object::Cached::CHI::USE_IN_SYNC and $successful_pk_set) 0 0 0
Condition0%0/3
$Rose::DBx::Object::Cached::CHI::USE_IN_SYNC and $successful_pk_set
!ll&&!rl&&r
000
MC/DC0%0/2
$Rose::DBx::Object::Cached::CHI::USE_IN_SYNC and $successful_pk_set
$Rose::DBx::Object::Cached::CHI::USE_IN_SYNC $successful_pk_set
Truth table0%0/3
A and B
A= $Rose::DBx::Object::Cached::CHI::USE_IN_SYNCB= $successful_pk_set
ABresult
0X0
100
111
88 0 my $chi_cache_object = $self->{__xrdbopriv_chi_created_at} = $cache->get_object("${class}::Objects_By_Id" . LEVEL_SEP . $pk);
89 0 $self->{__xrdbopriv_chi_created_at} = $chi_cache_object->created_at();
90 }
91  
92 };
93  
94  
95 sub __xrdbopriv_get_object
96 0 ▶{
Subroutine __xrdbopriv_get_object: not called CC=7 SCAR=40.3
97 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
98  
99 0 local $Storable::Eval = 1;
100  
101 0 my $cache = $class->__xrdbopriv_get_cache_handle;
102  
103 0 ▶ if(@_ == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
104 {
105 0 my($pk) = $_[1];
106  
107 0 my $cache_object = $cache->get_object("${class}::Objects_By_Id" . LEVEL_SEP . $pk);
108  
109  
110 0 ▶ if($cache_object && !$cache_object->is_expired)
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_object and not $cache_object->is_expired) 0 0 0
Condition0%0/3
$cache_object and not $cache_object->is_expired
!ll&&!rl&&r
000
MC/DC0%0/2
$cache_object and not $cache_object->is_expired
$cache_object not $cache_object->is_expired
Truth table0%0/3
A and not(B)
A= $cache_objectB= not $cache_object->is_expired
ABresult
0X0
100
111
111 {
112 0 my $rose_object = $cache_object->value;
113 0 $rose_object->{__xrdbopriv_chi_created_at} = $cache_object->created_at;
114 0 return $rose_object;
115 }
116  
117 0 return undef;
118 }
119 else
120 {
121 0 my($key_name, $key_value) = ($_[1], $_[2]);
122  
123 0 my $cache_object = $cache->get_object("${class}::Objects_By_Key" . LEVEL_SEP . $key_name . LEVEL_SEP . $key_value);
124 0 ▶ if($cache_object && !$cache_object->is_expired)
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_object and not $cache_object->is_expired) 0 0 0
Condition0%0/3
$cache_object and not $cache_object->is_expired
!ll&&!rl&&r
000
MC/DC0%0/2
$cache_object and not $cache_object->is_expired
$cache_object not $cache_object->is_expired
Truth table0%0/3
A and not(B)
A= $cache_objectB= not $cache_object->is_expired
ABresult
0X0
100
111
125 {
126 0 my $rose_object = $cache_object->value;
127 0 $rose_object->{__xrdbopriv_chi_created_at} = $cache_object->created_at;
128 0 return $rose_object;
129 }
130  
131 0 return undef;
132 }
133 };
134  
135  
136 sub load
137 0 ▶{
Subroutine load: not called CC=13 SCAR=50.5
Pod documented
138 # XXX: Must maintain alias to actual "self" object arg
139  
140 0 my %args = (self => @_); # faster than @_[1 .. $#_];
141  
142 0 my $class = ref $_[0];
143  
144 0 ▶ unless(delete $args{'refresh'})
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $args{"refresh"}) 0 0 0
145 {
146 0 my $pk = join(PK_SEP, grep { defined } map { $_[0]->$_() } $_[0]->meta->primary_key_column_accessor_names);
147  
148 0 ▶ my $object = $pk ? __xrdbopriv_get_object($class, $pk) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pk ? : 0 0 0
149  
150 0 ▶ if($object)
Branch: 2 branches
BranchTrueFalseTotal
if ($object) { } 0 0 0
elsif (not defined $object) { } 0 0 0
151 {
152 0 $_[0] = $object;
153 0 $_[0]->{STATE_IN_DB()} = 1;
154 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
155 }
156 elsif(!(defined $object))
157 {
158 0 my $meta = $_[0]->meta;
159 0 my $accessor = $meta->column_accessor_method_names_hash;
160  
161 0 foreach my $cols ($_[0]->meta->unique_keys_column_names)
162 {
163 0 my $values_defined = 0;
164  
165 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 0 my $key_name = join(UK_SEP, @$cols);
167 0 ▶ my $key_value = join(UK_SEP, grep { defined($_) ? $_ : UNDEF }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
168 0 ▶ map { my $m = $accessor->{$_}; my $colval = $_[0]->$m();$values_defined++ if defined($colval);$colval } @$cols);
Branch: 1 branch
BranchTrueFalseTotal
if defined $colval 0 0 0
169  
170  
171 0 ▶ next unless $values_defined;
Branch: 1 branch
BranchTrueFalseTotal
unless $values_defined 0 0 0
172  
173 0 ▶ if(my $object = __xrdbopriv_get_object($class, $key_name, $key_value))
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = __xrdbopriv_get_object($class, $key_name, $key_value)) 0 0 0
174 {
175 0 $_[0] = $object;
176 0 $_[0]->{STATE_IN_DB()} = 1;
177 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
178 }
179 }
180 }
181 }
182  
183 0 my $ret = $_[0]->SUPER::load(%args);
184 0 ▶ $_[0]->remember if($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
185  
186 0 return $ret;
187 }
188  
189  
190 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
191 0 my($self) = shift;
192  
193 0 my $ret = $self->SUPER::update(@_);
194 0 ▶ return $ret unless($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
195  
196 0 $self->remember;
197  
198 0 return $ret;
199 }
200  
201 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
202 0 my($self) = shift;
203  
204 0 my $ret = $self->SUPER::insert(@_);
205 0 ▶ return $ret unless($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
206  
207 0 $self->remember;
208  
209 0 return $ret;
210 }
211  
212  
213 sub delete
214 0 ▶{
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
215 0 my($self) = shift;
216 0 my $ret = $self->SUPER::delete(@_);
217 0 ▶ $self->forget if($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
218 0 return $ret;
219 }
220  
221  
222 sub forget
223 0 ▶{
Subroutine forget: not called CC=3 SCAR=24.8
Pod undocumented
224 0 my($self) = shift;
225 0 my $class = ref $self;
226  
227 0 my $cache = $class->__xrdbopriv_get_cache_handle;
228  
229 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_accessor_names);
230  
231 0 $cache->expire("${class}::Objects_By_Id" . LEVEL_SEP . $pk);
232  
233 0 foreach my $cols ($self->meta->unique_keys_column_names)
234 {
235 0 my $key_name = join(UK_SEP, @$cols);
236 0 ▶ my $key_value = $cache->get("${class}::Objects_Keys" . LEVEL_SEP . $pk . LEVEL_SEP . $key_name) || '';
Condition0%0/2
$cache->get("${class}::Objects_Keys" . '&' . $pk . '&' . $key_name) || ''
l!l
00
MC/DC0%0/1
$cache->get("${class}::Objects_Keys" . '&' . $pk . '&' . $key_name) || ''
$cache->get("${class}::Objects_Keys" . '&' . $pk . '&' . $key_name)
Truth table0%0/2
A
A= $cache->get("${class}::Objects_Keys" . '&' . $pk . '&' . $key_name)
Aresult
11
00
237 0 $cache->expire("${class}::Objects_By_Key" . LEVEL_SEP . $key_name . LEVEL_SEP . $key_value);
238 }
239  
240 0 $cache->expire("${class}::Objects_Keys" . LEVEL_SEP . $pk);
241  
242 0 return 1;
243 }
244  
245  
246 sub remember_by_primary_key
247 0 ▶{
Subroutine remember_by_primary_key: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my($self) = shift;
249 0 my $class = ref $self;
250  
251 0 my $cache = $class->__xrdbopriv_get_cache_handle;
252  
253 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_accessor_names);
254  
255 0 $cache->set("${class}::Objects_By_Id" . LEVEL_SEP . $pk, $self->__xrdbopriv_clone->__xrdbopriv_strip);
256 }
257  
258 sub remember_all
259 0 ▶{
Subroutine remember_all: not called CC=3 SCAR=24.8
Pod undocumented
260 0 my($class) = shift;
261  
262 0 require Rose::DB::Object::Manager;
263  
264 0 my(undef, %args) = Rose::DB::Object::Manager->normalize_get_objects_args(@_);
265  
266 0 my $objects =
267 Rose::DB::Object::Manager->get_objects(
268 object_class => $class,
269 share_db => 0,
270 %args);
271  
272 0 foreach my $object (@$objects)
273 {
274 0 $object->remember;
275 }
276  
277 0 ▶ return @$objects if(defined wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
278 }
279  
280  
281 sub clear_object_cache
282 0 ▶{
Subroutine clear_object_cache: not called CC=1 SCAR=6.9
Pod undocumented
283 0 my($class) = shift;
284  
285 0 my $cache = $class->__xrdbopriv_get_cache_handle;
286  
287 0 $cache->clear;
288 }
289  
290  
291 112 ▶sub cached_objects_expire_in {
Subroutine cached_objects_expire_in: called CC=1 SCAR=0.0
Pod undocumented
292 112 my $class = shift;
293 112 Rose::DB::Object::Cached::cached_objects_expire_in($class,@_);
294 }
295  
296  
297 0 ▶sub cached_objects_settings {
Subroutine cached_objects_settings: not called CC=4 SCAR=26.6
Pod documented
298 0 my ($class, %params) = @_;;
299  
300 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
301  
302 0 ▶ if (keys %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %params) { } 0 0 0
303  
304 0 ${"${class}::CHI_SETTINGS"} = \%params;
305  
306 } else {
307 0 ▶ if (! defined ${"${class}::CHI_SETTINGS"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${"${class}::CHI_SETTINGS";}) 0 0 0
308 0 ▶ if (defined $SETTINGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $SETTINGS) { } 0 0 0
309 0 ${"${class}::CHI_SETTINGS"} = $SETTINGS;
310 } else {
311 0 ${"${class}::CHI_SETTINGS"} = $class->default_cached_objects_settings;
312 }
313 }
314 0 return ${"${class}::CHI_SETTINGS"};
315 }
316  
317 }
318  
319  
320 0 ▶sub is_cache_in_sync {
Subroutine is_cache_in_sync: not called CC=5 SCAR=34.0
Pod documented
321 0 my $self = shift;
322  
323 0 ▶ if ($Rose::DBx::Object::Cached::CHI::USE_IN_SYNC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($USE_IN_SYNC) { } 0 0 0
324 0 ▶ return unless $self->{__xrdbopriv_chi_created_at};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__xrdbopriv_chi_created_at"} 0 0 0
325 0 my $class = ref $self;
326 0 my $cache = $class->__xrdbopriv_get_cache_handle;
327 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_names);
328 0 my $created_at = $cache->get_object("${class}::Objects_By_Id" . LEVEL_SEP . $pk)->created_at;
329  
330 0 ▶ if ($created_at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created_at) { } 0 0 0
331 0 return ($self->{__xrdbopriv_chi_created_at} == $created_at);
332 } else {
333 0 ▶ if ($_[0]->{STATE_IN_DB()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{'__xrdbopriv_in_db'}) { } 0 0 0
334 # Has been loaded
335 } else {
336 0 Carp::cluck "Object never loaded";
337 }
338 0 return 0;
339 }
340 } else {
341 0 warn 'is_cache_in_sync not activated. Set $Rose::DBx::Object::Cached::CHI::USE_IN_SYNC to a true value';
342 0 return undef;
343 }
344  
345 }
346  
347  
348 0 ▶sub default_cached_objects_settings {
Subroutine default_cached_objects_settings: not called CC=1 SCAR=6.9
Pod documented
349 0 my $class = shift;
350  
351 return {
352 0 driver => 'Memory',
353 namespace => $class,
354 global => 1
355 };
356 }
357  
358  
359 0 ▶sub __xrdbopriv_get_cache_handle {
Subroutine __xrdbopriv_get_cache_handle: not called CC=3 SCAR=21.4
360 0 my $class = shift;
361  
362 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
363  
364 0 ▶ if (defined ${"${class}::CHI_CACHE_HANDLE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${"${class}::CHI_CACHE_HANDLE";}) { } 0 0 0
365 0 return ${"${class}::CHI_CACHE_HANDLE"};
366 } else {
367 0 my $defaults = $class->default_cached_objects_settings();
368  
369 0 my $current_settings = $class->cached_objects_settings;
370  
371 0 ▶ my %chi_settings = (
Condition0%0/3
%$SETTINGS || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
%$SETTINGS || ()
%$SETTINGS ()
Truth table0%0/3
A || B
A= %$SETTINGSB= ()
ABresult
1X1
011
000
372 %$defaults,
373 (%$SETTINGS || ()),
374 %$current_settings
375 );
376  
377 0 my $cache = new CHI(%chi_settings);
378  
379 0 ${"${class}::CHI_CACHE_HANDLE"} = $cache;
380 0 return $cache;
381 }
382 }
383  
384  
385 0 ▶sub __xrdbopriv_strip {
Subroutine __xrdbopriv_strip: not called CC=1 SCAR=6.9
386 0 my $self = shift;
387  
388 0 Rose::DB::Object::Helpers::strip($self,@_);
389  
390 0 delete $self->{__xrdbopriv_chi_created_at};
391  
392 0 return $self;
393 }
394  
395 0 ▶sub __xrdbopriv_clone {
Subroutine __xrdbopriv_clone: not called CC=1 SCAR=6.9
396 0 my $self = shift;
397  
398 0 Rose::DB::Object::Helpers::clone($self,@_);
399 }
400  
401 2 1;
402  
403 __END__
↑ Summary