本文整理汇总了C++中QuerySolutionNode类的典型用法代码示例。如果您正苦于以下问题:C++ QuerySolutionNode类的具体用法?C++ QuerySolutionNode怎么用?C++ QuerySolutionNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QuerySolutionNode类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sortOrder
void SortKeyGenerator::getBoundsForSort(OperationContext* txn,
const BSONObj& queryObj,
const BSONObj& sortObj) {
QueryPlannerParams params;
params.options = QueryPlannerParams::NO_TABLE_SCAN;
// We're creating a "virtual index" with key pattern equal to the sort order.
IndexEntry sortOrder(sortObj,
IndexNames::BTREE,
true,
MultikeyPaths{},
false,
false,
"doesnt_matter",
NULL,
BSONObj());
params.indices.push_back(sortOrder);
auto statusWithQueryForSort = CanonicalQuery::canonicalize(
txn, NamespaceString("fake.ns"), queryObj, ExtensionsCallbackNoop());
verify(statusWithQueryForSort.isOK());
std::unique_ptr<CanonicalQuery> queryForSort = std::move(statusWithQueryForSort.getValue());
std::vector<QuerySolution*> solns;
LOG(5) << "Sort key generation: Planning to obtain bounds for sort.";
QueryPlanner::plan(*queryForSort, params, &solns);
// TODO: are there ever > 1 solns? If so, do we look for a specific soln?
if (1 == solns.size()) {
IndexScanNode* ixScan = NULL;
QuerySolutionNode* rootNode = solns[0]->root.get();
if (rootNode->getType() == STAGE_FETCH) {
FetchNode* fetchNode = static_cast<FetchNode*>(rootNode);
if (fetchNode->children[0]->getType() != STAGE_IXSCAN) {
delete solns[0];
// No bounds.
return;
}
ixScan = static_cast<IndexScanNode*>(fetchNode->children[0]);
} else if (rootNode->getType() == STAGE_IXSCAN) {
ixScan = static_cast<IndexScanNode*>(rootNode);
}
if (ixScan) {
_bounds.fields.swap(ixScan->bounds.fields);
_hasBounds = true;
}
}
for (size_t i = 0; i < solns.size(); ++i) {
delete solns[i];
}
}
示例2: turnIxscanIntoDistinctIxscan
/**
* If possible, turn the provided QuerySolution into a QuerySolution that uses a DistinctNode
* to provide results for the distinct command.
*
* If the provided solution could be mutated successfully, returns true, otherwise returns
* false.
*/
bool turnIxscanIntoDistinctIxscan(QuerySolution* soln, const string& field) {
QuerySolutionNode* root = soln->root.get();
// We're looking for a project on top of an ixscan.
if (STAGE_PROJECTION == root->getType() && (STAGE_IXSCAN == root->children[0]->getType())) {
IndexScanNode* isn = static_cast<IndexScanNode*>(root->children[0]);
// An additional filter must be applied to the data in the key, so we can't just skip
// all the keys with a given value; we must examine every one to find the one that (may)
// pass the filter.
if (NULL != isn->filter.get()) {
return false;
}
// We only set this when we have special query modifiers (.max() or .min()) or other
// special cases. Don't want to handle the interactions between those and distinct.
// Don't think this will ever really be true but if it somehow is, just ignore this
// soln.
if (isn->bounds.isSimpleRange) {
return false;
}
// Make a new DistinctNode. We swap this for the ixscan in the provided solution.
DistinctNode* dn = new DistinctNode();
dn->indexKeyPattern = isn->indexKeyPattern;
dn->direction = isn->direction;
dn->bounds = isn->bounds;
// Figure out which field we're skipping to the next value of. TODO: We currently only
// try to distinct-hack when there is an index prefixed by the field we're distinct-ing
// over. Consider removing this code if we stick with that policy.
dn->fieldNo = 0;
BSONObjIterator it(isn->indexKeyPattern);
while (it.more()) {
if (field == it.next().fieldName()) {
break;
}
dn->fieldNo++;
}
// Delete the old index scan, set the child of project to the fast distinct scan.
delete root->children[0];
root->children[0] = dn;
return true;
}
return false;
}
示例3: sortOrder
void SortStageKeyGenerator::getBoundsForSort(const BSONObj& queryObj, const BSONObj& sortObj) {
QueryPlannerParams params;
params.options = QueryPlannerParams::NO_TABLE_SCAN;
// We're creating a "virtual index" with key pattern equal to the sort order.
IndexEntry sortOrder(sortObj, IndexNames::BTREE, true, false, false, "doesnt_matter",
BSONObj());
params.indices.push_back(sortOrder);
CanonicalQuery* rawQueryForSort;
verify(CanonicalQuery::canonicalize(
"fake_ns", queryObj, &rawQueryForSort, WhereCallbackNoop()).isOK());
auto_ptr<CanonicalQuery> queryForSort(rawQueryForSort);
vector<QuerySolution*> solns;
QLOG() << "Sort stage: Planning to obtain bounds for sort." << endl;
QueryPlanner::plan(*queryForSort, params, &solns);
// TODO: are there ever > 1 solns? If so, do we look for a specific soln?
if (1 == solns.size()) {
IndexScanNode* ixScan = NULL;
QuerySolutionNode* rootNode = solns[0]->root.get();
if (rootNode->getType() == STAGE_FETCH) {
FetchNode* fetchNode = static_cast<FetchNode*>(rootNode);
if (fetchNode->children[0]->getType() != STAGE_IXSCAN) {
delete solns[0];
// No bounds.
return;
}
ixScan = static_cast<IndexScanNode*>(fetchNode->children[0]);
}
else if (rootNode->getType() == STAGE_IXSCAN) {
ixScan = static_cast<IndexScanNode*>(rootNode);
}
if (ixScan) {
_bounds.fields.swap(ixScan->bounds.fields);
_hasBounds = true;
}
}
for (size_t i = 0; i < solns.size(); ++i) {
delete solns[i];
}
}
示例4: build
//static
bool StageBuilder::build(const QuerySolution& solution, PlanStage** rootOut,
WorkingSet** wsOut) {
QuerySolutionNode* root = solution.root.get();
if (NULL == root) { return false; }
if (STAGE_COLLSCAN == root->getType()) {
const CollectionScanNode* csn = static_cast<const CollectionScanNode*>(root);
CollectionScanParams params;
params.ns = csn->name;
params.tailable = csn->tailable;
params.direction = (csn->direction == 1) ? CollectionScanParams::FORWARD
: CollectionScanParams::BACKWARD;
*wsOut = new WorkingSet();
*rootOut = new CollectionScan(params, *wsOut, csn->filter);
return true;
}
else {
return false;
}
}
示例5: orExpr
//.........这里部分代码省略.........
_child.reset(new MultiPlanStage(_txn, _collection,
branchResult->canonicalQuery.get()));
MultiPlanStage* multiPlanStage = static_cast<MultiPlanStage*>(_child.get());
// Dump all the solutions into the MPS.
for (size_t ix = 0; ix < branchResult->solutions.size(); ++ix) {
PlanStage* nextPlanRoot;
invariant(StageBuilder::build(_txn,
_collection,
*branchResult->solutions[ix],
_ws,
&nextPlanRoot));
// Takes ownership of solution with index 'ix' and 'nextPlanRoot'.
multiPlanStage->addPlan(branchResult->solutions.releaseAt(ix),
nextPlanRoot,
_ws);
}
Status planSelectStat = multiPlanStage->pickBestPlan(yieldPolicy);
if (!planSelectStat.isOK()) {
return planSelectStat;
}
if (!multiPlanStage->bestPlanChosen()) {
mongoutils::str::stream ss;
ss << "Failed to pick best plan for subchild "
<< branchResult->canonicalQuery->toString();
return Status(ErrorCodes::BadValue, ss);
}
QuerySolution* bestSoln = multiPlanStage->bestSolution();
// Check that we have good cache data. For example, we don't cache things
// for 2d indices.
if (NULL == bestSoln->cacheData.get()) {
mongoutils::str::stream ss;
ss << "No cache data for subchild " << orChild->toString();
return Status(ErrorCodes::BadValue, ss);
}
if (SolutionCacheData::USE_INDEX_TAGS_SOLN != bestSoln->cacheData->solnType) {
mongoutils::str::stream ss;
ss << "No indexed cache data for subchild "
<< orChild->toString();
return Status(ErrorCodes::BadValue, ss);
}
// Add the index assignments to our original query.
Status tagStatus = QueryPlanner::tagAccordingToCache(
orChild, bestSoln->cacheData->tree.get(), _indexMap);
if (!tagStatus.isOK()) {
mongoutils::str::stream ss;
ss << "Failed to extract indices from subchild "
<< orChild->toString();
return Status(ErrorCodes::BadValue, ss);
}
cacheData->children.push_back(bestSoln->cacheData->tree->clone());
}
}
// Must do this before using the planner functionality.
sortUsingTags(orExpr.get());
// Use the cached index assignments to build solnRoot. Takes ownership of 'orExpr'.
QuerySolutionNode* solnRoot = QueryPlannerAccess::buildIndexedDataAccess(
*_query, orExpr.release(), false, _plannerParams.indices, _plannerParams);
if (NULL == solnRoot) {
mongoutils::str::stream ss;
ss << "Failed to build indexed data path for subplanned query\n";
return Status(ErrorCodes::BadValue, ss);
}
QLOG() << "Subplanner: fully tagged tree is " << solnRoot->toString();
// Takes ownership of 'solnRoot'
_compositeSolution.reset(QueryPlannerAnalysis::analyzeDataAccess(*_query,
_plannerParams,
solnRoot));
if (NULL == _compositeSolution.get()) {
mongoutils::str::stream ss;
ss << "Failed to analyze subplanned query";
return Status(ErrorCodes::BadValue, ss);
}
QLOG() << "Subplanner: Composite solution is " << _compositeSolution->toString() << endl;
// Use the index tags from planning each branch to construct the composite solution,
// and set that solution as our child stage.
_ws->clear();
PlanStage* root;
invariant(StageBuilder::build(_txn, _collection, *_compositeSolution.get(), _ws, &root));
_child.reset(root);
return Status::OK();
}
示例6: theOr
//.........这里部分代码省略.........
multiPlanStage->pickBestPlan();
if (! multiPlanStage->bestPlanChosen()) {
QLOG() << "Subplanner: Failed to pick best plan for subchild "
<< orChildCQ->toString();
return false;
}
Runner* mpr = new SingleSolutionRunner(_collection,
orChildCQ.release(),
multiPlanStage->bestSolution(),
multiPlanStage,
sharedWorkingSet);
_underlyingRunner.reset(mpr);
if (_killed) {
QLOG() << "Subplanner: Killed while picking best plan for subchild "
<< orChild->toString();
return false;
}
QuerySolution* bestSoln = multiPlanStage->bestSolution();
if (SolutionCacheData::USE_INDEX_TAGS_SOLN != bestSoln->cacheData->solnType) {
QLOG() << "Subplanner: No indexed cache data for subchild "
<< orChild->toString();
return false;
}
// Add the index assignments to our original query.
Status tagStatus = QueryPlanner::tagAccordingToCache(
orChild, bestSoln->cacheData->tree.get(), _indexMap);
if (!tagStatus.isOK()) {
QLOG() << "Subplanner: Failed to extract indices from subchild "
<< orChild->toString();
return false;
}
cacheData->children.push_back(bestSoln->cacheData->tree->clone());
}
}
// Must do this before using the planner functionality.
sortUsingTags(theOr.get());
// Use the cached index assignments to build solnRoot. Takes ownership of 'theOr'
QuerySolutionNode* solnRoot = QueryPlannerAccess::buildIndexedDataAccess(
*_query, theOr.release(), false, _plannerParams.indices);
if (NULL == solnRoot) {
QLOG() << "Subplanner: Failed to build indexed data path for subplanned query\n";
return false;
}
QLOG() << "Subplanner: fully tagged tree is " << solnRoot->toString();
// Takes ownership of 'solnRoot'
QuerySolution* soln = QueryPlannerAnalysis::analyzeDataAccess(*_query,
_plannerParams,
solnRoot);
if (NULL == soln) {
QLOG() << "Subplanner: Failed to analyze subplanned query";
return false;
}
// We want our franken-solution to be cached.
SolutionCacheData* scd = new SolutionCacheData();
scd->tree.reset(cacheData.release());
soln->cacheData.reset(scd);
QLOG() << "Subplanner: Composite solution is " << soln->toString() << endl;
// We use one of these even if there is one plan. We do this so that the entry is cached
// with stats obtained in the same fashion as a competitive ranking would have obtained
// them.
MultiPlanStage* multiPlanStage = new MultiPlanStage(_collection, _query.get());
WorkingSet* ws = new WorkingSet();
PlanStage* root;
verify(StageBuilder::build(_txn, _collection, *soln, ws, &root));
multiPlanStage->addPlan(soln, root, ws); // Takes ownership first two arguments.
multiPlanStage->pickBestPlan();
if (! multiPlanStage->bestPlanChosen()) {
QLOG() << "Subplanner: Failed to pick best plan for subchild "
<< _query->toString();
return false;
}
Runner* mpr = new SingleSolutionRunner(_collection,
_query.release(),
multiPlanStage->bestSolution(),
multiPlanStage,
ws);
_underlyingRunner.reset(mpr);
return true;
}
示例7: cacheData
//.........这里部分代码省略.........
ON_BLOCK_EXIT([&] {
invariant(_children.size() == 1); // Make sure nothing else was added to _children.
_children.pop_back();
});
MultiPlanStage* multiPlanStage = static_cast<MultiPlanStage*>(child().get());
// Dump all the solutions into the MPS.
for (size_t ix = 0; ix < branchResult->solutions.size(); ++ix) {
PlanStage* nextPlanRoot;
invariant(StageBuilder::build(getOpCtx(),
_collection,
*branchResult->canonicalQuery,
*branchResult->solutions[ix],
_ws,
&nextPlanRoot));
// Takes ownership of solution with index 'ix' and 'nextPlanRoot'.
multiPlanStage->addPlan(branchResult->solutions.releaseAt(ix), nextPlanRoot, _ws);
}
Status planSelectStat = multiPlanStage->pickBestPlan(yieldPolicy);
if (!planSelectStat.isOK()) {
return planSelectStat;
}
if (!multiPlanStage->bestPlanChosen()) {
mongoutils::str::stream ss;
ss << "Failed to pick best plan for subchild "
<< branchResult->canonicalQuery->toString();
return Status(ErrorCodes::BadValue, ss);
}
QuerySolution* bestSoln = multiPlanStage->bestSolution();
// Check that we have good cache data. For example, we don't cache things
// for 2d indices.
if (NULL == bestSoln->cacheData.get()) {
mongoutils::str::stream ss;
ss << "No cache data for subchild " << orChild->toString();
return Status(ErrorCodes::BadValue, ss);
}
if (SolutionCacheData::USE_INDEX_TAGS_SOLN != bestSoln->cacheData->solnType) {
mongoutils::str::stream ss;
ss << "No indexed cache data for subchild " << orChild->toString();
return Status(ErrorCodes::BadValue, ss);
}
// Add the index assignments to our original query.
Status tagStatus = QueryPlanner::tagAccordingToCache(
orChild, bestSoln->cacheData->tree.get(), _indexMap);
if (!tagStatus.isOK()) {
mongoutils::str::stream ss;
ss << "Failed to extract indices from subchild " << orChild->toString();
return Status(ErrorCodes::BadValue, ss);
}
cacheData->children.push_back(bestSoln->cacheData->tree->clone());
}
}
// Must do this before using the planner functionality.
sortUsingTags(_orExpression.get());
// Use the cached index assignments to build solnRoot. Takes ownership of '_orExpression'.
QuerySolutionNode* solnRoot = QueryPlannerAccess::buildIndexedDataAccess(
*_query, _orExpression.release(), false, _plannerParams.indices, _plannerParams);
if (NULL == solnRoot) {
mongoutils::str::stream ss;
ss << "Failed to build indexed data path for subplanned query\n";
return Status(ErrorCodes::BadValue, ss);
}
LOG(5) << "Subplanner: fully tagged tree is " << solnRoot->toString();
// Takes ownership of 'solnRoot'
_compositeSolution.reset(
QueryPlannerAnalysis::analyzeDataAccess(*_query, _plannerParams, solnRoot));
if (NULL == _compositeSolution.get()) {
mongoutils::str::stream ss;
ss << "Failed to analyze subplanned query";
return Status(ErrorCodes::BadValue, ss);
}
LOG(5) << "Subplanner: Composite solution is " << _compositeSolution->toString();
// Use the index tags from planning each branch to construct the composite solution,
// and set that solution as our child stage.
_ws->clear();
PlanStage* root;
invariant(StageBuilder::build(
getOpCtx(), _collection, *_query, *_compositeSolution.get(), _ws, &root));
invariant(_children.empty());
_children.emplace_back(root);
return Status::OK();
}