Session/_common.pm

filter: Statementstmt 96.0% 145 / 151 Branchbran 91.0% 71 / 78 Conditioncond 92.3% 24 / 26 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 2 totaltotal 93.3% 279 / 299 CC 56 SCAR 15.0
CC
56
Cov
94%
_forEachSession
9.3
_checkThresholds
6.0
__ANON__
6.0
CRAP 4.5
« Session/Store/DBI.pm ↑ Summary Session/Serialize/JSON.pm »
1 package Web::Session::_common;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use AutoLoader 'AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 our $VERSION = '1.0.0';
7  
8 # Number of sessions read per query by _forEachSession(). Values outside
9 # 1..1_000_000 fall back to the default (a huge value would break LIMIT)
10 0 our $BatchSize = 1000;
11  
12 # Attributes of the handles used by class methods (searchOn(), ...), made fork
13 # safe. These handles are shared through DBI->connect_cached(): in a pre-fork
14 # server, if the parent called a class method before forking, every child
15 # would get the parent's handle, so its socket: concurrent use corrupts the
16 # protocol and a child exiting closes the parent's server connection.
17 # - private_ws_pid: connect_cached() uses the attributes as part of its cache
18 # key, so each process gets (and caches) its own handle
19 # - AutoInactiveDestroy: a handle inherited from the parent is not closed on
20 # the server when the child destroys it
21 # Threads (ithreads) share the pid, but each one has its own interpreter, so
22 # its own connect_cached() cache: a handle is never shared between threads.
23 # A store connected before a fork is handled by _forgetInheritedHandle() in
24 # Store/DBI.pm
25 454 ▶sub _forkSafeAttr {
Subroutine _forkSafeAttr: called CC=1 SCAR=0.0
26 454 my ( $class, $attr ) = @_;
27 454 return { %$attr, AutoInactiveDestroy => 1, private_ws_pid => $$ };
28 }
29  
30 102 ▶sub _tabInTab {
Subroutine _tabInTab: called CC=5 SCAR=16.1
31 102 my ( $class, $t1, $t2 ) = @_;
32  
33 # if no fields are required, return 0
34 102 ▶ return 0 unless ( @$t1 and @$t2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @$t1 and @$t2 36 66 102
Condition66%2/3
@$t1 and @$t2
!ll&&!rl&&r
36066
MC/DC50%1/2
@$t1 and @$t2
@$t1 @$t2
Truth table66%2/3
A and B
A= @$t1B= @$t2
ABresult
0X0
100
111
35 66 foreach my $f (@$t1) {
36 89 ▶ unless ( grep { $_ eq $f } @$t2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $f;} @$t2) 35 54 89
37 35 return 0;
38 }
39 }
40 31 return 1;
41 }
42  
43 160 ▶sub _fieldIsIndexed {
Subroutine _fieldIsIndexed: called CC=2 SCAR=6.9
44 160 my ( $class, $args, $field ) = @_;
45 my $index =
46 160 ▶ ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 6 154 160
47 160 return ( grep { $_ eq $field } @$index );
48 }
49  
50 # Check the structure of a deleteIfLowerThan() rule
51 71 ▶sub _checkRule {
Subroutine _checkRule: called CC=5 SCAR=16.1
52 71 my ( $class, $rule ) = @_;
53 71 ▶ unless ( ref($rule) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rule eq "HASH") 6 65 71
54 6 print STDERR "deleteIfLowerThan: rule must be a hash reference\n";
55 6 return 0;
56 }
57 65 foreach my $type (qw(or and not)) {
58 182 ▶ next unless defined $rule->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule->{$type} 95 87 182
59 87 ▶ unless ( ref( $rule->{$type} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rule->{$type} eq "HASH") 13 74 87
60 13 print STDERR "deleteIfLowerThan: $type must be a hash reference\n";
61 13 return 0;
62 }
63 }
64 52 return 1;
65 }
66  
67 # deleteIfLowerThan() thresholds are inserted into SQL queries, so they must
68 # be numbers
69 52 ▶sub _checkThresholds {
Subroutine _checkThresholds: called CC=6 SCAR=17.9
70 52 my ( $class, $rule ) = @_;
71 52 ▶ return 1 unless ( ref($rule) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" 0 52 52
72 52 ▶ my $thresholds = $rule->{or} || $rule->{and};
Condition100%3/3
$rule->{'or'} || $rule->{'and'}
l!l&&r!l&&!r
4651
MC/DC100%2/2
$rule->{'or'} || $rule->{'and'}
$rule->{'or'} $rule->{'and'}
Truth table100%3/3
A || B
A= $rule->{'or'}B= $rule->{'and'}
ABresult
1X1
011
000
73 52 ▶ return 1 unless ( ref($thresholds) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thresholds eq "HASH" 1 51 52
74 51 foreach ( values %$thresholds ) {
75 55 ▶ unless ( $class->_isNumber($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_isNumber($_)) 16 39 55
76 16 print STDERR "deleteIfLowerThan: threshold must be a number\n";
77 16 return 0;
78 }
79 }
80 35 return 1;
81 }
82  
83 # Identifier (table or column name) inserted into an SQL query. It is quoted
84 # with the driver's quote_identifier() when the driver supports it the
85 # standard way. PostgreSQL folds unquoted names to lower case, so tables and
86 # columns created without quotes have lower case names: the name is
87 # lowercased before being quoted, which keeps the previous behaviour for
88 # names in mixed case (TableName => 'Sessions' still finds "sessions"). A
89 # "schema.table" name is quoted part by part, and a name that already
90 # contains quotes is left unchanged. Other drivers get the name as before:
91 # quoting could change its case or meaning there
92 1327 ▶sub _quoteIdentifier {
Subroutine _quoteIdentifier: called CC=4 SCAR=13.9
93 1327 my ( $class, $dbh, $name ) = @_;
94 1327 my $driver = $dbh->{Driver}->{Name};
95 1327 ▶ unless ( $driver =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/) 9 1318 1327
96 9 $name =~ s/'/''/g;
97 9 return $name;
98 }
99 1318 ▶ return $name if ( $name =~ /["`]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /["`]/ 7 1311 1318
100 1311 ▶ $name = lc($name) if ( $driver eq 'Pg' );
Branch: 1 branch
BranchTrueFalseTotal
if $driver eq "Pg" 99 1212 1311
101 1311 return join '.', map { $dbh->quote_identifier($_) } split /\./, $name, -1;
102 }
103  
104 # Column of an indexed field in SQL queries. Callers check Index with the
105 # field name, then insert the result of this method: backends that need
106 # another quoting override it
107 111 ▶sub _quoteColumn {
Subroutine _quoteColumn: called CC=1 SCAR=0.0
108 111 my ( $class, $dbh, $field ) = @_;
109 111 return $class->_quoteIdentifier( $dbh, $field );
110 }
111  
112 # Quoted table name, see _quoteIdentifier()
113 225 ▶sub _tableName {
Subroutine _tableName: called CC=2 SCAR=7.1
114 225 my ( $class, $dbh, $args ) = @_;
115 return $class->_quoteIdentifier( $dbh,
116 225 ▶ $args->{TableName} || $Apache::Session::Store::DBI::TableName );
Condition66%2/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
581670
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table66%2/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
117 }
118  
119 # Digits are bounded: MySQL rejects out-of-range literals
120 246 ▶sub _isNumber {
Subroutine _isNumber: called CC=2 SCAR=6.9
121 246 my ( $class, $value ) = @_;
122 246 ▶ return ( defined($value)
Condition100%3/3
defined $value && $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
!ll&&!rl&&r
1498134
MC/DC100%2/2
defined $value && $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
defined $value $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
Truth table100%3/3
A && B
A= defined $valueB= $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/
ABresult
0X0
100
111
123 and $value =~ /^-?[0-9]{1,20}(?:\.[0-9]{1,20})?\z/ );
124 }
125  
126 # The operator of a comparison is inserted into SQL queries
127 200 ▶sub _checkOp {
Subroutine _checkOp: called CC=5 SCAR=16.1
128 200 my ( $class, $op ) = @_;
129 200 ▶ die "_buildCompareExpression: invalid operator '"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $op and $op eq "<" || $op eq ">" 32 168 200
defined $op ? : 24 8 32
Condition100%6/6
defined $op and $op eq "<" || $op eq ">"
!ll&&!rl&&r
824168
$op eq "<" or $op eq ">"
l!l&&r!l&&!r
1135524
MC/DC100%3/3
defined $op and $op eq "<" || $op eq ">"
defined $op $op eq "<" $op eq ">"
Truth table100%4/4
A and B or C
A= defined $opB= $op eq "<"C= $op eq ">"
ABCresult
0XX0
1000
11X1
1011
130 . ( defined $op ? $op : 'undef' ) . "'\n"
131 unless ( defined $op and ( $op eq '<' or $op eq '>' ) );
132 168 return $op;
133 }
134  
135 # searchLt() and searchGt() values are also inserted into SQL queries. Return
136 # the value without surrounding spaces (kept by the Lemonldap::NG CLI), or
137 # undef if it isn't a number
138 191 ▶sub _checkSearchValue {
Subroutine _checkSearchValue: called CC=4 SCAR=13.9
139 191 my ( $class, $op, $value ) = @_;
140 191 ▶ $value =~ s/^\s+|\s+\z//g if ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 179 12 191
141 191 ▶ unless ( $class->_isNumber($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_isNumber($value)) 96 95 191
142 96 ▶ print STDERR 'search'
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 88 8 96
143 . ( $op eq '<' ? 'Lt' : 'Gt' )
144 . ": value must be a number\n";
145 96 return undef;
146 }
147 95 return $value;
148 }
149  
150 # Run the SQL query $sub of searchLt() and searchGt(). The database may
151 # reject the comparison: a cast fails as soon as one session has a value that
152 # isn't an integer. The error is printed, then $scan compares the sessions in
153 # Perl, as the generic search of Lemonldap::NG does. {} if the scan fails
154 # too: these methods don't die (see "ERRORS" in Web::Session)
155 76 ▶sub _searchQuery {
Subroutine _searchQuery: called CC=5 SCAR=16.1
156 76 my ( $class, $op, $sub, $scan ) = @_;
157 76 ▶ my $name = 'search' . ( $op eq '<' ? 'Lt' : 'Gt' );
Branch: 1 branch
BranchTrueFalseTotal
$op eq '<' ? : 38 38 76
158 76 my $res = eval { $sub->() };
159 76 ▶ return $res unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 44 32 76
160 32 my $err = $@;
161 32 chomp $err;
162 32 print STDERR "$name: $err\n";
163 32 ▶ return {} unless ($scan);
Branch: 1 branch
BranchTrueFalseTotal
unless $scan 0 32 32
164 32 print STDERR "$name: comparing all sessions in Perl instead\n";
165 32 $res = eval { $scan->() };
166 32 ▶ return $res unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 20 12 32
167 12 $err = $@;
168 12 chomp $err;
169 12 print STDERR "$name: $err\n";
170 12 return {};
171 }
172  
173 # Perl test of searchLt() and searchGt(), for _searchByTest(). Sessions
174 # without the field are skipped, as by the SQL comparison
175 45 ▶sub _compareTest {
Subroutine _compareTest: called CC=2 SCAR=6.9
176 45 my ( $class, $op, $value ) = @_;
177 return $op eq '<'
178 72 ▶ ? sub { defined( $_[0] ) and $_[0] < $value }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] < $value 58 14 72
179 45 ▶ : sub { defined( $_[0] ) and $_[0] > $value };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$op eq '<' ? : 27 18 45
defined $_[0] and $_[0] > $value 60 14 74
180 }
181  
182 # Perl version of searchLt() and searchGt(): read all sessions and keep those
183 # for which $test->( value of $selectField ) is true. As in SQL, a value that
184 # doesn't start with a number is 0: Perl would read "inf" or "nan" as
185 # Infinity and NaN
186 51 ▶sub _searchByTest {
Subroutine _searchByTest: called CC=1 SCAR=0.0
187 51 my ( $class, $args, $selectField, $test, @fields ) = @_;
188 51 my %res;
189 $class->get_key_from_all_sessions(
190 $args,
191 174 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
192 174 my ( $entry, $id ) = @_;
193 174 my $v = $entry->{$selectField};
194 174 ▶ $v = 0 if ( defined $v and $v !~ /^\s*-?[0-9]/ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and not $v =~ /^\s*-?[0-9]/ 62 112 174
Condition100%3/3
defined $v and not $v =~ /^\s*-?[0-9]/
!ll&&!rl&&r
308262
MC/DC100%2/2
defined $v and not $v =~ /^\s*-?[0-9]/
defined $v not $v =~ /^\s*-?[0-9]/
Truth table100%3/3
A and not(B)
A= defined $vB= not $v =~ /^\s*-?[0-9]/
ABresult
0X0
100
111
195 174 ▶ return undef unless ( $test->($v) );
Branch: 1 branch
BranchTrueFalseTotal
unless &$test($v) 96 78 174
196 78 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 7 71 78
197 7 $res{$id}->{$_} = $entry->{$_} foreach (@fields);
198 }
199 else {
200 71 $res{$id} = $entry;
201 }
202 78 undef;
203 }
204 51 );
205 39 return \%res;
206 }
207  
208 # Get the unserialize sub of a class (populate() may be inherited)
209 181 ▶sub _unserializer {
Subroutine _unserializer: called CC=2 SCAR=7.1
210 181 my ($class) = @_;
211 181 ▶ my $p = $class->can('populate') or die "$class has no populate()\n";
Branch: 1 branch
BranchTrueFalseTotal
my $p = $class->can("populate") or die "$class has no populate()\n" 181 0 181
212 181 return $p->()->{unserialize};
213 }
214  
215 # Batch size (LIMIT) used by _forEachSession() and by the batched purge of
216 # PgJSON: $BatchSize if it is an integer in 1..1_000_000, else 1000
217 121 ▶sub _batchSize {
Subroutine _batchSize: called CC=4 SCAR=13.9
218 121 ▶ my ($limit) = ( $BatchSize // '' ) =~ /^\s*([1-9][0-9]*)\s*\z/;
Condition100%2/2
$Web::Session::_common::BatchSize // ""
l!l
1201
MC/DC100%1/1
$Web::Session::_common::BatchSize // ""
$Web::Session::_common::BatchSize
Truth table100%2/2
A
A= $Web::Session::_common::BatchSize
Aresult
11
00
219 121 ▶ return ( !$limit or $limit > 1_000_000 ) ? 1000 : $limit;
Branch: 1 branch
BranchTrueFalseTotal
!$limit || $limit > 1000000 ? : 7 114 121
Condition100%3/3
not $limit or $limit > 1000000
l!l&&r!l&&!r
52114
MC/DC100%2/2
not $limit or $limit > 1000000
not $limit $limit > 1000000
Truth table100%3/3
not(A) or B
A= not $limitB= $limit > 1000000
ABresult
1X1
011
000
220 }
221  
222 # Call $sub->( $id, $serialized ) for each session. If the driver supports
223 # LIMIT, sessions are read by batches to avoid loading the whole table in
224 # memory. No server-side cursor here: $sub may reuse the database handle.
225 105 ▶sub _forEachSession {
Subroutine _forEachSession: called CC=8 SCAR=22.3
226 105 my ( $class, $dbh, $table_name, $sub ) = @_;
227 105 my $limit = $class->_batchSize;
228 105 my $sql = "SELECT id,a_session FROM $table_name";
229 105 ▶ unless ( $dbh->{Driver}->{Name} =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"Driver"}{"Name"} =~ /^(?:Pg|mysql|MariaDB|SQLite)\z/) 0 105 105
230 0 my $sth = $dbh->prepare_cached($sql);
231 0 $sth->execute;
232 0 ▶ while ( my @row = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $sth->fetchrow_array) 0 0 0
233 0 $sub->(@row);
234 }
235 0 return;
236 }
237  
238 # Not a single snapshot: rows inserted during the iteration with an id
239 # lower than the current position are not visited (harmless for purge
240 # and sessions explorer)
241 105 my ( $last, $rows );
242 105 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@$rows >= $limit and do { do { my $sth = $dbh->prepare_cached($sql . (defined $last ? " WHERE id > ?" : "") . " ORDER BY id LIMIT $limit"); $sth->execute(defined $last ? $last : ()); $rows = $sth->fetchall_arrayref; &$sub(@$_) foreach (@$rows); $last = $rows->[-1][0] if @$rows } } 8 102 110
243 113 ▶ my $sth =
Branch: 1 branch
BranchTrueFalseTotal
defined $last ? : 8 105 113
244 $dbh->prepare_cached( $sql
245 . ( defined($last) ? ' WHERE id > ?' : '' )
246 . " ORDER BY id LIMIT $limit" );
247 111 ▶ $sth->execute( defined($last) ? ($last) : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $last ? : 8 103 111
248 110 $rows = $sth->fetchall_arrayref;
249 110 $sub->(@$_) foreach (@$rows);
250 110 ▶ $last = $rows->[-1]->[0] if (@$rows);
Branch: 1 branch
BranchTrueFalseTotal
if @$rows 108 2 110
251 } while ( @$rows >= $limit );
252 102 return;
253 }
254  
255 34 1;
256 __END__
« Session/Store/DBI.pm ↑ Summary Session/Serialize/JSON.pm »