| 1 | # Copyright 2015 - present MongoDB, Inc. | ||||||||||
| 2 | # | ||||||||||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
| 4 | # you may not use this file except in compliance with the License. | ||||||||||
| 5 | # You may obtain a copy of the License at | ||||||||||
| 6 | # | ||||||||||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| 8 | # | ||||||||||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||||||||||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| 12 | # See the License for the specific language governing permissions and | ||||||||||
| 13 | # limitations under the License. | ||||||||||
| 14 | |||||||||||
| 15 | 59 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 59 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | package MongoDB::Op::_Explain; | ||||||||||
| 18 | |||||||||||
| 19 | # Encapsulate code path for explain commands/queries | ||||||||||
| 20 | |||||||||||
| 21 | 59 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 59 | our $VERSION = 'v2.2.2'; | |||||||||
| 23 | |||||||||||
| 24 | 59 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 59 | ▶ | use MongoDB::Op::_Command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 59 | ▶ | use Types::Standard qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | InstanceOf | ||||||||||
| 29 | 59 | ); | |||||||||
| 30 | 59 | ▶ | use Tie::IxHash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 59 | ▶ | use boolean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 59 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 59 | has query => ( | |||||||||
| 36 | is => 'ro', | ||||||||||
| 37 | required => 1, | ||||||||||
| 38 | isa => InstanceOf['MongoDB::Op::_Query'], | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 59 | with $_ for qw( | |||||||||
| 42 | MongoDB::Role::_PrivateConstructor | ||||||||||
| 43 | MongoDB::Role::_CollectionOp | ||||||||||
| 44 | MongoDB::Role::_ReadOp | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 48 | 0 | my ( $self, $link, $topology ) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | my $res = | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | $link->supports_explain_command | ||||||||||
| 52 | ? $self->_command_explain( $link, $topology ) | ||||||||||
| 53 | : $self->_legacy_explain( $link, $topology ); | ||||||||||
| 54 | |||||||||||
| 55 | 0 | return $res; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub _command_explain { | ||||||||
|
Subroutine
_command_explain: not called CC=2 SCAR=17.9
| |||||||||||
| 59 | 0 | my ( $self, $link, $topology ) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $cmd = Tie::IxHash->new( @{ $self->query->_as_command } ); | |||||||||
| 62 | |||||||||||
| 63 | # XXX need to standardize error here | ||||||||||
| 64 | 0 | ▶ | if ($self->query->has_hint) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | # cannot use hint on explain, throw error | ||||||||||
| 66 | 0 | MongoDB::Error->throw( | |||||||||
| 67 | message => "cannot use 'hint' with 'explain'", | ||||||||||
| 68 | ); | ||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | my $op = MongoDB::Op::_Command->_new( | ||||||||||
| 72 | db_name => $self->db_name, | ||||||||||
| 73 | query => [ | ||||||||||
| 74 | explain => $cmd, | ||||||||||
| 75 | 0 | @{ $self->read_concern->as_args( $self->session ) } | |||||||||
| 76 | ], | ||||||||||
| 77 | query_flags => {}, | ||||||||||
| 78 | read_preference => $self->read_preference, | ||||||||||
| 79 | bson_codec => $self->bson_codec, | ||||||||||
| 80 | session => $self->session, | ||||||||||
| 81 | monitoring_callback => $self->monitoring_callback, | ||||||||||
| 82 | ); | ||||||||||
| 83 | 0 | my $res = $op->execute( $link, $topology ); | |||||||||
| 84 | |||||||||||
| 85 | 0 | return $res->{output}; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub _legacy_explain { | ||||||||
|
Subroutine
_legacy_explain: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | my ( $self, $link, $topology ) = @_; | |||||||||
| 90 | |||||||||||
| 91 | 0 | my $orig_query = $self->query; | |||||||||
| 92 | 0 | my $cloned_opts = { %{ $orig_query->options } }; | |||||||||
| 93 | 0 | $cloned_opts->{explain} = 1; | |||||||||
| 94 | |||||||||||
| 95 | # per David Storch, drivers *must* send a negative limit to instruct | ||||||||||
| 96 | # the query planner analysis module to add a LIMIT stage. For older | ||||||||||
| 97 | # explain implementations, it also ensures a cursor isn't left open. | ||||||||||
| 98 | 0 | $cloned_opts->{limit} = ( -1 * abs( $cloned_opts->{limit} ) ); | |||||||||
| 99 | |||||||||||
| 100 | 0 | my $new_query = MongoDB::Op::_Query->_new( | |||||||||
| 101 | %$orig_query, | ||||||||||
| 102 | # override options from original | ||||||||||
| 103 | options => $cloned_opts | ||||||||||
| 104 | ); | ||||||||||
| 105 | |||||||||||
| 106 | 0 | return $new_query->execute( $link, $topology )->next; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 59 | 1; | |||||||||