LRU/LinkedList.pm

filter: Statementstmt 85.4% 117 / 137 Branchbran 64.2% 27 / 42 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.4% 19 / 21 Podpod 100% 2 / 2 totaltotal 79.3% 173 / 218 CC 25 SCAR 13.4
CC
25
Cov
79%
_cull
10.8
_promote
6.1
max_size
4.1
CRAP 3.8
↑ Summary LRU/Array.pm »
1 package Tie::Cache::LRU::LinkedList;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw(Tie::Cache::LRU::Virtual);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use constant SUCCESS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use constant FAILURE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Node members.
13 2 ▶use enum qw(KEY VALUE PREV NEXT);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 =pod
17  
18 =head1 NAME
19  
20 Tie::Cache::LRU::LinkedList - Tie::Cache::LRU implemented using a linked list
21  
22 =head1 SYNOPSIS
23  
24 use Tie::Cache::LRU::LinkedList;
25  
26 tie %cache, 'Tie::Cache::LRU', 500;
27  
28 ...the rest is as Tie::Cache::LRU...
29  
30 =head1 DESCRIPTION
31  
32 This is an implementation of Tie::Cache::LRU using a linked list
33 structure. Theoretically, this is an efficient algorithm, however it
34 may be lose out in smaller cache sizes (where small <= ??) due to its
35 relatively high constant.
36  
37 =cut
38  
39 1 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=7.0
40 1 my($class, $max_size) = @_;
41 1 my $self = bless {}, $class;
42  
43 1 ▶ $max_size = $class->DEFAULT_MAX_SIZE unless defined $max_size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_size 0 1 1
44  
45 1 $self->_init;
46 1 $self->max_size($max_size);
47  
48 1 return $self;
49 }
50  
51  
52 4 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.0
53 4 my($self, $key) = @_;
54  
55 4 ▶ return unless $self->EXISTS($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->EXISTS($key) 0 4 4
56  
57 4 my $node = $self->{index}{$key};
58 4 $self->_promote($node);
59 4 return $node->[VALUE];
60 }
61  
62  
63 2 ▶sub STORE {
Subroutine STORE: called CC=4 SCAR=14.0
64 2 my($self, $key, $value) = @_;
65  
66 2 ▶ if( $self->EXISTS($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->EXISTS($key)) { } 0 2 2
67 0 my $node = $self->{index}{$key};
68 0 $node->[VALUE] = $value;
69 0 $self->_promote($node);
70 }
71 else {
72 2 my $node = [];
73 2 @{$node}[KEY, VALUE] = ($key, $value);
74  
75 ### Might it be smarter to just attach the new node to the list
76 ### and call _promote()?
77 # Make ourselves the freshest.
78 2 ▶ if(defined $self->{freshest} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'freshest'}) { } 1 1 2
79 1 $self->{freshest}->[NEXT] = $node;
80 1 $node->[PREV] = $self->{freshest};
81 }
82 else {
83 1 assert($self->{size} == 0);
84 }
85 2 $self->{freshest} = $node;
86
87 # If we're the first node, we are stinky, too.
88 2 ▶ unless( defined $self->{stinkiest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"stinkiest"}) 1 1 2
89 1 assert($self->{size} == 0);
90 1 $self->{stinkiest} = $node;
91 }
92 2 $self->{size}++;
93 2 $self->{index}{$key} = $node;
94 2 $self->_cull;
95 }
96 2 return SUCCESS;
97 }
98  
99  
100 8 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
101 8 my($self, $key) = @_;
102  
103 8 return exists $self->{index}{$key};
104 }
105  
106  
107 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
108 0 my($self) = @_;
109 0 $self->_init;
110 }
111  
112  
113 2 ▶sub DELETE {
Subroutine DELETE: called CC=4 SCAR=14.0
114 2 my($self, $key) = @_;
115  
116 2 ▶ return unless $self->EXISTS($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->EXISTS($key) 0 2 2
117  
118 2 my $node = $self->{index}{$key};
119 2 ▶ $self->{freshest} = $node->[PREV] if $self->{freshest} == $node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"freshest"} == $node 2 0 2
120 2 ▶ $self->{stinkiest} = $node->[NEXT] if $self->{stinkiest} == $node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stinkiest"} == $node 1 1 2
121 2 $self->_yank($node);
122 2 delete $self->{index}{$key};
123
124 2 $self->{size}--;
125
126 2 return $node->[VALUE];
127 }
128  
129  
130 # keys() should return most to least recent.
131 3 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=3 SCAR=11.1
132 3 my($self) = shift;
133 3 my $node = $self->{freshest};
134 3 ▶ assert($self->{size} == 0 xor defined $node);
Condition50%2/4
$self->{"size"} == 0 ^^ defined $node
l&&rl&&!r!l&&r!l&&!r
0120
MC/DC0%0/2
$self->{"size"} == 0 ^^ defined $node
$self->{"size"} == 0 defined $node
Truth table50%2/4
A ^^ B
A= $self->{"size"} == 0B= defined $node
ABresult
110
101
011
000
135  
136 3 my @nodes;
137 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $node and do { do { push @nodes, $node; $node = $node->[2] } } 2 3 5
138 5 push @nodes, $node;
139 5 $node = $node->[PREV];
140 } while defined $node;
141  
142 3 $self->{nodes} = \@nodes;
143 3 $self->NEXTKEY();
144 }
145  
146 7 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
147 7 my $self = shift;
148 7 my $node = shift @{$self->{nodes}};
149 7 return $node->[KEY];
150 }
151  
152  
153 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
154 1 my($self) = shift;
155  
156 # The chain must be broken.
157 1 $self->_init;
158
159 1 return SUCCESS;
160 }
161  
162  
163 3 ▶sub max_size {
Subroutine max_size: called CC=4 SCAR=14.2
Pod documented
164 3 my($self) = shift;
165  
166 3 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 2 3
167 1 my ($new_max_size) = shift;
168 1 ▶ assert(defined $new_max_size && $new_max_size !~ /\D/);
Condition33%1/3
defined $new_max_size && !($new_max_size =~ /\D/)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $new_max_size && !($new_max_size =~ /\D/)
defined $new_max_size !($new_max_size =~ /\D/)
Truth table33%1/3
A && not(B)
A= defined $new_max_sizeB= !($new_max_size =~ /\D/)
ABresult
0X0
100
111
169 1 $self->{max_size} = $new_max_size;
170  
171 # Immediately purge the cache if necessary.
172 1 ▶ $self->_cull if $self->{size} > $new_max_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"size"} > $new_max_size 0 1 1
173  
174 1 return SUCCESS;
175 }
176 else {
177 2 return $self->{max_size};
178 }
179 }
180  
181  
182 0 ▶sub curr_size {
Subroutine curr_size: not called CC=1 SCAR=6.9
Pod documented
183 0 my($self) = shift;
184  
185 # We brook no arguments.
186 0 assert(!@_);
187  
188 0 return $self->{size};
189 }
190  
191  
192 2 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.2
193 2 my($self) = shift;
194  
195 # The cache is a chain. We must break up its structure so Perl
196 # can GC it.
197 2 ▶ while( my($key, $node) = each %{$self->{index}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"index"};}) 0 2 2
198 0 $node->[NEXT] = undef;
199 0 $node->[PREV] = undef;
200 }
201  
202 2 $self->{freshest} = undef;
203 2 $self->{stinkiest} = undef;
204 2 $self->{index} = {};
205 2 $self->{size} = 0;
206 2 $self->{nodes} = [];
207  
208 2 return SUCCESS;
209 }
210  
211  
212 3 ▶sub _yank {
Subroutine _yank: called CC=3 SCAR=11.0
213 3 my($self, $node) = @_;
214
215 3 my $prev_node = $node->[PREV];
216 3 my $next_node = $node->[NEXT];
217 3 ▶ $prev_node->[NEXT] = $next_node if defined $prev_node;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_node 1 2 3
218 3 ▶ $next_node->[PREV] = $prev_node if defined $next_node;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_node 1 2 3
219  
220 3 $node->[NEXT] = undef;
221 3 $node->[PREV] = undef;
222  
223 3 return SUCCESS;
224 }
225  
226  
227 4 ▶sub _promote {
Subroutine _promote: called CC=6 SCAR=18.0
228 4 my($self, $node) = @_;
229  
230 # _promote can take a node or a key. Get the node from the key.
231 4 ▶ $node = $self->{index}{$node} unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 4 4
232 4 ▶ return unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 4 4
233  
234 # Don't bother if there's only one node, or if this node is
235 # already the freshest.
236 4 ▶ return if $self->{size} == 1 or $self->{freshest} == $node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"size"} == 1 or $self->{"freshest"} == $node 3 1 4
Condition100%3/3
$self->{"size"} == 1 or $self->{"freshest"} == $node
l!l&&r!l&&!r
121
MC/DC100%2/2
$self->{"size"} == 1 or $self->{"freshest"} == $node
$self->{"size"} == 1 $self->{"freshest"} == $node
Truth table100%3/3
A or B
A= $self->{"size"} == 1B= $self->{"freshest"} == $node
ABresult
1X1
011
000
237  
238 # On the off chance that we're about to promote the stinkiest node,
239 # make sure the stinkiest pointer is updated.
240 1 ▶ if( $self->{stinkiest} == $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stinkiest"} == $node) 1 0 1
241 1 assert(not defined $node->[PREV]);
242 1 $self->{stinkiest} = $node->[NEXT];
243 }
244  
245 # Pull the $node out of its position.
246 1 $self->_yank($node);
247  
248 # Place the $node at the head.
249 1 my $old_head = $self->{freshest};
250 1 $old_head->[NEXT] = $node;
251 1 $node->[PREV] = $old_head;
252 1 $node->[NEXT] = undef;
253  
254 1 $self->{freshest} = $node;
255  
256 1 return SUCCESS;
257 }
258  
259  
260 2 ▶sub _cull {
Subroutine _cull: called CC=4 SCAR=23.7
261 2 my($self) = @_;
262
263 2 my $max_size = $self->max_size;
264  
265 2 ▶ for( ;$self->{size} > $max_size; $self->{size}-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"size"} > $max_size) 0 2 2
266 0 my $rotten = $self->{stinkiest};
267 0 assert(!defined $rotten->[PREV]);
268 0 my $new_stink = $rotten->[NEXT];
269
270 0 $rotten->[NEXT] = undef;
271
272 # Gotta watch out for autoviv.
273 0 ▶ $new_stink->[PREV] = undef if defined $new_stink;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_stink 0 0 0
274
275 0 $self->{stinkiest} = $new_stink;
276 0 ▶ if( $self->{freshest} eq $rotten ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"freshest"} eq $rotten) 0 0 0
277 0 assert( $self->{size} == 1 ) if DEBUG;
278 0 $self->{freshest} = $new_stink;
279 }
280  
281 0 delete $self->{index}{$rotten->[KEY]};
282 }
283
284 2 return SUCCESS;
285 }
286  
287  
288 =pod
289  
290 =head1 AUTHOR
291  
292 Michael G Schwern <schwern@pobox.com>
293  
294 =head1 SEE ALSO
295  
296 L<Tie::Cache::LRU>, L<Tie::Cache::LRU::LinkedList>,
297 L<Tie::Cache::LRU::Virtual>, L<Tie::Cache>
298  
299 =cut
300  
301 2 1;
302  
↑ Summary LRU/Array.pm »