From 18e7e1fa40da60fe6670123c86e6eedc8bdc1dfb Mon Sep 17 00:00:00 2001 From: Dwight Date: Fri, 12 Sep 2008 11:26:51 -0400 Subject: fix assumed initialization order of globals --- db/cloner.cpp | 14 ++++++++++++++ db/commands.cpp | 18 +++++++++--------- db/commands.h | 7 +------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/db/cloner.cpp b/db/cloner.cpp index e06d4d8276d..9ce0eb7d0e3 100644 --- a/db/cloner.cpp +++ b/db/cloner.cpp @@ -22,6 +22,7 @@ #include "../util/builder.h" #include "jsobj.h" #include "query.h" +#include "commands.h" extern int port; bool userCreateNS(const char *ns, JSObj& j, string& err); @@ -87,3 +88,16 @@ bool cloneFrom(const char *masterHost, string& errmsg) Cloner c; return c.go(masterHost, errmsg); } + +class CmdClone : public Command { +public: + CmdClone() : Command("clone") { } + + virtual bool run(const char *ns, JSObj& cmdObj, string& errmsg, JSObjBuilder& result) { + string from = cmdObj.getStringField("clone"); + if( from.empty() ) + return false; + return cloneFrom(from.c_str(), errmsg); + } +} cmdclone; + diff --git a/db/commands.cpp b/db/commands.cpp index 049b14e1dec..95ee8117763 100644 --- a/db/commands.cpp +++ b/db/commands.cpp @@ -40,7 +40,14 @@ int runCount(const char *ns, JSObj& cmd, string& err); const int edebug=0; -map Command::commands; +static map *commands; + +Command::Command(const char *_name) : name(_name) { + // register ourself. + if( commands == 0 ) + commands = new map; + (*commands)[name] = this; +} bool dbEval(JSObj& cmd, JSObjBuilder& result) { Element e = cmd.firstElement(); @@ -300,7 +307,7 @@ bool _runCommands(const char *ns, JSObj& jsobj, stringstream& ss, BufBuilder &b, /* check for properly registered command objects. Note that all the commands below should be migrated over to the command object format. */ - else if( (i = Command::commands.find(e.fieldName())) != Command::commands.end() ) { + else if( (i = commands->find(e.fieldName())) != commands->end() ) { valid = true; string errmsg; Command *c = i->second; @@ -370,13 +377,6 @@ bool _runCommands(const char *ns, JSObj& jsobj, stringstream& ss, BufBuilder &b, } anObjBuilder.append("n", (double) nn); } - else if( strcmp( e.fieldName(), "clone") == 0 ) { - valid = true; - string err; - ok = cloneFrom(e.valuestr(), err); - if( !err.empty() ) - anObjBuilder.append("errmsg", err.c_str()); - } else if( strcmp( e.fieldName(), "create") == 0 ) { valid = true; string ns = us + '.' + e.valuestr(); diff --git a/db/commands.h b/db/commands.h index 884c761d09b..062867a1997 100644 --- a/db/commands.h +++ b/db/commands.h @@ -31,10 +31,5 @@ public: /* return true if only the admin ns has privileges to run this command. */ virtual bool adminOnly() { return false; } - static map commands; - - Command(const char *_name) : name(_name) { - // register ourself. - commands[name] = this; - } + Command(const char *_name); }; -- cgit v1.2.1