blib/lib/MojoX/Redis.pm

filter: Statementstmt 80.2% 122 / 152 Branchbran 55.5% 20 / 36 Conditioncond 55.0% 11 / 20 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 82.7% 24 / 29 Podpod 60.0% 3 / 5 totaltotal 72.3% 183 / 253 CC 30 SCAR 16.4
CC
30
Cov
74%
_reencode_message
15.4
DESTROY
12.0
execute
7.3
CRAP 5.1
↑ Summary
1 package MojoX::Redis;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = 0.88;
7 2 ▶use base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 require Carp;
14  
15 2 __PACKAGE__->attr(server => '127.0.0.1:6379');
16 1 ▶__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->singleton });
Subroutine __ANON__: called CC=1 SCAR=0.0
17 2 __PACKAGE__->attr(error => undef);
18 2 __PACKAGE__->attr(timeout => 300);
19 2 __PACKAGE__->attr(encoding => 'UTF-8');
20 __PACKAGE__->attr(
21 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 my $redis = shift;
24 0 warn "Redis error: ", $redis->error, "\n";
25 }
26 0 }
27 2 );
28  
29 __PACKAGE__->attr(
30 1 ▶ protocol_redis => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 require Protocol::Redis;
32 1 "Protocol::Redis";
33 }
34 2 );
35  
36 2 our @COMMANDS = qw/
37 append auth bgrewriteaof bgsave blpop brpop brpoplpush config_get config_set
38 config_resetstat dbsize debug_object debug_segfault decr decrby del discard
39 echo exec exists expire expireat flushall flushdb get getbit getrange getset
40 hdel hexists hget hgetall hincrby hkeys hlen hmget hmset hset hsetnx hvals
41 incr incrby info keys lastsave lindex linsert llen lpop lpush lpushx lrange
42 lrem lset ltrim mget monitor move mset msetnx multi persist ping psubscribe
43 publish punsubscribe quit randomkey rename renamenx rpop rpoplpush rpush
44 rpushx sadd save scard sdiff sdiffstore select set setbit setex setnx
45 setrange shutdown sinter sinterstore sismember slaveof smembers smove sort
46 spop srandmember srem strlen subscribe sunion sunionstore sync ttl type
47 unsubscribe unwatch watch zadd zcard zcount zincrby zinterstore zrange
48 zrangebyscore zrank zrem zremrangebyrank zremrangebyscore zrevrange
49 zrevrangebyscore zrevrank zscore zunionstore
50 /;
51  
52 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.3
53 1 my ($package, $cmd) = our $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
54  
55 Carp::croak(qq|Can't locate object method "$cmd" via "$package"|)
56 1 ▶ unless List::Util::first { $_ eq $cmd } @COMMANDS;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &List::Util::first(sub { $_ eq $cmd; } , @MojoX::Redis::COMMANDS) 0 1 1
57  
58 1 my $self = shift;
59  
60 1 my $args = [@_];
61 1 my $cb = $args->[-1];
62 1 ▶ if (ref $cb ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb ne 'CODE') { } 0 1 1
63 0 $cb = undef;
64 }
65 else {
66 1 pop @$args;
67 }
68  
69 1 $self->execute($cmd, $args, $cb);
70 }
71  
72 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
73 0 my $self = shift;
74  
75 # Loop
76 0 ▶ return unless my $loop = $self->ioloop;
Branch: 1 branch
BranchTrueFalseTotal
unless my $loop = $self->ioloop 0 0 0
77  
78 # Cleanup connection
79 $loop->remove($self->{_connection})
80 0 ▶ if $self->{_connection};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_connection"} 0 0 0
81 }
82  
83 1 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.9
Pod documented
84 1 my $self = shift;
85  
86 # drop old connection
87 1 ▶ if ($self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connected) 0 1 1
88 0 $self->ioloop->remove($self->{_connection});
89 }
90  
91 1 $self->server =~ m{^([^:]+)(:(\d+))?};
92 1 my $address = $1;
93 1 ▶ my $port = $3 || 6379;
Condition50%1/2
$3 || 6379
l!l
10
MC/DC0%0/1
$3 || 6379
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
94  
95 1 Scalar::Util::weaken $self;
96  
97 1 $self->{_protocol} = $self->_create_protocol;
98  
99 # connect
100 1 $self->{_connecting} = 1;
101 $self->{_connection} = $self->ioloop->client(
102 { address => $address,
103 port => $port
104 },
105 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=8.0
106 1 my ($loop, $err, $stream) = @_;
107  
108 1 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 1 1
109 0 $self->error($err);
110 0 $self->_inform_queue;
111  
112 0 $self->on_error->($self);
113 0 return;
114 }
115  
116 1 delete $self->{_connecting};
117 1 $stream->timeout($self->timeout);
118 1 $self->_send_next_message;
119  
120 $stream->on(
121 4 ▶ read => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 4 my ($stream, $chunk) = @_;
123 4 $self->{_protocol}->parse($chunk);
124 }
125 1 );
126 $stream->on(
127 1 ▶ close => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
128 1 my $str = shift;
129 1 ▶ $self->{error} ||= 'disconnected';
Condition50%1/2
$self->{"error"} ||= "disconnected"
l!l
01
MC/DC0%0/1
$self->{"error"} ||= "disconnected"
$self->{"error"}
Truth table50%1/2
A
A= $self->{"error"}
Aresult
11
00
130 1 $self->_inform_queue;
131  
132 1 delete $self->{_message_queue};
133  
134 1 delete $self->{_connecting};
135 1 delete $self->{_connection};
136 }
137 1 );
138 $stream->on(
139 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 my ($str, $error) = @_;
141 0 $self->error($error);
142 0 $self->_inform_queue;
143  
144 0 $self->on_error->($self);
145 0 $self->ioloop->remove($self->{_connection});
146 }
147 1 );
148  
149 }
150 1 );
151  
152 1 return $self;
153 }
154  
155 1 ▶sub connected {
Subroutine connected: called CC=1 SCAR=0.0
Pod undocumented
156 1 my $self = shift;
157  
158 1 return $self->{_connection};
159 }
160  
161 5 ▶sub execute {
Subroutine execute: called CC=7 SCAR=19.9
Pod documented
162 5 my ($self, $command, $args, $cb) = @_;
163  
164 5 ▶ if (!$cb && ref $args eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $cb and ref $args eq 'CODE') { } 0 5 5
elsif (not ref $args) { } 4 1 5
Condition33%1/3
not $cb and ref $args eq "CODE"
!ll&&!rl&&r
500
MC/DC0%0/2
not $cb and ref $args eq "CODE"
not $cb ref $args eq "CODE"
Truth table33%1/3
not(A) and B
A= not $cbB= ref $args eq "CODE"
ABresult
0X0
100
111
165 0 $cb = $args;
166 0 $args = [];
167 }
168 elsif (!ref $args) {
169 4 $args = [$args];
170 }
171  
172 5 unshift @$args, uc $command;
173  
174 5 ▶ my $mqueue = $self->{_message_queue} ||= [];
Condition100%2/2
$self->{"_message_queue"} ||= []
l!l
41
MC/DC100%1/1
$self->{"_message_queue"} ||= []
$self->{"_message_queue"}
Truth table100%2/2
A
A= $self->{"_message_queue"}
Aresult
11
00
175 5 ▶ my $cqueue = $self->{_cb_queue} ||= [];
Condition100%2/2
$self->{"_cb_queue"} ||= []
l!l
41
MC/DC100%1/1
$self->{"_cb_queue"} ||= []
$self->{"_cb_queue"}
Truth table100%2/2
A
A= $self->{"_cb_queue"}
Aresult
11
00
176  
177  
178 5 push @$mqueue, $args;
179 5 push @$cqueue, $cb;
180  
181 5 ▶ $self->connect unless $self->{_connection};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connection"} 1 4 5
182 5 $self->_send_next_message;
183  
184 5 return $self;
185 }
186  
187 1 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
188 1 my ($self) = @_;
189  
190 1 $self->ioloop->start;
191 1 return $self;
192 }
193  
194 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod undocumented
195 0 my ($self) = @_;
196  
197 0 $self->ioloop->stop;
198 0 return $self;
199 }
200  
201 1 ▶sub _create_protocol {
Subroutine _create_protocol: called CC=2 SCAR=7.0
202 1 my $self = shift;
203  
204 1 my $protocol = $self->protocol_redis->new(api => 1);
205 $protocol->on_message(
206 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
207 3 my ($parser, $command) = @_;
208 3 $self->_return_command_data($command);
209 }
210 1 );
211  
212 1 ▶ Carp::croak(q/Protocol::Redis implementation doesn't support APIv1/)
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 0 1 1
213 unless $protocol;
214  
215 1 $protocol;
216 }
217  
218 6 ▶sub _send_next_message {
Subroutine _send_next_message: called CC=6 SCAR=18.0
219 6 my ($self) = @_;
220  
221 6 ▶ if ((my $id = $self->{_connection}) && !$self->{_connecting}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->{"_connection"} and not $self->{"_connecting"}) 5 1 6
Condition66%2/3
my $id = $self->{"_connection"} and not $self->{"_connecting"}
!ll&&!rl&&r
015
MC/DC50%1/2
my $id = $self->{"_connection"} and not $self->{"_connecting"}
my $id = $self->{"_connection"} not $self->{"_connecting"}
Truth table66%2/3
A and not(B)
A= my $id = $self->{"_connection"}B= not $self->{"_connecting"}
ABresult
0X0
100
111
222 5 ▶ while (my $args = shift @{$self->{_message_queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $args = shift @{$$self{"_message_queue"};}) 5 5 10
223 5 my $cmd_arg = [];
224 5 my $cmd = {type => '*', data => $cmd_arg};
225 5 foreach my $token (@$args) {
226 11 ▶ $token = Encode::encode($self->encoding, $token)
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 11 0 11
227 if $self->encoding;
228 11 push @$cmd_arg, {type => '$', data => $token};
229 }
230 5 my $message = $self->{_protocol}->encode($cmd);
231  
232 5 $self->ioloop->stream($id)->write($message);
233 }
234 }
235 }
236  
237  
238 3 ▶sub _reencode_message {
Subroutine _reencode_message: called CC=7 SCAR=27.3
239 3 my ($self, $message) = @_;
240  
241 3 my ($type, $data) = @{$message}{'type', 'data'};
242  
243 # Decode data
244 3 ▶ if ($type ne '*' && $self->encoding && $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "*" and $self->encoding and $data) 3 0 3
Condition33%2/6
$type ne "*" and $self->encoding and $data
!ll&&!rl&&r
003
$type ne "*" and $self->encoding
!ll&&!rl&&r
003
MC/DC0%0/3
$type ne "*" and $self->encoding and $data
$type ne "*" $self->encoding $data
Truth table25%1/4
A and B and C
A= $type ne "*"B= $self->encodingC= $data
ABCresult
0XX0
10X0
1100
1111
245 3 $data = Encode::decode($self->encoding, $data);
246 }
247  
248 3 ▶ if ($type eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '-') { } 0 3 3
elsif ($type ne '*') { } 3 0 3
249 0 $self->error($data);
250 0 $self->on_error->($self);
251 0 return;
252 }
253 elsif ($type ne '*') {
254 3 return [$data];
255 }
256 else {
257 0 my $reencoded_data = [];
258 0 foreach my $item (@$data) {
259 0 my $message = $self->_reencode_message($item);
260 0 push @$reencoded_data, $message;
261 }
262 0 return $reencoded_data;
263 }
264 }
265  
266 3 ▶sub _return_command_data {
Subroutine _return_command_data: called CC=2 SCAR=7.0
267 3 my ($self, $message) = @_;
268  
269 3 my $data = $self->_reencode_message($message);
270  
271 3 my $cb = shift @{$self->{_cb_queue}};
272 3 ▶ $cb->($self, $data) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 0 3
273  
274 # Reset error after callback dispatching
275 3 $self->error(undef);
276 }
277  
278  
279 1 ▶sub _inform_queue {
Subroutine _inform_queue: called CC=3 SCAR=11.1
280 1 my ($self) = @_;
281  
282 1 for my $cb (@{$self->{_cb_queue}}) {
283 2 ▶ $cb->($self) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 2 0 2
284 }
285 1 $self->{_queue} = [];
286 }
287  
288 2 1;
289 __END__
↑ Summary