diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-09-29 16:54:31 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-09-29 16:54:31 -0400 |
commit | ae3d49d718ac6778f7abdc133263121971f64569 (patch) | |
tree | c8eec1cd64667e56ccc69b645ff280cdc9fc0abd /scripting/engine.cpp | |
parent | 966e13e27f875a863bea62e02c7425b77dab39c6 (diff) | |
download | mongo-ae3d49d718ac6778f7abdc133263121971f64569.tar.gz |
allow you to store js functions in the server SERVER-157
Diffstat (limited to 'scripting/engine.cpp')
-rw-r--r-- | scripting/engine.cpp | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/scripting/engine.cpp b/scripting/engine.cpp index 5c53cdd1f65..c82f5961563 100644 --- a/scripting/engine.cpp +++ b/scripting/engine.cpp @@ -3,10 +3,13 @@ #include "stdafx.h" #include "engine.h" #include "../util/file.h" +#include "../client/dbclient.h" namespace mongo { + + long long Scope::_lastVersion = 1; - Scope::Scope(){ + Scope::Scope() : _localDBName("") , _loadedVersion(0){ } Scope::~Scope(){ @@ -88,6 +91,37 @@ namespace mongo { return exec( data , filename , printResult , reportError , assertOnError, timeoutMs ); } + + void Scope::storedFuncMod(){ + _lastVersion++; + } + + void Scope::loadStored( bool ignoreNotConnected ){ + if ( _localDBName.size() == 0 ){ + if ( ignoreNotConnected ) + return; + uassert( "need to have locallyConnected already" , _localDBName.size() ); + } + if ( _loadedVersion == _lastVersion ) + return; + + _loadedVersion = _lastVersion; + + static DBClientBase * db = createDirectClient(); + + auto_ptr<DBClientCursor> c = db->query( _localDBName + ".system.js" , Query() ); + while ( c->more() ){ + BSONObj o = c->next(); + + BSONElement n = o["_id"]; + BSONElement v = o["value"]; + + uassert( "name has to be a string" , n.type() == String ); + uassert( "value has to be set" , v.type() != EOO ); + + setElement( n.valuestr() , v ); + } + } typedef map< string , list<Scope*> > PoolToScopes; @@ -157,7 +191,9 @@ namespace mongo { class PooledScope : public Scope { public: - PooledScope( const string pool , Scope * real ) : _pool( pool ) , _real( real ){}; + PooledScope( const string pool , Scope * real ) : _pool( pool ) , _real( real ){ + _real->loadStored( true ); + }; virtual ~PooledScope(){ ScopeCache * sc = scopeCache.get(); if ( sc ){ @@ -202,6 +238,9 @@ namespace mongo { return _real->type( field ); } + void setElement( const char *field , const BSONElement& val ){ + _real->setElement( field , val ); + } void setNumber( const char *field , double val ){ _real->setNumber( field , val ); } |