summaryrefslogtreecommitdiff
path: root/src/mongo/db/query_optimizer.cpp
blob: 1b4403dfe1465260938712964b92c66185b678b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 *    Copyright (C) 2011 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mongo/db/query_optimizer.h"

#include "mongo/db/query_optimizer_internal.h"
#include "mongo/db/queryoptimizercursorimpl.h"
#include "mongo/db/queryutil.h"

namespace mongo {

    shared_ptr<Cursor> getOptimizedCursor( const StringData& ns,
                                           const BSONObj& query,
                                           const BSONObj& order,
                                           const QueryPlanSelectionPolicy& planPolicy,
                                           const shared_ptr<const ParsedQuery>& parsedQuery,
                                           bool requireOrder,
                                           QueryPlanSummary* singlePlanSummary ) {
        CursorGenerator generator( ns,
                                   query,
                                   order,
                                   planPolicy,
                                   parsedQuery,
                                   requireOrder,
                                   singlePlanSummary );
        return generator.generate();
    }

    shared_ptr<Cursor> getBestGuessCursor( const char* ns,
                                           const BSONObj& query,
                                           const BSONObj& sort ) {

        auto_ptr<FieldRangeSetPair> frsp( new FieldRangeSetPair( ns, query, true ) );
        auto_ptr<FieldRangeSetPair> origFrsp( new FieldRangeSetPair( *frsp ) );

        scoped_ptr<QueryPlanSet> qps( QueryPlanSet::make( ns,
                                                          frsp,
                                                          origFrsp,
                                                          query,
                                                          sort,
                                                          shared_ptr<const ParsedQuery>(),
                                                          BSONObj(),
                                                          QueryPlanGenerator::UseIfInOrder,
                                                          BSONObj(),
                                                          BSONObj(),
                                                          true ) );
        QueryPlanSet::QueryPlanPtr qpp = qps->getBestGuess();
        if( ! qpp.get() ) return shared_ptr<Cursor>();

        shared_ptr<Cursor> ret = qpp->newCursor();

        // If we don't already have a matcher, supply one.
        if ( !query.isEmpty() && ! ret->matcher() ) {
            ret->setMatcher( qpp->matcher() );
        }
        return ret;
    }
    
} // namespace mongo;