Redis/Server/Database.pm

filter: Statementstmt 13.1% 11 / 84 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 14.2% 3 / 21 Podpod 0.0% 0 / 18 totaltotal 8.4% 14 / 166 CC 18 SCAR 41.7
CC
18
Cov
9%
set
182.0
keys
12.0
exists
6.0
CRAP 64.9
↑ Summary Redis.pm »
1 package Net::Async::Redis::Server::Database;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '2.001'; # VERSION
7  
8 =head1 NAME
9  
10 Net::Async::Redis::Server::Database - implementation for database-related Redis commands
11  
12 =head1 DESCRIPTION
13  
14 See L<Net::Async::Redis::Commands> for the full list of commands.
15 Not all of them will be implemented yet.
16  
17 =cut
18  
19 1 ▶use Math::Random::Secure ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub set : method {
22 0 ▶ my ($self, $k, $v, @args) = @_;
Subroutine set: not called CC=13 SCAR=52.0
Pod undocumented
23 0 my %opt;
24 0 ▶ while(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
25 0 my $cmd = shift @args;
26 0 ▶ if($cmd eq 'EX') {
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd eq 'EX') { } 0 0 0
elsif ($cmd eq 'PX') { } 0 0 0
elsif ($cmd eq 'NX') { } 0 0 0
elsif ($cmd eq 'XX') { } 0 0 0
27 0 $opt{ttl} = 1000 * shift @args;
28 } elsif($cmd eq 'PX') {
29 0 $opt{ttl} = shift @args;
30 } elsif($cmd eq 'NX') {
31 0 ▶ die 'Cannot set NX and XX' if exists $opt{xx};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"xx"} 0 0 0
32 0 $opt{nx} = 1;
33 } elsif($cmd eq 'XX') {
34 0 ▶ die 'Cannot set NX and XX' if exists $opt{nx};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"nx"} 0 0 0
35 0 $opt{xx} = 1;
36 } else {
37 0 die 'Invalid input: ' . $cmd
38 }
39 }
40 0 ▶ return Future->done(undef) if not exists $self->{keys}{$k} and $opt{xx};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"keys"}{$k} and $opt{"xx"} 0 0 0
Condition0%0/3
not exists $self->{"keys"}{$k} and $opt{"xx"}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $self->{"keys"}{$k} and $opt{"xx"}
not exists $self->{"keys"}{$k} $opt{"xx"}
Truth table0%0/3
not(A) and B
A= not exists $self->{"keys"}{$k}B= $opt{"xx"}
ABresult
0X0
100
111
41 0 ▶ return Future->done(undef) if exists $self->{keys}{$k} and $opt{nx};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"keys"}{$k} and $opt{"nx"} 0 0 0
Condition0%0/3
exists $self->{"keys"}{$k} and $opt{"nx"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"keys"}{$k} and $opt{"nx"}
exists $self->{"keys"}{$k} $opt{"nx"}
Truth table0%0/3
A and B
A= exists $self->{"keys"}{$k}B= $opt{"nx"}
ABresult
0X0
100
111
42 0 $self->{keys}{$k} = $v;
43 0 ▶ if(exists $opt{ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"ttl"}) 0 0 0
44 0 $self->{expiry}{$k} = $opt{ttl} + $self->time;
45 }
46 0 return Future->done('OK');
47 }
48  
49 sub get : method {
50 0 ▶ my ($self, $k) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
51 0 return Future->done($self->{keys}{$k});
52 }
53  
54 sub auth : method {
55 0 ▶ my ($self, $auth) = @_;
Subroutine auth: not called CC=1 SCAR=6.9
Pod undocumented
56 0 return Future->done('OK');
57 }
58  
59 sub echo : method {
60 0 ▶ my ($self, $string) = @_;
Subroutine echo: not called CC=1 SCAR=6.9
Pod undocumented
61 0 return Future->done($string);
62 }
63  
64 sub ping : method {
65 0 ▶ my ($self, $string) = @_;
Subroutine ping: not called CC=1 SCAR=6.9
Pod undocumented
66 0 return Future->done(PONG => $string);
67 }
68  
69 sub quit : method {
70 0 ▶ my ($self, $string) = @_;
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
71 0 $self->connection->close;
72 0 return Future->done('OK');
73 }
74  
75 sub del : method {
76 0 ▶ my ($self, @keys) = @_;
Subroutine del: not called CC=1 SCAR=6.9
Pod undocumented
77 0 return Future->done(delete @{$self->{keys}}{@keys});
78 }
79  
80 sub exists : method {
81 0 ▶ my ($self, @keys) = @_;
Subroutine exists: not called CC=2 SCAR=17.9
Pod undocumented
82 0 ▶ return Future->done(map { exists $self->{keys}{$_} ? 1 : 0 } @keys);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'keys'}{$_} ? : 0 0 0
83 }
84  
85 sub expire : method {
86 0 ▶ my ($self, $k, $ttl) = @_;
Subroutine expire: not called CC=1 SCAR=6.9
Pod undocumented
87 0 return $self->pexpire($k => 1000 * $ttl);
88 }
89  
90 sub expireat : method {
91 0 ▶ my ($self, $k, $time) = @_;
Subroutine expireat: not called CC=1 SCAR=6.9
Pod undocumented
92 0 return $self->pexpireat($k => 1000 * $time);
93 }
94  
95 sub pexpire : method {
96 0 ▶ my ($self, $k, $ttl) = @_;
Subroutine pexpire: not called CC=2 SCAR=17.9
Pod undocumented
97 0 ▶ $self->{expiry}{$k} = $ttl + $self->time if exists $self->{keys}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"keys"}{$k} 0 0 0
98 0 return Future->done('OK');
99 }
100  
101 sub pexpireat : method {
102 0 ▶ my ($self, $k, $time) = @_;
Subroutine pexpireat: not called CC=2 SCAR=17.9
Pod undocumented
103 0 ▶ $self->{expiry}{$k} = $time if exists $self->{keys}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"keys"}{$k} 0 0 0
104 0 return Future->done('OK');
105 }
106  
107 sub keys : method {
108 0 ▶ my ($self, $pattern) = @_;
Subroutine keys: not called CC=3 SCAR=24.8
Pod undocumented
109 0 ▶ $pattern = '*' unless defined($pattern) and length($pattern);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern and length $pattern 0 0 0
Condition0%0/3
defined $pattern and length $pattern
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pattern and length $pattern
defined $pattern length $pattern
Truth table0%0/3
A and B
A= defined $patternB= length $pattern
ABresult
0X0
100
111
110 0 $pattern = qr/^\Q$pattern\E$/;
111 0 $pattern =~ s{\\\*}{.*}g;
112 0 return Future->done(grep { $_ =~ $pattern } sort keys %{$self->{keys}});
113 }
114  
115 sub persist : method {
116 0 ▶ my ($self, $k) = @_;
Subroutine persist: not called CC=1 SCAR=6.9
Pod undocumented
117 0 delete $self->{expiry}{$k};
118 0 return Future->done('OK');
119 }
120  
121 sub randomkey : method {
122 0 ▶ my ($self) = @_;
Subroutine randomkey: not called CC=1 SCAR=6.9
Pod undocumented
123 0 return Future->done((keys %{$self->{keys}})[Math::Random::Secure::irand keys %{$self->{keys}}]);
124 }
125  
126 sub lpush : method {
127 0 ▶ my ($self, $k, @values) = @_;
Subroutine lpush: not called CC=1 SCAR=6.9
Pod undocumented
128 0 unshift @{$self->{keys}{$k}}, @values;
129 0 return Future->done('OK');
130 }
131  
132 sub rpush : method {
133 0 ▶ my ($self, $k, @values) = @_;
Subroutine rpush: not called CC=1 SCAR=6.9
Pod undocumented
134 0 push @{$self->{keys}{$k}}, @values;
135 0 return Future->done('OK');
136 }
137  
138 sub brpoplpush : method {
139 0 ▶ my ($self, $src, $dst, $timeout) = @_;
Subroutine brpoplpush: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $v = pop @{$self->{keys}{$src}};
141 0 unshift @{$self->{keys}{$dst}}, $v;
142 0 return Future->done($v);
143 }
144  
145 1 1;
146  
↑ Summary Redis.pm »