diff options
author | Arun Banala <arun.banala@mongodb.com> | 2019-06-11 16:17:36 +0100 |
---|---|---|
committer | Arun Banala <arun.banala@mongodb.com> | 2019-06-20 11:03:02 +0100 |
commit | 0877e9b24bc35aeb953d4b45ca6aba277a3a037d (patch) | |
tree | 3c3ec41249ac0e6c954e2b91a263b4412573381d | |
parent | 5fac7b938d7a4ac41ec7ad2faa16350bfe6de41a (diff) | |
download | mongo-0877e9b24bc35aeb953d4b45ca6aba277a3a037d.tar.gz |
SERVER-40830 Move 'ReplaceRootTransformation' to header file
(cherry picked from commit 4c95c46e1ef6de25e96d410369108110ce47c0e1)
-rw-r--r-- | src/mongo/db/pipeline/document_source_replace_root.cpp | 83 | ||||
-rw-r--r-- | src/mongo/db/pipeline/document_source_replace_root.h | 48 |
2 files changed, 68 insertions, 63 deletions
diff --git a/src/mongo/db/pipeline/document_source_replace_root.cpp b/src/mongo/db/pipeline/document_source_replace_root.cpp index 41ff856cdd0..deefe509bb7 100644 --- a/src/mongo/db/pipeline/document_source_replace_root.cpp +++ b/src/mongo/db/pipeline/document_source_replace_root.cpp @@ -34,9 +34,7 @@ #include <boost/smart_ptr/intrusive_ptr.hpp> #include "mongo/db/jsobj.h" -#include "mongo/db/pipeline/document.h" #include "mongo/db/pipeline/document_source_replace_root_gen.h" -#include "mongo/db/pipeline/expression.h" #include "mongo/db/pipeline/lite_parsed_document_source.h" #include "mongo/db/pipeline/value.h" @@ -44,67 +42,26 @@ namespace mongo { using boost::intrusive_ptr; -/** - * 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 { - // Extract subdocument in the form of a Value. - Value newRoot = _newRoot->evaluate(input, &_expCtx->variables); - - // The newRoot expression, if it exists, must evaluate to an object. - uassert(40228, - str::stream() - << "'newRoot' expression must evaluate to an object, but resulting value was: " - << newRoot.toString() - << ". Type of resulting value: '" - << typeName(newRoot.getType()) - << "'. Input document: " - << input.toString(), - newRoot.getType() == BSONType::Object); - - // Turn the value into a document. - MutableDocument newDoc(newRoot.getDocument()); - newDoc.copyMetaDataFrom(input); - return newDoc.freeze(); - } - - // 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>{}, {}}; - } - -private: - const boost::intrusive_ptr<ExpressionContext> _expCtx; - boost::intrusive_ptr<Expression> _newRoot; -}; +Document ReplaceRootTransformation::applyTransformation(const Document& input) { + // Extract subdocument in the form of a Value. + Value newRoot = _newRoot->evaluate(input, &_expCtx->variables); + + // The newRoot expression, if it exists, must evaluate to an object. + uassert(40228, + str::stream() + << "'newRoot' expression must evaluate to an object, but resulting value was: " + << newRoot.toString() + << ". Type of resulting value: '" + << typeName(newRoot.getType()) + << "'. Input document: " + << input.toString(), + newRoot.getType() == BSONType::Object); + + // Turn the value into a document. + MutableDocument newDoc(newRoot.getDocument()); + newDoc.copyMetaDataFrom(input); + return newDoc.freeze(); +} REGISTER_DOCUMENT_SOURCE(replaceRoot, LiteParsedDocumentSourceDefault::parse, 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 |