lib/Redis/SQLite.pm

filter: Statementstmt 97.1% 304 / 313 Branchbran 87.2% 82 / 94 Conditioncond 73.3% 33 / 45 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 95.4% 42 / 44 Podpod 90.0% 36 / 40 totaltotal 91.2% 510 / 559 CC 84 SCAR 14.5
CC
84
Cov
93%
new
11.5
setbit
7.0
getbit
6.9
CRAP 4.3
↑ Summary
1  
2 =head1 NAME
3  
4 Redis::SQLite - Redis-Compatible module which writes to SQLite.
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 =for example begin
11  
12 #!/usr/bin/perl -w
13  
14 use Redis::SQLite;
15 use strict;
16  
17 my $db = Redis::SQLite->new();
18  
19 $db->set( "foo", "bar" );
20  
21 print $db->get( "foo" ) . "\n";
22  
23 =for example end
24  
25  
26 =head1 DESCRIPTION
27  
28 This package is an implementation of the L<Redis> Perl-client API, which
29 stores all data in an SQLite database rather than in RAM.
30  
31 It is B<not> a drop-in replacement, because it doesn't implement all the
32 features you'd expect from the real Redis module. Just enough to be useful.
33  
34 =cut
35  
36 =head1 COMPATIBILITY
37  
38 This module is designed to be source compatible with the L<Redis> module,
39 providing you're only operating upon either sets or simple strings.
40  
41 Specifically we do not support:
42  
43 =over 8
44  
45 =item HASH
46  
47 For example C<hlen>, C<hkeys>, C<hexists>, etc.
48  
49 =item Lists
50  
51 For example C<lset>, C<ltrim>, etc.
52  
53 =item Scripting
54  
55 Perl is itself a wonderful scripting language, so we've no need for Lua support.
56  
57 =item ZSET
58  
59 For example C<zcard>, C<zadd>, C<zcount>, etc.
60  
61 =back
62  
63 All of the set-related primitives are supported, with the exception of C<SSCAN>,
64 and the basic commands for working with string-based keys are also present, such
65 as:
66  
67 =over 8
68  
69 =item append
70  
71 =item del
72  
73 =item exists
74  
75 =item get
76  
77 =item set
78  
79 =item etc..
80  
81 =back
82  
83 =cut
84  
85 =head1 METHODS
86  
87 =cut
88  
89  
90 package Redis::SQLite;
91  
92  
93  
94 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 20 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98  
99 20 our $VERSION = '0.3';
100  
101  
102 =head2 new
103  
104 Constructor. The only (optional) argument is C<path> which will
105 change the default SQLite database-file location, if unspecified
106 C<~/.predis.db> will be used.
107  
108 =cut
109  
110 sub new
111 19 ▶{
Subroutine new: called CC=9 SCAR=24.4
Pod documented
112 19 my ( $proto, %supplied ) = (@_);
113 19 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0190
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
114  
115 19 my $self = {};
116 19 bless( $self, $class );
117  
118 # Get the user's home-directory
119 my $home =
120 19 ▶ $ENV{ 'HOME' } || $ENV{ 'USERPROFILE' } || ( getpwuid($<) )[7] || "C:/";
Condition37%3/8
$ENV{'HOME'} || $ENV{'USERPROFILE'} || (getpwuid $<)[7] || 'C:/'
l!l
190
$ENV{"HOME"} or $ENV{"USERPROFILE"} or (getpwuid $<)[7]
l!l&&r!l&&!r
1900
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
1900
MC/DC0%0/3
$ENV{'HOME'} || $ENV{'USERPROFILE'} || (getpwuid $<)[7] || 'C:/'
$ENV{"HOME"} $ENV{"USERPROFILE"} (getpwuid $<)[7]
Truth table25%1/4
A or B or C
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}C= (getpwuid $<)[7]
ABCresult
1XX1
01X1
0011
0000
121  
122 # Create ~/.predis.db unless an alternative path was specified.
123 19 ▶ my $file = $supplied{ 'path' } || "$home/.predis.db";
Condition50%1/2
$supplied{'path'} || "$home/.predis.db"
l!l
190
MC/DC0%0/1
$supplied{'path'} || "$home/.predis.db"
$supplied{'path'}
Truth table50%1/2
A
A= $supplied{'path'}
Aresult
11
00
124  
125 19 my $create = 1;
126 19 ▶ $create = 0 if ( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 19 19
127  
128 19 $self->{ 'db' } =
129 DBI->connect( "dbi:SQLite:dbname=$file", "", "", { AutoCommit => 1 } );
130  
131 #
132 # Populate the database tables, if it was missing.
133 #
134 19 ▶ if ($create)
Branch: 1 branch
BranchTrueFalseTotal
if ($create) 19 0 19
135 {
136 19 $self->{ 'db' }->do(
137 "CREATE TABLE string (id INTEGER PRIMARY KEY, key UNIQUE, val );");
138 19 $self->{ 'db' }
139 ->do("CREATE TABLE sets (id INTEGER PRIMARY KEY, key, val );");
140 }
141  
142 #
143 # This is potentially risky, but improves the throughput by several
144 # orders of magnitude.
145 #
146 19 ▶ if ( !$ENV{ 'SAFE' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SAFE"}) 19 0 19
147 {
148 19 $self->{ 'db' }->do("PRAGMA synchronous = OFF");
149 19 $self->{ 'db' }->do("PRAGMA journal_mode = MEMORY");
150 }
151  
152 19 return $self;
153 }
154  
155  
156 =head2 append
157  
158 Append the given string to the contents of the existing key, creating it
159 if didn't previously exist.
160  
161 =cut
162  
163 sub append
164 2 ▶{
Subroutine append: called CC=1 SCAR=0.0
Pod documented
165 2 my ( $self, $key, $data ) = (@_);
166  
167 2 my $r = $self->get($key);
168 2 $r .= $data;
169 2 $self->set( $key, $r );
170 }
171  
172  
173 =head2 exists
174  
175 Does the given key exist?
176  
177 =cut
178  
179 sub exists
180 37 ▶{
Subroutine exists: called CC=5 SCAR=16.1
Pod documented
181 37 my ( $self, $key ) = (@_);
182  
183 37 my $sql = $self->{ 'db' }->prepare("SELECT key FROM string WHERE key=?");
184 37 $sql->execute($key);
185 37 ▶ my $x = $sql->fetchrow_array() || undef;
Condition100%2/2
$sql->fetchrow_array || undef
l!l
2215
MC/DC100%1/1
$sql->fetchrow_array || undef
$sql->fetchrow_array
Truth table100%2/2
A
A= $sql->fetchrow_array
Aresult
11
00
186 37 $sql->finish();
187  
188 37 ▶ if ($x)
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 22 15 37
189 {
190 22 return 1;
191 }
192  
193 15 $sql = $self->{ 'db' }->prepare("SELECT key FROM sets WHERE key=?");
194 15 $sql->execute($key);
195 15 ▶ $x = $sql->fetchrow_array() || undef;
Condition100%2/2
$sql->fetchrow_array || undef
l!l
114
MC/DC100%1/1
$sql->fetchrow_array || undef
$sql->fetchrow_array
Truth table100%2/2
A
A= $sql->fetchrow_array
Aresult
11
00
196 15 $sql->finish();
197  
198 15 ▶ if ($x)
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 1 14 15
199 {
200 1 return 1;
201 }
202  
203 14 return 0;
204 }
205  
206  
207 =head2 get
208  
209 Get the value of a string-key. Returns C<undef> if the key didn't exist,
210 or contain data.
211  
212 =cut
213  
214 sub get
215 352 ▶{
Subroutine get: called CC=2 SCAR=6.9
Pod documented
216 352 my ( $self, $key ) = (@_);
217  
218 352 ▶ if ( !$self->{ 'get' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"get"}) 12 340 352
219 {
220 $self->{ 'get' } =
221 12 $self->{ 'db' }->prepare("SELECT val FROM string WHERE key=?");
222 }
223 352 $self->{ 'get' }->execute($key);
224 352 my $x = $self->{ 'get' }->fetchrow_array();
225 352 $self->{ 'get' }->finish();
226 352 return ($x);
227 }
228  
229  
230  
231 =head2 getset
232  
233 Update the value of a key, and return the previous value if any.
234  
235 =cut
236  
237 sub getset
238 1 ▶{
Subroutine getset: called CC=1 SCAR=0.0
Pod documented
239 1 my ( $self, $key, $val ) = (@_);
240  
241 1 my $old = $self->get($key);
242 1 $self->set( $key, $val );
243  
244 1 return ($old);
245 }
246  
247  
248 =head2 getrange
249  
250 Return the chunk of the key's value between the given offsets.
251  
252 =cut
253  
254 sub getrange
255 4 ▶{
Subroutine getrange: called CC=3 SCAR=11.0
Pod documented
256 4 my ( $self, $key, $start, $end ) = (@_);
257  
258 4 my $val = $self->get($key);
259 4 my $s = $start;
260 4 my $e = $end;
261  
262 4 ▶ if ( $s < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($s < 0) 1 3 4
263 {
264 1 $s = length($val) + $s;
265 }
266 4 ▶ if ( $e < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($e < 0) 2 2 4
267 {
268 2 $e = length($val) + $e;
269 }
270  
271 4 return ( substr( $val, $s, ( $e - $s + 1 ) ) );
272 }
273  
274  
275  
276 =head2 strlen
277  
278 Return the length of the given value of the given key.
279  
280 =cut
281  
282 sub strlen
283 6 ▶{
Subroutine strlen: called CC=2 SCAR=7.4
Pod documented
284 6 my ( $self, $key ) = (@_);
285  
286 6 my $data = $self->get($key);
287 6 ▶ if ( defined($data) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 6 0 6
288 {
289 6 return ( length($data) );
290 }
291 0 return 0;
292 }
293  
294  
295 =head2 rename
296  
297 Rename a string key. Deleting the target if it exists.
298  
299 =cut
300  
301 sub rename
302 1 ▶{
Subroutine rename: called CC=1 SCAR=0.0
Pod documented
303 1 my ( $self, $key, $new_name ) = (@_);
304  
305 1 $self->del($new_name);
306  
307 1 my $val = $self->get($key);
308 1 $self->set( $new_name, $val );
309  
310 1 $self->del($key);
311 }
312  
313  
314 =head2 renamenx
315  
316 Attempt to rename the given key, if the destination exists then
317 nothing happens.
318  
319 =cut
320  
321 sub renamenx
322 2 ▶{
Subroutine renamenx: called CC=2 SCAR=6.9
Pod documented
323 2 my ( $self, $key, $new_name ) = (@_);
324  
325 2 ▶ return 0 if ( $self->exists($new_name) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists($new_name) 1 1 2
326  
327 # Get the value and save it.
328 1 my $val = $self->get($key);
329 1 $self->set( $new_name, $val );
330  
331 # Remove the original
332 1 $self->del($key);
333  
334 1 return "OK";
335 }
336  
337  
338 =head2 set
339  
340 Set the value of a string-key.
341  
342 =cut
343  
344 sub set
345 175 ▶{
Subroutine set: called CC=2 SCAR=6.9
Pod documented
346 175 my ( $self, $key, $val ) = (@_);
347  
348 175 ▶ if ( !$self->{ 'ins' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ins"}) 13 162 175
349 {
350 $self->{ 'ins' } =
351 13 $self->{ 'db' }
352 ->prepare("INSERT OR REPLACE INTO string (key,val) VALUES( ?,? )");
353 }
354 175 $self->{ 'ins' }->execute( $key, $val );
355 175 $self->{ 'ins' }->finish();
356  
357 }
358  
359  
360 =head2 setnx
361  
362 Store the given value in the named key, unless that key exists.
363  
364 =cut
365  
366 sub setnx
367 1 ▶{
Subroutine setnx: called CC=2 SCAR=9.2
Pod documented
368 1 my ( $self, $key, $val ) = (@_);
369  
370 1 ▶ return 0 if ( $self->exists($key) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists($key) 1 0 1
371  
372 0 $self->set( $key, $val );
373 0 return 1;
374 }
375  
376  
377 =head2 setrange
378  
379 Insert some new data at the given offset of the specific key's value.
380  
381 If the current length of the key's value is too short it is NULL-padded
382 first.
383  
384 =cut
385  
386 sub setrange
387 2 ▶{
Subroutine setrange: called CC=3 SCAR=11.0
Pod documented
388 2 my ( $self, $key, $offset, $data ) = (@_);
389  
390 2 my $val = $self->get($key);
391  
392 2 ▶ while ( ( $val ? length($val) : 0 ) < $offset )
Branch: 2 branches
BranchTrueFalseTotal
if (($val ? length $val : 0) < $offset) 6 2 8
$val ? : 7 1 8
393 {
394 6 $val .= chr(0x00);
395 }
396  
397  
398 2 substr( $val, $offset, length($data), $data );
399 2 $self->set( $key, $val );
400 2 return ( length($val) );
401 }
402  
403  
404 =head2 type
405  
406 Return the type of the named key.
407  
408 =cut
409  
410 sub type
411 6 ▶{
Subroutine type: called CC=5 SCAR=16.1
Pod documented
412 6 my ( $self, $key ) = (@_);
413  
414  
415 6 my $sql = $self->{ 'db' }->prepare("SELECT key FROM string WHERE key=?");
416 6 $sql->execute($key);
417 6 ▶ my $x = $sql->fetchrow_array() || undef;
Condition100%2/2
$sql->fetchrow_array || undef
l!l
33
MC/DC100%1/1
$sql->fetchrow_array || undef
$sql->fetchrow_array
Truth table100%2/2
A
A= $sql->fetchrow_array
Aresult
11
00
418 6 $sql->finish();
419  
420 6 ▶ return 'string' if ($x);
Branch: 1 branch
BranchTrueFalseTotal
if $x 3 3 6
421  
422 3 $sql = $self->{ 'db' }->prepare("SELECT key FROM sets WHERE key=?");
423 3 $sql->execute($key);
424 3 ▶ $x = $sql->fetchrow_array() || undef;
Condition100%2/2
$sql->fetchrow_array || undef
l!l
12
MC/DC100%1/1
$sql->fetchrow_array || undef
$sql->fetchrow_array
Truth table100%2/2
A
A= $sql->fetchrow_array
Aresult
11
00
425 3 $sql->finish();
426  
427 3 ▶ return 'set' if ($x);
Branch: 1 branch
BranchTrueFalseTotal
if $x 1 2 3
428  
429 2 return undef;
430 }
431  
432  
433 =head2 incr
434  
435 Increment and return the value of an (integer) string-key.
436  
437 =cut
438  
439 sub incr
440 9 ▶{
Subroutine incr: called CC=1 SCAR=0.0
Pod documented
441 9 my ( $self, $key ) = (@_);
442  
443 9 return ( $self->incrby( $key, 1 ) );
444 }
445  
446  
447  
448 =head2 incrby
449  
450 Increment and return the value of an (integer) string-key.
451  
452 =cut
453  
454 sub incrby
455 12 ▶{
Subroutine incrby: called CC=3 SCAR=11.0
Pod documented
456 12 my ( $self, $key, $amt ) = (@_);
457  
458 12 ▶ $amt = 1 if ( !defined($amt) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amt 0 12 12
459  
460 12 ▶ my $cur = $self->get($key) || 0;
Condition100%2/2
$self->get($key) || 0
l!l
93
MC/DC100%1/1
$self->get($key) || 0
$self->get($key)
Truth table100%2/2
A
A= $self->get($key)
Aresult
11
00
461 12 $cur += $amt;
462 12 $self->set( $key, $cur );
463  
464 12 return ($cur);
465 }
466  
467  
468 =head2 decr
469  
470 Decrement and return the value of an (integer) string-key.
471  
472 =cut
473  
474 sub decr
475 4 ▶{
Subroutine decr: called CC=1 SCAR=0.0
Pod documented
476 4 my ( $self, $key ) = (@_);
477  
478 4 return ( $self->decrby( $key, 1 ) );
479 }
480  
481  
482  
483 =head2 decrby
484  
485 Decrement and return the value of an (integer) string-key.
486  
487 =cut
488  
489 sub decrby
490 12 ▶{
Subroutine decrby: called CC=3 SCAR=11.0
Pod documented
491 12 my ( $self, $key, $amt ) = (@_);
492  
493 12 ▶ $amt = 1 if ( !defined($amt) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amt 0 12 12
494  
495 12 ▶ my $cur = $self->get($key) || 0;
Condition100%2/2
$self->get($key) || 0
l!l
102
MC/DC100%1/1
$self->get($key) || 0
$self->get($key)
Truth table100%2/2
A
A= $self->get($key)
Aresult
11
00
496 12 $cur -= $amt;
497 12 $self->set( $key, $cur );
498  
499 12 return ($cur);
500 }
501  
502  
503 =head2 del
504  
505 Delete a given key, regardless of whether it holds a string or a set.
506  
507 =cut
508  
509 sub del
510 78 ▶{
Subroutine del: called CC=3 SCAR=11.0
Pod documented
511 78 my ( $self, $key ) = (@_);
512  
513 # strings
514 78 my $str = $self->{ 'db' }->prepare("DELETE FROM string WHERE key=?");
515 78 $str->execute($key);
516 78 $str->finish();
517  
518 # Deleted a string-keyu
519 78 ▶ return 1 if ( $str->rows > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $str->rows > 0 72 6 78
520  
521 # sets
522 6 my $set = $self->{ 'db' }->prepare("DELETE FROM sets WHERE key=?");
523 6 $set->execute($key);
524 6 $set->finish();
525  
526 # Deleted a set-key.
527 6 ▶ return 1 if ( $set->rows > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $set->rows > 0 1 5 6
528  
529 # Deleted nothing.
530 5 return 0;
531 }
532  
533  
534 =head2 keys
535  
536 Return the names of each known key.
537  
538 These can be optionally filtered by a (perl) regular expression, for example:
539  
540 =for example begin
541  
542 $redis->set( "foo", 1 );
543 $redis->set( "moo", 1 );
544  
545 $redis->keys( "^f" ); # -> [ "foo" ]
546 $redis->keys( "oo\$" ); # -> [ "foo", "moo" ]
547  
548 =for example end
549  
550 =cut
551  
552 sub keys
553 42 ▶{
Subroutine keys: called CC=6 SCAR=17.9
Pod documented
554 42 my ( $self, $pattern ) = (@_);
555  
556 # Get all keys into this hash
557 42 my %known;
558  
559 # We run the same query against two tables.
560 42 foreach my $table (qw! string sets !)
561 {
562 # Get the names of the key.
563 84 my $str = $self->{ 'db' }->prepare("SELECT key FROM $table");
564 84 $str->execute();
565 84 ▶ while ( my ($name) = $str->fetchrow_array )
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = $str->fetchrow_array) 122 84 206
566 {
567 122 $known{ $name } += 1;
568 }
569 84 $str->finish();
570 }
571  
572 # The keys we've found
573 42 my @keys = keys %known;
574  
575 42 ▶ if ($pattern)
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern) { } 4 38 42
576 {
577 4 my @ret;
578 4 foreach my $ent (@keys)
579 {
580 30 ▶ push( @ret, $ent ) if ( $ent =~ /$pattern/ );
Branch: 1 branch
BranchTrueFalseTotal
if $ent =~ /$pattern/ 13 17 30
581 }
582 4 return (@ret);
583 }
584 else
585 {
586 38 return (@keys);
587 }
588 }
589  
590  
591 =head2 randomkey
592  
593 Return the name of a random key.
594  
595 =cut
596  
597 sub randomkey
598 1 ▶{
Subroutine randomkey: called CC=3 SCAR=11.0
Pod documented
599 1 my ($self) = (@_);
600  
601 # Get all keys into this hash
602 1 my %known;
603  
604 # We run the same query against two tables.
605 1 foreach my $table (qw! string sets !)
606 {
607 # Get the names of the key.
608 2 my $str = $self->{ 'db' }->prepare("SELECT key FROM $table");
609 2 $str->execute();
610 2 ▶ while ( my ($name) = $str->fetchrow_array )
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = $str->fetchrow_array) 9 2 11
611 {
612 9 $known{ $name } += 1;
613 }
614 2 $str->finish();
615 }
616  
617 # The keys we've found
618 1 my @keys = CORE::keys %known;
619  
620 1 return ( $keys[rand @keys] );
621 }
622  
623  
624 =head2 smembers
625  
626 Return the members of the given set.
627  
628 =cut
629  
630 sub smembers
631 13 ▶{
Subroutine smembers: called CC=3 SCAR=11.0
Pod documented
632 13 my ( $self, $key ) = (@_);
633  
634 13 ▶ if ( !$self->{ 'smembers' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"smembers"}) 3 10 13
635 {
636 $self->{ 'smembers' } =
637 3 $self->{ 'db' }->prepare("SELECT val FROM sets WHERE key=?");
638 }
639 13 $self->{ 'smembers' }->execute($key);
640  
641 13 my @vals;
642 13 ▶ while ( my ($name) = $self->{ 'smembers' }->fetchrow_array )
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = $self->{"smembers"}->fetchrow_array) 42 13 55
643 {
644 42 push( @vals, $name );
645 }
646 13 $self->{ 'smembers' }->finish();
647  
648 13 return (@vals);
649 }
650  
651  
652 =head2 smove
653  
654 Move a member from a given set to a new one.
655  
656 =cut
657  
658 sub smove
659 2 ▶{
Subroutine smove: called CC=2 SCAR=6.9
Pod documented
660 2 my ( $self, $src, $dst, $ent ) = (@_);
661  
662 # Get the value from the original set
663 2 my $sql = $self->{ 'db' }
664 ->prepare("UPDATE sets SET key=? WHERE ( key=? AND val=?)");
665  
666 2 $sql->execute( $dst, $src, $ent );
667 2 $sql->finish();
668  
669 2 ▶ if ( $sql->rows > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql->rows > 0) 1 1 2
670 {
671 1 return 1;
672 }
673 1 return 0;
674  
675 }
676  
677  
678 =head2 sismember
679  
680 Is the given item a member of the set?
681  
682 =cut
683  
684 sub sismember
685 22 ▶{
Subroutine sismember: called CC=4 SCAR=13.9
Pod documented
686 22 my ( $self, $set, $key ) = (@_);
687  
688 my $sql =
689 22 $self->{ 'db' }->prepare("SELECT val FROM sets WHERE key=? AND val=?");
690 22 $sql->execute( $set, $key );
691  
692 22 ▶ my $x = $sql->fetchrow_array() || undef;
Condition100%2/2
$sql->fetchrow_array || undef
l!l
1111
MC/DC100%1/1
$sql->fetchrow_array || undef
$sql->fetchrow_array
Truth table100%2/2
A
A= $sql->fetchrow_array
Aresult
11
00
693 22 $sql->finish();
694  
695 22 ▶ if ( defined($x) && ( $x eq $key ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x and $x eq $key) 11 11 22
Condition66%2/3
defined $x and $x eq $key
!ll&&!rl&&r
11011
MC/DC50%1/2
defined $x and $x eq $key
defined $x $x eq $key
Truth table66%2/3
A and B
A= defined $xB= $x eq $key
ABresult
0X0
100
111
696 {
697 11 return 1;
698 }
699 11 return 0;
700 }
701  
702  
703 =head2 sadd
704  
705 Add a member to a set.
706  
707 =cut
708  
709 sub sadd
710 65 ▶{
Subroutine sadd: called CC=3 SCAR=11.1
Pod documented
711 65 my ( $self, $key, $val ) = (@_);
712  
713 65 ▶ if ( !$self->{ 'sadd' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sadd"}) 7 58 65
714 {
715 $self->{ 'sadd' } =
716 7 $self->{ 'db' }->prepare(
717 "INSERT INTO sets (key,val) SELECT ?,? WHERE NOT EXISTS( SELECT key, val FROM sets WHERE key=? AND val=? );"
718 );
719  
720 }
721 65 $self->{ 'sadd' }->execute( $key, $val, $key, $val );
722 65 $self->{ 'sadd' }->finish();
723  
724 65 ▶ if ( $self->{ 'sadd' }->rows > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sadd"}->rows > 0) 65 0 65
725 {
726 65 return 1;
727 }
728 0 return 0;
729  
730 }
731  
732  
733 =head2 srem
734  
735 Remove a member from a set.
736  
737 =cut
738  
739 sub srem
740 9 ▶{
Subroutine srem: called CC=3 SCAR=11.1
Pod documented
741 9 my ( $self, $key, $val ) = (@_);
742  
743 9 ▶ if ( !$self->{ 'srem' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"srem"}) 3 6 9
744 {
745 $self->{ 'srem' } =
746 3 $self->{ 'db' }->prepare("DELETE FROM sets WHERE (key=? AND val=?)");
747 }
748 9 $self->{ 'srem' }->execute( $key, $val );
749 9 $self->{ 'srem' }->finish();
750  
751 9 ▶ if ( $self->{ 'srem' }->rows > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"srem"}->rows > 0) 9 0 9
752 {
753 9 return 1;
754 }
755 0 return 0;
756 }
757  
758  
759 =head2 spop
760  
761 Remove a given number of elements from the named set, and return them.
762  
763 =cut
764  
765 sub spop
766 4 ▶{
Subroutine spop: called CC=4 SCAR=13.9
Pod documented
767 4 my ( $self, $key, $count ) = (@_);
768  
769 4 ▶ $count = 1 if ( !defined($count) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 1 3 4
770  
771 4 my @res;
772  
773 4 ▶ while ( ( $count > 0 ) && ( $count <= $self->scard($key) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0 and $count <= $self->scard($key)) 5 4 9
Condition100%3/3
$count > 0 and $count <= $self->scard($key)
!ll&&!rl&&r
315
MC/DC100%2/2
$count > 0 and $count <= $self->scard($key)
$count > 0 $count <= $self->scard($key)
Truth table100%3/3
A and B
A= $count > 0B= $count <= $self->scard($key)
ABresult
0X0
100
111
774 {
775 5 my $rand = $self->srandmember($key);
776 5 push( @res, $rand );
777 5 $self->srem( $key, $rand );
778  
779 5 $count -= 1;
780 }
781  
782 4 return (@res);
783 }
784  
785  
786  
787 =head2 srandmember
788  
789 Fetch the value of a random member from a set.
790  
791 =cut
792  
793 sub srandmember
794 6 ▶{
Subroutine srandmember: called CC=4 SCAR=14.0
Pod documented
795 6 my ( $self, $key ) = (@_);
796  
797 6 ▶ if ( !$self->{ 'srandommember' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"srandommember"}) 2 4 6
798 {
799 $self->{ 'srandommember' } =
800 2 ▶ $self->{ 'db' }->prepare(
Branch: 1 branch
BranchTrueFalseTotal
$self->{"srandommember"} = $self->{"db"}->prepare("SELECT val FROM sets where key=? ORDER BY RANDOM() LIMIT 1") or die "Failed to prepare" 2 0 2
801 "SELECT val FROM sets where key=? ORDER BY RANDOM() LIMIT 1") or
802 die "Failed to prepare";
803 }
804 6 $self->{ 'srandommember' }->execute($key);
805 6 ▶ my $x = $self->{ 'srandommember' }->fetchrow_array() || "";
Condition50%1/2
$self->{'srandommember'}->fetchrow_array || ''
l!l
60
MC/DC0%0/1
$self->{'srandommember'}->fetchrow_array || ''
$self->{'srandommember'}->fetchrow_array
Truth table50%1/2
A
A= $self->{'srandommember'}->fetchrow_array
Aresult
11
00
806 6 $self->{ 'srandommember' }->finish();
807  
808 6 return ($x);
809 }
810  
811  
812 =head2 sunion
813  
814 Return the values which are present in each of the sets named, duplicates
815 will only be returned one time.
816  
817 For example:
818  
819 =for example begin
820  
821 $redis->sadd( "one", 1 );
822 $redis->sadd( "one", 2 );
823 $redis->sadd( "one", 3 );
824  
825 $redis->sadd( "two", 2 );
826 $redis->sadd( "two", 3 );
827 $redis->sadd( "two", 4 );
828  
829 $redis->sunion( "one", "two" ); # -> [ 1,2,3,4 ]
830  
831 =for example end
832  
833 =cut
834  
835 sub sunion
836 2 ▶{
Subroutine sunion: called CC=3 SCAR=11.0
Pod documented
837 2 my ( $self, @keys ) = (@_);
838  
839  
840 2 my %result;
841  
842 2 foreach my $key (@keys)
843 {
844 4 my @vals = $self->smembers($key);
845 4 foreach my $val (@vals)
846 {
847 12 $result{ $val } += 1;
848 }
849 }
850  
851 2 return ( CORE::keys(%result) );
852 }
853  
854  
855 =head2 sunionstore
856  
857 Store the values which are present in each of the named sets in a new set.
858  
859 =cut
860  
861 sub sunionstore
862 1 ▶{
Subroutine sunionstore: called CC=2 SCAR=6.9
Pod documented
863 1 my ( $self, $dest, @keys ) = (@_);
864  
865 # Get the union
866 1 my @union = $self->sunion(@keys);
867  
868 # Delete the current contents of the destination.
869 1 $self->del($dest);
870  
871 # Now store the members
872 1 foreach my $ent (@union)
873 {
874 6 $self->sadd( $dest, $ent );
875 }
876  
877 # Return the number of entries added
878 1 return ( scalar @union );
879 }
880  
881  
882 =head2 sinter
883  
884 Return only those members who exist in each of the named sets.
885  
886 =for example begin
887  
888 $redis->sadd( "one", 1 );
889 $redis->sadd( "one", 2 );
890 $redis->sadd( "one", 3 );
891  
892 $redis->sadd( "two", 2 );
893 $redis->sadd( "two", 3 );
894 $redis->sadd( "two", 4 );
895  
896 $redis->sinter( "one", "two" ); # -> [ 2,3 ]
897  
898 =for example end
899  
900 =cut
901  
902 sub sinter
903 3 ▶{
Subroutine sinter: called CC=5 SCAR=16.1
Pod documented
904 3 my ( $self, @names ) = (@_);
905  
906 3 my %seen;
907  
908 3 foreach my $key (@names)
909 {
910 6 my @vals = $self->smembers($key);
911 6 foreach my $val (@vals)
912 {
913 20 $seen{ $val } += 1;
914 }
915 }
916  
917 3 my @result;
918  
919 3 foreach my $key ( CORE::keys(%seen) )
920 {
921 15 ▶ if ( $seen{ $key } == scalar @names )
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$key} == scalar @names) 5 10 15
922 {
923 5 push( @result, $key );
924 }
925 }
926 3 return (@result);
927 }
928  
929  
930  
931 =head2 sinterstore
932  
933 Store those members who exist in all the named sets in a new set.
934  
935 =cut
936  
937 sub sinterstore
938 1 ▶{
Subroutine sinterstore: called CC=2 SCAR=6.9
Pod documented
939 1 my ( $self, $dest, @names ) = (@_);
940  
941 # Get the values that intersect
942 1 my @update = $self->sinter(@names);
943  
944 # Delete the current contents of the destination.
945 1 $self->del($dest);
946  
947 # Now store the members
948 1 foreach my $ent (@update)
949 {
950 2 $self->sadd( $dest, $ent );
951 }
952  
953 # Return the number of entries added
954 1 return ( scalar @update );
955 }
956  
957  
958 =head2 scard
959  
960 Count the number of entries in the given set.
961  
962 =cut
963  
964 sub scard
965 26 ▶{
Subroutine scard: called CC=3 SCAR=11.0
Pod documented
966 26 my ( $self, $key ) = (@_);
967  
968 26 ▶ if ( !$self->{ 'scard' } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"scard"}) 6 20 26
969 {
970 $self->{ 'scard' } =
971 6 $self->{ 'db' }->prepare("SELECT COUNT(id) FROM sets where key=?");
972 }
973 26 $self->{ 'scard' }->execute($key);
974 26 ▶ my $count = $self->{ 'scard' }->fetchrow_array() || 0;
Condition100%2/2
$self->{'scard'}->fetchrow_array || 0
l!l
215
MC/DC100%1/1
$self->{'scard'}->fetchrow_array || 0
$self->{'scard'}->fetchrow_array
Truth table100%2/2
A
A= $self->{'scard'}->fetchrow_array
Aresult
11
00
975 26 $self->{ 'scard' }->finish();
976  
977 26 return ($count);
978 }
979  
980  
981  
982 =head2 bitcount
983  
984 Count the number of set bits in the content of the given key.
985  
986 =cut
987  
988 sub bitcount
989 74 ▶{
Subroutine bitcount: called CC=2 SCAR=6.9
Pod documented
990 74 my ( $self, $key ) = (@_);
991  
992 74 my $val = $self->get($key);
993  
994 # Use a lookup-table for each byte.
995 74 my @bitcounts = ( 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2,
996 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3,
997 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4,
998 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4,
999 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4,
1000 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,
1001 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
1002 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1003 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3,
1004 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,
1005 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4,
1006 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
1007 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5,
1008 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7,
1009 6, 7, 7, 8
1010 );
1011  
1012 74 my $sum = 0;
1013  
1014 74 foreach my $char ( split( //, $val ) )
1015 {
1016 316 $sum += $bitcounts[ord($char)];
1017 }
1018  
1019 74 return ($sum);
1020 }
1021  
1022  
1023 sub setbit
1024 88 ▶{
Subroutine setbit: called CC=7 SCAR=19.5
Pod undocumented
1025 88 my ( $self, $key, $offset, $value ) = (@_);
1026  
1027 88 ▶ my $val = $self->get($key) || "";
Condition100%2/2
$self->get($key) || ''
l!l
2068
MC/DC100%1/1
$self->get($key) || ''
$self->get($key)
Truth table100%2/2
A
A= $self->get($key)
Aresult
11
00
1028 88 ▶ my $len = length($val) || 0;
Condition100%2/2
length $val || 0
l!l
2068
MC/DC100%1/1
length $val || 0
length $val
Truth table100%2/2
A
A= length $val
Aresult
11
00
1029  
1030 # Convert to binary.
1031 88 my $bin;
1032  
1033 # Convert the current value to binary.
1034 88 foreach my $byte ( split( //, $val ) )
1035 {
1036 20 $bin .= unpack( "B*", $byte );
1037 }
1038  
1039 # Ensure we have a long-enough string.
1040 88 ▶ while ( $offset >= ( $bin ? length($bin) : 0 ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($offset >= ($bin ? length $bin : 0)) 300 88 388
$bin ? : 320 68 388
1041 {
1042 300 $bin .= "00000000";
1043 }
1044  
1045 # Change the bit.
1046 88 substr( $bin, $offset, 1, $value );
1047  
1048 88 my $updated;
1049 88 ▶ while ( length($bin) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin) 320 88 408
1050 {
1051 320 my $next = substr( $bin, 0, 8 );
1052 320 $bin = substr( $bin, 8 );
1053  
1054 320 $updated .= pack( "B*", $next );
1055  
1056 }
1057  
1058 88 $self->set( $key, $updated );
1059 }
1060  
1061  
1062  
1063 sub getbit
1064 40 ▶{
Subroutine getbit: called CC=6 SCAR=19.3
Pod undocumented
1065 40 my ( $self, $key, $offset ) = (@_);
1066  
1067 40 ▶ my $val = $self->get($key) || "";
Condition50%1/2
$self->get($key) || ''
l!l
400
MC/DC0%0/1
$self->get($key) || ''
$self->get($key)
Truth table50%1/2
A
A= $self->get($key)
Aresult
11
00
1068 40 ▶ my $len = length($val) || 0;
Condition50%1/2
length $val || 0
l!l
400
MC/DC0%0/1
length $val || 0
length $val
Truth table50%1/2
A
A= length $val
Aresult
11
00
1069  
1070 # Convert to binary.
1071 40 my $bin;
1072  
1073 # Convert the current value to binary.
1074 40 foreach my $byte ( split( //, $val ) )
1075 {
1076 40 $bin .= unpack( "B*", $byte );
1077 }
1078  
1079 # Ensure we have a long-enough string.
1080 40 ▶ while ( $offset >= ( $bin ? length($bin) : 0 ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($offset >= ($bin ? length $bin : 0)) 0 40 40
$bin ? : 40 0 40
1081 {
1082 0 $bin .= "00000000";
1083 }
1084  
1085 # Get the bit.
1086 40 return ( substr( $bin, $offset, 1 ) );
1087 }
1088  
1089  
1090 =head2 ping
1091  
1092 This would usually check if the Redis connection was alive, and the
1093 server was present, in this implementation we always return true.
1094  
1095 =cut
1096  
1097 sub ping
1098 0 ▶{
Subroutine ping: not called CC=1 SCAR=6.9
Pod documented
1099 0 return 1;
1100 }
1101  
1102  
1103 =head2 echo
1104  
1105 Return the parameters given.
1106  
1107 =cut
1108  
1109 sub echo
1110 0 ▶{
Subroutine echo: not called CC=1 SCAR=6.9
Pod documented
1111 0 my ( $self, $arg ) = (@_);
1112  
1113 0 return ($arg);
1114 }
1115  
1116 20 our $AUTOLOAD;
1117  
1118 sub AUTOLOAD
1119 19 ▶{
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
1120 19 my $command = $AUTOLOAD;
1121 19 $command =~ s/.*://;
1122 19 warn "NOT IMPLEMENTED:$command";
1123  
1124 19 return 1;
1125 }
1126  
1127  
1128 =head2 mget
1129  
1130 Return the values of multiple-keys. If a given key doesn't exist
1131 then C<undef> will be returned for that entry.
1132  
1133 =cut
1134  
1135 sub mget
1136 2 ▶{
Subroutine mget: called CC=3 SCAR=11.0
Pod documented
1137 2 my ( $self, @keys ) = (@_);
1138  
1139 2 my @ret;
1140  
1141 2 foreach my $key (@keys)
1142 {
1143 5 ▶ if ( $self->exists($key) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists($key)) { } 4 1 5
1144 {
1145 4 push( @ret, $self->get($key) );
1146 }
1147 else
1148 {
1149 1 push( @ret, undef );
1150 }
1151 }
1152  
1153 2 return (@ret);
1154 }
1155  
1156  
1157 =head2 mget
1158  
1159 Update the values of multiple-keys.
1160  
1161 =cut
1162  
1163 sub mset
1164 2 ▶{
Subroutine mset: called CC=2 SCAR=6.9
Pod undocumented
1165 2 my ( $self, @keys ) = (@_);
1166  
1167 2 ▶ while ( scalar @keys )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys) 5 2 7
1168 {
1169 5 my ( $key, $val ) = splice( @keys, 0, 2 );
1170  
1171 5 $self->set( $key, $val );
1172 }
1173 }
1174  
1175  
1176  
1177 =head2 mgetnx
1178  
1179 Update the values of multiple-keys, only if all the keys don't already exist.
1180  
1181 =cut
1182  
1183 sub msetnx
1184 2 ▶{
Subroutine msetnx: called CC=5 SCAR=16.1
Pod undocumented
1185 2 my ( $self, @keys ) = (@_);
1186  
1187 2 my %hash;
1188  
1189 # Update so we can test the keys.
1190 2 ▶ while ( scalar @keys )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys) 3 2 5
1191 {
1192 3 my ( $key, $val ) = splice( @keys, 0, 2 );
1193 3 $hash{ $key } = $val;
1194 }
1195  
1196 # Does any key already exist? If so we should do nothing.
1197 2 foreach my $key ( CORE::keys %hash )
1198 {
1199 3 ▶ return 0 if ( $self->exists($key) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists($key) 1 2 3
1200 }
1201  
1202 1 foreach my $key ( CORE::keys %hash )
1203 {
1204 2 $self->set( $key, $hash{ $key } );
1205 }
1206 1 return 1;
1207  
1208 }
1209  
1210 20 1;
1211  
1212  
1213  
1214 =head1 AUTHOR
1215  
1216 Steve Kemp
1217  
1218 http://www.steve.org.uk/
1219  
1220 =cut
1221  
1222  
1223  
1224 =head1 LICENSE
1225  
1226 Copyright (c) 2016 by Steve Kemp. All rights reserved.
1227  
1228 This module is free software;
1229 you can redistribute it and/or modify it under
1230 the same terms as Perl itself.
1231 The LICENSE file contains the full text of the license.
1232  
1233 =cut
↑ Summary