blib/lib/DR/Tarantool/Spaces.pm

filter: Statementstmt 94.0% 270 / 287 Branchbran 69.8% 141 / 202 Conditioncond 48.1% 26 / 54 MC/DCmcdc 20.5% 7 / 34 Subroutinesub 97.3% 36 / 37 Podpod 87.5% 7 / 8 totaltotal 78.3% 487 / 622 CC 130 SCAR 30.5
CC
130
Cov
80%
new
66.3
pack_field
21.7
pack_operation
18.1
CRAP 21.2
« blib/lib/DR/Tarantool/AsyncClient.pm ↑ Summary blib/lib/DR/Tarantool/SyncClient.pm »
1 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DR::Tarantool::Spaces;
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 $Carp::Internal{ (__PACKAGE__) }++;
8  
9 7 ▶my $LE = $] > 5.01 ? '<' : '';
Branch: 1 branch
BranchTrueFalseTotal
$] > 5.01 ? : 7 0 7
10  
11 =head1 NAME
12  
13 DR::Tarantool::Spaces - Tarantool schema description
14  
15 =head1 SYNOPSIS
16  
17 use DR::Tarantool::Spaces;
18 my $s = new DR::Tarantool::Spaces({
19 1 => {
20 name => 'users', # space name
21 default_type => 'STR', # undescribed fields
22 fields => [
23 qw(login password role),
24 {
25 name => 'counter',
26 type => 'NUM'
27 },
28 {
29 name => 'something',
30 type => 'UTF8STR',
31 },
32 {
33 name => 'opts',
34 type => 'JSON',
35 }
36 ],
37 indexes => {
38 0 => 'login',
39 1 => [ qw(login password) ],
40 2 => {
41 name => 'my_idx',
42 fields => 'login',
43 },
44 3 => {
45 name => 'my_idx2',
46 fields => [ 'counter', 'something' ]
47 }
48 }
49 },
50  
51 0 => {
52 ...
53 }
54 });
55  
56 my $f = $s->pack_field('users', 'counter', 10);
57 my $f = $s->pack_field('users', 3, 10); # the same
58 my $f = $s->pack_field(1, 3, 10); # the same
59  
60 my $ts = $s->pack_keys([1,2,3] => 'my_idx');
61 my $t = $s->pack_primary_key([1,2,3]);
62  
63  
64 =head1 DESCRIPTION
65  
66 The package describes all spaces used in an application.
67 It supports the following field types:
68  
69 =over
70  
71 =item NUM, NUM64, STR
72  
73 The standard L<Tarantool|http://tarantool.org> types.
74  
75 =item UTF8STR
76  
77 The same as B<STR>, but the string is utf8-decoded
78 after it's received from the server.
79  
80 =item INT & INT64
81  
82 The same as B<NUM> and B<NUM64>, but contain signed values.
83  
84 =item JSON
85  
86 The field is encoded with L<JSON::XS> when putting
87 into a database, and decoded after is received back
88 from the server.
89  
90 =back
91  
92 =head1 METHODS
93  
94 =head2 new
95  
96 my $spaces = DR::Tarantool::Spaces->new( $spaces );
97  
98 =cut
99  
100 sub new {
101 5 ▶ my ($class, $spaces, %opts) = @_;
Subroutine new: called CC=6 SCAR=18.2
Pod documented
102  
103 5 ▶ $opts{family} ||= 1;
Condition50%1/2
$opts{"family"} ||= 1
l!l
05
MC/DC0%0/1
$opts{"family"} ||= 1
$opts{"family"}
Truth table50%1/2
A
A= $opts{"family"}
Aresult
11
00
104  
105 5 ▶ $spaces = {} unless defined $spaces;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spaces 1 4 5
106 5 ▶ croak 'spaces must be a HASHREF' unless 'HASH' eq ref $spaces;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $spaces 1 4 5
107  
108 4 my (%spaces, %fast);
109 4 for (keys %$spaces) {
110 4 my $s = new DR::Tarantool::Space($_ => $spaces->{ $_ }, %opts);
111 3 $spaces{ $s->name } = $s;
112 3 $fast{ $_ } = $s->name;
113 }
114  
115 return bless {
116 spaces => \%spaces,
117 fast => \%fast,
118 family => $opts{family},
119 3 ▶ } => ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
120 }
121  
122  
123 sub family {
124 3 ▶ my ($self, $family) = @_;
Subroutine family: called CC=3 SCAR=11.0
Pod undocumented
125 3 ▶ return $self->{family} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1 2 3
126 2 $self->{family} = $family;
127 2 $_->family($family) for values %{ $self->{spaces} };
128 2 return $self->{family};
129 }
130  
131  
132 =head2 space
133  
134 Return space object by number or name.
135  
136 my $space = $spaces->space('name');
137 my $space = $spaces->space(0);
138  
139 =cut
140  
141 sub space {
142 94 ▶ my ($self, $space) = @_;
Subroutine space: called CC=5 SCAR=16.2
Pod documented
143 94 ▶ croak 'space name or number is not defined' unless defined $space;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $space 1 93 94
144 93 ▶ if ($space =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space =~ /^\d+$/) 6 87 93
145 croak "space '$space' is not defined"
146 6 ▶ unless exists $self->{fast}{$space};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fast"}{$space} 0 6 6
147 6 return $self->{spaces}{ $self->{fast}{$space} };
148 }
149 croak "space '$space' is not defined"
150 87 ▶ unless exists $self->{spaces}{$space};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"spaces"}{$space} 0 87 87
151 87 return $self->{spaces}{$space};
152 }
153  
154  
155 =head2 space_number
156  
157 Return space number by its name.
158  
159 =cut
160  
161 sub space_number {
162 3 ▶ my ($self, $space) = @_;
Subroutine space_number: called CC=1 SCAR=0.0
Pod documented
163 3 return $self->space($space)->number;
164 }
165  
166  
167 =head2 pack_field
168  
169 Packs one field into a format suitable for making a database request:
170  
171 my $field = $spaces->pack_field('space', 'field', $data);
172  
173 =cut
174  
175 sub pack_field {
176 23 ▶ my ($self, $space, $field, $value) = @_;
Subroutine pack_field: called CC=2 SCAR=7.1
Pod documented
177 23 ▶ croak q{Usage: $spaces->pack_field('space', 'field', $value)}
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 23 23
178 unless @_ == 4;
179 23 return $self->space($space)->pack_field($field => $value);
180 }
181  
182  
183 =head2 unpack_field
184  
185 Unpack one field after getting it from the server:
186  
187 my $field = $spaces->unpack_field('space', 'field', $data);
188  
189 =cut
190  
191 sub unpack_field {
192 19 ▶ my ($self, $space, $field, $value) = @_;
Subroutine unpack_field: called CC=2 SCAR=7.1
Pod documented
193 19 ▶ croak q{Usage: $spaces->unpack_field('space', 'field', $value)}
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 19 19
194 unless @_ == 4;
195  
196 19 return $self->space($space)->unpack_field($field => $value);
197 }
198  
199  
200 =head2 pack_tuple
201  
202 Pack a tuple before making database request.
203  
204 my $t = $spaces->pack_tuple('space', [ 1, 2, 3 ]);
205  
206 =cut
207  
208 sub pack_tuple {
209 1 ▶ my ($self, $space, $tuple) = @_;
Subroutine pack_tuple: called CC=2 SCAR=7.1
Pod documented
210 1 ▶ croak q{Usage: $spaces->pack_tuple('space', $tuple)} unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 1 1
211 1 return $self->space($space)->pack_tuple( $tuple );
212 }
213  
214  
215 =head2 unpack_tuple
216  
217 Unpack a tuple after getting it from the database:
218  
219 my $t = $spaces->unpack_tuple('space', \@fields);
220  
221 =cut
222  
223 sub unpack_tuple {
224 1 ▶ my ($self, $space, $tuple) = @_;
Subroutine unpack_tuple: called CC=2 SCAR=7.1
Pod documented
225 1 ▶ croak q{Usage: $spaces->unpack_tuple('space', $tuple)} unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 1 1
226 1 return $self->space($space)->unpack_tuple( $tuple );
227 }
228  
229 package DR::Tarantool::Space;
230 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 7 $Carp::Internal{ (__PACKAGE__) }++;
232 7 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
233 7 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
234  
235  
236 =head1 SPACES methods
237  
238 =head2 new
239  
240 constructor
241  
242 use DR::Tarantool::Spaces;
243 my $space = DR::Tarantool::Space->new($no, $space);
244  
245 =cut
246  
247 sub new {
248 4 ▶ my ($class, $no, $space, %opts) = @_;
Subroutine new: called CC=34 SCAR=41.9
249  
250 4 ▶ $opts{family} ||= 1;
Condition50%1/2
$opts{"family"} ||= 1
l!l
40
MC/DC0%0/1
$opts{"family"} ||= 1
$opts{"family"}
Truth table50%1/2
A
A= $opts{"family"}
Aresult
11
00
251 4 ▶ croak 'space number must conform the regexp qr{^\d+}'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no and $no =~ /^\d+$/ 1 3 4
Condition66%2/3
defined $no and $no =~ /^\d+$/
!ll&&!rl&&r
013
MC/DC50%1/2
defined $no and $no =~ /^\d+$/
defined $no $no =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $noB= $no =~ /^\d+$/
ABresult
0X0
100
111
252 unless defined $no and $no =~ /^\d+$/;
253 croak "'fields' not defined in space hash"
254 3 ▶ unless 'ARRAY' eq ref $space->{fields};
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $space->{"fields"} 0 3 3
255 croak "wrong 'indexes' hash"
256 3 ▶ if !$space->{indexes} or 'HASH' ne ref $space->{indexes};
Branch: 1 branch
BranchTrueFalseTotal
if not $space->{"indexes"} or "HASH" ne ref $space->{"indexes"} 0 3 3
Condition33%1/3
not $space->{"indexes"} or "HASH" ne ref $space->{"indexes"}
l!l&&r!l&&!r
003
MC/DC0%0/2
not $space->{"indexes"} or "HASH" ne ref $space->{"indexes"}
not $space->{"indexes"} "HASH" ne ref $space->{"indexes"}
Truth table33%1/3
not(A) or B
A= not $space->{"indexes"}B= "HASH" ne ref $space->{"indexes"}
ABresult
1X1
011
000
257  
258 3 my $name = $space->{name};
259 3 ▶ croak 'wrong space name: ' . (defined($name) ? $name : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless $name and $name =~ /^[a-z_]\w*$/i 0 3 3
defined $name ? : 0 0 0
Condition33%1/3
$name and $name =~ /^[a-z_]\w*$/i
!ll&&!rl&&r
003
MC/DC0%0/2
$name and $name =~ /^[a-z_]\w*$/i
$name $name =~ /^[a-z_]\w*$/i
Truth table33%1/3
A and B
A= $nameB= $name =~ /^[a-z_]\w*$/i
ABresult
0X0
100
111
260 unless $name and $name =~ /^[a-z_]\w*$/i;
261  
262  
263 3 my $fqr = qr{^(?:STR|NUM|NUM64|INT|INT64|UTF8STR|JSON|MONEY|BIGMONEY)$};
264  
265 3 my (@fields, %fast, $default_type);
266 3 ▶ $default_type = $space->{default_type} || 'STR';
Condition100%2/2
$space->{'default_type'} || 'STR'
l!l
21
MC/DC100%1/1
$space->{'default_type'} || 'STR'
$space->{'default_type'}
Truth table100%2/2
A
A= $space->{'default_type'}
Aresult
11
00
267 3 ▶ croak "wrong 'default_type'" unless $default_type =~ $fqr;
Branch: 1 branch
BranchTrueFalseTotal
unless $default_type =~ /$fqr/ 0 3 3
268  
269 3 ▶ for (my $no = 0; $no < @{ $space->{fields} }; $no++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no < @{$space->{"fields"};}) 15 3 18
270 15 my $f = $space->{ fields }[ $no ];
271  
272 15 ▶ if (ref $f eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $f eq 'HASH') { } 9 6 15
elsif (ref $f) { } 0 6 6
273 push @fields => {
274 name => $f->{name} || "f$no",
275 idx => $no,
276 type => $f->{type}
277 9 ▶ };
Condition50%1/2
$f->{'name'} || "f$no"
l!l
90
MC/DC0%0/1
$f->{'name'} || "f$no"
$f->{'name'}
Truth table50%1/2
A
A= $f->{'name'}
Aresult
11
00
278 } elsif(ref $f) {
279 0 croak 'wrong field name or description';
280 } else {
281 6 push @fields => {
282 name => $f,
283 idx => $no,
284 type => $default_type,
285 }
286 }
287  
288 15 my $s = $fields[ -1 ];
289 croak 'unknown field type: ' .
290 (defined($s->{type}) ? $s->{type} : 'undef')
291 15 ▶ unless $s->{type} and $s->{type} =~ $fqr;
Branch: 2 branches
BranchTrueFalseTotal
unless $s->{"type"} and $s->{"type"} =~ /$fqr/ 0 15 15
defined $s->{'type'} ? : 0 0 0
Condition33%1/3
$s->{"type"} and $s->{"type"} =~ /$fqr/
!ll&&!rl&&r
0015
MC/DC0%0/2
$s->{"type"} and $s->{"type"} =~ /$fqr/
$s->{"type"} $s->{"type"} =~ /$fqr/
Truth table33%1/3
A and B
A= $s->{"type"}B= $s->{"type"} =~ /$fqr/
ABresult
0X0
100
111
292  
293 croak 'wrong field name: ' .
294 (defined($s->{name}) ? $s->{name} : 'undef')
295 15 ▶ unless $s->{name} and $s->{name} =~ /^[a-z_]\w*$/i;
Branch: 2 branches
BranchTrueFalseTotal
unless $s->{"name"} and $s->{"name"} =~ /^[a-z_]\w*$/i 0 15 15
defined $s->{'name'} ? : 0 0 0
Condition33%1/3
$s->{"name"} and $s->{"name"} =~ /^[a-z_]\w*$/i
!ll&&!rl&&r
0015
MC/DC0%0/2
$s->{"name"} and $s->{"name"} =~ /^[a-z_]\w*$/i
$s->{"name"} $s->{"name"} =~ /^[a-z_]\w*$/i
Truth table33%1/3
A and B
A= $s->{"name"}B= $s->{"name"} =~ /^[a-z_]\w*$/i
ABresult
0X0
100
111
296  
297 15 ▶ croak "Duplicate field name: $s->{name}" if exists $fast{ $s->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $fast{$s->{"name"}} 0 15 15
298 15 $fast{ $s->{name} } = $no;
299 }
300  
301 3 my %indexes;
302 3 ▶ if ($space->{indexes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space->{"indexes"}) 3 0 3
303 3 for my $no (keys %{ $space->{indexes} }) {
304 6 my $l = $space->{indexes}{ $no };
305 6 ▶ croak "wrong index number: $no" unless $no =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $no =~ /^\d+$/ 0 6 6
306  
307 6 my ($name, $fields);
308  
309 6 ▶ if ('ARRAY' eq ref $l) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $l) { } 2 4 6
elsif ('HASH' eq ref $l) { } 1 3 4
310 2 $name = "i$no";
311 2 $fields = $l;
312 } elsif ('HASH' eq ref $l) {
313 1 ▶ $name = $l->{name} || "i$no";
Condition50%1/2
$l->{'name'} || "i$no"
l!l
10
MC/DC0%0/1
$l->{'name'} || "i$no"
$l->{'name'}
Truth table50%1/2
A
A= $l->{'name'}
Aresult
11
00
314 $fields =
315 1 ▶ [ ref($l->{fields}) ? @{ $l->{fields} } : $l->{fields} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $l->{'fields'} ? : 1 0 1
316 } else {
317 3 $name = "i$no";
318 3 $fields = [ $l ];
319 }
320  
321 6 ▶ croak "wrong index name: $name" unless $name =~ /^[a-z_]\w*$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[a-z_]\w*$/i 0 6 6
322  
323 6 for (@$fields) {
324 croak "field '$_' is presend in index but isn't in fields"
325 10 ▶ unless exists $fast{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fast{$_} 0 10 10
326 }
327  
328 6 $indexes{ $name } = {
329 no => $no,
330 name => $name,
331 fields => $fields
332 };
333  
334 }
335 }
336  
337 3 my $tuple_class = 'DR::Tarantool::Tuple::Instance' .
338 Digest::MD5::md5_hex( join "\0", sort keys %fast );
339  
340 bless {
341 fields => \@fields,
342 fast => \%fast,
343 name => $name,
344 number => $no,
345 default_type => $default_type,
346 indexes => \%indexes,
347 tuple_class => $tuple_class,
348 family => $opts{family},
349 3 ▶ } => ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
350  
351 }
352  
353  
354 sub family {
355 21 ▶ my ($self, $family) = @_;
Subroutine family: called CC=2 SCAR=6.9
356 21 ▶ return $self->{family} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 17 4 21
357 4 return $self->{family} = $family;
358 }
359  
360  
361 =head2 tuple_class
362  
363 Create (or return) a class to hold tuple data.
364 The class is a descendant of L<DR::Tarantool::Tuple>. Returns a unique class
365 (package) name. If a package with such name is already exists, the method
366 doesn't recreate it.
367  
368 =cut
369  
370 sub tuple_class {
371 11 ▶ my ($self) = @_;
Subroutine tuple_class: called CC=4 SCAR=13.9
372 11 my $class = $self->{tuple_class};
373  
374  
375 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
376 11 ▶ return $class if ${ $class . '::CREATED' };
Branch: 1 branch
BranchTrueFalseTotal
if ${$class . '::CREATED';} 8 3 11
377  
378 3 ▶ die unless eval "package $class; use base 'DR::Tarantool::Tuple'; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless eval "package $class; use base 'DR::Tarantool::Tuple'; 1" 0 3 3
379  
380 3 for my $fname (keys %{ $self->{fast} }) {
381 15 my $fnumber = $self->{fast}{$fname};
382  
383 15 *{ $class . '::' . $fname } = eval "sub { \$_[0]->raw($fnumber) }";
384 }
385  
386 3 ${ $class . '::CREATED' } = time;
387  
388 3 return $class;
389 }
390  
391  
392 =head2 name
393  
394 Get a space name.
395  
396 =cut
397  
398 7 ▶sub name { $_[0]{name} }
Subroutine name: called CC=1 SCAR=0.0
399  
400  
401 =head2 number
402  
403 Get a space number.
404  
405 =cut
406  
407 9 ▶sub number { $_[0]{number} }
Subroutine number: called CC=1 SCAR=0.0
408  
409 sub _field {
410 105 ▶ my ($self, $field) = @_;
Subroutine _field: called CC=5 SCAR=16.8
411  
412 105 ▶ croak 'field name or number is not defined' unless defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 105 105
413 109 ▶ if ($field =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /^\d+$/) 20 85 105
414 21 ▶ return $self->{fields}[ $field ] if $field < @{ $self->{fields} };
Branch: 1 branch
BranchTrueFalseTotal
if $field < @{$self->{"fields"};} 20 0 20
415 0 return undef;
416 }
417 croak "field with name '$field' is not defined in this space"
418 85 ▶ unless exists $self->{fast}{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fast"}{$field} 0 85 85
419 85 return $self->{fields}[ $self->{fast}{$field} ];
420 }
421  
422  
423 =head2 field_number
424  
425 Return field index by field name.
426  
427 =cut
428  
429 sub field_number {
430 4 ▶ my ($self, $field) = @_;
Subroutine field_number: called CC=3 SCAR=11.0
431 4 ▶ croak 'field name or number is not defined' unless defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 4 4
432 4 ▶ return $self->{fast}{$field} if exists $self->{fast}{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"fast"}{$field} 3 1 4
433 1 croak "Can't find field '$field' in this space";
434 }
435  
436  
437 =head2 tail_index
438  
439 Return index of the first element that is not described in the space.
440  
441 =cut
442  
443 sub tail_index {
444 3 ▶ my ($self) = @_;
Subroutine tail_index: called CC=1 SCAR=0.0
445 3 return scalar @{ $self->{fields} };
446 }
447  
448  
449 =head2 pack_field
450  
451 Pack a field before making a database request.
452  
453 =cut
454  
455 sub pack_field {
456 52 ▶ my ($self, $field, $value) = @_;
Subroutine pack_field: called CC=20 SCAR=30.8
457 52 ▶ croak q{Usage: $space->pack_field('field', $value)}
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 52 52
458 unless @_ == 3;
459  
460 52 my $f = $self->_field($field);
461  
462 52 ▶ my $type = $f ? $f->{type} : $self->{default_type};
Branch: 1 branch
BranchTrueFalseTotal
$f ? : 52 0 52
463  
464 52 ▶ if ($type eq 'JSON') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "JSON") 8 44 52
465 8 my $v = eval { JSON::XS->new->allow_nonref->utf8->encode( $value ) };
466 8 ▶ croak "Can't pack json: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
467 8 return $v;
468 }
469  
470 44 my $v = $value;
471 44 ▶ utf8::encode( $v ) if utf8::is_utf8( $v );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($v) 4 40 44
472 44 ▶ return $v if $type eq 'STR' or $type eq 'UTF8STR';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "STR" or $type eq "UTF8STR" 7 37 44
Condition100%3/3
$type eq "STR" or $type eq "UTF8STR"
l!l&&r!l&&!r
1637
MC/DC100%2/2
$type eq "STR" or $type eq "UTF8STR"
$type eq "STR" $type eq "UTF8STR"
Truth table100%3/3
A or B
A= $type eq "STR"B= $type eq "UTF8STR"
ABresult
1X1
011
000
473 37 ▶ return pack "L$LE" => $v if $type eq 'NUM';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NUM" 29 8 37
474 8 ▶ return pack "l$LE" => $v if $type eq 'INT';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "INT" 1 7 8
475 7 ▶ return pack "Q$LE" => $v if $type eq 'NUM64';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NUM64" 0 7 7
476 7 ▶ return pack "q$LE" => $v if $type eq 'INT64';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "INT64" 0 7 7
477  
478 7 ▶ if ($type eq 'MONEY' or $type eq 'BIGMONEY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "MONEY" or $type eq "BIGMONEY") 7 0 7
Condition33%1/3
$type eq "MONEY" or $type eq "BIGMONEY"
l!l&&r!l&&!r
700
MC/DC0%0/2
$type eq "MONEY" or $type eq "BIGMONEY"
$type eq "MONEY" $type eq "BIGMONEY"
Truth table33%1/3
A or B
A= $type eq "MONEY"B= $type eq "BIGMONEY"
ABresult
1X1
011
000
479 7 my ($r, $k) = split /\./, $v;
480 7 for ($k) {
481 7 ▶ $_ = '.00' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 3 4 7
482 7 s/^\.//;
483 7 ▶ $_ .= '0' if length $_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < 2 1 6 7
484 7 $_ = substr $_, 0, 2;
485 }
486 7 ▶ $r ||= 0;
Condition100%2/2
$r ||= 0
l!l
52
MC/DC100%1/1
$r ||= 0
$r
Truth table100%2/2
A
A= $r
Aresult
11
00
487  
488 7 ▶ if ($r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < 0) { } 2 5 7
489 2 $v = $r * 100 - $k;
490 } else {
491 5 $v = $r * 100 + $k;
492 }
493  
494 7 ▶ return pack "l$LE", $v if $type eq 'MONEY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MONEY" 7 0 7
495 0 return pack "q$LE", $v;
496 }
497  
498  
499 0 croak 'Unknown field type:' . $type;
500 }
501  
502  
503 =head2 unpack_field
504  
505 Unpack a single field in a server response.
506  
507 =cut
508  
509 sub unpack_field {
510 27 ▶ my ($self, $field, $value) = @_;
Subroutine unpack_field: called CC=16 SCAR=28.2
511 27 ▶ croak q{Usage: $space->pack_field('field', $value)}
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 27 27
512 unless @_ == 3;
513  
514 27 my $f = $self->_field($field);
515  
516 27 ▶ my $type = $f ? $f->{type} : $self->{default_type};
Branch: 1 branch
BranchTrueFalseTotal
$f ? : 27 0 27
517  
518 27 my $v = $value;
519 27 ▶ utf8::encode( $v ) if utf8::is_utf8( $v );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($v) 2 25 27
520  
521 27 ▶ if ($type eq 'JSON') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "JSON") 8 19 27
522 8 $v = JSON::XS->new->allow_nonref->utf8->decode( $v );
523 8 ▶ croak "Can't unpack json: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
524 8 return $v;
525 }
526  
527 19 ▶ $v = unpack "L$LE" => $v if $type eq 'NUM';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NUM" 9 10 19
528 19 ▶ $v = unpack "l$LE" => $v if $type eq 'INT';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "INT" 1 18 19
529 19 ▶ $v = unpack "Q$LE" => $v if $type eq 'NUM64';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NUM64" 0 19 19
530 19 ▶ $v = unpack "q$LE" => $v if $type eq 'INT64';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "INT64" 0 19 19
531 19 ▶ utf8::decode( $v ) if $type eq 'UTF8STR';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "UTF8STR" 2 17 19
532 19 ▶ if ($type eq 'MONEY' or $type eq 'BIGMONEY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "MONEY" or $type eq "BIGMONEY") 4 15 19
Condition66%2/3
$type eq "MONEY" or $type eq "BIGMONEY"
l!l&&r!l&&!r
4015
MC/DC50%1/2
$type eq "MONEY" or $type eq "BIGMONEY"
$type eq "MONEY" $type eq "BIGMONEY"
Truth table66%2/3
A or B
A= $type eq "MONEY"B= $type eq "BIGMONEY"
ABresult
1X1
011
000
533 4 ▶ $v = unpack "l$LE" => $v if $type eq 'MONEY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MONEY" 4 0 4
534 4 ▶ $v = unpack "q$LE" => $v if $type eq 'BIGMONEY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "BIGMONEY" 0 4 4
535 4 my $s = '';
536 4 ▶ if ($v < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < 0) 1 3 4
537 1 $v = -$v;
538 1 $s = '-';
539 }
540 4 my $k = $v % 100;
541 4 my $r = ($v - $k) / 100;
542 4 $v = sprintf '%s%d.%02d', $s, $r, $k;
543 }
544 19 return $v;
545 }
546  
547  
548 =head2 pack_tuple
549  
550 Pack a tuple to the binary protocol format:
551  
552 =cut
553  
554 sub pack_tuple {
555 1 ▶ my ($self, $tuple) = @_;
Subroutine pack_tuple: called CC=4 SCAR=14.2
556 1 ▶ croak 'tuple must be ARRAYREF' unless 'ARRAY' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple 0 1 1
557 1 my @res;
558 1 ▶ if ($self->family == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->family == 1) { } 1 0 1
559 1 ▶ for (my $i = 0; $i < @$tuple; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tuple) 6 1 7
560 6 push @res => $self->pack_field($i, $tuple->[ $i ]);
561 }
562 } else {
563 0 @res = @$tuple;
564 }
565 1 return \@res;
566 }
567  
568  
569 =head2 unpack_tuple
570  
571 Unpack a tuple in a server response.
572  
573 =cut
574  
575 sub unpack_tuple {
576 2 ▶ my ($self, $tuple) = @_;
Subroutine unpack_tuple: called CC=4 SCAR=14.2
577 2 ▶ croak 'tuple must be ARRAYREF' unless 'ARRAY' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple 0 2 2
578 2 my @res;
579 2 ▶ if ($self->family == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->family == 1) { } 2 0 2
580 2 ▶ for (my $i = 0; $i < @$tuple; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tuple) 8 2 10
581 8 push @res => $self->unpack_field($i, $tuple->[ $i ]);
582 }
583 } else {
584 0 @res = @$tuple;
585 }
586 2 return \@res;
587 }
588  
589  
590 sub _index {
591 16 ▶ my ($self, $index) = @_;
Subroutine _index: called CC=5 SCAR=16.1
592 16 ▶ if ($index =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index =~ /^\d+$/) 5 11 16
593 5 for (values %{ $self->{indexes} }) {
594 14 ▶ return $_ if $_->{no} == $index;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"no"} == $index 5 9 14
595 }
596 0 croak "index $index is undefined";
597 }
598  
599 11 ▶ return $self->{indexes}{$index} if exists $self->{indexes}{$index};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"indexes"}{$index} 10 1 11
600 1 croak "index `$index' is undefined";
601 }
602  
603  
604 =head2 index_number
605  
606 returns index number by its name.
607  
608 =cut
609  
610 sub index_number {
611 4 ▶ my ($self, $idx) = @_;
Subroutine index_number: called CC=2 SCAR=6.9
612 4 ▶ croak "index name is undefined" unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 1 3 4
613 3 return $self->_index( $idx )->{no};
614 }
615  
616  
617 =head2 index_name
618  
619 returns index name by its number.
620  
621 =cut
622  
623 sub index_name {
624 2 ▶ my ($self, $idx) = @_;
Subroutine index_name: called CC=2 SCAR=7.1
625 2 ▶ croak "index number is undefined" unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 2 2
626 2 return $self->_index( $idx )->{name};
627 }
628  
629  
630 sub pack_keys {
631 11 ▶ my ($self, $keys, $idx, $disable_warn) = @_;
Subroutine pack_keys: called CC=9 SCAR=22.0
632  
633 11 $idx = $self->_index($idx);
634 11 my $ksize = @{ $idx->{fields} };
635  
636 11 ▶ $keys = [[ $keys ]] unless 'ARRAY' eq ref $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $keys 1 10 11
637 11 ▶ unless('ARRAY' eq ref $keys->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("ARRAY" eq ref $keys->[0]) 4 7 11
638 4 ▶ if ($ksize == @$keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ksize == @$keys) { } 3 1 4
639 3 $keys = [ $keys ];
640 3 ▶ carp "Ambiguous keys list (it was used as ONE key), ".
Branch: 1 branch
BranchTrueFalseTotal
if $ksize > 1 and not $disable_warn 2 1 3
Condition66%2/3
$ksize > 1 and not $disable_warn
!ll&&!rl&&r
102
MC/DC50%1/2
$ksize > 1 and not $disable_warn
$ksize > 1 not $disable_warn
Truth table66%2/3
A and not(B)
A= $ksize > 1B= not $disable_warn
ABresult
0X0
100
111
641 "Use brackets to solve the trouble."
642 if $ksize > 1 and !$disable_warn;
643 } else {
644 1 $keys = [ map { [ $_ ] } @$keys ];
645 }
646 }
647  
648 11 my @res;
649 11 for my $k (@$keys) {
650 13 ▶ croak "key must have $ksize elements" unless $ksize >= @$k;
Branch: 1 branch
BranchTrueFalseTotal
unless $ksize >= @$k 3 10 13
651 10 my @packed;
652 10 ▶ for (my $i = 0; $i < @$k; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$k) 14 10 24
653 14 my $f = $self->_field($idx->{fields}[$i]);
654 14 push @packed => $self->pack_field($f->{name}, $k->[$i])
655 }
656 10 push @res => \@packed;
657 }
658 8 return \@res;
659 }
660  
661 sub pack_primary_key {
662 0 ▶ my ($self, $key) = @_;
Subroutine pack_primary_key: not called CC=3 SCAR=24.8
663  
664 0 ▶ croak 'wrong key format'
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $key and "ARRAY" eq ref $key->[0] 0 0 0
Condition0%0/3
"ARRAY" eq ref $key and "ARRAY" eq ref $key->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
"ARRAY" eq ref $key and "ARRAY" eq ref $key->[0]
"ARRAY" eq ref $key "ARRAY" eq ref $key->[0]
Truth table0%0/3
A and B
A= "ARRAY" eq ref $keyB= "ARRAY" eq ref $key->[0]
ABresult
0X0
100
111
665 if 'ARRAY' eq ref $key and 'ARRAY' eq ref $key->[0];
666  
667 0 my $t = $self->pack_keys($key, 0, 1);
668 0 return $t->[0];
669 }
670  
671 sub pack_operation {
672 12 ▶ my ($self, $op) = @_;
Subroutine pack_operation: called CC=12 SCAR=28.9
673 12 ▶ croak 'wrong operation' unless 'ARRAY' eq ref $op and @$op > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $op and @$op > 1 0 12 12
Condition33%1/3
"ARRAY" eq ref $op and @$op > 1
!ll&&!rl&&r
0012
MC/DC0%0/2
"ARRAY" eq ref $op and @$op > 1
"ARRAY" eq ref $op @$op > 1
Truth table33%1/3
A and B
A= "ARRAY" eq ref $opB= @$op > 1
ABresult
0X0
100
111
674  
675 12 ▶ if ($self->family == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->family == 1) 12 0 12
676 12 my $fno = $op->[0];
677 12 my $opname = $op->[1];
678  
679 12 my $f = $self->_field($fno);
680  
681 12 ▶ if ($opname eq 'delete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opname eq "delete") 1 11 12
682 1 ▶ croak 'wrong operation' unless @$op == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$op == 2 0 1 1
683 1 return [ $f->{idx} => $opname ];
684 }
685  
686 11 ▶ if ($opname =~ /^(?:set|insert|add|and|or|xor)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opname =~ /^(?:set|insert|add|and|or|xor)$/) 9 2 11
687 9 ▶ croak 'wrong operation' unless @$op == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @$op == 3 0 9 9
688 9 return [ $f->{idx} => $opname, $self->pack_field($fno, $op->[2]) ];
689 }
690  
691 2 ▶ if ($opname eq 'substr') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opname eq "substr") 2 0 2
692 2 ▶ croak 'wrong operation11' unless @$op >= 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @$op >= 4 0 2 2
693 2 ▶ croak 'wrong offset in substr operation' unless $op->[2] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->[2] =~ /^\d+$/ 0 2 2
694 2 ▶ croak 'wrong length in substr operation' unless $op->[3] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->[3] =~ /^\d+$/ 0 2 2
695 2 return [ $f->{idx}, $opname, $op->[2], $op->[3], $op->[4] ];
696 }
697 0 croak "unknown operation: $opname";
698 }
699  
700 0 my $fno = $op->[1];
701 0 my $f = $self->_field($fno);
702 0 my @res = @$op;
703 0 splice @res, 1, 1, $f->{idx};
704 0 return \@res;
705 }
706  
707 sub pack_operations {
708 2 ▶ my ($self, $ops) = @_;
Subroutine pack_operations: called CC=5 SCAR=16.7
709  
710 2 ▶ croak 'wrong operation' unless 'ARRAY' eq ref $ops and @$ops >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $ops and @$ops >= 1 0 2 2
Condition33%1/3
"ARRAY" eq ref $ops and @$ops >= 1
!ll&&!rl&&r
002
MC/DC0%0/2
"ARRAY" eq ref $ops and @$ops >= 1
"ARRAY" eq ref $ops @$ops >= 1
Truth table33%1/3
A and B
A= "ARRAY" eq ref $opsB= @$ops >= 1
ABresult
0X0
100
111
711 2 ▶ $ops = [ $ops ] unless 'ARRAY' eq ref $ops->[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $ops->[0] 1 1 2
712  
713 2 my @res;
714 2 push @res => $self->pack_operation( $_ ) for @$ops;
715 2 return \@res;
716 }
717  
718 =head1 COPYRIGHT AND LICENSE
719  
720 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
721 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
722  
723 This program is free software, you can redistribute it and/or
724 modify it under the terms of the Artistic License.
725  
726 =head1 VCS
727  
728 The project is placed git repo on github:
729 L<https://github.com/dr-co/dr-tarantool/>.
730  
731 =cut
732  
733 7 1;
« blib/lib/DR/Tarantool/AsyncClient.pm ↑ Summary blib/lib/DR/Tarantool/SyncClient.pm »