Perlbal/Cache.pm

filter: Statementstmt 37.9% 55 / 145 Branchbran 12.8% 9 / 70 Conditioncond 5.5% 2 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 61.1% 11 / 18 Podpod 0.0% 0 / 10 totaltotal 25.5% 77 / 302 CC 48 SCAR 55.5
CC
48
Cov
26%
validate_list
650.0
drop_tail
30.0
remove
30.0
CRAP 258.5
↑ Summary Perlbal/Plugin/AccessControl.pm »
1 # (This is a copy of Cache::SimpleLRU.)
2 # License to use and redistribute this under the same terms as Perl itself.
3  
4 package Perlbal::Cache;
5  
6 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use fields qw(items size tail head maxsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use constant PREVREF => 0; # ptr left, to newer item
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use constant VALUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use constant NEXTREF => 2; # ptr right, to older item
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use constant KEY => 3; # copy of key for unlinking from namespace on fallout
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 $VERSION = '1.0';
16  
17 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
18 1 my $class = shift;
19 1 my $self = fields::new($class);
20 1 ▶ my $args = @_ == 1 ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
21  
22 $self->{head} = undef,
23 $self->{tail} = undef,
24 1 $self->{items} = {}; # key -> arrayref, indexed by constants above
25 1 $self->{size} = 0;
26 1 $self->{maxsize} = $args->{maxsize}+0;
27 1 return $self;
28 }
29  
30 # need to DESTROY to cleanup doubly-linked list (circular refs)
31 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
32 0 my $self = shift;
33 0 $self->set_maxsize(0);
34 0 $self->validate_list;
35 }
36  
37 # calls $code->($val) for each value in cache. $code must return true
38 # to continue walking. foreach returns true if you hit the end.
39 0 ▶sub foreach {
Subroutine foreach: not called CC=4 SCAR=30.0
Pod undocumented
40 0 my Perlbal::Cache $self = shift;
41 0 my $code = shift;
42 0 my $iter = $self->{head};
43 0 ▶ while ($iter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter) 0 0 0
44 0 my $val = $iter->[VALUE];
45 0 $iter = $iter->[NEXTREF];
46 0 ▶ last unless $code->($val);
Branch: 1 branch
BranchTrueFalseTotal
unless &$code($val) 0 0 0
47 }
48 0 ▶ return $iter ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$iter ? : 0 0 0
49 }
50  
51 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my Perlbal::Cache $self = shift;
53 0 return $self->{size};
54 }
55  
56 0 ▶sub maxsize {
Subroutine maxsize: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my Perlbal::Cache $self = shift;
58 0 return $self->{maxsize};
59 }
60  
61 1 ▶sub set_maxsize {
Subroutine set_maxsize: called CC=2 SCAR=7.0
Pod undocumented
62 1 my Perlbal::Cache $self = shift;
63 1 my $maxsize = shift;
64 1 $self->{maxsize} = $maxsize;
65 $self->drop_tail while
66 1 ▶ $self->{size} > $self->{maxsize};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"size"} > $self->{"maxsize"} and do { $self->drop_tail } 0 1 1
67 }
68  
69 # For debugging only
70 0 ▶sub validate_list {
Subroutine validate_list: not called CC=25 SCAR=64.8
Pod undocumented
71 0 my Perlbal::Cache $self = shift;
72  
73 0 ▶ die "no tail pointer\n" if $self->{size} && ! $self->{tail};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"size"} and not $self->{"tail"} 0 0 0
Condition0%0/3
$self->{"size"} and not $self->{"tail"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"size"} and not $self->{"tail"}
$self->{"size"} not $self->{"tail"}
Truth table0%0/3
A and not(B)
A= $self->{"size"}B= not $self->{"tail"}
ABresult
0X0
100
111
74 0 ▶ die "no head pointer\n" if $self->{size} && ! $self->{head};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"size"} and not $self->{"head"} 0 0 0
Condition0%0/3
$self->{"size"} and not $self->{"head"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"size"} and not $self->{"head"}
$self->{"size"} not $self->{"head"}
Truth table0%0/3
A and not(B)
A= $self->{"size"}B= not $self->{"head"}
ABresult
0X0
100
111
75 0 ▶ die "unwanted tail pointer\n" if ! $self->{size} && $self->{tail};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"size"} and $self->{"tail"} 0 0 0
Condition0%0/3
not $self->{"size"} and $self->{"tail"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"size"} and $self->{"tail"}
not $self->{"size"} $self->{"tail"}
Truth table0%0/3
not(A) and B
A= not $self->{"size"}B= $self->{"tail"}
ABresult
0X0
100
111
76 0 ▶ die "unwanted head pointer\n" if ! $self->{size} && $self->{head};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"size"} and $self->{"head"} 0 0 0
Condition0%0/3
not $self->{"size"} and $self->{"head"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"size"} and $self->{"head"}
not $self->{"size"} $self->{"head"}
Truth table0%0/3
not(A) and B
A= not $self->{"size"}B= $self->{"head"}
ABresult
0X0
100
111
77  
78 0 my $iter = $self->{head};
79 0 my $last = undef;
80 0 my $count = 1;
81 0 ▶ while ($count <= $self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count <= $self->{"size"}) 0 0 0
82 0 ▶ if (! defined $iter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $iter) 0 0 0
83 0 die "undefined iterator on element \#$count (trying to get to size $self->{size})\n";
84 }
85 0 my $key = $iter->[KEY];
86 0 ▶ my $it_via_hash = $self->{items}->{$key} or
Branch: 1 branch
BranchTrueFalseTotal
my $it_via_hash = $self->{"items"}{$key} or die "item '${key}' found in list, but not in hash\n" 0 0 0
87 die "item '$key' found in list, but not in hash\n";
88  
89 0 ▶ unless ($it_via_hash == $iter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($it_via_hash == $iter) 0 0 0
90 0 die "Hash value of '$key' maps to different node than we found.\n";
91 }
92  
93 0 ▶ if ($count == 1 && $iter->[PREVREF]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1 and $iter->[0]) 0 0 0
Condition0%0/3
$count == 1 and $iter->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$count == 1 and $iter->[0]
$count == 1 $iter->[0]
Truth table0%0/3
A and B
A= $count == 1B= $iter->[0]
ABresult
0X0
100
111
94 0 die "Head element shouldn't have previous pointer!\n";
95 }
96 0 ▶ if ($count == $self->{size} && $iter->[NEXTREF]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $self->{"size"} and $iter->[2]) 0 0 0
Condition0%0/3
$count == $self->{"size"} and $iter->[2]
!ll&&!rl&&r
000
MC/DC0%0/2
$count == $self->{"size"} and $iter->[2]
$count == $self->{"size"} $iter->[2]
Truth table0%0/3
A and B
A= $count == $self->{"size"}B= $iter->[2]
ABresult
0X0
100
111
97 0 die "Last element shouldn't have next pointer!\n";
98 }
99 0 ▶ if ($iter->[NEXTREF] && $iter->[NEXTREF]->[PREVREF] != $iter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->[2] and $iter->[2][0] != $iter) 0 0 0
Condition0%0/3
$iter->[2] and $iter->[2][0] != $iter
!ll&&!rl&&r
000
MC/DC0%0/2
$iter->[2] and $iter->[2][0] != $iter
$iter->[2] $iter->[2][0] != $iter
Truth table0%0/3
A and B
A= $iter->[2]B= $iter->[2][0] != $iter
ABresult
0X0
100
111
100 0 die "next's previous should be us.\n";
101 }
102 0 ▶ if ($last && $iter->[PREVREF] != $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and $iter->[0] != $last) 0 0 0
Condition0%0/3
$last and $iter->[0] != $last
!ll&&!rl&&r
000
MC/DC0%0/2
$last and $iter->[0] != $last
$last $iter->[0] != $last
Truth table0%0/3
A and B
A= $lastB= $iter->[0] != $last
ABresult
0X0
100
111
103 0 die "defined \$last but its previous isn't us.\n";
104 }
105 0 ▶ if ($last && $last->[NEXTREF] != $iter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and $last->[2] != $iter) 0 0 0
Condition0%0/3
$last and $last->[2] != $iter
!ll&&!rl&&r
000
MC/DC0%0/2
$last and $last->[2] != $iter
$last $last->[2] != $iter
Truth table0%0/3
A and B
A= $lastB= $last->[2] != $iter
ABresult
0X0
100
111
106 0 die "defined \$last but our next isn't it\n";
107 }
108 0 ▶ if (!$last && $iter->[PREVREF]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $last and $iter->[0]) 0 0 0
Condition0%0/3
not $last and $iter->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
not $last and $iter->[0]
not $last $iter->[0]
Truth table0%0/3
not(A) and B
A= not $lastB= $iter->[0]
ABresult
0X0
100
111
109 0 die "uh, we have a nextref but shouldn't\n";
110 }
111  
112 0 $last = $iter;
113 0 $iter = $iter->[NEXTREF];
114 0 $count++;
115 }
116 0 return 1;
117 }
118  
119 0 ▶sub drop_tail {
Subroutine drop_tail: not called CC=5 SCAR=34.0
Pod undocumented
120 0 my Perlbal::Cache $self = shift;
121 0 ▶ die "no tail (size)" unless $self->{size};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"size"} 0 0 0
122  
123 ## who's going to die?
124 0 ▶ my $to_die = $self->{tail} or die "no tail (key)";
Branch: 1 branch
BranchTrueFalseTotal
my $to_die = $self->{"tail"} or die "no tail (key)" 0 0 0
125  
126 ## set the tail to the item before the one dying.
127 0 $self->{tail} = $self->{tail}->[PREVREF];
128  
129 ## adjust the forward pointer on the tail to be undef
130 0 ▶ if (defined $self->{tail}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tail"}) 0 0 0
131 0 $self->{tail}->[NEXTREF] = undef;
132 }
133  
134 ## kill the item
135 0 delete $self->{items}->{$to_die->[KEY]};
136  
137 ## shrink the overall size
138 0 $self->{size}--;
139  
140 0 ▶ if (!$self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"size"}) 0 0 0
141 0 $self->{head} = undef;
142 }
143 }
144  
145 32 ▶sub get {
Subroutine get: called CC=6 SCAR=29.2
Pod undocumented
146 32 my Perlbal::Cache $self = shift;
147 32 my ($key) = @_;
148  
149 32 ▶ my $item = $self->{items}{$key} or
Branch: 1 branch
BranchTrueFalseTotal
my $item = $self->{"items"}{$key} or return undef 9 23 32
150 return undef;
151  
152 # promote this to the head
153 9 ▶ unless ($self->{head} == $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"head"} == $item) 0 9 9
154 0 ▶ if ($self->{tail} == $item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tail"} == $item) 0 0 0
155 0 $self->{tail} = $item->[PREVREF];
156 }
157  
158 # remove this element from the linked list.
159 0 my $next = $item->[NEXTREF];
160 0 my $prev = $item->[PREVREF];
161 0 ▶ if ($next) { $next->[PREVREF] = $prev; }
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 0 0 0
162 0 ▶ if ($prev) { $prev->[NEXTREF] = $next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 0 0 0
163  
164 # make current head point backwards to this item
165 0 $self->{head}->[PREVREF] = $item;
166  
167 # make this item point forwards to current head, and backwards nowhere
168 0 $item->[NEXTREF] = $self->{head};
169 0 $item->[PREVREF] = undef;
170  
171 # make this the new head
172 0 $self->{head} = $item;
173 }
174  
175 9 return $item->[VALUE];
176 }
177  
178 0 ▶sub remove {
Subroutine remove: not called CC=5 SCAR=34.0
Pod undocumented
179 0 my Perlbal::Cache $self = shift;
180 0 my ($key) = @_;
181  
182 0 ▶ my $item = $self->{items}{$key} or
Branch: 1 branch
BranchTrueFalseTotal
my $item = $self->{"items"}{$key} or return 0 0 0 0
183 return 0;
184 0 delete $self->{items}{$key};
185 0 $self->{size}--;
186  
187 0 ▶ if (!$self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"size"}) 0 0 0
188 0 $self->{head} = undef;
189 0 $self->{tail} = undef;
190 0 return 1;
191 }
192  
193 0 ▶ if ($self->{head} == $item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"head"} == $item) 0 0 0
194 0 $self->{head} = $item->[NEXTREF];
195 0 $self->{head}->[PREVREF] = undef;
196 0 return 1;
197 }
198 0 ▶ if ($self->{tail} == $item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tail"} == $item) 0 0 0
199 0 $self->{tail} = $item->[PREVREF];
200 0 $self->{tail}->[NEXTREF] = undef;
201 0 return 1;
202 }
203  
204 # remove from middle
205 0 $item->[PREVREF]->[NEXTREF] = $item->[NEXTREF];
206 0 $item->[NEXTREF]->[PREVREF] = $item->[PREVREF];
207 0 return 1;
208  
209 }
210  
211 2 ▶sub set {
Subroutine set: called CC=6 SCAR=19.0
Pod undocumented
212 2 my Perlbal::Cache $self = shift;
213 2 my ($key, $value) = @_;
214  
215 $self->drop_tail while
216 $self->{maxsize} &&
217 $self->{size} >= $self->{maxsize} &&
218 2 ▶ ! exists $self->{items}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"} and not exists $self->{"items"}{$key} and do { $self->drop_tail } 0 2 2
Condition33%2/6
$self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"} and not exists $self->{"items"}{$key}
!ll&&!rl&&r
200
$self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"}
!ll&&!rl&&r
020
MC/DC0%0/3
$self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"} and not exists $self->{"items"}{$key}
$self->{"maxsize"} $self->{"size"} >= $self->{"maxsize"} not exists $self->{"items"}{$key}
Truth table25%1/4
A and B and not(C)
A= $self->{"maxsize"}B= $self->{"size"} >= $self->{"maxsize"}C= not exists $self->{"items"}{$key}
ABCresult
0XX0
10X0
1100
1111
219  
220 2 ▶ if (exists $self->{items}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'items'}{$key}) { } 1 1 2
221 # update the value
222 1 my $it = $self->{items}->{$key};
223 1 $it->[VALUE] = $value;
224 } else {
225 # stick it at the end, for now
226 1 my $it = $self->{items}->{$key} = [];
227 1 $it->[PREVREF] = undef;
228 1 $it->[NEXTREF] = undef;
229 1 $it->[KEY] = $key;
230 1 $it->[VALUE] = $value;
231 1 ▶ if ($self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'size'}) { } 0 1 1
232 0 $self->{tail}->[NEXTREF] = $it;
233 0 $it->[PREVREF] = $self->{tail};
234 } else {
235 1 $self->{head} = $it;
236 }
237 1 $self->{tail} = $it;
238 1 $self->{size}++;
239 }
240  
241 # this will promote it to the top:
242 2 $self->get($key);
243 }
244  
245 22 1;
↑ Summary Perlbal/Plugin/AccessControl.pm »