Session/Redis.pm

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