summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source.cpp
diff options
context:
space:
mode:
authorBernard Gorman <bernard.gorman@gmail.com>2017-09-21 02:22:00 -0400
committerBernard Gorman <bernard.gorman@gmail.com>2017-09-24 15:59:10 -0400
commit21f5028d4404ea9584d53bdb81dd63ca8a505d6f (patch)
treeec6e458b171de330c0e4756603ba95ba3df06834 /src/mongo/db/pipeline/document_source.cpp
parent280981d3a4cadeb91da9fd69864924e61d7ef99a (diff)
downloadmongo-21f5028d4404ea9584d53bdb81dd63ca8a505d6f.tar.gz
SERVER-30399 Add caching for $lookup non-correlated sub-pipeline prefix
Diffstat (limited to 'src/mongo/db/pipeline/document_source.cpp')
-rw-r--r--src/mongo/db/pipeline/document_source.cpp11
1 files changed, 10 insertions, 1 deletions
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<DocumentSourceMatch>& 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<DocumentSourceSequentialDocumentCache*>(this)
+ ? doOptimizeAt(itr, container)
+ : container->end();
+ }
+
auto nextMatch = dynamic_cast<DocumentSourceMatch*>((*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