/**
* Copyright (C) 2016 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
#pragma once
#include "mongo/db/pipeline/document_source.h"
#include "mongo/db/pipeline/field_path.h"
namespace mongo {
class DocumentSourceUnwind final : public DocumentSource {
public:
// virtuals from DocumentSource
GetNextResult getNext() final;
const char* getSourceName() const final;
Value serialize(boost::optional explain = boost::none) const final;
BSONObjSet getOutputSorts() final;
/**
* Returns the unwound path, and the 'includeArrayIndex' path, if specified.
*/
GetModPathsReturn getModifiedPaths() const final;
StageConstraints constraints(Pipeline::SplitState pipeState) const final {
StageConstraints constraints(StreamType::kStreaming,
PositionRequirement::kNone,
HostTypeRequirement::kNone,
DiskUseRequirement::kNoDiskUse,
FacetRequirement::kAllowed);
constraints.canSwapWithMatch = true;
return constraints;
}
GetDepsReturn getDependencies(DepsTracker* deps) const final;
/**
* Creates a new $unwind DocumentSource from a BSON specification.
*/
static boost::intrusive_ptr createFromBson(
BSONElement elem, const boost::intrusive_ptr& pExpCtx);
static boost::intrusive_ptr create(
const boost::intrusive_ptr& expCtx,
const std::string& path,
bool includeNullIfEmptyOrMissing,
const boost::optional& includeArrayIndex);
std::string getUnwindPath() const {
return _unwindPath.fullPath();
}
bool preserveNullAndEmptyArrays() const {
return _preserveNullAndEmptyArrays;
}
const boost::optional& indexPath() const {
return _indexPath;
}
private:
DocumentSourceUnwind(const boost::intrusive_ptr& pExpCtx,
const FieldPath& fieldPath,
bool includeNullIfEmptyOrMissing,
const boost::optional& includeArrayIndex);
// Configuration state.
const FieldPath _unwindPath;
// Documents that have a nullish value, or an empty array for the field '_unwindPath', will pass
// through the $unwind stage unmodified if '_preserveNullAndEmptyArrays' is true.
const bool _preserveNullAndEmptyArrays;
// If set, the $unwind stage will include the array index in the specified path, overwriting any
// existing value, setting to null when the value was a non-array or empty array.
const boost::optional _indexPath;
// Iteration state.
class Unwinder;
std::unique_ptr _unwinder;
};
} // namespace mongo