ORM/Cache.pm

filter: Statementstmt 66.1% 39 / 59 Branchbran 35.7% 10 / 28 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 8 totaltotal 48.0% 61 / 127 CC 20 SCAR 24.8
CC
20
Cov
53%
change_size
30.0
add
18.4
delete
5.5
CRAP 12.0
« ORM/StatMetaprop.pm ↑ Summary ORM/Auto.pm »
1 #
2 # DESCRIPTION
3 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
4 # library that implements object-relational mapping. Its features are
5 # much similar to those of Java's Hibernate library, but interface is
6 # much different and easier to use.
7 #
8 # AUTHOR
9 # Alexey V. Akimov <akimov_alexey@sourceforge.net>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2005-2006 Alexey V. Akimov
13 #
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23 #
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #
28  
29 package ORM::Cache;
30  
31 5 $VERSION = 0.83;
32  
33 5 ▶use vars '$use_weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 BEGIN
36 5 ▶{
Subroutine BEGIN: called CC=2 SCAR=7.0
37 eval
38 5 {
39 5 require Scalar::Util;
40 5 import Scalar::Util 'weaken';
41 };
42 5 ▶ $use_weaken = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 5 5
43 }
44  
45 5 my $cache_hit = 0;
46 5 my $cache_all = 0;
47  
48 sub new
49 5 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
50 5 my $class = shift;
51 5 my %arg = @_;
52 my $self =
53 {
54 hash => {},
55 array => [],
56 ptr => 0,
57 5 ▶ size => int( $arg{size}||0 ),
Condition50%1/2
$arg{'size'} || 0
l!l
50
MC/DC0%0/1
$arg{'size'} || 0
$arg{'size'}
Truth table50%1/2
A
A= $arg{'size'}
Aresult
11
00
58 };
59  
60 5 bless $self, $class;
61 5 ▶ $self->{array}[$self->size-1] = undef if( $self->size );
Branch: 1 branch
BranchTrueFalseTotal
if $self->size 5 0 5
62  
63 5 return $self;
64 }
65  
66 ##
67 ## PROPERTIES
68 ##
69  
70 10 ▶sub size { $_[0]->{size}; }
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
71 0 ▶sub total_efficiency { $cache_hit / $cache_all; }
Subroutine total_efficiency: not called CC=1 SCAR=6.9
Pod undocumented
72  
73 sub get
74 7 ▶{
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
75 7 my $self = shift;
76 7 my $id = shift;
77 7 my $hit = shift;
78 7 my $obj = $self->{hash}{$id};
79  
80 7 ▶ $hit = 1 unless( defined $hit );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hit 3 4 7
81  
82 7 ▶ $cache_hit+=$hit if( $obj );
Branch: 1 branch
BranchTrueFalseTotal
if $obj 1 6 7
83 7 $cache_all++;
84  
85 7 return $obj;
86 }
87  
88 ##
89 ## METHODS
90 ##
91  
92 sub add
93 27 ▶{
Subroutine add: called CC=8 SCAR=29.1
Pod undocumented
94 27 my $self = shift;
95 27 my $obj = shift;
96 27 ▶ my $id = $obj && $obj->id;
Condition33%1/3
$obj && $obj->id
!ll&&!rl&&r
0027
MC/DC0%0/2
$obj && $obj->id
$obj $obj->id
Truth table33%1/3
A && B
A= $objB= $obj->id
ABresult
0X0
100
111
97  
98 27 ▶ if( $id && !$self->{hash}{$id} )
Branch: 1 branch
BranchTrueFalseTotal
if ($id and not $self->{"hash"}{$id}) 27 0 27
Condition33%1/3
$id and not $self->{"hash"}{$id}
!ll&&!rl&&r
0027
MC/DC0%0/2
$id and not $self->{"hash"}{$id}
$id not $self->{"hash"}{$id}
Truth table33%1/3
A and not(B)
A= $idB= not $self->{"hash"}{$id}
ABresult
0X0
100
111
99 {
100 27 ▶ if( $use_weaken )
Branch: 1 branch
BranchTrueFalseTotal
if ($use_weaken) { } 27 0 27
101 {
102 27 ▶ if( $self->{size} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"size"}) 27 0 27
103 {
104 27 $self->{array}[$self->{ptr}] = $obj;
105 27 $self->{ptr} = ( $self->{ptr} + 1 ) % $self->{size};
106 }
107 27 $self->{hash}{$id} = $obj;
108 27 weaken $self->{hash}{$id};
109 }
110 else
111 {
112 0 ▶ if( $self->{size} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"size"}) 0 0 0
113 {
114 0 my $slot = \( $self->{array}[$self->{ptr}] );
115 0 ▶ delete $self->{hash}{ ${$slot}->id } if( $$slot );
Branch: 1 branch
BranchTrueFalseTotal
if $$slot 0 0 0
116  
117 0 $$slot = $obj;
118 0 $self->{ptr} = ( $self->{ptr} + 1 ) % $self->{size};
119 0 $self->{hash}{$id} = $obj;
120 }
121 }
122 }
123 }
124  
125 sub delete
126 6 ▶{
Subroutine delete: called CC=4 SCAR=17.1
Pod undocumented
127 6 my $self = shift;
128 6 my $obj = shift;
129  
130 6 ▶ $use_weaken && $obj && $obj->id && delete $self->{hash}{$obj->id};
Branch: 1 branch
BranchTrueFalseTotal
$use_weaken and $obj and $obj->id and delete $self->{"hash"}{$obj->id} 6 0 6
Condition33%2/6
$use_weaken and $obj and $obj->id
!ll&&!rl&&r
006
$use_weaken and $obj
!ll&&!rl&&r
006
MC/DC0%0/3
$use_weaken and $obj and $obj->id
$use_weaken $obj $obj->id
Truth table25%1/4
A and B and C
A= $use_weakenB= $objC= $obj->id
ABCresult
0XX0
10X0
1100
1111
131 }
132  
133 sub clear_stat
134 0 ▶{
Subroutine clear_stat: not called CC=1 SCAR=6.9
Pod undocumented
135 0 $cache_hit = 0;
136 0 $cache_all = 0;
137 }
138  
139 sub change_size
140 0 ▶{
Subroutine change_size: not called CC=5 SCAR=34.0
Pod undocumented
141 0 my $self = shift;
142 0 my $new_size = int shift;
143  
144 0 ▶ $new_size = 0 if( $new_size < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $new_size < 0 0 0 0
145  
146 0 ▶ if( $new_size > $self->size )
Branch: 2 branches
BranchTrueFalseTotal
if ($new_size > $self->size) { } 0 0 0
elsif ($new_size < $self->size) { } 0 0 0
147 {
148 0 $self->{size} = $new_size;
149 0 $self->{array}[$new_size-1] = undef;
150 }
151 elsif( $new_size < $self->size )
152 {
153 0 $self->{size} = $new_size;
154 0 ▶ $self->{ptr} = 0 if( $self->{ptr} >= $new_size );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ptr"} >= $new_size 0 0 0
155 0 splice @{$self->{array}}, $new_size;
156 }
157 }
158  
159 5 1;
« ORM/StatMetaprop.pm ↑ Summary ORM/Auto.pm »