blib/lib/Bloomd/Client.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 195 SCAR 37.4
CC
6
Cov
0%
create
20.0
_check_line
12.0
_build__socket
2.0
CRAP 42.0
1 #
2 # This file is part of Bloomd-Client
3 #
4 # This software is copyright (c) 2013 by Damien "dams" Krotkine.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Bloomd::Client;
10 {
11 $Bloomd::Client::VERSION = '0.26';
12 }
13  
14 # ABSTRACT: Perl client to the bloomd server
15  
16 use feature ':5.10';
17 use Moo;
18 use List::MoreUtils qw(any mesh);
19 use Carp;
20 use Socket qw(:crlf);
21 use IO::Socket::INET;
22 use Errno qw(:POSIX);
23 use POSIX qw(strerror);
24 use Config;
25 use Types::Standard -types;
26  
27  
28 has protocol => ( is => 'ro', default => sub {'tcp'} );
29  
30  
31 has host => ( is => 'ro', isa => StrMatch[qr/.+/], default => sub {'127.0.0.1'} );
32  
33  
34 has port => ( is => 'ro', isa => Int, default => sub {8673} );
35 has _socket => ( is => 'lazy', predicate => 1, clearer => 1 );
36  
37  
38 has timeout => ( is => 'ro', , default => sub { 10 } );
39  
40  
41 sub _build__socket {
42 my ($self) = @_;
43 my $socket = IO::Socket::INET->new(
44 Proto => $self->protocol,
45 PeerHost => $self->host,
46 PeerPort => $self->port,
47 Timeout => $self->timeout,
48 ) or die "Can't connect to server: $!";
49  
50 $self->timeout
51 or return $socket;
52  
53 $Config{osname} eq 'netbsd' || $Config{osname} eq 'solaris'
54 and croak "the timeout option is not yet supported on NetBSD or Solaris";
55  
56 my $seconds = int( $self->timeout );
57 my $useconds = int( 1_000_000 * ( $self->timeout - $seconds ) );
58 my $timeout = pack( 'l!l!', $seconds, $useconds );
59  
60 $socket->setsockopt( SOL_SOCKET, SO_RCVTIMEO, $timeout )
61 or croak "setsockopt(SO_RCVTIMEO): $!";
62  
63 $socket->setsockopt( SOL_SOCKET, SO_SNDTIMEO, $timeout )
64 or croak "setsockopt(SO_SNDTIMEO): $!";
65  
66 return $socket;
67  
68 }
69  
70  
71 sub disconnect {
72 my ($self) = @_;
73 $self->_has_socket
74 and $self->_socket->close;
75 $self->_clear_socket;
76 }
77  
78  
79 sub create {
80 my ($self, $name, $capacity, $prob, $in_memory) = @_;
81 my $args =
82 ( $capacity ? "capacity=$capacity" : '' )
83 . ( $prob ? " prob=$prob" : '' )
84 . ( $in_memory ? " in_memory=$in_memory" : '' );
85 $self->_execute("create $name $args" ) eq 'Done';
86 }
87  
88  
89 sub list {
90 my ($self, $prefix) = @_;
91 $prefix //= '';
92 my @keys = qw(name prob size capacity items);
93 [
94 map {
95 my @values = split / /;
96 +{ mesh @keys, @values };
97 }
98 $self->_execute("list $prefix" )
99 ];
100 }
101  
102  
103 sub drop {
104 my ($self, $name) = @_;
105 $self->_execute("drop $name") eq 'Done';
106 }
107  
108  
109  
110 sub close {
111 my ($self, $name) = @_;
112 $self->_execute("close $name") eq 'Done';
113 }
114  
115  
116 sub clear {
117 my ($self, $name) = @_;
118 $self->_execute("clear $name") eq 'Done';
119 }
120  
121  
122 sub check {
123 my ($self, $name, $key) = @_;
124 $self->_execute("c $name $key") eq 'Yes';
125 }
126  
127  
128 sub multi {
129 my ($self, $name, @keys) = @_;
130 @keys
131 or return {};
132 my @values = map { $_ eq 'Yes' } split / /, $self->_execute("m $name @keys");
133 +{mesh @keys, @values };
134 }
135  
136  
137 sub set {
138 my ($self, $name, $key) = @_;
139 $self->_execute("s $name $key") eq 'Yes';
140 }
141  
142  
143 sub bulk {
144 my ($self, $name, @keys) = @_;
145 @keys
146 or return;
147 $self->_execute("b $name @keys");
148 return;
149 }
150  
151  
152 sub info {
153 my ($self, $name) = @_;
154 +{ map { split / / } $self->_execute("info $name") };
155 }
156  
157  
158 sub flush {
159 my ($self, $name) = @_;
160 $self->_execute("info $name") eq "Done";
161 }
162  
163 sub _execute {
164 my ($self, $command) = @_;
165 my $socket = $self->_socket;
166  
167 local $\;
168 $socket->print($command . $CRLF)
169 or croak "couldn't write to socket";
170  
171 my $line = $self->_check_line($socket->getline);
172 $line =~ /^Client Error:/
173 and croak "$line: $command";
174  
175 $line eq 'START'
176 or return $line;
177  
178 my @lines;
179 while (1) {
180 $line = $self->_check_line($socket->getline);
181 $line eq 'END'
182 and last;
183 push @lines, $line;
184 }
185  
186 return @lines;
187 }
188  
189 sub _check_line {
190 my ($self, $line) = @_;
191 if (!defined $line) {
192 my $e = $!;
193 if (any { $_ } ( $!{EWOULDBLOCK}, $!{EAGAIN}, $!{ETIMEDOUT} )) {
194 $e = strerror(ETIMEDOUT);
195 $self->disconnect;
196 }
197 undef $!;
198 croak $e;
199 }
200 $line =~ s/$CR?$LF?$//;
201 return $line;
202 }
203  
204 1;
205  
206 __END__