summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2011-04-04 19:19:48 -0400
committerEliot Horowitz <eliot@10gen.com>2011-05-03 11:17:25 -0400
commit8a9f531efe371b8e79a82b0014d81f23927ff6fa (patch)
treec6533854ef7cb6216a25eaa39c16e62c02e1872b
parentd050d236e5cfc0af296e84d57675a4d70337ada6 (diff)
downloadmongo-8a9f531efe371b8e79a82b0014d81f23927ff6fa.tar.gz
C++ driver now supports 1.8 M/R options SERVER-2893
-rw-r--r--client/dbclient.cpp7
-rw-r--r--client/dbclient.h19
-rw-r--r--client/examples/clientTest.cpp22
3 files changed, 42 insertions, 6 deletions
diff --git a/client/dbclient.cpp b/client/dbclient.cpp
index a68b1af2adc..bb2419951b4 100644
--- a/client/dbclient.cpp
+++ b/client/dbclient.cpp
@@ -442,15 +442,16 @@ namespace mongo {
return false;
}
- BSONObj DBClientWithCommands::mapreduce(const string &ns, const string &jsmapf, const string &jsreducef, BSONObj query, const string& outputcolname) {
+ DBClientWithCommands::MROutput DBClientWithCommands::MRInline (BSON("inline" << 1));
+
+ BSONObj DBClientWithCommands::mapreduce(const string &ns, const string &jsmapf, const string &jsreducef, BSONObj query, MROutput output) {
BSONObjBuilder b;
b.append("mapreduce", nsGetCollection(ns));
b.appendCode("map", jsmapf);
b.appendCode("reduce", jsreducef);
if( !query.isEmpty() )
b.append("query", query);
- if( !outputcolname.empty() )
- b.append("out", outputcolname);
+ b.append("out", output.out);
BSONObj info;
runCommand(nsGetDB(ns), b.done(), info);
return info;
diff --git a/client/dbclient.h b/client/dbclient.h
index 9cb6571ca85..9bc71fd0aac 100644
--- a/client/dbclient.h
+++ b/client/dbclient.h
@@ -528,6 +528,19 @@ namespace mongo {
bool setDbProfilingLevel(const string &dbname, ProfilingLevel level, BSONObj *info = 0);
bool getDbProfilingLevel(const string &dbname, ProfilingLevel& level, BSONObj *info = 0);
+
+ /** This implicitly converts from char*, string, and BSONObj to be an argument to mapreduce
+ You shouldn't need to explicitly construct this
+ */
+ struct MROutput {
+ MROutput(const char* collection) : out(BSON("replace" << collection)) {}
+ MROutput(const string& collection) : out(BSON("replace" << collection)) {}
+ MROutput(const BSONObj& obj) : out(obj) {}
+
+ BSONObj out;
+ };
+ static MROutput MRInline;
+
/** Run a map/reduce job on the server.
See http://www.mongodb.org/display/DOCS/MapReduce
@@ -536,8 +549,8 @@ namespace mongo {
jsmapf javascript map function code
jsreducef javascript reduce function code.
query optional query filter for the input
- output optional permanent output collection name. if not specified server will
- generate a temporary collection and return its name.
+ output either a string collection name or an object representing output type
+ if not specified uses inline output type
returns a result object which contains:
{ result : <collection_name>,
@@ -551,7 +564,7 @@ namespace mongo {
result.getField("ok").trueValue()
on the result to check if ok.
*/
- BSONObj mapreduce(const string &ns, const string &jsmapf, const string &jsreducef, BSONObj query = BSONObj(), const string& output = "");
+ BSONObj mapreduce(const string &ns, const string &jsmapf, const string &jsreducef, BSONObj query = BSONObj(), MROutput output = MRInline);
/** Run javascript code on the database server.
dbname database SavedContext in which the code runs. The javascript variable 'db' will be assigned
diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp
index bd4432eb0cf..96c014e245c 100644
--- a/client/examples/clientTest.cpp
+++ b/client/examples/clientTest.cpp
@@ -224,5 +224,27 @@ int main( int argc, const char **argv ) {
}
}
+ {
+ //Map Reduce (this mostly just tests that it compiles with all output types)
+ const string ns = "test.mr";
+ conn.insert(ns, BSON("a" << 1));
+ conn.insert(ns, BSON("a" << 1));
+
+ const char* map = "function() { emit(this.a, 1); }";
+ const char* reduce = "function(key, values) { return Array.sum(values); }";
+
+ const string outcoll = ns + ".out";
+
+ BSONObj out;
+ out = conn.mapreduce(ns, map, reduce, BSONObj()); // default to inline
+ //MONGO_PRINT(out);
+ out = conn.mapreduce(ns, map, reduce, BSONObj(), outcoll);
+ //MONGO_PRINT(out);
+ out = conn.mapreduce(ns, map, reduce, BSONObj(), outcoll.c_str());
+ //MONGO_PRINT(out);
+ out = conn.mapreduce(ns, map, reduce, BSONObj(), BSON("reduce" << outcoll));
+ //MONGO_PRINT(out);
+ }
+
cout << "client test finished!" << endl;
}