From 21f5028d4404ea9584d53bdb81dd63ca8a505d6f Mon Sep 17 00:00:00 2001 From: Bernard Gorman Date: Thu, 21 Sep 2017 02:22:00 -0400 Subject: SERVER-30399 Add caching for $lookup non-correlated sub-pipeline prefix --- src/mongo/db/pipeline/document_source.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/mongo/db/pipeline/document_source.cpp') diff --git a/src/mongo/db/pipeline/document_source.cpp b/src/mongo/db/pipeline/document_source.cpp index a9d9f74181a..d51ad84ab67 100644 --- a/src/mongo/db/pipeline/document_source.cpp +++ b/src/mongo/db/pipeline/document_source.cpp @@ -32,6 +32,7 @@ #include "mongo/db/matcher/expression_algo.h" #include "mongo/db/pipeline/document_source_match.h" +#include "mongo/db/pipeline/document_source_sequential_document_cache.h" #include "mongo/db/pipeline/expression_context.h" #include "mongo/db/pipeline/field_path.h" #include "mongo/db/pipeline/value.h" @@ -163,7 +164,15 @@ splitMatchByModifiedFields(const boost::intrusive_ptr& matc Pipeline::SourceContainer::iterator DocumentSource::optimizeAt( Pipeline::SourceContainer::iterator itr, Pipeline::SourceContainer* container) { - invariant(*itr == this && (std::next(itr) != container->end())); + invariant(*itr == this); + + // If we are at the end of the pipeline, only optimize in the special case of a cache stage. + if (std::next(itr) == container->end()) { + return dynamic_cast(this) + ? doOptimizeAt(itr, container) + : container->end(); + } + auto nextMatch = dynamic_cast((*std::next(itr)).get()); if (constraints().canSwapWithMatch && nextMatch && !nextMatch->isTextQuery()) { // We're allowed to swap with a $match and the stage after us is a $match. Furthermore, the -- cgit v1.2.1