Redis/Protocol.pm

filter: Statementstmt 60.0% 72 / 120 Branchbran 46.2% 49 / 106 Conditioncond 55.1% 16 / 29 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 71.4% 10 / 14 Podpod 37.5% 3 / 8 totaltotal 52.7% 155 / 294 CC 65 SCAR 52.3
CC
65
Cov
54%
decode
351.7
encode
20.9
item
12.5
CRAP 186.4
↑ Summary Redis.pm »
1 package Net::Async::Redis::Protocol;
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 = '6.006'; # VERSION
7  
8 =head1 NAME
9  
10 Net::Async::Redis::Protocol - simple implementation of the Redis wire protocol
11  
12 =head1 DESCRIPTION
13  
14 Used internally by L<Net::Async::Redis> and L<Net::Async::Redis::Server>.
15  
16 =cut
17  
18 2 ▶use Scalar::Util qw(blessed reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Normal string interpolation
23 2 ▶use constant CRLF => "\x0D\x0A";
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # Regex usage
26 2 my $CRLF = CRLF;
27  
28 4 ▶sub new { bless { protocol => 'resp3', hashrefs => 0, @_[1..$#_] }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
29  
30 =head2 encode
31  
32 Given a Perl data structure, will return data suitable for sending
33 back as a response as from a Redis server.
34  
35 Note that this is not the correct format for client requests,
36 see L</encode_from_client> instead.
37  
38 =cut
39  
40 9049 ▶sub encode {
Subroutine encode: called CC=15 SCAR=30.4
Pod documented
41 9049 my ($self, $data) = @_;
42 9049 ▶ die 'blessed data is not ok' if blessed $data;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) 0 9049 9049
43 9049 ▶ if(my $type = reftype $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = builtin::reftype($data)) 2014 7035 9049
44 2014 ▶ if($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 2014 0 2014
elsif ($type eq 'HASH') { } 0 0 0
45 2014 return '*' . (0 + @$data) . CRLF . join '', map $self->encode($_), @$data
46 } elsif($type eq 'HASH') {
47 0 return '%' . (0 + keys %$data) . CRLF . join '', map $self->encode($_), map { $_ => $data->{$_} } sort keys %$data
48 }
49 0 die 'no support for ' . $type
50 }
51 7035 ▶ if(!defined($data)) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $data) { } 5 7030 7035
elsif (not length $data) { } 4 7026 7030
elsif (($data ^ $data) eq '0' and int 0 + $data eq $data and not $data =~ /inf/i) { } 1014 6012 7026
elsif (($data ^ $data) eq '0' and 0 + $data eq $data) { } 0 6012 6012
elsif (length $data < 100 and not $data =~ /[$CRLF]/) { } 6010 4 6014
52 5 ▶ return $self->{protocol} eq 'resp3' ? '_' . CRLF : '$-1' . CRLF;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'protocol'} eq 'resp3' ? : 1 4 5
53 } elsif(!length($data)) {
54 4 return '$0' . CRLF . CRLF;
55 ▶ } elsif(($data ^ $data) eq "0" and int(0+$data) eq $data and $data !~ /inf/i) {
Condition66%4/6
($data ^ $data) eq 0 and int 0 + $data eq $data and not $data =~ /inf/i
!ll&&!rl&&r
601201014
($data ^ $data) eq 0 and int 0 + $data eq $data
!ll&&!rl&&r
601201014
MC/DC33%1/3
($data ^ $data) eq 0 and int 0 + $data eq $data and not $data =~ /inf/i
($data ^ $data) eq 0 int 0 + $data eq $data not $data =~ /inf/i
Truth table50%2/4
A and B and not(C)
A= ($data ^ $data) eq 0B= int 0 + $data eq $dataC= not $data =~ /inf/i
ABCresult
0XX0
10X0
1100
1111
56 1014 return ':' . (0 + $data) . CRLF;
57 ▶ } elsif(($data ^ $data) eq "0" and 0+$data eq $data) {
Condition33%1/3
($data ^ $data) eq 0 and 0 + $data eq $data
!ll&&!rl&&r
601200
MC/DC0%0/2
($data ^ $data) eq 0 and 0 + $data eq $data
($data ^ $data) eq 0 0 + $data eq $data
Truth table33%1/3
A and B
A= ($data ^ $data) eq 0B= 0 + $data eq $data
ABresult
0X0
100
111
58 0 return ',' . lc(0 + $data) . CRLF;
59 ▶ } elsif(length($data) < 100 and $data !~ /[$CRLF]/) {
Condition66%2/3
length $data < 100 and not $data =~ /[$CRLF]/
!ll&&!rl&&r
206010
MC/DC50%1/2
length $data < 100 and not $data =~ /[$CRLF]/
length $data < 100 not $data =~ /[$CRLF]/
Truth table66%2/3
A and not(B)
A= length $data < 100B= not $data =~ /[$CRLF]/
ABresult
0X0
100
111
60 6010 return '+' . $data . CRLF;
61 }
62 2 return '$' . length($data) . CRLF . $data . CRLF;
63 }
64  
65 =head2 encode_from_client
66  
67 Handles client format encoding. Expects a list of data items, and will
68 convert them into length-prefixed bulk strings as a single response item.
69  
70 =cut
71  
72 0 ▶sub encode_from_client {
Subroutine encode_from_client: not called CC=1 SCAR=6.9
Pod documented
73 0 my ($self, @data) = @_;
74 return '*' . (0 + @data) . CRLF . join '', map {
75 0 '$' . length($_) . CRLF . $_ . CRLF
76 } @data;
77 }
78  
79 =head2 decode
80  
81 Decodes wire protocol data into Perl data structures.
82  
83 Expects to be called with a reference to a byte string, and will
84 extract as much as it can from that string (destructively).
85  
86 Likely to call L</item> or L</item_error> zero or more times.
87  
88 =cut
89  
90 32 ▶sub decode {
Subroutine decode: called CC=40 SCAR=58.6
Pod documented
91 32 my ($self, $bytes) = @_;
92  
93 32 my $len = $self->{parsing_bulk};
94 ITEM:
95 32 for ($$bytes) {
96 9055 ▶ if($log->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Async::Redis::Protocol::log->is_trace) 0 9055 9055
97 0 my $bytes = substr $_, 0, min(16, length($_));
98 0 $log->tracef('Next few bytes: %s (%v02x)', $bytes, $bytes);
99 }
100 9055 ▶ if(defined($len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 6 9049 9055
101 6 ▶ last ITEM unless length($_) >= $len + 2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ >= $len + 2 0 6 6
102 6 ▶ die 'invalid bulk data, did not end in CRLF' unless substr($_, $len, 2, '') eq CRLF;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($_, $len, 2, "") eq "\r\n" 0 6 6
103 6 $self->item(substr $_, 0, delete $self->{parsing_bulk}, '');
104 6 undef $len;
105 6 ▶ last ITEM unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 6 0 6
106 }
107 9049 ▶ if(s{^\+([^\x0D]*)$CRLF}{}) {
Branch: 14 branches
BranchTrueFalseTotal
if (s/^\+([^\x0D]*)$CRLF//) { } 6010 3039 9049
elsif (s/^:([^\x0D]*)$CRLF//) { } 1014 2025 3039
elsif (s/^,([^\x0D]*)$CRLF//) { } 0 2025 2025
elsif (s/^#([tf])$CRLF//) { } 0 2025 2025
elsif (s/^\$([0-9]+)$CRLF//) { } 6 2019 2025
elsif (s/^=([0-9]+)$CRLF//) { } 0 2019 2019
elsif (s/^\$-1$CRLF// or s/^\*-1$CRLF// or s/^_$CRLF//) { } 5 2014 2019
elsif (s/^>([0-9]+)$CRLF//) { } 0 2014 2014
elsif (s/^\*([0-9]+)$CRLF//) { } 2014 0 2014
elsif (s/^~([0-9]+)$CRLF//) { } 0 0 0
elsif (s/^%([0-9]+)$CRLF//) { } 0 0 0
elsif (s/^\|([0-9]+)$CRLF//) { } 0 0 0
elsif (s/^-([^\x0D]*)$CRLF//) { } 0 0 0
elsif (s/^!([^\x0D]*)$CRLF//) { } 0 0 0
108 6010 $self->item("$1");
109 } elsif(s{^:([^\x0D]*)$CRLF}{}) {
110 1014 my $int = $1;
111 1014 ▶ die 'invalid integer value ' . $int unless looks_like_number($int) && int($int) eq $int;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($int) and int $int eq $int 0 1014 1014
Condition33%1/3
&looks_like_number($int) and int $int eq $int
!ll&&!rl&&r
001014
MC/DC0%0/2
&looks_like_number($int) and int $int eq $int
&looks_like_number($int) int $int eq $int
Truth table33%1/3
A and B
A= &looks_like_number($int)B= int $int eq $int
ABresult
0X0
100
111
112 1014 $self->item(0 + $int);
113 } elsif(s{^,([^\x0D]*)$CRLF}{}) {
114 0 my $num = $1;
115 0 ▶ die 'invalid numeric value ' . $num unless looks_like_number($num) && lc(0 + $num) eq lc($num);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($num) and lc 0 + $num eq lc $num 0 0 0
Condition0%0/3
&looks_like_number($num) and lc 0 + $num eq lc $num
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($num) and lc 0 + $num eq lc $num
&looks_like_number($num) lc 0 + $num eq lc $num
Truth table0%0/3
A and B
A= &looks_like_number($num)B= lc 0 + $num eq lc $num
ABresult
0X0
100
111
116 0 $self->item(0 + $num);
117 } elsif(s{^#([tf])$CRLF}{}) {
118 0 $self->item($1 eq 't');
119 } elsif(s{^\$([0-9]+)$CRLF}{}) {
120 6 $len = $1;
121 6 ▶ die 'invalid numeric value for length ' . $len unless 0+$len eq $len;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $len eq $len 0 6 6
122 6 $self->{parsing_bulk} = $len;
123 } elsif(s{^=([0-9]+)$CRLF}{}) {
124 0 $len = $1;
125 0 ▶ die 'invalid numeric value for length ' . $len unless 0+$len eq $len;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $len eq $len 0 0 0
126 0 $self->{parsing_bulk} = $len;
127 ▶ } elsif(s{^\$-1$CRLF}{} or s{^\*-1$CRLF}{} or s{^_$CRLF}{}) {
Condition83%5/6
s/^\$-1$CRLF// or s/^\*-1$CRLF// or s/^_$CRLF//
l!l&&r!l&&!r
412014
s/^\$-1$CRLF// or s/^\*-1$CRLF//
l!l&&r!l&&!r
402015
MC/DC66%2/3
s/^\$-1$CRLF// or s/^\*-1$CRLF// or s/^_$CRLF//
s/^\$-1$CRLF// s/^\*-1$CRLF// s/^_$CRLF//
Truth table75%3/4
A or B or C
A= s/^\$-1$CRLF//B= s/^\*-1$CRLF//C= s/^_$CRLF//
ABCresult
1XX1
01X1
0011
0000
128 5 $self->item(undef);
129 } elsif(s{^>([0-9]+)$CRLF}{}) {
130 0 my $pending = $1;
131 0 ▶ die 'invalid numeric value for push ' . $pending unless 0+$pending eq $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $pending eq $pending 0 0 0
132 0 ▶ push @{$self->{active}}, {
Branch: 1 branch
BranchTrueFalseTotal
if $pending 0 0 0
133 type => 'push',
134 pending => $pending
135 } if $pending;
136 } elsif(s{^\*([0-9]+)$CRLF}{}) {
137 2014 my $pending = $1;
138 2014 ▶ die 'invalid numeric value for array ' . $pending unless 0+$pending eq $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $pending eq $pending 0 2014 2014
139 2014 ▶ if($pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) { } 2012 2 2014
140 2012 push @{$self->{active}}, { type => 'array', pending => $pending };
141 } else {
142 2 $self->item([ ]);
143 }
144 } elsif(s{^~([0-9]+)$CRLF}{}) {
145 0 my $pending = $1;
146 0 ▶ die 'invalid numeric value for set ' . $pending unless 0+$pending eq $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $pending eq $pending 0 0 0
147 0 ▶ if($pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) { } 0 0 0
148 0 push @{$self->{active}}, { type => 'set', pending => $pending };
149 } else {
150 0 $self->item([ ]);
151 }
152 } elsif(s{^%([0-9]+)$CRLF}{}) {
153 0 my $pending = $1;
154 0 ▶ die 'invalid numeric value for map ' . $pending unless 0+$pending eq $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $pending eq $pending 0 0 0
155 0 ▶ if($pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) { } 0 0 0
156 # We provide 2x the count here, for key/value pairs, and expect
157 # the handler to convert to a hash once it's received sufficient
158 # items to emit the full element
159 0 push @{$self->{active}}, { type => 'map', pending => 2 * $pending };
160 } else {
161 0 $self->item({ });
162 }
163 } elsif(s{^\|([0-9]+)$CRLF}{}) {
164 0 my $pending = $1;
165 0 ▶ die 'invalid numeric value for attribute ' . $pending unless 0+$pending eq $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $pending eq $pending 0 0 0
166 0 ▶ push @{$self->{active}}, {
Branch: 1 branch
BranchTrueFalseTotal
if $pending 0 0 0
167 type => 'attribute',
168 pending => 2 * $pending
169 } if $pending;
170 } elsif(s{^-([^\x0D]*)$CRLF}{}) {
171 0 $self->item_error($1);
172 } elsif(s{^!([^\x0D]*)$CRLF}{}) {
173 0 die 'cannot handle blob error yet';
174 } else {
175 0 last ITEM;
176 }
177 9049 ▶ redo ITEM if length;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ 9023 26 9049
178 }
179 }
180  
181 0 ▶sub parse { $_[0]->decode($_[1]) }
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
182  
183 7037 ▶sub item {
Subroutine item: called CC=10 SCAR=25.3
Pod undocumented
184 7037 my ($self, $data) = @_;
185 7037 ▶ $data = [ %$data ] if ref $data eq 'HASH' and not $self->{hashrefs};
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "HASH" and not $self->{"hashrefs"} 0 7037 7037
Condition33%1/3
ref $data eq "HASH" and not $self->{"hashrefs"}
!ll&&!rl&&r
703700
MC/DC0%0/2
ref $data eq "HASH" and not $self->{"hashrefs"}
ref $data eq "HASH" not $self->{"hashrefs"}
Truth table33%1/3
A and not(B)
A= ref $data eq "HASH"B= not $self->{"hashrefs"}
ABresult
0X0
100
111
186 7037 while(1) {
187 9049 ▶ return $self->{handler}->($data) unless @{$self->{active} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"active"} or [];} 32 9017 9049
Condition100%2/2
$self->{'active'} || []
l!l
902722
MC/DC100%1/1
$self->{'active'} || []
$self->{'active'}
Truth table100%2/2
A
A= $self->{'active'}
Aresult
11
00
188  
189 9017 push @{$self->{active}[-1]{items}}, $data;
190 9017 ▶ return if --$self->{active}[-1]{pending};
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"active"}[-1]{"pending"} 7005 2012 9017
191 2012 my $active = pop @{$self->{active}};
192 $data = $active->{type} eq 'map'
193 ? ($self->{hashrefs}
194 0 ? { @{$active->{items}} }
195 0 : [ @{$active->{items}} ]
196 )
197 2012 ▶ : $active->{items};
Branch: 2 branches
BranchTrueFalseTotal
$active->{'type'} eq 'map' ? : 0 2012 2012
$self->{'hashrefs'} ? : 0 0 0
198  
199 # Skip attributes entirely for now
200 2012 ▶ return if $active->{type} eq 'attribute';
Branch: 1 branch
BranchTrueFalseTotal
if $active->{"type"} eq "attribute" 0 2012 2012
201 2012 ▶ return $self->item_pubsub($data) if $active->{type} eq 'push';
Branch: 1 branch
BranchTrueFalseTotal
if $active->{"type"} eq "push" 0 2012 2012
202 }
203 }
204  
205 0 ▶sub item_error {
Subroutine item_error: not called CC=2 SCAR=17.9
Pod undocumented
206 0 my ($self, $err) = @_;
207 0 ▶ $self->{error}->($err) if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
208 0 $self
209 }
210  
211 0 ▶sub item_pubsub {
Subroutine item_pubsub: not called CC=2 SCAR=17.9
Pod undocumented
212 0 my ($self, $item) = @_;
213 0 ▶ $self->{pubsub}->(@$item) if $self->{pubsub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pubsub"} 0 0 0
214 0 $self
215 }
216  
217 2 1;
218  
219 __END__
↑ Summary Redis.pm »