Redis/Protocol.pm

filter: Statementstmt 83.5% 61 / 73 Branchbran 71.4% 40 / 56 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 72.7% 8 / 11 Podpod 42.8% 3 / 7 totaltotal 73.4% 119 / 162 CC 33 SCAR 26.8
CC
33
Cov
77%
decode
23.9
encode
12.4
item_error
6.0
CRAP 14.6
« Redis/Multi.pm ↑ Summary Redis/Cluster/Node.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 = '2.004_001'; # TRIAL 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  
21 2 my $CRLF = "\x0D\x0A";
22  
23 3 ▶sub new { bless { @_[1..$#_] }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
24  
25 =head2 encode
26  
27 Given a Perl data structure, will return data suitable for sending
28 back as a response as from a Redis server.
29  
30 Note that this is not the correct format for client requests,
31 see L</encode_from_client> instead.
32  
33 =cut
34  
35 sub encode {
36 44 ▶ my ($self, $data) = @_;
Subroutine encode: called CC=11 SCAR=25.2
Pod documented
37 44 ▶ die 'blessed data is not ok' if blessed $data;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) 0 44 44
38 44 ▶ if(my $type = reftype $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = builtin::reftype($data)) 12 32 44
39 12 ▶ if($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 12 0 12
elsif ($type eq 'HASH') { } 0 0 0
40 12 return '*' . (0 + @$data) . $CRLF . join '', map $self->encode($_), @$data
41 } elsif($type eq 'HASH') {
42 0 die 'no hash support'
43 }
44 0 die 'no support for ' . $type
45 }
46 32 ▶ if(!defined($data)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $data) { } 4 28 32
elsif (not length $data) { } 4 24 28
elsif (($data ^ $data) eq '0' and int 0 + $data eq $data) { } 12 12 24
elsif (length $data < 100 and not $data =~ /[$CRLF]/) { } 10 4 14
47 4 return '$-1' . $CRLF;
48 } elsif(!length($data)) {
49 4 return '$0' . $CRLF . $CRLF;
50 ▶ } elsif(($data ^ $data) eq "0" and int(0+$data) eq $data) {
Condition66%2/3
($data ^ $data) eq 0 and int 0 + $data eq $data
!ll&&!rl&&r
12012
MC/DC50%1/2
($data ^ $data) eq 0 and int 0 + $data eq $data
($data ^ $data) eq 0 int 0 + $data eq $data
Truth table66%2/3
A and B
A= ($data ^ $data) eq 0B= int 0 + $data eq $data
ABresult
0X0
100
111
51 12 return ':' . (0 + $data) . $CRLF;
52 ▶ } elsif(length($data) < 100 and $data !~ /[$CRLF]/) {
Condition66%2/3
length $data < 100 and not $data =~ /[$CRLF]/
!ll&&!rl&&r
2010
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
53 10 return '+' . $data . $CRLF;
54 }
55 2 return '$' . length($data) . $CRLF . $data . $CRLF;
56 }
57  
58 =head2 encode_from_client
59  
60 Handles client format encoding. Expects a list of data items, and will
61 convert them into length-prefixed bulk strings as a single response item.
62  
63 =cut
64  
65 sub encode_from_client {
66 0 ▶ my ($self, @data) = @_;
Subroutine encode_from_client: not called CC=1 SCAR=6.9
Pod documented
67 return '*' . (0 + @data) . $CRLF . join '', map {
68 0 '$' . length($_) . $CRLF . $_ . $CRLF
69 } @data;
70 }
71  
72 =head2 decode
73  
74 Decodes wire protocol data into Perl data structures.
75  
76 Expects to be called with a reference to a byte string, and will
77 extract as much as it can from that string (destructively).
78  
79 Likely to call L</item> or L</item_error> zero or more times.
80  
81 =cut
82  
83 sub decode {
84 28 ▶ my ($self, $bytes) = @_;
Subroutine decode: called CC=19 SCAR=31.7
Pod documented
85  
86 28 my $len = $self->{parsing_bulk};
87 ITEM:
88 28 for ($$bytes) {
89 50 ▶ if(defined($len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 6 44 50
90 6 ▶ last ITEM unless length($_) >= $len + 2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ >= $len + 2 0 6 6
91 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 $CRLF 0 6 6
92 6 $self->item(substr $_, 0, delete $self->{parsing_bulk}, '');
93 6 undef $len;
94 6 ▶ last ITEM unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 6 0 6
95 }
96 44 ▶ if(s{^\+([^\x0D]*)$CRLF}{}) {
Branch: 7 branches
BranchTrueFalseTotal
if (s/^\+([^\x0D]*)$CRLF//) { } 10 34 44
elsif (s/^:([^\x0D]*)$CRLF//) { } 12 22 34
elsif (s/^\$-1$CRLF//) { } 4 18 22
elsif (s/^\$([0-9]+)$CRLF//) { } 6 12 18
elsif (s/^\*-1$CRLF//) { } 0 12 12
elsif (s/^\*([0-9]+)$CRLF//) { } 12 0 12
elsif (s/^-([^\x0D]*)$CRLF//) { } 0 0 0
97 10 $self->item("$1");
98 } elsif(s{^:([^\x0D]*)$CRLF}{}) {
99 12 my $int = $1;
100 12 ▶ 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 12 12
Condition33%1/3
&looks_like_number($int) and int $int eq $int
!ll&&!rl&&r
0012
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
101 12 $self->item(0 + $int);
102 } elsif(s{^\$-1$CRLF}{}) {
103 4 $self->item(undef);
104 } elsif(s{^\$([0-9]+)$CRLF}{}) {
105 6 $len = $1;
106 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
107 6 $self->{parsing_bulk} = $len;
108 } elsif(s{^\*-1$CRLF}{}) {
109 0 $self->item(undef);
110 } elsif(s{^\*([0-9]+)$CRLF}{}) {
111 12 my $pending = $1;
112 12 ▶ die 'invalid numeric value for array ' . $pending unless 0+$pending eq $pending;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $pending eq $pending 0 12 12
113 12 ▶ if($pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) { } 10 2 12
114 10 push @{$self->{active}}, { array => $pending };
115 } else {
116 2 $self->item([]);
117 }
118 } elsif(s{^-([^\x0D]*)$CRLF}{}) {
119 0 $self->item_error($1);
120 } else {
121 0 last ITEM;
122 }
123 44 ▶ redo ITEM if length;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ 22 22 44
124 }
125 }
126  
127 0 ▶sub parse { $_[0]->decode($_[1]) }
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
128  
129 sub item {
130 34 ▶ my ($self, $data) = @_;
Subroutine item: called CC=4 SCAR=13.9
Pod undocumented
131 34 while(1) {
132 44 ▶ return $self->{handler}->($data) unless @{$self->{active} || []};
Branch: 2 branches
BranchTrueFalseTotal
unless @{$self->{"active"} or [];} 28 16 44
$self->{"active"} or [] 20 24 44
133  
134 16 push @{$self->{active}[-1]{items}}, $data;
135 16 ▶ return if --$self->{active}[-1]{array};
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"active"}[-1]{"array"} 6 10 16
136 10 $data = (pop @{$self->{active}})->{items};
137 }
138 }
139  
140 sub item_error {
141 0 ▶ my ($self, $err) = @_;
Subroutine item_error: not called CC=2 SCAR=17.9
Pod undocumented
142 0 ▶ $self->{error}->($err) if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
143 0 $self
144 }
145  
146 2 1;
147  
148 __END__
« Redis/Multi.pm ↑ Summary Redis/Cluster/Node.pm »