| 1 | package Tie::Hash::Expire; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use POSIX qw/ceil/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use vars qw($VERSION $HI_RES_AVAILABLE); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | eval "use Time::HiRes qw/time/"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | unless($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 2 | $HI_RES_AVAILABLE = 1; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | $Tie::Hash::Expire::clean_int = 180; # Maybe later, the user can set this. | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 22 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 5 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # TODO: What do we do without $args->{expire_seconds} | ||||||||||||||||||||||||||||
| 26 | 5 | ▶ | unless(exists $args->{expire_seconds}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 1 | carp "hash tied to Tie::Hash::Expire without specifying expire_seconds. Hash keys will not expire."; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 5 | ▶ | if(!$HI_RES_AVAILABLE and $args->{expire_seconds} =~ /\.\d+/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $Tie::Hash::Expire::HI_RES_AVAILABLE and $args->{"expire_seconds"} =~ /\.\d+/
MC/DC0%0/2
not $Tie::Hash::Expire::HI_RES_AVAILABLE and $args->{"expire_seconds"} =~ /\.\d+/
not $Tie::Hash::Expire::HI_RES_AVAILABLE
$args->{"expire_seconds"} =~ /\.\d+/
Truth table33%1/3
not(A) and B
A= not $Tie::Hash::Expire::HI_RES_AVAILABLEB= $args->{"expire_seconds"} =~ /\.\d+/
| |||||||||||||||||||||||||||||
| 30 | 0 | carp "expire_seconds appears to be a decimal number, but Time::HiRes is not available."; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $self = { | ||||||||||||||||||||||||||||
| 34 | 'last_clean' => time, | ||||||||||||||||||||||||||||
| 35 | 'clean_int' => $Tie::Hash::Expire::clean_int, | ||||||||||||||||||||||||||||
| 36 | 'hash' => {}, | ||||||||||||||||||||||||||||
| 37 | 'array' => [], | ||||||||||||||||||||||||||||
| 38 | 'lifespan' => $args->{expire_seconds}, | ||||||||||||||||||||||||||||
| 39 | 5 | }; | |||||||||||||||||||||||||||
| 40 | 5 | bless $self, $class; | |||||||||||||||||||||||||||
| 41 | 5 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 17 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 17 | my $key = shift; | |||||||||||||||||||||||||||
| 48 | 17 | my $value = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 17 | my $time = time; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 17 | $self->maybe_clean(); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 17 | $self->DELETE($key); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Insert it on the end. | ||||||||||||||||||||||||||||
| 57 | 17 | push @{$self->{array}}, [$time,$key,$value]; | |||||||||||||||||||||||||||
| 58 | 17 | $self->{hash}->{$key} = $#{$self->{array}}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 26 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 26 | my $key = shift; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 26 | $self->maybe_clean(); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 26 | ▶ | if(exists $self->{hash}->{$key}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | # It exists, but may be expired. | ||||||||||||||||||||||||||||
| 71 | 24 | my $time = time; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 24 | my $index = $self->{hash}->{$key}; | |||||||||||||||||||||||||||
| 74 | 24 | ▶ | if((defined $self->{lifespan}) and $time - $self->{array}->[$index]->[0] >= $self->{lifespan}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"lifespan"} and $time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
MC/DC100%2/2
defined $self->{"lifespan"} and $time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
defined $self->{"lifespan"}
$time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
Truth table100%3/3
A and B
A= defined $self->{"lifespan"}B= $time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
| |||||||||||||||||||||||||||||
| 75 | # It is expired. | ||||||||||||||||||||||||||||
| 76 | 2 | $self->chop_hash($index); | |||||||||||||||||||||||||||
| 77 | 2 | return undef; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | # It is not expired. | ||||||||||||||||||||||||||||
| 80 | 22 | return $self->{array}->[$index]->[2]; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 2 | return undef; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 2 | my $key = shift; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | $self->maybe_clean(); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | if(exists $self->{hash}->{$key}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | # It exists, but may be expired. | ||||||||||||||||||||||||||||
| 95 | 2 | my $time = time; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | my $index = $self->{hash}->{$key}; | |||||||||||||||||||||||||||
| 98 | 2 | ▶ | if(defined $self->{lifespan} and $time - $self->{array}->[$index]->[0] >= $self->{lifespan}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"lifespan"} and $time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
MC/DC50%1/2
defined $self->{"lifespan"} and $time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
defined $self->{"lifespan"}
$time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
Truth table66%2/3
A and B
A= defined $self->{"lifespan"}B= $time - $self->{"array"}[$index][0] >= $self->{"lifespan"}
| |||||||||||||||||||||||||||||
| 99 | # It is expired. | ||||||||||||||||||||||||||||
| 100 | 1 | $self->chop_hash($index); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | return exists $self->{hash}->{$key}; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 18 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 18 | my $key = shift; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 18 | $self->maybe_clean(); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 18 | ▶ | if(exists($self->{hash}->{$key})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 2 | splice @{$self->{array}}, $self->{hash}->{$key},1; | |||||||||||||||||||||||||||
| 117 | 2 | $self->rebuild_hash(); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | $self->{hash} = {}; | |||||||||||||||||||||||||||
| 126 | 3 | $self->{array} = []; | |||||||||||||||||||||||||||
| 127 | 3 | $self->{last_clean} = time; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 8 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 8 | $self->clean_house(); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 8 | ▶ | if(scalar @{$self->{array}}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 7 | my $key = $self->{array}->[0]->[1]; | |||||||||||||||||||||||||||
| 138 | 7 | $self->{curr_key} = 0; | |||||||||||||||||||||||||||
| 139 | 7 | return $key; | |||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||
| 141 | 1 | return undef; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 20 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 20 | my $chopped = $self->clean_house(); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # First, update $self->{curr_key} | ||||||||||||||||||||||||||||
| 152 | 20 | $self->{curr_key}++; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 20 | ▶ | if(defined $chopped){ # The hash has changed while iterating. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | if($self->{curr_key} <= $chopped){ # Start over | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $self->{curr_key} = 0; | |||||||||||||||||||||||||||
| 157 | } else { # Adjust number | ||||||||||||||||||||||||||||
| 158 | 1 | $self->{curr_key} = ($self->{curr_key}-$chopped)-1; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Return the right thing: | ||||||||||||||||||||||||||||
| 163 | 20 | ▶ | if($self->{curr_key} <= $#{$self->{array}}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 14 | return $self->{array}->[$self->{curr_key}]->[1]; | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 6 | return undef; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 28 | ▶ | sub clean_house { | ||||||||||||||||||||||||||
|
Subroutine
clean_house: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # Locate the first expired datum and chop there. | ||||||||||||||||||||||||||||
| 175 | # Return the index of the first chopped key, or undef if no chop | ||||||||||||||||||||||||||||
| 176 | # occurred. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 28 | ▶ | unless(defined $self->{lifespan}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | return undef; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 28 | my $max = $#{$self->{array}}; | |||||||||||||||||||||||||||
| 183 | 28 | my $min = -1; | |||||||||||||||||||||||||||
| 184 | 28 | my $time = time; | |||||||||||||||||||||||||||
| 185 | 28 | $self->{last_clean} = $time; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 28 | ▶ | while($max > $min){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 48 | my $try = ceil(($max+$min)/2); | |||||||||||||||||||||||||||
| 189 | 48 | ▶ | if($time - $self->{array}->[$try]->[0] >= $self->{lifespan}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 2 | $min = $try; | |||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||
| 192 | 46 | $max = $try-1; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 28 | ▶ | if($min>=0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 2 | $self->chop_hash($min); | |||||||||||||||||||||||||||
| 197 | 2 | return $min; | |||||||||||||||||||||||||||
| 198 | } else { | ||||||||||||||||||||||||||||
| 199 | 26 | return undef; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 63 | ▶ | sub maybe_clean { | ||||||||||||||||||||||||||
|
Subroutine
maybe_clean: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 63 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 63 | my $time = time; | |||||||||||||||||||||||||||
| 209 | 63 | ▶ | if($time - $self->{last_clean} >= $self->{clean_int}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $self->clean_house(); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 5 | ▶ | sub chop_hash { | ||||||||||||||||||||||||||
|
Subroutine
chop_hash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 217 | 5 | my ($index) = @_; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # Eliminate all entries from the array at $index and before. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 5 | ▶ | if($index >= $#{$self->{array}}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 3 | @{$self->{array}} = (); | |||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||
| 224 | 2 | @{$self->{array}} = @{$self->{array}}[($index+1) .. $#{$self->{array}}]; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 5 | $self->rebuild_hash(); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 7 | ▶ | sub rebuild_hash { | ||||||||||||||||||||||||||
|
Subroutine
rebuild_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | $self->{hash} = { | ||||||||||||||||||||||||||||
| 235 | 7 | map {$self->{array}->[$_]->[1], $_} (0..$#{$self->{array}}) | |||||||||||||||||||||||||||
| 236 | }; | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 2 | 1; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||||||||||