File Coverage

blib/lib/MongoDB/Op/_Explain.pm
Criterion Covered Total %
statement 27 47 57.4
branch 0 4 0.0
condition n/a
subroutine 9 12 75.0
pod 0 1 0.0
total 36 64 56.2


line stmt bran cond sub pod time code
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 58     58   492 use strict;
  58         148  
  58         1815  
16 58     58   324 use warnings;
  58         121  
  58         2131  
17             package MongoDB::Op::_Explain;
18              
19             # Encapsulate code path for explain commands/queries
20              
21 58     58   302 use version;
  58         118  
  58         387  
22             our $VERSION = 'v2.2.0';
23              
24 58     58   4656 use Moo;
  58         139  
  58         418  
25              
26 58     58   21617 use MongoDB::Op::_Command;
  58         162  
  58         1861  
27 58         549 use Types::Standard qw(
28             InstanceOf
29 58     58   374 );
  58         130  
30 58     58   32709 use Tie::IxHash;
  58         129  
  58         1675  
31 58     58   337 use boolean;
  58         163  
  58         588  
32              
33 58     58   5004 use namespace::clean;
  58         141  
  58         495  
34              
35             has query => (
36             is => 'ro',
37             required => 1,
38             isa => InstanceOf['MongoDB::Op::_Query'],
39             );
40              
41             with $_ for qw(
42             MongoDB::Role::_PrivateConstructor
43             MongoDB::Role::_CollectionOp
44             MongoDB::Role::_ReadOp
45             );
46              
47             sub execute {
48 0     0 0   my ( $self, $link, $topology ) = @_;
49              
50 0 0         my $res =
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             sub _command_explain {
59 0     0     my ( $self, $link, $topology ) = @_;
60              
61 0           my $cmd = Tie::IxHash->new( @{ $self->query->_as_command } );
  0            
62              
63             # XXX need to standardize error here
64 0 0         if ($self->query->has_hint) {
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 ) }
  0            
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             sub _legacy_explain {
89 0     0     my ( $self, $link, $topology ) = @_;
90              
91 0           my $orig_query = $self->query;
92 0           my $cloned_opts = { %{ $orig_query->options } };
  0            
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             1;