Mango.pm

filter: Statementstmt 21.4% 48 / 224 Branchbran 0.0% 0 / 102 Conditioncond 1.6% 1 / 62 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 25.5% 12 / 47 Podpod 100% 7 / 7 totaltotal 14.2% 68 / 476 CC 81 SCAR 43.0
CC
81
Cov
13%
from_string
210.0
_write
210.0
_next
132.0
CRAP 73.7
↑ Summary Mango/Cursor/Query.pm »
1 package Mango;
2 9 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Hash::Util::FieldHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Mango::BSON 'bson_doc';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Mango::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Mango::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Mojo::Util 'dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use constant DEBUG => $ENV{MANGO_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MANGO_DEBUG'} || 0
l!l
09
MC/DC0%0/1
$ENV{'MANGO_DEBUG'} || 0
$ENV{'MANGO_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MANGO_DEBUG'}
Aresult
11
00
15 9 ▶use constant DEFAULT_PORT => 27017;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 has default_db => 'admin';
18 0 ▶has hosts => sub { [['localhost']] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 9 has [qw(inactivity_timeout j)] => 0;
20 0 ▶has ioloop => sub { Mojo::IOLoop->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 9 has max_bson_size => 16777216;
22 9 has max_connections => 5;
23 9 has [qw(max_write_batch_size wtimeout)] => 1000;
24 0 ▶has protocol => sub { Mango::Protocol->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 9 has w => 1;
26  
27 # Private variables are not visible in the object's dump. This
28 # is good for security.
29 9 Hash::Util::FieldHash::fieldhash my %AUTH;
30  
31 9 our $VERSION = '1.27';
32  
33 0 ▶sub DESTROY { shift->_cleanup }
Subroutine DESTROY: not called CC=1 SCAR=6.9
34  
35 0 ▶sub backlog { scalar @{shift->{queue} || []} }
Subroutine backlog: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"queue"} or [] 0 0 0
36  
37 sub db {
38 0 ▶ my ($self, $name) = @_;
Subroutine db: not called CC=2 SCAR=17.9
Pod documented
39 0 ▶ $name //= $self->default_db;
Condition0%0/3
$name //= $self->default_db
l!l&&r!l&&!r
000
MC/DC0%0/2
$name //= $self->default_db
$name $self->default_db
Truth table0%0/3
A //= B
A= $nameB= $self->default_db
ABresult
1X1
011
000
40 0 my $db = Mango::Database->new(mango => $self, name => $name);
41 0 weaken $db->{mango};
42 0 return $db;
43 }
44  
45 sub from_string {
46 0 ▶ my ($self, $str) = @_;
Subroutine from_string: not called CC=14 SCAR=53.5
Pod documented
47  
48 # Protocol
49 0 ▶ return $self unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 0 0
50 0 my $url = Mojo::URL->new($str);
51 0 ▶ croak qq{Invalid MongoDB connection string "$str"}
Branch: 1 branch
BranchTrueFalseTotal
unless $url->protocol eq "mongodb" 0 0 0
52 unless $url->protocol eq 'mongodb';
53  
54 # Hosts
55 0 my @hosts;
56 /^([^,:]+)(?::(\d+))?/ and push @hosts, $2 ? [$1, $2] : [$1]
57 0 ▶ for split /,/, join(':', map { $_ // '' } $url->host, $url->port);
Branch: 2 branches
BranchTrueFalseTotal
/^([^,:]+)(?::(\d+))?/u and push @hosts, $2 ? [$1, $2] : [$1] 0 0 0
$2 ? : 0 0 0
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
58 0 ▶ $self->hosts(\@hosts) if @hosts;
Branch: 1 branch
BranchTrueFalseTotal
if @hosts 0 0 0
59  
60 # Database
61 0 ▶ if (my $db = $url->path->parts->[0]) { $self->default_db($db) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $db = $url->path->parts->[0]) 0 0 0
62  
63 # User and password
64 0 ▶ if (($url->userinfo // '') =~ /^([^:]+):([^:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($url->userinfo // "") =~ /^([^:]+):([^:]+)$/u) 0 0 0
Condition0%0/2
$url->userinfo // ""
l!l
00
MC/DC0%0/1
$url->userinfo // ""
$url->userinfo
Truth table0%0/2
A
A= $url->userinfo
Aresult
11
00
65 0 require Mango::Auth::SCRAM;
66 0 $self->_auth(Mango::Auth::SCRAM->new)
67 ->_auth->_credentials([$self->default_db, $1, $2]);
68 }
69  
70 # Options
71 0 my $query = $url->query;
72 0 ▶ if (my $j = $query->param('journal')) { $self->j($j) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $j = $query->param("journal")) 0 0 0
73 0 ▶ if (my $w = $query->param('w')) { $self->w($w) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $query->param("w")) 0 0 0
74 0 ▶ if (my $timeout = $query->param('wtimeoutMS')) { $self->wtimeout($timeout) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout = $query->param("wtimeoutMS")) 0 0 0
75  
76 0 return $self;
77 }
78  
79 0 ▶sub get_more { shift->_op('get_more', 1, @_) }
Subroutine get_more: not called CC=1 SCAR=6.9
Pod documented
80  
81 0 ▶sub kill_cursors { shift->_op('kill_cursors', 0, @_) }
Subroutine kill_cursors: not called CC=1 SCAR=6.9
Pod documented
82  
83 0 ▶sub new { shift->SUPER::new->from_string(@_) }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
84  
85 0 ▶sub query { shift->_op('query', 1, @_) }
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
86  
87 sub _auth {
88 0 ▶ my ($self, $mode) = @_;
Subroutine _auth: not called CC=2 SCAR=17.9
89 0 ▶ return $AUTH{$self} unless $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode 0 0 0
90  
91 0 $AUTH{$self} = $mode;
92 0 $AUTH{$self}->mango($self);
93 0 weaken $AUTH{$self}->{mango};
94 0 return $self;
95 }
96  
97 sub _build {
98 0 ▶ my ($self, $name) = (shift, shift);
Subroutine _build: not called CC=1 SCAR=6.9
99 0 my $next = $self->_id;
100 0 warn "-- Operation #$next ($name)\n@{[dumper [@_]]}" if DEBUG;
101 0 my $method = "build_$name";
102 0 return ($next, $self->protocol->$method($next, @_));
103 }
104  
105 sub _cleanup {
106 0 ▶ my $self = shift;
Subroutine _cleanup: not called CC=9 SCAR=45.0
107 0 ▶ return unless $self->_loop(0);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_loop(0) 0 0 0
108  
109 # Clean up connections
110 0 delete $self->{pid};
111 0 my $connections = delete $self->{connections};
112 0 for my $c (keys %$connections) {
113 0 my $loop = $self->_loop($connections->{$c}{nb});
114 0 ▶ $loop->remove($c) if $loop;
Branch: 1 branch
BranchTrueFalseTotal
if $loop 0 0 0
115 }
116  
117 # Clean up active operations
118 0 ▶ my $queue = delete $self->{queue} || [];
Condition0%0/2
delete $self->{'queue'} || []
l!l
00
MC/DC0%0/1
delete $self->{'queue'} || []
delete $self->{'queue'}
Truth table0%0/2
A
A= delete $self->{'queue'}
Aresult
11
00
119 $_->{last} && !$_->{start} && unshift @$queue, $_->{last}
120 0 ▶ for values %$connections;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"last"} and not $_->{"start"} and unshift @$queue, $_->{"last"} 0 0 0
Condition0%0/3
$_->{"last"} and not $_->{"start"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{"last"} and not $_->{"start"}
$_->{"last"} not $_->{"start"}
Truth table0%0/3
A and not(B)
A= $_->{"last"}B= not $_->{"start"}
ABresult
0X0
100
111
121 0 $self->_finish(undef, $_->{cb}, 'Premature connection close') for @$queue;
122 }
123  
124 sub _close {
125 0 ▶ my ($self, $id) = @_;
Subroutine _close: not called CC=4 SCAR=30.0
126  
127 0 ▶ return unless my $c = delete $self->{connections}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless my $c = delete $self->{"connections"}{$id} 0 0 0
128 0 my $last = $c->{last};
129 0 ▶ $self->_finish(undef, $last->{cb}, 'Premature connection close') if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 0 0 0
130 0 ▶ $self->_connect($c->{nb}) if @{$self->{queue}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"queue"};} 0 0 0
131 }
132  
133 sub _connect {
134 0 ▶ my ($self, $nb, $hosts) = @_;
Subroutine _connect: not called CC=3 SCAR=24.8
135  
136 0 ▶ my ($host, $port) = @{shift @{$hosts ||= [@{$self->hosts}]}};
Condition0%0/2
$hosts ||= [@{$self->hosts;}]
l!l
00
MC/DC0%0/1
$hosts ||= [@{$self->hosts;}]
$hosts
Truth table0%0/2
A
A= $hosts
Aresult
11
00
137 0 weaken $self;
138 0 my $id;
139 $id = $self->_loop($nb)->client(
140 {address => $host, port => $port //= DEFAULT_PORT} => sub {
141 0 ▶ my ($loop, $err, $stream) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
142  
143 # Connection error (try next server)
144 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
145 0 ▶ return $self->_error($id, $err) unless @$hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @$hosts 0 0 0
146 0 delete $self->{connections}{$id};
147 0 return $self->_connect($nb, $hosts);
148 }
149  
150 # Connection established
151 0 $stream->timeout($self->inactivity_timeout);
152 0 ▶ $stream->on(close => sub { $self && $self->_close($id) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_close($id) 0 0 0
153 0 ▶ $stream->on(error => sub { $self && $self->_error($id, pop) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_error($id, pop()) 0 0 0
154 0 ▶ $stream->on(read => sub { $self->_read($id, pop) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
155  
156 # Check node information with "isMaster" command
157 0 ▶ my $cb = sub { shift->_master($id, $nb, $hosts, pop) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 $self->_fast($id, $self->default_db, {isMaster => 1}, $cb);
159 }
160 0 ▶ );
Condition0%0/2
$port //= 27017
l!l
00
MC/DC0%0/1
$port //= 27017
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
161 0 $self->{connections}{$id} = { nb => $nb, start => 1 };
162  
163 0 my $num = scalar keys %{$self->{connections}};
164 0 warn "-- New connection ($host:$port:$num)\n" if DEBUG;
165 }
166  
167 sub _error {
168 0 ▶ my ($self, $id, $err) = @_;
Subroutine _error: not called CC=4 SCAR=30.0
169  
170 0 ▶ return unless my $c = delete $self->{connections}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless my $c = delete $self->{"connections"}{$id} 0 0 0
171 0 $self->_loop($c->{nb})->remove($id);
172  
173 0 ▶ my $last = $c->{last} // shift @{$self->{queue}};
Condition0%0/3
$c->{"last"} // shift @{$$self{"queue"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"last"} // shift @{$$self{"queue"};}
$c->{"last"} shift @{$$self{"queue"};}
Truth table0%0/3
A // B
A= $c->{"last"}B= shift @{$$self{"queue"};}
ABresult
1X1
011
000
174 0 ▶ $self->_finish(undef, $last->{cb}, $err) if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 0 0 0
175 }
176  
177 sub _fast {
178 0 ▶ my ($self, $id, $db, $command, $cb) = @_;
Subroutine _fast: not called CC=1 SCAR=6.9
179  
180 # Handle errors
181 my $wrapper = sub {
182 0 ▶ my ($self, $err, $reply) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
183  
184 0 my $doc = $reply->{docs}[0];
185 0 ▶ $err ||= $self->protocol->command_error($doc);
Condition0%0/3
$err ||= $self->protocol->command_error($doc)
l!l&&r!l&&!r
000
MC/DC0%0/2
$err ||= $self->protocol->command_error($doc)
$err $self->protocol->command_error($doc)
Truth table0%0/3
A ||= B
A= $errB= $self->protocol->command_error($doc)
ABresult
1X1
011
000
186 0 ▶ return $self->$cb(undef, $doc) unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
187  
188 0 ▶ return unless my $last = shift @{$self->{queue}};
Branch: 1 branch
BranchTrueFalseTotal
unless my $last = shift @{$$self{"queue"};} 0 0 0
189 0 $self->_finish(undef, $last->{cb}, $err);
190 0 };
191  
192 # Skip the queue and run command right away
193 0 my ($next, $msg)
194 = $self->_build('query', "$db.\$cmd", {}, 0, -1, $command, {});
195 $self->{connections}{$id}{fast}
196 0 = {id => $next, safe => 1, msg => $msg, cb => $wrapper};
197 0 $self->_next;
198 }
199  
200 sub _finish {
201 0 ▶ my ($self, $reply, $cb, $err) = @_;
Subroutine _finish: not called CC=2 SCAR=17.9
202 0 ▶ $self->$cb($err || $self->protocol->query_failure($reply), $reply);
Condition0%0/3
$err || $self->protocol->query_failure($reply)
l!l&&r!l&&!r
000
MC/DC0%0/2
$err || $self->protocol->query_failure($reply)
$err $self->protocol->query_failure($reply)
Truth table0%0/3
A || B
A= $errB= $self->protocol->query_failure($reply)
ABresult
1X1
011
000
203 }
204  
205 0 ▶sub _id { $_[0]{id} = $_[0]->protocol->next_id($_[0]{id} // 0) }
Subroutine _id: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]{"id"} // 0
l!l
00
MC/DC0%0/1
$_[0]{"id"} // 0
$_[0]{"id"}
Truth table0%0/2
A
A= $_[0]{"id"}
Aresult
11
00
206  
207 0 ▶sub _loop { $_[1] ? Mojo::IOLoop->singleton : $_[0]->ioloop }
Subroutine _loop: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
208  
209 sub _master {
210 0 ▶ my ($self, $id, $nb, $hosts, $doc) = @_;
Subroutine _master: not called CC=8 SCAR=42.8
211  
212 # Check version
213 return $self->_error($id, 'MongoDB version 3.0 required')
214 0 ▶ unless ($doc->{maxWireVersion} || 0) >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc->{"maxWireVersion"} || 0) >= 3 0 0 0
Condition0%0/2
$doc->{'maxWireVersion'} || 0
l!l
00
MC/DC0%0/1
$doc->{'maxWireVersion'} || 0
$doc->{'maxWireVersion'}
Truth table0%0/2
A
A= $doc->{'maxWireVersion'}
Aresult
11
00
215  
216 # Continue with authentication if we are connected to the primary
217 0 ▶ if ($doc->{ismaster}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{"ismaster"}) 0 0 0
218 0 ▶ return $self->_auth
Branch: 1 branch
BranchTrueFalseTotal
$self->_auth ? : 0 0 0
219 ? $self->_auth->_authenticate($id)
220 : $self->emit(connection => $id)->_next;
221 }
222  
223 # Get primary and try to connect again
224 0 ▶ unshift @$hosts, [$1, $2] if ($doc->{primary} // '') =~ /^(.+):(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{"primary"} // "") =~ /^(.+):(\d+)$/u 0 0 0
Condition0%0/2
$doc->{"primary"} // ""
l!l
00
MC/DC0%0/1
$doc->{"primary"} // ""
$doc->{"primary"}
Truth table0%0/2
A
A= $doc->{"primary"}
Aresult
11
00
225 0 ▶ return $self->_error($id, "Couldn't find primary node") unless @$hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @$hosts 0 0 0
226 0 delete $self->{connections}{$id};
227 0 $self->_loop($nb)->remove($id);
228 0 $self->_connect($nb, $hosts);
229 }
230  
231 sub _next {
232 0 ▶ my ($self, $op) = @_;
Subroutine _next: not called CC=11 SCAR=48.8
233  
234 # Make sure all connections are saturated
235 0 ▶ push @{$self->{queue} ||= []}, $op if $op;
Branch: 1 branch
BranchTrueFalseTotal
if $op 0 0 0
Condition0%0/2
$self->{"queue"} ||= []
l!l
00
MC/DC0%0/1
$self->{"queue"} ||= []
$self->{"queue"}
Truth table0%0/2
A
A= $self->{"queue"}
Aresult
11
00
236 0 my $connections = $self->{connections};
237 0 my $start;
238 0 ▶ $self->_write($_) and $start++ for keys %$connections;
Branch: 1 branch
BranchTrueFalseTotal
$self->_write($_) and ++$start 0 0 0
239  
240 # Check if we need a blocking connection
241 0 ▶ return unless $op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 0 0 0
242 0 my @ids = keys %$connections;
243 return $self->_connect(0)
244 0 ▶ if !$op->{nb} && !grep { !$connections->{$_}{nb} } @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->{"nb"} or grep {not $connections->{$_}{"nb"};} @ids 0 0 0
Condition0%0/3
$op->{"nb"} or grep {not $connections->{$_}{"nb"};} @ids
l!l&&r!l&&!r
000
MC/DC0%0/2
$op->{"nb"} or grep {not $connections->{$_}{"nb"};} @ids
$op->{"nb"} grep {not $connections->{$_}{"nb"};} @ids
Truth table0%0/3
A or B
A= $op->{"nb"}B= grep {not $connections->{$_}{"nb"};} @ids
ABresult
1X1
011
000
245  
246 # Check if we need more non-blocking connections
247 $self->_connect(1)
248 0 ▶ if !$start && @{$self->{queue}} && @ids < $self->max_connections;
Branch: 1 branch
BranchTrueFalseTotal
if not $start and @{$self->{"queue"};} and @ids < $self->max_connections 0 0 0
Condition0%0/6
not $start and @{$self->{"queue"};} and @ids < $self->max_connections
!ll&&!rl&&r
000
not $start and @{$self->{"queue"};}
!ll&&!rl&&r
000
MC/DC0%0/3
not $start and @{$self->{"queue"};} and @ids < $self->max_connections
not $start @{$self->{"queue"};} @ids < $self->max_connections
Truth table0%0/4
not(A) and B and C
A= not $startB= @{$self->{"queue"};}C= @ids < $self->max_connections
ABCresult
0XX0
10X0
1100
1111
249 }
250  
251 sub _op {
252 0 ▶ my ($self, $op, $safe) = (shift, shift, shift);
Subroutine _op: not called CC=2 SCAR=17.9
253 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
254 0 my ($next, $msg) = $self->_build($op, @_);
255 0 $self->_start(
256 {id => $next, safe => $safe, msg => $msg, nb => !!$cb, cb => $cb});
257 }
258  
259 sub _read {
260 0 ▶ my ($self, $id, $chunk) = @_;
Subroutine _read: not called CC=3 SCAR=24.8
261  
262 0 my $c = $self->{connections}{$id};
263 0 $c->{buffer} .= $chunk;
264 0 ▶ while (my $reply = $self->protocol->parse_reply(\$c->{buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reply = $self->protocol->parse_reply(\$c->{"buffer"})) 0 0 0
265 0 warn "-- Client <<< Server (#$reply->{to})\n@{[dumper $reply]}" if DEBUG;
266 0 ▶ next unless $reply->{to} == $c->{last}{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $reply->{"to"} == $c->{"last"}{"id"} 0 0 0
267 0 $self->_finish($reply, (delete $c->{last})->{cb});
268 }
269 0 $self->_next;
270 }
271  
272 sub _start {
273 0 ▶ my ($self, $op) = @_;
Subroutine _start: not called CC=5 SCAR=34.0
274  
275 # Fork safety
276 0 ▶ $self->_cleanup unless ($self->{pid} //= $$) eq $$;
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pid"} //= $$) eq $$ 0 0 0
Condition0%0/3
$self->{"pid"} //= $$
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pid"} //= $$
$self->{"pid"} $$
Truth table0%0/3
A //= B
A= $self->{"pid"}B= $$
ABresult
1X1
011
000
277  
278 # Non-blocking
279 0 ▶ return $self->_next($op) if $op->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $op->{"cb"} 0 0 0
280  
281 # Blocking
282 0 my ($err, $reply);
283 0 ▶ $op->{cb} = sub { shift->ioloop->stop; ($err, $reply) = @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
284 0 $self->_next($op);
285 0 $self->ioloop->start;
286 0 ▶ return $err ? croak $err : $reply;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
287 }
288  
289 sub _write {
290 0 ▶ my ($self, $id) = @_;
Subroutine _write: not called CC=14 SCAR=53.5
291  
292 # Make sure connection has not been corrupted while event loop was stopped
293 0 my $c = $self->{connections}{$id};
294 0 ▶ return $c->{start} if $c->{last};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"last"} 0 0 0
295 0 my $loop = $self->_loop($c->{nb});
296 0 ▶ return undef unless my $stream = $loop->stream($id);
Branch: 1 branch
BranchTrueFalseTotal
unless my $stream = $loop->stream($id) 0 0 0
297 0 ▶ if (!$loop->is_running && $stream->is_readable) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $loop->is_running and $stream->is_readable) 0 0 0
Condition0%0/3
not $loop->is_running and $stream->is_readable
!ll&&!rl&&r
000
MC/DC0%0/2
not $loop->is_running and $stream->is_readable
not $loop->is_running $stream->is_readable
Truth table0%0/3
not(A) and B
A= not $loop->is_runningB= $stream->is_readable
ABresult
0X0
100
111
298 0 $stream->close;
299 0 return undef;
300 }
301  
302 # Fast operation
303 0 ▶ delete $c->{start} unless my $last = delete $c->{fast};
Branch: 1 branch
BranchTrueFalseTotal
unless my $last = delete $c->{"fast"} 0 0 0
304  
305 # Blocking operations have a higher precedence
306 return $c->{start}
307 0 ▶ unless $last || ($c->{nb} xor !($self->{queue}->[-1] || {})->{nb});
Branch: 1 branch
BranchTrueFalseTotal
unless $last or ($c->{"nb"} xor not +($self->{"queue"}[-1] || {})->{"nb"}) 0 0 0
Condition0%0/9
$last or ($c->{"nb"} xor not +($self->{"queue"}[-1] || {})->{"nb"})
l!l&&r!l&&!r
000
$c->{"nb"} xor not +($self->{"queue"}[-1] || {})->{"nb"}
l&&rl&&!r!l&&r!l&&!r
0000
$self->{'queue'}[-1] || {}
l!l
00
MC/DC0%0/3
$last or ($c->{"nb"} xor not +($self->{"queue"}[-1] || {})->{"nb"})
$last $c->{"nb"} $self->{'queue'}[-1]
Truth table0%0/5
A or B xor not(C)
A= $lastB= $c->{"nb"}C= $self->{'queue'}[-1]
ABCresult
1XX1
0111
0001
0100
0010
308 0 ▶ $last ||= $c->{nb} ? shift @{$self->{queue}} : pop @{$self->{queue}};
Branch: 1 branch
BranchTrueFalseTotal
$c->{'nb'} ? : 0 0 0
Condition0%0/3
$last ||= $c->{"nb"} ? shift @{$$self{"queue"};} : pop @{$$self{"queue"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$last ||= $c->{"nb"} ? shift @{$$self{"queue"};} : pop @{$$self{"queue"};}
$last $c->{"nb"} ? shift @{$$self{"queue"};} : pop @{$$self{"queue"};}
Truth table0%0/3
A ||= B
A= $lastB= $c->{"nb"} ? shift @{$$self{"queue"};} : pop @{$$self{"queue"};}
ABresult
1X1
011
000
309  
310 0 ▶ return $c->{start} unless $c->{last} = $last;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"last"} = $last 0 0 0
311 0 warn "-- Client >>> Server (#$last->{id})\n" if DEBUG;
312 0 $stream->write(delete $last->{msg});
313  
314 # Unsafe operations are done when they are written
315 0 ▶ return $c->{start} if $last->{safe};
Branch: 1 branch
BranchTrueFalseTotal
if $last->{"safe"} 0 0 0
316 0 weaken $self;
317 0 ▶ $stream->write('', sub { $self->_finish(undef, delete($c->{last})->{cb}) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
318 0 return $c->{start};
319 }
320  
321 9 1;
322  
323 =encoding utf8
324  
325 =head1 NAME
326  
327 Mango - Pure-Perl non-blocking I/O MongoDB driver
328  
329 =head1 SYNOPSIS
330  
331 use Mango;
332  
333 # Insert document
334 my $mango = Mango->new('mongodb://localhost:27017');
335 my $oid = $mango->db('test')->collection('foo')->insert({bar => 'baz'});
336  
337 # Find document
338 my $doc = $mango->db('test')->collection('foo')->find_one({bar => 'baz'});
339 say $doc->{bar};
340  
341 # Update document
342 $mango->db('test')->collection('foo')
343 ->update({bar => 'baz'}, {bar => 'yada'});
344  
345 # Remove document
346 $mango->db('test')->collection('foo')->remove({bar => 'yada'});
347  
348 # Insert document with special BSON types
349 use Mango::BSON ':bson';
350 my $oid = $mango->db('test')->collection('foo')
351 ->insert({data => bson_bin("\x00\x01"), now => bson_time});
352  
353 # Non-blocking concurrent find
354 my $delay = Mojo::IOLoop->delay(sub {
355 my ($delay, @docs) = @_;
356 ...
357 });
358 for my $name (qw(sri marty)) {
359 my $end = $delay->begin(0);
360 $mango->db('test')->collection('users')->find({name => $name})->all(sub {
361 my ($cursor, $err, $docs) = @_;
362 $end->(@$docs);
363 });
364 }
365 $delay->wait;
366  
367 # Event loops such as AnyEvent are supported through EV
368 use EV;
369 use AnyEvent;
370 my $cv = AE::cv;
371 $mango->db('test')->command(buildInfo => sub {
372 my ($db, $err, $doc) = @_;
373 $cv->send($doc->{version});
374 });
375 say $cv->recv;
376  
377 =head1 DESCRIPTION
378  
379 L<Mango> is a pure-Perl non-blocking I/O MongoDB driver, optimized for use
380 with the L<Mojolicious> real-time web framework, and with multiple event loop
381 support. Since MongoDB is still changing rapidly, only the latest stable
382 version is supported.
383  
384 For MongoDB 2.6 support, use L<Mango> 1.16.
385  
386 To learn more about MongoDB you should take a look at the
387 L<official documentation|http://docs.mongodb.org>, the documentation included
388 in this distribution is no replacement for it.
389  
390 Look at L<Mango::Collection> for CRUD operations.
391  
392 Many arguments passed to methods as well as values of attributes get
393 serialized to BSON with L<Mango::BSON>, which provides many helper functions
394 you can use to generate data types that are not available natively in Perl.
395 All connections will be reset automatically if a new process has been forked,
396 this allows multiple processes to share the same L<Mango> object safely.
397  
398 For better scalability (epoll, kqueue) and to provide IPv6, SOCKS5 as well as
399 TLS support, the optional modules L<EV> (4.0+), L<IO::Socket::IP> (0.20+),
400 L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL> (1.84+) will be used
401 automatically if they are installed. Individual features can also be disabled
402 with the C<MOJO_NO_IPV6>, C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment
403 variables.
404  
405 =head1 EVENTS
406  
407 L<Mango> inherits all events from L<Mojo::EventEmitter> and can emit the
408 following new ones.
409  
410 =head2 connection
411  
412 $mango->on(connection => sub {
413 my ($mango, $id) = @_;
414 ...
415 });
416  
417 Emitted when a new connection has been established.
418  
419 =head1 ATTRIBUTES
420  
421 L<Mango> implements the following attributes.
422  
423 =head2 default_db
424  
425 my $name = $mango->default_db;
426 $mango = $mango->default_db('test');
427  
428 Default database, defaults to C<admin>.
429  
430 =head2 hosts
431  
432 my $hosts = $mango->hosts;
433 $mango = $mango->hosts([['localhost', 3000], ['localhost', 4000]]);
434  
435 Servers to connect to, defaults to C<localhost> and port C<27017>.
436  
437 =head2 inactivity_timeout
438  
439 my $timeout = $mango->inactivity_timeout;
440 $mango = $mango->inactivity_timeout(15);
441  
442 Maximum amount of time in seconds a connection can be inactive before getting
443 closed, defaults to C<0>. Setting the value to C<0> will allow connections to
444 be inactive indefinitely.
445  
446 =head2 ioloop
447  
448 my $loop = $mango->ioloop;
449 $mango = $mango->ioloop(Mojo::IOLoop->new);
450  
451 Event loop object to use for blocking I/O operations, defaults to a
452 L<Mojo::IOLoop> object.
453  
454 =head2 j
455  
456 my $j = $mango->j;
457 $mango = $mango->j(1);
458  
459 Wait for all operations to have reached the journal, defaults to C<0>.
460  
461 =head2 max_bson_size
462  
463 my $max = $mango->max_bson_size;
464 $mango = $mango->max_bson_size(16777216);
465  
466 Maximum size for BSON documents in bytes, defaults to C<16777216> (16MB).
467  
468 =head2 max_connections
469  
470 my $max = $mango->max_connections;
471 $mango = $mango->max_connections(5);
472  
473 Maximum number of connections to use for non-blocking operations, defaults to
474 C<5>.
475  
476 =head2 max_write_batch_size
477  
478 my $max = $mango->max_write_batch_size;
479 $mango = $mango->max_write_batch_size(1000);
480  
481 Maximum number of write operations to batch together, defaults to C<1000>.
482  
483 =head2 protocol
484  
485 my $protocol = $mango->protocol;
486 $mango = $mango->protocol(Mango::Protocol->new);
487  
488 Protocol handler, defaults to a L<Mango::Protocol> object.
489  
490 =head2 w
491  
492 my $w = $mango->w;
493 $mango = $mango->w(2);
494  
495 Wait for all operations to have reached at least this many servers, C<1>
496 indicates just primary, C<2> indicates primary and at least one secondary,
497 defaults to C<1>.
498  
499 =head2 wtimeout
500  
501 my $timeout = $mango->wtimeout;
502 $mango = $mango->wtimeout(1);
503  
504 Timeout for write propagation in milliseconds, defaults to C<1000>.
505  
506 =head1 METHODS
507  
508 L<Mango> inherits all methods from L<Mojo::Base> and implements the following
509 new ones.
510  
511 =head2 backlog
512  
513 my $num = $mango->backlog;
514  
515 Number of queued operations that have not yet been assigned to a connection.
516  
517 =head2 db
518  
519 my $db = $mango->db;
520 my $db = $mango->db('test');
521  
522 Build L<Mango::Database> object for database, uses L</"default_db"> if no name
523 is provided. Note that the reference L<Mango::Database/"mango"> is weakened,
524 so the L<Mango> object needs to be referenced elsewhere as well.
525  
526 =head2 from_string
527  
528 $mango
529 = $mango->from_string('mongodb://sri:s3cret@localhost:3000/test?w=2');
530  
531 Parse configuration from connection string.
532  
533 =head2 get_more
534  
535 my $reply = $mango->get_more($namespace, $return, $cursor);
536  
537 Perform low level C<GET_MORE> operation. You can also append a callback to
538 perform operation non-blocking.
539  
540 $mango->get_more(($namespace, $return, $cursor) => sub {
541 my ($mango, $err, $reply) = @_;
542 ...
543 });
544 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
545  
546 =head2 kill_cursors
547  
548 $mango->kill_cursors(@ids);
549  
550 Perform low level C<KILL_CURSORS> operation. You can also append a callback to
551 perform operation non-blocking.
552  
553 $mango->kill_cursors(@ids => sub {
554 my ($mango, $err) = @_;
555 ...
556 });
557 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
558  
559 =head2 new
560  
561 my $mango = Mango->new;
562 my $mango = Mango->new('mongodb://sri:s3cret@localhost:3000/test?w=2');
563  
564 Construct a new L<Mango> object and parse connection string with
565 L</"from_string"> if necessary.
566  
567 If a username and password are provided, Mango will try to authenticate using
568 SCRAM-SHA1. B<Warning:> this will require L<Authen::SCRAM> which is not
569 installed by default.
570  
571 =head2 query
572  
573 my $reply
574 = $mango->query($namespace, $flags, $skip, $return, $query, $fields);
575  
576 Perform low level C<QUERY> operation. You can also append a callback to
577 perform operation non-blocking.
578  
579 $mango->query(($namespace, $flags, $skip, $return, $query, $fields) => sub {
580 my ($mango, $err, $reply) = @_;
581 ...
582 });
583 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
584  
585 =head1 DEBUGGING
586  
587 You can set the C<MANGO_DEBUG> environment variable to get some advanced
588 diagnostics information printed to C<STDERR>.
589  
590 MANGO_DEBUG=1
591  
592 =head1 SPONSORS
593  
594 Some of the work on this distribution has been sponsored by
595 L<Drip Depot|http://www.dripdepot.com>, thank you!
596  
597 =head1 AUTHOR
598  
599 Sebastian Riedel, C<sri@cpan.org>.
600  
601 Current maintainer: Olivier Duclos C<odc@cpan.org>.
602  
603 =head1 CREDITS
604  
605 In alphabetical order:
606  
607 =over 2
608  
609 alexbyk
610  
611 Andrey Khozov
612  
613 Colin Cyr
614  
615 =back
616  
617 =head1 COPYRIGHT AND LICENSE
618  
619 Copyright (C) 2013-2014, Sebastian Riedel.
620  
621 This program is free software, you can redistribute it and/or modify it under
622 the terms of the Artistic License version 2.0.
623  
624 =head1 SEE ALSO
625  
626 L<https://github.com/oliwer/mango>, L<Mojolicious::Guides>,
627 L<http://mojolicio.us>.
628  
629 =cut
↑ Summary Mango/Cursor/Query.pm »