summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <ehorowitz@shopwiki.com>2008-09-14 13:16:50 -0400
committerEliot Horowitz <ehorowitz@shopwiki.com>2008-09-14 13:16:50 -0400
commit31eff8fb7cfa94783fcfb47622199a0ed27a0a53 (patch)
treee7ebaff7406aa75ea6c0fdcea6445c4413ea5e3a
parent54668a9afecd39d21d77003bcc20991bddee2090 (diff)
downloadmongo-31eff8fb7cfa94783fcfb47622199a0ed27a0a53.tar.gz
can use this. instead of obj. for $where
scope passing for db.eval
-rw-r--r--db/commands.cpp11
-rw-r--r--db/javajs.cpp12
-rw-r--r--db/javajs.h7
-rw-r--r--db/jsobj.h6
-rw-r--r--db/matcher.cpp7
5 files changed, 35 insertions, 8 deletions
diff --git a/db/commands.cpp b/db/commands.cpp
index 049b14e1dec..a9c393649d5 100644
--- a/db/commands.cpp
+++ b/db/commands.cpp
@@ -44,13 +44,14 @@ map<string,Command*> Command::commands;
bool dbEval(JSObj& cmd, JSObjBuilder& result) {
Element e = cmd.firstElement();
- assert( e.type() == Code );
- const char *code = e.valuestr();
+ assert( e.type() == Code || e.type() == CodeWScope );
+ const char *code = e.type() == Code ? e.valuestr() : e.codeWScopeCode();
+
if ( ! JavaJS ) {
result.append("errmsg", "db side execution is disabled");
return false;
}
-
+
jlong f = JavaJS->functionCreate(code);
if( f == 0 ) {
result.append("errmsg", "compile failed");
@@ -58,6 +59,8 @@ bool dbEval(JSObj& cmd, JSObjBuilder& result) {
}
Scope s;
+ if ( e.type() == CodeWScope )
+ s.init( e.codeWScopeScopeData() );
s.setString("$client", client->name.c_str());
Element args = cmd.findElement("args");
if( args.type() == Array ) {
@@ -314,7 +317,7 @@ bool _runCommands(const char *ns, JSObj& jsobj, stringstream& ss, BufBuilder &b,
if( !ok )
anObjBuilder.append("errmsg", errmsg);
}
- else if( e.type() == Code ) {
+ else if( e.type() == Code || e.type() == CodeWScope ) {
valid = true;
ok = dbEval(jsobj, anObjBuilder);
}
diff --git a/db/javajs.cpp b/db/javajs.cpp
index e0bfc4ce0ce..9d4d558731b 100644
--- a/db/javajs.cpp
+++ b/db/javajs.cpp
@@ -181,6 +181,7 @@ JavaJSImpl::JavaJSImpl(const char *appserverPath){
_scopeCreate = _mainEnv->GetStaticMethodID( _dbhook , "scopeCreate" , "()J" );
_scopeInit = _mainEnv->GetStaticMethodID( _dbhook , "scopeInit" , "(JLjava/nio/ByteBuffer;)Z" );
+ _scopeSetThis = _mainEnv->GetStaticMethodID( _dbhook , "scopeSetThis" , "(JLjava/nio/ByteBuffer;)Z" );
_scopeReset = _mainEnv->GetStaticMethodID( _dbhook , "scopeReset" , "(J)Z" );
_scopeFree = _mainEnv->GetStaticMethodID( _dbhook , "scopeFree" , "(J)V" );
@@ -201,6 +202,7 @@ JavaJSImpl::JavaJSImpl(const char *appserverPath){
jassert( _scopeCreate );
jassert( _scopeInit );
+ jassert( _scopeSetThis );
jassert( _scopeReset );
jassert( _scopeFree );
@@ -290,6 +292,16 @@ int JavaJSImpl::scopeInit( jlong id , JSObj * obj ){
return _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeInit , id , bb );
}
+int JavaJSImpl::scopeSetThis( jlong id , JSObj * obj ){
+ if ( ! obj )
+ return 0;
+
+ jobject bb = _getEnv()->NewDirectByteBuffer( (void*)(obj->objdata()) , (jlong)(obj->objsize()) );
+ jassert( bb );
+
+ return _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeSetThis , id , bb );
+}
+
// scope getters
char JavaJSImpl::scopeGetType( jlong id , const char * field ){
diff --git a/db/javajs.h b/db/javajs.h
index 95f7625f485..64693286be4 100644
--- a/db/javajs.h
+++ b/db/javajs.h
@@ -53,6 +53,7 @@ class JavaJSImpl {
jlong scopeCreate();
int scopeInit( jlong id , JSObj * obj );
+ int scopeSetThis( jlong id , JSObj * obj );
jboolean scopeReset( jlong id );
void scopeFree( jlong id );
@@ -119,6 +120,7 @@ class JavaJSImpl {
jmethodID _scopeCreate;
jmethodID _scopeInit;
+ jmethodID _scopeSetThis;
jmethodID _scopeReset;
jmethodID _scopeFree;
@@ -148,6 +150,11 @@ class Scope {
Scope() { s = JavaJS->scopeCreate(); }
~Scope() { JavaJS->scopeFree(s); s = 0; }
void reset() { JavaJS->scopeReset(s); }
+
+ void init( const char * data ){
+ JSObj o( data , 0 );
+ JavaJS->scopeInit( s , & o );
+ }
double getNumber(const char *field) { return JavaJS->scopeGetNumber(s,field); }
string getString(const char *field) { return JavaJS->scopeGetString(s,field); }
diff --git a/db/jsobj.h b/db/jsobj.h
index b7489412a85..9ce429c48af 100644
--- a/db/jsobj.h
+++ b/db/jsobj.h
@@ -127,6 +127,12 @@ public:
// for strings. also gives you start of the real data for an embedded object
const char * valuestr() const { return value() + 4; }
+ const char * codeWScopeCode() const { return value() + 8; }
+ const char * codeWScopeScopeData() const {
+ // TODO fix
+ return codeWScopeCode() + strlen( codeWScopeCode() ) + 1;
+ }
+
JSObj embeddedObject();
const char *regex() { assert(type() == RegEx); return value(); }
diff --git a/db/matcher.cpp b/db/matcher.cpp
index fe8f259871e..b5835389fb4 100644
--- a/db/matcher.cpp
+++ b/db/matcher.cpp
@@ -144,9 +144,8 @@ JSMatcher::JSMatcher(JSObj &_jsobj) :
JavaJS->scopeSetString(where->scope, "$client", client->name.c_str());
if ( e.type() == CodeWScope ){
- const char *code = (e.value() + 8);
- where->setFunc(code);
- where->jsScope = new JSObj( code + strlen( code ) + 1 , 0 );
+ where->setFunc( e.codeWScopeCode() );
+ where->jsScope = new JSObj( e.codeWScopeScopeData() , 0 );
}
else {
const char *code = e.valuestr();
@@ -416,9 +415,9 @@ bool JSMatcher::matches(JSObj& jsobj, bool *deep) {
if( 1 || jsobj.objsize() < 200 || where->fullObject ) {
if ( where->jsScope ){
- cout << "jsScope->objsize " << where->jsScope->objsize() << endl;
JavaJS->scopeInit( where->scope , where->jsScope );
}
+ JavaJS->scopeSetThis(where->scope, &jsobj);
JavaJS->scopeSetObject(where->scope, "obj", &jsobj);
}
else {