File Coverage

blib/lib/App/Memcached/Tool/DataSource.pm
Criterion Covered Total %
statement 17 60 28.3
branch 0 14 0.0
condition 0 2 0.0
subroutine 6 13 46.1
pod 0 5 0.0
total 23 94 24.4


line stmt bran cond sub pod time code
1             package App::Memcached::Tool::DataSource;
2              
3 2     2   872 use strict;
  2         3  
  2         52  
4 2     2   8 use warnings;
  2         2  
  2         49  
5 2     2   31 use 5.008_001;
  2         6  
6              
7 2     2   8 use Carp;
  2         2  
  2         123  
8 2     2   1139 use IO::Socket;
  2         40356  
  2         7  
9              
10 2     2   1975 use App::Memcached::Tool::Util qw(is_unixsocket debug);
  2         5  
  2         1190  
11              
12             sub new {
13 0     0 0   my $class = shift;
14 0           my %args = @_;
15 0           bless \%args, $class;
16             }
17              
18             sub connect {
19 0     0 0   my $class = shift;
20 0           my $addr = shift;
21 0           my %opts = @_;
22              
23             my $socket = sub {
24 0 0   0     return IO::Socket::UNIX->new(Peer => $addr) if is_unixsocket($addr);
25             return IO::Socket::INET->new(
26             PeerAddr => $addr,
27             Proto => 'tcp',
28 0   0       Timeout => $opts{timeout} || 5,
29             );
30 0           }->();
31 0 0         confess "Can't connect to $addr" unless $socket;
32              
33 0           return $class->new(socket => $socket);
34             }
35              
36             sub disconnect {
37 0     0 0   my $self = shift;
38 0 0         $self->{socket}->close or confess "Failed to close connection!";
39 0           undef $self->{socket};
40             }
41              
42             sub get {
43 0     0 0   my $self = shift;
44 0           my $key = shift;
45              
46 0           my $socket = $self->{socket};
47 0           print $socket "get $key\r\n";
48              
49 0           my %data = (key => $key);
50 0           my $response = <$socket>;
51 0 0         if ($response =~ m/VALUE \S+ (\d+) (\d+)/) {
52 0           $data{flags} = $1;
53 0           $data{length} = $2;
54 0           read $socket, $response, $data{length};
55 0           $data{value} = $response;
56              
57 0           while ($response !~ m/^END/) { $response = <$socket>; }
  0            
58             } else {
59 0           warn "KEY $key not found in $response";
60             }
61              
62 0           return \%data;
63             }
64              
65             sub query {
66 0     0 0   my $self = shift;
67 0           my $query = shift;
68              
69 0           my $socket = $self->{socket};
70 0           print $socket "$query\r\n";
71              
72 0           my @response;
73 0           while (<$socket>) {
74 0 0         last if m/^END/;
75 0 0         confess $_ if m/^SERVER_ERROR/;
76 0           $_ =~ s/[\r\n]+$//;
77 0           push @response, $_;
78             }
79              
80 0           return \@response;
81             }
82              
83             sub DESTROY {
84 0     0     my $self = shift;
85 0 0         if ($self->{socket}) { $self->{socket}->close; }
  0            
86             }
87              
88             1;
89             __END__