| 1 | package POE::Filter::Redis; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | $POE::Filter::Redis::VERSION = '0.02'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | #ABSTRACT: A POE Filter for the Redis protocol | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp qw[carp croak]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use base qw[POE::Filter]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Bit mask. | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | PARSER_IDLE => 0x01, | |||||||||||||||||||||||||||
| 16 | PARSER_BETWEEN_BULK => 0x02, | ||||||||||||||||||||||||||||
| 17 | PARSER_IN_BULK => 0x04, | ||||||||||||||||||||||||||||
| 18 | 2 | }; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | SELF_BUFFER => 0, | |||||||||||||||||||||||||||
| 22 | SELF_STATE => 1, | ||||||||||||||||||||||||||||
| 23 | SELF_HAS => 2, | ||||||||||||||||||||||||||||
| 24 | SELF_LENGTH => 3, | ||||||||||||||||||||||||||||
| 25 | SELF_AWAITING => 4, | ||||||||||||||||||||||||||||
| 26 | SELF_TYPE => 5, | ||||||||||||||||||||||||||||
| 27 | 2 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 1 | my $package = shift; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | return bless [ | |||||||||||||||||||||||||||
| 35 | '', # SELF_BUFFER | ||||||||||||||||||||||||||||
| 36 | PARSER_IDLE, # SELF_STATE | ||||||||||||||||||||||||||||
| 37 | [ ], # SELF_HAS | ||||||||||||||||||||||||||||
| 38 | 0, # SELF_LENGTH | ||||||||||||||||||||||||||||
| 39 | 0, # SELF_AWAITING | ||||||||||||||||||||||||||||
| 40 | undef, # SELF_TYPE | ||||||||||||||||||||||||||||
| 41 | ], $package; | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 52 | ▶ | sub get_one_start { | ||||||||||||||||||||||||||
|
Subroutine
get_one_start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 52 | my ($self, $stream) = @_; | |||||||||||||||||||||||||||
| 46 | 52 | $self->[SELF_BUFFER] .= join '', @{ $stream }; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 68 | ▶ | sub get_one { | ||||||||||||||||||||||||||
|
Subroutine
get_one: called CC=17 SCAR=28.7
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 68 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 68 | ▶ | return [ ] unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $self->[0] and $self->[0] =~ /\x0D\x0A/
MC/DC100%2/2
length $self->[0] and $self->[0] =~ /\x0D\x0A/
length $self->[0]
$self->[0] =~ /\x0D\x0A/
Truth table100%3/3
A and B
A= length $self->[0]B= $self->[0] =~ /\x0D\x0A/
| |||||||||||||||||||||||||||||
| 53 | length $self->[SELF_BUFFER] and $self->[SELF_BUFFER] =~ /\x0D\x0A/ | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # I expect it to be here mostly. | ||||||||||||||||||||||||||||
| 57 | 28 | ▶ | if ($self->[SELF_STATE] & PARSER_IDLE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Single-line responses. Remain in PARSER_IDLE state. | ||||||||||||||||||||||||||||
| 60 | 18 | ▶ | return [ [ $1, $2 ] ] if $self->[SELF_BUFFER] =~ s/^([-+:])(.*?)\x0D\x0A//s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 12 | ▶ | if ($self->[SELF_BUFFER] =~ s/^\*(-?\d+)\x0D\x0A//) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Zero-item multibulk is an empty list. | ||||||||||||||||||||||||||||
| 65 | # Remain in the PARSER_IDLE state. | ||||||||||||||||||||||||||||
| 66 | 6 | ▶ | return [ [ '*', ] ] if $1 == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Negative item multibulk is an undef list. | ||||||||||||||||||||||||||||
| 69 | 4 | ▶ | return [ [ '*', undef ] ] if $1 < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | @$self[SELF_STATE, SELF_AWAITING, SELF_HAS, SELF_TYPE] = ( | |||||||||||||||||||||||||||
| 72 | PARSER_BETWEEN_BULK, $1, [], '*' | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | elsif ($self->[SELF_BUFFER] =~ s/^\$(-?\d+)\x0D\x0A//) { | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # -1 length is undef. | ||||||||||||||||||||||||||||
| 78 | # Remain in the PARSER_IDLE state. | ||||||||||||||||||||||||||||
| 79 | 6 | ▶ | return [ [ '$', undef ] ] if $1 < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | @$self[SELF_STATE, SELF_AWAITING, SELF_LENGTH, SELF_HAS, SELF_TYPE] = ( | |||||||||||||||||||||||||||
| 82 | PARSER_IN_BULK, 1, $1 + 2, [], '$' | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | # TODO - Recover somehow. | ||||||||||||||||||||||||||||
| 87 | 0 | croak "illegal redis response:\n$self->[SELF_BUFFER]"; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 16 | while (1) { | |||||||||||||||||||||||||||
| 92 | 20 | ▶ | if ($self->[SELF_STATE] & PARSER_BETWEEN_BULK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Can't parse a bulk header? | ||||||||||||||||||||||||||||
| 95 | 12 | ▶ | return [ ] unless $self->[SELF_BUFFER] =~ s/^\$(-?\d+)\x0D\x0A//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # -1 length is undef. | ||||||||||||||||||||||||||||
| 98 | 6 | ▶ | if ($1 < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if (push(@{$self->[SELF_HAS]}, undef) == $self->[SELF_AWAITING]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $self->[SELF_STATE] = PARSER_IDLE; | |||||||||||||||||||||||||||
| 101 | 0 | return [ [ $self->[SELF_TYPE], @{$self->[SELF_HAS]} ] ]; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Remain in PARSER_BETWEEN_BULK state. | ||||||||||||||||||||||||||||
| 105 | 2 | next; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Got a bulk length. | ||||||||||||||||||||||||||||
| 109 | 4 | @$self[SELF_STATE, SELF_LENGTH] = (PARSER_IN_BULK, $1 + 2); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Fall through. | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # TODO - Just for debugging.. | ||||||||||||||||||||||||||||
| 115 | 12 | ▶ | croak "unexpected state $self->[SELF_STATE]" unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | $self->[SELF_STATE] & PARSER_IN_BULK | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Not enough data? | ||||||||||||||||||||||||||||
| 120 | 12 | ▶ | return [ ] if length $self->[SELF_BUFFER] < $self->[SELF_LENGTH]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Got a bulk value. | ||||||||||||||||||||||||||||
| 123 | 8 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | push( | ||||||||||||||||||||||||||||
| 125 | 8 | @{$self->[SELF_HAS]}, | |||||||||||||||||||||||||||
| 126 | substr( | ||||||||||||||||||||||||||||
| 127 | substr($self->[SELF_BUFFER], 0, $self->[SELF_LENGTH], ''), | ||||||||||||||||||||||||||||
| 128 | 0, $self->[SELF_LENGTH] - 2 | ||||||||||||||||||||||||||||
| 129 | ) | ||||||||||||||||||||||||||||
| 130 | ) == $self->[SELF_AWAITING] | ||||||||||||||||||||||||||||
| 131 | ) { | ||||||||||||||||||||||||||||
| 132 | 6 | $self->[SELF_STATE] = PARSER_IDLE; | |||||||||||||||||||||||||||
| 133 | 6 | return [ [ $self->[SELF_TYPE], @{$self->[SELF_HAS]} ] ]; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # But... not enough of them. | ||||||||||||||||||||||||||||
| 137 | 2 | $self->[SELF_STATE] = PARSER_BETWEEN_BULK; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | croak "never gonna give you up, never gonna let you down"; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub get_pending { | ||||||||||||||||||||||||||
|
Subroutine
get_pending: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | return [ $self->[SELF_BUFFER] ] if length $self->[SELF_BUFFER]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | return undef; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 4 | my ($self,$cmds) = @_; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | my @raw; | |||||||||||||||||||||||||||
| 153 | 4 | foreach my $line ( @{ $cmds } ) { | |||||||||||||||||||||||||||
| 154 | 4 | ▶ | next unless ref $line eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 4 | ▶ | next unless scalar @{ $line }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 4 | my $cmd = shift @{ $line }; | |||||||||||||||||||||||||||
| 157 | push @raw, | ||||||||||||||||||||||||||||
| 158 | join( "\x0D\x0A", | ||||||||||||||||||||||||||||
| 159 | 4 | '*' . ( 1 + @{ $line } ), | |||||||||||||||||||||||||||
| 160 | 8 | map { ('$' . length $_ => $_) } | |||||||||||||||||||||||||||
| 161 | 4 | ( uc($cmd), @{ $line } ) ) . "\x0D\x0A"; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 4 | \@raw; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | qq[Redis Filter]; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # vim: ts=2 sw=2 expandtab | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||