File Coverage

blib/lib/ORM/Cache.pm
Criterion Covered Total %
statement 35 55 63.6
branch 10 28 35.7
condition 5 14 35.7
subroutine 7 10 70.0
pod 0 8 0.0
total 57 115 49.5


line stmt bran cond sub pod time code
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
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             $VERSION = 0.83;
32              
33 5     5   26 use vars '$use_weaken';
  5         7  
  5         448  
34              
35             BEGIN
36             {
37             eval
38 5     5   10 {
39 5         23 require Scalar::Util;
40 5         159 import Scalar::Util 'weaken';
41             };
42 5 50       2839 $use_weaken = $@ ? 0 : 1;
43             }
44              
45             my $cache_hit = 0;
46             my $cache_all = 0;
47              
48             sub new
49             {
50 5     5 0 69 my $class = shift;
51 5         18 my %arg = @_;
52 5   50     53 my $self =
53             {
54             hash => {},
55             array => [],
56             ptr => 0,
57             size => int( $arg{size}||0 ),
58             };
59              
60 5         17 bless $self, $class;
61 5 50       16 $self->{array}[$self->size-1] = undef if( $self->size );
62              
63 5         24 return $self;
64             }
65              
66             ##
67             ## PROPERTIES
68             ##
69              
70 10     10 0 78 sub size { $_[0]->{size}; }
71 0     0 0 0 sub total_efficiency { $cache_hit / $cache_all; }
72              
73             sub get
74             {
75 7     7 0 81 my $self = shift;
76 7         11 my $id = shift;
77 7         13 my $hit = shift;
78 7         38 my $obj = $self->{hash}{$id};
79              
80 7 100       26 $hit = 1 unless( defined $hit );
81              
82 7 100       24 $cache_hit+=$hit if( $obj );
83 7         13 $cache_all++;
84              
85 7         23 return $obj;
86             }
87              
88             ##
89             ## METHODS
90             ##
91              
92             sub add
93             {
94 27     27 0 301 my $self = shift;
95 27         41 my $obj = shift;
96 27   33     204 my $id = $obj && $obj->id;
97              
98 27 50 33     243 if( $id && !$self->{hash}{$id} )
99             {
100 27 50       79 if( $use_weaken )
101             {
102 27 50       103 if( $self->{size} )
103             {
104 27         85 $self->{array}[$self->{ptr}] = $obj;
105 27         103 $self->{ptr} = ( $self->{ptr} + 1 ) % $self->{size};
106             }
107 27         90 $self->{hash}{$id} = $obj;
108 27         181 weaken $self->{hash}{$id};
109             }
110             else
111             {
112 0 0       0 if( $self->{size} )
113             {
114 0         0 my $slot = \( $self->{array}[$self->{ptr}] );
115 0 0       0 delete $self->{hash}{ ${$slot}->id } if( $$slot );
  0         0  
116              
117 0         0 $$slot = $obj;
118 0         0 $self->{ptr} = ( $self->{ptr} + 1 ) % $self->{size};
119 0         0 $self->{hash}{$id} = $obj;
120             }
121             }
122             }
123             }
124              
125             sub delete
126             {
127 6     6 0 69 my $self = shift;
128 6         13 my $obj = shift;
129              
130 6 50 33     70 $use_weaken && $obj && $obj->id && delete $self->{hash}{$obj->id};
      33        
131             }
132              
133             sub clear_stat
134             {
135 0     0 0   $cache_hit = 0;
136 0           $cache_all = 0;
137             }
138              
139             sub change_size
140             {
141 0     0 0   my $self = shift;
142 0           my $new_size = int shift;
143              
144 0 0         $new_size = 0 if( $new_size < 0 );
145              
146 0 0         if( $new_size > $self->size )
    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 0         $self->{ptr} = 0 if( $self->{ptr} >= $new_size );
155 0           splice @{$self->{array}}, $new_size;
  0            
156             }
157             }
158              
159             1;