CHI/Driver/Role/IsSizeAware.pm

filter: Statementstmt 94.6% 89 / 94 Branchbran 79.1% 19 / 24 Conditioncond 100% 10 / 10 MC/DCmcdc 100% 6 / 6 Subroutinesub 95.2% 20 / 21 Podpod 0.0% 0 / 3 totaltotal 91.1% 144 / 158 CC 17 SCAR 10.8
CC
17
Cov
92%
discard_to_size
6.8
__ANON__
4.0
__ANON__
4.0
CRAP 3.0
« CHI/Driver/FastMmap.pm ↑ Summary CHI/Driver/Role/HasSubcaches.pm »
1 package CHI::Driver::Role::IsSizeAware;
2 19 $CHI::Driver::Role::IsSizeAware::VERSION = '0.61';
3 19 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use CHI::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 19 has 'discard_policy' => ( is => 'lazy', isa => Maybe[DiscardPolicy] );
11 161 ▶has 'discard_timeout' => ( is => 'rw', isa => Num, default => sub { 10 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 19 has 'max_size' => ( is => 'rw', isa => MemorySize, coerce => \&to_MemorySize );
13 161 ▶has 'max_size_reduction_factor' => ( is => 'rw', isa => Num, default => sub { 0.8 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 19 ▶use constant Size_Key => 'CHI_IsSizeAware_size';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 46 ▶sub _build_discard_policy {
Subroutine _build_discard_policy: called CC=2 SCAR=6.9
18 46 my $self = shift;
19  
20 46 ▶ return $self->can('default_discard_policy')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('default_discard_policy') ? : 29 17 46
21 ? $self->default_discard_policy
22 : 'arbitrary';
23 }
24  
25 161 ▶after 'BUILD_roles' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 161 my ( $self, $params ) = @_;
27  
28 161 $self->{is_size_aware} = 1;
29 19 };
30  
31 128 ▶after 'clear' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 128 my $self = shift;
33  
34 128 $self->_set_size(0);
35 19 };
36  
37 988 ▶around 'remove' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
38 988 my $orig = shift;
39 988 my $self = shift;
40 988 my ($key) = @_;
41  
42 988 my ( $size_delta, $obj );
43 988 ▶ if ( !$self->{_no_set_size_on_remove}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_no_set_size_on_remove"} and $obj = $self->get_object($key)) 34 954 988
Condition100%3/3
not $self->{"_no_set_size_on_remove"} and $obj = $self->get_object($key)
!ll&&!rl&&r
8955934
MC/DC100%2/2
not $self->{"_no_set_size_on_remove"} and $obj = $self->get_object($key)
not $self->{"_no_set_size_on_remove"} $obj = $self->get_object($key)
Truth table100%3/3
not(A) and B
A= not $self->{"_no_set_size_on_remove"}B= $obj = $self->get_object($key)
ABresult
0X0
100
111
44 && ( $obj = $self->get_object($key) ) )
45 {
46 34 $size_delta = -1 * $obj->size;
47 }
48 988 $self->$orig(@_);
49 988 ▶ if ($size_delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_delta) 34 954 988
50 34 $self->_add_to_size($size_delta);
51 }
52 19 };
53  
54 1305 ▶around 'set_object' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
55 1305 my ( $orig, $self, $key, $obj ) = @_;
56  
57 # If item exists, record its size so we can subtract it below
58 #
59 1305 my $size_delta = 0;
60 1305 ▶ if ( my $obj = $self->get_object($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_object($key)) 74 1231 1305
61 74 $size_delta = -1 * $obj->size;
62 }
63  
64 1305 my $result = $self->$orig( $key, $obj );
65  
66 # Add to size and reduce size if over the maximum
67 #
68 1305 $size_delta += $obj->size;
69 1305 my $namespace_size = $self->_add_to_size($size_delta);
70  
71 1305 ▶ if ( defined( $self->max_size )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->max_size and $namespace_size > $self->max_size) 262 1043 1305
Condition100%3/3
defined $self->max_size and $namespace_size > $self->max_size
!ll&&!rl&&r
399644262
MC/DC100%2/2
defined $self->max_size and $namespace_size > $self->max_size
defined $self->max_size $namespace_size > $self->max_size
Truth table100%3/3
A and B
A= defined $self->max_sizeB= $namespace_size > $self->max_size
ABresult
0X0
100
111
72 && $namespace_size > $self->max_size )
73 {
74 262 $self->discard_to_size(
75 $self->max_size * $self->max_size_reduction_factor );
76 }
77  
78 1305 return $result;
79 19 };
80  
81 2281 ▶sub get_size {
Subroutine get_size: called CC=2 SCAR=6.9
Pod undocumented
82 2281 my ($self) = @_;
83  
84 2281 ▶ my $size = $self->metacache->get(Size_Key) || 0;
Condition100%2/2
$self->metacache->get('CHI_IsSizeAware_size') || 0
l!l
2138143
MC/DC100%1/1
$self->metacache->get('CHI_IsSizeAware_size') || 0
$self->metacache->get('CHI_IsSizeAware_size')
Truth table100%2/2
A
A= $self->metacache->get('CHI_IsSizeAware_size')
Aresult
11
00
85 2281 return $size;
86 }
87  
88 1799 ▶sub _set_size {
Subroutine _set_size: called CC=1 SCAR=0.0
89 1799 my ( $self, $new_size ) = @_;
90  
91 1799 $self->metacache->set( Size_Key, $new_size );
92 }
93  
94 1339 ▶sub _add_to_size {
Subroutine _add_to_size: called CC=2 SCAR=6.9
95 1339 my ( $self, $incr ) = @_;
96  
97 # Non-atomic, so may be inaccurate over time
98 1339 ▶ my $new_size = ( $self->get_size || 0 ) + $incr;
Condition100%2/2
$self->get_size || 0
l!l
1228111
MC/DC100%1/1
$self->get_size || 0
$self->get_size
Truth table100%2/2
A
A= $self->get_size
Aresult
11
00
99 1339 $self->_set_size($new_size);
100 1339 return $new_size;
101 }
102  
103 332 ▶sub discard_to_size {
Subroutine discard_to_size: called CC=6 SCAR=19.2
Pod undocumented
104 332 my ( $self, $ceiling ) = @_;
105  
106 # Get an iterator that produces keys in the order they should be removed
107 #
108 332 my $discard_iterator =
109 $self->_get_iterator_for_discard_policy( $self->discard_policy );
110  
111 # Remove keys until we are under $ceiling. Temporarily turn off size
112 # setting on remove because we will set size once at end. Check if
113 # we exceed discard timeout.
114 #
115 332 my $end_time = time + $self->discard_timeout;
116 332 local $self->{_no_set_size_on_remove} = 1;
117 332 my $size = $self->get_size();
118 332 eval {
119 332 ▶ while ( $size > $ceiling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $ceiling) 895 332 1227
120 895 ▶ if ( defined( my $key = $discard_iterator->() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = &$discard_iterator())) { } 895 0 895
121 895 ▶ if ( my $obj = $self->get_object($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_object($key)) 895 0 895
122 895 $self->remove($key);
123 895 $size -= $obj->size;
124 }
125 }
126 else {
127 0 ▶ affirm { $self->is_empty }
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 "iterator returned undef, cache should be empty";
129 0 last;
130 }
131 895 ▶ if ( time > $end_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time > $end_time) 0 895 895
132 0 die sprintf( "discard timeout (%s sec) reached",
133 $self->discard_timeout );
134 }
135 }
136 };
137 332 $self->_set_size($size);
138 332 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 332 332
139 }
140  
141 332 ▶sub _get_iterator_for_discard_policy {
Subroutine _get_iterator_for_discard_policy: called CC=3 SCAR=11.2
142 332 my ( $self, $discard_policy ) = @_;
143  
144 332 ▶ if ( ref($discard_policy) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $discard_policy eq 'CODE') { } 70 262 332
145 70 return $discard_policy->($self);
146 }
147 else {
148 262 my $discard_policy_sub = "discard_policy_" . $discard_policy;
149 262 ▶ if ( $self->can($discard_policy_sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($discard_policy_sub)) { } 262 0 262
150 262 return $self->$discard_policy_sub();
151 }
152 else {
153 0 die sprintf( "cannot get iterator for discard policy '%s' ('%s')",
154 $discard_policy, $discard_policy_sub );
155 }
156 }
157 }
158  
159 58 ▶sub discard_policy_arbitrary {
Subroutine discard_policy_arbitrary: called CC=1 SCAR=0.0
Pod undocumented
160 58 my ($self) = @_;
161  
162 58 return $self->get_keys_iterator();
163 }
164  
165 19 1;
« CHI/Driver/FastMmap.pm ↑ Summary CHI/Driver/Role/HasSubcaches.pm »