| 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
| |||||||||||||||||||||||||||||
| 25 | 0 | my $cmd = shift @args; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | if($cmd eq 'EX') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not exists $self->{"keys"}{$k} and $opt{"xx"}
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"}
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return Future->done(undef) if exists $self->{keys}{$k} and $opt{nx}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->{"keys"}{$k} and $opt{"nx"}
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"}
| |||||||||||||||||||||||||||||
| 42 | 0 | $self->{keys}{$k} = $v; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | if(exists $opt{ttl}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $pattern and length $pattern
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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||