diff options
author | Eliot Horowitz <eliot@10gen.com> | 2014-07-24 15:19:57 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2014-07-28 08:43:19 -0400 |
commit | e28e721d7e9a4f08126f4e1b983bbf73cbe7aec7 (patch) | |
tree | 0a72f946a757c2252bd7738d7ccae48cef4e1348 /src/mongo/client/dbclient.cpp | |
parent | 0777e69dbc961d16e7d74daeb6b49fc82718c313 (diff) | |
download | mongo-e28e721d7e9a4f08126f4e1b983bbf73cbe7aec7.tar.gz |
SERVER-14378: Cloner shouldn't use system.namespaces
Diffstat (limited to 'src/mongo/client/dbclient.cpp')
-rw-r--r-- | src/mongo/client/dbclient.cpp | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/src/mongo/client/dbclient.cpp b/src/mongo/client/dbclient.cpp index d03b7bf4d67..b55ca0cd829 100644 --- a/src/mongo/client/dbclient.cpp +++ b/src/mongo/client/dbclient.cpp @@ -864,17 +864,60 @@ namespace mongo { } list<string> DBClientWithCommands::getCollectionNames( const string& db ) { + list<BSONObj> infos = getCollectionInfos( db ); list<string> names; + for ( list<BSONObj>::iterator it = infos.begin(); it != infos.end(); ++it ) { + names.push_back( db + "." + (*it)["name"].valuestr() ); + } + return names; + } + + list<BSONObj> DBClientWithCommands::getCollectionInfos( const string& db ) { + list<BSONObj> infos; + + // first we're going to try the command + // it was only added in 2.8, so if we're talking to an older server + // we'll fail back to querying system.namespaces + + { + BSONObj res; + if ( runCommand( db, BSON( "listCollections" << 1), res ) ) { + BSONObj collections = res["collections"].Obj(); + BSONObjIterator it( collections ); + while ( it.more() ) { + BSONElement e = it.next(); + infos.push_back( e.Obj().getOwned() ); + } + return infos; + } + + // command failed + + int code = res["code"].numberInt(); + string errmsg = res["errmsg"].valuestrsafe(); + if ( code == 59 || errmsg.find( "no such cmd" ) != string::npos ) { + // old version of server, ok, fall through to old code + } + else { + uasserted( 18530, str::stream() << "listCollections failed: " << res ); + } + + } string ns = db + ".system.namespaces"; auto_ptr<DBClientCursor> c = query( ns.c_str() , BSONObj() ); while ( c->more() ) { - string name = c->nextSafe()["name"].valuestr(); - if ( name.find( "$" ) != string::npos ) + BSONObj obj = c->nextSafe(); + string ns = obj["name"].valuestr(); + if ( ns.find( "$" ) != string::npos ) continue; - names.push_back( name ); + BSONObjBuilder b; + b.append( "name", ns.substr( db.size() + 1 ) ); + b.appendElementsUnique( obj ); + infos.push_back( b.obj() ); } - return names; + + return infos; } bool DBClientWithCommands::exists( const string& ns ) { |