summaryrefslogtreecommitdiff
path: root/src/mongo/client/dbclientinterface.h
diff options
context:
space:
mode:
authorSpencer T Brody <spencer@mongodb.com>2015-04-15 13:24:09 -0400
committerSpencer T Brody <spencer@mongodb.com>2015-04-16 16:02:48 -0400
commit492e26db0702abd8b44794fb718f76b26afecefd (patch)
tree8b1878dc5d8e2fe17b5d5ac34160cb639d7bab6e /src/mongo/client/dbclientinterface.h
parentaa303eb3b5898842a0f5730472413d2b2fdc0930 (diff)
downloadmongo-492e26db0702abd8b44794fb718f76b26afecefd.tar.gz
SERVER-18070 Split ConnectionString out into its own library
Diffstat (limited to 'src/mongo/client/dbclientinterface.h')
-rw-r--r--src/mongo/client/dbclientinterface.h156
1 files changed, 1 insertions, 155 deletions
diff --git a/src/mongo/client/dbclientinterface.h b/src/mongo/client/dbclientinterface.h
index d53166107ae..05f62affef8 100644
--- a/src/mongo/client/dbclientinterface.h
+++ b/src/mongo/client/dbclientinterface.h
@@ -37,6 +37,7 @@
#include <boost/scoped_ptr.hpp>
#include "mongo/base/string_data.h"
+#include "mongo/client/connection_string.h"
#include "mongo/config.h"
#include "mongo/db/jsobj.h"
#include "mongo/logger/log_severity.h"
@@ -200,161 +201,6 @@ namespace mongo {
class DBClientConnection;
/**
- * ConnectionString handles parsing different ways to connect to mongo and determining method
- * samples:
- * server
- * server:port
- * foo/server:port,server:port SET
- * server,server,server SYNC
- * Warning - you usually don't want "SYNC", it's used
- * for some special things such as sharding config servers.
- * See syncclusterconnection.h for more info.
- *
- * tyipcal use
- * std::string errmsg,
- * ConnectionString cs = ConnectionString::parse( url , errmsg );
- * if ( ! cs.isValid() ) throw "bad: " + errmsg;
- * DBClientBase * conn = cs.connect( errmsg );
- */
- class ConnectionString {
- public:
-
- enum ConnectionType { INVALID , MASTER , PAIR , SET , SYNC, CUSTOM };
-
- ConnectionString() {
- _type = INVALID;
- }
-
- // Note: This should only be used for direct connections to a single server. For replica
- // set and SyncClusterConnections, use ConnectionString::parse.
- ConnectionString( const HostAndPort& server ) {
- _type = MASTER;
- _servers.push_back( server );
- _finishInit();
- }
-
- ConnectionString( ConnectionType type , const std::string& s , const std::string& setName = "" ) {
- _type = type;
- _setName = setName;
- _fillServers( s );
-
- switch ( _type ) {
- case MASTER:
- verify( _servers.size() == 1 );
- break;
- case SET:
- verify( _setName.size() );
- verify( _servers.size() >= 1 ); // 1 is ok since we can derive
- break;
- case PAIR:
- verify( _servers.size() == 2 );
- break;
- default:
- verify( _servers.size() > 0 );
- }
-
- _finishInit();
- }
-
- ConnectionString( const std::string& s , ConnectionType favoredMultipleType ) {
- _type = INVALID;
-
- _fillServers( s );
- if ( _type != INVALID ) {
- // set already
- }
- else if ( _servers.size() == 1 ) {
- _type = MASTER;
- }
- else {
- _type = favoredMultipleType;
- verify( _type == SET || _type == SYNC );
- }
- _finishInit();
- }
-
- bool isValid() const { return _type != INVALID; }
-
- std::string toString() const { return _string; }
-
- DBClientBase* connect( std::string& errmsg, double socketTimeout = 0 ) const;
-
- std::string getSetName() const { return _setName; }
-
- const std::vector<HostAndPort>& getServers() const { return _servers; }
-
- ConnectionType type() const { return _type; }
-
- /**
- * This returns true if this and other point to the same logical entity.
- * For single nodes, thats the same address.
- * For replica sets, thats just the same replica set name.
- * For pair (deprecated) or sync cluster connections, that's the same hosts in any ordering.
- */
- bool sameLogicalEndpoint( const ConnectionString& other ) const;
-
- static ConnectionString parse( const std::string& url , std::string& errmsg );
-
- static std::string typeToString( ConnectionType type );
-
- //
- // Allow overriding the default connection behavior
- // This is needed for some tests, which otherwise would fail because they are unable to contact
- // the correct servers.
- //
-
- class ConnectionHook {
- public:
- virtual ~ConnectionHook(){}
-
- // Returns an alternative connection object for a string
- virtual DBClientBase* connect( const ConnectionString& c,
- std::string& errmsg,
- double socketTimeout ) = 0;
- };
-
- static void setConnectionHook( ConnectionHook* hook ){
- boost::lock_guard<boost::mutex> lk( _connectHookMutex );
- _connectHook = hook;
- }
-
- static ConnectionHook* getConnectionHook() {
- boost::lock_guard<boost::mutex> lk( _connectHookMutex );
- return _connectHook;
- }
-
- // Allows ConnectionStrings to be stored more easily in sets/maps
- bool operator<(const ConnectionString& other) const {
- return _string < other._string;
- }
-
- //
- // FOR TESTING ONLY - useful to be able to directly mock a connection std::string without
- // including the entire client library.
- //
-
- static ConnectionString mock( const HostAndPort& server ) {
- ConnectionString connStr;
- connStr._servers.push_back( server );
- connStr._string = server.toString();
- return connStr;
- }
-
- private:
-
- void _fillServers( std::string s );
- void _finishInit();
-
- ConnectionType _type;
- std::vector<HostAndPort> _servers;
- std::string _string;
- std::string _setName;
-
- static boost::mutex _connectHookMutex;
- static ConnectionHook* _connectHook;
- };
-
- /**
* controls how much a clients cares about writes
* default is NORMAL
*/