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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/**
* Copyright (C) 2013 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/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/commands.h"
#include "mongo/db/exec/and_hash.h"
#include "mongo/db/exec/and_sorted.h"
#include "mongo/db/exec/index_scan.h"
#include "mongo/db/exec/simple_plan_runner.h"
#include "mongo/db/index/catalog_hack.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/matcher/matcher.h"
#include "mongo/db/namespace_details.h"
#include "mongo/db/pdfile.h"
namespace mongo {
/**
* A command for manually constructing a query tree and running it.
*
* db.runCommand({stageDebug: rootNode})
*
* The value of the filter field is a BSONObj that specifies values that fields must have. What
* you'd pass to a matcher.
*
* Leaf Nodes:
*
* node -> {ixscan: {filter: {FILTER},
* args: {name: "collectionname", indexKeyPattern: kpObj, start: startObj,
* stop: stopObj, endInclusive: true/false, direction: -1/1,
* limit: int}}}
*
* Internal Nodes:
*
* node -> {andHash: {filter: {filter}, args: { nodes: [node, node]}}}
* node -> {andSorted: {filter: {filter}, args: { nodes: [node, node]}}}
*
* Forthcoming Nodes:
*
* node -> {cscan: {filter: {filter}, args: {name: "collectionname" }}}
* node -> {or: {filter: {filter}, args: { dedup:bool, nodes:[node, node]}}}
* node -> {fetch: {filter: {filter}, args: {node: node}}}
* node -> {sort: {filter: {filter}, args: {node: node, pattern: objWithSortCriterion}}}
* node -> {dedup: {filter: {filter}, args: {node: node, field: field}}}
* node -> {limit: {filter: filter}, args: {node: node, num: posint}}
* node -> {skip: {filter: filter}, args: {node: node, num: posint}}
* node -> {unwind: {filter: filter}, args: {node: node, field: field}}
*/
class StageDebugCmd : public Command {
public:
StageDebugCmd() : Command("stageDebug") { }
// Boilerplate for commands
virtual LockType locktype() const { return READ; }
bool slaveOk() const { return true; }
bool slaveOverrideOk() const { return true; }
void help(stringstream& h) const { }
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {
ActionSet actions;
actions.addAction(ActionType::find);
out->push_back(Privilege(parseNs(dbname, cmdObj), actions));
}
bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result,
bool fromRepl) {
BSONElement argElt = cmdObj["stageDebug"];
if (argElt.eoo() || !argElt.isABSONObj()) { return false; }
BSONObj argObj = argElt.Obj();
SimplePlanRunner runner;
auto_ptr<PlanStage> root(parseQuery(dbname, argObj, runner.getWorkingSet()));
uassert(16911, "Couldn't parse plan from " + argObj.toString(), root.get());
runner.setRoot(root.release());
BSONArrayBuilder resultBuilder(result.subarrayStart("results"));
for (BSONObj obj; runner.getNext(&obj); ) {
resultBuilder.append(obj);
}
resultBuilder.done();
return true;
}
PlanStage* parseQuery(const string& dbname, BSONObj obj, WorkingSet* workingSet) {
BSONElement firstElt = obj.firstElement();
if (!firstElt.isABSONObj()) { return NULL; }
BSONObj paramObj = firstElt.Obj();
auto_ptr<Matcher> matcher;
BSONObj nodeArgs;
// Every node has these two fields.
const string filterTag = "filter";
const string argsTag = "args";
BSONObjIterator it(paramObj);
while (it.more()) {
BSONElement e = it.next();
if (!e.isABSONObj()) { return NULL; }
BSONObj argObj = e.Obj();
if (filterTag == e.fieldName()) {
matcher.reset(new Matcher2(argObj));
}
else if (argsTag == e.fieldName()) {
nodeArgs = argObj;
}
else {
uasserted(16910, "Unknown fieldname " + string(e.fieldName())
+ " in query node " + obj.toString());
return NULL;
}
}
string nodeName = firstElt.fieldName();
if ("ixscan" == nodeName) {
NamespaceDetails* nsd = nsdetails(dbname + "." + nodeArgs["name"].String());
uassert(16913, "Can't find collection " + nodeArgs["name"].String(), nsd);
int idxNo = nsd->findIndexByKeyPattern(nodeArgs["keyPattern"].Obj());
uassert(16890, "Can't find index: " + nodeArgs["keyPattern"].Obj().toString(),
idxNo != -1);
IndexScanParams params;
params.descriptor = CatalogHack::getDescriptor(nsd, idxNo);
params.startKey = nodeArgs["startKey"].Obj();
params.endKey = nodeArgs["endKey"].Obj();
params.endKeyInclusive = nodeArgs["endKeyInclusive"].Bool();
params.direction = nodeArgs["direction"].numberInt();
params.limit = nodeArgs["limit"].numberInt();
params.forceBtreeAccessMethod = false;
return new IndexScan(params, workingSet, matcher.release());
}
else if ("andHash" == nodeName) {
uassert(16921, "Nodes argument must be provided to AND",
nodeArgs["nodes"].isABSONObj());
auto_ptr<AndHashStage> andStage(new AndHashStage(workingSet, matcher.release()));
int nodesAdded = 0;
BSONObjIterator it(nodeArgs["nodes"].Obj());
while (it.more()) {
BSONElement e = it.next();
uassert(16922, "node of AND isn't an obj?: " + e.toString(),
e.isABSONObj());
PlanStage* subNode = parseQuery(dbname, e.Obj(), workingSet);
uassert(16923, "Can't parse sub-node of AND: " + e.Obj().toString(),
NULL != subNode);
// takes ownership
andStage->addChild(subNode);
++nodesAdded;
}
uassert(16927, "AND requires more than one child", nodesAdded >= 2);
return andStage.release();
}
else if ("andSorted" == nodeName) {
uassert(16924, "Nodes argument must be provided to AND",
nodeArgs["nodes"].isABSONObj());
auto_ptr<AndSortedStage> andStage(new AndSortedStage(workingSet,
matcher.release()));
int nodesAdded = 0;
BSONObjIterator it(nodeArgs["nodes"].Obj());
while (it.more()) {
BSONElement e = it.next();
uassert(16925, "node of AND isn't an obj?: " + e.toString(),
e.isABSONObj());
PlanStage* subNode = parseQuery(dbname, e.Obj(), workingSet);
uassert(16926, "Can't parse sub-node of AND: " + e.Obj().toString(),
NULL != subNode);
// takes ownership
andStage->addChild(subNode);
++nodesAdded;
}
uassert(16928, "AND requires more than one child", nodesAdded >= 2);
return andStage.release();
}
else {
return NULL;
}
}
} stageDebugCmd;
} // namespace mongo
|