| 1 | package CHI::Driver::Role::IsSizeAware; | ||||||||||||||||||||||||||||
| 2 | 20 | $CHI::Driver::Role::IsSizeAware::VERSION = '0.59'; | |||||||||||||||||||||||||||
| 3 | 20 | ▶ | use Carp::Assert; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use CHI::Types qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 20 | 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 | 20 | 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 | 20 | ▶ | use constant Size_Key => 'CHI_IsSizeAware_size'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub _build_discard_policy { | ||||||||||||||||||||||||||||
| 18 | 46 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_discard_policy: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 46 | ▶ | return $self->can('default_discard_policy') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | ? $self->default_discard_policy | ||||||||||||||||||||||||||||
| 22 | : 'arbitrary'; | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | after 'BUILD_roles' => sub { | ||||||||||||||||||||||||||||
| 26 | 161 | ▶ | my ( $self, $params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 161 | $self->{is_size_aware} = 1; | |||||||||||||||||||||||||||
| 29 | 20 | }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | after 'clear' => sub { | ||||||||||||||||||||||||||||
| 32 | 128 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 128 | $self->_set_size(0); | |||||||||||||||||||||||||||
| 35 | 20 | }; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | around 'remove' => sub { | ||||||||||||||||||||||||||||
| 38 | 998 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 39 | 998 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 998 | my ($key) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 998 | my ( $size_delta, $obj ); | |||||||||||||||||||||||||||
| 43 | 998 | ▶ | if ( !$self->{_no_set_size_on_remove} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"_no_set_size_on_remove"} and $obj = $self->get_object($key)
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)
| |||||||||||||||||||||||||||||
| 44 | && ( $obj = $self->get_object($key) ) ) | ||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 34 | $size_delta = -1 * $obj->size; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 998 | $self->$orig(@_); | |||||||||||||||||||||||||||
| 49 | 998 | ▶ | if ($size_delta) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 34 | $self->_add_to_size($size_delta); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 20 | }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | around 'set_object' => sub { | ||||||||||||||||||||||||||||
| 55 | 1302 | ▶ | my ( $orig, $self, $key, $obj ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # If item exists, record its size so we can subtract it below | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | 1302 | my $size_delta = 0; | |||||||||||||||||||||||||||
| 60 | 1302 | ▶ | if ( my $obj = $self->get_object($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 64 | $size_delta = -1 * $obj->size; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1302 | my $result = $self->$orig( $key, $obj ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Add to size and reduce size if over the maximum | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | 1302 | $size_delta += $obj->size; | |||||||||||||||||||||||||||
| 69 | 1302 | my $namespace_size = $self->_add_to_size($size_delta); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1302 | ▶ | if ( defined( $self->max_size ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->max_size and $namespace_size > $self->max_size
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
| |||||||||||||||||||||||||||||
| 72 | && $namespace_size > $self->max_size ) | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 267 | $self->discard_to_size( | |||||||||||||||||||||||||||
| 75 | $self->max_size * $self->max_size_reduction_factor ); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1302 | return $result; | |||||||||||||||||||||||||||
| 79 | 20 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub get_size { | ||||||||||||||||||||||||||||
| 82 | 2283 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_size: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2283 | ▶ | my $size = $self->metacache->get(Size_Key) || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->metacache->get('CHI_IsSizeAware_size') || 0
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')
| |||||||||||||||||||||||||||||
| 85 | 2283 | return $size; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub _set_size { | ||||||||||||||||||||||||||||
| 89 | 1801 | ▶ | my ( $self, $new_size ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_size: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1801 | $self->metacache->set( Size_Key, $new_size ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub _add_to_size { | ||||||||||||||||||||||||||||
| 95 | 1336 | ▶ | my ( $self, $incr ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_add_to_size: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Non-atomic, so may be inaccurate over time | ||||||||||||||||||||||||||||
| 98 | 1336 | ▶ | my $new_size = ( $self->get_size || 0 ) + $incr; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->get_size || 0
MC/DC100%1/1
$self->get_size || 0
$self->get_size
Truth table100%2/2
A
A= $self->get_size
| |||||||||||||||||||||||||||||
| 99 | 1336 | $self->_set_size($new_size); | |||||||||||||||||||||||||||
| 100 | 1336 | return $new_size; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub discard_to_size { | ||||||||||||||||||||||||||||
| 104 | 337 | ▶ | my ( $self, $ceiling ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
discard_to_size: called CC=6 SCAR=19.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Get an iterator that produces keys in the order they should be removed | ||||||||||||||||||||||||||||
| 107 | # | ||||||||||||||||||||||||||||
| 108 | 337 | 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 | 337 | my $end_time = time + $self->discard_timeout; | |||||||||||||||||||||||||||
| 116 | 337 | local $self->{_no_set_size_on_remove} = 1; | |||||||||||||||||||||||||||
| 117 | 337 | my $size = $self->get_size(); | |||||||||||||||||||||||||||
| 118 | 337 | eval { | |||||||||||||||||||||||||||
| 119 | 337 | ▶ | while ( $size > $ceiling ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 905 | ▶ | if ( defined( my $key = $discard_iterator->() ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 905 | ▶ | if ( my $obj = $self->get_object($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 905 | $self->remove($key); | |||||||||||||||||||||||||||
| 123 | 905 | $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 | 905 | ▶ | if ( time > $end_time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | die sprintf( "discard timeout (%s sec) reached", | |||||||||||||||||||||||||||
| 133 | $self->discard_timeout ); | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||
| 137 | 337 | $self->_set_size($size); | |||||||||||||||||||||||||||
| 138 | 337 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub _get_iterator_for_discard_policy { | ||||||||||||||||||||||||||||
| 142 | 337 | ▶ | my ( $self, $discard_policy ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_iterator_for_discard_policy: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 337 | ▶ | if ( ref($discard_policy) eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 70 | return $discard_policy->($self); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||
| 148 | 267 | my $discard_policy_sub = "discard_policy_" . $discard_policy; | |||||||||||||||||||||||||||
| 149 | 267 | ▶ | if ( $self->can($discard_policy_sub) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 267 | 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 | sub discard_policy_arbitrary { | ||||||||||||||||||||||||||||
| 160 | 62 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
discard_policy_arbitrary: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 62 | return $self->get_keys_iterator(); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 20 | 1; | |||||||||||||||||||||||||||