Mango/Cursor.pm

filter: Statementstmt 13.0% 9 / 69 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 10.0% 2 / 20 Podpod 100% 5 / 5 totaltotal 9.9% 16 / 161 CC 25 SCAR 26.2
CC
25
Cov
7%
_continue
30.0
num_to_return
20.0
rewind
20.0
CRAP 13.8
« Mango/Collection.pm ↑ Summary Mango/GridFS/Reader.pm »
1 package Mango::Cursor;
2 9 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 has [qw(collection id ns)];
7 9 has [qw(batch_size limit)] => 0;
8  
9 0 ▶sub add_batch {
Subroutine add_batch: not called CC=2 SCAR=17.9
Pod documented
10 0 my ($self, $docs) = @_;
11 0 ▶ push @{$self->{results} ||= []}, @$docs;
Condition0%0/2
$self->{"results"} ||= []
l!l
00
MC/DC0%0/1
$self->{"results"} ||= []
$self->{"results"}
Truth table0%0/2
A
A= $self->{"results"}
Aresult
11
00
12 0 return $self;
13 }
14  
15 0 ▶sub all {
Subroutine all: not called CC=3 SCAR=24.8
Pod documented
16 0 my ($self, $cb) = @_;
17  
18 # Non-blocking
19 0 my @all;
20 0 ▶ return $self->next(sub { shift->_collect(\@all, $cb, @_) }) if $cb;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
21  
22 # Blocking
23 0 ▶ while (my $next = $self->next) { push @all, $next }
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $self->next) 0 0 0
24 0 return \@all;
25 }
26  
27 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
28 0 my ($self, $cb) = @_;
29 0 ▶ return defined $self->id ? $self->_continue($cb) : $self->_start($cb);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->id ? : 0 0 0
30 }
31  
32 0 ▶sub num_to_return {
Subroutine num_to_return: not called CC=4 SCAR=30.0
Pod documented
33 0 my $self = shift;
34 0 my $limit = $self->limit;
35 0 my $size = $self->batch_size;
36 0 ▶ return $limit == 0 || ($size > 0 && $size < $limit) ? $size : $limit;
Branch: 1 branch
BranchTrueFalseTotal
$limit == 0 || $size > 0 && $size < $limit ? : 0 0 0
Condition0%0/6
$limit == 0 or $size > 0 and $size < $limit
l!l&&r!l&&!r
000
$size > 0 and $size < $limit
!ll&&!rl&&r
000
MC/DC0%0/3
$limit == 0 or $size > 0 and $size < $limit
$limit == 0 $size > 0 $size < $limit
Truth table0%0/4
A or B and C
A= $limit == 0B= $size > 0C= $size < $limit
ABCresult
1XX1
0111
00X0
0100
37 }
38  
39 0 ▶sub rewind {
Subroutine rewind: not called CC=4 SCAR=30.0
Pod documented
40 0 my ($self, $cb) = @_;
41  
42 0 delete @$self{qw(num results)};
43 0 ▶ return $cb ? $self->_defer($cb) : undef unless defined(my $id = $self->id);
Branch: 2 branches
BranchTrueFalseTotal
unless defined(my $id = $self->id) 0 0 0
$cb ? : 0 0 0
44 0 $self->id(undef);
45  
46 # Non-blocking
47 0 my $mango = $self->collection->db->mango;
48 0 ▶ return $mango->kill_cursors($id => sub { shift; $self->$cb(@_) }) if $cb;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
49  
50 # Blocking
51 0 $mango->kill_cursors($id);
52 }
53  
54 0 ▶sub _collect {
Subroutine _collect: not called CC=3 SCAR=24.8
55 0 my ($self, $all, $cb, $err, $doc) = @_;
56 0 ▶ return $self->_defer($cb, $err, $all) if $err || !$doc;
Branch: 1 branch
BranchTrueFalseTotal
if $err or not $doc 0 0 0
Condition0%0/3
$err or not $doc
l!l&&r!l&&!r
000
MC/DC0%0/2
$err or not $doc
$err not $doc
Truth table0%0/3
A or not(B)
A= $errB= not $doc
ABresult
1X1
011
000
57 0 push @$all, $doc;
58 0 ▶ $self->next(sub { shift->_collect($all, $cb, @_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 }
60  
61 0 ▶sub _continue {
Subroutine _continue: not called CC=5 SCAR=34.0
62 0 my ($self, $cb) = @_;
63  
64 0 my $collection = $self->collection;
65 0 ▶ my $name = $self->ns // $collection->full_name;
Condition0%0/3
$self->ns // $collection->full_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->ns // $collection->full_name
$self->ns $collection->full_name
Truth table0%0/3
A // B
A= $self->nsB= $collection->full_name
ABresult
1X1
011
000
66 0 my $mango = $collection->db->mango;
67  
68 # Non-blocking
69 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
70 0 ▶ return $self->_defer($cb, undef, $self->_dequeue) if $self->_enough;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_enough 0 0 0
71 return $mango->get_more(($name, $self->num_to_return, $self->id) =>
72 0 ▶ sub { shift; $self->$cb(shift, $self->_enqueue(shift)) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 }
74  
75 # Blocking
76 0 ▶ return $self->_dequeue if $self->_enough;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_enough 0 0 0
77 0 return $self->_enqueue(
78 $mango->get_more($name, $self->num_to_return, $self->id));
79 }
80  
81 0 ▶sub _defer {
Subroutine _defer: not called CC=1 SCAR=6.9
82 0 my ($self, $cb, @args) = @_;
83 0 ▶ Mojo::IOLoop->next_tick(sub { $self->$cb(@args) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 }
85  
86 0 ▶sub _dequeue {
Subroutine _dequeue: not called CC=2 SCAR=17.9
87 0 my $self = shift;
88 0 ▶ return undef if $self->_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_finished 0 0 0
89 0 $self->{num}++;
90 0 return shift @{$self->{results}};
91 }
92  
93 0 ▶sub _enough {
Subroutine _enough: not called CC=4 SCAR=30.0
94 0 my $self = shift;
95 0 ▶ return $self->id eq '0' || $self->_finished || !!@{$self->{results} // []};
Condition0%0/8
$self->id eq '0' || $self->_finished || !!@{$self->{'results'} // [];}
l!l&&r!l&&!r
000
$self->id eq 0 or $self->_finished
l!l&&r!l&&!r
000
$self->{"results"} // []
l!l
00
MC/DC0%0/3
$self->id eq '0' || $self->_finished || !!@{$self->{'results'} // [];}
$self->id eq 0 $self->_finished !!@{$self->{'results'} // [];}
MC/DC0%0/1
$self->{"results"} // []
$self->{"results"}
Truth table0%0/4
A or B || C
A= $self->id eq 0B= $self->_finishedC= !!@{$self->{'results'} // [];}
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $self->{"results"}
Aresult
11
00
96 }
97  
98 0 ▶sub _enqueue {
Subroutine _enqueue: not called CC=2 SCAR=17.9
99 0 my ($self, $reply) = @_;
100 0 ▶ return undef unless $reply;
Branch: 1 branch
BranchTrueFalseTotal
unless $reply 0 0 0
101 0 return $self->add_batch($reply->{docs})->id($reply->{cursor})->_dequeue;
102 }
103  
104 0 ▶sub _finished {
Subroutine _finished: not called CC=4 SCAR=30.0
105 0 my $self = shift;
106 0 ▶ return undef unless my $limit = $self->limit;
Branch: 1 branch
BranchTrueFalseTotal
unless my $limit = $self->limit 0 0 0
107 0 ▶ return ($self->{num} // 0) >= abs($limit) ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
($self->{'num'} // 0) >= abs $limit ? : 0 0 0
Condition0%0/2
$self->{"num"} // 0
l!l
00
MC/DC0%0/1
$self->{"num"} // 0
$self->{"num"}
Truth table0%0/2
A
A= $self->{"num"}
Aresult
11
00
108 }
109  
110 0 ▶sub _start { die 'Cursor cannot be restarted' }
Subroutine _start: not called CC=1 SCAR=6.9
111  
112 9 1;
113  
114 =encoding utf8
115  
116 =head1 NAME
117  
118 Mango::Cursor - MongoDB cursor
119  
120 =head1 SYNOPSIS
121  
122 use Mango::Cursor;
123  
124 my $cursor = Mango::Cursor->new(collection => $collection);
125 my $docs = $cursor->all;
126  
127 =head1 DESCRIPTION
128  
129 L<Mango::Cursor> is a container for MongoDB cursors used by
130 L<Mango::Collection>.
131  
132 =head1 ATTRIBUTES
133  
134 L<Mango::Cursor> implements the following attributes.
135  
136 =head2 batch_size
137  
138 my $size = $cursor->batch_size;
139 $cursor = $cursor->batch_size(10);
140  
141 Number of documents to fetch in one batch, defaults to C<0>.
142  
143 =head2 collection
144  
145 my $collection = $cursor->collection;
146 $cursor = $cursor->collection(Mango::Collection->new);
147  
148 L<Mango::Collection> object this cursor belongs to.
149  
150 =head2 id
151  
152 my $id = $cursor->id;
153 $cursor = $cursor->id(123456);
154  
155 Cursor id.
156  
157 =head2 limit
158  
159 my $limit = $cursor->limit;
160 $cursor = $cursor->limit(10);
161  
162 Limit the number of documents, defaults to C<0>.
163  
164 =head1 METHODS
165  
166 L<Mango::Cursor> inherits all methods from L<Mojo::Base> and implements the
167 following new ones.
168  
169 =head2 add_batch
170  
171 $cursor = $cursor->add_batch($docs);
172  
173 Add batch of documents to cursor.
174  
175 =head2 all
176  
177 my $docs = $cursor->all;
178  
179 Fetch all documents at once. You can also append a callback to perform
180 operation non-blocking.
181  
182 $cursor->all(sub {
183 my ($cursor, $err, $docs) = @_;
184 ...
185 });
186 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
187  
188 =head2 next
189  
190 my $doc = $cursor->next;
191  
192 Fetch next document. You can also append a callback to perform operation
193 non-blocking.
194  
195 $cursor->next(sub {
196 my ($cursor, $err, $doc) = @_;
197 ...
198 });
199 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
200  
201 =head2 rewind
202  
203 $cursor->rewind;
204  
205 Rewind cursor and kill it on the server. You can also append a callback to
206 perform operation non-blocking.
207  
208 $cursor->rewind(sub {
209 my ($cursor, $err) = @_;
210 ...
211 });
212 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
213  
214 =head2 num_to_return
215  
216 my $num = $cursor->num_to_return;
217  
218 Number of results to return with next C<QUERY> or C<GET_MORE> operation based
219 on L</"batch_size"> and L</"limit">.
220  
221 =head1 SEE ALSO
222  
223 L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
224  
225 =cut
« Mango/Collection.pm ↑ Summary Mango/GridFS/Reader.pm »