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 59     59   372 use strict;
  59         112  
  59         1729  
16 59     59   272 use warnings;
  59         105  
  59         1990  
17             package MongoDB::Op::_Explain;
18              
19             # Encapsulate code path for explain commands/queries
20              
21 59     59   288 use version;
  59         104  
  59         401  
22             our $VERSION = 'v2.2.2';
23              
24 59     59   4324 use Moo;
  59         117  
  59         392  
25              
26 59     59   19363 use MongoDB::Op::_Command;
  59         191  
  59         1749  
27 59         641 use Types::Standard qw(
28             InstanceOf
29 59     59   338 );
  59         104  
30 59     59   43400 use Tie::IxHash;
  59         127  
  59         1597  
31 59     59   296 use boolean;
  59         128  
  59         520  
32              
33 59     59   4566 use namespace::clean;
  59         160  
  59         476  
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;