summaryrefslogtreecommitdiff
path: root/src/mongo/db/matcher/expression_array.cpp
diff options
context:
space:
mode:
authorJustin Seyster <justin.seyster@mongodb.com>2017-09-28 18:20:41 -0400
committerJustin Seyster <justin.seyster@mongodb.com>2017-09-28 18:20:41 -0400
commit3cf4e0593c394dd7eb45d8000d76b5dc73a3f425 (patch)
tree26559cb6599a592e7ca0368f2f31abe22dea47c7 /src/mongo/db/matcher/expression_array.cpp
parentf88f6f43b7ae2af0286437da8f00c0079ed99145 (diff)
downloadmongo-3cf4e0593c394dd7eb45d8000d76b5dc73a3f425.tar.gz
SERVER-30991 Introduce MatchExpression::optimize().
This patch refactors CanonicalQuery::normalizeTree() so that the normalization logic for each type of MatchExpression goes with the class, rather than all the optimization rules getting bundled into one huge else if chain. We wanted something along the lines of an optimize() member function that would optimize 'this' and return the optimized result (possibly the same object). However, we also wanted unique_ptr semantics, so that the optimize function creates a new tree that does not include the original object, it autmotatically gets destroyed. There's no way to specify a member function that accepts a unique_ptr 'this' value. To get around that, we provide a getOptimizer() private function that returns a function with the unique_ptr signature we want: unique_ptr<MatchExpression> -> unique_ptr<MatchExpression>. This way, we still get to replace our if else chain with virtual dispatch, and we can maintain unique_ptr semantics for the MatchExpression tree.
Diffstat (limited to 'src/mongo/db/matcher/expression_array.cpp')
-rw-r--r--src/mongo/db/matcher/expression_array.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/mongo/db/matcher/expression_array.cpp b/src/mongo/db/matcher/expression_array.cpp
index b80e321b976..84c544df2bf 100644
--- a/src/mongo/db/matcher/expression_array.cpp
+++ b/src/mongo/db/matcher/expression_array.cpp
@@ -108,6 +108,14 @@ void ElemMatchObjectMatchExpression::serialize(BSONObjBuilder* out) const {
out->append(path(), BSON("$elemMatch" << subBob.obj()));
}
+MatchExpression::ExpressionOptimizerFunc ElemMatchObjectMatchExpression::getOptimizer() const {
+ return [](std::unique_ptr<MatchExpression> expression) {
+ auto& elemExpression = static_cast<ElemMatchObjectMatchExpression&>(*expression);
+ elemExpression._sub = MatchExpression::optimize(std::move(elemExpression._sub));
+
+ return expression;
+ };
+}
// -------
@@ -184,6 +192,19 @@ void ElemMatchValueMatchExpression::serialize(BSONObjBuilder* out) const {
out->append(path(), BSON("$elemMatch" << emBob.obj()));
}
+MatchExpression::ExpressionOptimizerFunc ElemMatchValueMatchExpression::getOptimizer() const {
+ return [](std::unique_ptr<MatchExpression> expression) {
+ auto& subs = static_cast<ElemMatchValueMatchExpression&>(*expression)._subs;
+
+ for (MatchExpression*& subExpression : subs) {
+ auto optimizedSubExpression =
+ MatchExpression::optimize(std::unique_ptr<MatchExpression>(subExpression));
+ subExpression = optimizedSubExpression.release();
+ }
+
+ return expression;
+ };
+}
// ---------