diff options
author | Eliot Horowitz <eliot@10gen.com> | 2010-01-20 14:02:14 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-01-20 14:02:14 -0500 |
commit | ea6189d491c3cd5e25438f9a652873880810dae4 (patch) | |
tree | 60b536e210be901aaa6451b8d83574da28246381 | |
parent | 9cde296ca6bdaf968396de9be2c65bf067771187 (diff) | |
download | mongo-ea6189d491c3cd5e25438f9a652873880810dae4.tar.gz |
some command cleaning
-rw-r--r-- | db/commands.cpp | 29 | ||||
-rw-r--r-- | db/commands.h | 9 | ||||
-rw-r--r-- | db/dbcommands.cpp | 19 | ||||
-rw-r--r-- | dbtests/mockdbclient.h | 2 | ||||
-rw-r--r-- | s/strategy_single.cpp | 2 |
5 files changed, 37 insertions, 24 deletions
diff --git a/db/commands.cpp b/db/commands.cpp index 48770aee3be..3078ea1068f 100644 --- a/db/commands.cpp +++ b/db/commands.cpp @@ -23,20 +23,20 @@ namespace mongo { - map<string,Command*> *commands; + map<string,Command*> * Command::_commands; Command::Command(const char *_name) : name(_name) { // register ourself. - if ( commands == 0 ) - commands = new map<string,Command*>; - (*commands)[name] = this; + if ( _commands == 0 ) + _commands = new map<string,Command*>; + (*_commands)[name] = this; } void Command::help( stringstream& help ) const { help << "no help defined"; } - - bool runCommandAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder) { + + bool Command::runAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder) { const char *p = strchr(ns, '.'); if ( !p ) return false; if ( strcmp(p, ".$cmd") != 0 ) return false; @@ -54,7 +54,7 @@ namespace mongo { /* check for properly registered command objects. Note that all the commands below should be migrated over to the command object format. */ - else if ( (i = commands->find(e.fieldName())) != commands->end() ) { + else if ( (i = _commands->find(e.fieldName())) != _commands->end() ) { valid = true; string errmsg; Command *c = i->second; @@ -84,4 +84,19 @@ namespace mongo { return false; } + Command* Command::findCommand( const string& name ){ + map<string,Command*>::iterator i = _commands->find( name ); + if ( i == _commands->end() ) + return 0; + return i->second; + } + + + bool Command::readOnly( const string& name ){ + Command * c = findCommand( name ); + if ( ! c ) + return false; + return c->readOnly(); + } + } // namespace mongo diff --git a/db/commands.h b/db/commands.h index 3795c3822ef..20fb98c98e2 100644 --- a/db/commands.h +++ b/db/commands.h @@ -100,9 +100,14 @@ namespace mongo { return cmdObj["q"].embeddedObject(); return BSONObj(); } - }; - bool runCommandAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder); + static map<string,Command*> * _commands; + + public: + static bool runAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder); + static bool readOnly( const string& name ); + static Command * findCommand( const string& name ); + }; bool _runCommands(const char *ns, BSONObj& jsobj, BufBuilder &b, BSONObjBuilder& anObjBuilder, bool fromRepl, int queryOptions); diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp index c49355afe36..ff072a17288 100644 --- a/db/dbcommands.cpp +++ b/db/dbcommands.cpp @@ -1354,9 +1354,7 @@ namespace mongo { return true; } } cmdFindAndModify; - - extern map<string,Command*> *commands; - + bool commandIsReadOnly(BSONObj& _cmdobj) { BSONObj jsobj; { @@ -1369,12 +1367,9 @@ namespace mongo { } } BSONElement e = jsobj.firstElement(); - map<string,Command*>::iterator i; - if ( e.type() && ( i = commands->find(e.fieldName()) ) != commands->end() ){ - Command *c = i->second; - return c->readOnly(); - } - return false; + if ( ! e.type() ) + return false; + return Command::readOnly( e.fieldName() ); } /* TODO make these all command objects -- legacy stuff here @@ -1407,11 +1402,9 @@ namespace mongo { BSONElement e = jsobj.firstElement(); - map<string,Command*>::iterator i; - - if ( e.type() && ( i = commands->find(e.fieldName()) ) != commands->end() ){ + Command * c = e.type() ? Command::findCommand( e.fieldName() ) : 0; + if ( c ){ string errmsg; - Command *c = i->second; AuthenticationInfo *ai = currentClient.get()->ai; uassert( 10045 , "unauthorized", ai->isAuthorized(cc().database()->name.c_str()) || !c->requiresAuth()); diff --git a/dbtests/mockdbclient.h b/dbtests/mockdbclient.h index 631515ddc45..26f625049f9 100644 --- a/dbtests/mockdbclient.h +++ b/dbtests/mockdbclient.h @@ -66,7 +66,7 @@ public: if ( cc_ ) cc_->beforeCommand(); SetGlobalReplPair s( rp_ ); BSONObjBuilder result; - result.append( "ok", runCommandAgainstRegistered( "admin.$cmd", query.obj, result ) ? 1.0 : 0.0 ); + result.append( "ok", Command::runAgainstRegistered( "admin.$cmd", query.obj, result ) ? 1.0 : 0.0 ); if ( cc_ ) cc_->afterCommand(); return result.obj(); } diff --git a/s/strategy_single.cpp b/s/strategy_single.cpp index e882a913cf5..9cf8a635370 100644 --- a/s/strategy_single.cpp +++ b/s/strategy_single.cpp @@ -26,7 +26,7 @@ namespace mongo { try { if ( ( q.ntoreturn == -1 || q.ntoreturn == 1 ) && strstr(q.ns, ".$cmd") ) { BSONObjBuilder builder; - bool ok = runCommandAgainstRegistered(q.ns, q.query, builder); + bool ok = Command::runAgainstRegistered(q.ns, q.query, builder); if ( ok ) { BSONObj x = builder.done(); replyToQuery(0, r.p(), r.m(), x); |