0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

centralize connection url parsing SERVER-1319

This commit is contained in:
Eliot Horowitz 2010-07-30 11:20:33 -04:00
parent 2b8634ff6e
commit 12aa34d0fa
5 changed files with 56 additions and 94 deletions

View File

@ -74,36 +74,12 @@ namespace mongo {
return c;
}
int numCommas = DBClientBase::countCommas( host );
if( numCommas == 0 ) {
DBClientConnection *cc = new DBClientConnection(true);
log(2) << "creating new connection for pool to:" << host << endl;
string errmsg;
if ( !cc->connect(host.c_str(), errmsg) ) {
delete cc;
uassert( 11002 , (string)"dbconnectionpool: connect failed " + host , false);
return 0;
}
c = cc;
}
else if ( numCommas == 1 ) {
DBClientPaired *p = new DBClientPaired();
if( !p->connect(host) ) {
delete p;
uassert( 11003 , (string)"dbconnectionpool: connect failed [2] " + host , false);
return 0;
}
c = p;
}
else if ( numCommas == 2 ) {
c = new SyncClusterConnection( host );
}
else {
uassert( 13071 , (string)"invalid hostname [" + host + "]" , 0 );
c = 0; // prevents compiler warning
}
string errmsg;
ConnectionString cs = ConnectionString::parse( host , errmsg );
uassert( 13071 , (string)"invalid hostname [" + host + "]" + errmsg , cs.isValid() );
c = cs.connect( errmsg );
uassert( 11002 , (string)"dbconnectionpool: connect failed " + host + " : " + errmsg , c );
return _finishCreate( host , c );
}

View File

@ -43,29 +43,49 @@ namespace mongo {
return c;
}
case SET: {
case PAIR: {
DBClientPaired *p = new DBClientPaired();
if( !p->connect( _servers[0] , _servers[1] ) ){
delete p;
errmsg = "connect failed";
errmsg = "connect failed to pair";
return 0;
}
return p;
}
case SYNC:
case SYNC: {
// TODO , don't copy
list<HostAndPort> l;
for ( unsigned i=0; i<_servers.size(); i++ )
l.push_back( _servers[i] );
return new SyncClusterConnection( l );
}
case INVALID:
throw UserException( 13421 , "trying to connect to invalid ConnectionString" );
break;
}
assert( 0 );
return 0;
}
ConnectionString ConnectionString::parse( const string& host , string& errmsg ){
int numCommas = DBClientBase::countCommas( host );
if( numCommas == 0 )
return ConnectionString( HostAndPort( host ) );
if ( numCommas == 1 )
return ConnectionString( PAIR , host );
if ( numCommas == 2 )
return ConnectionString( SYNC , host );
errmsg = (string)"invalid hostname [" + host + "]";
return ConnectionString(); // INVALID
}
Query& Query::where(const string &jscode, BSONObj scope) {
/* use where() before sort() and hint() and explain(), else this will assert. */
assert( ! isComplex() );

View File

@ -98,7 +98,7 @@ namespace mongo {
class ConnectionString {
public:
enum ConnectionType { MASTER , SET , SYNC };
enum ConnectionType { INVALID , MASTER , PAIR , SYNC };
ConnectionString( const HostAndPort& server ){
_type = MASTER;
@ -137,6 +137,8 @@ namespace mongo {
}
_finishInit();
}
bool isValid() const { return _type != INVALID; }
string toString() const {
return _string;
@ -148,7 +150,13 @@ namespace mongo {
DBClientBase* connect( string& errmsg ) const;
static ConnectionString parse( const string& url , string& errmsg );
private:
ConnectionString(){
_type = INVALID;
}
void _fillServers( string s ){
string::size_type idx;

View File

@ -162,36 +162,19 @@ namespace mongo {
if ( argc > 0 )
host = c.toString( argv[0] );
int numCommas = DBClientBase::countCommas( host );
shared_ptr< DBClientWithCommands > conn;
string errmsg;
if ( numCommas == 0 ){
DBClientConnection * c = new DBClientConnection( true );
conn.reset( c );
if ( ! c->connect( host , errmsg ) ){
JS_ReportError( cx , ((string)"couldn't connect: " + errmsg).c_str() );
return JS_FALSE;
}
ScriptEngine::runConnectCallback( *c );
}
else if ( numCommas == 1 ){ // paired
DBClientPaired * c = new DBClientPaired();
conn.reset( c );
if ( ! c->connect( host ) ){
JS_ReportError( cx , "couldn't connect to pair" );
return JS_FALSE;
}
}
else if ( numCommas == 2 ){
conn.reset( new SyncClusterConnection( host ) );
}
else {
JS_ReportError( cx , "1 (paired) or 2(quorum) commas are allowed" );
ConnectionString cs = ConnectionString::parse( host , errmsg );
if ( ! cs.isValid() ){
JS_ReportError( cx , errmsg.c_str() );
return JS_FALSE;
}
shared_ptr< DBClientWithCommands > conn( cs.connect( errmsg ) );
if ( ! conn ){
JS_ReportError( cx , errmsg.c_str() );
return JS_FALSE;
}
assert( JS_SetPrivate( cx , obj , (void*)( new shared_ptr< DBClientWithCommands >( conn ) ) ) );
jsval host_val = c.toval( host.c_str() );

View File

@ -153,40 +153,15 @@ namespace mongo {
strcpy( host , "127.0.0.1" );
}
DBClientWithCommands * conn = 0;
int commas = 0;
for ( int i=0; i<255; i++ ){
if ( host[i] == ',' )
commas++;
else if ( host[i] == 0 )
break;
}
string errmsg;
ConnectionString cs = ConnectionString::parse( host , errmsg );
if ( ! cs.isValid() )
return v8::ThrowException( v8::String::New( errmsg.c_str() ) );
if ( commas == 0 ){
DBClientConnection * c = new DBClientConnection( true );
string errmsg;
if ( ! c->connect( host , errmsg ) ){
delete c;
string x = "couldn't connect: ";
x += errmsg;
return v8::ThrowException( v8::String::New( x.c_str() ) );
}
conn = c;
}
else if ( commas == 1 ){
DBClientPaired * c = new DBClientPaired();
if ( ! c->connect( host ) ){
delete c;
return v8::ThrowException( v8::String::New( "couldn't connect to pair" ) );
}
conn = c;
}
else if ( commas == 2 ){
conn = new SyncClusterConnection( host );
}
else {
return v8::ThrowException( v8::String::New( "too many commas" ) );
}
DBClientWithCommands * conn = cs.connect( errmsg );
if ( ! conn )
return v8::ThrowException( v8::String::New( errmsg.c_str() ) );
Persistent<v8::Object> self = Persistent<v8::Object>::New( args.Holder() );
self.MakeWeak( conn , destroyConnection );