diff options
Diffstat (limited to 'src/mongo/db/dbdirectclient.cpp')
-rw-r--r-- | src/mongo/db/dbdirectclient.cpp | 249 |
1 files changed, 119 insertions, 130 deletions
diff --git a/src/mongo/db/dbdirectclient.cpp b/src/mongo/db/dbdirectclient.cpp index 6f297520379..15ab03eaf80 100644 --- a/src/mongo/db/dbdirectclient.cpp +++ b/src/mongo/db/dbdirectclient.cpp @@ -39,163 +39,152 @@ namespace mongo { - using std::unique_ptr; - using std::endl; - using std::string; +using std::unique_ptr; +using std::endl; +using std::string; - // Called from scripting/engine.cpp and scripting/v8_db.cpp. - DBClientBase* createDirectClient(OperationContext* txn) { - return new DBDirectClient(txn); - } +// Called from scripting/engine.cpp and scripting/v8_db.cpp. +DBClientBase* createDirectClient(OperationContext* txn) { + return new DBDirectClient(txn); +} - namespace { +namespace { - class DirectClientScope { - MONGO_DISALLOW_COPYING(DirectClientScope); - public: - explicit DirectClientScope(OperationContext* txn) - : _txn(txn), _prev(_txn->getClient()->isInDirectClient()) { - _txn->getClient()->setInDirectClient(true); - } +class DirectClientScope { + MONGO_DISALLOW_COPYING(DirectClientScope); - ~DirectClientScope() { - _txn->getClient()->setInDirectClient(_prev); - } +public: + explicit DirectClientScope(OperationContext* txn) + : _txn(txn), _prev(_txn->getClient()->isInDirectClient()) { + _txn->getClient()->setInDirectClient(true); + } - private: - OperationContext* const _txn; - const bool _prev; - }; + ~DirectClientScope() { + _txn->getClient()->setInDirectClient(_prev); + } - } // namespace +private: + OperationContext* const _txn; + const bool _prev; +}; +} // namespace - DBDirectClient::DBDirectClient(OperationContext* txn) : _txn(txn) { } - bool DBDirectClient::isFailed() const { - return false; - } +DBDirectClient::DBDirectClient(OperationContext* txn) : _txn(txn) {} - bool DBDirectClient::isStillConnected() { - return true; - } +bool DBDirectClient::isFailed() const { + return false; +} - std::string DBDirectClient::toString() const { - return "DBDirectClient"; - } +bool DBDirectClient::isStillConnected() { + return true; +} - std::string DBDirectClient::getServerAddress() const { - return "localhost"; // TODO: should this have the port? - } +std::string DBDirectClient::toString() const { + return "DBDirectClient"; +} - void DBDirectClient::sayPiggyBack(Message& toSend) { - // don't need to piggy back when connected locally - return say(toSend); - } +std::string DBDirectClient::getServerAddress() const { + return "localhost"; // TODO: should this have the port? +} - bool DBDirectClient::callRead(Message& toSend, Message& response) { - return call(toSend, response); - } +void DBDirectClient::sayPiggyBack(Message& toSend) { + // don't need to piggy back when connected locally + return say(toSend); +} - ConnectionString::ConnectionType DBDirectClient::type() const { - return ConnectionString::MASTER; - } +bool DBDirectClient::callRead(Message& toSend, Message& response) { + return call(toSend, response); +} - double DBDirectClient::getSoTimeout() const { - return 0; - } +ConnectionString::ConnectionType DBDirectClient::type() const { + return ConnectionString::MASTER; +} - bool DBDirectClient::lazySupported() const { - return true; - } +double DBDirectClient::getSoTimeout() const { + return 0; +} - void DBDirectClient::setOpCtx(OperationContext* txn) { - _txn = txn; - } +bool DBDirectClient::lazySupported() const { + return true; +} - QueryOptions DBDirectClient::_lookupAvailableOptions() { - // Exhaust mode is not available in DBDirectClient. - return QueryOptions(DBClientBase::_lookupAvailableOptions() & ~QueryOption_Exhaust); - } +void DBDirectClient::setOpCtx(OperationContext* txn) { + _txn = txn; +} - bool DBDirectClient::call(Message& toSend, - Message& response, - bool assertOk, - string* actualServer) { - DirectClientScope directClientScope(_txn); - LastError::get(_txn->getClient()).startRequest(); +QueryOptions DBDirectClient::_lookupAvailableOptions() { + // Exhaust mode is not available in DBDirectClient. + return QueryOptions(DBClientBase::_lookupAvailableOptions() & ~QueryOption_Exhaust); +} - DbResponse dbResponse; - CurOp curOp(_txn); - assembleResponse(_txn, toSend, dbResponse, dummyHost); - verify(dbResponse.response); +bool DBDirectClient::call(Message& toSend, Message& response, bool assertOk, string* actualServer) { + DirectClientScope directClientScope(_txn); + LastError::get(_txn->getClient()).startRequest(); - // can get rid of this if we make response handling smarter - dbResponse.response->concat(); - response = *dbResponse.response; + DbResponse dbResponse; + CurOp curOp(_txn); + assembleResponse(_txn, toSend, dbResponse, dummyHost); + verify(dbResponse.response); - return true; - } + // can get rid of this if we make response handling smarter + dbResponse.response->concat(); + response = *dbResponse.response; - void DBDirectClient::say(Message& toSend, bool isRetry, string* actualServer) { - DirectClientScope directClientScope(_txn); - LastError::get(_txn->getClient()).startRequest(); + return true; +} - DbResponse dbResponse; - CurOp curOp(_txn); - assembleResponse(_txn, toSend, dbResponse, dummyHost); - } +void DBDirectClient::say(Message& toSend, bool isRetry, string* actualServer) { + DirectClientScope directClientScope(_txn); + LastError::get(_txn->getClient()).startRequest(); - unique_ptr<DBClientCursor> DBDirectClient::query(const string& ns, - Query query, - int nToReturn, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize) { - - return DBClientBase::query(ns, - query, - nToReturn, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize); - } + DbResponse dbResponse; + CurOp curOp(_txn); + assembleResponse(_txn, toSend, dbResponse, dummyHost); +} - void DBDirectClient::killCursor(long long id) { - // The killCursor command on the DB client is only used by sharding, - // so no need to have it for MongoD. - verify(!"killCursor should not be used in MongoD"); - } +unique_ptr<DBClientCursor> DBDirectClient::query(const string& ns, + Query query, + int nToReturn, + int nToSkip, + const BSONObj* fieldsToReturn, + int queryOptions, + int batchSize) { + return DBClientBase::query( + ns, query, nToReturn, nToSkip, fieldsToReturn, queryOptions, batchSize); +} - const HostAndPort DBDirectClient::dummyHost("0.0.0.0", 0); - - unsigned long long DBDirectClient::count(const string& ns, - const BSONObj& query, - int options, - int limit, - int skip) { - BSONObj cmdObj = _countCmd(ns, query, options, limit, skip); - - NamespaceString nsString(ns); - std::string dbname = nsString.db().toString(); - - Command* countCmd = Command::findCommand("count"); - invariant(countCmd); - - std::string errmsg; - BSONObjBuilder result; - bool runRetval = countCmd->run(_txn, dbname, cmdObj, options, errmsg, result); - if (!runRetval) { - Command::appendCommandStatus(result, runRetval, errmsg); - Status commandStatus = Command::getStatusFromCommandResult(result.obj()); - invariant(!commandStatus.isOK()); - uassertStatusOK(commandStatus); - } - - BSONObj resultObj = result.obj(); - return static_cast<unsigned long long>(resultObj["n"].numberLong()); - } +void DBDirectClient::killCursor(long long id) { + // The killCursor command on the DB client is only used by sharding, + // so no need to have it for MongoD. + verify(!"killCursor should not be used in MongoD"); +} + +const HostAndPort DBDirectClient::dummyHost("0.0.0.0", 0); + +unsigned long long DBDirectClient::count( + const string& ns, const BSONObj& query, int options, int limit, int skip) { + BSONObj cmdObj = _countCmd(ns, query, options, limit, skip); + + NamespaceString nsString(ns); + std::string dbname = nsString.db().toString(); + + Command* countCmd = Command::findCommand("count"); + invariant(countCmd); + + std::string errmsg; + BSONObjBuilder result; + bool runRetval = countCmd->run(_txn, dbname, cmdObj, options, errmsg, result); + if (!runRetval) { + Command::appendCommandStatus(result, runRetval, errmsg); + Status commandStatus = Command::getStatusFromCommandResult(result.obj()); + invariant(!commandStatus.isOK()); + uassertStatusOK(commandStatus); + } + + BSONObj resultObj = result.obj(); + return static_cast<unsigned long long>(resultObj["n"].numberLong()); +} } // namespace mongo |