0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/dbeval.cpp

105 lines
2.9 KiB
C++
Raw Normal View History

2008-10-22 22:56:39 +02:00
/* commands.cpp
db "commands" (sent via db.$cmd.findOne(...))
*/
/**
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "query.h"
#include "pdfile.h"
#include "jsobj.h"
#include "../util/builder.h"
#include <time.h>
#include "introspect.h"
#include "btree.h"
#include "../util/lruishmap.h"
#include "javajs.h"
#include "json.h"
#include "repl.h"
#include "commands.h"
const int edebug=0;
2008-11-24 20:23:19 +01:00
bool dbEval(const char *ns, BSONObj& cmd, BSONObjBuilder& result, string& errmsg) {
2008-10-22 22:56:39 +02:00
BSONElement e = cmd.firstElement();
assert( e.type() == Code || e.type() == CodeWScope );
const char *code = e.type() == Code ? e.valuestr() : e.codeWScopeCode();
if ( ! JavaJS ) {
errmsg = "db side execution is disabled";
return false;
}
jlong f = JavaJS->functionCreate(code);
if( f == 0 ) {
errmsg = "compile failed";
return false;
}
Scope s;
if ( e.type() == CodeWScope )
s.init( e.codeWScopeScopeData() );
2008-12-03 02:02:00 +01:00
s.setString("$client", database->name.c_str());
2008-10-22 22:56:39 +02:00
BSONElement args = cmd.findElement("args");
if( args.type() == Array ) {
BSONObj eo = args.embeddedObject();
if( edebug ) {
cout << "args:" << eo.toString() << endl;
cout << "code:\n" << code << endl;
}
s.setObject("args", eo);
}
2008-11-24 20:23:19 +01:00
int res;
{
Timer t;
res = s.invoke(f);
int m = t.millis();
if( m > 100 ) {
2008-12-01 16:59:53 +01:00
problem() << "dbeval time: " << dec << m << "ms " << ns << endl;
2008-11-24 20:23:19 +01:00
OCCASIONALLY log() << code << endl;
else if( m >= 1000 ) log() << code << endl;
}
}
2008-10-22 22:56:39 +02:00
if( res ) {
result.append("errno", (double) res);
errmsg = "invoke failed";
return false;
}
int type = s.type("return");
if( type == Object || type == Array )
result.append("retval", s.getObject("return"));
else if( type == NumberDouble )
result.append("retval", s.getNumber("return"));
else if( type == String )
result.append("retval", s.getString("return").c_str());
else if( type == Bool ) {
result.appendBool("retval", s.getBoolean("return"));
}
return true;
}
class CmdEval : public Command {
public:
virtual bool slaveOk() { return false; }
2008-10-22 22:56:39 +02:00
CmdEval() : Command("$eval") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
2008-11-24 20:23:19 +01:00
return dbEval(ns, cmdObj, result, errmsg);
2008-10-22 22:56:39 +02:00
}
} cmdeval;