Redis/Protocol.pm

filter: Statementstmt 10.0% 7 / 70 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 22.2% 2 / 9 Podpod 0.0% 0 / 1 totaltotal 6.3% 9 / 142 CC 26 SCAR 48.8
CC
26
Cov
6%
__ANON__
240.0
__ANON__
90.0
__ANON__
12.0
CRAP 131.4
« Redis.pm ↑ Summary Redis/Error.pm »
1 package AnyEvent::Redis::Protocol;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 AnyEvent::Redis::Protocol - Redis response parser (read handler) for AnyEvent
9  
10 =head1 DESCRIPTION
11  
12 This package should not be directly used. It provides an AnyEvent read handler
13 capable of parsing Redis responses.
14  
15 =head1 SEE ALSO
16  
17 L<AnyEvent::Handle>,
18 Redis Protocol Specification L<http://redis.io/topics/protocol>
19  
20 =cut
21  
22 0 ▶sub anyevent_read_type {
Subroutine anyevent_read_type: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my ($handle, $cb) = @_;
24  
25 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 ▶ $handle->push_read(line => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
27 0 my $line = $_[1];
28 0 my $type = substr($line, 0, 1);
29 0 my $value = substr($line, 1);
30 0 ▶ if ($type eq '*') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq '*') { } 0 0 0
elsif ($type eq '+' or $type eq ':') { } 0 0 0
elsif ($type eq '-') { } 0 0 0
elsif ($type eq '$') { } 0 0 0
31 # Multi-bulk reply
32 0 my $remaining = $value;
33 0 ▶ if ($remaining == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($remaining == 0) { } 0 0 0
elsif ($remaining == -1) { } 0 0 0
34 0 $cb->([]);
35 } elsif ($remaining == -1) {
36 0 $cb->(undef);
37 } else {
38 0 my $results = [];
39 0 ▶ $handle->unshift_read(sub {
Subroutine __ANON__: not called CC=15 SCAR=54.8
40 0 my $need_more_data = 0;
41 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$need_more_data or do { do { if ($handle->{"rbuf"} =~ /^(\$(-?\d+)\015\012)/) { () = ($1, $2); if ($vallen == -1) { substr $handle->{"rbuf"}, 0, length $match, ""; push @$results, undef; unless (--$remaining) { &$cb($results); return 1; } ; } elsif (length $handle->{"rbuf"} >= length($match) + $vallen + 2) { substr $handle->{"rbuf"}, 0, length $match, ""; my $value = substr($handle->{"rbuf"}, 0, $vallen, ""); $value = $handle->{"encoding"}->decode($value) if $handle->{"encoding"} and $vallen; push @$results, $value; substr $handle->{"rbuf"}, 0, 2, ""; unless (--$remaining) { &$cb($results); return 1; } ; } else { $need_more_data = 1; } ; } elsif ($handle->{"rbuf"} =~ s/^([\+\-:])([^\015\012]*)\015\012//) { () = ($1, $2); if ($type eq "+" or $type eq ":") { push @$results, $value; } elsif ($type eq "-") { push @$results, bless(\$value, "AnyEvent::Redis::Error"); } ; unless (--$remaining) { &$cb($results); return 1; } ; } elsif (substr($handle->{"rbuf"}, 0, 1) eq "*") { my $reader; $reader = sub { $handle->unshift_read("AnyEvent::Redis::Protocol", sub { push @$results, $_[0]; if (--$remaining) { &$reader(); } else { undef $reader; &$cb($results); } ; } ); } ; &$reader(); return 1; } else { $need_more_data = 1; } } } 0 0 0
42 0 ▶ if ($handle->{rbuf} =~ /^(\$(-?\d+)\015\012)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($handle->{'rbuf'} =~ /^(\$(-?\d+)\015\012)/) { } 0 0 0
elsif ($handle->{'rbuf'} =~ s/^([\+\-:])([^\015\012]*)\015\012//) { } 0 0 0
elsif (substr($handle->{'rbuf'}, 0, 1) eq '*') { } 0 0 0
43 0 my ($match, $vallen) = ($1, $2);
44 0 ▶ if ($vallen == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($vallen == -1) { } 0 0 0
elsif (length $handle->{'rbuf'} >= length($match) + $vallen + 2) { } 0 0 0
45 # Delete the bulk header.
46 0 substr($handle->{rbuf}, 0, length($match), '');
47 0 push @$results, undef;
48 0 ▶ unless (--$remaining) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$remaining) 0 0 0
49 0 $cb->($results);
50 0 return 1;
51 }
52 } elsif (length $handle->{rbuf} >= (length($match) + $vallen + 2)) {
53 # OK, we have enough in our buffer.
54 # Delete the bulk header.
55 0 substr($handle->{rbuf}, 0, length($match), '');
56 0 my $value = substr($handle->{rbuf}, 0, $vallen, '');
57 $value = $handle->{encoding}->decode($value)
58 0 ▶ if $handle->{encoding} && $vallen;
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"encoding"} and $vallen 0 0 0
Condition0%0/3
$handle->{"encoding"} and $vallen
!ll&&!rl&&r
000
MC/DC0%0/2
$handle->{"encoding"} and $vallen
$handle->{"encoding"} $vallen
Truth table0%0/3
A and B
A= $handle->{"encoding"}B= $vallen
ABresult
0X0
100
111
59 0 push @$results, $value;
60 # Delete trailing data characters.
61 0 substr($handle->{rbuf}, 0, 2, '');
62 0 ▶ unless (--$remaining) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$remaining) 0 0 0
63 0 $cb->($results);
64 0 return 1;
65 }
66 } else {
67 0 $need_more_data = 1;
68 }
69 } elsif ($handle->{rbuf} =~ s/^([\+\-:])([^\015\012]*)\015\012//) {
70 0 my ($type, $value) = ($1, $2);
71 0 ▶ if ($type eq '+' || $type eq ':') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '+' or $type eq ':') { } 0 0 0
elsif ($type eq '-') { } 0 0 0
Condition0%0/3
$type eq "+" or $type eq ":"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "+" or $type eq ":"
$type eq "+" $type eq ":"
Truth table0%0/3
A or B
A= $type eq "+"B= $type eq ":"
ABresult
1X1
011
000
72 0 push @$results, $value;
73 } elsif ($type eq '-') {
74 # Embedded error; this seems possible only in EXEC answer,
75 # so include error in results; don't abort parsing
76 0 push @$results, bless \$value, 'AnyEvent::Redis::Error';
77 }
78 0 ▶ unless (--$remaining) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$remaining) 0 0 0
79 0 $cb->($results);
80 0 return 1;
81 }
82 } elsif (substr($handle->{rbuf}, 0, 1) eq '*') {
83 # Oh, how fun! A nested bulk reply.
84 0 ▶ my $reader; $reader = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 ▶ $handle->unshift_read("AnyEvent::Redis::Protocol" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 push @$results, $_[0];
87 0 ▶ if (--$remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$remaining) { } 0 0 0
88 0 $reader->();
89 } else {
90 0 undef $reader;
91 0 $cb->($results);
92 }
93 0 });
94 0 };
95 0 $reader->();
96 0 return 1;
97 } else {
98 # Nothing matched - read more...
99 0 $need_more_data = 1;
100 }
101 } until $need_more_data;
102 0 return; # get more data
103 0 });
104 }
105 ▶ } elsif ($type eq '+' || $type eq ':') {
Condition0%0/3
$type eq "+" or $type eq ":"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "+" or $type eq ":"
$type eq "+" $type eq ":"
Truth table0%0/3
A or B
A= $type eq "+"B= $type eq ":"
ABresult
1X1
011
000
106 # Single line/integer reply
107 0 $cb->($value);
108 } elsif ($type eq '-') {
109 # Single-line error reply
110 0 $cb->($value, 1);
111 } elsif ($type eq '$') {
112 # Bulk reply
113 0 my $length = $value;
114 0 ▶ if ($length == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == -1) { } 0 0 0
115 0 $cb->(undef);
116 } else {
117 # We need to read 2 bytes more than the length (stupid
118 # CRLF framing). Then we need to discard them.
119 0 ▶ $handle->unshift_read(chunk => $length + 2, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
120 0 my $data = $_[1];
121 0 my $value = substr($data, 0, $length);
122 $value = $handle->{encoding}->decode($value)
123 0 ▶ if $handle->{encoding} && $length;
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"encoding"} and $length 0 0 0
Condition0%0/3
$handle->{"encoding"} and $length
!ll&&!rl&&r
000
MC/DC0%0/2
$handle->{"encoding"} and $length
$handle->{"encoding"} $length
Truth table0%0/3
A and B
A= $handle->{"encoding"}B= $length
ABresult
0X0
100
111
124 0 $cb->($value);
125 0 });
126 }
127 }
128 0 return 1;
129 0 });
130 0 return 1;
131 0 };
132 }
133  
134 =head1 AUTHOR
135  
136 Michael S. Fischer <michael+cpan@dynamine.net>
137  
138 =head1 COPYRIGHT AND LICENSE
139  
140 Copyright (C) 2010 Michael S. Fischer.
141  
142 This program is free software; you can redistribute it and/or modify it
143 under the terms of either: the GNU General Public License as published
144 by the Free Software Foundation; or the Artistic License.
145  
146 See http://dev.perl.org/licenses/ for more information.
147  
148 =cut
149  
150 8 1;
151  
152 __END__
« Redis.pm ↑ Summary Redis/Error.pm »