0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00

some threaded options for doing queries remotely

This commit is contained in:
Eliot Horowitz 2009-11-03 22:53:32 -05:00
parent 37baf952b8
commit 974dc89ad5
2 changed files with 96 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
};
}