summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_unwind.cpp
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2015-06-20 00:22:50 -0400
committerMark Benvenuto <mark.benvenuto@mongodb.com>2015-06-20 10:56:02 -0400
commit9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 (patch)
tree3814f79c10d7b490948d8cb7b112ac1dd41ceff1 /src/mongo/db/pipeline/document_source_unwind.cpp
parent01965cf52bce6976637ecb8f4a622aeb05ab256a (diff)
downloadmongo-9c2ed42daa8fbbef4a919c21ec564e2db55e8d60.tar.gz
SERVER-18579: Clang-Format - reformat code, no comment reflow
Diffstat (limited to 'src/mongo/db/pipeline/document_source_unwind.cpp')
-rw-r--r--src/mongo/db/pipeline/document_source_unwind.cpp247
1 files changed, 119 insertions, 128 deletions
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<Document> getNext();
-
- private:
- // Path to the array to unwind.
- const FieldPath _unwindPath;
-
- Value _inputArray;
- MutableDocument _output;
-
- // Document indexes of the field path components.
- vector<Position> _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<Document> getNext();
+
+private:
+ // Path to the array to unwind.
+ const FieldPath _unwindPath;
+
+ Value _inputArray;
+ MutableDocument _output;
+
+ // Document indexes of the field path components.
+ vector<Position> _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<Document> DocumentSourceUnwind::Unwinder::getNext() {
+ if (_inputArray.missing())
+ return boost::none;
- _inputArray = pathValue;
- }
-
- boost::optional<Document> 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<ExpressionContext> &pExpCtx):
- DocumentSource(pExpCtx) {
- }
+DocumentSourceUnwind::DocumentSourceUnwind(const intrusive_ptr<ExpressionContext>& pExpCtx)
+ : DocumentSource(pExpCtx) {}
- const char *DocumentSourceUnwind::getSourceName() const {
- return unwindName;
- }
-
- boost::optional<Document> DocumentSourceUnwind::getNext() {
- pExpCtx->checkForInterrupt();
+const char* DocumentSourceUnwind::getSourceName() const {
+ return unwindName;
+}
- boost::optional<Document> 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<Document> input = pSource->getNext();
- if (!input)
- return boost::none; // input exhausted
+boost::optional<Document> DocumentSourceUnwind::getNext() {
+ pExpCtx->checkForInterrupt();
- // Try to extract an output document from the new input document.
- _unwinder->resetDocument(*input);
- out = _unwinder->getNext();
- }
+ boost::optional<Document> 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<Document> 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<DocumentSource> DocumentSourceUnwind::createFromBson(
- BSONElement elem,
- const intrusive_ptr<ExpressionContext> &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<DocumentSourceUnwind> 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<DocumentSource> DocumentSourceUnwind::createFromBson(
+ BSONElement elem, const intrusive_ptr<ExpressionContext>& 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<DocumentSourceUnwind> pUnwind(new DocumentSourceUnwind(pExpCtx));
+ pUnwind->unwindPath(FieldPath(pathString));
+
+ return pUnwind;
+}
}