Ref/CLOCK/Base.pm

filter: Statementstmt 100% 67 / 67 Branchbran 94.4% 17 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 6 totaltotal 90.3% 103 / 114 CC 14 SCAR 10.7
CC
14
Cov
97%
expire
5.0
get
4.0
_find_free_slot
4.0
CRAP 2.9
« Ref/Role/WithDoublyLinkedList.pm ↑ Summary Ref/Role/API.pm »
1 package Cache::Ref::CLOCK::Base;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Cache::Ref::CLOCK::Base::AUTHORITY = 'cpan:NUFFIN';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Cache::Ref::CLOCK::Base::VERSION = '0.05'; # TRIAL
7 }
8 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with qw(
13 Cache::Ref::Role::API
14 Cache::Ref::Role::Index
15 );
16  
17 1 requires qw(_hit);
18  
19 1 has size => (
20 isa => "Int",
21 is => "ro",
22 required => 1,
23 );
24  
25 has _hand => (
26 isa => "ScalarRef",
27 is => "ro",
28 4 ▶ default => sub { my $x = -1; return \$x },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 1 );
30  
31 has _buffer => (
32 isa => "ArrayRef",
33 is => "ro",
34 lazy => 1,
35 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 4 my ( $self, $p ) = @_;
37 4 return [ map { [] } 1 .. $self->size ],
38 },
39 1 );
40  
41 5 ▶sub hit {
Subroutine hit: called CC=1 SCAR=0.0
Pod undocumented
42 5 my ( $self, @keys ) = @_;
43  
44 5 $self->_hit( [ grep { defined } $self->_index_get(@keys) ] );
45  
46 5 return;
47 }
48  
49 6050 ▶sub get {
Subroutine get: called CC=4 SCAR=14.0
Pod undocumented
50 6050 my ( $self, @keys ) = @_;
51  
52 6050 my @ret;
53  
54 6050 my @entries = $self->_index_get(@keys);
55  
56 6050 $self->_hit( [ grep { defined } @entries ] );
57  
58 6050 ▶ return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries );
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 ? : 6048 2 6050
Condition66%4/6
$entries[0] and $entries[0][2]
!ll&&!rl&&r
348502563
$_ && $_->[2]
!ll&&!rl&&r
204
MC/DC50%1/2
$entries[0] and $entries[0][2]
$entries[0] $entries[0][2]
MC/DC50%1/2
$_ && $_->[2]
$_ $_->[2]
Truth table66%2/3
A and B
A= $entries[0]B= $entries[0][2]
ABresult
0X0
100
111
Truth table66%2/3
A && B
A= $_B= $_->[2]
ABresult
0X0
100
111
59 }
60  
61 2 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod undocumented
62 2 my $self = shift;
63 2 $self->_index_clear;
64 2 @$_ = () for @{ $self->_buffer };
65 }
66  
67 3473 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod undocumented
68 3473 my ( $self, @keys ) = @_;
69 3473 @$_ = () for $self->_index_delete(@keys);
70 }
71  
72 3487 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod undocumented
73 3487 my ( $self, $key, $value ) = @_;
74  
75 3487 ▶ if ( my $e = $self->_index_get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $self->_index_get($key)) { } 2 3485 3487
76 2 $e->[2] = $value;
77 } else {
78 3485 my $e = $self->_find_free_slot;
79 3485 @$e = ( 0, $key, $value ); # start at 0, not k
80 3485 $self->_index_set( $key, $e );
81 }
82 }
83  
84 2 ▶sub expire {
Subroutine expire: called CC=5 SCAR=16.1
Pod undocumented
85 2 my ( $self, $how_many ) = @_;
86  
87 2 my $i = $self->_hand;
88 2 my $b = $self->_buffer;
89  
90 2 ▶ while ( $how_many ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($how_many) 13 2 15
91 13 ▶ if ( $$i == $#$b ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$i == $b->$#*) 4 9 13
92 4 $$i = -1;
93 }
94  
95 13 ▶ if ( my $e = $b->[++$$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $$b[++$$i]) 13 0 13
96 13 ▶ if ( !$e->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $e->[0]) { } 4 9 13
97 4 $self->remove($e->[1]); # also clears @$e
98 4 $how_many--;
99 } else {
100 9 $e->[0]--;
101 }
102 }
103 }
104  
105 2 return;
106 }
107  
108 3485 ▶sub _find_free_slot {
Subroutine _find_free_slot: called CC=4 SCAR=13.9
109 3485 my $self = shift;
110  
111 3485 my $i = $self->_hand;
112 3485 my $b = $self->_buffer;
113  
114 loop: {
115 3485 ▶ if ( $$i == $#$b ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$i == $b->$#*) 1111 4432 5543
116 1111 $$i = -1;
117 }
118  
119 5543 my $e = $b->[++$$i];
120  
121 5543 ▶ if ( not @$e ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @$e) { } 18 5525 5543
elsif (not $e->[0]) { } 3467 2058 5525
122 18 return $e;
123 } elsif ( !$e->[0] ) {
124 3467 $self->remove($e->[1]); # also clears @$e
125 3467 return $e;
126 } else {
127 2058 $e->[0]--;
128 2058 redo loop;
129 }
130 }
131 }
132  
133 # ex: set sw=4 et:
134  
135 1 __PACKAGE__;
136  
137  
138 __END__
« Ref/Role/WithDoublyLinkedList.pm ↑ Summary Ref/Role/API.pm »