diff options
author | Carly Robison <crobison@caltech.edu> | 2016-08-11 12:28:07 -0400 |
---|---|---|
committer | Carly Robison <crobison@caltech.edu> | 2016-08-11 12:28:07 -0400 |
commit | 54c295e43229197db8741ad30ecef33aaca59798 (patch) | |
tree | 2ff1057580b47355f657703689c10670a2b9f3a3 | |
parent | 4fc451a03f06b696ec563144385aabe2d3d619cf (diff) | |
download | mongo-54c295e43229197db8741ad30ecef33aaca59798.tar.gz |
Revert "SERVER-5781 fixed unittest error code for addFields"
This reverts commit 4fc451a03f06b696ec563144385aabe2d3d619cf.
-rw-r--r-- | src/mongo/db/pipeline/document_source_replace_root.cpp | 107 | ||||
-rw-r--r-- | src/mongo/db/pipeline/document_source_test.cpp | 2 |
2 files changed, 51 insertions, 58 deletions
diff --git a/src/mongo/db/pipeline/document_source_replace_root.cpp b/src/mongo/db/pipeline/document_source_replace_root.cpp index 18b0063f03d..9b2b594377b 100644 --- a/src/mongo/db/pipeline/document_source_replace_root.cpp +++ b/src/mongo/db/pipeline/document_source_replace_root.cpp @@ -41,63 +41,6 @@ namespace mongo { using boost::intrusive_ptr; -/** - * This class implements the transformation logic for the $replaceRoot stage. - */ -class ReplaceRootTransformation final : public DocumentSourceSingleDocumentTransformation::TransformerInterface { - -public: - virtual Document applyTransformation(Document input) { - // Extract subdocument in the form of a Value. - _variables->setRoot(*input); - Value newRoot = _newRoot->evaluate(_variables.get()); - - // The newRoot expression must evaluate to a valid Value. - uassert(40232, - str::stream() - << " 'newRoot' argument to $replaceRoot stage must evaluate to a valid Value, " - << "try ensuring that your field path(s) exist by prepending a " - << "$match: {<path>: $exists} aggregation stage.", - !newRoot.missing()); - - // The newRoot expression, if it exists, must evaluate to an object. - uassert(40228, - str::stream() - << " 'newRoot' argument to $replaceRoot stage must evaluate to an object, but got " - << typeName(newRoot.getType()) - << " try ensuring that it evaluates to an object by prepending a " - << "$match: {<path>: {$type: 'object'}} aggregation stage.", - newRoot.getType() == Object); - - // Turn the value into a document. - return newRoot.getDocument(); - } - - // Optimize the newRoot expression. - virtual void optimize() { - _newRoot = _newRoot->optimize(); - } - - virtual Document serialize(bool explain) { - return Document{{getSourceName(), Document{{"newRoot", _newRoot->serialize(explain)}}}}; - } - - virtual DocumentSource::GetDepsReturn addDependencies(DepsTracker* deps) { - _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 DocumentSource::EXHAUSTIVE_FIELDS; - } - - virtual void injectExpressionContext( - const boost::intrusive_ptr<ExpressionContext>& pExpCtx) {} - -private: - std::unique_ptr<Variables> _variables; - boost::intrusive_ptr<Expression> _newRoot; - -}; - DocumentSourceReplaceRoot::DocumentSourceReplaceRoot( const intrusive_ptr<ExpressionContext>& pExpCtx, const intrusive_ptr<Expression> expr) : DocumentSource(pExpCtx), _newRoot(expr) {} @@ -108,6 +51,40 @@ const char* DocumentSourceReplaceRoot::getSourceName() const { return "$replaceRoot"; } +boost::optional<Document> DocumentSourceReplaceRoot::getNext() { + pExpCtx->checkForInterrupt(); + + // Get the next input document. + boost::optional<Document> input = pSource->getNext(); + if (!input) { + return boost::none; + } + + // Extract subdocument in the form of a Value. + _variables->setRoot(*input); + Value newRoot = _newRoot->evaluate(_variables.get()); + + // The newRoot expression must evaluate to a valid Value. + uassert(40232, + str::stream() + << " 'newRoot' argument to $replaceRoot stage must evaluate to a valid Value, " + << "try ensuring that your field path(s) exist by prepending a " + << "$match: {<path>: $exists} aggregation stage.", + !newRoot.missing()); + + // The newRoot expression, if it exists, must evaluate to an object. + uassert(40228, + str::stream() + << " 'newRoot' argument to $replaceRoot stage must evaluate to an object, but got " + << typeName(newRoot.getType()) + << " try ensuring that it evaluates to an object by prepending a " + << "$match: {<path>: {$type: 'object'}} aggregation stage.", + newRoot.getType() == Object); + + // Turn the value into a document. + return newRoot.getDocument(); +} + Pipeline::SourceContainer::iterator DocumentSourceReplaceRoot::optimizeAt( Pipeline::SourceContainer::iterator itr, Pipeline::SourceContainer* container) { invariant(*itr == this); @@ -122,6 +99,16 @@ Pipeline::SourceContainer::iterator DocumentSourceReplaceRoot::optimizeAt( return std::next(itr); } +intrusive_ptr<DocumentSource> DocumentSourceReplaceRoot::optimize() { + _newRoot = _newRoot->optimize(); + return this; +} + +// Serialize the expression at newRoot too. +Value DocumentSourceReplaceRoot::serialize(bool explain) const { + return Value(Document{{getSourceName(), Document{{"newRoot", _newRoot->serialize(explain)}}}}); +} + intrusive_ptr<DocumentSource> DocumentSourceReplaceRoot::createFromBson( BSONElement elem, const intrusive_ptr<ExpressionContext>& pExpCtx) { @@ -159,4 +146,10 @@ intrusive_ptr<DocumentSource> DocumentSourceReplaceRoot::createFromBson( return source; } +DocumentSource::GetDepsReturn DocumentSourceReplaceRoot::getDependencies(DepsTracker* deps) const { + _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 EXHAUSTIVE_FIELDS; +} } // namespace mongo diff --git a/src/mongo/db/pipeline/document_source_test.cpp b/src/mongo/db/pipeline/document_source_test.cpp index 4b9e1bcf6ab..d52eb018e7b 100644 --- a/src/mongo/db/pipeline/document_source_test.cpp +++ b/src/mongo/db/pipeline/document_source_test.cpp @@ -4822,7 +4822,7 @@ TEST_F(AddFieldsTest, ShouldErrorOnNonObjectSpec) { << "foo"); BSONElement specElement = spec.firstElement(); ASSERT_THROWS_CODE( - DocumentSourceAddFields::createFromBson(specElement, ctx()), UserException, 40272); + DocumentSourceAddFields::createFromBson(specElement, ctx()), UserException, 40269); } // Verify that mutiple documents can be processed in a row with the addFields stage. |