blib/lib/DR/Tarantool/LLClient.pm

filter: Statementstmt 14.2% 40 / 281 Branchbran 1.0% 1 / 96 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 24.4% 12 / 49 Podpod 78.5% 11 / 14 totaltotal 12.2% 64 / 521 CC 72 SCAR 36.3
CC
72
Cov
10%
_log_transaction
182.0
connect
90.0
_check_rbuf
42.0
CRAP 37.8
« blib/lib/DR/Tarantool/RealSyncClient.pm ↑ Summary blib/lib/DR/Tarantool/AsyncClient.pm »
1 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 DR::Tarantool::LLClient - a low level async client
8 for L<Tarantool|http://tarantool.org>
9  
10 =head1 SYNOPSIS
11  
12 DR::Tarantool::LLClient->connect(
13 host => '127.0.0.1',
14 port => '33033',
15 cb => {
16 my ($tnt) = @_;
17 ...
18 }
19 );
20  
21 $tnt->ping( sub { .. } );
22 $tnt->insert(0, [ 1, 2, 3 ], sub { ... });
23 $tnt->select(1, 0, [ [ 1, 2 ], [ 3, 4 ] ], sub { ... });
24 $tnt->update(0, [ 1 ], [ [ 1 => add pack 'L<', 1 ] ], sub { ... });
25 $tnt->call_lua( 'box.select', [ 0, 1, 2 ], sub { ... });
26  
27  
28 =head1 DESCRIPTION
29  
30 This module provides a low-level interface to
31 L<Tarantool|http://tarantool.org>.
32  
33 =head1 METHODS
34  
35 All methods receive B<callback> as the last argument. The callback receives
36 B<HASHREF> value with the following fields:
37  
38 =over
39  
40 =item status
41  
42 Done status:
43  
44 =over
45  
46 =item fatal
47  
48 A fatal error occurred. The server closed the connection or returned a
49 broken package.
50  
51 =item buffer
52  
53 An internal driver error.
54  
55 =item error
56  
57 The request wasn't executed: the server returned an error.
58  
59 =item ok
60  
61 Request was executed OK.
62  
63 =back
64  
65 =item errstr
66  
67 If an error occurred, contains error description.
68  
69 =item code
70  
71 Contains reply code.
72  
73 =item req_id
74  
75 Contains request id.
76 (see
77 L<protocol documentation|https://github.com/mailru/tarantool/blob/master/doc/box-protocol.txt>)
78  
79 =item type
80  
81 Contains request type
82 (see
83 L<protocol documentation|https://github.com/mailru/tarantool/blob/master/doc/box-protocol.txt>)
84  
85 =item count
86  
87 Contains the count of returned tuples.
88  
89 =item tuples
90  
91 Returned tuples (B<ARRAYREF> of B<ARRAYREF>).
92  
93 =back
94  
95 If you use B<NUM> or B<NUM64> field types, values
96 for these fields need to be packed before they are sent to the
97 server, and unpacked when received in a response.
98 This is a low-level driver :)
99  
100 =cut
101  
102  
103 package DR::Tarantool::LLClient;
104 5 ▶use base qw(DR::Tarantool::AEConnection);
Subroutine BEGIN: called CC=1 SCAR=0.0
105 5 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 5 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 5 ▶use Devel::GlobalDestruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 5 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 5 $Carp::Internal{ (__PACKAGE__) }++;
111  
112 5 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
113 5 require DR::Tarantool;
114 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 5 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 5 ▶my $LE = $] > 5.01 ? '<' : '';
Branch: 1 branch
BranchTrueFalseTotal
$] > 5.01 ? : 5 0 5
118  
119  
120 =head2 connect
121  
122 Creates a connection to L<Tarantool| http://tarantool.org>
123  
124 DR::Tarantool::LLClient->connect(
125 host => '127.0.0.1',
126 port => '33033',
127 cb => {
128 my ($tnt) = @_;
129 ...
130 }
131 );
132  
133 =head3 Arguments
134  
135 =over
136  
137 =item host & port
138  
139 Host and port to connect to.
140  
141 =item reconnect_period
142  
143 An interval to wait before trying to reconnect after a fatal error or
144 unsuccessful connect. If the field is defined and is greater than 0, the
145 driver tries to reconnect to the server after this interval.
146  
147 B<Important>: the driver does not reconnect after B<the first>
148 unsuccessful connection. It calls B<callback> instead.
149  
150 =item reconnect_always
151  
152 Try to reconnect even after the first unsuccessful connection.
153  
154 =item cb
155  
156 Done callback. The callback receives a connection handle
157 connected to the server or an error string.
158  
159 =back
160  
161 =cut
162  
163 sub connect {
164 0 ▶ my $class = shift;
Subroutine connect: not called CC=9 SCAR=45.0
Pod documented
165  
166 0 my (%opts, $cb);
167  
168 0 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 0 0 0
169 0 $cb = pop;
170 0 %opts = @_;
171 } else {
172 0 %opts = @_;
173 0 $cb = delete $opts{cb};
174 }
175  
176 0 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$cb ||= sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table0%0/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
177  
178 0 $class->_check_cb( $cb );
179  
180 0 ▶ return $class->SUPER::connect if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
181  
182  
183 0 ▶ my $host = $opts{host} || 'localhost';
Condition0%0/2
$opts{'host'} || 'localhost'
l!l
00
MC/DC0%0/1
$opts{'host'} || 'localhost'
$opts{'host'}
Truth table0%0/2
A
A= $opts{'host'}
Aresult
11
00
184 0 ▶ my $port = $opts{port} or croak "port is undefined";
Branch: 1 branch
BranchTrueFalseTotal
my $port = $opts{"port"} or croak("port is undefined") 0 0 0
185  
186 0 ▶ my $reconnect_period = $opts{reconnect_period} || 0;
Condition0%0/2
$opts{'reconnect_period'} || 0
l!l
00
MC/DC0%0/1
$opts{'reconnect_period'} || 0
$opts{'reconnect_period'}
Truth table0%0/2
A
A= $opts{'reconnect_period'}
Aresult
11
00
187 0 ▶ my $reconnect_always = $opts{reconnect_always} || 0;
Condition0%0/2
$opts{'reconnect_always'} || 0
l!l
00
MC/DC0%0/1
$opts{'reconnect_always'} || 0
$opts{'reconnect_always'}
Truth table0%0/2
A
A= $opts{'reconnect_always'}
Aresult
11
00
188  
189 0 my $self = $class->SUPER::new(
190 host => $host,
191 port => $port,
192 reconnect_period => $reconnect_period,
193 reconnect_always => $reconnect_always,
194 );
195  
196 $self->on(connected => sub {
197 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 $self->on(connected => $self->on_connected);
199 0 $self->on_connected->($self);
200 0 $cb->($self);
201 0 });
202  
203 $self->on(connfail => sub {
204 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 $self->on(connfail => undef);
206 0 ▶ unless($self->reconnect_always) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->reconnect_always) 0 0 0
207 0 $self->on(connected => undef);
208 0 $cb->($self->error);
209 }
210 0 });
211  
212 $self->on(error => sub {
213 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 $self->_fatal_error($self->error);
215 0 });
216  
217 0 $self->SUPER::connect;
218  
219 0 ▶ unless (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
220 0 my $cbb = $cb;
221 $cb = sub {
222 0 ▶ &$cbb;
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 undef $self;
224 0 };
225 0 return;
226 }
227  
228 0 return $self;
229 }
230  
231 ▶sub _reconnected {
Subroutine _reconnected: not called CC=1 SCAR=0.0
232 }
233  
234  
235 sub on_connected {
236 sub {
237 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 $self->_reconnected;
239 0 $self->{guard}{read} = AE::io $self->fh, 0, $self->on_read;
240 }
241 0 ▶}
Subroutine on_connected: not called CC=1 SCAR=6.9
Pod undocumented
242  
243  
244 sub disconnect {
245 0 ▶ my ($self, $cb) = @_;
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod undocumented
246 0 ▶ $cb ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$cb ||= sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= sub { }
$cb sub { }
Truth table0%0/3
A ||= B
A= $cbB= sub { }
ABresult
1X1
011
000
247 0 $self->_check_cb( $cb );
248  
249 0 $self->SUPER::disconnect;
250 0 $cb->( 'ok' );
251 }
252  
253 sub DESTROY {
254 0 ▶ return if in_global_destruction;
Subroutine DESTROY: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if &in_global_destruction() 0 0 0
255 0 my ($self) = @_;
256 0 $self->disconnect;
257 }
258  
259 =head2 is_connected
260  
261 B<True> if this connection is established.
262  
263 =cut
264  
265 sub is_connected {
266 0 ▶ my ($self) = @_;
Subroutine is_connected: not called CC=1 SCAR=6.9
Pod documented
267 0 $self->state eq 'connected';
268 }
269  
270 =head2 connection_status
271  
272 Contains a string with the status of connection. Return value can be:
273  
274 =over
275  
276 =item ok
277  
278 Connection is established.
279  
280 =item not_connected
281  
282 Connection isn't established yet, or was lost.
283  
284 =item connecting
285  
286 The driver is connecting to the server.
287  
288 =item fatal
289  
290 An attempt to connect was made, but ended up with an error.
291 If the event loop is running, and B<reconnect_period> option
292 is set, the driver continues to try to reconnect and update its status.
293  
294 =back
295  
296 =cut
297  
298 sub connection_status {
299 0 ▶ my ($self) = @_;
Subroutine connection_status: not called CC=4 SCAR=30.0
Pod documented
300 0 ▶ return 'ok' if $self->state eq 'connected';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "connected" 0 0 0
301 0 ▶ return 'connecting' if $self->state eq 'connecting';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "connecting" 0 0 0
302 0 ▶ return 'fatal' if $self->state eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "error" 0 0 0
303 0 return 'not_connected';
304 }
305  
306  
307 =head2 ping
308  
309 Ping the server.
310  
311 $tnt->ping( sub { .. } );
312  
313 =head3 Arguments
314  
315 =over
316  
317 =item a callback
318  
319 =back
320  
321 =cut
322  
323 sub ping :method {
324 0 ▶ my ($self, $cb) = @_;
Subroutine ping: not called CC=3 SCAR=24.8
Pod documented
325 0 my $id = $self->_req_id;
326 0 $self->_check_cb( $cb );
327 0 my $pkt = DR::Tarantool::_pkt_ping( $id );
328  
329 0 ▶ if ($self->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_connected) 0 0 0
330 0 $self->_request( $id, $pkt, $cb );
331 0 return;
332 }
333
334 0 ▶ unless($self->reconnect_period) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->reconnect_period) 0 0 0
335 0 $cb->({
336 status => 'fatal',
337 req_id => $id,
338 errstr => "Connection isn't established (yet)"
339 }
340 );
341 0 return;
342 }
343  
344 0 my $this = $self;
345 0 weaken $this;
346  
347 0 my $tmr;
348 $tmr = AE::timer $self->reconnect_period, 0, sub {
349 0 ▶ undef $tmr;
Subroutine __ANON__: not called CC=3 SCAR=24.8
350 0 ▶ if ($this and $this->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this and $this->is_connected) 0 0 0
Condition0%0/3
$this and $this->is_connected
!ll&&!rl&&r
000
MC/DC0%0/2
$this and $this->is_connected
$this $this->is_connected
Truth table0%0/3
A and B
A= $thisB= $this->is_connected
ABresult
0X0
100
111
351 0 $this->_request( $id, $pkt, $cb );
352 0 return;
353 }
354 0 $cb->({
355 status => 'fatal',
356 req_id => $id,
357 errstr => "Connection isn't established (yet)"
358 }
359 );
360 0 };
361 }
362  
363  
364 =head2 insert
365  
366 Insert a tuple.
367  
368 $tnt->insert(0, [ 1, 2, 3 ], sub { ... });
369 $tnt->insert(0, [ 4, 5, 6 ], $flags, sub { .. });
370  
371 =head3 Arguments
372  
373 =over
374  
375 =item space
376  
377 =item tuple
378  
379 =item flags (optional)
380  
381 =item callback
382  
383 =back
384  
385 =cut
386  
387 sub insert :method {
388  
389 0 ▶ my $self = shift;
Subroutine insert: not called CC=4 SCAR=30.0
Pod documented
390 0 $self->_check_number( my $space = shift );
391 0 $self->_check_tuple( my $tuple = shift );
392 0 $self->_check_cb( my $cb = pop );
393 0 ▶ $self->_check_number( my $flags = pop || 0 );
Condition0%0/2
pop() || 0
l!l
00
MC/DC0%0/1
pop() || 0
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
394 0 ▶ croak "insert: tuple must be ARRAYREF" unless ref $tuple eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tuple eq "ARRAY" 0 0 0
395 0 ▶ $flags ||= 0;
Condition0%0/2
$flags ||= 0
l!l
00
MC/DC0%0/1
$flags ||= 0
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
396  
397 0 my $id = $self->_req_id;
398 0 my $pkt = DR::Tarantool::_pkt_insert( $id, $space, $flags, $tuple );
399 0 $self->_request( $id, $pkt, $cb );
400 0 return;
401 }
402  
403 =head2 select
404  
405 Select a tuple or tuples.
406  
407 $tnt->select(1, 0, [ [ 1, 2 ], [ 3, 4 ] ], sub { ... });
408 $tnt->select(1, 0, [ [ 1, 2 ], [ 3, 4 ] ], 1, sub { ... });
409 $tnt->select(1, 0, [ [ 1, 2 ], [ 3, 4 ] ], 1, 2, sub { ... });
410  
411 =head3 Arguments
412  
413 =over
414  
415 =item space
416  
417 =item index
418  
419 =item tuple_keys
420  
421 =item limit (optional)
422  
423 If the limit isn't set or is zero, select extracts all records without
424 a limit.
425  
426 =item offset (optional)
427  
428 Default value is B<0>.
429  
430 =item callback for results
431  
432 =back
433  
434 =cut
435  
436 sub select :method {
437  
438 0 ▶ my $self = shift;
Subroutine select: not called CC=3 SCAR=24.8
Pod documented
439 0 $self->_check_number( my $ns = shift );
440 0 $self->_check_number( my $idx = shift );
441 0 $self->_check_tuple_list( my $keys = shift );
442 0 $self->_check_cb( my $cb = pop );
443 0 ▶ $self->_check_number( my $limit = shift || 0x7FFFFFFF );
Condition0%0/2
shift() || 2147483647
l!l
00
MC/DC0%0/1
shift() || 2147483647
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
444 0 ▶ $self->_check_number( my $offset = 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
445  
446 0 my $id = $self->_req_id;
447 0 my $pkt =
448 DR::Tarantool::_pkt_select($id, $ns, $idx, $offset, $limit, $keys);
449 0 $self->_request( $id, $pkt, $cb );
450 0 return;
451 }
452  
453 =head2 update
454  
455 Update a tuple.
456  
457 $tnt->update(0, [ 1 ], [ [ 1 => add 1 ] ], sub { ... });
458 $tnt->update(
459 0, # space
460 [ 1 ], # key
461 [ [ 1 => add 1 ], [ 2 => add => 1 ], # operations
462 $flags, # flags
463 sub { ... } # callback
464 );
465 $tnt->update(0, [ 1 ], [ [ 1 => add 1 ] ], $flags, sub { ... });
466  
467 =head3 Arguments
468  
469 =over
470  
471 =item space
472  
473 =item tuple_key
474  
475 =item operations list
476  
477 =item flags (optional)
478  
479 =item callback for results
480  
481 =back
482  
483 =cut
484  
485 sub update :method {
486  
487 0 ▶ my $self = shift;
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
488 0 $self->_check_number( my $ns = shift );
489 0 $self->_check_tuple( my $key = shift );
490 0 $self->_check_operations( my $operations = shift );
491 0 $self->_check_cb( my $cb = pop );
492 0 ▶ $self->_check_number( my $flags = pop || 0 );
Condition0%0/2
pop() || 0
l!l
00
MC/DC0%0/1
pop() || 0
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
493  
494 0 my $id = $self->_req_id;
495 0 my $pkt = DR::Tarantool::_pkt_update($id, $ns, $flags, $key, $operations);
496 0 $self->_request( $id, $pkt, $cb );
497 0 return;
498  
499 }
500  
501 =head2 delete
502  
503 Delete a tuple.
504  
505 $tnt->delete( 0, [ 1 ], sub { ... });
506 $tnt->delete( 0, [ 1 ], $flags, sub { ... });
507  
508 =head3 Arguments
509  
510 =over
511  
512 =item space
513  
514 =item tuple_key
515  
516 =item flags (optional)
517  
518 =item callback for results
519  
520 =back
521  
522 =cut
523  
524 sub delete :method {
525 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
526 0 my $ns = shift;
527 0 my $key = shift;
528 0 $self->_check_tuple( $key );
529 0 my $cb = pop;
530 0 $self->_check_cb( $cb );
531 0 ▶ my $flags = pop || 0;
Condition0%0/2
pop() || 0
l!l
00
MC/DC0%0/1
pop() || 0
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
532  
533 0 my $id = $self->_req_id;
534 0 my $pkt = DR::Tarantool::_pkt_delete($id, $ns, $flags, $key);
535 0 $self->_request( $id, $pkt, $cb );
536 0 return;
537 }
538  
539  
540 =head2 call_lua
541  
542 Calls a lua procedure.
543  
544 $tnt->call_lua( 'box.select', [ 0, 1, 2 ], sub { ... });
545 $tnt->call_lua( 'box.select', [ 0, 1, 2 ], $flags, sub { ... });
546  
547 =head3 Arguments
548  
549 =over
550  
551 =item name of the procedure
552  
553 =item tuple_key
554  
555 =item flags (optional)
556  
557 =item callback to call when the request is ready
558  
559 =back
560  
561 =cut
562  
563 sub call_lua :method {
564  
565 0 ▶ my $self = shift;
Subroutine call_lua: not called CC=2 SCAR=17.9
Pod documented
566 0 my $proc = shift;
567 0 my $tuple = shift;
568 0 $self->_check_tuple( $tuple );
569 0 my $cb = pop;
570 0 $self->_check_cb( $cb );
571 0 ▶ my $flags = pop || 0;
Condition0%0/2
pop() || 0
l!l
00
MC/DC0%0/1
pop() || 0
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
572  
573 0 my $id = $self->_req_id;
574 0 my $pkt = DR::Tarantool::_pkt_call_lua($id, $flags, $proc, $tuple);
575 0 $self->_request( $id, $pkt, $cb );
576 0 return;
577 }
578  
579  
580 =head2 last_code
581  
582 Return code of the last request or B<undef> if there was no
583 request.
584  
585 =cut
586  
587 sub last_code {
588 0 ▶ my ($self) = @_;
Subroutine last_code: not called CC=2 SCAR=17.9
Pod documented
589 0 ▶ return $self->{last_code} if exists $self->{last_code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"last_code"} 0 0 0
590 0 return undef;
591 }
592  
593  
594 =head2 last_error_string
595  
596 An error string if the last request ended up with an
597 error, or B<undef> otherwise.
598  
599 =cut
600  
601 sub last_error_string {
602 0 ▶ my ($self) = @_;
Subroutine last_error_string: not called CC=2 SCAR=17.9
Pod documented
603 0 ▶ return $self->{last_error_string} if exists $self->{last_error_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"last_error_string"} 0 0 0
604 0 return undef;
605 }
606  
607 =head1 Logging
608  
609 The module can log requests/responses. Logging can be turned ON by
610 setting these environment variables:
611  
612 =over
613  
614 =item TNT_LOG_DIR
615  
616 Instructs LLClient to record all requests/responses into this directory.
617  
618 =item TNT_LOG_ERRDIR
619  
620 Instructs LLClient to record all requests/responses which
621 ended up with an error into this directory.
622  
623 =back
624  
625 =cut
626  
627  
628 sub _log_transaction {
629 0 ▶ my ($self, $id, $pkt, $response, $res_pkt) = @_;
Subroutine _log_transaction: not called CC=13 SCAR=52.0
630  
631 0 my $logdir = $ENV{TNT_LOG_DIR};
632 0 ▶ goto DOLOG if $logdir;
Branch: 1 branch
BranchTrueFalseTotal
if $logdir 0 0 0
633 0 $logdir = $ENV{TNT_LOG_ERRDIR};
634 0 ▶ goto DOLOG if $logdir and $response->{status} ne 'ok';
Branch: 1 branch
BranchTrueFalseTotal
if $logdir and $response->{"status"} ne "ok" 0 0 0
Condition0%0/3
$logdir and $response->{"status"} ne "ok"
!ll&&!rl&&r
000
MC/DC0%0/2
$logdir and $response->{"status"} ne "ok"
$logdir $response->{"status"} ne "ok"
Truth table0%0/3
A and B
A= $logdirB= $response->{"status"} ne "ok"
ABresult
0X0
100
111
635 0 return;
636  
637 DOLOG:
638 0 eval {
639 0 ▶ die "Directory $logdir was not found, transaction wasn't logged\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $logdir 0 0 0
640 unless -d $logdir;
641  
642 0 my $now = Time::HiRes::time;
643  
644 my $logdirname = catfile $logdir,
645 0 sprintf '%s-%s', $now, $response->{status};
646  
647 0 ▶ die "Object $logdirname is already exists, transaction wasn't logged\n"
Branch: 1 branch
BranchTrueFalseTotal
if -e $logdirname or -d $logdirname 0 0 0
Condition0%0/3
-e $logdirname or -d $logdirname
l!l&&r!l&&!r
000
MC/DC0%0/2
-e $logdirname or -d $logdirname
-e $logdirname -d $logdirname
Truth table0%0/3
A or B
A= -e $logdirnameB= -d $logdirname
ABresult
1X1
011
000
648 if -e $logdirname or -d $logdirname;
649
650 0 ▶ die $! unless mkdir $logdirname;
Branch: 1 branch
BranchTrueFalseTotal
unless mkdir $logdirname 0 0 0
651
652 0 my $rrname = catfile $logdirname,
653 sprintf 'rawrequest-%04d.bin', $id;
654 0 ▶ open my $fh, '>:raw', $rrname or die "Can't open $rrname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $rrname or die "Can't open ${rrname}: $!\n" 0 0 0
655 0 print $fh $pkt;
656 0 close $fh;
657  
658 0 my $respname = catfile $logdirname,
659 sprintf 'dumpresponse-%04d.txt', $id;
660  
661 0 ▶ open $fh, '>:raw', $respname or die "Can't open $respname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:raw", $respname or die "Can't open ${respname}: $!\n" 0 0 0
662
663 0 local $Data::Dumper::Indent = 1;
664 0 local $Data::Dumper::Terse = 1;
665 0 local $Data::Dumper::Useqq = 1;
666 0 local $Data::Dumper::Deepcopy = 1;
667 0 local $Data::Dumper::Maxdepth = 0;
668 0 print $fh Dumper($response);
669 0 close $fh;
670  
671 0 ▶ if (defined $res_pkt) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res_pkt) 0 0 0
672 0 $respname = catfile $logdirname,
673 sprintf 'rawresponse-%04d.bin', $id;
674 0 ▶ open $fh, '>:raw', $respname or die "Can't open $respname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:raw", $respname or die "Can't open ${respname}: $!\n" 0 0 0
675 0 print $fh $res_pkt;
676 0 close $fh;
677 }
678 };
679 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
680 }
681  
682  
683 sub _request {
684 0 ▶ my ($self, $id, $pkt, $cb ) = @_;
Subroutine _request: not called CC=3 SCAR=24.8
685 # Scalar::Util::weaken $self;
686
687 0 my $cbres = $cb;
688 0 ▶ $cbres = sub { $self->_log_transaction($id, $pkt, @_); &$cb }
Subroutine __ANON__: not called CC=1 SCAR=6.9
689 0 ▶ if $ENV{TNT_LOG_ERRDIR} or $ENV{TNT_LOG_DIR};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TNT_LOG_ERRDIR"} or $ENV{"TNT_LOG_DIR"} 0 0 0
Condition0%0/3
$ENV{"TNT_LOG_ERRDIR"} or $ENV{"TNT_LOG_DIR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"TNT_LOG_ERRDIR"} or $ENV{"TNT_LOG_DIR"}
$ENV{"TNT_LOG_ERRDIR"} $ENV{"TNT_LOG_DIR"}
Truth table0%0/3
A or B
A= $ENV{"TNT_LOG_ERRDIR"}B= $ENV{"TNT_LOG_DIR"}
ABresult
1X1
011
000
690  
691 0 $self->{ wait }{ $id } = $cbres;
692  
693 0 $self->push_write($pkt);
694 }
695  
696 sub _req_id {
697 0 ▶ my ($self) = @_;
Subroutine _req_id: not called CC=4 SCAR=30.0
698 0 ▶ for (my $id = $self->{req_id} || 0;; $id++) {
Condition0%0/2
$self->{'req_id'} || 0
l!l
00
MC/DC0%0/1
$self->{'req_id'} || 0
$self->{'req_id'}
Truth table0%0/2
A
A= $self->{'req_id'}
Aresult
11
00
699 0 ▶ $id = 0 unless $id < 0x7FFF_FFFF;
Branch: 1 branch
BranchTrueFalseTotal
unless $id < 2147483647 0 0 0
700 0 ▶ next if exists $self->{wait}{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"wait"}{$id} 0 0 0
701 0 $self->{req_id} = $id + 1;
702 0 return $id;
703 }
704 }
705  
706 sub _fatal_error {
707 0 ▶ my ($self, $msg, $raw) = @_;
Subroutine _fatal_error: not called CC=5 SCAR=34.0
708  
709 0 ▶ $self->{last_code} ||= -1;
Condition0%0/2
$self->{"last_code"} ||= -1
l!l
00
MC/DC0%0/1
$self->{"last_code"} ||= -1
$self->{"last_code"}
Truth table0%0/2
A
A= $self->{"last_code"}
Aresult
11
00
710 0 ▶ $self->{last_error_string} ||= $msg;
Condition0%0/3
$self->{"last_error_string"} ||= $msg
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"last_error_string"} ||= $msg
$self->{"last_error_string"} $msg
Truth table0%0/3
A ||= B
A= $self->{"last_error_string"}B= $msg
ABresult
1X1
011
000
711  
712 0 delete $self->{fh};
713 0 $self->{wbuf} = '';
714  
715 0 my $wait = delete $self->{wait};
716 0 $self->{wait} = {};
717 0 for (keys %$wait) {
718 0 my $cb = delete $wait->{$_};
719 0 $cb->({ status => 'fatal', errstr => $msg, req_id => $_ }, $raw);
720 }
721  
722 0 ▶ $self->set_error($msg) if $self->state ne 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state ne "error" 0 0 0
723 }
724  
725  
726 sub _check_rbuf {{
727 0 ▶ my ($self) = @_;
Subroutine _check_rbuf: not called CC=6 SCAR=37.4
728 0 ▶ return unless length $self->{rbuf} >= 12;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"rbuf"} >= 12 0 0 0
729 0 my (undef, $blen) = unpack "L$LE L$LE", $self->{rbuf};
730 0 ▶ return unless length $self->{rbuf} >= 12 + $blen;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"rbuf"} >= 12 + $blen 0 0 0
731
732  
733 0 my $pkt = substr $self->{rbuf}, 0, 12 + $blen, '';
734  
735 0 my $res = DR::Tarantool::_pkt_parse_response( $pkt );
736  
737 0 $self->{last_code} = $res->{code};
738 0 ▶ if (exists $res->{errstr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->{'errstr'}) { } 0 0 0
739 0 $self->{last_error_string} = $res->{errstr};
740 } else {
741 0 delete $self->{last_error_string};
742 }
743  
744 0 ▶ if ($res->{status} =~ /^(fatal|buffer)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} =~ /^(fatal|buffer)$/) 0 0 0
745 0 $self->_fatal_error( $res->{errstr}, $pkt );
746 0 return;
747 }
748  
749 0 my $id = $res->{req_id};
750 0 my $cb = delete $self->{ wait }{ $id };
751 0 ▶ if ('CODE' eq ref $cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $cb) { } 0 0 0
752 0 $cb->( $res, $pkt );
753 } else {
754 0 warn "Unexpected reply from tarantool with id = $id";
755 }
756 0 redo;
757 }}
758  
759  
760 sub on_read {
761 0 ▶ my $self = shift;
Subroutine on_read: not called CC=1 SCAR=6.9
Pod undocumented
762 sub {
763 0 ▶ my $rd = sysread $self->fh, my $buf, 4096;
Subroutine __ANON__: not called CC=4 SCAR=30.0
764 0 ▶ unless(defined $rd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rd) 0 0 0
765 0 ▶ return if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
766 0 $self->_fatal_error("Socket error: $!");
767 0 return;
768 }
769  
770 0 ▶ unless($rd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rd) 0 0 0
771 0 $self->_fatal_error("Socket error: Server closed connection");
772 0 return;
773 }
774 0 $self->{rbuf} .= $buf;
775 0 $self->_check_rbuf;
776 }
777 # write responses as binfile for tests
778 # {
779 # my ($type, $blen, $id, $code, $body) =
780 # unpack 'L< L< L< L< A*', $hdr . $data;
781  
782 # my $sname = sprintf 't/test-data/%05d-%03d-%s.bin',
783 # $type || 0, $code, $code ? 'fail' : 'ok';
784 # open my $fh, '>:raw', $sname;
785 # print $fh $hdr;
786 # print $fh $data;
787 # warn "$sname saved (body length: $blen)";
788 # }
789 0 }
790  
791 sub _check_cb {
792 0 ▶ my ($self, $cb) = @_;
Subroutine _check_cb: not called CC=2 SCAR=17.9
793 0 ▶ croak 'Callback must be CODEREF' unless 'CODE' eq ref $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $cb 0 0 0
794 }
795  
796 sub _check_tuple {
797 0 ▶ my ($self, $tuple) = @_;
Subroutine _check_tuple: not called CC=2 SCAR=17.9
798 0 ▶ croak 'Tuple must be ARRAYREF' unless 'ARRAY' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple 0 0 0
799 }
800  
801 sub _check_tuple_list {
802 0 ▶ my ($self, $list) = @_;
Subroutine _check_tuple_list: not called CC=4 SCAR=30.0
803 0 ▶ croak 'Tuplelist must be ARRAYREF of ARRAYREF' unless 'ARRAY' eq ref $list;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $list 0 0 0
804 0 ▶ croak 'Tuplelist is empty' unless @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 0 0 0
805 0 $self->_check_tuple($_) for @$list;
806 }
807  
808 sub _check_number {
809 0 ▶ my ($self, $number) = @_;
Subroutine _check_number: not called CC=3 SCAR=24.8
810 0 ▶ croak "argument must be number"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number and $number =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $number and $number =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $number and $number =~ /^\d+$/
defined $number $number =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $numberB= $number =~ /^\d+$/
ABresult
0X0
100
111
811 unless defined $number and $number =~ /^\d+$/;
812 }
813  
814  
815 sub _check_operation {
816 0 ▶ my ($self, $op) = @_;
Subroutine _check_operation: not called CC=5 SCAR=34.0
817 0 ▶ croak 'Operation must be ARRAYREF' unless 'ARRAY' eq ref $op;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $op 0 0 0
818 0 ▶ croak 'Wrong update operation: too short arglist' unless @$op >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$op >= 2 0 0 0
819 0 ▶ croak "Wrong operation: $op->[1]"
Branch: 1 branch
BranchTrueFalseTotal
unless $op->[1] and $op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/ 0 0 0
Condition0%0/3
$op->[1] and $op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$op->[1] and $op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/
$op->[1] $op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/
Truth table0%0/3
A and B
A= $op->[1]B= $op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/
ABresult
0X0
100
111
820 unless $op->[1] and
821 $op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/;
822 0 $self->_check_number($op->[0]);
823 }
824  
825 sub _check_operations {
826 0 ▶ my ($self, $list) = @_;
Subroutine _check_operations: not called CC=4 SCAR=30.0
827 0 ▶ croak 'Operations list must be ARRAYREF of ARRAYREF'
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $list 0 0 0
828 unless 'ARRAY' eq ref $list;
829 0 ▶ croak 'Operations list is empty' unless @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 0 0 0
830 0 $self->_check_operation( $_ ) for @$list;
831 }
832  
833 =head1 COPYRIGHT AND LICENSE
834  
835 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
836 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
837  
838 This program is free software, you can redistribute it and/or
839 modify it under the terms of the Artistic License.
840  
841 =head1 VCS
842  
843 The project is placed git repo on github:
844 L<https://github.com/dr-co/dr-tarantool/>.
845  
846 =cut
847  
848 5 1;
« blib/lib/DR/Tarantool/RealSyncClient.pm ↑ Summary blib/lib/DR/Tarantool/AsyncClient.pm »