2
db "commands" (sent via db.$cmd.findOne(...))
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU Affero General Public License, version 3,
9
* as published by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Affero General Public License for more details.
16
* You should have received a copy of the GNU Affero General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
#include "../util/builder.h"
26
#include "introspect.h"
28
#include "../util/lruishmap.h"
34
#include "../scripting/engine.h"
40
bool dbEval(const char *ns, BSONObj& cmd, BSONObjBuilder& result, string& errmsg) {
41
BSONElement e = cmd.firstElement();
42
uassert( 10046 , "eval needs Code" , e.type() == Code || e.type() == CodeWScope || e.type() == String );
51
code = e.codeWScopeCode();
58
if ( ! globalScriptEngine ) {
59
errmsg = "db side execution is disabled";
63
auto_ptr<Scope> s = globalScriptEngine->getPooledScope( ns );
64
ScriptingFunction f = s->createFunction(code);
66
errmsg = (string)"compile failed: " + s->getError();
70
if ( e.type() == CodeWScope )
71
s->init( e.codeWScopeScopeData() );
72
s->localConnect( cc().database()->name.c_str() );
76
BSONElement argsElement = cmd.findElement("args");
77
if ( argsElement.type() == Array ) {
78
args = argsElement.embeddedObject();
80
out() << "args:" << args.toString() << endl;
81
out() << "code:\n" << code << endl;
89
res = s->invoke(f,args, cmdLine.quota ? 10 * 60 * 1000 : 0 );
91
if ( m > cmdLine.slowMS ) {
92
out() << "dbeval slow, time: " << dec << m << "ms " << ns << endl;
93
if ( m >= 1000 ) log() << code << endl;
94
else OCCASIONALLY log() << code << endl;
98
result.append("errno", (double) res);
99
errmsg = "invoke failed: ";
100
errmsg += s->getError();
104
s->append( result , "retval" , "return" );
109
class CmdEval : public Command {
111
virtual bool slaveOk() {
114
CmdEval() : Command("$eval") { }
115
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
116
return dbEval(ns, cmdObj, result, errmsg);