mirror of
https://github.com/mongodb/mongo.git
synced 2024-11-28 07:59:02 +01:00
SERVER-17286 add --readMode flag to shell
Passing '--readMode commands' to the shell will now cause it to run find using the find command, combined with wire protocol OP_GET_MORE messages. The default readMode remains OP_QUERY style find.
This commit is contained in:
parent
a33b1a8688
commit
2729de652a
@ -252,7 +252,32 @@ Mongo.prototype.writeMode = function() {
|
||||
return this._writeMode;
|
||||
};
|
||||
|
||||
//
|
||||
// Whether to use find command versus OP_QUERY style find.
|
||||
//
|
||||
|
||||
Mongo.prototype.useFindCommand = function() {
|
||||
return (this.readMode() === "commands");
|
||||
}
|
||||
|
||||
Mongo.prototype.readMode = function() {
|
||||
if ("_readMode" in this) {
|
||||
// We already have determined our read mode. Just return it.
|
||||
return this._readMode;
|
||||
}
|
||||
|
||||
// Determine read mode based on shell params.
|
||||
//
|
||||
// TODO: Detect what to use based on wire protocol version.
|
||||
if (_readMode) {
|
||||
this._readMode = _readMode();
|
||||
}
|
||||
else {
|
||||
this._readMode = "compatibility";
|
||||
}
|
||||
|
||||
return this._readMode;
|
||||
}
|
||||
|
||||
//
|
||||
// Write Concern can be set at the connection level, and is used for all write operations unless
|
||||
|
@ -80,8 +80,25 @@ DBQuery.prototype._checkModify = function(){
|
||||
DBQuery.prototype._exec = function(){
|
||||
if ( ! this._cursor ){
|
||||
assert.eq( 0 , this._numReturned );
|
||||
this._cursor = this._mongo.find( this._ns , this._query , this._fields , this._limit , this._skip , this._batchSize , this._options );
|
||||
this._cursorSeen = 0;
|
||||
|
||||
if (this._mongo.useFindCommand() && this._collection.getName().indexOf("$cmd") !== 0) {
|
||||
// Run the query as a find command. Since runCommand() is implemented by running
|
||||
// a findOne() against the $cmd collection, we have to make sure that we don't try
|
||||
// to run a find command against the $cmd collection.
|
||||
var findCmd = this._convertToCommand();
|
||||
var cmdRes = this._db.runCommand(findCmd);
|
||||
this._cursor = new DBCommandCursor(this._mongo, cmdRes);
|
||||
}
|
||||
else {
|
||||
this._cursor = this._mongo.find(this._ns,
|
||||
this._query,
|
||||
this._fields,
|
||||
this._limit,
|
||||
this._skip,
|
||||
this._batchSize,
|
||||
this._options);
|
||||
}
|
||||
}
|
||||
return this._cursor;
|
||||
}
|
||||
|
@ -142,6 +142,12 @@ namespace mongo {
|
||||
"mode to determine how writes are done:"
|
||||
" commands, compatibility, legacy").hidden();
|
||||
|
||||
options->addOptionChaining("readMode",
|
||||
"readMode",
|
||||
moe::String,
|
||||
"mode to determine how .find() queries are done:"
|
||||
" commands, compatibility").hidden();
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
@ -260,6 +266,16 @@ namespace mongo {
|
||||
}
|
||||
shellGlobalParams.writeMode = mode;
|
||||
}
|
||||
if (params.count("readMode")) {
|
||||
std::string mode = params["readMode"].as<string>();
|
||||
if (mode != "commands" && mode != "compatibility") {
|
||||
throw MsgAssertionException(17397,
|
||||
mongoutils::str::stream()
|
||||
<< "Unknown readMode option: '" << mode
|
||||
<< "'. Valid modes are: {commands, compatibility}");
|
||||
}
|
||||
shellGlobalParams.readMode = mode;
|
||||
}
|
||||
|
||||
/* This is a bit confusing, here are the rules:
|
||||
*
|
||||
|
@ -66,9 +66,12 @@ namespace mongo {
|
||||
bool useWriteCommandsDefault;
|
||||
std::string writeMode;
|
||||
|
||||
std::string readMode;
|
||||
|
||||
ShellGlobalParams() : autoKillOp(false),
|
||||
useWriteCommandsDefault(true),
|
||||
writeMode("commands") {
|
||||
writeMode("commands"),
|
||||
readMode("compatibility") {
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -214,6 +214,10 @@ namespace mongo {
|
||||
return BSON("" << shellGlobalParams.writeMode);
|
||||
}
|
||||
|
||||
BSONObj readMode(const BSONObj&, void*) {
|
||||
return BSON("" << shellGlobalParams.readMode);
|
||||
}
|
||||
|
||||
BSONObj interpreterVersion(const BSONObj& a, void* data) {
|
||||
uassert( 16453, "interpreterVersion accepts no arguments", a.nFields() == 0 );
|
||||
return BSON( "" << globalScriptEngine->getInterpreterVersionString() );
|
||||
@ -243,6 +247,7 @@ namespace mongo {
|
||||
// Need to define this method before JSFiles::utils is executed.
|
||||
scope.injectNative("_useWriteCommandsDefault", useWriteCommandsDefault);
|
||||
scope.injectNative("_writeMode", writeMode);
|
||||
scope.injectNative("_readMode", readMode);
|
||||
scope.externalSetup();
|
||||
mongo::shell_utils::installShellUtils( scope );
|
||||
scope.execSetup(JSFiles::servers);
|
||||
|
@ -362,6 +362,11 @@ if (typeof(_writeMode) == 'undefined') {
|
||||
_writeMode = function() { return "commands"; };
|
||||
};
|
||||
|
||||
if (typeof(_readMode) == 'undefined') {
|
||||
// This is for cases when the v8 engine is used other than the mongo shell, like map reduce.
|
||||
_readMode = function() { return "compatibility"; };
|
||||
};
|
||||
|
||||
shellPrintHelper = function (x) {
|
||||
if (typeof (x) == "undefined") {
|
||||
// Make sure that we have a db var before we use it
|
||||
|
Loading…
Reference in New Issue
Block a user