summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source.cpp
diff options
context:
space:
mode:
authorCharlie Swanson <cswanson310@gmail.com>2015-06-24 10:03:42 -0400
committerCharlie Swanson <cswanson310@gmail.com>2015-06-24 18:18:18 -0400
commit0e9371dbed499f6f2ab5066fffdc746747351c6d (patch)
tree8a15fd87d8004fbcf324e02fe52586f8f509deab /src/mongo/db/pipeline/document_source.cpp
parent92750368e7a65031f4545d7c52ba268033b94a74 (diff)
downloadmongo-0e9371dbed499f6f2ab5066fffdc746747351c6d.tar.gz
SERVER-19105 Define macro for registering DocumentSources
Diffstat (limited to 'src/mongo/db/pipeline/document_source.cpp')
-rw-r--r--src/mongo/db/pipeline/document_source.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/document_source.cpp b/src/mongo/db/pipeline/document_source.cpp
index 57d12a7c85c..718fa618300 100644
--- a/src/mongo/db/pipeline/document_source.cpp
+++ b/src/mongo/db/pipeline/document_source.cpp
@@ -31,15 +31,47 @@
#include "mongo/db/pipeline/document_source.h"
#include "mongo/db/pipeline/expression_context.h"
#include "mongo/db/pipeline/value.h"
+#include "mongo/util/string_map.h"
namespace mongo {
+using Parser = DocumentSource::Parser;
using boost::intrusive_ptr;
+using std::string;
using std::vector;
DocumentSource::DocumentSource(const intrusive_ptr<ExpressionContext>& pCtx)
: pSource(NULL), pExpCtx(pCtx) {}
+namespace {
+// Used to keep track of which DocumentSources are registered under which name.
+static StringMap<Parser> parserMap;
+} // namespace
+
+void DocumentSource::registerParser(string name, Parser parser) {
+ auto it = parserMap.find(name);
+ massert(28707,
+ str::stream() << "Duplicate document source (" << name << ") registered.",
+ it == parserMap.end());
+ parserMap[name] = parser;
+}
+
+intrusive_ptr<DocumentSource> DocumentSource::parse(const intrusive_ptr<ExpressionContext> expCtx,
+ BSONObj stageObj) {
+ uassert(16435,
+ "A pipeline stage specification object must contain exactly one field.",
+ stageObj.nFields() == 1);
+ BSONElement stageSpec = stageObj.firstElement();
+ auto stageName = stageSpec.fieldNameStringData();
+
+ // Get the registered parser and call that.
+ auto it = parserMap.find(stageName);
+ uassert(16436,
+ str::stream() << "Unrecognized pipeline stage name: '" << stageName << "'",
+ it != parserMap.end());
+ return it->second(stageSpec, expCtx);
+}
+
const char* DocumentSource::getSourceName() const {
static const char unknown[] = "[UNKNOWN]";
return unknown;