2009-04-23 20:28:22 +02:00
|
|
|
// engine.cpp
|
|
|
|
|
2009-04-23 20:31:40 +02:00
|
|
|
#include "stdafx.h"
|
2009-04-23 20:28:22 +02:00
|
|
|
#include "engine.h"
|
2009-05-13 23:00:02 +02:00
|
|
|
#include "../util/file.h"
|
2009-04-23 20:28:22 +02:00
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
Scope::Scope(){
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::~Scope(){
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEngine::ScriptEngine(){
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEngine::~ScriptEngine(){
|
|
|
|
}
|
|
|
|
|
2009-05-19 22:13:24 +02:00
|
|
|
int Scope::invoke( const char* code , const BSONObj& args, int timeoutMs ){
|
2009-04-24 22:41:40 +02:00
|
|
|
ScriptingFunction func = createFunction( code );
|
|
|
|
uassert( "compile failed" , func );
|
2009-05-19 22:13:24 +02:00
|
|
|
return invoke( func , args, timeoutMs );
|
2009-04-24 22:41:40 +02:00
|
|
|
}
|
|
|
|
|
2009-05-19 22:13:24 +02:00
|
|
|
bool Scope::execFile( const string& filename , bool printResult , bool reportError , bool assertOnError, int timeoutMs ){
|
2009-05-13 23:00:02 +02:00
|
|
|
|
|
|
|
path p( filename );
|
|
|
|
if ( is_directory( p ) ){
|
|
|
|
cerr << "can't read directory [" << filename << "]" << endl;
|
|
|
|
if ( assertOnError )
|
|
|
|
assert( 0 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
File f;
|
|
|
|
f.open( filename.c_str() );
|
|
|
|
|
2009-05-28 19:16:23 +02:00
|
|
|
fileofs L = f.len();
|
|
|
|
assert( L <= 0x7ffffffe );
|
|
|
|
char * data = (char*)malloc( (size_t) L+1 );
|
|
|
|
data[L] = 0;
|
|
|
|
f.read( 0 , data , (size_t) L );
|
2009-05-13 23:00:02 +02:00
|
|
|
|
2009-05-19 22:13:24 +02:00
|
|
|
return exec( data , filename , printResult , reportError , assertOnError, timeoutMs );
|
2009-05-13 23:00:02 +02:00
|
|
|
}
|
2009-08-05 19:14:06 +02:00
|
|
|
|
|
|
|
typedef map< string , list<Scope*> > PoolToScopes;
|
|
|
|
|
|
|
|
class ScopeCache {
|
|
|
|
public:
|
|
|
|
|
|
|
|
~ScopeCache(){
|
|
|
|
for ( PoolToScopes::iterator i=_pools.begin() ; i != _pools.end(); i++ ){
|
|
|
|
for ( list<Scope*>::iterator j=i->second.begin(); j != i->second.end(); j++ )
|
|
|
|
delete *j;
|
|
|
|
}
|
|
|
|
}
|
2009-05-13 23:00:02 +02:00
|
|
|
|
2009-08-05 19:14:06 +02:00
|
|
|
void done( const string& pool , Scope * s ){
|
|
|
|
boostlock lk( _mutex );
|
|
|
|
list<Scope*> & l = _pools[pool];
|
|
|
|
if ( l.size() > 10 ){
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
l.push_back( s );
|
|
|
|
s->reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope * get( const string& pool ){
|
|
|
|
boostlock lk( _mutex );
|
|
|
|
list<Scope*> & l = _pools[pool];
|
|
|
|
if ( l.size() == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Scope * s = l.back();
|
|
|
|
l.pop_back();
|
|
|
|
s->reset();
|
|
|
|
return s;
|
|
|
|
}
|
2009-05-13 23:00:02 +02:00
|
|
|
|
2009-08-05 19:14:06 +02:00
|
|
|
private:
|
|
|
|
PoolToScopes _pools;
|
|
|
|
mutex _mutex;
|
|
|
|
};
|
2009-05-13 23:00:02 +02:00
|
|
|
|
2009-08-05 19:14:06 +02:00
|
|
|
thread_specific_ptr<ScopeCache> scopeCache;
|
|
|
|
|
|
|
|
class PooledScope : public Scope {
|
|
|
|
public:
|
|
|
|
PooledScope( const string pool , Scope * real ) : _pool( pool ) , _real( real ){};
|
|
|
|
virtual ~PooledScope(){
|
|
|
|
scopeCache->done( _pool , _real );
|
|
|
|
_real = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(){
|
|
|
|
_real->reset();
|
|
|
|
}
|
|
|
|
void init( BSONObj * data ){
|
|
|
|
_real->init( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
void localConnect( const char * dbName ){
|
|
|
|
_real->localConnect( dbName );
|
|
|
|
}
|
|
|
|
void externalSetup(){
|
|
|
|
_real->externalSetup();
|
|
|
|
}
|
|
|
|
|
|
|
|
double getNumber( const char *field ){
|
|
|
|
return _real->getNumber( field );
|
|
|
|
}
|
|
|
|
string getString( const char *field ){
|
|
|
|
return _real->getString( field );
|
|
|
|
}
|
|
|
|
bool getBoolean( const char *field ){
|
|
|
|
return _real->getBoolean( field );
|
|
|
|
}
|
|
|
|
BSONObj getObject( const char *field ){
|
|
|
|
return _real->getObject( field );
|
|
|
|
}
|
|
|
|
|
|
|
|
int type( const char *field ){
|
|
|
|
return _real->type( field );
|
|
|
|
}
|
|
|
|
|
|
|
|
void setNumber( const char *field , double val ){
|
|
|
|
_real->setNumber( field , val );
|
|
|
|
}
|
|
|
|
void setString( const char *field , const char * val ){
|
|
|
|
_real->setString( field , val );
|
|
|
|
}
|
|
|
|
void setObject( const char *field , const BSONObj& obj , bool readOnly=true ){
|
|
|
|
_real->setObject( field , obj , readOnly );
|
|
|
|
}
|
|
|
|
void setBoolean( const char *field , bool val ){
|
|
|
|
_real->setBoolean( field , val );
|
|
|
|
}
|
|
|
|
void setThis( const BSONObj * obj ){
|
|
|
|
_real->setThis( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptingFunction createFunction( const char * code ){
|
|
|
|
return _real->createFunction( code );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
|
|
|
int invoke( ScriptingFunction func , const BSONObj& args, int timeoutMs = 0 ){
|
|
|
|
return _real->invoke( func , args , timeoutMs );
|
|
|
|
}
|
|
|
|
|
|
|
|
string getError(){
|
|
|
|
return _real->getError();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exec( const string& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs = 0 ){
|
|
|
|
return _real->exec( code , name , printResult , reportError , assertOnError , timeoutMs );
|
|
|
|
}
|
|
|
|
bool execFile( const string& filename , bool printResult , bool reportError , bool assertOnError, int timeoutMs = 0 ){
|
|
|
|
return _real->execFile( filename , printResult , reportError , assertOnError , timeoutMs );
|
|
|
|
}
|
|
|
|
|
|
|
|
void injectNative( const char *field, NativeFunction func ){
|
|
|
|
_real->injectNative( field , func );
|
|
|
|
}
|
|
|
|
|
|
|
|
void gc(){
|
|
|
|
_real->gc();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
string _pool;
|
|
|
|
Scope * _real;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto_ptr<Scope> ScriptEngine::getPooledScope( const string& pool ){
|
|
|
|
if ( ! scopeCache.get() ){
|
|
|
|
scopeCache.reset( new ScopeCache() );
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope * s = scopeCache->get( pool );
|
|
|
|
if ( ! s ){
|
|
|
|
s = createScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_ptr<Scope> p;
|
|
|
|
p.reset( new PooledScope( pool , s ) );
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-04-23 20:28:22 +02:00
|
|
|
ScriptEngine * globalScriptEngine;
|
|
|
|
}
|