diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-11-03 22:53:32 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-11-03 22:53:32 -0500 |
commit | 974dc89ad514256355a278536a337d573bedd52d (patch) | |
tree | 26695da4945f01853166fa45e862ad09601c4b1b /client | |
parent | 37baf952b83a24deeb98dc1a1e66adaed4e9a12b (diff) | |
download | mongo-974dc89ad514256355a278536a337d573bedd52d.tar.gz |
some threaded options for doing queries remotely
Diffstat (limited to 'client')
-rw-r--r-- | client/parallel.cpp | 44 | ||||
-rw-r--r-- | client/parallel.h | 52 |
2 files changed, 96 insertions, 0 deletions
diff --git a/client/parallel.cpp b/client/parallel.cpp index 410e3e90499..7c4b12f1e90 100644 --- a/client/parallel.cpp +++ b/client/parallel.cpp @@ -206,4 +206,48 @@ namespace mongo { } + // ----------------- + // ---- Future ----- + // ----------------- + + Future::CommandResult::CommandResult( const string& server , const string& db , const BSONObj& cmd ){ + _server = server; + _db = db; + _cmd = cmd; + _done = false; + } + + bool Future::CommandResult::join(){ + while ( ! _done ) + sleepmicros( 50 ); + return _ok; + } + + void Future::commandThread(){ + assert( _grab ); + shared_ptr<CommandResult> res = *_grab; + _grab = 0; + + ScopedDbConnection conn( res->_server ); + res->_ok = conn->runCommand( res->_db , res->_cmd , res->_res ); + res->_done = true; + } + + shared_ptr<Future::CommandResult> Future::spawnCommand( const string& server , const string& db , const BSONObj& cmd ){ + shared_ptr<Future::CommandResult> res; + res.reset( new Future::CommandResult( server , db , cmd ) ); + + _grab = &res; + + boost::thread thr( Future::commandThread ); + + while ( _grab ) + sleepmicros(2); + + return res; + } + + shared_ptr<Future::CommandResult> * Future::_grab; + + } diff --git a/client/parallel.h b/client/parallel.h index a2189c6c745..3129937c193 100644 --- a/client/parallel.h +++ b/client/parallel.h @@ -105,4 +105,56 @@ namespace mongo { BSONObj * _nexts; }; + + class Future { + public: + class CommandResult { + public: + + string getServer() const { return _server; } + + bool isDone() const { return _done; } + + bool ok() const { + assert( _done ); + return _ok; + } + + BSONObj result() const { + assert( _done ); + return _res; + } + + /** + blocks until command is done + returns ok() + */ + bool join(); + + private: + + CommandResult( const string& server , const string& db , const BSONObj& cmd ); + + string _server; + string _db; + BSONObj _cmd; + + boost::thread _thr; + + BSONObj _res; + bool _done; + bool _ok; + + friend class Future; + }; + + static void commandThread(); + + static shared_ptr<CommandResult> spawnCommand( const string& server , const string& db , const BSONObj& cmd ); + + private: + static shared_ptr<CommandResult> * _grab; + }; + + } |