lib/Bloomd/Client.pm

filter: Statementstmt 68.7% 77 / 112 Branchbran 35.2% 12 / 34 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 60.0% 18 / 30 Podpod 100% 12 / 12 totaltotal 61.7% 121 / 196 SCAR 38.1
CC
20
Cov
60%
create
20.0
_execute
8.6
_build__socket
8.4
CRAP 45.1
↑ Summary blib/lib/Bloomd/Client.pm »
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 2 $Bloomd::Client::VERSION = '0.26';
12 }
13  
14 # ABSTRACT: Perl client to the bloomd server
15  
16 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use List::MoreUtils qw(any mesh);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Socket qw(:crlf);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use POSIX qw(strerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 1 ▶has protocol => ( is => 'ro', default => sub {'tcp'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30  
31 0 ▶has host => ( is => 'ro', isa => StrMatch[qr/.+/], default => sub {'127.0.0.1'} );
Subroutine __ANON__: not called CC=1 SCAR=1.2
32  
33  
34 0 ▶has port => ( is => 'ro', isa => Int, default => sub {8673} );
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 2 has _socket => ( is => 'lazy', predicate => 1, clearer => 1 );
36  
37  
38 0 ▶has timeout => ( is => 'ro', , default => sub { 10 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
39  
40  
41 sub _build__socket {
42 2 ▶ my ($self) = @_;
Subroutine _build__socket: called CC=7 SCAR=21.3
43 2 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("Proto", $self->protocol, "PeerHost", $self->host, "PeerPort", $self->port, "Timeout", $self->timeout) or die "Can't connect to server: $!" 0 2 2
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 2 ▶ $self->timeout
Branch: 1 branch
BranchTrueFalseTotal
$self->timeout or return $socket 0 2 2
51 or return $socket;
52  
53 2 ▶ $Config{osname} eq 'netbsd' || $Config{osname} eq 'solaris'
Branch: 1 branch
BranchTrueFalseTotal
$Bloomd::Client::Config{"osname"} eq "netbsd" || $Bloomd::Client::Config{"osname"} eq "solaris" and croak("the timeout option is not yet supported on NetBSD or Solaris") 0 2 2
Condition33%1/3
$Bloomd::Client::Config{"osname"} eq "netbsd" or $Bloomd::Client::Config{"osname"} eq "solaris"
l!l&&r!l&&!r
002
MC/DC0%0/2
$Bloomd::Client::Config{"osname"} eq "netbsd" or $Bloomd::Client::Config{"osname"} eq "solaris"
$Bloomd::Client::Config{"osname"} eq "netbsd" $Bloomd::Client::Config{"osname"} eq "solaris"
Truth table33%1/3
A or B
A= $Bloomd::Client::Config{"osname"} eq "netbsd"B= $Bloomd::Client::Config{"osname"} eq "solaris"
ABresult
1X1
011
000
54 and croak "the timeout option is not yet supported on NetBSD or Solaris";
55  
56 2 my $seconds = int( $self->timeout );
57 2 my $useconds = int( 1_000_000 * ( $self->timeout - $seconds ) );
58 2 my $timeout = pack( 'l!l!', $seconds, $useconds );
59  
60 2 ▶ $socket->setsockopt( SOL_SOCKET, SO_RCVTIMEO, $timeout )
Branch: 1 branch
BranchTrueFalseTotal
$socket->setsockopt(1, 20, $timeout) or croak("setsockopt(SO_RCVTIMEO): $!") 0 2 2
61 or croak "setsockopt(SO_RCVTIMEO): $!";
62  
63 2 ▶ $socket->setsockopt( SOL_SOCKET, SO_SNDTIMEO, $timeout )
Branch: 1 branch
BranchTrueFalseTotal
$socket->setsockopt(1, 21, $timeout) or croak("setsockopt(SO_SNDTIMEO): $!") 0 2 2
64 or croak "setsockopt(SO_SNDTIMEO): $!";
65  
66 2 return $socket;
67  
68 }
69  
70  
71 sub disconnect {
72 1 ▶ my ($self) = @_;
Subroutine disconnect: called CC=2 SCAR=7.1
Pod documented
73 1 ▶ $self->_has_socket
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_socket and $self->_socket->close 1 0 1
74 and $self->_socket->close;
75 1 $self->_clear_socket;
76 }
77  
78  
79 sub create {
80 0 ▶ my ($self, $name, $capacity, $prob, $in_memory) = @_;
Subroutine create: not called CC=4 SCAR=30.0
Pod documented
81 0 ▶ my $args =
Branch: 3 branches
BranchTrueFalseTotal
$capacity ? : 0 0 0
$prob ? : 0 0 0
$in_memory ? : 0 0 0
82 ( $capacity ? "capacity=$capacity" : '' )
83 . ( $prob ? " prob=$prob" : '' )
84 . ( $in_memory ? " in_memory=$in_memory" : '' );
85 0 $self->_execute("create $name $args" ) eq 'Done';
86 }
87  
88  
89 sub list {
90 1 ▶ my ($self, $prefix) = @_;
Subroutine list: called CC=2 SCAR=7.9
Pod documented
91 1 ▶ $prefix //= '';
Condition50%1/2
$prefix //= ""
l!l
01
MC/DC0%0/1
$prefix //= ""
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
92 1 my @keys = qw(name prob size capacity items);
93 [
94 map {
95 1 my @values = split / /;
96 0 +{ mesh @keys, @values };
97 }
98 $self->_execute("list $prefix" )
99 ];
100 }
101  
102  
103 sub drop {
104 0 ▶ my ($self, $name) = @_;
Subroutine drop: not called CC=1 SCAR=6.9
Pod documented
105 0 $self->_execute("drop $name") eq 'Done';
106 }
107  
108  
109  
110 sub close {
111 0 ▶ my ($self, $name) = @_;
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
112 0 $self->_execute("close $name") eq 'Done';
113 }
114  
115  
116 sub clear {
117 0 ▶ my ($self, $name) = @_;
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
118 0 $self->_execute("clear $name") eq 'Done';
119 }
120  
121  
122 sub check {
123 0 ▶ my ($self, $name, $key) = @_;
Subroutine check: not called CC=1 SCAR=6.9
Pod documented
124 0 $self->_execute("c $name $key") eq 'Yes';
125 }
126  
127  
128 sub multi {
129 0 ▶ my ($self, $name, @keys) = @_;
Subroutine multi: not called CC=2 SCAR=17.9
Pod documented
130 @keys
131 0 ▶ or return {};
Branch: 1 branch
BranchTrueFalseTotal
@keys or return {} 0 0 0
132 0 my @values = map { $_ eq 'Yes' } split / /, $self->_execute("m $name @keys");
133 0 +{mesh @keys, @values };
134 }
135  
136  
137 sub set {
138 0 ▶ my ($self, $name, $key) = @_;
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
139 0 $self->_execute("s $name $key") eq 'Yes';
140 }
141  
142  
143 sub bulk {
144 0 ▶ my ($self, $name, @keys) = @_;
Subroutine bulk: not called CC=2 SCAR=17.9
Pod documented
145 @keys
146 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
@keys or return 0 0 0
147 0 $self->_execute("b $name @keys");
148 0 return;
149 }
150  
151  
152 sub info {
153 1 ▶ my ($self, $name) = @_;
Subroutine info: called CC=1 SCAR=0.0
Pod documented
154 1 +{ map { split / / } $self->_execute("info $name") };
155 }
156  
157  
158 sub flush {
159 0 ▶ my ($self, $name) = @_;
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
160 0 $self->_execute("info $name") eq "Done";
161 }
162  
163 sub _execute {
164 2 ▶ my ($self, $command) = @_;
Subroutine _execute: called CC=5 SCAR=21.5
165 2 my $socket = $self->_socket;
166  
167 2 local $\;
168 2 ▶ $socket->print($command . $CRLF)
Branch: 1 branch
BranchTrueFalseTotal
$socket->print($command . $Bloomd::Client::CRLF) or croak("couldn't write to socket") 0 2 2
169 or croak "couldn't write to socket";
170  
171 2 my $line = $self->_check_line($socket->getline);
172 1 ▶ $line =~ /^Client Error:/
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^Client Error:/ and croak("${line}: $command") 0 1 1
173 and croak "$line: $command";
174  
175 1 ▶ $line eq 'START'
Branch: 1 branch
BranchTrueFalseTotal
$line eq "START" or return $line 1 0 1
176 or return $line;
177  
178 0 my @lines;
179 0 while (1) {
180 0 $line = $self->_check_line($socket->getline);
181 0 ▶ $line eq 'END'
Branch: 1 branch
BranchTrueFalseTotal
$line eq "END" and last 0 0 0
182 and last;
183 0 push @lines, $line;
184 }
185  
186 0 return @lines;
187 }
188  
189 sub _check_line {
190 2 ▶ my ($self, $line) = @_;
Subroutine _check_line: called CC=3 SCAR=11.0
191 2 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 1 1 2
192 1 my $e = $!;
193 1 ▶ if (any { $_ } ( $!{EWOULDBLOCK}, $!{EAGAIN}, $!{ETIMEDOUT} )) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_; } , $!{"EWOULDBLOCK"}, $!{"EAGAIN"}, $!{"ETIMEDOUT"})) 1 0 1
194 1 $e = strerror(ETIMEDOUT);
195 1 $self->disconnect;
196 }
197 1 undef $!;
198 1 croak $e;
199 }
200 1 $line =~ s/$CR?$LF?$//;
201 1 return $line;
202 }
203  
204 2 1;
205  
206 __END__
↑ Summary blib/lib/Bloomd/Client.pm »