| Filename | /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm |
| Statements | Executed 30 statements in 1.82ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 3.64ms | 4.04ms | MongoDB::Cursor::BEGIN@23 |
| 1 | 1 | 1 | 2.96ms | 29.4ms | MongoDB::Cursor::BEGIN@21 |
| 1 | 1 | 1 | 780µs | 4.38ms | MongoDB::Cursor::BEGIN@22 |
| 1 | 1 | 1 | 11µs | 148µs | MongoDB::Cursor::BEGIN@487 |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::_do_query |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::_ensure_special |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::all |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::count |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::explain |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::fields |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::hint |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::limit |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::skip |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::snapshot |
| 0 | 0 | 0 | 0s | 0s | MongoDB::Cursor::sort |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | # | ||||
| 2 | # Copyright 2009 10gen, Inc. | ||||
| 3 | # | ||||
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| 5 | # you may not use this file except in compliance with the License. | ||||
| 6 | # You may obtain a copy of the License at | ||||
| 7 | # | ||||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||
| 9 | # | ||||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| 13 | # See the License for the specific language governing permissions and | ||||
| 14 | # limitations under the License. | ||||
| 15 | # | ||||
| 16 | |||||
| 17 | package MongoDB::Cursor; | ||||
| 18 | 1 | 700ns | our $VERSION = '0.42'; | ||
| 19 | |||||
| 20 | # ABSTRACT: A cursor/iterator for Mongo query results | ||||
| 21 | 3 | 133µs | 2 | 30.0ms | # spent 29.4ms (2.96+26.5) within MongoDB::Cursor::BEGIN@21 which was called:
# once (2.96ms+26.5ms) by MongoDB::Connection::BEGIN@23 at line 21 # spent 29.4ms making 1 call to MongoDB::Cursor::BEGIN@21
# spent 542µs making 1 call to Any::Moose::import |
| 22 | 3 | 123µs | 2 | 4.45ms | # spent 4.38ms (780µs+3.60) within MongoDB::Cursor::BEGIN@22 which was called:
# once (780µs+3.60ms) by MongoDB::Connection::BEGIN@23 at line 22 # spent 4.38ms making 1 call to MongoDB::Cursor::BEGIN@22
# spent 64µs making 1 call to Exporter::import |
| 23 | 3 | 1.44ms | 1 | 4.04ms | # spent 4.04ms (3.64+407µs) within MongoDB::Cursor::BEGIN@23 which was called:
# once (3.64ms+407µs) by MongoDB::Connection::BEGIN@23 at line 23 # spent 4.04ms making 1 call to MongoDB::Cursor::BEGIN@23 |
| 24 | |||||
| 25 | =head1 NAME | ||||
| 26 | |||||
| - - | |||||
| 48 | 1 | 20µs | $MongoDB::Cursor::_request_id = int(rand(1000000)); | ||
| 49 | |||||
| 50 | =head1 STATIC ATTRIBUTES | ||||
| 51 | |||||
| - - | |||||
| 60 | 1 | 200ns | $MongoDB::Cursor::slave_okay = 0; | ||
| 61 | |||||
| 62 | =head2 timeout | ||||
| 63 | |||||
| - - | |||||
| 75 | 1 | 200ns | $MongoDB::Cursor::timeout = 30000; | ||
| 76 | |||||
| 77 | =head1 ATTRIBUTES | ||||
| 78 | |||||
| - - | |||||
| 87 | 1 | 3µs | 1 | 368µs | has started_iterating => ( # spent 368µs making 1 call to Mouse::has |
| 88 | is => 'rw', | ||||
| 89 | isa => 'Bool', | ||||
| 90 | required => 1, | ||||
| 91 | default => 0, | ||||
| 92 | ); | ||||
| 93 | |||||
| 94 | 1 | 2µs | 1 | 407µs | has _connection => ( # spent 407µs making 1 call to Mouse::has |
| 95 | is => 'ro', | ||||
| 96 | isa => 'MongoDB::Connection', | ||||
| 97 | required => 1, | ||||
| 98 | ); | ||||
| 99 | |||||
| 100 | 1 | 2µs | 1 | 201µs | has _ns => ( # spent 201µs making 1 call to Mouse::has |
| 101 | is => 'ro', | ||||
| 102 | isa => 'Str', | ||||
| 103 | required => 1, | ||||
| 104 | ); | ||||
| 105 | |||||
| 106 | 1 | 2µs | 1 | 162µs | has _query => ( # spent 162µs making 1 call to Mouse::has |
| 107 | is => 'rw', | ||||
| 108 | required => 1, | ||||
| 109 | ); | ||||
| 110 | |||||
| 111 | 1 | 2µs | 1 | 200µs | has _fields => ( # spent 200µs making 1 call to Mouse::has |
| 112 | is => 'rw', | ||||
| 113 | isa => 'HashRef', | ||||
| 114 | required => 0, | ||||
| 115 | ); | ||||
| 116 | |||||
| 117 | 1 | 2µs | 1 | 196µs | has _limit => ( # spent 196µs making 1 call to Mouse::has |
| 118 | is => 'rw', | ||||
| 119 | isa => 'Int', | ||||
| 120 | required => 0, | ||||
| 121 | default => 0, | ||||
| 122 | ); | ||||
| 123 | |||||
| 124 | 1 | 2µs | 1 | 192µs | has _skip => ( # spent 192µs making 1 call to Mouse::has |
| 125 | is => 'rw', | ||||
| 126 | isa => 'Int', | ||||
| 127 | required => 0, | ||||
| 128 | default => 0, | ||||
| 129 | ); | ||||
| 130 | |||||
| 131 | =head2 immortal | ||||
| 132 | |||||
| - - | |||||
| 144 | 1 | 2µs | 1 | 193µs | has immortal => ( # spent 193µs making 1 call to Mouse::has |
| 145 | is => 'rw', | ||||
| 146 | isa => 'Bool', | ||||
| 147 | required => 0, | ||||
| 148 | default => 0, | ||||
| 149 | ); | ||||
| 150 | |||||
| 151 | =head2 tailable | ||||
| 152 | |||||
| - - | |||||
| 165 | 1 | 2µs | 1 | 192µs | has tailable => ( # spent 192µs making 1 call to Mouse::has |
| 166 | is => 'rw', | ||||
| 167 | isa => 'Bool', | ||||
| 168 | required => 0, | ||||
| 169 | default => 0, | ||||
| 170 | ); | ||||
| 171 | |||||
| 172 | =head2 slave_okay | ||||
| 173 | |||||
| - - | |||||
| 182 | 1 | 2µs | 1 | 192µs | has slave_okay => ( # spent 192µs making 1 call to Mouse::has |
| 183 | is => 'rw', | ||||
| 184 | isa => 'Bool', | ||||
| 185 | required => 0, | ||||
| 186 | default => 0, | ||||
| 187 | ); | ||||
| 188 | |||||
| 189 | |||||
| 190 | # stupid hack for inconsistent database handling of queries | ||||
| 191 | 1 | 2µs | 1 | 207µs | has _grrrr => ( # spent 207µs making 1 call to Mouse::has |
| 192 | is => 'rw', | ||||
| 193 | isa => 'Bool', | ||||
| 194 | default => 0, | ||||
| 195 | ); | ||||
| 196 | |||||
| 197 | 1 | 2µs | 1 | 199µs | has _request_id => ( # spent 199µs making 1 call to Mouse::has |
| 198 | is => 'rw', | ||||
| 199 | isa => 'Int', | ||||
| 200 | default => 0, | ||||
| 201 | ); | ||||
| 202 | |||||
| 203 | =head1 METHODS | ||||
| 204 | |||||
| - - | |||||
| 207 | sub _ensure_special { | ||||
| 208 | my ($self) = @_; | ||||
| 209 | |||||
| 210 | if ($self->_grrrr) { | ||||
| 211 | return; | ||||
| 212 | } | ||||
| 213 | |||||
| 214 | $self->_grrrr(1); | ||||
| 215 | $self->_query({'query' => $self->_query}) | ||||
| 216 | } | ||||
| 217 | |||||
| 218 | # this does the query if it hasn't been done yet | ||||
| 219 | sub _do_query { | ||||
| 220 | my ($self) = @_; | ||||
| 221 | |||||
| 222 | if ($self->started_iterating) { | ||||
| 223 | return; | ||||
| 224 | } | ||||
| 225 | |||||
| 226 | my $opts = $MongoDB::Cursor::slave_okay | ($self->tailable << 1) | | ||||
| 227 | ($self->slave_okay << 2) | ($self->immortal << 4); | ||||
| 228 | |||||
| 229 | my ($query, $info) = MongoDB::write_query($self->_ns, $opts, $self->_skip, $self->_limit, $self->_query, $self->_fields); | ||||
| 230 | $self->_request_id($info->{'request_id'}); | ||||
| 231 | |||||
| 232 | $self->_connection->send($query); | ||||
| 233 | $self->_connection->recv($self); | ||||
| 234 | |||||
| 235 | $self->started_iterating(1); | ||||
| 236 | } | ||||
| 237 | |||||
| 238 | =head2 fields (\%f) | ||||
| 239 | |||||
| - - | |||||
| 251 | sub fields { | ||||
| 252 | my ($self, $f) = @_; | ||||
| 253 | confess "cannot set fields after querying" | ||||
| 254 | if $self->started_iterating; | ||||
| 255 | confess 'not a hash reference' | ||||
| 256 | unless ref $f eq 'HASH'; | ||||
| 257 | |||||
| 258 | $self->_fields($f); | ||||
| 259 | return $self; | ||||
| 260 | } | ||||
| 261 | |||||
| 262 | =head2 sort ($order) | ||||
| 263 | |||||
| - - | |||||
| 274 | sub sort { | ||||
| 275 | my ($self, $order) = @_; | ||||
| 276 | confess "cannot set sort after querying" | ||||
| 277 | if $self->started_iterating; | ||||
| 278 | confess 'not a hash reference' | ||||
| 279 | unless ref $order eq 'HASH' || ref $order eq 'Tie::IxHash'; | ||||
| 280 | |||||
| 281 | $self->_ensure_special; | ||||
| 282 | $self->_query->{'orderby'} = $order; | ||||
| 283 | return $self; | ||||
| 284 | } | ||||
| 285 | |||||
| 286 | |||||
| 287 | =head2 limit ($num) | ||||
| 288 | |||||
| - - | |||||
| 297 | sub limit { | ||||
| 298 | my ($self, $num) = @_; | ||||
| 299 | confess "cannot set limit after querying" | ||||
| 300 | if $self->started_iterating; | ||||
| 301 | |||||
| 302 | $self->_limit($num); | ||||
| 303 | return $self; | ||||
| 304 | } | ||||
| 305 | |||||
| 306 | =head2 skip ($num) | ||||
| 307 | |||||
| - - | |||||
| 318 | sub skip { | ||||
| 319 | my ($self, $num) = @_; | ||||
| 320 | confess "cannot set skip after querying" | ||||
| 321 | if $self->started_iterating; | ||||
| 322 | |||||
| 323 | $self->_skip($num); | ||||
| 324 | return $self; | ||||
| 325 | } | ||||
| 326 | |||||
| 327 | =head2 snapshot | ||||
| 328 | |||||
| - - | |||||
| 342 | sub snapshot { | ||||
| 343 | my ($self) = @_; | ||||
| 344 | confess "cannot set snapshot after querying" | ||||
| 345 | if $self->started_iterating; | ||||
| 346 | |||||
| 347 | $self->_ensure_special; | ||||
| 348 | $self->_query->{'$snapshot'} = 1; | ||||
| 349 | return $self; | ||||
| 350 | } | ||||
| 351 | |||||
| 352 | =head2 hint | ||||
| 353 | |||||
| - - | |||||
| 360 | sub hint { | ||||
| 361 | my ($self, $index) = @_; | ||||
| 362 | confess "cannot set hint after querying" | ||||
| 363 | if $self->started_iterating; | ||||
| 364 | confess 'not a hash reference' | ||||
| 365 | unless ref $index eq 'HASH'; | ||||
| 366 | |||||
| 367 | $self->_ensure_special; | ||||
| 368 | $self->_query->{'$hint'} = $index; | ||||
| 369 | return $self; | ||||
| 370 | } | ||||
| 371 | |||||
| 372 | =head2 explain | ||||
| 373 | |||||
| - - | |||||
| 388 | sub explain { | ||||
| 389 | my ($self) = @_; | ||||
| 390 | my $temp = $self->_limit; | ||||
| 391 | if ($self->_limit > 0) { | ||||
| 392 | $self->_limit($self->_limit * -1); | ||||
| 393 | } | ||||
| 394 | |||||
| 395 | $self->_ensure_special; | ||||
| 396 | $self->_query->{'$explain'} = boolean::true; | ||||
| 397 | |||||
| 398 | my $retval = $self->reset->next; | ||||
| 399 | $self->reset->limit($temp); | ||||
| 400 | |||||
| 401 | undef $self->_query->{'$explain'}; | ||||
| 402 | |||||
| 403 | return $retval; | ||||
| 404 | } | ||||
| 405 | |||||
| 406 | =head2 count($all?) | ||||
| 407 | |||||
| - - | |||||
| 417 | sub count { | ||||
| 418 | my ($self, $all) = @_; | ||||
| 419 | |||||
| 420 | my ($db, $coll) = $self->_ns =~ m/^([^\.]+)\.(.*)/; | ||||
| 421 | my $cmd = new Tie::IxHash(count => $coll); | ||||
| 422 | |||||
| 423 | if ($self->_grrrr) { | ||||
| 424 | $cmd->Push(query => $self->_query->{'query'}); | ||||
| 425 | } | ||||
| 426 | else { | ||||
| 427 | $cmd->Push(query => $self->_query); | ||||
| 428 | } | ||||
| 429 | |||||
| 430 | if ($all) { | ||||
| 431 | $cmd->Push(limit => $self->_limit) if $self->_limit; | ||||
| 432 | $cmd->Push(skip => $self->_skip) if $self->_skip; | ||||
| 433 | } | ||||
| 434 | |||||
| 435 | my $result = $self->_connection->get_database($db)->run_command($cmd); | ||||
| 436 | |||||
| 437 | # returns "ns missing" if collection doesn't exist | ||||
| 438 | return 0 unless ref $result eq 'HASH'; | ||||
| 439 | return $result->{'n'}; | ||||
| 440 | } | ||||
| 441 | |||||
| 442 | =head2 reset | ||||
| 443 | |||||
| - - | |||||
| 476 | sub all { | ||||
| 477 | my ($self) = @_; | ||||
| 478 | my @ret; | ||||
| 479 | |||||
| 480 | while (my $entry = $self->next) { | ||||
| 481 | push @ret, $entry; | ||||
| 482 | } | ||||
| 483 | |||||
| 484 | return @ret; | ||||
| 485 | } | ||||
| 486 | |||||
| 487 | 3 | 56µs | 2 | 284µs | # spent 148µs (11+137) within MongoDB::Cursor::BEGIN@487 which was called:
# once (11µs+137µs) by MongoDB::Connection::BEGIN@23 at line 487 # spent 148µs making 1 call to MongoDB::Cursor::BEGIN@487
# spent 137µs making 1 call to Any::Moose::unimport |
| 488 | 1 | 6µs | 2 | 76µs | __PACKAGE__->meta->make_immutable (inline_destructor => 0); # spent 67µs making 1 call to Mouse::Meta::Class::make_immutable
# spent 9µs making 1 call to MongoDB::Cursor::meta |
| 489 | |||||
| 490 | 1 | 27µs | 1; | ||
| 491 | |||||
| 492 | =head1 AUTHOR |