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

filter: Statementstmt 25.8% 45 / 174 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 53.5% 15 / 28 Podpod n/a totaltotal 21.3% 60 / 281 CC 37 SCAR 40.9
CC
37
Cov
19%
load
155.8
remember
132.0
__xrdbopriv_get_object
30.0
CRAP 59.6
↑ Summary
1 package Rose::DBx::Object::Cached::FastMmap;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Cache::FastMmap;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Rose::DB::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Rose::DB::Object::Helpers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Rose::DB::Object::Cached;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 our @ISA = qw(Rose::DB::Object);
14  
15 1 ▶use Rose::DB::Object::Constants qw(STATE_IN_DB);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 our $VERSION = '0.05';
18  
19 our $SETTINGS = undef;
20  
21 our $Debug = 0;
22  
23 # Use same expiration units from Rose::DB::Object::Cached;
24 my %Expiration_Units = %Rose::DB::Object::Cached::Expiration_Units;
25  
26  
27 # Anything that cannot be in a column name will work for these
28 1 ▶use constant PK_SEP => "\0\0";
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant UK_SEP => "\0\0";
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant LEVEL_SEP => "\0\0";
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # Try to pick a very unlikely value to stand in for undef in
33 # the stringified multi-column unique key value
34 1 ▶use constant UNDEF => "\1\2undef\2\1";
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37 sub remember
38 0 ▶{
Subroutine remember: not called CC=11 SCAR=48.8
39 0 my($self) = shift;
40 0 my $class = ref $self;
41 0 my $meta = $self->meta;
42
43 0 local $Storable::Deparse = 1;
44  
45 0 my $cache = $class->__xrdbopriv_get_cache_handle;
46  
47 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_names);
48  
49 0 my $safe_obj = $self->__xrdbopriv_clone->__xrdbopriv_strip;
50 0 $safe_obj->{__xrdbopriv_modified_columns} = {};
51  
52 0 ▶ my $successful_set = $cache->set("${class}::Objects_By_Id" . LEVEL_SEP . $pk, $safe_obj,($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
53  
54  
55 0 my $accessor = $meta->column_accessor_method_names_hash;
56  
57 0 foreach my $cols ($self->meta->unique_keys_column_names)
58 {
59 0 my $values_defined=0;
60  
61 0 my $key_name = join(UK_SEP, @$cols);
62 0 ▶ my $key_value = join(UK_SEP, grep { defined($_) ? $_ : UNDEF }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
63 0 ▶ map { my $m = $accessor->{$_};my $colval = $self->$m();$values_defined++ if defined($colval);$colval } @$cols);
Branch: 1 branch
BranchTrueFalseTotal
if defined $colval 0 0 0
64  
65 0 ▶ next unless $values_defined;
Branch: 1 branch
BranchTrueFalseTotal
unless $values_defined 0 0 0
66  
67  
68 0 ▶ $cache->set("${class}::Objects_By_Key" . LEVEL_SEP . $key_name . LEVEL_SEP . $key_value, $safe_obj, ($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
69  
70 0 ▶ $cache->set("${class}::Objects_Keys" . LEVEL_SEP . $pk . LEVEL_SEP . $key_name, $key_value, ($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
71
72 }
73  
74  
75 };
76  
77  
78 sub __xrdbopriv_get_object
79 0 ▶{
Subroutine __xrdbopriv_get_object: not called CC=5 SCAR=34.0
80 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
81  
82 0 local $Storable::Eval = 1;
83  
84 0 my $cache = $class->__xrdbopriv_get_cache_handle;
85  
86 0 ▶ if(@_ == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
87 {
88 0 my($pk) = $_[1];
89  
90 0 my $rose_object = $cache->get("${class}::Objects_By_Id" . LEVEL_SEP . $pk);
91  
92  
93 0 ▶ if($rose_object)
Branch: 1 branch
BranchTrueFalseTotal
if ($rose_object) 0 0 0
94 {
95 0 return $rose_object;
96 }
97  
98 0 return undef;
99 }
100 else
101 {
102 0 my($key_name, $key_value) = ($_[1], $_[2]);
103  
104 0 my $rose_object = $cache->get("${class}::Objects_By_Key" . LEVEL_SEP . $key_name . LEVEL_SEP . $key_value);
105 0 ▶ if($rose_object)
Branch: 1 branch
BranchTrueFalseTotal
if ($rose_object) 0 0 0
106 {
107 0 return $rose_object;
108 }
109  
110 0 return undef;
111 }
112 };
113  
114  
115 sub load
116 0 ▶{
Subroutine load: not called CC=13 SCAR=50.5
117 # XXX: Must maintain alias to actual "self" object arg
118  
119 0 my %args = (self => @_); # faster than @_[1 .. $#_];
120  
121 0 my $class = ref $_[0];
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 = $pk ? __xrdbopriv_get_object($class, $pk) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pk ? : 0 0 0
128  
129 0 ▶ if($object)
Branch: 2 branches
BranchTrueFalseTotal
if ($object) { } 0 0 0
elsif (not defined $object) { } 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))
136 {
137 0 my $meta = $_[0]->meta;
138 0 my $accessor = $meta->column_accessor_method_names_hash;
139  
140 0 foreach my $cols ($_[0]->meta->unique_keys_column_names)
141 {
142 0 my $values_defined=0;
143  
144 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 0 my $key_name = join(UK_SEP, @$cols);
146 0 ▶ my $key_value = join(UK_SEP, grep { defined($_) ? $_ : UNDEF }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
147 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
148  
149 0 ▶ next unless $values_defined;
Branch: 1 branch
BranchTrueFalseTotal
unless $values_defined 0 0 0
150  
151 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
152 {
153 0 $_[0] = $object;
154 0 $_[0]->{STATE_IN_DB()} = 1;
155 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
156 }
157 }
158 }
159 }
160  
161 0 my $ret = $_[0]->SUPER::load(%args);
162 0 ▶ $_[0]->remember if($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
163  
164 0 return $ret;
165 }
166  
167  
168 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
169 0 my($self) = shift;
170  
171 0 my $ret = $self->SUPER::update(@_);
172 0 ▶ return $ret unless($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
173  
174 0 $self->remember;
175  
176 0 return $ret;
177 }
178  
179 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
180 0 my($self) = shift;
181  
182 0 my $ret = $self->SUPER::insert(@_);
183 0 ▶ return $ret unless($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
184  
185 0 $self->remember;
186  
187 0 return $ret;
188 }
189  
190  
191 #sub save
192 #{
193 # my($self) = shift;
194 #
195 # my $ret = $self->SUPER::save(@_);
196 # return $ret unless($ret);
197 #
198 # $self->remember;
199 #
200 # return $ret;
201 #}
202  
203  
204 sub delete
205 0 ▶{
Subroutine delete: not called CC=2 SCAR=17.9
206 0 my($self) = shift;
207 0 my $ret = $self->SUPER::delete(@_);
208 0 ▶ $self->forget if($ret);
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
209 0 return $ret;
210 }
211  
212 sub forget
213 0 ▶{
Subroutine forget: not called CC=3 SCAR=24.8
214 0 my($self) = shift;
215 0 my $class = ref $self;
216  
217 0 my $cache = $class->__xrdbopriv_get_cache_handle;
218  
219 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_accessor_names);
220  
221 0 $cache->remove("${class}::Objects_By_Id" . LEVEL_SEP . $pk);
222  
223 0 foreach my $cols ($self->meta->unique_keys_column_names)
224 {
225 0 my $key_name = join(UK_SEP, @$cols);
226 0 ▶ my $key_value = $cache->get("${class}::Objects_Keys" . LEVEL_SEP . $pk . LEVEL_SEP . $key_name) || '';
Condition0%0/2
$cache->get("${class}::Objects_Keys" . "\000\000" . $pk . "\000\000" . $key_name) || ''
l!l
00
MC/DC0%0/1
$cache->get("${class}::Objects_Keys" . "\000\000" . $pk . "\000\000" . $key_name) || ''
$cache->get("${class}::Objects_Keys" . "\000\000" . $pk . "\000\000" . $key_name)
Truth table0%0/2
A
A= $cache->get("${class}::Objects_Keys" . "\000\000" . $pk . "\000\000" . $key_name)
Aresult
11
00
227 0 $cache->remove("${class}::Objects_By_Key" . LEVEL_SEP . $key_name . LEVEL_SEP . $key_value);
228 }
229  
230 0 $cache->remove("${class}::Objects_Keys" . LEVEL_SEP . $pk);
231  
232 0 return 1;
233 }
234  
235 sub remember_by_primary_key
236 0 ▶{
Subroutine remember_by_primary_key: not called CC=1 SCAR=6.9
237 0 my($self) = shift;
238 0 my $class = ref $self;
239  
240 0 my $cache = $class->__xrdbopriv_get_cache_handle;
241  
242 0 my $pk = join(PK_SEP, grep { defined } map { $self->$_() } $self->meta->primary_key_column_accessor_names);
243  
244 0 $cache->set("${class}::Objects_By_Id" . LEVEL_SEP . $pk, $self->__xrdbopriv_clone->__xrdbopriv_strip);
245 }
246  
247 sub remember_all
248 0 ▶{
Subroutine remember_all: not called CC=3 SCAR=24.8
249 0 my($class) = shift;
250  
251 0 require Rose::DB::Object::Manager;
252  
253 0 my(undef, %args) = Rose::DB::Object::Manager->normalize_get_objects_args(@_);
254  
255 0 my $objects =
256 Rose::DB::Object::Manager->get_objects(
257 object_class => $class,
258 share_db => 0,
259 %args);
260  
261 0 foreach my $object (@$objects)
262 {
263 0 $object->remember;
264 }
265  
266 0 ▶ return @$objects if(defined wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
267 }
268  
269  
270 sub clear_object_cache
271 0 ▶{
Subroutine clear_object_cache: not called CC=1 SCAR=6.9
272 0 my($class) = shift;
273  
274 0 my $cache = $class->__xrdbopriv_get_cache_handle;
275  
276 0 $cache->clear;
277 }
278  
279  
280 0 ▶sub cached_objects_expire_in {
Subroutine cached_objects_expire_in: not called CC=1 SCAR=6.9
281 0 my $class = shift;
282 0 Rose::DB::Object::Cached::cached_objects_expire_in($class,@_);
283 }
284  
285  
286 0 ▶sub cached_objects_settings {
Subroutine cached_objects_settings: not called CC=4 SCAR=26.6
287 0 my ($class, %params) = @_;;
288  
289 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
290  
291 0 ▶ if (keys %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %params) { } 0 0 0
292  
293 0 ${"${class}::FastMmap_SETTINGS"} = \%params;
294  
295 } else {
296 0 ▶ if (! defined ${"${class}::FastMmap_SETTINGS"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${"${class}::FastMmap_SETTINGS";}) 0 0 0
297 0 ▶ if (defined $SETTINGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Rose::DBx::Object::Cached::FastMmap::SETTINGS) { } 0 0 0
298 0 ${"${class}::FastMmap_SETTINGS"} = $SETTINGS;
299 } else {
300 0 ${"${class}::FastMmap_SETTINGS"} = $class->default_cached_objects_settings;
301 }
302 }
303 0 return ${"${class}::FastMmap_SETTINGS"};
304 }
305  
306 }
307  
308  
309 0 ▶sub default_cached_objects_settings {
Subroutine default_cached_objects_settings: not called CC=1 SCAR=6.9
310 0 my $class = shift;
311  
312 return {
313 0 driver => 'Memory',
314 namespace => $class
315 };
316 }
317  
318  
319 sub __xrdbopriv_get_cache_handle {
320 my $class = shift;
321  
322 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
323  
324 if (defined ${"${class}::FastMmap_CACHE_HANDLE"}) {
325 return ${"${class}::FastMmap_CACHE_HANDLE"};
326 } else {
327 my $defaults = $class->default_cached_objects_settings();
328  
329 my $current_settings = $class->cached_objects_settings;
330  
331 my %chi_settings = (
332 %$defaults,
333 (defined %$SETTINGS ? %$SETTINGS : ()),
334 %$current_settings
335 );
336  
337 my $cache = new Cache::FastMmap(%chi_settings);
338  
339 ${"${class}::FastMmap_CACHE_HANDLE"} = $cache;
340 return $cache;
341 }
342 }
343  
344  
345 sub __xrdbopriv_strip {
346 my $self = shift;
347  
348 Rose::DB::Object::Helpers::strip($self,@_);
349  
350 delete $self->{__xrdbopriv_chi_created_at};
351  
352 return $self;
353 }
354  
355 sub __xrdbopriv_clone {
356 my $self = shift;
357  
358 Rose::DB::Object::Helpers::clone($self,@_);
359 }
360  
361 1;
362  
363 __END__
↑ Summary