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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/* queryoptimizer.h */
/**
* Copyright (C) 2008 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/>.
*/
#pragma once
#include "cursor.h"
#include "jsobj.h"
namespace mongo {
class FieldBound {
public:
FieldBound( const BSONElement &e = emptyObj.firstElement() );
FieldBound &operator&=( const FieldBound &other );
BSONElement lower() const { return lower_; }
BSONElement upper() const { return upper_; }
bool equality() const { return lower_.woCompare( upper_, false ) == 0; }
bool nontrivial() const {
return
minKey.firstElement().woCompare( lower_, false ) != 0 ||
maxKey.firstElement().woCompare( upper_, false ) != 0;
}
private:
BSONObj addObj( const BSONObj &o );
string simpleRegexEnd( string regex );
BSONElement lower_;
BSONElement upper_;
vector< BSONObj > objData_;
};
class FieldBoundSet {
public:
FieldBoundSet( const BSONObj &query );
const FieldBound &bound( const char *fieldName ) const {
map< string, FieldBound >::const_iterator f = bounds_.find( fieldName );
if ( f == bounds_.end() )
return trivialBound();
return f->second;
}
int nBounds() const {
int count = 0;
for( map< string, FieldBound >::const_iterator i = bounds_.begin(); i != bounds_.end(); ++i )
++count;
return count;
}
int nNontrivialBounds() const {
int count = 0;
for( map< string, FieldBound >::const_iterator i = bounds_.begin(); i != bounds_.end(); ++i )
if ( i->second.nontrivial() )
++count;
return count;
}
private:
static FieldBound *trivialBound_;
static FieldBound &trivialBound();
map< string, FieldBound > bounds_;
BSONObj query_;
};
class QueryPlan {
public:
QueryPlan( const FieldBoundSet &fbs, const BSONObj &order, const BSONObj &idxKey );
/* If true, no other index can do better. */
bool optimal() const { return optimal_; }
/* ScanAndOrder processing will be required if true */
bool scanAndOrderRequired() const { return scanAndOrderRequired_; }
/* When true, the index we are using has keys such that it can completely resolve the
query expression to match by itself without ever checking the main object.
*/
bool keyMatch() const { return keyMatch_; }
/* True if keyMatch() is true, and all matches will be equal according to woEqual() */
bool exactKeyMatch() const { return exactKeyMatch_; }
int direction() const { return direction_; }
BSONObj startKey() const { return startKey_; }
BSONObj endKey() const { return endKey_; }
private:
bool optimal_;
bool scanAndOrderRequired_;
bool keyMatch_;
bool exactKeyMatch_;
int direction_;
BSONObj startKey_;
BSONObj endKey_;
};
class QueryPlanSet {
public:
QueryPlanSet( const char *ns, const BSONObj &query, const BSONObj &order, const BSONElement *hint = 0 );
int nPlans() const { return plans_.size(); }
private:
FieldBoundSet fbs_;
vector< QueryPlan > plans_;
};
// class QueryOptimizer {
// public:
// static QueryPlan getPlan(
// const char *ns,
// BSONObj* query,
// BSONObj* order = 0,
// BSONObj* hint = 0);
// };
} // namespace mongo
|