blib/lib/Bot/Cobalt/Plugin/RDB/SearchCache.pm

filter: Statementstmt 95.8% 46 / 48 Branchbran 75.0% 18 / 24 Conditioncond 45.0% 9 / 20 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 5 totaltotal 71.7% 84 / 117 CC 20 SCAR 16.1
CC
20
Cov
79%
fetch
6.8
invalidate
6.7
_shrink
6.0
CRAP 5.0
« blib/lib/Bot/Cobalt/IRC/Event/Nick.pm ↑ Summary blib/lib/Bot/Cobalt/IRC/Message.pm »
1 package Bot::Cobalt::Plugin::RDB::SearchCache;
2 1 $Bot::Cobalt::Plugin::RDB::SearchCache::VERSION = '0.021003';
3 ## This is a fairly generic in-memory cache object.
4 ##
5 ## It's intended for use with Plugin::RDB, but will likely work for
6 ## just about situation where you want to store a set number of keys
7 ## mapping an identifier to an array reference.
8 ##
9 ## This can be useful for caching the results of deep searches against
10 ## Bot::Cobalt::DB instances, for example.
11 ##
12 ## This may get moved out to the core lib directory, in which case this
13 ## module will become a placeholder.
14  
15 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
21 1 my $self = +{ Cache => +{} };
22 1 my $class = shift;
23 1 bless $self, $class;
24  
25 1 my %opts = @_;
26 1 ▶ $self->MaxKeys($opts{MaxKeys} || 30);
Condition50%1/2
$opts{'MaxKeys'} || 30
l!l
01
MC/DC0%0/1
$opts{'MaxKeys'} || 30
$opts{'MaxKeys'}
Truth table50%1/2
A
A= $opts{'MaxKeys'}
Aresult
11
00
27
28 1 $self
29 }
30  
31 8 ▶sub cache {
Subroutine cache: called CC=4 SCAR=15.2
Pod undocumented
32 8 my ($self, $ckey, $match, $resultset) = @_;
33 ## should be passed rdb, search str, and array of matching indices
34  
35 8 ▶ return unless defined $ckey and defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ckey and defined $match 0 8 8
Condition33%1/3
defined $ckey and defined $match
!ll&&!rl&&r
008
MC/DC0%0/2
defined $ckey and defined $match
defined $ckey defined $match
Truth table33%1/3
A and B
A= defined $ckeyB= defined $match
ABresult
0X0
100
111
36 8 ▶ $resultset = [] unless ref $resultset eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $resultset eq "ARRAY" 0 8 8
37  
38 ## _shrink will do the right thing depending on size of cache
39 ## (MaxKeys can be used to adjust cachesize per-rdb 'on the fly')
40 8 $self->_shrink($ckey);
41
42 8 $self->{Cache}->{$ckey}->{$match} = +{
43 TS => Time::HiRes::time(),
44 Results => $resultset,
45 };
46 }
47  
48 5 ▶sub fetch {
Subroutine fetch: called CC=6 SCAR=19.1
Pod undocumented
49 5 my ($self, $ckey, $match) = @_;
50
51 return
52 unless defined $ckey
53 and defined $match
54 and exists $self->{Cache}->{$ckey}
55 5 ▶ and $self->{Cache}->{$ckey}->{$match};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ckey and defined $match and exists $self->{"Cache"}{$ckey} and $self->{"Cache"}{$ckey}{$match} 2 3 5
Condition44%4/9
defined $ckey and defined $match and exists $self->{"Cache"}{$ckey} and $self->{"Cache"}{$ckey}{$match}
!ll&&!rl&&r
023
defined $ckey and defined $match and exists $self->{"Cache"}{$ckey}
!ll&&!rl&&r
005
defined $ckey and defined $match
!ll&&!rl&&r
005
MC/DC25%1/4
defined $ckey and defined $match and exists $self->{"Cache"}{$ckey} and $self->{"Cache"}{$ckey}{$match}
defined $ckey defined $match exists $self->{"Cache"}{$ckey} $self->{"Cache"}{$ckey}{$match}
Truth table40%2/5
A and B and C and D
A= defined $ckeyB= defined $matchC= exists $self->{"Cache"}{$ckey}D= $self->{"Cache"}{$ckey}{$match}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
56  
57 3 my $ref = $self->{Cache}->{$ckey}->{$match};
58 1 wantarray ? @{ $ref->{Results} } : $ref->{Results}
59 3 ▶}
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
60  
61 10 ▶sub MaxKeys {
Subroutine MaxKeys: called CC=2 SCAR=6.9
Pod undocumented
62 10 my ($self, $max) = @_;
63 10 ▶ return $self->{MAX_KEYS} = $max if defined $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 2 8 10
64 $self->{MAX_KEYS}
65 8 }
66  
67 1 ▶sub invalidate {
Subroutine invalidate: called CC=5 SCAR=19.1
Pod undocumented
68 1 my ($self, $ckey, $match) = @_;
69 ## should be called on add/del operations
70  
71 1 ▶ unless (defined $ckey) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ckey) 0 1 1
72 ## invalidate all by not passing an arg
73 0 $self->{Cache} = +{};
74 return
75 0 }
76  
77 return unless exists $self->{Cache}->{$ckey}
78 1 ▶ and keys %{ $self->{Cache}->{$ckey} } ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Cache"}{$ckey} and keys %{$$self{"Cache"}{$ckey};} 0 1 1
Condition33%1/3
exists $self->{"Cache"}{$ckey} and keys %{$$self{"Cache"}{$ckey};}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $self->{"Cache"}{$ckey} and keys %{$$self{"Cache"}{$ckey};}
exists $self->{"Cache"}{$ckey} keys %{$$self{"Cache"}{$ckey};}
Truth table33%1/3
A and B
A= exists $self->{"Cache"}{$ckey}B= keys %{$$self{"Cache"}{$ckey};}
ABresult
0X0
100
111
79  
80 1 ▶ return delete $self->{Cache}->{$ckey}->{$match}
Branch: 1 branch
BranchTrueFalseTotal
if defined $match 0 1 1
81 if defined $match;
82  
83 1 delete $self->{Cache}->{$ckey}
84 }
85  
86 8 ▶sub _shrink {
Subroutine _shrink: called CC=6 SCAR=18.0
87 8 my ($self, $ckey) = @_;
88
89 8 ▶ return unless defined $ckey and ref $self->{Cache}->{$ckey};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ckey and ref $self->{"Cache"}{$ckey} 2 6 8
Condition66%2/3
defined $ckey and ref $self->{"Cache"}{$ckey}
!ll&&!rl&&r
026
MC/DC50%1/2
defined $ckey and ref $self->{"Cache"}{$ckey}
defined $ckey ref $self->{"Cache"}{$ckey}
Truth table66%2/3
A and B
A= defined $ckeyB= ref $self->{"Cache"}{$ckey}
ABresult
0X0
100
111
90  
91 6 my $cacheref = $self->{Cache}->{$ckey};
92  
93 6 ▶ return unless scalar keys %$cacheref > $self->MaxKeys;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$cacheref > $self->MaxKeys 5 1 6
94  
95 my @cached = sort {
96 1 $cacheref->{$a}->{TS} <=> $cacheref->{$b}->{TS}
97 10 } keys %$cacheref;
98
99 1 my $deleted = 0;
100  
101 1 ▶ while (scalar keys %$cacheref > $self->MaxKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$cacheref > $self->MaxKeys) 1 1 2
102 1 my $nextkey = shift @cached;
103 1 ▶ ++$deleted if delete $cacheref->{$nextkey};
Branch: 1 branch
BranchTrueFalseTotal
if delete $cacheref->{$nextkey} 1 0 1
104 }
105  
106 $deleted
107 1 }
108  
109 1 1;
110 __END__
« blib/lib/Bot/Cobalt/IRC/Event/Nick.pm ↑ Summary blib/lib/Bot/Cobalt/IRC/Message.pm »