blib/lib/DR/Tarantool/Iterator.pm

filter: Statementstmt 99.2% 128 / 129 Branchbran 94.1% 64 / 68 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 95.8% 23 / 24 Podpod 100% 19 / 19 totaltotal 95.6% 239 / 250 CC 37 SCAR 14.9
CC
37
Cov
97%
all
8.0
raw_item
7.0
new
7.0
CRAP 4.5
« blib/lib/DR/Tarantool/MsgPack/CoroClient.pm ↑ Summary blib/lib/DR/Tarantool/MsgPack.pm »
1 =head1 NAME
2  
3 DR::Tarantool::Iterator - an iterator and a container class for
4 L<DR::Tarantool>
5  
6 =head1 SYNOPSIS
7  
8 use DR::Tarantool::Iterator;
9  
10 my $iter = DR::Tarantool::Iterator->new([1, 2, 3]);
11  
12 my $item0 = $iter->item(0);
13  
14 my @all = $iter->all;
15 my $all = $iter->all;
16  
17 while(my $item = $iter->next) {
18 do_something_with_item( $item );
19 }
20  
21  
22 =head1 METHODS
23  
24 =cut
25  
26 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 package DR::Tarantool::Iterator;
31 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 =head2 new
36  
37 A constructor.
38  
39 =head3 Arguments
40  
41 =over
42  
43 =item *
44  
45 An array of tuples to iterate over.
46  
47 =item *
48  
49 A list of named arguments:
50  
51 =over
52  
53 =item item_class
54  
55 Name of the class to bless each tuple in the iterator with.
56 If the field is 'B<ARRAYREF>' then the first element of the array is
57 B<item_class>, and the second element is B<item_constructor>.
58  
59 =item item_constructor
60  
61 Name of a constructor to invoke for each tuple. If this value is
62 undefined and B<item_class> is defined, the iterator blesses each
63 tuple but does not invoke a constructor on it.
64  
65 The constructor is invoked on with three arguments: B<item>,
66 B<item_index> and B<iterator>, for example:
67  
68  
69 my $iter = DR::Tarantool::Iterator->new(
70 [ [1], [2], [3] ],
71 item_class => 'MyClass',
72 item_constructor => 'new'
73 );
74  
75 my $iter = DR::Tarantool::Iterator->new( # the same
76 [ [1], [2], [3] ],
77 item_class => [ 'MyClass', 'new' ]
78 );
79  
80  
81 my $item = $iter->item(0);
82 my $item = MyClass->new( [1], 0, $iter ); # the same
83  
84 my $item = $iter->item(2);
85 my $item = MyClass->new( [3], 2, $iter ); # the same
86  
87 =item data
88  
89 Application state to store in the iterator. Is useful
90 if additional state needs to be passed into tuple constructor.
91  
92 =back
93  
94 =back
95  
96 =cut
97  
98 sub new {
99 21 ▶ my ($class, $items, %opts) = @_;
Subroutine new: called CC=7 SCAR=19.5
Pod documented
100  
101 21 ▶ croak 'usage: DR::Tarantool::Iterator->new([$item1, $item2, ... ], %opts)'
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $items 1 20 21
102 unless 'ARRAY' eq ref $items;
103  
104  
105 20 ▶ my $self = bless { items => $items } => ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
8120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
106  
107 $self->item_class(
108 ('ARRAY' eq ref $opts{item_class}) ?
109 1 @{ $opts{item_class} } : $opts{item_class}
110 20 ▶ ) if exists $opts{item_class};
Branch: 2 branches
BranchTrueFalseTotal
if exists $opts{"item_class"} 18 2 20
'ARRAY' eq ref $opts{'item_class'} ? : 1 17 18
111  
112 $self->item_constructor($opts{item_constructor})
113 20 ▶ if exists $opts{item_constructor};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"item_constructor"} 16 4 20
114  
115 20 ▶ $self->data( $opts{data} ) if exists $opts{data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"data"} 16 4 20
116 20 $self;
117 }
118  
119  
120 =head2 clone(%opt)
121  
122 Clone the iterator object, but do not clone the tuples.
123 This method can be used to create an iterator that has
124 a different B<item_class> and (or) B<item_constructor>.
125  
126 If B<clone_items> argument is true, the function clones the tuple
127 list as well.
128  
129 my $iter1 = $old_iter->clone(item_class => [ 'MyClass', 'new' ]);
130 my $iter2 = $old_iter->clone(item_class => [ 'MyClass', 'new' ],
131 clone_items => 1);
132  
133 $old_iter->sort(sub { $_[0]->name cmp $_[1]->name });
134 # $iter1 is sorted, too, but $iter2 is not
135  
136 =cut
137  
138 sub clone {
139  
140 4 ▶ my $self = shift;
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
141 4 my %opts;
142 4 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 3 4
143 1 %opts = (clone_items => shift);
144 } else {
145 3 %opts = @_;
146 }
147  
148 4 my %pre = (
149 data => $self->data,
150 item_class => $self->item_class,
151 item_constructor => $self->item_constructor
152 );
153  
154 4 my $clone_items = delete $opts{clone_items};
155  
156 4 ▶ my $items = $clone_items ? [ @{ $self->{items} } ] : $self->{items};
Branch: 1 branch
BranchTrueFalseTotal
$clone_items ? : 1 3 4
157 4 $self = $self->new( $items, %pre, %opts );
158 4 $self;
159 }
160  
161  
162 =head2 count
163  
164 Return the number of tuples available through the iterator.
165  
166 =cut
167  
168 sub count {
169 548 ▶ my ($self) = @_;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
170 548 return scalar @{ $self->{items} };
171 }
172  
173  
174 =head2 item
175  
176 Return one tuple from the iterator by its index
177 (or croak an error if the index is out of range).
178  
179 =cut
180  
181 sub item {
182 135 ▶ my ($self, $no) = @_;
Subroutine item: called CC=4 SCAR=13.9
Pod documented
183  
184 135 my $item = $self->raw_item( $no );
185  
186 131 ▶ if (my $class = $self->item_class) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $self->item_class) 43 88 131
187  
188 43 ▶ if (my $m = $self->item_constructor) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $self->item_constructor) 39 4 43
189 39 return $class->$m( $item, $no, $self );
190 }
191  
192 4 ▶ return bless $item => $class if ref $item;
Branch: 1 branch
BranchTrueFalseTotal
if ref $item 0 4 4
193 4 return bless \$item => $class;
194 }
195  
196 88 return $self->{items}[ $no ];
197 }
198  
199  
200 =head2 raw_item
201  
202 Return one raw tuple from the iterator by its index
203 (or croak error if the index is out of range).
204  
205 In other words, this method ignores B<item_class> and B<item_constructor>.
206  
207 =cut
208  
209 sub raw_item {
210 172 ▶ my ($self, $no) = @_;
Subroutine raw_item: called CC=7 SCAR=19.5
Pod documented
211  
212 172 my $exists = $self->exists($no);
213 172 ▶ croak "wrong item number format: " . (defined($no) ? $no : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $exists 2 170 172
defined $no ? : 1 1 2
214 unless defined $exists;
215 170 ▶ croak 'wrong item number: ' . $no unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 2 168 170
216  
217 168 ▶ if ($no >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no >= 0) { } 164 4 168
218 164 ▶ croak "iterator doesn't contain item with number $no"
Branch: 1 branch
BranchTrueFalseTotal
unless $no < $self->count 0 164 164
219 unless $no < $self->count;
220 } else {
221 4 ▶ croak "iterator doesn't contain item with number $no"
Branch: 1 branch
BranchTrueFalseTotal
unless $no >= -$self->count 0 4 4
222 unless $no >= -$self->count;
223 }
224  
225 168 return $self->{items}[ $no ];
226 }
227  
228  
229 =head2 raw_sort(&)
230  
231 Sort the contents referred to by the iterator (changes the current
232 iterator object).
233 The compare function receives two B<raw> objects:
234  
235 $iter->raw_sort(sub { $_[0]->field cmp $_[1]->field });
236  
237 =cut
238  
239 sub raw_sort {
240 1 ▶ my ($self, $cb) = @_;
Subroutine raw_sort: called CC=1 SCAR=0.0
Pod documented
241 1 my $items = $self->{items};
242 1 @$items = sort { &$cb($a, $b) } @$items;
243 1 return $self;
244 }
245  
246 =head2 sort(&)
247  
248 Sort the contents referred to by the iterator (changes the current object).
249 The compare function receives two constructed objects:
250  
251 $iter->sort(sub { $_[0]->field <=> $_[1]->field });
252  
253 =cut
254  
255 sub sort : method {
256 1 ▶ my ($self, $cb) = @_;
Subroutine sort: called CC=1 SCAR=0.0
Pod documented
257 1 my $items = $self->{items};
258 1 my @bitems = map { $self->item( $_ ) } 0 .. $#$items;
259 1 my @isorted = sort { &$cb( $bitems[$a], $bitems[$b] ) } 0 .. $#$items;
260  
261 1 @$items = @$items[ @isorted ];
262 1 return $self;
263 }
264  
265  
266 =head2 grep(&)
267  
268 Find all objects in the set referred to by the iterator that
269 match a given search criteria (linear search).
270  
271 my $admins = $users->grep(sub { $_[0]->is_admin });
272  
273 =cut
274  
275 sub grep :method {
276 3 ▶ my ($self, $cb) = @_;
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
277 3 my $items = $self->{items};
278 3 my @bitems = map { $self->item( $_ ) } 0 .. $#$items;
279 3 my @igrepped = grep { &$cb( $bitems[$_] ) } 0 .. $#$items;
280 3 @igrepped = @$items[ @igrepped ];
281  
282 3 return $self->new(
283 \@igrepped,
284 item_class => $self->item_class,
285 item_constructor => $self->item_constructor,
286 data => $self->data
287 );
288 }
289  
290  
291 =head2 raw_grep(&)
292  
293 Same as grep, but works on raw objects.
294  
295 my $admins = $users->raw_grep(sub { $_[0]->is_admin });
296  
297 =cut
298  
299 sub raw_grep :method {
300 1 ▶ my ($self, $cb) = @_;
Subroutine raw_grep: called CC=1 SCAR=0.0
Pod documented
301 1 my $items = $self->{items};
302 1 my @igrepped = grep { &$cb($_) } @$items;
303  
304 1 return $self->new(
305 \@igrepped,
306 item_class => $self->item_class,
307 item_constructor => $self->item_constructor,
308 data => $self->data
309 );
310 }
311  
312  
313 =head2 get
314  
315 An alias for L<item> method.
316  
317 =cut
318  
319 0 ▶sub get { goto \&item; }
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
320  
321  
322 =head2 exists
323  
324 Return B<true> if the iterator contains a tuple with the given
325 index.
326  
327 my $item = $iter->exists(10) ? $iter->get(10) : somethig_else();
328  
329 =cut
330  
331 sub exists : method{
332 172 ▶ my ($self, $no) = @_;
Subroutine exists: called CC=5 SCAR=16.1
Pod documented
333 172 ▶ return undef unless defined $no;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no 1 171 172
334 171 ▶ return undef unless $no =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $no =~ /^-?\d+$/ 1 170 171
335 170 ▶ return 0 if $no >= $self->count;
Branch: 1 branch
BranchTrueFalseTotal
if $no >= $self->count 1 169 170
336 169 ▶ return 0 if $no < -$self->count;
Branch: 1 branch
BranchTrueFalseTotal
if $no < -$self->count 1 168 169
337 168 return 1;
338 }
339  
340  
341 =head2 next
342  
343 Return the next tuple, or B<undef> in case of eof.
344  
345 while(my $item = $iter->next) {
346 do_something_with( $item );
347 }
348  
349 Index of the current tuple can be queried with function 'L<iter>'.
350  
351 =cut
352  
353 sub next :method {
354 120 ▶ my ($self) = @_;
Subroutine next: called CC=4 SCAR=13.9
Pod documented
355 120 my $iter = $self->iter;
356  
357 120 ▶ if (defined $self->{iter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"iter"}) 90 30 120
358 return $self->item(++$self->{iter})
359 90 ▶ if $self->iter < $#{ $self->{items} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->iter < $#{$$self{"items"};} 69 21 90
360 21 delete $self->{iter};
361 21 return undef;
362 }
363  
364 30 ▶ return $self->item($self->{iter} = 0) if $self->count;
Branch: 1 branch
BranchTrueFalseTotal
if $self->count 29 1 30
365 1 return undef;
366 }
367  
368  
369 =head2 iter
370  
371 Return index of the tuple at the current iterator position.
372  
373 =cut
374  
375 sub iter {
376 210 ▶ my ($self) = @_;
Subroutine iter: called CC=1 SCAR=0.0
Pod documented
377 210 return $self->{iter};
378 }
379  
380  
381 =head2 reset
382  
383 Reset iteration index, return the previous value of the index.
384  
385 =cut
386  
387 sub reset :method {
388 3 ▶ my ($self) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
389 3 return delete $self->{iter};
390 }
391  
392  
393 =head2 all
394  
395 Return all tuples available through the iterator.
396  
397 my @list = $iter->all;
398 my $list_aref = $iter->all;
399  
400 my @abc_list = map { $_->abc } $iter->all;
401 my @abc_list = $iter->all('abc'); # the same
402  
403  
404 my @list = map { [ $_->abc, $_->cde ] } $iter->all;
405 my @list = $iter->all('abc', 'cde'); # the same
406  
407  
408 my @list = map { $_->abc + $_->cde } $iter->all;
409 my @list = $iter->all(sub { $_[0]->abc + $_->cde }); # the same
410  
411 =cut
412  
413 sub all {
414 16 ▶ my ($self, @items) = @_;
Subroutine all: called CC=8 SCAR=20.8
Pod documented
415  
416 16 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 16 16
417 16 my @res;
418  
419 16 local $self->{iter};
420  
421  
422 16 ▶ if (@items == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@items == 1) { } 2 14 16
elsif (@items) { } 1 13 14
423 2 my $m = shift @items;
424  
425 2 ▶ while (defined(my $i = $self->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = $self->next)) 6 2 8
426 6 push @res => $i->$m;
427 }
428 } elsif (@items) {
429 1 ▶ while (defined(my $i = $self->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = $self->next)) 3 1 4
430 3 push @res => [ map { $i->$_ } @items ];
431 }
432 } else {
433 13 ▶ while (defined(my $i = $self->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = $self->next)) 56 13 69
434 56 push @res => $i;
435 }
436 }
437  
438 16 ▶ return @res if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 6 10 16
439 10 return \@res;
440 }
441  
442  
443  
444 =head2 item_class
445  
446 Set/return the tuple class. If the value is defined, the iterator
447 blesses tuples with it (and also calls L<item_constructor> if it is set).
448  
449 =cut
450  
451 sub item_class {
452 164 ▶ my ($self, $v, $m) = @_;
Subroutine item_class: called CC=4 SCAR=13.9
Pod documented
453 164 ▶ $self->item_constructor($m) if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 6 158 164
454 164 ▶ return $self->{item_class} = ref($v) || $v if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 25 139 164
Condition66%2/3
ref $v || $v
l!l&&r!l&&!r
0178
MC/DC50%1/2
ref $v || $v
ref $v $v
Truth table66%2/3
A || B
A= ref $vB= $v
ABresult
1X1
011
000
455 139 return $self->{item_class};
456 }
457  
458  
459 =head2 item_constructor
460  
461 Set/return the tuple constructor.
462 The value is used only if L<item_class> is defined.
463  
464 =cut
465  
466 sub item_constructor {
467 75 ▶ my ($self, $v) = @_;
Subroutine item_constructor: called CC=2 SCAR=6.9
Pod documented
468 75 ▶ return $self->{item_constructor} = $v if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 24 51 75
469 51 return $self->{item_constructor};
470 }
471  
472  
473 =head2 push
474  
475 Push a tuple into the iterator.
476  
477 =cut
478  
479 sub push :method {
480 2 ▶ my ($self, @i) = @_;
Subroutine push: called CC=1 SCAR=0.0
Pod documented
481 2 push @{ $self->{items}} => @i;
482 2 return $self;
483 }
484  
485  
486 =head2 data
487  
488 Return/set an application-specific context maintained in the iterator
489 object. This can be useful to pass additional state to B<item_constructor>.
490  
491 =cut
492  
493 sub data {
494 29 ▶ my ($self, $data) = @_;
Subroutine data: called CC=2 SCAR=6.9
Pod documented
495 29 ▶ $self->{data} = $data if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 18 11 29
496 29 return $self->{data};
497 }
498  
499 8 1;
« blib/lib/DR/Tarantool/MsgPack/CoroClient.pm ↑ Summary blib/lib/DR/Tarantool/MsgPack.pm »