blib/lib/Suggest/PrePop.pm

filter: Statementstmt 25.7% 25 / 97 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 33.3% 5 / 15 Podpod 100% 4 / 4 totaltotal 21.7% 34 / 156 CC 23 SCAR 33.1
CC
23
Cov
19%
ask
56.0
drop_prefix
42.0
prune
42.0
CRAP 27.5
↑ Summary
1 package Suggest::PrePop;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '2.1.1';
6  
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Cache::RedisDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has cache_namespace => (
12 is => 'ro',
13 isa => 'Str',
14 default => 'SUGGEST-PREPOP',
15 );
16  
17 1 my $key_sep = chr(0x02); # Start-of-text
18  
19 has 'scopes' => (
20 is => 'ro',
21 isa => 'ArrayRef',
22 lazy => 1,
23 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=12.6
24 0 my $self = shift;
25 0 my $size = length($self->_cnt_key_base . $key_sep);
26 1 ▶ no warnings('substr');
Subroutine BEGIN: called CC=1 SCAR=0.0
27 return [
28 0 sort { $a cmp $b }
29 0 ▶ map { substr($_, $size) // '' }
Condition0%0/2
substr($_, $size) // ""
l!l
00
MC/DC0%0/1
substr($_, $size) // ""
substr($_, $size)
Truth table0%0/2
A
A= substr($_, $size)
Aresult
11
00
30 0 @{$self->_redis->keys($self->_cnt_key_base . '*')}];
31 },
32 1 );
33  
34 has _lex_key_base => (
35 is => 'ro',
36 isa => 'Str',
37 lazy => 1,
38 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 my $self = shift;
40 0 return join($key_sep, $self->cache_namespace, 'ITEMS_BY_LEX');
41 },
42 1 );
43  
44 has _cnt_key_base => (
45 is => 'ro',
46 isa => 'Str',
47 lazy => 1,
48 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my $self = shift;
50 0 return join($key_sep, $self->cache_namespace, 'ITEMS_BY_COUNT');
51 },
52 1 );
53  
54 1 has min_activity => (
55 is => 'ro',
56 isa => 'Int',
57 default => 5,
58 );
59  
60 1 has entries_limit => (
61 is => 'ro',
62 isa => 'Int',
63 default => 131072,
64 );
65  
66 1 has top_count => (
67 is => 'ro',
68 isa => 'Int',
69 default => 7,
70 );
71  
72 # Convenience
73 0 ▶sub _redis { Cache::RedisDB->redis }
Subroutine _redis: not called CC=1 SCAR=6.9
74  
75 0 ▶sub _lex_key {
Subroutine _lex_key: not called CC=2 SCAR=17.9
76 0 my ($self, $scope) = @_;
77  
78 0 ▶ return ($scope)
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 0 0 0
79 ? join($key_sep, $self->_lex_key_base, lc $scope)
80 : $self->_lex_key_base;
81 }
82  
83 0 ▶sub _cnt_key {
Subroutine _cnt_key: not called CC=2 SCAR=17.9
84 0 my ($self, $scope) = @_;
85  
86 0 ▶ return ($scope)
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 0 0 0
87 ? join($key_sep, $self->_cnt_key_base, lc $scope)
88 : $self->_cnt_key_base;
89 }
90  
91 0 ▶sub add {
Subroutine add: not called CC=4 SCAR=30.0
Pod documented
92 0 my ($self, $item, $count, @scopes) = @_;
93  
94 0 ▶ $count //= 1; # Most of the time we'll just get a single entry
Condition0%0/2
$count //= 1
l!l
00
MC/DC0%0/1
$count //= 1
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
95 0 ▶ @scopes = ('') unless @scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @scopes 0 0 0
96  
97 # For now, we just assume supplied items are well-formed
98 0 my $redis = $self->_redis;
99  
100 0 my $keyed_item = join($key_sep, lc $item, $item);
101  
102 0 my $how_many = 0;
103 0 foreach my $scope (@scopes) {
104 # Lexically sorted items are all zero-scored
105 0 $redis->zadd($self->_lex_key($scope), 0, $keyed_item);
106 # Score sorted items get incremented.
107 0 $how_many += $redis->zincrby($self->_cnt_key($scope), $count, $keyed_item);
108 }
109  
110 0 return $how_many;
111 }
112  
113 0 ▶sub drop_prefix {
Subroutine drop_prefix: not called CC=6 SCAR=37.4
Pod documented
114 0 my ($self, $prefix, @scopes) = @_;
115  
116 0 ▶ return 0 unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
117 0 ▶ @scopes = ('') unless @scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @scopes 0 0 0
118  
119 0 my $redis = $self->_redis;
120 0 my $how_many = 0;
121 0 foreach my $scope (@scopes) {
122 0 my $lex_key = $self->_lex_key($scope);
123 0 my $cnt_key = $self->_cnt_key($scope);
124 0 foreach my $member (
125 @{
126 0 ▶ $redis->zrangebylex(
Condition0%0/2
$redis->zrangebylex($lex_key, '[' . $prefix, '[' . $prefix . "\377") // []
l!l
00
MC/DC0%0/1
$redis->zrangebylex($lex_key, '[' . $prefix, '[' . $prefix . "\377") // []
$redis->zrangebylex($lex_key, '[' . $prefix, '[' . $prefix . "\377")
Truth table0%0/2
A
A= $redis->zrangebylex($lex_key, '[' . $prefix, '[' . $prefix . "\377")
Aresult
11
00
127 $lex_key,
128 '[' . $prefix,
129 '[' . $prefix . "\xff"
130 ) // []})
131 {
132 0 $redis->zrem($lex_key, $member);
133 0 $redis->zrem($cnt_key, $member);
134 0 $how_many++;
135 }
136 }
137  
138 0 return $how_many;
139 }
140  
141 0 ▶sub ask {
Subroutine ask: not called CC=7 SCAR=40.3
Pod documented
142 0 my ($self, $prefix, $count, @scopes) = @_;
143  
144 0 ▶ $count //= $self->top_count; # If they don't say we try to find the 5 best.
Condition0%0/3
$count //= $self->top_count
l!l&&r!l&&!r
000
MC/DC0%0/2
$count //= $self->top_count
$count $self->top_count
Truth table0%0/3
A //= B
A= $countB= $self->top_count
ABresult
1X1
011
000
145 0 ▶ @scopes = ('') unless @scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @scopes 0 0 0
146  
147 0 my $redis = $self->_redis;
148  
149 0 my @full;
150  
151 0 foreach my $scope (@scopes) {
152 0 push @full, grep { $_->[1] >= $self->min_activity }
153 0 map { [$_, $redis->zscore($self->_cnt_key($scope), $_)] } @{
154 0 ▶ $redis->zrangebylex(
Condition0%0/2
$redis->zrangebylex($self->_lex_key($scope), '[' . $prefix, '[' . $prefix . "\377") // []
l!l
00
MC/DC0%0/1
$redis->zrangebylex($self->_lex_key($scope), '[' . $prefix, '[' . $prefix . "\377") // []
$redis->zrangebylex($self->_lex_key($scope), '[' . $prefix, '[' . $prefix . "\377")
Truth table0%0/2
A
A= $redis->zrangebylex($self->_lex_key($scope), '[' . $prefix, '[' . $prefix . "\377")
Aresult
11
00
155 $self->_lex_key($scope),
156 '[' . $prefix,
157 '[' . $prefix . "\xff"
158 ) // []};
159 }
160 0 my %seen;
161  
162 my @final;
163 0 foreach my $thing (sort { $b->[1] <=> $a->[1] } @full) {
164 0 my ($lc, $pc) = split $key_sep, $thing->[0];
165 0 ▶ next if defined $seen{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen{$lc} 0 0 0
166 0 $seen{$lc} = 1;
167 0 push @final, $pc;
168 }
169  
170 0 return [splice(@final, 0, $count)];
171 }
172  
173 0 ▶sub prune {
Subroutine prune: not called CC=6 SCAR=37.4
Pod documented
174 0 my ($self, $keep, @scopes) = @_;
175  
176 0 ▶ $keep //= $self->entries_limit;
Condition0%0/3
$keep //= $self->entries_limit
l!l&&r!l&&!r
000
MC/DC0%0/2
$keep //= $self->entries_limit
$keep $self->entries_limit
Truth table0%0/3
A //= B
A= $keepB= $self->entries_limit
ABresult
1X1
011
000
177 0 ▶ @scopes = ('') unless @scopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @scopes 0 0 0
178  
179 0 my $redis = $self->_redis;
180  
181 0 my $count = 0;
182  
183 0 foreach my $scope (@scopes) {
184 # Count key is the one from which results are collated, so even
185 # if things are out of sync, this is the one about which we care.
186 0 ▶ next if ($redis->zcard($self->_cnt_key($scope)) <= $keep);
Branch: 1 branch
BranchTrueFalseTotal
if $redis->zcard($self->_cnt_key($scope)) <= $keep 0 0 0
187  
188 0 my $final_index = -1 * $keep - 1; # Range below is inclusive.
189 my @to_prune =
190 0 @{$redis->zrange($self->_cnt_key($scope), 0, $final_index)};
191 0 $count += scalar @to_prune;
192  
193 # We're going to do this the slow way to keep them in sync.
194 0 foreach my $item (@to_prune) {
195 0 $redis->zrem($self->_cnt_key($scope), $item);
196 0 $redis->zrem($self->_lex_key($scope), $item);
197 }
198  
199 }
200  
201 0 return $count;
202 }
203  
204 1 1;
205  
206 __END__
↑ Summary