From 9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 Mon Sep 17 00:00:00 2001 From: Mark Benvenuto Date: Sat, 20 Jun 2015 00:22:50 -0400 Subject: SERVER-18579: Clang-Format - reformat code, no comment reflow --- src/mongo/db/pipeline/document_source_unwind.cpp | 247 +++++++++++------------ 1 file changed, 119 insertions(+), 128 deletions(-) (limited to 'src/mongo/db/pipeline/document_source_unwind.cpp') diff --git a/src/mongo/db/pipeline/document_source_unwind.cpp b/src/mongo/db/pipeline/document_source_unwind.cpp index 2caf7f95c64..8ec126b967c 100644 --- a/src/mongo/db/pipeline/document_source_unwind.cpp +++ b/src/mongo/db/pipeline/document_source_unwind.cpp @@ -36,150 +36,141 @@ namespace mongo { - using boost::intrusive_ptr; - using std::string; - using std::vector; - - /** Helper class to unwind array from a single document. */ - class DocumentSourceUnwind::Unwinder { - public: - /** @param unwindPath is the field path to the array to unwind. */ - Unwinder(const FieldPath& unwindPath); - /** Reset the unwinder to unwind a new document. */ - void resetDocument(const Document& document); - - /** - * @return the next document unwound from the document provided to resetDocument(), using - * the current value in the array located at the provided unwindPath. - * - * Returns boost::none if the array is exhausted. - */ - boost::optional getNext(); - - private: - // Path to the array to unwind. - const FieldPath _unwindPath; - - Value _inputArray; - MutableDocument _output; - - // Document indexes of the field path components. - vector _unwindPathFieldIndexes; - // Index into the _inputArray to return next. - size_t _index; - }; - - DocumentSourceUnwind::Unwinder::Unwinder(const FieldPath& unwindPath): - _unwindPath(unwindPath) { +using boost::intrusive_ptr; +using std::string; +using std::vector; + +/** Helper class to unwind array from a single document. */ +class DocumentSourceUnwind::Unwinder { +public: + /** @param unwindPath is the field path to the array to unwind. */ + Unwinder(const FieldPath& unwindPath); + /** Reset the unwinder to unwind a new document. */ + void resetDocument(const Document& document); + + /** + * @return the next document unwound from the document provided to resetDocument(), using + * the current value in the array located at the provided unwindPath. + * + * Returns boost::none if the array is exhausted. + */ + boost::optional getNext(); + +private: + // Path to the array to unwind. + const FieldPath _unwindPath; + + Value _inputArray; + MutableDocument _output; + + // Document indexes of the field path components. + vector _unwindPathFieldIndexes; + // Index into the _inputArray to return next. + size_t _index; +}; + +DocumentSourceUnwind::Unwinder::Unwinder(const FieldPath& unwindPath) : _unwindPath(unwindPath) {} + +void DocumentSourceUnwind::Unwinder::resetDocument(const Document& document) { + // Reset document specific attributes. + _inputArray = Value(); + _output.reset(document); + _unwindPathFieldIndexes.clear(); + _index = 0; + + Value pathValue = document.getNestedField(_unwindPath, &_unwindPathFieldIndexes); + if (pathValue.nullish()) { + // The path does not exist or is null. + return; } - void DocumentSourceUnwind::Unwinder::resetDocument(const Document& document) { - - // Reset document specific attributes. - _inputArray = Value(); - _output.reset(document); - _unwindPathFieldIndexes.clear(); - _index = 0; + _inputArray = pathValue; +} - Value pathValue = document.getNestedField(_unwindPath, &_unwindPathFieldIndexes); - if (pathValue.nullish()) { - // The path does not exist or is null. - return; - } +boost::optional DocumentSourceUnwind::Unwinder::getNext() { + if (_inputArray.missing()) + return boost::none; - _inputArray = pathValue; - } - - boost::optional DocumentSourceUnwind::Unwinder::getNext() { - if (_inputArray.missing()) - return boost::none; + // If needed, this will automatically clone all the documents along the + // field path so that the end values are not shared across documents + // that have come out of this pipeline operator. This is a partial deep + // clone. Because the value at the end will be replaced, everything + // along the path leading to that will be replaced in order not to share + // that change with any other clones (or the original). - // If needed, this will automatically clone all the documents along the - // field path so that the end values are not shared across documents - // that have come out of this pipeline operator. This is a partial deep - // clone. Because the value at the end will be replaced, everything - // along the path leading to that will be replaced in order not to share - // that change with any other clones (or the original). - - if (_inputArray.getType() == Array) { - if (_index == _inputArray.getArrayLength()) - return boost::none; - _output.setNestedField(_unwindPathFieldIndexes, _inputArray[_index]); - } - else if (_index > 0) { + if (_inputArray.getType() == Array) { + if (_index == _inputArray.getArrayLength()) return boost::none; - } - else { - //_output.setNestedField(_unwindPathFieldIndexes, _inputArray); - } - _index++; - return _output.peek(); + _output.setNestedField(_unwindPathFieldIndexes, _inputArray[_index]); + } else if (_index > 0) { + return boost::none; + } else { + //_output.setNestedField(_unwindPathFieldIndexes, _inputArray); } + _index++; + return _output.peek(); +} - const char DocumentSourceUnwind::unwindName[] = "$unwind"; +const char DocumentSourceUnwind::unwindName[] = "$unwind"; - DocumentSourceUnwind::DocumentSourceUnwind( - const intrusive_ptr &pExpCtx): - DocumentSource(pExpCtx) { - } +DocumentSourceUnwind::DocumentSourceUnwind(const intrusive_ptr& pExpCtx) + : DocumentSource(pExpCtx) {} - const char *DocumentSourceUnwind::getSourceName() const { - return unwindName; - } - - boost::optional DocumentSourceUnwind::getNext() { - pExpCtx->checkForInterrupt(); +const char* DocumentSourceUnwind::getSourceName() const { + return unwindName; +} - boost::optional out = _unwinder->getNext(); - while (!out) { - // No more elements in array currently being unwound. This will loop if the input - // document is missing the unwind field or has an empty array. - boost::optional input = pSource->getNext(); - if (!input) - return boost::none; // input exhausted +boost::optional DocumentSourceUnwind::getNext() { + pExpCtx->checkForInterrupt(); - // Try to extract an output document from the new input document. - _unwinder->resetDocument(*input); - out = _unwinder->getNext(); - } + boost::optional out = _unwinder->getNext(); + while (!out) { + // No more elements in array currently being unwound. This will loop if the input + // document is missing the unwind field or has an empty array. + boost::optional input = pSource->getNext(); + if (!input) + return boost::none; // input exhausted - return out; + // Try to extract an output document from the new input document. + _unwinder->resetDocument(*input); + out = _unwinder->getNext(); } - Value DocumentSourceUnwind::serialize(bool explain) const { - verify(_unwindPath); - return Value(DOC(getSourceName() << _unwindPath->getPath(true))); - } + return out; +} - DocumentSource::GetDepsReturn DocumentSourceUnwind::getDependencies(DepsTracker* deps) const { - deps->fields.insert(_unwindPath->getPath(false)); - return SEE_NEXT; - } +Value DocumentSourceUnwind::serialize(bool explain) const { + verify(_unwindPath); + return Value(DOC(getSourceName() << _unwindPath->getPath(true))); +} - void DocumentSourceUnwind::unwindPath(const FieldPath &fieldPath) { - // Can't set more than one unwind path. - uassert(15979, str::stream() << unwindName << "can't unwind more than one path", - !_unwindPath); - // Record the unwind path. - _unwindPath.reset(new FieldPath(fieldPath)); - _unwinder.reset(new Unwinder(fieldPath)); - } +DocumentSource::GetDepsReturn DocumentSourceUnwind::getDependencies(DepsTracker* deps) const { + deps->fields.insert(_unwindPath->getPath(false)); + return SEE_NEXT; +} - intrusive_ptr DocumentSourceUnwind::createFromBson( - BSONElement elem, - const intrusive_ptr &pExpCtx) { - /* - The value of $unwind should just be a field path. - */ - uassert(15981, str::stream() << "the " << unwindName << - " field path must be specified as a string", - elem.type() == String); - - string prefixedPathString(elem.str()); - string pathString(Expression::removeFieldPrefix(prefixedPathString)); - intrusive_ptr pUnwind(new DocumentSourceUnwind(pExpCtx)); - pUnwind->unwindPath(FieldPath(pathString)); - - return pUnwind; - } +void DocumentSourceUnwind::unwindPath(const FieldPath& fieldPath) { + // Can't set more than one unwind path. + uassert(15979, str::stream() << unwindName << "can't unwind more than one path", !_unwindPath); + // Record the unwind path. + _unwindPath.reset(new FieldPath(fieldPath)); + _unwinder.reset(new Unwinder(fieldPath)); +} + +intrusive_ptr DocumentSourceUnwind::createFromBson( + BSONElement elem, const intrusive_ptr& pExpCtx) { + /* + The value of $unwind should just be a field path. + */ + uassert(15981, + str::stream() << "the " << unwindName << " field path must be specified as a string", + elem.type() == String); + + string prefixedPathString(elem.str()); + string pathString(Expression::removeFieldPrefix(prefixedPathString)); + intrusive_ptr pUnwind(new DocumentSourceUnwind(pExpCtx)); + pUnwind->unwindPath(FieldPath(pathString)); + + return pUnwind; +} } -- cgit v1.2.1