diff options
Diffstat (limited to 'src/mongo/db/dbeval.cpp')
-rw-r--r-- | src/mongo/db/dbeval.cpp | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/src/mongo/db/dbeval.cpp b/src/mongo/db/dbeval.cpp index ae42d2a349d..ada1495be36 100644 --- a/src/mongo/db/dbeval.cpp +++ b/src/mongo/db/dbeval.cpp @@ -48,33 +48,31 @@ namespace mongo { - using std::unique_ptr; - using std::dec; - using std::endl; - using std::string; - using std::stringstream; +using std::unique_ptr; +using std::dec; +using std::endl; +using std::string; +using std::stringstream; namespace { - const int edebug=0; +const int edebug = 0; - bool dbEval(OperationContext* txn, - const string& dbName, - const BSONObj& cmd, - BSONObjBuilder& result, - string& errmsg) { - - RARELY { - warning() << "the eval command is deprecated" << startupWarningsLog; - } +bool dbEval(OperationContext* txn, + const string& dbName, + const BSONObj& cmd, + BSONObjBuilder& result, + string& errmsg) { + RARELY { + warning() << "the eval command is deprecated" << startupWarningsLog; + } - const BSONElement e = cmd.firstElement(); - uassert(10046, - "eval needs Code", - e.type() == Code || e.type() == CodeWScope || e.type() == String); + const BSONElement e = cmd.firstElement(); + uassert( + 10046, "eval needs Code", e.type() == Code || e.type() == CodeWScope || e.type() == String); - const char *code = 0; - switch ( e.type() ) { + const char* code = 0; + switch (e.type()) { case String: case Code: code = e.valuestr(); @@ -84,112 +82,114 @@ namespace { break; default: verify(0); - } + } - verify(code); + verify(code); - if (!globalScriptEngine) { - errmsg = "db side execution is disabled"; - return false; - } + if (!globalScriptEngine) { + errmsg = "db side execution is disabled"; + return false; + } - unique_ptr<Scope> s(globalScriptEngine->newScope()); - s->registerOperation(txn); + unique_ptr<Scope> s(globalScriptEngine->newScope()); + s->registerOperation(txn); - ScriptingFunction f = s->createFunction(code); - if (f == 0) { - errmsg = string("compile failed: ") + s->getError(); - return false; - } - - s->localConnectForDbEval(txn, dbName.c_str()); + ScriptingFunction f = s->createFunction(code); + if (f == 0) { + errmsg = string("compile failed: ") + s->getError(); + return false; + } - if (e.type() == CodeWScope) { - s->init(e.codeWScopeScopeDataUnsafe()); - } + s->localConnectForDbEval(txn, dbName.c_str()); - BSONObj args; - { - BSONElement argsElement = cmd.getField("args"); - if ( argsElement.type() == Array ) { - args = argsElement.embeddedObject(); - if ( edebug ) { - log() << "args:" << args.toString() << endl; - log() << "code:\n" << code << endl; - } - } - } + if (e.type() == CodeWScope) { + s->init(e.codeWScopeScopeDataUnsafe()); + } - int res; - { - Timer t; - res = s->invoke(f, &args, 0, 0); - int m = t.millis(); - if (m > serverGlobalParams.slowMS) { - log() << "dbeval slow, time: " << dec << m << "ms " << dbName << endl; - if ( m >= 1000 ) log() << code << endl; - else OCCASIONALLY log() << code << endl; + BSONObj args; + { + BSONElement argsElement = cmd.getField("args"); + if (argsElement.type() == Array) { + args = argsElement.embeddedObject(); + if (edebug) { + log() << "args:" << args.toString() << endl; + log() << "code:\n" << code << endl; } } + } - if (res || s->isLastRetNativeCode()) { - result.append("errno", (double) res); - errmsg = "invoke failed: "; - if (s->isLastRetNativeCode()) - errmsg += "cannot return native function"; + int res; + { + Timer t; + res = s->invoke(f, &args, 0, 0); + int m = t.millis(); + if (m > serverGlobalParams.slowMS) { + log() << "dbeval slow, time: " << dec << m << "ms " << dbName << endl; + if (m >= 1000) + log() << code << endl; else - errmsg += s->getError(); - - return false; + OCCASIONALLY log() << code << endl; } + } - s->append(result, "retval", "__returnValue"); + if (res || s->isLastRetNativeCode()) { + result.append("errno", (double)res); + errmsg = "invoke failed: "; + if (s->isLastRetNativeCode()) + errmsg += "cannot return native function"; + else + errmsg += s->getError(); - return true; + return false; } + s->append(result, "retval", "__returnValue"); - class CmdEval : public Command { - public: - virtual bool slaveOk() const { - return false; - } + return true; +} - virtual void help(stringstream &help) const { - help << "DEPRECATED\n" - << "Evaluate javascript at the server.\n" - << "http://dochub.mongodb.org/core/serversidecodeexecution"; - } - virtual bool isWriteCommandForConfigServer() const { return false; } - virtual void addRequiredPrivileges(const std::string& dbname, - const BSONObj& cmdObj, - std::vector<Privilege>* out) { - RoleGraph::generateUniversalPrivileges(out); - } +class CmdEval : public Command { +public: + virtual bool slaveOk() const { + return false; + } - CmdEval() : Command("eval", false, "$eval") { } + virtual void help(stringstream& help) const { + help << "DEPRECATED\n" + << "Evaluate javascript at the server.\n" + << "http://dochub.mongodb.org/core/serversidecodeexecution"; + } + virtual bool isWriteCommandForConfigServer() const { + return false; + } + virtual void addRequiredPrivileges(const std::string& dbname, + const BSONObj& cmdObj, + std::vector<Privilege>* out) { + RoleGraph::generateUniversalPrivileges(out); + } - bool run(OperationContext* txn, - const string& dbname, - BSONObj& cmdObj, - int options, - string& errmsg, - BSONObjBuilder& result) { + CmdEval() : Command("eval", false, "$eval") {} - if (cmdObj["nolock"].trueValue()) { - return dbEval(txn, dbname, cmdObj, result, errmsg); - } + bool run(OperationContext* txn, + const string& dbname, + BSONObj& cmdObj, + int options, + string& errmsg, + BSONObjBuilder& result) { + if (cmdObj["nolock"].trueValue()) { + return dbEval(txn, dbname, cmdObj, result, errmsg); + } - ScopedTransaction transaction(txn, MODE_X); - Lock::GlobalWrite lk(txn->lockState()); + ScopedTransaction transaction(txn, MODE_X); + Lock::GlobalWrite lk(txn->lockState()); - OldClientContext ctx(txn, dbname); + OldClientContext ctx(txn, dbname); - return dbEval(txn, dbname, cmdObj, result, errmsg); - } + return dbEval(txn, dbname, cmdObj, result, errmsg); + } - } cmdeval; +} cmdeval; -} // namespace -} // namespace mongo +} // namespace +} // namespace mongo |