blib/lib/DR/Tarantool/Tuple.pm

filter: Statementstmt 94.4% 68 / 72 Branchbran 65.0% 26 / 40 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 73.6% 120 / 163 CC 28 SCAR 20.8
CC
28
Cov
77%
new
18.4
unpack
10.1
raw
5.1
CRAP 8.0
« blib/lib/DR/Tarantool/MsgPack/AsyncClient.pm ↑ Summary blib/lib/DR/Tarantool/AEConnection.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 =head1 NAME
6  
7 DR::Tarantool::Tuple - a tuple container for L<DR::Tarantool>
8  
9 =head1 SYNOPSIS
10  
11 my $tuple = new DR::Tarantool::Tuple([ 1, 2, 3]);
12 my $tuple = new DR::Tarantool::Tuple([ 1, 2, 3], $space);
13 my $tuple = unpack DR::Tarantool::Tuple([ 1, 2, 3], $space);
14  
15  
16 $tuple->next( $other_tuple );
17  
18 $f = $tuple->raw(0);
19  
20 $f = $tuple->name_field;
21  
22  
23 =head1 DESCRIPTION
24  
25 A tuple contains normalized (unpacked) fields. You can access the fields
26 by their indexes (see L<raw> function) or by their names (if they are
27 described in the space).
28  
29 Each tuple can contain references to L<next> tuple and L<iter>ator,
30 so that if the server returns more than one tuple, all of them
31 can be accessed.
32  
33 =head1 METHODS
34  
35 =cut
36  
37 package DR::Tarantool::Tuple;
38 7 ▶use DR::Tarantool::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use Scalar::Util 'weaken', 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 $Carp::Internal{ (__PACKAGE__) }++;
42  
43  
44 =head2 new
45  
46 A constructor.
47  
48 my $t = DR::Tarantool::Tuple->new([1, 2, 3]);
49 my $t = DR::Tarantool::Tuple->new([1, 2, 3], $space);
50  
51 =cut
52  
53 sub new :method {
54 7 ▶ my ($class, $tuple, $space) = @_;
Subroutine new: called CC=11 SCAR=29.1
Pod documented
55  
56 7 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 7 7
57  
58 # hack to replace default autoload
59 7 ▶ $class = $space->tuple_class if $space and $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $space and $class eq __PACKAGE__ 7 0 7
Condition33%1/3
$space and $class eq __PACKAGE__
!ll&&!rl&&r
007
MC/DC0%0/2
$space and $class eq __PACKAGE__
$space $class eq __PACKAGE__
Truth table33%1/3
A and B
A= $spaceB= $class eq __PACKAGE__
ABresult
0X0
100
111
60  
61 7 ▶ croak 'wrong space' if defined $space and !blessed $space;
Branch: 1 branch
BranchTrueFalseTotal
if defined $space and not builtin::blessed($space) 0 7 7
Condition33%1/3
defined $space and not builtin::blessed($space)
!ll&&!rl&&r
070
MC/DC0%0/2
defined $space and not builtin::blessed($space)
defined $space not builtin::blessed($space)
Truth table33%1/3
A and not(B)
A= defined $spaceB= not builtin::blessed($space)
ABresult
0X0
100
111
62  
63 7 ▶ croak 'tuple must be ARRAYREF [of ARRAYREF]' unless 'ARRAY' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple 0 7 7
64 7 ▶ croak "tuple can't be empty" unless @$tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tuple 0 7 7
65  
66 7 ▶ $tuple = [ $tuple ] unless 'ARRAY' eq ref $tuple->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple->[0] 3 4 7
67  
68 7 ▶ my $iterator = DR::Tarantool::Iterator->new(
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
69 $tuple,
70 data => $space,
71 item_class => ref($class) || $class,
72 item_constructor => '_new'
73 );
74  
75 7 ▶ return bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
76 idx => 0,
77 iterator => $iterator,
78 } => ref($class) || $class;
79 }
80  
81  
82 sub _new {
83 16 ▶ my ($class, $item, $idx, $iterator) = @_;
Subroutine _new: called CC=2 SCAR=8.1
84 16 ▶ return bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0160
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
85 idx => $idx,
86 iterator => $iterator,
87 } => ref($class) || $class;
88 }
89  
90  
91 =head2 unpack
92  
93 Another way to construct a tuple.
94  
95 my $t = DR::Tarantool::Tuple->unpack([1, 2, 3], $space);
96  
97 =cut
98  
99 sub unpack :method {
100 1 ▶ my ($class, $tuple, $space) = @_;
Subroutine unpack: called CC=7 SCAR=23.2
Pod documented
101 1 ▶ croak 'wrong space' unless blessed $space;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($space) 0 1 1
102 1 ▶ return undef unless defined $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tuple 0 1 1
103 1 ▶ croak 'tuple must be ARRAYREF [of ARRAYREF]' unless 'ARRAY' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $tuple 0 1 1
104 1 ▶ return undef unless @$tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tuple 0 1 1
105  
106 1 ▶ if ('ARRAY' eq ref $tuple->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $tuple->[0]) 0 1 1
107 0 my @tu;
108  
109 0 push @tu => $space->unpack_tuple($_) for @$tuple;
110 0 return $class->new(\@tu, $space);
111 }
112  
113 1 return $class->new($space->unpack_tuple($tuple), $space);
114 }
115  
116  
117 =head2 raw
118  
119 Return raw data from the tuple.
120  
121 my $array = $tuple->raw;
122  
123 my $field = $tuple->raw(0);
124  
125 =cut
126  
127 sub raw :method {
128 37 ▶ my ($self, $fno) = @_;
Subroutine raw: called CC=5 SCAR=16.2
Pod documented
129  
130 37 my $item = $self->{iterator}->raw_item( $self->{idx} );
131  
132 37 ▶ return $item unless defined $fno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fno 3 34 37
133  
134 34 ▶ croak 'wrong field number' unless $fno =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fno =~ /^-?\d+$/ 0 34 34
135  
136 34 ▶ return undef if $fno < -@$item;
Branch: 1 branch
BranchTrueFalseTotal
if $fno < -@$item 0 34 34
137 34 ▶ return undef if $fno >= @$item;
Branch: 1 branch
BranchTrueFalseTotal
if $fno >= @$item 2 32 34
138 32 return $item->[ $fno ];
139 }
140  
141  
142 =head2 next
143  
144 Append or return the next tuple, provided there is more than one
145 tuple in the result set.
146  
147 my $next_tuple = $tuple->next;
148  
149 =cut
150  
151 sub next :method {
152  
153 5 ▶ my ($self, $tuple) = @_;
Subroutine next: called CC=3 SCAR=11.0
Pod documented
154  
155 5 my $iterator = $self->{iterator};
156 5 my $idx = $self->{idx} + 1;
157  
158 # if tuple is exists next works like 'iterator->push'
159 5 ▶ if ('ARRAY' eq ref $tuple) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $tuple) 2 3 5
160 2 $iterator->push( $tuple );
161 2 $idx = $iterator->count - 1;
162 }
163  
164 5 ▶ return undef unless $idx < $iterator->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $idx < $iterator->count 0 5 5
165  
166 5 my $next = bless {
167 idx => $idx,
168 iterator => $iterator,
169 } => ref($self);
170  
171 5 return $next;
172 }
173  
174  
175 =head2 iter
176  
177 Return an iterator object associated with the tuple.
178  
179  
180 my $iterator = $tuple->iter;
181  
182 my $iterator = $tuple->iter('MyTupleClass', 'new');
183  
184 while(my $t = $iterator->next) {
185 # the first value of $t and $tuple are the same
186 ...
187 }
188  
189 =head3 Arguments
190  
191 =over
192  
193 =item package (optional)
194  
195 =item method (optional)
196  
197 If 'package' and 'method' are present, $iterator->L<next> method
198 constructs objects using C<< $package->$method( $next_tuple ) >>
199  
200 If 'method' is not present and 'package' is present, the iterator
201 blesses the raw array with 'package'.
202  
203 =back
204  
205 =cut
206  
207 sub iter :method {
208 6 ▶ my ($self, $class, $method) = @_;
Subroutine iter: called CC=2 SCAR=7.0
Pod documented
209  
210 6 my $iterator = $self->{iterator};
211  
212 6 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 2 4 6
213 return $self->{iterator}->clone(
214 item_class => $class,
215 item_constructor => sub {
216 6 ▶ my ($c, $item, $idx) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.2
217  
218 6 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 2 4 6
219 2 my $bitem = bless {
220 idx => $idx,
221 iterator => $iterator,
222 } => ref($self);
223  
224  
225 2 return $c->$method( $bitem );
226 }
227 4 ▶ return bless [ @$item ] => ref($c) || $c;
Condition33%1/3
ref $c || $c
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table33%1/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
228 }
229 2 );
230 }
231  
232 4 return $self->{iterator};
233 }
234  
235  
236 =head2 tail
237  
238 Return the tail of the tuple (array of unnamed fields). The function always
239 returns B<ARRAYREF> (as L<raw>).
240  
241 =cut
242  
243 sub tail {
244 1 ▶ my ($self) = @_;
Subroutine tail: called CC=2 SCAR=7.4
Pod documented
245 1 my $space = $self->{iterator}->data;
246 1 my $raw = $self->raw;
247  
248 1 ▶ return [ @$raw[ $space->tail_index .. $#$raw ] ] if $space;
Branch: 1 branch
BranchTrueFalseTotal
if $space 1 0 1
249 0 return $raw;
250 }
251  
252  
253  
254 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
255  
256  
257 =head1 COPYRIGHT AND LICENSE
258  
259 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
260 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
261  
262 This program is free software, you can redistribute it and/or
263 modify it under the terms of the Artistic License.
264  
265 =head1 VCS
266  
267 The project is placed git repo on github:
268 L<https://github.com/dr-co/dr-tarantool/>.
269  
270 =cut
271  
272 7 1;
« blib/lib/DR/Tarantool/MsgPack/AsyncClient.pm ↑ Summary blib/lib/DR/Tarantool/AEConnection.pm »