lib/DR/Tnt/FullCb.pm

filter: Statementstmt 17.0% 71 / 416 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 27.2% 15 / 55 Podpod 33.3% 1 / 3 totaltotal 13.2% 87 / 658 SCAR 85.1
CC
85
Cov
12%
request
552.0
_tuples
156.0
__ANON__
42.0
CRAP 4964.7
« blib/lib/DR/Tnt/Msgpack.pm ↑ Summary blib/lib/DR/Tnt/FullCb.pm »
1 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DR::Tnt::FullCb;
6 13 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 require DR::Tnt::LowLevel;
9 13 ▶use File::Spec::Functions 'catfile', 'rel2abs';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 $Carp::Internal{ (__PACKAGE__) }++;
12 13 ▶use DR::Tnt::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 with 'DR::Tnt::Role::Logging';
14 13 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 ▶use constant SPACE_space => 281; # _vspace
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use constant SPACE_index => 289; # _vindex
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use constant ER_TNT_PERMISSIONS => 0x8037;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use constant ER_TNT_SCHEMA => 0x806D;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 13 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 13 enum DriverType => [ 'sync', 'async' ];
26 13 enum FullCbState => [
27 'init',
28 'connecting',
29 'schema',
30 'ready',
31 'pause',
32 ];
33  
34 13 ▶no Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 13 has logger => is => 'ro', isa => 'Maybe[CodeRef]';
37 13 has host => is => 'ro', isa => 'Str', required => 1;
38 13 has port => is => 'ro', isa => 'Str', required => 1;
39 13 has user => is => 'ro', isa => 'Maybe[Str]';
40 13 has password => is => 'ro', isa => 'Maybe[Str]';
41 13 has driver => is => 'ro', isa => 'DriverType', required => 1;
42 13 has reconnect_interval => is => 'ro', isa => 'Maybe[Num]';
43 13 has hashify_tuples => is => 'ro', isa => 'Bool', default => 0;
44 13 has utf8 => is => 'ro', isa => 'Bool', default => 1;
45 13 has lua_dir =>
46 is => 'ro',
47 isa => 'Maybe[Str]',
48 writer => '_set_lua_dir'
49 ;
50 13 has last_error =>
51 is => 'ro',
52 isa => 'Maybe[ArrayRef]',
53 writer => '_set_last_error'
54 ;
55 has state =>
56 is => 'ro',
57 isa => 'FullCbState',
58 default => 'init',
59 writer => '_set_state',
60 trigger => sub {
61 0 ▶ my ($self, undef, $old_state) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 $self->_state_changed($self->_now);
63  
64 0 $self->_reconnector->event($self->state, $old_state);
65 0 $self->_log(info => 'Connector is in state: %s', $self->state);
66 13 };
67 ;
68  
69 13 has _state_changed => is => 'rw', isa => 'Maybe[Num]';
70  
71  
72 13 has last_schema =>
73 is => 'ro',
74 isa => 'Int',
75 default => 0,
76 writer => '_set_last_schema'
77 ;
78  
79  
80 has _reconnector =>
81 is => 'ro',
82 isa => 'Object',
83 lazy => 1,
84 builder => sub {
85 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
86  
87 0 goto $self->driver;
88  
89 0 sync:
90 require DR::Tnt::FullCb::Reconnector::Sync;
91 0 return DR::Tnt::FullCb::Reconnector::Sync->new(fcb => $self);
92  
93 0 async:
94 require DR::Tnt::FullCb::Reconnector::AE;
95 0 return DR::Tnt::FullCb::Reconnector::AE->new(fcb => $self);
96  
97 }
98 13 ;
99  
100  
101  
102 0 ▶has _unsent_lua => is => 'rw', isa => 'ArrayRef', default => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
103  
104 sub _preeval_lua {
105 0 ▶ my ($self, $cb) = @_;
Subroutine _preeval_lua: not called CC=2 SCAR=17.9
106  
107 0 $self->_unsent_lua([]);
108  
109 0 ▶ if ($self->lua_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lua_dir) 0 0 0
110 0 my @lua = sort glob catfile $self->lua_dir, '*.lua';
111 0 $self->_unsent_lua(\@lua);
112 }
113  
114 0 $self->_preeval_unsent_lua($cb);
115 0 return;
116 }
117  
118 sub _preeval_unsent_lua {
119 0 ▶ my ($self, $cb) = @_;
Subroutine _preeval_unsent_lua: not called CC=3 SCAR=24.8
120  
121 0 ▶ unless (@{ $self->_unsent_lua }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->_unsent_lua;}) 0 0 0
122 0 $self->_invalid_schema($cb);
123 0 return;
124 }
125  
126 0 my $lua = shift @{ $self->_unsent_lua };
127  
128 0 $self->_log(debug => 'Eval "%s" after connection', $lua);
129  
130 0 ▶ if (open my $fh, '<:raw', $lua) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<:raw', $lua) { } 0 0 0
131 0 local $/;
132 0 my $body = <$fh>;
133 $self->_reconnector->ll->send_request(eval_lua => undef, $body, sub {
134 0 ▶ my ($state, $message, $sync) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
135 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
136 0 $self->_set_last_error([ $state, $message ]);
137 0 $self->_set_state('pause');
138 0 $cb->($state => $message);
139 0 return;
140 }
141  
142 $self->_reconnector->ll->wait_response($sync, sub {
143 0 ▶ my ($state, $message, $resp) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
144 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
145 0 $self->_set_last_error([ $state, $message ]);
146 0 $self->_set_state('pause');
147 0 $cb->($state => $message);
148 0 return;
149 }
150 0 ▶ unless ($resp->{CODE} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"CODE"} == 0) 0 0 0
151 $cb->(ER_INIT_LUA =>
152 sprintf "lua (%s) error: %s",
153 0 ▶ $lua, $resp->{ERROR} // 'Unknown error'
Condition0%0/2
$resp->{"ERROR"} // "Unknown error"
l!l
00
MC/DC0%0/1
$resp->{"ERROR"} // "Unknown error"
$resp->{"ERROR"}
Truth table0%0/2
A
A= $resp->{"ERROR"}
Aresult
11
00
154 );
155 0 return;
156 }
157 0 $self->_preeval_unsent_lua($cb);
158 0 });
159 0 });
160  
161 } else {
162 0 $self->_set_last_error(ER_OPEN_FILE => "$lua: $!");
163 0 $self->_set_state('pause');
164 0 $cb->(@{ $self->last_error });
165 0 return;
166 }
167 }
168  
169  
170 13 has _sch => is => 'rw', isa => 'HashRef';
171 0 ▶has _spaces => is => 'rw', isa => 'ArrayRef', default => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
172 0 ▶has _indexes => is => 'rw', isa => 'ArrayRef', default => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
173  
174 0 ▶has _wait_ready => is => 'rw', isa => 'ArrayRef', default => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
175  
176 sub _invalid_schema {
177 0 ▶ my ($self, $cb) = @_;
Subroutine _invalid_schema: not called CC=1 SCAR=6.9
178  
179 0 goto $self->state;
180  
181 0 init:
182 pause:
183 0 confess "Internal error: _invalid_schema in state " . $self->state;
184  
185 0 schema:
186 connecting:
187 ready:
188 0 $self->_set_state('schema');
189 $self->_reconnector->ll->send_request(select =>
190 undef, SPACE_space, 0, [], undef, undef, 'ALL', sub {
191 0 ▶ my ($state, $message, $sync) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
192 0 $self->_log(debug => 'Loading spaces');
193 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
194 0 $self->_set_last_error([ $state, $message ]);
195 0 $self->_set_state('pause');
196 0 $cb->($state => $message);
197 0 return;
198 }
199  
200 $self->_reconnector->ll->wait_response($sync, sub {
201 0 ▶ my ($state, $message, $resp) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
202 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
203 0 $self->_set_last_error([ $state, $message ]);
204 0 $self->_set_state('pause');
205 0 $cb->($state => $message);
206 0 return;
207 }
208  
209  
210 # have no permissions
211 0 ▶ if ($resp->{CODE} == ER_TNT_PERMISSIONS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($resp->{'CODE'} == 32823) { } 0 0 0
elsif ($resp->{'CODE'}) { } 0 0 0
212 0 $self->_spaces([]);
213 } elsif ($resp->{CODE}) {
214 $self->_set_last_error([ ER_REQUEST =>
215 0 'Can not load tarantool schema', $resp->{CODE} ]);
216 0 $self->_set_state('pause');
217 0 $cb->(@{ $self->last_error });
218 0 return;
219 } else {
220 0 $self->_spaces($resp->{DATA});
221 }
222  
223 0 $self->_log(debug => 'Loading indexes');
224 $self->_reconnector->ll->send_request(select =>
225 $resp->{SCHEMA_ID}, SPACE_index, 0, [], undef, undef, 'ALL', sub {
226  
227 0 ▶ my ($state, $message, $sync) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
228 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
229 0 $self->_set_last_error([ $state, $message ]);
230 0 $self->_set_state('pause');
231 0 $cb->($state => $message);
232 0 return;
233 }
234  
235  
236 $self->_reconnector->ll->wait_response($sync, sub {
237 0 ▶ my ($state, $message, $resp) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
238 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
239 0 $self->_set_last_error([ $state, $message ]);
240 0 $self->_set_state('pause');
241 0 $cb->($state => $message);
242 0 return;
243 }
244  
245 0 ▶ if ($resp->{CODE} == ER_TNT_PERMISSIONS) {
Branch: 3 branches
BranchTrueFalseTotal
if ($resp->{'CODE'} == 32823) { } 0 0 0
elsif ($resp->{'CODE'} == 32877) { } 0 0 0
elsif ($resp->{'CODE'}) { } 0 0 0
246 0 $self->_indexes([]);
247 } elsif ($resp->{CODE} == ER_TNT_SCHEMA) {
248 # collision again!
249 0 $self->_invalid_schema($cb);
250 0 return;
251  
252 } elsif ($resp->{CODE}) {
253 $self->_set_last_error([ ER_REQUEST =>
254 0 'Can not load tarantool schema', $resp->{CODE} ]);
255 0 $self->_set_state('pause');
256 0 $cb->(@{ $self->last_error });
257 0 return;
258  
259 } else {
260 0 $self->_indexes($resp->{DATA});
261 }
262  
263 0 $self->_set_schema($resp->{SCHEMA_ID});
264 0 $self->_set_state('ready');
265  
266 0 $cb->('OK', 'Connected, schema loaded');
267 0 $self->request;
268 0 });
269 0 });
270 0 });
271 0 });
272 }
273  
274 sub _set_schema {
275 0 ▶ my ($self, $schema_id) = @_;
Subroutine _set_schema: not called CC=5 SCAR=34.0
276  
277 0 my %sch;
278  
279 0 for (@{ $self->_spaces }) {
280 0 my $space = $sch{ $_->[0] } = $sch{ $_->[2] } = {
281 id => $_->[0],
282 name => $_->[2],
283 engine => $_->[3],
284 flags => $_->[5],
285 fields => $_->[6],
286 indexes => { }
287 };
288  
289 0 for (@{ $self->_indexes }) {
290 0 ▶ next unless $_->[0] == $space->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[0] == $space->{"id"} 0 0 0
291  
292 $space->{indexes}{ $_->[2] } =
293 $space->{indexes}{ $_->[1] } = {
294 id => $_->[1],
295 name => $_->[2],
296 type => $_->[3],
297 flags => $_->[4],
298 fields => [
299 map {
300 'HASH' eq ref $_ ?
301 { type => $_->{type}, no => $_->{field} }
302 0 ▶ : { type => $_->[1], no => $_->[0] }
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_ ? : 0 0 0
303 }
304
305 0 @{ $_->[5] }
306 ]
307 }
308 }
309 }
310  
311 0 $self->_set_last_schema($schema_id);
312 0 $self->_sch(\%sch);
313 0 $self->_indexes([]);
314 0 $self->_spaces([]);
315 }
316  
317 sub _tuples {
318 0 ▶ my ($self, $resp, $space, $cb) = @_;
Subroutine _tuples: not called CC=12 SCAR=50.5
319  
320  
321 0 ▶ unless (defined $space) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $space) 0 0 0
322 0 ▶ $cb->(OK => 'Response received', $resp->{DATA} // []);
Condition0%0/2
$resp->{"DATA"} // []
l!l
00
MC/DC0%0/1
$resp->{"DATA"} // []
$resp->{"DATA"}
Truth table0%0/2
A
A= $resp->{"DATA"}
Aresult
11
00
323 0 return;
324 }
325  
326 0 ▶ unless (exists $self->_sch->{ $space }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_sch->{$space}) 0 0 0
327 0 ▶ $cb->(OK => "Space $space not exists in schema", $resp->{DATA} // []);
Condition0%0/2
$resp->{"DATA"} // []
l!l
00
MC/DC0%0/1
$resp->{"DATA"} // []
$resp->{"DATA"}
Truth table0%0/2
A
A= $resp->{"DATA"}
Aresult
11
00
328 0 return;
329 }
330  
331 0 ▶ my $res = $resp->{DATA} // [];
Condition0%0/2
$resp->{"DATA"} // []
l!l
00
MC/DC0%0/1
$resp->{"DATA"} // []
$resp->{"DATA"}
Truth table0%0/2
A
A= $resp->{"DATA"}
Aresult
11
00
332 0 $space = $self->_sch->{ $space };
333  
334 0 ▶ if ($self->hashify_tuples) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hashify_tuples) 0 0 0
335 0 for my $tuple (@$res) {
336 0 ▶ next unless 'ARRAY' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple 0 0 0
337 0 my %t;
338  
339 0 for (0 .. $#{ $space->{fields} }) {
340 0 ▶ my $fname = $space->{fields}[$_]{name} // sprintf "field:%02X", $_;
Condition0%0/3
$space->{"fields"}[$_]{"name"} // sprintf("field:%02X", $_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$space->{"fields"}[$_]{"name"} // sprintf("field:%02X", $_)
$space->{"fields"}[$_]{"name"} sprintf("field:%02X", $_)
Truth table0%0/3
A // B
A= $space->{"fields"}[$_]{"name"}B= sprintf("field:%02X", $_)
ABresult
1X1
011
000
341 0 $t{$fname} = $tuple->[$_];
342 }
343  
344 0 ▶ if (@{ $space->{fields} } < @$tuple) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$space->{'fields'};} < @$tuple) { } 0 0 0
345 0 $t{tail} = [ splice @$tuple, scalar @{ $space->{fields} } ];
346 } else {
347 0 $t{tail} = [];
348 }
349 0 $tuple = \%t;
350 }
351 }
352  
353 0 $cb->(OK => 'Response received', $res);
354 }
355  
356 sub restart {
357 0 ▶ my ($self, $cbc) = @_;
Subroutine restart: not called CC=2 SCAR=17.9
Pod undocumented
358  
359 0 ▶ $cbc ||= sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$cbc ||= sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$cbc ||= sub { }
$cbc sub { }
Truth table0%0/3
A ||= B
A= $cbcB= sub { }
ABresult
1X1
011
000
360  
361 0 $self->_log(info => 'Starting connection to %s:%s (driver: %s)',
362 $self->host, $self->port, $self->driver);
363  
364 0 goto $self->state;
365  
366 0 init:
367 connecting:
368 schema:
369 pause:
370 ready:
371 0 $self->_set_state('connecting');
372 $self->_reconnector->ll->connect(sub {
373 0 ▶ my ($state, $message) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
374 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
375 0 $self->_set_last_error([ $state, $message ]);
376 0 $self->_set_state('pause');
377 0 $cbc->(@{ $self->last_error });
378 0 return;
379 }
380  
381 $self->_reconnector->ll->handshake(sub {
382 0 ▶ my ($state, $message) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
383 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
384 0 $self->_set_last_error([ $state, $message ]);
385 0 $self->_set_state('pause');
386 0 $cbc->(@{ $self->last_error });
387 0 return;
388 }
389  
390 0 ▶ unless ($self->user and $self->password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->user and $self->password) 0 0 0
Condition0%0/3
$self->user and $self->password
!ll&&!rl&&r
000
MC/DC0%0/2
$self->user and $self->password
$self->user $self->password
Truth table0%0/3
A and B
A= $self->userB= $self->password
ABresult
0X0
100
111
391 0 return $self->_preeval_lua($cbc);
392 }
393  
394 $self->_reconnector->ll->send_request(auth => undef, sub {
395 0 ▶ my ($state, $message, $sync) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
396 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
397 0 $self->_set_last_error([ $state, $message ]);
398 0 $self->_set_state('pause');
399 0 $cbc->(@{ $self->last_error });
400 0 return;
401 }
402  
403 $self->_reconnector->ll->wait_response($sync, sub {
404 0 ▶ my ($state, $message, $resp) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
405 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
406 0 $self->_set_last_error([ $state, $message ]);
407 0 $self->_set_state('pause');
408 0 $cbc->(@{ $self->last_error });
409 0 return;
410 }
411  
412 0 ▶ unless ($resp->{CODE} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"CODE"} == 0) 0 0 0
413 0 $self->_log(warning => 'Can not auth: Wrong login or password');
414 $self->_set_last_error([ ER_BROKEN_PASSWORD =>
415 0 ▶ $resp->{ERROR} // 'Wrong password']
Condition0%0/2
$resp->{"ERROR"} // "Wrong password"
l!l
00
MC/DC0%0/1
$resp->{"ERROR"} // "Wrong password"
$resp->{"ERROR"}
Truth table0%0/2
A
A= $resp->{"ERROR"}
Aresult
11
00
416 );
417 0 $self->_set_state('pause');
418 0 $cbc->(@{ $self->last_error });
419 0 return;
420 }
421 0 $self->_preeval_lua($cbc);
422 0 });
423 0 });
424 0 });
425 0 });
426 }
427  
428 sub request {
429 0 ▶ my $self = shift;
Subroutine request: not called CC=23 SCAR=63.1
Pod undocumented
430  
431 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
432  
433 0 ▶ unless ('CODE' eq ref $_[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("CODE" eq ref $_[-1]) 0 0 0
434 0 croak 'usage: $connector->request(..., $CALLBACK)';
435 }
436 state $check = {
437 get => sub {
438 0 ▶ croak 'usage: $connector->get(space, index, key)'
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 5 0 0 0
439 unless @_ == 5;
440 },
441 select => sub {
442 0 ▶ croak 'usage: $connector->select(space, index, key[, limit, offset, iterator])'
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 5 and @_ <= 8 0 0 0
Condition0%0/3
@_ >= 5 and @_ <= 8
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 5 and @_ <= 8
@_ >= 5 @_ <= 8
Truth table0%0/3
A and B
A= @_ >= 5B= @_ <= 8
ABresult
0X0
100
111
443 unless @_ >= 5 and @_ <= 8;
444 },
445 ▶ update => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
446 insert => sub {
447 0 ▶ croak 'usage: $connector->insert(space, tuple)'
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 and "ARRAY" eq ref $_[2] 0 0 0
Condition0%0/3
@_ == 4 and "ARRAY" eq ref $_[2]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 4 and "ARRAY" eq ref $_[2]
@_ == 4 "ARRAY" eq ref $_[2]
Truth table0%0/3
A and B
A= @_ == 4B= "ARRAY" eq ref $_[2]
ABresult
0X0
100
111
448 unless @_ == 4 and 'ARRAY' eq ref $_[2];
449 },
450 replace => sub {
451 0 ▶ croak 'usage: $connector->replace(space, tuple)'
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 and "ARRAY" eq ref $_[2] 0 0 0
Condition0%0/3
@_ == 4 and "ARRAY" eq ref $_[2]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 4 and "ARRAY" eq ref $_[2]
@_ == 4 "ARRAY" eq ref $_[2]
Truth table0%0/3
A and B
A= @_ == 4B= "ARRAY" eq ref $_[2]
ABresult
0X0
100
111
452 unless @_ == 4 and 'ARRAY' eq ref $_[2];
453 },
454 delete => sub {
455 0 ▶ croak 'usage: $connector->delete(space, key)'
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 0 0
456 unless @_ == 4;
457 },
458 call_lua => sub {
459 0 ▶ croak 'usage: $connector->call_lua(name[, args])'
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
460 unless @_ >= 3;
461 },
462 eval_lua => sub {
463 0 ▶ croak 'usage: $connector->eval_lua(code[, args])'
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
464 unless @_ >= 3;
465 },
466 ▶ ping => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
467 },
468 auth => sub {
469 0 ▶ croak 'usage: $connector->auth([user, password])'
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 or @_ == 5 0 0 0
Condition0%0/3
@_ == 3 or @_ == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 3 or @_ == 5
@_ == 3 @_ == 5
Truth table0%0/3
A or B
A= @_ == 3B= @_ == 5
ABresult
1X1
011
000
470 unless @_ == 3 or @_ == 5;
471 },
472 0 };
473  
474 0 ▶ unless (exists $check->{ $_[0] // 'undef' }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$check{$_[0] // "undef"}) 0 0 0
Condition0%0/2
$_[0] // "undef"
l!l
00
MC/DC0%0/1
$_[0] // "undef"
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
475 0 ▶ croak 'unknown request method: ' . $_[0] // 'undef';
Condition0%0/2
'unknown request method: ' . $_[0] // "undef"
l!l
00
MC/DC0%0/1
'unknown request method: ' . $_[0] // "undef"
'unknown request method: ' . $_[0]
Truth table0%0/2
A
A= 'unknown request method: ' . $_[0]
Aresult
11
00
476 }
477  
478 0 $check->{ $_[0] }(@_);
479  
480 0 push @{ $self->_wait_ready } => \@_;
481 }
482  
483 restart:
484 0 goto $self->state;
485  
486  
487 0 init:
488 $self->_log(info => 'Autoconnect before first request');
489  
490 reinit:
491 $self->restart(sub {
492 0 ▶ return if $self->state eq 'ready';
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "ready" 0 0 0
493 0 ▶ unless (defined $self->reconnect_interval) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->reconnect_interval) 0 0 0
494 0 my $list = $self->_wait_ready;
495 0 $self->_wait_ready([]);
496 0 for (@$list) {
497 0 $_->[-1](@{ $self->last_error });
498 }
499 0 return;
500 }
501 0 });
502  
503 0 ▶ if ($self->driver eq 'sync') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->driver eq "sync") 0 0 0
504 0 ▶ goto ready if $self->state eq 'ready';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "ready" 0 0 0
505 0 goto sync_redo_check;
506 }
507  
508 0 return;
509
510 0 ▶ sync_redo_check:
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reconnect_interval 0 0 0
511 goto no_reconnect_errors unless defined $self->reconnect_interval;
512 0 Time::HiRes::sleep($self->reconnect_interval);
513 0 goto reinit;
514  
515 0 schema:
516 connecting:
517 0 return;
518  
519 pause:
520 0 ▶ if ($self->driver eq 'sync') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->driver eq "sync") 0 0 0
521 0 ▶ goto no_reconnect_errors unless defined $self->reconnect_interval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reconnect_interval 0 0 0
522 0 my $pause = $self->reconnect_interval -
523 ($self->_now - $self->_state_changed);
524 0 ▶ Time::HiRes::sleep($pause) if $pause > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pause > 0 0 0 0
525 0 goto reinit;
526 }
527 0 ▶ goto no_reconnect_errors unless defined $self->reconnect_interval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reconnect_interval 0 0 0
528 0 return;
529 no_reconnect_errors: {
530 0 my $list = $self->_wait_ready;
531 0 $self->_wait_ready([]);
532 0 for (@$list) {
533 0 $_->[-1](@{ $self->last_error });
534 }
535 return
536 0 }
537  
538 ready:
539 0 ▶ while (my $request = shift @{ $self->_wait_ready }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $request = shift @{$self->_wait_ready;}) 0 0 0
540  
541 0 ▶ unless ($self->state eq 'ready') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state eq "ready") 0 0 0
542 0 unshift @{ $self->_wait_ready } => $request;
543 0 goto restart;
544 };
545  
546 0 my @args = @$request;
547 0 my $name = shift @args;
548 0 my $cb = pop @args;
549  
550 0 my ($space, $index);
551 0 state $space_pos = {
552 select => 'index',
553 update => 'normal',
554 insert => 'normal',
555 replace => 'normal',
556 delete => 'normal',
557 call_lua => 'mayberef',
558 eval_lua => 'mayberef',
559 ping => 'none',
560 auth => 'none',
561 };
562  
563 0 ▶ croak "unknown method $name" unless exists $space_pos->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $space_pos->{$name} 0 0 0
564  
565 0 goto $space_pos->{$name};
566  
567 0 index:
568 $space = $args[0];
569 0 ▶ unless (exists $self->_sch->{ $space }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_sch->{$space}) 0 0 0
570 0 $self->_set_last_error([ER_NOSPACE => "Space $space not found"]);
571 0 $cb->(@{ $self->last_error });
572 0 next;
573 }
574 0 $args[0] = $self->_sch->{ $space }{id};
575  
576 0 $index = $args[1];
577 0 ▶ unless (exists $self->_sch->{ $space }{indexes}{ $index }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_sch->{$space}{"indexes"}{$index}) 0 0 0
578 0 $self->_set_last_error(
579 [ER_NOINDEX => "Index space[$space].$index not found"]
580 );
581 0 $cb->(@{ $self->last_error });
582 0 next;
583 }
584  
585 0 $index = $args[1] = $self->_sch->{$space}{indexes}{ $index }{id};
586 0 goto do_request;
587  
588 0 normal:
589 $space = $args[0];
590 0 ▶ unless (exists $self->_sch->{ $space }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_sch->{$space}) 0 0 0
591 0 $self->_set_last_error([ER_NOSPACE => "Space $space not found"]);
592 0 $cb->(@{ $self->last_error });
593 0 next;
594 }
595 0 $space = $args[0] = $self->_sch->{ $space }{id};
596 0 goto do_request;
597  
598 mayberef:
599 0 ▶ if ('ARRAY' eq ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $args[0]) 0 0 0
600 0 ($args[0], $space) = @{ $args[0] };
601 }
602 0 ▶ goto do_request unless defined $space;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $space 0 0 0
603 0 ▶ unless (exists $self->_sch->{ $space }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_sch->{$space}) 0 0 0
604 0 $self->_set_last_error([ER_NOSPACE => "Space $space not found"]);
605 0 $cb->(@{ $self->last_error });
606 0 next;
607 }
608 0 $space = $self->_sch->{ $space }{id};
609  
610  
611 none:
612  
613 do_request:
614  
615 $self->_reconnector->ll->send_request($name, $self->last_schema,
616 @args, sub {
617 0 ▶ my ($state, $message, $sync) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
618 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
619 0 $self->_set_last_error([ $state => $message ]);
620 0 $self->_set_state('pause');
621 0 $cb->(@{ $self->last_error });
622 0 return;
623 }
624  
625 $self->_reconnector->ll->wait_response($sync, sub {
626 0 ▶ my ($state, $message, $resp) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
627 0 ▶ unless ($state eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state eq "OK") 0 0 0
628 0 $self->_set_last_error([ $state => $message ]);
629 0 $self->_set_state('pause');
630 0 $cb->(@{ $self->last_error });
631 0 return;
632 }
633  
634 # schema collision
635 0 ▶ if ($resp->{CODE} == ER_TNT_SCHEMA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"CODE"} == 32877) 0 0 0
636 0 $self->_log(warning => 'Detected schema collision');
637 0 $self->_log(info => 'Defer request "%s" until schema loaded', $name);
638 0 unshift @{ $self->_wait_ready } => $request;
639 0 ▶ $self->_invalid_schema(sub {}) if $self->state eq 'ready';
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "ready" 0 0 0
640 0 return;
641 }
642  
643 0 ▶ unless ($resp->{CODE} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"CODE"} == 0) 0 0 0
644 $self->_set_last_error(
645 0 [ ER_REQUEST => $resp->{ERROR}, $resp->{CODE} ]
646 );
647 0 $cb->(@{ $self->last_error });
648 0 return;
649 }
650  
651 0 ▶ if ($resp->{SCHEMA_ID} != $self->last_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"SCHEMA_ID"} != $self->last_schema) 0 0 0
652 0 $self->_log(info => 'request was changed schema id');
653 }
654 0 $self->_set_last_error(undef);
655 0 $self->_tuples($resp, $space, $cb);
656 0 });
657 0 });
658 }
659 0 return;
660  
661 }
662  
663 sub _now {
664 0 ▶ my ($self) = @_;
Subroutine _now: not called CC=2 SCAR=17.9
665 0 ▶ return Time::HiRes::time() if $self->driver eq 'sync';
Branch: 1 branch
BranchTrueFalseTotal
if $self->driver eq "sync" 0 0 0
666 0 return AnyEvent->now();
667 }
668  
669 sub BUILD {
670 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
671 0 goto $self->driver;
672  
673 0 sync:
674 require Time::HiRes;
675 0 return;
676 0 async:
677 require AnyEvent;
678 0 return;
679 }
680  
681 13 __PACKAGE__->meta->make_immutable;
682  
« blib/lib/DR/Tnt/Msgpack.pm ↑ Summary blib/lib/DR/Tnt/FullCb.pm »