summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_replace_root.h
diff options
context:
space:
mode:
authorArun Banala <arun.banala@mongodb.com>2019-06-11 16:17:36 +0100
committerArun Banala <arun.banala@mongodb.com>2019-06-18 16:40:30 +0100
commit4c95c46e1ef6de25e96d410369108110ce47c0e1 (patch)
tree061cf91f0a96ee4893f968f2a8a412c7d24301cb /src/mongo/db/pipeline/document_source_replace_root.h
parent08be6c5794d704154cf2b89181bb734c6e777ac4 (diff)
downloadmongo-4c95c46e1ef6de25e96d410369108110ce47c0e1.tar.gz
SERVER-40830 Move 'ReplaceRootTransformation' to header file
Diffstat (limited to 'src/mongo/db/pipeline/document_source_replace_root.h')
-rw-r--r--src/mongo/db/pipeline/document_source_replace_root.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/document_source_replace_root.h b/src/mongo/db/pipeline/document_source_replace_root.h
index 0abb4dcaff1..290919615bc 100644
--- a/src/mongo/db/pipeline/document_source_replace_root.h
+++ b/src/mongo/db/pipeline/document_source_replace_root.h
@@ -29,10 +29,58 @@
#pragma once
+#include "mongo/db/pipeline/document.h"
#include "mongo/db/pipeline/document_source_single_document_transformation.h"
+#include "mongo/db/pipeline/expression.h"
namespace mongo {
+/**
+ * This class implements the transformation logic for the $replaceRoot and $replaceWith stages.
+ */
+class ReplaceRootTransformation final : public TransformerInterface {
+public:
+ ReplaceRootTransformation(const boost::intrusive_ptr<ExpressionContext>& expCtx,
+ boost::intrusive_ptr<Expression> newRootExpression)
+ : _expCtx(expCtx), _newRoot(std::move(newRootExpression)) {}
+
+ TransformerType getType() const final {
+ return TransformerType::kReplaceRoot;
+ }
+
+ Document applyTransformation(const Document& input) final;
+
+ // Optimize the newRoot expression.
+ void optimize() final {
+ _newRoot->optimize();
+ }
+
+ Document serializeTransformation(
+ boost::optional<ExplainOptions::Verbosity> explain) const final {
+ return Document{{"newRoot", _newRoot->serialize(static_cast<bool>(explain))}};
+ }
+
+ DepsTracker::State addDependencies(DepsTracker* deps) const final {
+ _newRoot->addDependencies(deps);
+ // This stage will replace the entire document with a new document, so any existing fields
+ // will be replaced and cannot be required as dependencies.
+ return DepsTracker::State::EXHAUSTIVE_FIELDS;
+ }
+
+ DocumentSource::GetModPathsReturn getModifiedPaths() const final {
+ // Replaces the entire root, so all paths are modified.
+ return {DocumentSource::GetModPathsReturn::Type::kAllPaths, std::set<std::string>{}, {}};
+ }
+
+ const boost::intrusive_ptr<Expression>& getExpression() const {
+ return _newRoot;
+ }
+
+private:
+ const boost::intrusive_ptr<ExpressionContext> _expCtx;
+ boost::intrusive_ptr<Expression> _newRoot;
+};
+
/*
* $replaceRoot takes an object containing only an expression in the newRoot field, and replaces
* each incoming document with the result of evaluating that expression. Throws an error if the