diff options
author | Mathias Stearn <mathias@10gen.com> | 2017-07-10 15:44:26 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2017-07-26 15:13:34 -0400 |
commit | 191931c4390ab1ede373e6772aa4d3999518fdaf (patch) | |
tree | c35ed32b3570ee9c420f1ee27e6aeb45907952a8 /src/mongo | |
parent | 516e0a50a0fb0e9df45cdc5f74cf0308c3706366 (diff) | |
download | mongo-191931c4390ab1ede373e6772aa4d3999518fdaf.tar.gz |
SERVER-28509 Flatten DBClient hierarchy to just DBClientBase and subclasses
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/client/authenticate.h | 1 | ||||
-rw-r--r-- | src/mongo/client/connection_pool.h | 2 | ||||
-rw-r--r-- | src/mongo/client/dbclient.cpp | 164 | ||||
-rw-r--r-- | src/mongo/client/dbclient_rs.cpp | 6 | ||||
-rw-r--r-- | src/mongo/client/dbclient_rs.h | 5 | ||||
-rw-r--r-- | src/mongo/client/dbclientcursor.cpp | 2 | ||||
-rw-r--r-- | src/mongo/client/dbclientinterface.h | 177 | ||||
-rw-r--r-- | src/mongo/client/index_spec.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/pipeline/document_source_out.cpp | 4 | ||||
-rw-r--r-- | src/mongo/dbtests/mock/mock_dbclient_connection.cpp | 2 | ||||
-rw-r--r-- | src/mongo/dbtests/mock/mock_dbclient_connection.h | 5 | ||||
-rw-r--r-- | src/mongo/scripting/engine.cpp | 2 | ||||
-rw-r--r-- | src/mongo/scripting/engine.h | 7 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/mongo.cpp | 6 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils.cpp | 8 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils.h | 6 |
16 files changed, 169 insertions, 230 deletions
diff --git a/src/mongo/client/authenticate.h b/src/mongo/client/authenticate.h index 552d43cdafb..d08bd030b62 100644 --- a/src/mongo/client/authenticate.h +++ b/src/mongo/client/authenticate.h @@ -38,7 +38,6 @@ namespace mongo { -class DBClientWithCommands; class BSONObj; namespace auth { diff --git a/src/mongo/client/connection_pool.h b/src/mongo/client/connection_pool.h index 853911b69a7..d3f6477865a 100644 --- a/src/mongo/client/connection_pool.h +++ b/src/mongo/client/connection_pool.h @@ -46,7 +46,7 @@ class NetworkConnectionHook; /** * Represents a pool of connections to a MongoDB server. The pool is synchronized internally * and its methods can be called from multiple threads, however once a connection is obtained - * it should only be used from one thread in accordance with the rules of DBClientInterface. + * it should only be used from one thread in accordance with the rules of DBClientBase. */ class ConnectionPool { MONGO_DISALLOW_COPYING(ConnectionPool); diff --git a/src/mongo/client/dbclient.cpp b/src/mongo/client/dbclient.cpp index e9de4469a2d..95b66f88c2d 100644 --- a/src/mongo/client/dbclient.cpp +++ b/src/mongo/client/dbclient.cpp @@ -111,16 +111,16 @@ AtomicInt64 DBClientBase::ConnectionIdSequence; /* --- dbclientcommands --- */ -bool DBClientWithCommands::isOk(const BSONObj& o) { +bool DBClientBase::isOk(const BSONObj& o) { return o["ok"].trueValue(); } -bool DBClientWithCommands::isNotMasterErrorString(const BSONElement& e) { +bool DBClientBase::isNotMasterErrorString(const BSONElement& e) { return e.type() == String && str::contains(e.valuestr(), "not master"); } -enum QueryOptions DBClientWithCommands::availableOptions() { +enum QueryOptions DBClientBase::availableOptions() { if (!_haveCachedAvailableOptions) { _cachedAvailableOptions = _lookupAvailableOptions(); _haveCachedAvailableOptions = true; @@ -128,7 +128,7 @@ enum QueryOptions DBClientWithCommands::availableOptions() { return _cachedAvailableOptions; } -enum QueryOptions DBClientWithCommands::_lookupAvailableOptions() { +enum QueryOptions DBClientBase::_lookupAvailableOptions() { BSONObj ret; if (runCommand("admin", BSON("availablequeryoptions" << 1), ret)) { return QueryOptions(ret.getIntField("options")); @@ -136,39 +136,39 @@ enum QueryOptions DBClientWithCommands::_lookupAvailableOptions() { return QueryOptions(0); } -rpc::ProtocolSet DBClientWithCommands::getClientRPCProtocols() const { +rpc::ProtocolSet DBClientBase::getClientRPCProtocols() const { return _clientRPCProtocols; } -rpc::ProtocolSet DBClientWithCommands::getServerRPCProtocols() const { +rpc::ProtocolSet DBClientBase::getServerRPCProtocols() const { return _serverRPCProtocols; } -void DBClientWithCommands::setClientRPCProtocols(rpc::ProtocolSet protocols) { +void DBClientBase::setClientRPCProtocols(rpc::ProtocolSet protocols) { _clientRPCProtocols = std::move(protocols); } -void DBClientWithCommands::_setServerRPCProtocols(rpc::ProtocolSet protocols) { +void DBClientBase::_setServerRPCProtocols(rpc::ProtocolSet protocols) { _serverRPCProtocols = std::move(protocols); } -void DBClientWithCommands::setRequestMetadataWriter(rpc::RequestMetadataWriter writer) { +void DBClientBase::setRequestMetadataWriter(rpc::RequestMetadataWriter writer) { _metadataWriter = std::move(writer); } -const rpc::RequestMetadataWriter& DBClientWithCommands::getRequestMetadataWriter() { +const rpc::RequestMetadataWriter& DBClientBase::getRequestMetadataWriter() { return _metadataWriter; } -void DBClientWithCommands::setReplyMetadataReader(rpc::ReplyMetadataReader reader) { +void DBClientBase::setReplyMetadataReader(rpc::ReplyMetadataReader reader) { _metadataReader = std::move(reader); } -const rpc::ReplyMetadataReader& DBClientWithCommands::getReplyMetadataReader() { +const rpc::ReplyMetadataReader& DBClientBase::getReplyMetadataReader() { return _metadataReader; } -std::pair<rpc::UniqueReply, DBClientWithCommands*> DBClientWithCommands::runCommandWithTarget( +std::pair<rpc::UniqueReply, DBClientBase*> DBClientBase::runCommandWithTarget( OpMsgRequest request) { // Make sure to reconnect if needed before building our request, since the request depends on // the negotiated protocol which can change due to a reconnect. @@ -226,8 +226,10 @@ std::pair<rpc::UniqueReply, DBClientWithCommands*> DBClientWithCommands::runComm return {rpc::UniqueReply(std::move(replyMsg), std::move(commandReply)), this}; } -std::tuple<bool, DBClientWithCommands*> DBClientWithCommands::runCommandWithTarget( - const string& dbname, BSONObj cmd, BSONObj& info, int options) { +std::tuple<bool, DBClientBase*> DBClientBase::runCommandWithTarget(const string& dbname, + BSONObj cmd, + BSONObj& info, + int options) { // TODO: This will be downconverted immediately if the underlying // requestBuilder is a legacyRequest builder. Not sure what the best // way to get around that is without breaking the abstraction. @@ -237,10 +239,7 @@ std::tuple<bool, DBClientWithCommands*> DBClientWithCommands::runCommandWithTarg return std::make_tuple(isOk(info), result.second); } -bool DBClientWithCommands::runCommand(const string& dbname, - BSONObj cmd, - BSONObj& info, - int options) { +bool DBClientBase::runCommand(const string& dbname, BSONObj cmd, BSONObj& info, int options) { auto res = runCommandWithTarget(dbname, std::move(cmd), info, options); return std::get<0>(res); } @@ -249,9 +248,7 @@ bool DBClientWithCommands::runCommand(const string& dbname, /* note - we build a bson obj here -- for something that is super common like getlasterror you should have that object prebuilt as that would be faster. */ -bool DBClientWithCommands::simpleCommand(const string& dbname, - BSONObj* info, - const string& command) { +bool DBClientBase::simpleCommand(const string& dbname, BSONObj* info, const string& command) { BSONObj o; if (info == 0) info = &o; @@ -260,12 +257,12 @@ bool DBClientWithCommands::simpleCommand(const string& dbname, return runCommand(dbname, b.done(), *info); } -bool DBClientWithCommands::runPseudoCommand(StringData db, - StringData realCommandName, - StringData pseudoCommandCol, - const BSONObj& cmdArgs, - BSONObj& info, - int options) { +bool DBClientBase::runPseudoCommand(StringData db, + StringData realCommandName, + StringData pseudoCommandCol, + const BSONObj& cmdArgs, + BSONObj& info, + int options) { BSONObjBuilder bob; bob.append(realCommandName, 1); bob.appendElements(cmdArgs); @@ -293,7 +290,7 @@ bool DBClientWithCommands::runPseudoCommand(StringData db, return success; } -unsigned long long DBClientWithCommands::count( +unsigned long long DBClientBase::count( const string& myns, const BSONObj& query, int options, int limit, int skip) { BSONObj cmd = _countCmd(myns, query, options, limit, skip); BSONObj res; @@ -302,7 +299,7 @@ unsigned long long DBClientWithCommands::count( return res["n"].numberLong(); } -BSONObj DBClientWithCommands::_countCmd( +BSONObj DBClientBase::_countCmd( const string& myns, const BSONObj& query, int options, int limit, int skip) { NamespaceString ns(myns); BSONObjBuilder b; @@ -315,11 +312,11 @@ BSONObj DBClientWithCommands::_countCmd( return b.obj(); } -BSONObj DBClientWithCommands::getLastErrorDetailed(bool fsync, bool j, int w, int wtimeout) { +BSONObj DBClientBase::getLastErrorDetailed(bool fsync, bool j, int w, int wtimeout) { return getLastErrorDetailed("admin", fsync, j, w, wtimeout); } -BSONObj DBClientWithCommands::getLastErrorDetailed( +BSONObj DBClientBase::getLastErrorDetailed( const std::string& db, bool fsync, bool j, int w, int wtimeout) { BSONObj info; BSONObjBuilder b; @@ -344,17 +341,16 @@ BSONObj DBClientWithCommands::getLastErrorDetailed( return info; } -string DBClientWithCommands::getLastError(bool fsync, bool j, int w, int wtimeout) { +string DBClientBase::getLastError(bool fsync, bool j, int w, int wtimeout) { return getLastError("admin", fsync, j, w, wtimeout); } -string DBClientWithCommands::getLastError( - const std::string& db, bool fsync, bool j, int w, int wtimeout) { +string DBClientBase::getLastError(const std::string& db, bool fsync, bool j, int w, int wtimeout) { BSONObj info = getLastErrorDetailed(db, fsync, j, w, wtimeout); return getLastErrorString(info); } -string DBClientWithCommands::getLastErrorString(const BSONObj& info) { +string DBClientBase::getLastErrorString(const BSONObj& info) { if (info["ok"].trueValue()) { BSONElement e = info["err"]; if (e.eoo()) @@ -375,14 +371,13 @@ string DBClientWithCommands::getLastErrorString(const BSONObj& info) { const BSONObj getpreverrorcmdobj = fromjson("{getpreverror:1}"); -BSONObj DBClientWithCommands::getPrevError() { +BSONObj DBClientBase::getPrevError() { BSONObj info; runCommand("admin", getpreverrorcmdobj, info); return info; } -string DBClientWithCommands::createPasswordDigest(const string& username, - const string& clearTextPassword) { +string DBClientBase::createPasswordDigest(const string& username, const string& clearTextPassword) { return mongo::createPasswordDigest(username, clearTextPassword); } @@ -391,7 +386,7 @@ class ScopedMetadataWriterRemover { MONGO_DISALLOW_COPYING(ScopedMetadataWriterRemover); public: - ScopedMetadataWriterRemover(DBClientWithCommands* cli) + ScopedMetadataWriterRemover(DBClientBase* cli) : _cli(cli), _oldWriter(cli->getRequestMetadataWriter()) { _cli->setRequestMetadataWriter(rpc::RequestMetadataWriter{}); } @@ -400,12 +395,12 @@ public: } private: - DBClientWithCommands* const _cli; + DBClientBase* const _cli; rpc::RequestMetadataWriter _oldWriter; }; } // namespace -void DBClientWithCommands::_auth(const BSONObj& params) { +void DBClientBase::_auth(const BSONObj& params) { ScopedMetadataWriterRemover remover{this}; // We will only have a client name if SSL is enabled @@ -441,7 +436,7 @@ void DBClientWithCommands::_auth(const BSONObj& params) { }); } -bool DBClientWithCommands::authenticateInternalUser() { +bool DBClientBase::authenticateInternalUser() { if (!isInternalAuthSet()) { if (!serverGlobalParams.quiet.load()) { log() << "ERROR: No authentication parameters set for internal user"; @@ -461,15 +456,15 @@ bool DBClientWithCommands::authenticateInternalUser() { } } -void DBClientWithCommands::auth(const BSONObj& params) { +void DBClientBase::auth(const BSONObj& params) { _auth(params); } -bool DBClientWithCommands::auth(const string& dbname, - const string& username, - const string& password_text, - string& errmsg, - bool digestPassword) { +bool DBClientBase::auth(const string& dbname, + const string& username, + const string& password_text, + string& errmsg, + bool digestPassword) { try { const auto authParams = auth::buildAuthParams(dbname, username, password_text, digestPassword); @@ -483,13 +478,13 @@ bool DBClientWithCommands::auth(const string& dbname, } } -void DBClientWithCommands::logout(const string& dbname, BSONObj& info) { +void DBClientBase::logout(const string& dbname, BSONObj& info) { runCommand(dbname, BSON("logout" << 1), info); } BSONObj ismastercmdobj = fromjson("{\"ismaster\":1}"); -bool DBClientWithCommands::isMaster(bool& isMaster, BSONObj* info) { +bool DBClientBase::isMaster(bool& isMaster, BSONObj* info) { BSONObj o; if (info == 0) info = &o; @@ -498,7 +493,7 @@ bool DBClientWithCommands::isMaster(bool& isMaster, BSONObj* info) { return ok; } -bool DBClientWithCommands::createCollection( +bool DBClientBase::createCollection( const string& ns, long long size, bool capped, int max, BSONObj* info) { verify(!capped || size); BSONObj o; @@ -516,10 +511,10 @@ bool DBClientWithCommands::createCollection( return runCommand(db.c_str(), b.done(), *info); } -bool DBClientWithCommands::copyDatabase(const string& fromdb, - const string& todb, - const string& fromhost, - BSONObj* info) { +bool DBClientBase::copyDatabase(const string& fromdb, + const string& todb, + const string& fromhost, + BSONObj* info) { BSONObj o; if (info == 0) info = &o; @@ -531,11 +526,11 @@ bool DBClientWithCommands::copyDatabase(const string& fromdb, return runCommand("admin", b.done(), *info); } -bool DBClientWithCommands::eval(const string& dbname, - const string& jscode, - BSONObj& info, - BSONElement& retValue, - BSONObj* args) { +bool DBClientBase::eval(const string& dbname, + const string& jscode, + BSONObj& info, + BSONElement& retValue, + BSONObj* args) { BSONObjBuilder b; b.appendCode("$eval", jscode); if (args) @@ -546,13 +541,13 @@ bool DBClientWithCommands::eval(const string& dbname, return ok; } -bool DBClientWithCommands::eval(const string& dbname, const string& jscode) { +bool DBClientBase::eval(const string& dbname, const string& jscode) { BSONObj info; BSONElement retValue; return eval(dbname, jscode, info, retValue); } -list<BSONObj> DBClientWithCommands::getCollectionInfos(const string& db, const BSONObj& filter) { +list<BSONObj> DBClientBase::getCollectionInfos(const string& db, const BSONObj& filter) { list<BSONObj> infos; BSONObj res; @@ -586,7 +581,7 @@ list<BSONObj> DBClientWithCommands::getCollectionInfos(const string& db, const B uasserted(18630, str::stream() << "listCollections failed: " << res); } -bool DBClientWithCommands::exists(const string& ns) { +bool DBClientBase::exists(const string& ns) { BSONObj filter = BSON("name" << nsToCollectionSubstring(ns)); list<BSONObj> results = getCollectionInfos(nsToDatabase(ns), filter); return !results.empty(); @@ -608,13 +603,13 @@ void DBClientConnection::_auth(const BSONObj& params) { /** query N objects from the database into an array. makes sense mostly when you want a small * number of results. if a huge number, use query() and iterate the cursor. */ -void DBClientInterface::findN(vector<BSONObj>& out, - const string& ns, - Query query, - int nToReturn, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions) { +void DBClientBase::findN(vector<BSONObj>& out, + const string& ns, + Query query, + int nToReturn, + int nToSkip, + const BSONObj* fieldsToReturn, + int queryOptions) { out.reserve(nToReturn); unique_ptr<DBClientCursor> c = @@ -641,10 +636,10 @@ void DBClientInterface::findN(vector<BSONObj>& out, } } -BSONObj DBClientInterface::findOne(const string& ns, - const Query& query, - const BSONObj* fieldsToReturn, - int queryOptions) { +BSONObj DBClientBase::findOne(const string& ns, + const Query& query, + const BSONObj* fieldsToReturn, + int queryOptions) { vector<BSONObj> v; findN(v, ns, query, 1, 0, fieldsToReturn, queryOptions); return v.empty() ? BSONObj() : v[0]; @@ -881,10 +876,9 @@ void DBClientConnection::logout(const string& dbname, BSONObj& info) { runCommand(dbname, BSON("logout" << 1), info); } -std::pair<rpc::UniqueReply, DBClientWithCommands*> DBClientConnection::runCommandWithTarget( +std::pair<rpc::UniqueReply, DBClientBase*> DBClientConnection::runCommandWithTarget( OpMsgRequest request) { - - auto out = DBClientWithCommands::runCommandWithTarget(std::move(request)); + auto out = DBClientBase::runCommandWithTarget(std::move(request)); if (!_parentReplSetName.empty()) { const auto replyBody = out.first->getCommandReply(); if (!isOk(replyBody)) { @@ -1097,7 +1091,7 @@ void DBClientBase::killCursor(long long cursorId) { say(msg); } -list<BSONObj> DBClientWithCommands::getIndexSpecs(const string& ns, int options) { +list<BSONObj> DBClientBase::getIndexSpecs(const string& ns, int options) { list<BSONObj> specs; BSONObj cmd = BSON("listIndexes" << nsToCollectionSubstring(ns) << "cursor" << BSONObj()); @@ -1131,12 +1125,12 @@ list<BSONObj> DBClientWithCommands::getIndexSpecs(const string& ns, int options) } -void DBClientWithCommands::dropIndex(const string& ns, BSONObj keys) { +void DBClientBase::dropIndex(const string& ns, BSONObj keys) { dropIndex(ns, genIndexName(keys)); } -void DBClientWithCommands::dropIndex(const string& ns, const string& indexName) { +void DBClientBase::dropIndex(const string& ns, const string& indexName) { BSONObj info; if (!runCommand(nsToDatabase(ns), BSON("deleteIndexes" << nsToCollectionSubstring(ns) << "index" << indexName), @@ -1146,7 +1140,7 @@ void DBClientWithCommands::dropIndex(const string& ns, const string& indexName) } } -void DBClientWithCommands::dropIndexes(const string& ns) { +void DBClientBase::dropIndexes(const string& ns) { BSONObj info; uassert(10008, "dropIndexes failed", @@ -1156,7 +1150,7 @@ void DBClientWithCommands::dropIndexes(const string& ns) { info)); } -void DBClientWithCommands::reIndex(const string& ns) { +void DBClientBase::reIndex(const string& ns) { BSONObj info; uassert(18908, str::stream() << "reIndex failed: " << info, @@ -1164,7 +1158,7 @@ void DBClientWithCommands::reIndex(const string& ns) { } -string DBClientWithCommands::genIndexName(const BSONObj& keys) { +string DBClientBase::genIndexName(const BSONObj& keys) { stringstream ss; bool first = 1; @@ -1185,7 +1179,7 @@ string DBClientWithCommands::genIndexName(const BSONObj& keys) { return ss.str(); } -void DBClientWithCommands::createIndex(StringData ns, const IndexSpec& descriptor) { +void DBClientBase::createIndex(StringData ns, const IndexSpec& descriptor) { const BSONObj descriptorObj = descriptor.toBSON(); BSONObjBuilder command; diff --git a/src/mongo/client/dbclient_rs.cpp b/src/mongo/client/dbclient_rs.cpp index 86607531b47..4b589fbdb22 100644 --- a/src/mongo/client/dbclient_rs.cpp +++ b/src/mongo/client/dbclient_rs.cpp @@ -186,7 +186,7 @@ void DBClientReplicaSet::setRequestMetadataWriter(rpc::RequestMetadataWriter wri if (_lastSlaveOkConn.get()) { _lastSlaveOkConn->setRequestMetadataWriter(writer); } - DBClientWithCommands::setRequestMetadataWriter(std::move(writer)); + DBClientBase::setRequestMetadataWriter(std::move(writer)); } void DBClientReplicaSet::setReplyMetadataReader(rpc::ReplyMetadataReader reader) { @@ -197,7 +197,7 @@ void DBClientReplicaSet::setReplyMetadataReader(rpc::ReplyMetadataReader reader) if (_lastSlaveOkConn.get()) { _lastSlaveOkConn->setReplyMetadataReader(reader); } - DBClientWithCommands::setReplyMetadataReader(std::move(reader)); + DBClientBase::setReplyMetadataReader(std::move(reader)); } int DBClientReplicaSet::getMinWireVersion() { @@ -901,7 +901,7 @@ void DBClientReplicaSet::checkResponse(const std::vector<BSONObj>& batch, } } -std::pair<rpc::UniqueReply, DBClientWithCommands*> DBClientReplicaSet::runCommandWithTarget( +std::pair<rpc::UniqueReply, DBClientBase*> DBClientReplicaSet::runCommandWithTarget( OpMsgRequest request) { // This overload exists so we can parse out the read preference and then use server // selection directly without having to re-parse the raw message. diff --git a/src/mongo/client/dbclient_rs.h b/src/mongo/client/dbclient_rs.h index d2f4a046bea..784b03bef3a 100644 --- a/src/mongo/client/dbclient_rs.h +++ b/src/mongo/client/dbclient_rs.h @@ -186,9 +186,8 @@ public: return true; } - using DBClientWithCommands::runCommandWithTarget; - std::pair<rpc::UniqueReply, DBClientWithCommands*> runCommandWithTarget( - OpMsgRequest request) final; + using DBClientBase::runCommandWithTarget; + std::pair<rpc::UniqueReply, DBClientBase*> runCommandWithTarget(OpMsgRequest request) final; void setRequestMetadataWriter(rpc::RequestMetadataWriter writer) final; diff --git a/src/mongo/client/dbclientcursor.cpp b/src/mongo/client/dbclientcursor.cpp index 3af409de4ad..832c384cbdf 100644 --- a/src/mongo/client/dbclientcursor.cpp +++ b/src/mongo/client/dbclientcursor.cpp @@ -58,7 +58,7 @@ using std::string; using std::vector; namespace { -Message assembleCommandRequest(DBClientWithCommands* cli, +Message assembleCommandRequest(DBClientBase* cli, StringData database, int legacyQueryOptions, BSONObj legacyQuery) { diff --git a/src/mongo/client/dbclientinterface.h b/src/mongo/client/dbclientinterface.h index 1b16532baac..d6bd3389901 100644 --- a/src/mongo/client/dbclientinterface.h +++ b/src/mongo/client/dbclientinterface.h @@ -164,35 +164,19 @@ std::string nsGetDB(const std::string& ns); std::string nsGetCollection(const std::string& ns); /** - The interface that any db connection should implement + abstract class that implements the core db operations */ -class DBClientInterface { - MONGO_DISALLOW_COPYING(DBClientInterface); +class DBClientBase { + MONGO_DISALLOW_COPYING(DBClientBase); public: - virtual std::unique_ptr<DBClientCursor> query(const std::string& ns, - Query query, - int nToReturn = 0, - int nToSkip = 0, - const BSONObj* fieldsToReturn = 0, - int queryOptions = 0, - int batchSize = 0) = 0; - - virtual void insert(const std::string& ns, BSONObj obj, int flags = 0) = 0; - - virtual void insert(const std::string& ns, const std::vector<BSONObj>& v, int flags = 0) = 0; - - virtual void remove(const std::string& ns, Query query, int flags) = 0; - - virtual void update(const std::string& ns, - Query query, - BSONObj obj, - bool upsert = false, - bool multi = false) = 0; - - virtual void update(const std::string& ns, Query query, BSONObj obj, int flags) = 0; + DBClientBase() + : _logLevel(logger::LogSeverity::Log()), + _connectionId(ConnectionIdSequence.fetchAndAdd(1)), + _cachedAvailableOptions((enum QueryOptions)0), + _haveCachedAvailableOptions(false) {} - virtual ~DBClientInterface() {} + virtual ~DBClientBase() {} /** @return a single object that matches the query. if none do, then the object is empty @@ -216,30 +200,6 @@ public: virtual std::string getServerAddress() const = 0; - /** don't use this - called automatically by DBClientCursor for you */ - virtual std::unique_ptr<DBClientCursor> getMore(const std::string& ns, - long long cursorId, - int nToReturn = 0, - int options = 0) = 0; - -protected: - DBClientInterface() = default; -}; - -/** - DB "commands" - Basically just invocations of connection.$cmd.findOne({...}); -*/ -class DBClientWithCommands : public DBClientInterface { -public: - /** controls how chatty the client is about network errors & such. See log.h */ - logger::LogSeverity _logLevel; - - DBClientWithCommands() - : _logLevel(logger::LogSeverity::Log()), - _cachedAvailableOptions((enum QueryOptions)0), - _haveCachedAvailableOptions(false) {} - /** helper function. run a simple command where the command expression is simply { command : 1 } @param info -- where to put result object. may be null if caller doesn't need that info @@ -316,8 +276,7 @@ public: /** * Runs the specified command request. */ - virtual std::pair<rpc::UniqueReply, DBClientWithCommands*> runCommandWithTarget( - OpMsgRequest request); + virtual std::pair<rpc::UniqueReply, DBClientBase*> runCommandWithTarget(OpMsgRequest request); /** * Runs the specified command request. This thin wrapper just unwraps the reply and ignores the @@ -349,10 +308,10 @@ public: * * This is used in the shell so that cursors can send getMore through the correct connection. */ - std::tuple<bool, DBClientWithCommands*> runCommandWithTarget(const std::string& dbname, - BSONObj cmd, - BSONObj& info, - int options = 0); + std::tuple<bool, DBClientBase*> runCommandWithTarget(const std::string& dbname, + BSONObj cmd, + BSONObj& info, + int options = 0); /** * Authenticates to another cluster member using appropriate authentication data. @@ -684,64 +643,8 @@ public: */ virtual void checkConnection() {} -protected: - /** if the result of a command is ok*/ - bool isOk(const BSONObj&); - - /** if the element contains a not master error */ - bool isNotMasterErrorString(const BSONElement& e); - - BSONObj _countCmd( - const std::string& ns, const BSONObj& query, int options, int limit, int skip); - - /** - * Look up the options available on this client. Caches the answer from - * _lookupAvailableOptions(), below. - */ - QueryOptions availableOptions(); - - virtual QueryOptions _lookupAvailableOptions(); - - virtual void _auth(const BSONObj& params); - - // should be set by subclasses during connection. - void _setServerRPCProtocols(rpc::ProtocolSet serverProtocols); - -private: - /** - * The rpc protocols this client supports. - * - */ - rpc::ProtocolSet _clientRPCProtocols{rpc::supports::kAll}; - - /** - * The rpc protocol the remote server(s) support. We support 'opQueryOnly' by default unless - * we detect support for OP_COMMAND at connection time. - */ - rpc::ProtocolSet _serverRPCProtocols{rpc::supports::kOpQueryOnly}; - - rpc::RequestMetadataWriter _metadataWriter; - rpc::ReplyMetadataReader _metadataReader; - - enum QueryOptions _cachedAvailableOptions; - bool _haveCachedAvailableOptions; -}; - -/** - abstract class that implements the core db operations - */ -class DBClientBase : public DBClientWithCommands { -protected: - static AtomicInt64 ConnectionIdSequence; - long long _connectionId; // unique connection id for this connection - -public: static const uint64_t INVALID_SOCK_CREATION_TIME; - DBClientBase() { - _connectionId = ConnectionIdSequence.fetchAndAdd(1); - } - long long getConnectionId() const { return _connectionId; } @@ -843,6 +746,53 @@ public: virtual void reset() {} +protected: + /** if the result of a command is ok*/ + bool isOk(const BSONObj&); + + /** if the element contains a not master error */ + bool isNotMasterErrorString(const BSONElement& e); + + BSONObj _countCmd( + const std::string& ns, const BSONObj& query, int options, int limit, int skip); + + /** + * Look up the options available on this client. Caches the answer from + * _lookupAvailableOptions(), below. + */ + QueryOptions availableOptions(); + + virtual QueryOptions _lookupAvailableOptions(); + + virtual void _auth(const BSONObj& params); + + // should be set by subclasses during connection. + void _setServerRPCProtocols(rpc::ProtocolSet serverProtocols); + + /** controls how chatty the client is about network errors & such. See log.h */ + const logger::LogSeverity _logLevel; + + static AtomicInt64 ConnectionIdSequence; + long long _connectionId; // unique connection id for this connection + +private: + /** + * The rpc protocols this client supports. + * + */ + rpc::ProtocolSet _clientRPCProtocols{rpc::supports::kAll}; + + /** + * The rpc protocol the remote server(s) support. We support 'opQueryOnly' by default unless + * we detect support for OP_COMMAND at connection time. + */ + rpc::ProtocolSet _serverRPCProtocols{rpc::supports::kOpQueryOnly}; + + rpc::RequestMetadataWriter _metadataWriter; + rpc::ReplyMetadataReader _metadataReader; + + enum QueryOptions _cachedAvailableOptions; + bool _haveCachedAvailableOptions; }; // DBClientBase /** @@ -947,9 +897,8 @@ public: const BSONObj* fieldsToReturn, int queryOptions); - using DBClientWithCommands::runCommandWithTarget; - std::pair<rpc::UniqueReply, DBClientWithCommands*> runCommandWithTarget( - OpMsgRequest request) override; + using DBClientBase::runCommandWithTarget; + std::pair<rpc::UniqueReply, DBClientBase*> runCommandWithTarget(OpMsgRequest request) override; /** @return true if this connection is currently in a failed state. When autoreconnect is on, diff --git a/src/mongo/client/index_spec.cpp b/src/mongo/client/index_spec.cpp index bf725088496..25e800bf140 100644 --- a/src/mongo/client/index_spec.cpp +++ b/src/mongo/client/index_spec.cpp @@ -233,7 +233,7 @@ BSONObj IndexSpec::toBSON() const { void IndexSpec::_rename() { if (!_dynamicName) return; - _name = DBClientWithCommands::genIndexName(_keys.asTempObj()); + _name = DBClientBase::genIndexName(_keys.asTempObj()); } } // namespace mongo diff --git a/src/mongo/db/pipeline/document_source_out.cpp b/src/mongo/db/pipeline/document_source_out.cpp index 069257abcb9..5eb3d8b4b51 100644 --- a/src/mongo/db/pipeline/document_source_out.cpp +++ b/src/mongo/db/pipeline/document_source_out.cpp @@ -137,7 +137,7 @@ void DocumentSourceOut::initialize() { << indexBson << " error: " << err, - DBClientWithCommands::getLastErrorString(err).empty()); + DBClientBase::getLastErrorString(err).empty()); } _initialized = true; } @@ -146,7 +146,7 @@ void DocumentSourceOut::spill(const vector<BSONObj>& toInsert) { BSONObj err = _mongod->insert(_tempNs, toInsert); uassert(16996, str::stream() << "insert for $out failed: " << err, - DBClientWithCommands::getLastErrorString(err).empty()); + DBClientBase::getLastErrorString(err).empty()); } DocumentSource::GetNextResult DocumentSourceOut::getNext() { diff --git a/src/mongo/dbtests/mock/mock_dbclient_connection.cpp b/src/mongo/dbtests/mock/mock_dbclient_connection.cpp index b69409b017a..43f37717117 100644 --- a/src/mongo/dbtests/mock/mock_dbclient_connection.cpp +++ b/src/mongo/dbtests/mock/mock_dbclient_connection.cpp @@ -60,7 +60,7 @@ bool MockDBClientConnection::connect(const char* hostName, return false; } -std::pair<rpc::UniqueReply, DBClientWithCommands*> MockDBClientConnection::runCommandWithTarget( +std::pair<rpc::UniqueReply, DBClientBase*> MockDBClientConnection::runCommandWithTarget( OpMsgRequest request) { checkConnection(); diff --git a/src/mongo/dbtests/mock/mock_dbclient_connection.h b/src/mongo/dbtests/mock/mock_dbclient_connection.h index bcb57be8aaa..c85089f234f 100644 --- a/src/mongo/dbtests/mock/mock_dbclient_connection.h +++ b/src/mongo/dbtests/mock/mock_dbclient_connection.h @@ -64,9 +64,8 @@ public: return connect(host.toString().c_str(), applicationName, errmsg); } - using DBClientWithCommands::runCommandWithTarget; - std::pair<rpc::UniqueReply, DBClientWithCommands*> runCommandWithTarget( - OpMsgRequest request) override; + using DBClientBase::runCommandWithTarget; + std::pair<rpc::UniqueReply, DBClientBase*> runCommandWithTarget(OpMsgRequest request) override; std::unique_ptr<mongo::DBClientCursor> query(const std::string& ns, mongo::Query query = mongo::Query(), diff --git a/src/mongo/scripting/engine.cpp b/src/mongo/scripting/engine.cpp index 8ae118d21da..483ed7cecd9 100644 --- a/src/mongo/scripting/engine.cpp +++ b/src/mongo/scripting/engine.cpp @@ -554,7 +554,7 @@ unique_ptr<Scope> ScriptEngine::getPooledScope(OperationContext* opCtx, return p; } -void (*ScriptEngine::_connectCallback)(DBClientWithCommands&) = 0; +void (*ScriptEngine::_connectCallback)(DBClientBase&) = 0; ScriptEngine* getGlobalScriptEngine() { if (hasGlobalServiceContext()) diff --git a/src/mongo/scripting/engine.h b/src/mongo/scripting/engine.h index e61fde92774..ff6dd1b1616 100644 --- a/src/mongo/scripting/engine.h +++ b/src/mongo/scripting/engine.h @@ -38,7 +38,6 @@ typedef unsigned long long ScriptingFunction; typedef BSONObj (*NativeFunction)(const BSONObj& args, void* data); typedef std::map<std::string, ScriptingFunction> FunctionCacheMap; -class DBClientWithCommands; class DBClientBase; class OperationContext; @@ -264,10 +263,10 @@ public: void setScopeInitCallback(void (*func)(Scope&)) { _scopeInitCallback = func; } - static void setConnectCallback(void (*func)(DBClientWithCommands&)) { + static void setConnectCallback(void (*func)(DBClientBase&)) { _connectCallback = func; } - static void runConnectCallback(DBClientWithCommands& c) { + static void runConnectCallback(DBClientBase& c) { if (_connectCallback) _connectCallback(c); } @@ -285,7 +284,7 @@ protected: void (*_scopeInitCallback)(Scope&); private: - static void (*_connectCallback)(DBClientWithCommands&); + static void (*_connectCallback)(DBClientBase&); }; void installGlobalUtils(Scope& scope); diff --git a/src/mongo/scripting/mozjs/mongo.cpp b/src/mongo/scripting/mozjs/mongo.cpp index bbade839e02..1c22e4e5e27 100644 --- a/src/mongo/scripting/mozjs/mongo.cpp +++ b/src/mongo/scripting/mozjs/mongo.cpp @@ -125,8 +125,8 @@ void setCursorHandle(MozJSImplScope* scope, } void setHiddenMongo(JSContext* cx, - DBClientWithCommands* resPtr, - DBClientWithCommands* origConn, + DBClientBase* resPtr, + DBClientBase* origConn, JS::CallArgs& args) { ObjectWrapper o(cx, args.rval()); // If the connection that ran the command is the same as conn, then we set a hidden "_mongo" @@ -545,7 +545,7 @@ void MongoBase::Functions::copyDatabaseWithSCRAM::call(JSContext* cx, JS::CallAr std::string password = ValueWriter(cx, args.get(4)).toString(); bool slaveOk = ValueWriter(cx, args.get(5)).toBoolean(); - std::string hashedPwd = DBClientWithCommands::createPasswordDigest(user, password); + std::string hashedPwd = DBClientBase::createPasswordDigest(user, password); std::unique_ptr<SaslClientSession> session(new NativeSaslClientSession()); diff --git a/src/mongo/shell/shell_utils.cpp b/src/mongo/shell/shell_utils.cpp index d165ae3e004..4c1d70f6f03 100644 --- a/src/mongo/shell/shell_utils.cpp +++ b/src/mongo/shell/shell_utils.cpp @@ -280,10 +280,10 @@ bool Prompter::confirm() { ConnectionRegistry::ConnectionRegistry() = default; -void ConnectionRegistry::registerConnection(DBClientWithCommands& client) { +void ConnectionRegistry::registerConnection(DBClientBase& client) { BSONObj info; if (client.runCommand("admin", BSON("whatsmyuri" << 1), info)) { - string connstr = dynamic_cast<DBClientBase&>(client).getServerAddress(); + string connstr = client.getServerAddress(); stdx::lock_guard<stdx::mutex> lk(_mutex); _connectionUris[connstr].insert(info["you"].str()); } @@ -303,7 +303,7 @@ void ConnectionRegistry::killOperationsOnAllConnections(bool withPrompt) const { const ConnectionString cs(status.getValue()); string errmsg; - std::unique_ptr<DBClientWithCommands> conn(cs.connect("MongoDB Shell", errmsg)); + std::unique_ptr<DBClientBase> conn(cs.connect("MongoDB Shell", errmsg)); if (!conn) { continue; } @@ -362,7 +362,7 @@ void ConnectionRegistry::killOperationsOnAllConnections(bool withPrompt) const { ConnectionRegistry connectionRegistry; bool _nokillop = false; -void onConnect(DBClientWithCommands& c) { +void onConnect(DBClientBase& c) { if (_nokillop) { return; } diff --git a/src/mongo/shell/shell_utils.h b/src/mongo/shell/shell_utils.h index b00b10f2d9c..12da7d52b14 100644 --- a/src/mongo/shell/shell_utils.h +++ b/src/mongo/shell/shell_utils.h @@ -36,7 +36,7 @@ namespace mongo { class Scope; -class DBClientWithCommands; +class DBClientBase; namespace shell_utils { @@ -48,7 +48,7 @@ void RecordMyLocation(const char* _argv0); void installShellUtils(Scope& scope); void initScope(Scope& scope); -void onConnect(DBClientWithCommands& c); +void onConnect(DBClientBase& c); const char* getUserDir(); @@ -71,7 +71,7 @@ private: class ConnectionRegistry { public: ConnectionRegistry(); - void registerConnection(DBClientWithCommands& client); + void registerConnection(DBClientBase& client); void killOperationsOnAllConnections(bool withPrompt) const; private: |