diff options
author | Eliot Horowitz <eliot@10gen.com> | 2010-06-29 15:34:01 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-06-29 15:34:01 -0400 |
commit | 9ed4a2a55f9d74d07f8d8044f02dd416069f2bcc (patch) | |
tree | aeae6ca6acada4199a884aeef62f9a6a0aaf7910 | |
parent | ea3fd1739cf4dcc6fac1fddae269f4c401e9cf46 (diff) | |
download | mongo-9ed4a2a55f9d74d07f8d8044f02dd416069f2bcc.tar.gz |
applyOps command with prereqs
-rw-r--r-- | db/oplog.cpp | 21 | ||||
-rw-r--r-- | jstests/apply_ops1.js | 28 | ||||
-rw-r--r-- | s/commands_public.cpp | 13 |
3 files changed, 61 insertions, 1 deletions
diff --git a/db/oplog.cpp b/db/oplog.cpp index f308bc23fcf..2232657dfa0 100644 --- a/db/oplog.cpp +++ b/db/oplog.cpp @@ -438,7 +438,7 @@ namespace mongo { virtual LockType locktype() const { return WRITE; } ApplyOpsCmd() : Command( "applyOps" ) {} virtual void help( stringstream &help ) const { - help << "examples: { applyOps : [ ] , query : {} , queryMatcher : {} } "; + help << "examples: { applyOps : [ ] , queries : [ { ns : ... , q : ... , res : ... } ] }"; } virtual bool run(const string& dbname, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) { @@ -461,6 +461,23 @@ namespace mongo { } } + if ( cmdObj["queries"].type() == Array ){ + BSONObjIterator i( cmdObj["queries"].Obj() ); + while ( i.more() ){ + BSONObj f = i.next().Obj(); + + BSONObj result = db.findOne( f["ns"].String() , f["q"].Obj() ); + + Matcher m( f["res"].Obj() ); + if ( ! m.matches( result ) ){ + stringstream ss; + ss << "pre req failed [" << f << "] got: " << result; + errmsg = ss.str(); + return false; + } + } + } + // apply int num = 0; BSONObjIterator i( ops ); @@ -474,6 +491,8 @@ namespace mongo { return true; } + + DBDirectClient db; } applyOpsCmd; diff --git a/jstests/apply_ops1.js b/jstests/apply_ops1.js index 8a40a1d0f7d..df4b2d3ce32 100644 --- a/jstests/apply_ops1.js +++ b/jstests/apply_ops1.js @@ -21,3 +21,31 @@ assert.eq( 1 , t.find().count() , "A3" ); assert.eq( o , t.findOne() , "A4" ); +res = db.runCommand( { applyOps : + [ + { "op" : "u" , "ns" : t.getFullName() , "o2" : { _id : 5 } , "o" : { $inc : { x : 1 } } } , + { "op" : "u" , "ns" : t.getFullName() , "o2" : { _id : 5 } , "o" : { $inc : { x : 1 } } } + ] + , + queries : [ { ns : t.getFullName() , q : { _id : 5 } , res : { x : 19 } } ] + } ); + +o.x++; +o.x++; + +assert.eq( 1 , t.find().count() , "B1" ); +assert.eq( o , t.findOne() , "B2" ); + + +res = db.runCommand( { applyOps : + [ + { "op" : "u" , "ns" : t.getFullName() , "o2" : { _id : 5 } , "o" : { $inc : { x : 1 } } } , + { "op" : "u" , "ns" : t.getFullName() , "o2" : { _id : 5 } , "o" : { $inc : { x : 1 } } } + ] + , + queries : [ { ns : t.getFullName() , q : { _id : 5 } , res : { x : 19 } } ] + } ); + +assert.eq( 1 , t.find().count() , "B3" ); +assert.eq( o , t.findOne() , "B4" ); + diff --git a/s/commands_public.cpp b/s/commands_public.cpp index 366e05ab10e..378007a1255 100644 --- a/s/commands_public.cpp +++ b/s/commands_public.cpp @@ -584,5 +584,18 @@ namespace mongo { return 1; } } mrCmd; + + class ApplyOpsCmd : public PublicGridCommand { + public: + ApplyOpsCmd() : PublicGridCommand( "applyOps" ){} + + virtual bool run(const string& dbName , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){ + errmsg = "applyOps not allowed through mongos"; + return false; + } + + } applyOpsCmd; + } + } |