From b9d4ce580ee057fc521ea531bb69a0ee91cbf8cd Mon Sep 17 00:00:00 2001 From: agirbal Date: Tue, 20 Sep 2011 22:58:58 -0700 Subject: [PATCH] SERVER-2031: setSlaveOk at each level in shell --- jstests/shell1.js | 10 ++++++++++ scripting/sm_db.cpp | 5 ++--- scripting/v8_db.cpp | 5 +---- shell/collection.js | 21 +++++++++++++++++---- shell/db.js | 10 ++++++++++ shell/mongo.js | 4 ++++ shell/mongo_vstudio.cpp | 37 ++++++++++++++++++++++++++++++++----- shell/utils.js | 2 +- 8 files changed, 77 insertions(+), 17 deletions(-) diff --git a/jstests/shell1.js b/jstests/shell1.js index 1e8437a2d13..ae2638f633a 100644 --- a/jstests/shell1.js +++ b/jstests/shell1.js @@ -5,6 +5,7 @@ shellHelper( "show", "tables;" ) shellHelper( "show", "tables" ) shellHelper( "show", "tables ;" ) +// test verbose shell option setVerboseShell(); var res = db.test.remove({a:1}); var res2 = db.test.update({a:1}, {b: 1}); @@ -13,3 +14,12 @@ setVerboseShell(false); var res = db.test.remove({a:1}); assert(res == undefined, "verbose shell 2") +// test slaveOk levels +assert(!db.getSlaveOk() && !db.test.getSlaveOk() && !db.getMongo().getSlaveOk(), "slaveOk 1"); +db.getMongo().setSlaveOk(); +assert(db.getSlaveOk() && db.test.getSlaveOk() && db.getMongo().getSlaveOk(), "slaveOk 2"); +db.setSlaveOk(false); +assert(!db.getSlaveOk() && !db.test.getSlaveOk() && db.getMongo().getSlaveOk(), "slaveOk 3"); +db.test.setSlaveOk(true); +assert(!db.getSlaveOk() && db.test.getSlaveOk() && db.getMongo().getSlaveOk(), "slaveOk 4"); + diff --git a/scripting/sm_db.cpp b/scripting/sm_db.cpp index 2a9169ba082..8acf070a9bd 100644 --- a/scripting/sm_db.cpp +++ b/scripting/sm_db.cpp @@ -245,13 +245,12 @@ namespace mongo { int nToReturn = (int) c.toNumber( argv[3] ); int nToSkip = (int) c.toNumber( argv[4] ); - bool slaveOk = c.getBoolean( obj , "slaveOk" ); int batchSize = (int) c.toNumber( argv[5] ); int options = (int)c.toNumber( argv[6] ); try { - auto_ptr cursor = conn->query( ns , q , nToReturn , nToSkip , f.nFields() ? &f : 0 , options | ( slaveOk ? QueryOption_SlaveOk : 0 ) , batchSize ); + auto_ptr cursor = conn->query( ns , q , nToReturn , nToSkip , f.nFields() ? &f : 0 , options , batchSize ); if ( ! cursor.get() ) { log() << "query failed : " << ns << " " << q << " to: " << conn->toString() << endl; JS_ReportError( cx , "error doing query: failed" ); @@ -1066,7 +1065,7 @@ zzz c.setProperty( obj , "_batchSize" , JSVAL_ZERO ); if ( argc > 9 && JSVAL_IS_NUMBER( argv[9] ) ) - c.setProperty( obj , "_options" , argv[8] ); + c.setProperty( obj , "_options" , argv[9] ); else c.setProperty( obj , "_options" , JSVAL_ZERO ); diff --git a/scripting/v8_db.cpp b/scripting/v8_db.cpp index bda549cece8..a26283d49c8 100644 --- a/scripting/v8_db.cpp +++ b/scripting/v8_db.cpp @@ -314,9 +314,6 @@ namespace mongo { fields = scope->v8ToMongo( args[2]->ToObject() ); Local mongo = args.This(); - Local slaveOkVal = mongo->Get( scope->getV8Str( "slaveOk" ) ); - jsassert( slaveOkVal->IsBoolean(), "slaveOk member invalid" ); - bool slaveOk = slaveOkVal->BooleanValue(); try { auto_ptr cursor; @@ -326,7 +323,7 @@ namespace mongo { int options = (int)(args[6]->ToNumber()->Value()); { V8Unlock u; - cursor = conn->query( ns, q , nToReturn , nToSkip , haveFields ? &fields : 0, options | ( slaveOk ? QueryOption_SlaveOk : 0 ) , batchSize ); + cursor = conn->query( ns, q , nToReturn , nToSkip , haveFields ? &fields : 0, options , batchSize ); if ( ! cursor.get() ) return v8::ThrowException( v8::String::New( "error doing query: failed" ) ); } diff --git a/shell/collection.js b/shell/collection.js index eae385e96e4..7b091506838 100644 --- a/shell/collection.js +++ b/shell/collection.js @@ -142,14 +142,14 @@ DBCollection.prototype._validateForStorage = function( o ){ }; -DBCollection.prototype.find = function( query , fields , limit , skip ){ +DBCollection.prototype.find = function( query , fields , limit , skip, batchSize, options ){ return new DBQuery( this._mongo , this._db , this , - this._fullName , this._massageObject( query ) , fields , limit , skip ); + this._fullName , this._massageObject( query ) , fields , limit , skip , batchSize , options || this.getQueryOptions() ); } -DBCollection.prototype.findOne = function( query , fields ){ +DBCollection.prototype.findOne = function( query , fields, options ){ var cursor = this._mongo.find( this._fullName , this._massageObject( query ) || {} , fields , - -1 /* limit */ , 0 /* skip*/, 0 /* batchSize */ , 0 /* options */ ); + -1 /* limit */ , 0 /* skip*/, 0 /* batchSize */ , options || this.getQueryOptions() /* options */ ); if ( ! cursor.hasNext() ) return null; var ret = cursor.next(); @@ -844,6 +844,19 @@ DBCollection.prototype.getSplitKeysForChunks = function( chunkSize ){ } +DBCollection.prototype.setSlaveOk = function( value ) { + if( value == undefined ) value = true; + this._slaveOk = value; +} +DBCollection.prototype.getSlaveOk = function() { + if (this._slaveOk != undefined) return this._slaveOk; + return this._db.getSlaveOk(); +} +DBCollection.prototype.getQueryOptions = function() { + var options = 0; + if (this.getSlaveOk()) options |= 4; + return options; +} diff --git a/shell/db.js b/shell/db.js index d5e1ea423bf..6d066d7381f 100644 --- a/shell/db.js +++ b/shell/db.js @@ -844,3 +844,13 @@ DB.autocomplete = function(obj){ } return ret; } + +DB.prototype.setSlaveOk = function( value ) { + if( value == undefined ) value = true; + this._slaveOk = value; +} + +DB.prototype.getSlaveOk = function() { + if (this._slaveOk != undefined) return this._slaveOk; + return this._mongo.getSlaveOk(); +} diff --git a/shell/mongo.js b/shell/mongo.js index d7d8c4e7415..186b68a231a 100644 --- a/shell/mongo.js +++ b/shell/mongo.js @@ -29,6 +29,10 @@ Mongo.prototype.setSlaveOk = function( value ) { this.slaveOk = value; } +Mongo.prototype.getSlaveOk = function() { + return this.slaveOk || false; +} + Mongo.prototype.getDB = function( name ){ return new DB( this , name ); } diff --git a/shell/mongo_vstudio.cpp b/shell/mongo_vstudio.cpp index fcbc4e57290..b6bf130d56a 100644 --- a/shell/mongo_vstudio.cpp +++ b/shell/mongo_vstudio.cpp @@ -1475,7 +1475,7 @@ const StringData _jscode_raw_utils = "print(\"\\tan error, even if the command succeeds.\");\n" "print(\"\\tsee also http://:28017/_replSet for additional diagnostic info\");\n" "}\n" -"rs.slaveOk = function () { return db.getMongo().setSlaveOk(); }\n" +"rs.slaveOk = function (value) { return db.getMongo().setSlaveOk(value); }\n" "rs.status = function () { return db._adminCommand(\"replSetGetStatus\"); }\n" "rs.isMaster = function () { return db.isMaster(); }\n" "rs.initiate = function (c) { return db._adminCommand({ replSetInitiate: c }); }\n" @@ -2675,6 +2675,16 @@ const StringData _jscode_raw_db = "}\n" "return ret;\n" "}\n" +"\n" +"DB.prototype.setSlaveOk = function( value ) {\n" +"if( value == undefined ) value = true;\n" +"this._slaveOk = value;\n" +"}\n" +"\n" +"DB.prototype.getSlaveOk = function() {\n" +"if (this._slaveOk != undefined) return this._slaveOk;\n" +"return this._mongo.getSlaveOk();\n" +"}\n" ; extern const JSFile db; const JSFile db = { "shell/db.js" , _jscode_raw_db }; @@ -2710,6 +2720,10 @@ const StringData _jscode_raw_mongo = "this.slaveOk = value;\n" "}\n" "\n" +"Mongo.prototype.getSlaveOk = function() {\n" +"return this.slaveOk || false;\n" +"}\n" +"\n" "Mongo.prototype.getDB = function( name ){\n" "return new DB( this , name );\n" "}\n" @@ -3342,14 +3356,14 @@ const StringData _jscode_raw_collection = "};\n" "\n" "\n" -"DBCollection.prototype.find = function( query , fields , limit , skip ){\n" +"DBCollection.prototype.find = function( query , fields , limit , skip, batchSize, options ){\n" "return new DBQuery( this._mongo , this._db , this ,\n" -"this._fullName , this._massageObject( query ) , fields , limit , skip );\n" +"this._fullName , this._massageObject( query ) , fields , limit , skip , batchSize , options || this.getQueryOptions() );\n" "}\n" "\n" -"DBCollection.prototype.findOne = function( query , fields ){\n" +"DBCollection.prototype.findOne = function( query , fields, options ){\n" "var cursor = this._mongo.find( this._fullName , this._massageObject( query ) || {} , fields ,\n" -"-1 /* limit */ , 0 /* skip*/, 0 /* batchSize */ , 0 /* options */ );\n" +"-1 /* limit */ , 0 /* skip*/, 0 /* batchSize */ , options || this.getQueryOptions() /* options */ );\n" "if ( ! cursor.hasNext() )\n" "return null;\n" "var ret = cursor.next();\n" @@ -4044,8 +4058,21 @@ const StringData _jscode_raw_collection = "\n" "}\n" "\n" +"DBCollection.prototype.setSlaveOk = function( value ) {\n" +"if( value == undefined ) value = true;\n" +"this._slaveOk = value;\n" +"}\n" "\n" +"DBCollection.prototype.getSlaveOk = function() {\n" +"if (this._slaveOk != undefined) return this._slaveOk;\n" +"return this._db.getSlaveOk();\n" +"}\n" "\n" +"DBCollection.prototype.getQueryOptions = function() {\n" +"var options = 0;\n" +"if (this.getSlaveOk()) options |= 4;\n" +"return options;\n" +"}\n" "\n" ; extern const JSFile collection; diff --git a/shell/utils.js b/shell/utils.js index e81b7c2023b..c12d1677ad0 100644 --- a/shell/utils.js +++ b/shell/utils.js @@ -1470,7 +1470,7 @@ rs.help = function () { print("\tan error, even if the command succeeds."); print("\tsee also http://:28017/_replSet for additional diagnostic info"); } -rs.slaveOk = function () { return db.getMongo().setSlaveOk(); } +rs.slaveOk = function (value) { return db.getMongo().setSlaveOk(value); } rs.status = function () { return db._adminCommand("replSetGetStatus"); } rs.isMaster = function () { return db.isMaster(); } rs.initiate = function (c) { return db._adminCommand({ replSetInitiate: c }); }