From 12c32474881bb33702b4499555dfe98070f332b1 Mon Sep 17 00:00:00 2001 From: Milena Ivanova Date: Fri, 10 Sep 2021 17:31:26 +0000 Subject: SERVER-58192 Use more unique_ptrs in QueryPlannerAnalysis::analyzeSort() (cherry picked from commit de21bf91f015f0ac21ed691ae0049db35f071920) --- src/mongo/db/query/planner_analysis.cpp | 29 +++++++++++++++-------------- src/mongo/db/query/query_solution.cpp | 6 +++--- 2 files changed, 18 insertions(+), 17 deletions(-) (limited to 'src/mongo') diff --git a/src/mongo/db/query/planner_analysis.cpp b/src/mongo/db/query/planner_analysis.cpp index 059564d783e..b5d1644d3db 100644 --- a/src/mongo/db/query/planner_analysis.cpp +++ b/src/mongo/db/query/planner_analysis.cpp @@ -552,21 +552,22 @@ QuerySolutionNode* QueryPlannerAnalysis::analyzeSort(const CanonicalQuery& query // And build the full sort stage. The sort stage has to have a sort key generating stage // as its child, supplying it with the appropriate sort keys. - SortKeyGeneratorNode* keyGenNode = new SortKeyGeneratorNode(); + auto keyGenNode = std::make_unique(); keyGenNode->sortSpec = sortObj; keyGenNode->children.push_back(solnRoot); - solnRoot = keyGenNode; + solnRoot = keyGenNode.release(); - SortNode* sort = new SortNode(); + auto sort = std::make_unique(); sort->pattern = sortObj; sort->children.push_back(solnRoot); - solnRoot = sort; + solnRoot = sort.release(); + auto sortNodeRaw = static_cast(solnRoot); // When setting the limit on the sort, we need to consider both // the limit N and skip count M. The sort should return an ordered list // N + M items so that the skip stage can discard the first M results. if (qr.getLimit()) { // We have a true limit. The limit can be combined with the SORT stage. - sort->limit = + sortNodeRaw->limit = static_cast(*qr.getLimit()) + static_cast(qr.getSkip().value_or(0)); } else if (qr.getNToReturn()) { // We have an ntoreturn specified by an OP_QUERY style find. This is used @@ -575,7 +576,7 @@ QuerySolutionNode* QueryPlannerAnalysis::analyzeSort(const CanonicalQuery& query // Overflow here would be bad and could cause a nonsense limit. Cast // skip and limit values to unsigned ints to make sure that the // sum is never stored as signed. (See SERVER-13537). - sort->limit = + sortNodeRaw->limit = static_cast(*qr.getNToReturn()) + static_cast(qr.getSkip().value_or(0)); // This is a SORT with a limit. The wire protocol has a single quantity @@ -610,20 +611,20 @@ QuerySolutionNode* QueryPlannerAnalysis::analyzeSort(const CanonicalQuery& query // // Not allowed for geo or text, because we assume elsewhere that those // stages appear just once. - OrNode* orn = new OrNode(); - orn->children.push_back(sort); - SortNode* sortClone = static_cast(sort->clone()); + auto orn = std::make_unique(); + orn->children.push_back(solnRoot); + SortNode* sortClone = static_cast(sortNodeRaw->clone()); sortClone->limit = 0; orn->children.push_back(sortClone); // Add ENSURE_SORTED above the OR. - EnsureSortedNode* esn = new EnsureSortedNode(); - esn->pattern = sort->pattern; - esn->children.push_back(orn); - solnRoot = esn; + auto esn = std::make_unique(); + esn->pattern = sortNodeRaw->pattern; + esn->children.push_back(orn.release()); + solnRoot = esn.release(); } } else { - sort->limit = 0; + sortNodeRaw->limit = 0; } *blockingSortOut = true; diff --git a/src/mongo/db/query/query_solution.cpp b/src/mongo/db/query/query_solution.cpp index 39ab80a4a4b..c287550e5bc 100644 --- a/src/mongo/db/query/query_solution.cpp +++ b/src/mongo/db/query/query_solution.cpp @@ -923,14 +923,14 @@ void SortNode::appendToString(mongoutils::str::stream* ss, int indent) const { } QuerySolutionNode* SortNode::clone() const { - SortNode* copy = new SortNode(); - cloneBaseData(copy); + auto copy = std::make_unique(); + cloneBaseData(copy.get()); copy->_sorts = this->_sorts; copy->pattern = this->pattern; copy->limit = this->limit; - return copy; + return copy.release(); } // -- cgit v1.2.1