blib/lib/Cache/MemcachedBinary.pm

filter: Statementstmt 27.7% 96 / 346 Branchbran 6.8% 6 / 88 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 43.5% 27 / 62 Podpod 0.0% 0 / 27 totaltotal 20.0% 129 / 644 CC 80 SCAR 39.2
CC
80
Cov
20%
_request_packet
182.0
_set
132.0
get
72.0
CRAP 50.6
↑ Summary
1 package Cache::MemcachedBinary;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $| = 1;
10
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $VERSION = "1.02";
13
14 1 ▶use constant HOST => '127.0.0.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant PORT => 11211;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant LOGIN => 'login';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant PASSWORD => 'password';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant TIMEOUT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant ERROR_FLAG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 1 ▶use constant PROTOCOL_BINARY_REQ => 0x80;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant PROTOCOL_BINARY_RES => 0x81;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant PROTOCOL_BINARY_RESPONSE_SUCCESS => 0x00;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant PROTOCOL_BINARY_RESPONSE_AUTH_ERROR => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant PROTOCOL_BINARY_CMD_GET => 0x00;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant PROTOCOL_BINARY_CMD_SET => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant PROTOCOL_BINARY_CMD_ADD => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant PROTOCOL_BINARY_CMD_DELETE => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant PROTOCOL_BINARY_CMD_INCREMENT => 0x05;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant PROTOCOL_BINARY_CMD_QUIT => 0x07;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant PROTOCOL_BINARY_CMD_FLUSH => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant PROTOCOL_BINARY_CMD_SASL_LIST_MECHS => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant PROTOCOL_BINARY_CMD_SASL_AUTH => 0x21;
Subroutine BEGIN: called CC=1 SCAR=0.0
35
36 1 ▶sub new {
Subroutine new: called CC=7 SCAR=20.2
Pod undocumented
37 1 my $class = shift;
38 1 my %param = @_;
39
40 1 ▶ if (! exists $param{host}) {$param{host} = HOST;}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"host"}) 1 0 1
41 1 ▶ if (! exists $param{port}) {$param{port} = PORT;}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"port"}) 1 0 1
42 1 ▶ if (! exists $param{timeout}) {$param{timeout} = TIMEOUT;}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"timeout"}) 1 0 1
43 1 ▶ if (! exists $param{login}) {$param{login} = LOGIN;}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"login"}) 1 0 1
44 1 ▶ if (! exists $param{password}) {$param{password} = PASSWORD;}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"password"}) 1 0 1
45 1 ▶ if (! exists $param{debug}) {$param{debug} = DEBUG;}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"debug"}) 1 0 1
46
47 1 bless {%param}, $class;
48 }
49
50 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
51 0 my $self = shift;
52 0 $self->_set(PROTOCOL_BINARY_CMD_ADD, @_);
53 }
54
55 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my $self = shift;
57 0 $self->_set(PROTOCOL_BINARY_CMD_SET, @_);
58 }
59
60 0 ▶sub _set {
Subroutine _set: not called CC=11 SCAR=48.8
61 0 my $self = shift;
62 0 my ($opcode, $key, $val, $sec) = @_;
63 0 ▶ $sec //= 0;
Condition0%0/2
$sec //= 0
l!l
00
MC/DC0%0/1
$sec //= 0
$sec
Truth table0%0/2
A
A= $sec
Aresult
11
00
64
65 0 ▶ if ($opcode == PROTOCOL_BINARY_CMD_ADD) {$self->_log("ADD key:$key, seconds:$sec");}
Branch: 2 branches
BranchTrueFalseTotal
if ($opcode == 2) { } 0 0 0
elsif ($opcode == 1) { } 0 0 0
66 0 elsif ($opcode == PROTOCOL_BINARY_CMD_SET) {$self->_log("SET key:$key, seconds:$sec");}
67
68 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
69
70 0 ▶ if (Encode::is_utf8($key)) {Encode::_utf8_off($key);}
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($key)) 0 0 0
71 0 ▶ if (Encode::is_utf8($val)) {Encode::_utf8_off($val);}
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($val)) 0 0 0
72
73 0 my $sec_binary = sprintf '%08x', $sec;
74
75 0 $self->set_err(undef);
76
77 0 $self->_request_packet({
78 Opcode => $opcode,
79 Extras => 'deadbeef' . $sec_binary,
80 Key => join("", map sprintf('%x', $_), unpack("C*", $key)),
81 Value => join("", map sprintf('%x', $_), unpack("C*", $val)),
82 });
83
84 0 my $data = $self->_read_responce();
85 0 ▶ if ( $$data{MagicUnpack} == PROTOCOL_BINARY_RES && $$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_SUCCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0) 0 0 0
Condition0%0/3
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
$data->{"MagicUnpack"} == 129 $data->{"StatusUnpack"} == 0
Truth table0%0/3
A and B
A= $data->{"MagicUnpack"} == 129B= $data->{"StatusUnpack"} == 0
ABresult
0X0
100
111
86 0 $self->_log("Result: success");
87 0 return 1;
88 }
89
90 0 $self->_log("Result: fail, server answer: $$data{ValueUnpack}");
91 0 $self->set_err($$data{ValueUnpack});
92
93 0 ▶ if ($$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_AUTH_ERROR) { # auth error, try auth
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"StatusUnpack"} == 32) 0 0 0
94 0 $self->_socket_destroy();
95 0 ▶ return $self->_set(@_) if $self->auth();
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth 0 0 0
96 }
97
98 0 return;
99 }
100
101 0 ▶sub get {
Subroutine get: not called CC=8 SCAR=42.8
Pod undocumented
102 0 my $self = shift;
103 0 ▶ my $key = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
104
105 0 $self->_log("GET key:$key");
106
107 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
108
109 0 ▶ if (Encode::is_utf8($key)) {Encode::_utf8_off($key);}
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($key)) 0 0 0
110
111 0 $self->set_err(undef);
112
113 0 $self->_request_packet({
114 Opcode => PROTOCOL_BINARY_CMD_GET,
115 Key => join("", map sprintf('%x', $_), unpack("C*", $key)),
116 });
117
118 0 my $data = $self->_read_responce();
119 0 ▶ if ( $$data{MagicUnpack} == PROTOCOL_BINARY_RES && $$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_SUCCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0) 0 0 0
Condition0%0/3
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
$data->{"MagicUnpack"} == 129 $data->{"StatusUnpack"} == 0
Truth table0%0/3
A and B
A= $data->{"MagicUnpack"} == 129B= $data->{"StatusUnpack"} == 0
ABresult
0X0
100
111
120 0 $self->_log("Result: success");
121 0 return $$data{ValueUnpack};
122 }
123
124 0 $self->_log("Result: fail, server answer: $$data{ValueUnpack}");
125 0 $self->set_err($$data{ValueUnpack});
126
127 0 ▶ if ($$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_AUTH_ERROR) { # auth error, try auth
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"StatusUnpack"} == 32) 0 0 0
128 0 $self->_socket_destroy();
129 0 ▶ return $self->get($key) if $self->auth();
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth 0 0 0
130 }
131
132 0 return;
133 }
134
135 0 ▶sub delete {
Subroutine delete: not called CC=8 SCAR=42.8
Pod undocumented
136 0 my $self = shift;
137 0 ▶ my $key = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
138
139 0 $self->_log("DELETE key:$key");
140
141 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
142
143 0 ▶ if (Encode::is_utf8($key)) {Encode::_utf8_off($key);}
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($key)) 0 0 0
144
145 0 $self->set_err(undef);
146
147 0 $self->_request_packet({
148 Opcode => PROTOCOL_BINARY_CMD_DELETE,
149 Key => join("", map sprintf('%x', $_), unpack("C*", $key)),
150 });
151
152 0 my $data = $self->_read_responce();
153 0 ▶ if ( $$data{MagicUnpack} == PROTOCOL_BINARY_RES && $$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_SUCCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0) 0 0 0
Condition0%0/3
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
$data->{"MagicUnpack"} == 129 $data->{"StatusUnpack"} == 0
Truth table0%0/3
A and B
A= $data->{"MagicUnpack"} == 129B= $data->{"StatusUnpack"} == 0
ABresult
0X0
100
111
154 0 $self->_log("Result: success");
155 0 return 1;
156 }
157
158 0 $self->_log("Result: fail, server answer: $$data{ValueUnpack}");
159 0 $self->set_err($$data{ValueUnpack});
160
161 0 ▶ if ($$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_AUTH_ERROR) { # auth error, try auth
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"StatusUnpack"} == 32) 0 0 0
162 0 $self->_socket_destroy();
163 0 ▶ return $self->delete($key) if $self->auth();
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth 0 0 0
164 }
165
166 0 return;
167 }
168
169 0 ▶sub incr {
Subroutine incr: not called CC=8 SCAR=42.8
Pod undocumented
170 0 my $self = shift;
171 0 ▶ my $key = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
172
173 0 $self->_log("INCREMENT key:$key");
174
175 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
176
177 0 ▶ if (Encode::is_utf8($key)) {Encode::_utf8_off($key);}
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($key)) 0 0 0
178
179 0 $self->set_err(undef);
180
181 0 $self->_request_packet({
182 Opcode => PROTOCOL_BINARY_CMD_INCREMENT,
183 Key => join("", map sprintf('%x', $_), unpack("C*", $key)),
184 Extras => '0000000000000001' . '0000000000000000' . '00000000',
185 });
186
187 0 my $data = $self->_read_responce();
188 0 ▶ if ( $$data{MagicUnpack} == PROTOCOL_BINARY_RES && $$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_SUCCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0) 0 0 0
Condition0%0/3
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
$data->{"MagicUnpack"} == 129 $data->{"StatusUnpack"} == 0
Truth table0%0/3
A and B
A= $data->{"MagicUnpack"} == 129B= $data->{"StatusUnpack"} == 0
ABresult
0X0
100
111
189 0 $self->_log("Result: success");
190 0 my @mess = unpack('N2', $$data{Value});
191 0 return $mess[1];
192 0 return 1;
193 }
194
195 0 $self->_log("Result: fail, server answer: $$data{ValueUnpack}");
196 0 $self->set_err($$data{ValueUnpack});
197
198 0 ▶ if ($$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_AUTH_ERROR) { # auth error, try auth
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"StatusUnpack"} == 32) 0 0 0
199 0 $self->_socket_destroy();
200 0 ▶ return $self->incr($key) if $self->auth();
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth 0 0 0
201 }
202
203 0 return;
204 }
205
206 0 ▶sub flush {
Subroutine flush: not called CC=7 SCAR=40.3
Pod undocumented
207 0 my $self = shift;
208 0 ▶ my $sec = shift // 0;
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
209
210 0 $self->_log("FLUSH seconds:$sec");
211
212 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
213
214 0 my $sec_binary = sprintf '%08x', $sec;
215
216 0 $self->set_err(undef);
217
218 0 $self->_request_packet({
219 Opcode => PROTOCOL_BINARY_CMD_FLUSH,
220 Extras => $sec_binary,
221 });
222
223 0 my $data = $self->_read_responce();
224 0 ▶ if ( $$data{MagicUnpack} == PROTOCOL_BINARY_RES && $$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_SUCCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0) 0 0 0
Condition0%0/3
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
$data->{"MagicUnpack"} == 129 $data->{"StatusUnpack"} == 0
Truth table0%0/3
A and B
A= $data->{"MagicUnpack"} == 129B= $data->{"StatusUnpack"} == 0
ABresult
0X0
100
111
225 0 $self->_log("Result: success");
226 0 return 1;
227 }
228
229 0 $self->_log("Result: fail, server answer: $$data{ValueUnpack}");
230 0 $self->set_err($$data{ValueUnpack});
231
232 0 ▶ if ($$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_AUTH_ERROR) { # auth error, try auth
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"StatusUnpack"} == 32) 0 0 0
233 0 $self->_socket_destroy();
234 0 ▶ return $self->flush($sec) if $self->auth();
Branch: 1 branch
BranchTrueFalseTotal
if $self->auth 0 0 0
235 }
236
237 0 return;
238 }
239
240 0 ▶sub quit {
Subroutine quit: not called CC=4 SCAR=30.0
Pod undocumented
241 0 my $self = shift;
242
243 0 $self->_log("QUIT");
244
245 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
246
247 0 $self->set_err(undef);
248
249 0 $self->_request_packet({
250 Opcode => PROTOCOL_BINARY_CMD_QUIT,
251 });
252
253 0 my $data = $self->_read_responce();
254 0 ▶ if ( $$data{MagicUnpack} == PROTOCOL_BINARY_RES && $$data{StatusUnpack} == PROTOCOL_BINARY_RESPONSE_SUCCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0) 0 0 0
Condition0%0/3
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"MagicUnpack"} == 129 and $data->{"StatusUnpack"} == 0
$data->{"MagicUnpack"} == 129 $data->{"StatusUnpack"} == 0
Truth table0%0/3
A and B
A= $data->{"MagicUnpack"} == 129B= $data->{"StatusUnpack"} == 0
ABresult
0X0
100
111
255 0 $self->_log("Result: success");
256 0 $self->_socket_destroy();
257 0 return 1;
258 }
259
260 0 $self->_log("Result: fail, server answer: $$data{ValueUnpack}");
261 0 $self->set_err($$data{ValueUnpack});
262
263 0 return;
264 }
265
266 0 ▶sub auth {
Subroutine auth: not called CC=4 SCAR=30.0
Pod undocumented
267 0 my $self = shift;
268
269 0 $self->_log("AUTH");
270
271 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
272
273 0 $self->_request_packet({
274 Opcode => PROTOCOL_BINARY_CMD_SASL_LIST_MECHS,
275 });
276
277 0 my $data = $self->_read_responce();
278
279 0 my $methods = unpack('A*', $$data{Value});
280 0 $self->_log("Server support auth SASL methods: $methods");
281 0 ▶ if ( $methods !~ m/PLAIN/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($methods =~ /PLAIN/) 0 0 0
282 0 $self->set_err("Server not support SASL PLAIN");
283 0 $self->_log("Server not support SASL PLAIN", ERROR_FLAG);
284 0 return;
285 }
286
287 0 my $login = join "", map sprintf('%x', $_), unpack("C*", $self->login);
288 0 my $password = join "", map sprintf('%x', $_), unpack("C*", $self->password);
289 0 $self->_request_packet({
290 Opcode => PROTOCOL_BINARY_CMD_SASL_AUTH,
291 Key => join("", map sprintf('%x', $_), unpack("C*", 'PLAIN')),
292 Value => $login . '00' . $login . '00' . $password,
293 });
294
295 0 $data = $self->_read_responce();
296 0 my $mess = unpack('A*', $$data{Value});
297 0 $self->_log("Server answer: $mess");
298 0 ▶ if ( $mess ne 'Authenticated' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess ne "Authenticated") 0 0 0
299 0 $self->set_err("Auth failure");
300 0 $self->_log("Auth failure with login: " . $self->login, ERROR_FLAG);
301 0 return;
302 }
303
304 0 return 1;
305 }
306
307 0 ▶sub _request_packet {
Subroutine _request_packet: not called CC=13 SCAR=52.0
308 0 my $self = shift;
309 0 ▶ my $data = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
310
311 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
312
313 0 $$data{Magic} = sprintf '%x', PROTOCOL_BINARY_REQ;
314 0 $$data{Opcode} = sprintf '%x', $$data{Opcode};
315 0 $$data{DataType} = '00'; # Raw bytes
316
317 0 ▶ $$data{Extras} //= '';
Condition0%0/2
$data->{"Extras"} //= ""
l!l
00
MC/DC0%0/1
$data->{"Extras"} //= ""
$data->{"Extras"}
Truth table0%0/2
A
A= $data->{"Extras"}
Aresult
11
00
318 0 ▶ $$data{Key} //= '';
Condition0%0/2
$data->{"Key"} //= ""
l!l
00
MC/DC0%0/1
$data->{"Key"} //= ""
$data->{"Key"}
Truth table0%0/2
A
A= $data->{"Key"}
Aresult
11
00
319 0 ▶ $$data{Value} //= '';
Condition0%0/2
$data->{"Value"} //= ""
l!l
00
MC/DC0%0/1
$data->{"Value"} //= ""
$data->{"Value"}
Truth table0%0/2
A
A= $data->{"Value"}
Aresult
11
00
320
321 0 $$data{ExtrasLength} = length($$data{Extras}) / 2;
322 0 $$data{KeyLength} = length($$data{Key}) / 2;
323 0 $$data{TotalBodyLength} = length($$data{Value}) / 2 + $$data{ExtrasLength} + $$data{KeyLength};
324
325 0 ▶ $$data{Opcode} = sprintf '%02d', ( $$data{Opcode} // '00' );
Condition0%0/2
$data->{"Opcode"} // "00"
l!l
00
MC/DC0%0/1
$data->{"Opcode"} // "00"
$data->{"Opcode"}
Truth table0%0/2
A
A= $data->{"Opcode"}
Aresult
11
00
326 0 ▶ $$data{KeyLength} = sprintf '%04x', ( $$data{KeyLength} // '0000' );
Condition0%0/2
$data->{"KeyLength"} // "0000"
l!l
00
MC/DC0%0/1
$data->{"KeyLength"} // "0000"
$data->{"KeyLength"}
Truth table0%0/2
A
A= $data->{"KeyLength"}
Aresult
11
00
327 0 ▶ $$data{ExtrasLength} = sprintf '%02x', ( $$data{ExtrasLength} // '00' );
Condition0%0/2
$data->{"ExtrasLength"} // "00"
l!l
00
MC/DC0%0/1
$data->{"ExtrasLength"} // "00"
$data->{"ExtrasLength"}
Truth table0%0/2
A
A= $data->{"ExtrasLength"}
Aresult
11
00
328 0 ▶ $$data{VbucketId} = sprintf '%04d', ( $$data{VbucketId} // '0000' );
Condition0%0/2
$data->{"VbucketId"} // "0000"
l!l
00
MC/DC0%0/1
$data->{"VbucketId"} // "0000"
$data->{"VbucketId"}
Truth table0%0/2
A
A= $data->{"VbucketId"}
Aresult
11
00
329 0 ▶ $$data{TotalBodyLength} = sprintf '%08x', ( $$data{TotalBodyLength} // '00000000' );
Condition0%0/2
$data->{"TotalBodyLength"} // "00000000"
l!l
00
MC/DC0%0/1
$data->{"TotalBodyLength"} // "00000000"
$data->{"TotalBodyLength"}
Truth table0%0/2
A
A= $data->{"TotalBodyLength"}
Aresult
11
00
330 0 ▶ $$data{Opaque} = sprintf '%08d', ( $$data{Opaque} // '00000000' );
Condition0%0/2
$data->{"Opaque"} // "00000000"
l!l
00
MC/DC0%0/1
$data->{"Opaque"} // "00000000"
$data->{"Opaque"}
Truth table0%0/2
A
A= $data->{"Opaque"}
Aresult
11
00
331 0 ▶ $$data{CAS} = sprintf '%016d', ( $$data{CAS} // '0000000000000000' );
Condition0%0/2
$data->{"CAS"} // "0000000000000000"
l!l
00
MC/DC0%0/1
$data->{"CAS"} // "0000000000000000"
$data->{"CAS"}
Truth table0%0/2
A
A= $data->{"CAS"}
Aresult
11
00
332
333 0 my $packet = join "", map( $$data{$_}, (qw(Magic Opcode KeyLength ExtrasLength DataType VbucketId TotalBodyLength Opaque CAS Extras Key Value)));
334
335 0 $self->_log(">> $packet");
336 0 send($socket, pack('H*', $packet), 0);
337
338 0 return 1;
339 }
340
341 0 ▶sub _read_responce {
Subroutine _read_responce: not called CC=7 SCAR=40.3
342 0 my $self = shift;
343
344 0 ▶ my $socket = $self->_get_socket() || return;
Condition0%0/2
$self->_get_socket || (return)
l!l
00
MC/DC0%0/1
$self->_get_socket || (return)
$self->_get_socket
Truth table0%0/2
A
A= $self->_get_socket
Aresult
11
00
345
346 0 my %data;
347
348 0 eval {
349 0 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
350 0 alarm $self->timeout;
351
352 0 sysread($socket, $data{Magic}, 1);
353 0 sysread($socket, $data{Opcode}, 1);
354 0 sysread($socket, $data{KeyLength}, 2);
355 0 sysread($socket, $data{ExtrasLength}, 1);
356 0 sysread($socket, $data{DataType}, 1);
357 0 sysread($socket, $data{Status}, 2);
358 0 sysread($socket, $data{TotalBodyLength}, 4);
359 0 sysread($socket, $data{Opaque}, 4);
360 0 sysread($socket, $data{CAS}, 8);
361
362 0 $data{Key} = '';
363 0 $data{Extras} = '';
364 0 $data{Value} = '';
365
366 0 my $len_key = sprintf('%d', unpack( 'H*', $data{KeyLength} ));
367 0 my $len_extras = hex(unpack( 'H*', $data{ExtrasLength} ));
368 0 ▶ my $len_total = unpack('N*', $data{TotalBodyLength}) || 0;
Condition0%0/2
unpack('N*', $data{'TotalBodyLength'}) || 0
l!l
00
MC/DC0%0/1
unpack('N*', $data{'TotalBodyLength'}) || 0
unpack('N*', $data{'TotalBodyLength'})
Truth table0%0/2
A
A= unpack('N*', $data{'TotalBodyLength'})
Aresult
11
00
369 0 my $len_value = $len_total - $len_extras - $len_key;
370
371 0 ▶ if ($len_extras) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_extras) 0 0 0
372 0 sysread($socket, $data{Extras}, $len_extras);
373 }
374
375 0 ▶ if ($len_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_key) 0 0 0
376 0 sysread($socket, $data{Key}, $len_key);
377 }
378
379 0 ▶ if ($len_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_value) 0 0 0
380 0 sysread($socket, $data{Value}, $len_value);
381 }
382
383 0 $data{MagicUnpack} = hex(unpack('H*', $data{Magic}));
384 0 $data{OpcodeUnpack} = hex(unpack('H*', $data{Opcode}));
385 0 $data{StatusUnpack} = unpack('n1', $data{Status});
386 0 $data{ValueUnpack} = unpack('A*', $data{Value});
387
388 0 alarm 0;
389 };
390
391 0 $self->_log("<< " . ( join "", map { unpack('H*', $data{$_}) } (qw(Magic Opcode KeyLength ExtrasLength DataType Status TotalBodyLength Opaque CAS Extras Key Value)) ));
392
393 0 ▶ if ( $data{OpcodeUnpack} == PROTOCOL_BINARY_CMD_GET ) { # clean binary data from Value section, maybe big data
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"OpcodeUnpack"} == 0) 0 0 0
394 0 $data{Value} = '';
395 }
396
397 0 return \%data;
398 }
399
400 0 ▶sub _get_socket {
Subroutine _get_socket: not called CC=3 SCAR=24.8
401 0 my $self = shift;
402
403 0 ▶ return $self->socket if $self->socket && $self->socket->connected();
Branch: 1 branch
BranchTrueFalseTotal
if $self->socket and $self->socket->connected 0 0 0
Condition0%0/3
$self->socket and $self->socket->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->socket and $self->socket->connected
$self->socket $self->socket->connected
Truth table0%0/3
A and B
A= $self->socketB= $self->socket->connected
ABresult
0X0
100
111
404 0 return $self->_connect();
405 }
406
407 0 ▶sub _connect {
Subroutine _connect: not called CC=4 SCAR=30.0
408 0 my $self = shift;
409
410 0 my $socket = IO::Socket::INET->new(
411 PeerAddr => $self->host,
412 PeerPort => $self->port,
413 Proto => 'tcp',
414 Timeout => $self->timeout,
415 Type => SOCK_STREAM
416 );
417
418 0 my $error = $!;
419 0 ▶ if ($error) {Encode::_utf8_on($error);}
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
420
421 0 $self->_log( sprintf "Connect to host: %s, port: %s, timeout: %s", $self->host, $self->port, $self->timeout );
422 0 ▶ $self->_log("result: " . ($socket ? 'success' : 'error: ' . $error));
Branch: 1 branch
BranchTrueFalseTotal
$socket ? : 0 0 0
423
424 0 ▶ if ($socket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket) 0 0 0
425 0 binmode $socket;
426 0 $self->set_socket($socket);
427 0 return $socket;
428 }
429
430 0 $self->set_err($!);
431 0 $self->_log( sprintf("Don't connect to %s:%s, error: %s", $self->host, $self->port, $error), ERROR_FLAG );
432 0 return;
433 }
434
435 0 ▶sub _socket_destroy {
Subroutine _socket_destroy: not called CC=2 SCAR=17.9
436 0 my $self = shift;
437 0 $self->_log("socket destroy " . $self->socket);
438 0 ▶ close($self->socket) if $self->socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->socket 0 0 0
439 0 $self->set_socket(undef);
440 0 return;
441 }
442
443 0 ▶sub _log {
Subroutine _log: not called CC=7 SCAR=40.3
444 0 my ($self, $str, $flag) = @_;
445 0 ▶ return unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 0 0
446
447 0 ▶ $flag //= 0;
Condition0%0/2
$flag //= 0
l!l
00
MC/DC0%0/1
$flag //= 0
$flag
Truth table0%0/2
A
A= $flag
Aresult
11
00
448
449 0 ▶ if (! $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->logger) 0 0 0
450 0 ▶ $self->set_logger( sub { print STDERR shift() . "\n" } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
451 }
452
453 0 ▶ if (ref $self->logger ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->logger ne "CODE") 0 0 0
454 0 print STDERR "param 'logger' has type of reference not function\n";
455 0 return;
456 }
457
458 0 ▶ if ($self->debug || $flag == ERROR_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug or $flag == 1) 0 0 0
Condition0%0/3
$self->debug or $flag == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->debug or $flag == 1
$self->debug $flag == 1
Truth table0%0/3
A or B
A= $self->debugB= $flag == 1
ABresult
1X1
011
000
459 0 $self->logger->($str);
460 }
461
462 0 return;
463 }
464
465 0 ▶sub host {return $_[0]->{host}}
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
466 0 ▶sub port {return $_[0]->{port}}
Subroutine port: not called CC=1 SCAR=6.9
Pod undocumented
467 0 ▶sub timeout {return $_[0]->{timeout}}
Subroutine timeout: not called CC=1 SCAR=6.9
Pod undocumented
468 0 ▶sub login {return $_[0]->{login}}
Subroutine login: not called CC=1 SCAR=6.9
Pod undocumented
469 0 ▶sub password {return $_[0]->{password}}
Subroutine password: not called CC=1 SCAR=6.9
Pod undocumented
470 0 ▶sub socket {return $_[0]->{socket}}
Subroutine socket: not called CC=1 SCAR=6.9
Pod undocumented
471 0 ▶sub debug {return $_[0]->{debug}}
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
472 0 ▶sub logger {return $_[0]->{logger}}
Subroutine logger: not called CC=1 SCAR=6.9
Pod undocumented
473 0 ▶sub err {return $_[0]->{err}}
Subroutine err: not called CC=1 SCAR=6.9
Pod undocumented
474
475 0 ▶sub set_host { $_[0]->{err} = $_[1]; }
Subroutine set_host: not called CC=1 SCAR=6.9
Pod undocumented
476 0 ▶sub set_port { $_[0]->{port} = $_[1]; }
Subroutine set_port: not called CC=1 SCAR=6.9
Pod undocumented
477 0 ▶sub set_timeout { $_[0]->{timeout} = $_[1]; }
Subroutine set_timeout: not called CC=1 SCAR=6.9
Pod undocumented
478 0 ▶sub set_login { $_[0]->{login} = $_[1]; }
Subroutine set_login: not called CC=1 SCAR=6.9
Pod undocumented
479 0 ▶sub set_password { $_[0]->{password} = $_[1]; }
Subroutine set_password: not called CC=1 SCAR=6.9
Pod undocumented
480 0 ▶sub set_socket { $_[0]->{socket} = $_[1]; }
Subroutine set_socket: not called CC=1 SCAR=6.9
Pod undocumented
481 0 ▶sub set_debug { $_[0]->{debug} = $_[1]; }
Subroutine set_debug: not called CC=1 SCAR=6.9
Pod undocumented
482 0 ▶sub set_logger { $_[0]->{logger} = $_[1]; }
Subroutine set_logger: not called CC=1 SCAR=6.9
Pod undocumented
483 0 ▶sub set_err { $_[0]->{err} = $_[1]; }
Subroutine set_err: not called CC=1 SCAR=6.9
Pod undocumented
484
485 1 1;
486 __END__
↑ Summary