Session/Redis.pm

filter: Statementstmt 31.2% 99 / 317 Branchbran 29.1% 42 / 144 Conditioncond 22.2% 12 / 54 MC/DCmcdc 12.1% 4 / 33 Subroutinesub 50.0% 18 / 36 Podpod 0.0% 0 / 11 totaltotal 29.4% 175 / 595 CC 109 SCAR 47.2
CC
109
Cov
30%
_searchCompare
380.0
searchOnExpr
210.0
searchOn
156.0
CRAP 111.8
« Session/Store/LDAP.pm ↑ Summary Session/MySQLJSON.pm »
1 package Web::Session::Redis;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Web::Session::Store::Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Web::Session::Generate::SHA256;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Apache::Session::Lock::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Web::Session::Serialize::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Web::Session::_common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 our $VERSION = '1.0.0';
13 7 our @ISA = qw(Apache::Session);
14  
15 0 ▶sub populate {
Subroutine populate: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my $self = shift;
17  
18 0 $self->{object_store} = new Web::Session::Store::Redis $self;
19 0 $self->{lock_manager} = new Apache::Session::Lock::Null $self;
20 0 $self->{generate} = \&Web::Session::Generate::SHA256::generate;
21 0 $self->{validate} = \&Web::Session::Generate::SHA256::validate;
22 0 $self->{serialize} = \&Web::Session::Serialize::JSON::serializeLatin1;
23 $self->{unserialize} =
24 0 \&Web::Session::Serialize::JSON::unserializeLatin1;
25  
26 0 return $self;
27 }
28  
29 # Key name sent to Redis, see Store::Redis::keyName()
30 4 ▶sub _keyName {
Subroutine _keyName: called CC=1 SCAR=0.0
31 4 return Web::Session::Store::Redis::keyName(@_);
32 }
33  
34 # SCAN patterns for searchOnExpr(). The name of a set is Latin-1 unless it
35 # holds characters above U+00FF, so a wildcard may stand for such characters
36 # while the literal part is not ASCII: the UTF-8 form is then scanned too
37 4 ▶sub _exprPatterns {
Subroutine _exprPatterns: called CC=3 SCAR=11.0
38 4 my ( $field, $value ) = @_;
39 4 my $pattern = _exprPattern( $field, $value );
40 4 my @res = ( _keyName($pattern) );
41 4 ▶ if ( $value =~ /\*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\*/) 3 1 4
42 3 my $p = $pattern;
43 3 utf8::encode($p);
44 3 ▶ push @res, $p unless ( $p eq $res[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless $p eq $res[0] 1 2 3
45 }
46 4 return @res;
47 }
48  
49 0 ▶sub unserialize {
Subroutine unserialize: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $session = shift;
51 0 my $tmp = { serialized => $session };
52 0 Web::Session::Serialize::JSON::unserializeLatin1($tmp);
53 0 return $tmp->{data};
54 }
55  
56 # Keys per SCAN call (a hint for Redis)
57 7 our $SCAN_COUNT = 1000;
58  
59 # Keys per MGET: a huge index set must not block Redis with one big command
60 7 our $MGET_BATCH = 500;
61  
62 # Like $redisObj->mget(@keys), in batches. MGET returns undef for missing
63 # keys and keys that aren't strings
64 1 ▶sub _mget {
Subroutine _mget: called CC=2 SCAR=6.9
65 1 my ( $redisObj, @keys ) = @_;
66 1 my @res;
67 1 ▶ while (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 3 1 4
68 3 push @res, $redisObj->mget( splice( @keys, 0, $MGET_BATCH ) );
69 }
70 1 return @res;
71 }
72  
73 # Members of index sets @$sets, read in one pipeline: { set => [ keys ] }.
74 # A key that isn't a set is ignored; other Redis errors are raised
75 2 ▶sub _smembers {
Subroutine _smembers: called CC=3 SCAR=11.0
76 2 my ( $redisObj, $sets ) = @_;
77 2 my ( %res, $fatal );
78 2 foreach my $set (@$sets) {
79 $redisObj->smembers(
80 $set,
81 5 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
82 5 my ( $reply, $err ) = @_;
83 5 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 3 2 5
84 3 ▶ $fatal //= $err unless ( $err =~ /WRONGTYPE/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /WRONGTYPE/ 2 1 3
Condition66%2/3
$fatal //= $err
l!l&&r!l&&!r
110
MC/DC0%0/2
$fatal //= $err
$fatal $err
Truth table66%2/3
A //= B
A= $fatalB= $err
ABresult
1X1
011
000
85 }
86 else {
87 2 $res{$set} = $reply;
88 }
89 }
90 5 );
91 }
92 2 $redisObj->wait_all_responses;
93 2 ▶ die $fatal if ($fatal);
Branch: 1 branch
BranchTrueFalseTotal
if $fatal 1 1 2
94 1 return \%res;
95 }
96  
97 # Remove session KEYS[2] from index KEYS[1] only if it doesn't exist, in one
98 # step: a concurrent writer may be recreating it
99 7 our $SREM_ORPHAN = q{if redis.call('exists',KEYS[2])==0 then }
100 . q{return redis.call('srem',KEYS[1],KEYS[2]) end return 0};
101  
102 7 our $lua_warned;
103  
104 # Remove orphan session $k from index set $set. Without Lua (EVAL forbidden by
105 # ACL, proxy...), WATCH the session so that a concurrent writer aborts the
106 # removal. $k must have been checked with isLlngKey()
107 3 ▶sub _removeOrphan {
Subroutine _removeOrphan: called CC=5 SCAR=16.7
108 3 my ( $class, $redisObj, $set, $k ) = @_;
109 3 ▶ return if ( eval { $redisObj->eval( $SREM_ORPHAN, 2, $set, $k ); 1 } );
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $redisObj->eval($Web::Session::Redis::SREM_ORPHAN, 2, $set, $k); 1 } } 1 2 3
110 2 ▶ unless ($lua_warned) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Web::Session::Redis::lua_warned) 1 1 2
111 1 $lua_warned = 1;
112 1 print STDERR "Redis EVAL failed, orphan index members are removed "
113 . "without Lua: $@\n";
114 }
115 2 my $err;
116 2 eval {
117 2 $redisObj->watch($k);
118 2 ▶ if ( $redisObj->exists($k) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redisObj->exists($k)) { } 1 1 2
119 1 $redisObj->unwatch;
120 }
121 else {
122 1 $redisObj->multi;
123 1 $redisObj->srem( $set, $k );
124 1 $redisObj->exec;
125 }
126 };
127 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
128 0 $err = $@;
129 0 eval { $redisObj->discard };
130 0 eval { $redisObj->unwatch };
131 0 print STDERR "Unable to remove '$k' from index $set: $err\n";
132 }
133 }
134  
135 0 ▶sub searchOn {
Subroutine searchOn: not called CC=12 SCAR=50.5
Pod undocumented
136 0 my ( $class, $args, $selectField, $value, @fields ) = @_;
137  
138 0 my %res = ();
139 0 ▶ if ( $class->isIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isIndexed($args, $selectField)) { } 0 0 0
140  
141 0 my $redisObj = $class->_getRedis($args);
142 0 my $index_key = _keyName("${selectField}_$value");
143 0 my @keys = eval { $redisObj->smembers($index_key) };
144 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
145 0 ▶ die $@ unless ( $@ =~ /WRONGTYPE/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /WRONGTYPE/ 0 0 0
146 0 @keys = ();
147 }
148  
149 # Members of another application are neither read nor removed
150 0 ▶ @keys = grep { $_ and $class->isLlngKey( $args, $_ ) } @keys;
Condition0%0/3
$_ && $class->isLlngKey($args, $_)
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && $class->isLlngKey($args, $_)
$_ $class->isLlngKey($args, $_)
Truth table0%0/3
A && B
A= $_B= $class->isLlngKey($args, $_)
ABresult
0X0
100
111
151 0 my %values;
152 0 @values{@keys} = _mget( $redisObj, @keys );
153 0 foreach my $k (@keys) {
154 0 my $tmp = $values{$k};
155 0 ▶ unless ($tmp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp) 0 0 0
156 # Lazy cleanup: remove orphan from index
157 0 $class->_removeOrphan( $redisObj, $index_key, $k );
158 0 next;
159 }
160 0 eval {
161 0 $tmp = unserialize($tmp);
162  
163 # The index may be stale or shared with another field
164 # ("a_b" + "c" and "a" + "b_c" give the same set name)
165 return
166 unless ( defined $tmp->{$selectField}
167 0 ▶ and $tmp->{$selectField} eq $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp->{$selectField} and $tmp->{$selectField} eq $value 0 0 0
Condition0%0/3
defined $tmp->{$selectField} and $tmp->{$selectField} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tmp->{$selectField} and $tmp->{$selectField} eq $value
defined $tmp->{$selectField} $tmp->{$selectField} eq $value
Truth table0%0/3
A and B
A= defined $tmp->{$selectField}B= $tmp->{$selectField} eq $value
ABresult
0X0
100
111
168 0 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
169 0 $res{$k}->{$_} = $tmp->{$_} foreach (@fields);
170 }
171 else {
172 0 $res{$k} = $tmp;
173 }
174 };
175 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
176 0 print STDERR "Error in session $k: $@\n";
177 0 delete $res{$k};
178 }
179 }
180 }
181 else {
182 $class->get_key_from_all_sessions(
183 $args,
184 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
185 0 my $entry = shift;
186 0 my $id = shift;
187 return undef
188 unless ( defined $entry->{$selectField}
189 0 ▶ and $entry->{$selectField} eq $value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{$selectField} and $entry->{$selectField} eq $value 0 0 0
Condition0%0/3
defined $entry->{$selectField} and $entry->{$selectField} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $entry->{$selectField} and $entry->{$selectField} eq $value
defined $entry->{$selectField} $entry->{$selectField} eq $value
Truth table0%0/3
A and B
A= defined $entry->{$selectField}B= $entry->{$selectField} eq $value
ABresult
0X0
100
111
190 0 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
191 0 $res{$id}->{$_} = $entry->{$_} foreach (@fields);
192 }
193 else {
194 0 $res{$id} = $entry;
195 }
196 0 undef;
197 }
198 0 );
199 }
200 0 return \%res;
201 }
202  
203 # searchOnExpr() patterns: '*' is a wildcard
204 0 ▶sub _exprRe {
Subroutine _exprRe: not called CC=1 SCAR=6.9
205 0 my ($value) = @_;
206 0 $value = quotemeta($value);
207 0 $value =~ s/\\\*/\.\*/g;
208 0 return qr/^$value$/;
209 }
210  
211 # Redis glob escape: MATCH patterns of SCAN must find names literally
212 10 ▶sub _globEscape {
Subroutine _globEscape: called CC=1 SCAR=0.0
213 10 my ($s) = @_;
214 10 $s =~ s/([*?\[\]\\])/\\$1/g;
215 10 return $s;
216 }
217  
218 # SCAN pattern of the index sets for searchOnExpr(): '*' is the only
219 # wildcard of the value. The sessions are checked afterwards with _exprRe()
220 9 ▶sub _exprPattern {
Subroutine _exprPattern: called CC=1 SCAR=0.0
221 9 my ( $field, $value ) = @_;
222 9 $value =~ s/([?\[\]\\])/\\$1/g;
223 9 return _globEscape($field) . "_$value";
224 }
225  
226 0 ▶sub searchOnExpr {
Subroutine searchOnExpr: not called CC=14 SCAR=53.5
Pod undocumented
227 0 my ( $class, $args, $selectField, $value, @fields ) = @_;
228 0 my %res;
229 0 my $re = _exprRe($value);
230 0 ▶ if ( $class->isIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isIndexed($args, $selectField)) { } 0 0 0
231 0 my $redisObj = $class->_getRedis($args);
232 0 foreach my $pattern ( _exprPatterns( $selectField, $value ) ) {
233 0 my $cursor = 0;
234 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cursor != 0 and do { do { () = $redisObj->scan($cursor, "MATCH", $pattern, "COUNT", $Web::Session::Redis::SCAN_COUNT); my $members = _smembers($redisObj, $sets); foreach my $set (keys %$members) { $members->{$set} = [grep({$_ and $class->isLlngKey($args, $_);} @{$$members{$set};})]; } ; my %seen; my(@keys) = grep({not $seen{$_}++;} map({@$_;} values %$members)); my %values; @values{@keys} = _mget($redisObj, @keys); foreach my $set (keys %$members) { foreach my $k (@{$$members{$set};}) { my $v = $values{$k}; unless ($v) { $class->_removeOrphan($redisObj, $set, $k); next; } ; my $tmp = eval { do { unserialize($v) } }; if ($@ or ref $tmp ne "HASH") { print STDERR "Error in session ${k}: " . ($@ || "not a session\n"); next; } ; unless (defined $tmp->{$selectField} and $tmp->{$selectField} =~ /$re/) { next; } ; $res{$k} = $class->extractFields($tmp, @fields); } ; } ; $cursor = $new_cursor } } 0 0 0
235 0 my ( $new_cursor, $sets ) =
236 $redisObj->scan( $cursor,
237 MATCH => $pattern,
238 COUNT => $SCAN_COUNT );
239  
240 # The sets of a SCAN page are read together: a few round trips
241 # instead of two per set. Members of another application are
242 # neither read nor removed
243 0 my $members = _smembers( $redisObj, $sets );
244 0 foreach my $set ( keys %$members ) {
245 $members->{$set} =
246 0 ▶ [ grep { $_ and $class->isLlngKey( $args, $_ ) }
Condition0%0/3
$_ && $class->isLlngKey($args, $_)
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && $class->isLlngKey($args, $_)
$_ $class->isLlngKey($args, $_)
Truth table0%0/3
A && B
A= $_B= $class->isLlngKey($args, $_)
ABresult
0X0
100
111
247 0 @{ $members->{$set} } ];
248 }
249 0 my %seen;
250 0 my @keys = grep { !$seen{$_}++ } map { @$_ } values %$members;
251 0 my %values;
252 0 @values{@keys} = _mget( $redisObj, @keys );
253 0 foreach my $set ( keys %$members ) {
254 0 foreach my $k ( @{ $members->{$set} } ) {
255 0 my $v = $values{$k};
256 0 ▶ unless ($v) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v) 0 0 0
257 # Lazy cleanup: remove orphan from index
258 0 $class->_removeOrphan( $redisObj, $set, $k );
259 0 next;
260 }
261 0 my $tmp = eval { unserialize($v) };
262 0 ▶ if ( $@ or ref($tmp) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $tmp ne "HASH") 0 0 0
Condition0%0/3
$@ or ref $tmp ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or ref $tmp ne "HASH"
$@ ref $tmp ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $tmp ne "HASH"
ABresult
1X1
011
000
263 0 ▶ print STDERR "Error in session $k: "
Condition0%0/2
$@ || "not a session\n"
l!l
00
MC/DC0%0/1
$@ || "not a session\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
264 . ( $@ || "not a session\n" );
265 0 next;
266 }
267  
268 # The pattern may match sets of another field
269 0 ▶ unless ( defined $tmp->{$selectField}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp->{$selectField} and $tmp->{$selectField} =~ /$re/) 0 0 0
Condition0%0/3
defined $tmp->{$selectField} and $tmp->{$selectField} =~ /$re/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tmp->{$selectField} and $tmp->{$selectField} =~ /$re/
defined $tmp->{$selectField} $tmp->{$selectField} =~ /$re/
Truth table0%0/3
A and B
A= defined $tmp->{$selectField}B= $tmp->{$selectField} =~ /$re/
ABresult
0X0
100
111
270 and $tmp->{$selectField} =~ $re )
271 {
272 0 next;
273 }
274 0 $res{$k} = $class->extractFields( $tmp, @fields );
275 }
276 }
277 0 $cursor = $new_cursor;
278 } while ( $cursor != 0 );
279 }
280 }
281 else {
282 $class->get_key_from_all_sessions(
283 $args,
284 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
285 0 my ( $entry, $id ) = @_;
286 0 ▶ return undef unless ( $entry->{$selectField} =~ $re );
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{$selectField} =~ /$re/ 0 0 0
287 0 $res{$id} = $class->extractFields( $entry, @fields );
288 0 undef;
289 }
290 0 );
291 }
292 0 return \%res;
293 }
294  
295 0 ▶sub searchLt {
Subroutine searchLt: not called CC=1 SCAR=6.9
Pod undocumented
296 0 my $class = shift;
297 0 return $class->_searchCompare( '<', @_ );
298 }
299  
300 0 ▶sub searchGt {
Subroutine searchGt: not called CC=1 SCAR=6.9
Pod undocumented
301 0 my $class = shift;
302 0 return $class->_searchCompare( '>', @_ );
303 }
304  
305 # Trimmed number, or undef
306 0 ▶sub _number {
Subroutine _number: not called CC=4 SCAR=30.0
307 0 my ($v) = @_;
308 0 ▶ return undef unless ( defined($v) and !ref($v) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v 0 0 0
Condition0%0/3
defined $v and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and not ref $v
defined $v not ref $v
Truth table0%0/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
309 0 $v =~ s/^\s+|\s+$//g;
310 0 ▶ return $v =~ /^-?[0-9]+(?:\.[0-9]+)?\z/ ? $v : undef;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /^-?[0-9]+(?:\.[0-9]+)?\z/ ? : 0 0 0
311 }
312  
313 # Sessions where $selectField isn't a number (see _number()) are skipped.
314 # Indexed field: SCAN walks the keyspace server-side and returns only the
315 # index sets of $selectField; only sets holding a matching value are read.
316 # Used by purges: Redis errors are reported and the sets ignored
317 0 ▶sub _searchCompare {
Subroutine _searchCompare: not called CC=19 SCAR=59.4
318 0 my ( $class, $op, $args, $selectField, $value, @fields ) = @_;
319  
320 # LLNG CLI keeps spaces around the value ("--where 'f < 123 '")
321 0 $value = _number($value);
322 0 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
323 0 ▶ print STDERR 'search'
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 0 0 0
324 . ( $op eq '<' ? 'Lt' : 'Gt' )
325 . ": value must be a number\n";
326 0 return {};
327 }
328 0 ▶ my $test = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
329 0 my $v = _number(shift);
330 0 ▶ return 0 unless ( defined $v );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
331 0 ▶ return $op eq '<' ? $v < $value : $v > $value;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 0 0 0
332 0 };
333 0 my %res;
334 0 ▶ unless ( $class->isIndexed( $args, $selectField ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->isIndexed($args, $selectField)) 0 0 0
335 $class->get_key_from_all_sessions(
336 $args,
337 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
338 0 my ( $entry, $id ) = @_;
339 $res{$id} = $class->extractFields( $entry, @fields )
340 0 ▶ if ( $test->( $entry->{$selectField} ) );
Branch: 1 branch
BranchTrueFalseTotal
if &$test($entry->{$selectField}) 0 0 0
341 0 undef;
342 }
343 0 );
344 0 return \%res;
345 }
346  
347 0 my $redisObj = $class->_getRedis($args);
348 0 my $prefix = "${selectField}_";
349 0 ( my $pattern = $prefix ) =~ s/([*?\[\]\\])/\\$1/g;
350 0 my $cursor = 0;
351 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cursor != 0 and do { do { () = $redisObj->scan($cursor, "MATCH", "$pattern*"); foreach my $set (grep {&$test(substr($_, length $prefix));} @$sets) { my(@keys) = eval { do { $redisObj->smembers($set) } }; if ($@) { print STDERR "Error when reading index ${set}: $@\n" unless $@ =~ /WRONGTYPE/; next; } ; foreach my $k (@keys) { next unless $k and not exists $res{$k} and $class->isLlngKey($args, $k); my $v = eval { do { $redisObj->get($k) } }; if ($@) { print STDERR "Error when reading ${k}: $@\n" unless $@ =~ /WRONGTYPE/; next; } ; unless ($v) { $class->_removeOrphan($redisObj, $set, $k); next; } ; my $tmp = eval { do { unserialize($v) } }; if ($@ or ref $tmp ne "HASH") { print STDERR "Error in session ${k}: " . ($@ || "not a session\n"); next; } ; $res{$k} = $class->extractFields($tmp, @fields) if &$test($tmp->{$selectField}); } ; } ; $cursor = $new_cursor } } 0 0 0
352 0 my ( $new_cursor, $sets ) =
353 $redisObj->scan( $cursor, MATCH => "$pattern*" );
354  
355 # Sets of other fields ("${selectField}_x_1") aren't numbers
356 0 foreach my $set (
357 0 grep { $test->( substr( $_, length($prefix) ) ) } @$sets )
358 {
359 0 my @keys = eval { $redisObj->smembers($set) };
360 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
361 0 ▶ print STDERR "Error when reading index $set: $@\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /WRONGTYPE/ 0 0 0
362 unless ( $@ =~ /WRONGTYPE/ );
363 0 next;
364 }
365 0 foreach my $k (@keys) {
366  
367 # Members of another application are neither read nor removed
368 next
369 unless ( $k
370 0 ▶ and !exists $res{$k}
Branch: 1 branch
BranchTrueFalseTotal
unless $k and not exists $res{$k} and $class->isLlngKey($args, $k) 0 0 0
Condition0%0/6
$k and not exists $res{$k} and $class->isLlngKey($args, $k)
!ll&&!rl&&r
000
$k and not exists $res{$k}
!ll&&!rl&&r
000
MC/DC0%0/3
$k and not exists $res{$k} and $class->isLlngKey($args, $k)
$k not exists $res{$k} $class->isLlngKey($args, $k)
Truth table0%0/4
A and not(B) and C
A= $kB= not exists $res{$k}C= $class->isLlngKey($args, $k)
ABCresult
0XX0
10X0
1100
1111
371 and $class->isLlngKey( $args, $k ) );
372 0 my $v = eval { $redisObj->get($k) };
373 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
374 0 ▶ print STDERR "Error when reading $k: $@\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /WRONGTYPE/ 0 0 0
375 unless ( $@ =~ /WRONGTYPE/ );
376 0 next;
377 }
378 0 ▶ unless ($v) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v) 0 0 0
379  
380 # Lazy cleanup: remove orphan from index
381 0 $class->_removeOrphan( $redisObj, $set, $k );
382 0 next;
383 }
384 0 my $tmp = eval { unserialize($v) };
385 0 ▶ if ( $@ or ref($tmp) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $tmp ne "HASH") 0 0 0
Condition0%0/3
$@ or ref $tmp ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or ref $tmp ne "HASH"
$@ ref $tmp ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $tmp ne "HASH"
ABresult
1X1
011
000
386 0 ▶ print STDERR "Error in session $k: "
Condition0%0/2
$@ || "not a session\n"
l!l
00
MC/DC0%0/1
$@ || "not a session\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
387 . ( $@ || "not a session\n" );
388 0 next;
389 }
390  
391 # The index may be stale: check the session itself
392 $res{$k} = $class->extractFields( $tmp, @fields )
393 0 ▶ if ( $test->( $tmp->{$selectField} ) );
Branch: 1 branch
BranchTrueFalseTotal
if &$test($tmp->{$selectField}) 0 0 0
394 }
395 }
396 0 $cursor = $new_cursor;
397 } while ( $cursor != 0 );
398 0 return \%res;
399 }
400  
401 # Decide if a session must be purged. Like Lemonldap::NG's generic purge,
402 # only a missing _utime means "expired": any other missing field is not lower
403 # than the threshold (so a session without _lastSeen is not purged by it).
404 13 ▶sub _isDominated {
Subroutine _isDominated: called CC=14 SCAR=26.4
405 13 my ( $class, $v, $rule ) = @_;
406  
407 # Empty or data-less sessions should be purged
408 13 ▶ return 1 if ( !$v || !%$v || !exists $v->{_session_id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $v and %$v and exists $v->{"_session_id"} 3 10 13
Condition100%6/6
$v and %$v and exists $v->{"_session_id"}
!ll&&!rl&&r
2110
$v and %$v
!ll&&!rl&&r
1111
MC/DC100%3/3
$v and %$v and exists $v->{"_session_id"}
$v %$v exists $v->{"_session_id"}
Truth table100%4/4
A and B and C
A= $vB= %$vC= exists $v->{"_session_id"}
ABCresult
0XX0
10X0
1100
1111
409 10 ▶ if ( $rule->{or} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'or'}) { } 5 5 10
elsif ($rule->{'and'}) { } 4 1 5
410 5 foreach ( keys %{ $rule->{or} } ) {
411 8 ▶ if ( !defined( $v->{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v->{$_}) 2 6 8
412  
413 # Session without _utime: treat as expired
414 2 ▶ return 1 if $_ eq '_utime';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_utime" 1 1 2
415 1 next;
416 }
417 6 ▶ return 1 if $v->{$_} < $rule->{or}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $v->{$_} < $rule->{"or"}{$_} 2 4 6
418 }
419 }
420 elsif ( $rule->{and} ) {
421 4 foreach ( keys %{ $rule->{and} } ) {
422 8 ▶ if ( !defined( $v->{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v->{$_}) 2 6 8
423  
424 # Only a missing _utime counts as lower
425 2 ▶ return 0 unless $_ eq '_utime';
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "_utime" 1 1 2
426 1 next;
427 }
428 6 ▶ return 0 unless $v->{$_} < $rule->{and}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $v->{$_} < $rule->{"and"}{$_} 1 5 6
429 }
430 2 return 1;
431 }
432 3 return 0;
433 }
434  
435 8 ▶sub deleteIfLowerThan {
Subroutine deleteIfLowerThan: called CC=5 SCAR=30.8
Pod undocumented
436 8 my ( $class, $args, $rule ) = @_;
437  
438 # Same contract as the SQL backends: an invalid rule deletes nothing
439 8 ▶ return wantarray ? ( 0, 0 ) : 0
Branch: 2 branches
BranchTrueFalseTotal
unless "Web::Session::_common"->_checkRule($rule) and "Web::Session::_common"->_checkThresholds($rule) 8 0 8
wantarray ? : 4 4 8
Condition66%2/3
"Web::Session::_common"->_checkRule($rule) and "Web::Session::_common"->_checkThresholds($rule)
!ll&&!rl&&r
620
MC/DC0%0/2
"Web::Session::_common"->_checkRule($rule) and "Web::Session::_common"->_checkThresholds($rule)
"Web::Session::_common"->_checkRule($rule) "Web::Session::_common"->_checkThresholds($rule)
Truth table66%2/3
A and B
A= "Web::Session::_common"->_checkRule($rule)B= "Web::Session::_common"->_checkThresholds($rule)
ABresult
0X0
100
111
440 unless ( Web::Session::_common->_checkRule($rule)
441 and Web::Session::_common->_checkThresholds($rule) );
442 0 my $deleted = 0;
443 0 my $redisObj = $class->_getRedis($args);
444 my $index =
445 ref( $args->{Index} )
446 ? $args->{Index}
447 0 ▶ : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 0 0
448  
449 $class->get_key_from_all_sessions(
450 $args,
451 0 ▶ sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
452 0 my ( $v, $k ) = @_;
453 0 ▶ if ( $rule->{not} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"not"}) 0 0 0
454 0 foreach ( keys %{ $rule->{not} } ) {
455 0 ▶ if (defined( $v->{$_} ) and $v->{$_} eq $rule->{not}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v->{$_} and $v->{$_} eq $rule->{"not"}{$_}) 0 0 0
Condition0%0/3
defined $v->{$_} and $v->{$_} eq $rule->{"not"}{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v->{$_} and $v->{$_} eq $rule->{"not"}{$_}
defined $v->{$_} $v->{$_} eq $rule->{"not"}{$_}
Truth table0%0/3
A and B
A= defined $v->{$_}B= $v->{$_} eq $rule->{"not"}{$_}
ABresult
0X0
100
111
456 0 return ();
457 }
458 }
459 }
460 0 my $dominated = $class->_isDominated( $v, $rule );
461 0 ▶ if ($dominated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dominated) 0 0 0
462 # Clean up index entries before deleting the session
463 0 my $index_ok = 1;
464 0 foreach my $i (@$index) {
465 0 my $t = $v->{$i};
466 0 ▶ next unless ( defined($t) and length($t) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and length $t > 0 0 0 0
Condition0%0/3
defined $t and length $t > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $t and length $t > 0
defined $t length $t > 0
Truth table0%0/3
A and B
A= defined $tB= length $t > 0
ABresult
0X0
100
111
467 0 eval { $redisObj->srem( _keyName("${i}_$t"), $k ) };
468 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
469 0 warn "Failed to remove '$k' from index '${i}_$t': $@";
470 0 $index_ok = 0;
471 }
472 }
473 0 ▶ if ($index_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_ok) { } 0 0 0
474 0 $redisObj->del($k);
475 0 $deleted++;
476 }
477 else {
478 0 warn "Skipping deletion of session '$k' due to index cleanup failure";
479 }
480 }
481 0 return ();
482 },
483 0 );
484 0 return ( 1, $deleted );
485 }
486  
487 0 ▶sub extractFields {
Subroutine extractFields: not called CC=3 SCAR=24.8
Pod undocumented
488 0 my ( $class, $entry, @fields ) = @_;
489 0 my $res;
490 0 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
491 0 $res->{$_} = $entry->{$_} foreach (@fields);
492 }
493 else {
494 0 $res = $entry;
495 }
496 0 return $res;
497 }
498  
499 0 ▶sub isIndexed {
Subroutine isIndexed: not called CC=2 SCAR=17.9
Pod undocumented
500 0 my ( $class, $args, $field ) = @_;
501 my $indexes =
502 0 ▶ ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 0 0
503 0 return grep { $_ eq $field } @$indexes;
504 }
505  
506 7 my %keysRe_re;
507 6 ▶sub isLlngKey {
Subroutine isLlngKey: called CC=3 SCAR=11.0
Pod undocumented
508 6 my ( $class, $args, $name ) = @_;
509 6 ▶ my $expr = $args->{keysRe} || '^[0-9a-f]{32,}$';
Condition100%2/2
$args->{'keysRe'} || '^[0-9a-f]{32,}$'
l!l
24
MC/DC100%1/1
$args->{'keysRe'} || '^[0-9a-f]{32,}$'
$args->{'keysRe'}
Truth table100%2/2
A
A= $args->{'keysRe'}
Aresult
11
00
510  
511 # Compile per expression: a process may use several keysRe
512 6 ▶ $keysRe_re{$expr} = qr/$expr/ unless ( exists $keysRe_re{$expr} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keysRe_re{$expr} 2 4 6
513 6 return ( $name =~ $keysRe_re{$expr} );
514 }
515  
516 0 ▶sub get_key_from_all_sessions {
Subroutine get_key_from_all_sessions: not called CC=12 SCAR=50.5
Pod undocumented
517 0 my ( $class, $args, $data ) = @_;
518 0 my %res;
519  
520 0 my $redisObj = $class->_getRedis($args);
521 0 my $cursor = 0;
522 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cursor != 0 and do { do { () = $redisObj->scan($cursor); foreach my $k (@$keys) { next unless $class->isLlngKey($args, $k); next unless $redisObj->type($k) eq "string"; eval { do { my $v = $redisObj->get($k); next unless $v; my $tmp = unserialize($v); if (ref $data eq "CODE") { $tmp = &$data($tmp, $k); $res{$k} = $tmp if defined $tmp; } elsif ($data) { $data = [$data] unless ref $data; $res{$k}{$_} = $tmp->{$_} foreach (@$data); } else { $res{$k} = $tmp; } } }; if ($@) { print STDERR "Error in session ${k}: $@\n"; } ; } ; $cursor = $new_cursor } } 0 0 0
523 0 my ( $new_cursor, $keys ) = $redisObj->scan($cursor);
524 0 foreach my $k (@$keys) {
525  
526 # Keep only our keys
527 0 ▶ next unless $class->isLlngKey( $args, $k );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isLlngKey($args, $k) 0 0 0
528  
529 # Don't scan sets,...
530 0 ▶ next unless $redisObj->type($k) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
unless $redisObj->type($k) eq "string" 0 0 0
531 0 eval {
532 0 my $v = $redisObj->get($k);
533 0 ▶ next unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
534 0 my $tmp = unserialize($v);
535 0 ▶ if ( ref($data) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'CODE') { } 0 0 0
elsif ($data) { } 0 0 0
536 0 $tmp = &$data( $tmp, $k );
537 0 ▶ $res{$k} = $tmp if ( defined($tmp) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 0 0 0
538 }
539 elsif ($data) {
540 0 ▶ $data = [$data] unless ( ref($data) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
541 0 $res{$k}->{$_} = $tmp->{$_} foreach (@$data);
542 }
543 else {
544 0 $res{$k} = $tmp;
545 }
546 };
547 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
548 0 print STDERR "Error in session $k: $@\n";
549  
550 # Don't delete, it may own to another app
551 #delete $res{$k};
552 }
553 }
554 0 $cursor = $new_cursor;
555 } while ( $cursor != 0 );
556 0 return \%res;
557 }
558  
559 0 ▶sub _getRedis {
Subroutine _getRedis: not called CC=1 SCAR=6.9
560 0 my ( $class, $args ) = @_;
561 0 return Web::Session::Store::Redis->_getRedis($args);
562 }
563  
564 7 1;
565 __END__
« Session/Store/LDAP.pm ↑ Summary Session/MySQLJSON.pm »