summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-09-24 17:41:09 -0400
committerEliot Horowitz <eliot@10gen.com>2009-09-24 17:41:09 -0400
commit8b91377b098c378fc33bff10e81c2f0daa95feb1 (patch)
tree807204da146b91aaf1206adbfd281c56597cb6ce /client
parent1981f140a85848869ea6ae3d0de49be86ed51555 (diff)
downloadmongo-8b91377b098c378fc33bff10e81c2f0daa95feb1.tar.gz
dropIndex, dropIndexes, getIndexes, reIndex for c++ driver SERVER-314
Diffstat (limited to 'client')
-rw-r--r--client/dbclient.cpp85
-rw-r--r--client/dbclient.h26
2 files changed, 93 insertions, 18 deletions
diff --git a/client/dbclient.cpp b/client/dbclient.cpp
index 5344b34c930..d5e1cb2cec9 100644
--- a/client/dbclient.cpp
+++ b/client/dbclient.cpp
@@ -554,6 +554,70 @@ namespace mongo {
say( toSend );
}
+ auto_ptr<DBClientCursor> DBClientBase::getIndexes( const string &ns ){
+ return query( Namespace( ns.c_str() ).getSisterNS( "system.indexes" ).c_str() , BSON( "ns" << ns ) );
+ }
+
+ void DBClientBase::dropIndex( const string& ns , BSONObj keys ){
+ dropIndex( ns , genIndexName( keys ) );
+ }
+
+
+ void DBClientBase::dropIndex( const string& ns , const string& indexName ){
+ BSONObj info;
+ if ( ! runCommand( nsToClient( ns.c_str() ) ,
+ BSON( "deleteIndexes" << NamespaceString( ns ).coll << "index" << indexName ) ,
+ info ) ){
+ log() << "dropIndex failed: " << info << endl;
+ uassert( "dropIndex failed" , 0 );
+ }
+ resetIndexCache();
+ }
+
+ void DBClientBase::dropIndexes( const string& ns ){
+ BSONObj info;
+ uassert( "dropIndexes failed" , runCommand( nsToClient( ns.c_str() ) ,
+ BSON( "deleteIndexes" << NamespaceString( ns ).coll << "index" << "*") ,
+ info ) );
+ resetIndexCache();
+ }
+
+ void DBClientBase::reIndex( const string& ns ){
+ list<BSONObj> all;
+ auto_ptr<DBClientCursor> i = getIndexes( ns );
+ while ( i->more() ){
+ all.push_back( i->next().getOwned() );
+ }
+
+ dropIndexes( ns );
+
+ for ( list<BSONObj>::iterator i=all.begin(); i!=all.end(); i++ ){
+ BSONObj o = *i;
+ insert( Namespace( ns.c_str() ).getSisterNS( "system.indexes" ).c_str() , o );
+ }
+
+ }
+
+
+ string DBClientBase::genIndexName( const BSONObj& keys ){
+ stringstream ss;
+
+ bool first = 1;
+ for ( BSONObjIterator i(keys); i.more(); ) {
+ BSONElement f = i.next();
+
+ if ( first )
+ first = 0;
+ else
+ ss << "_";
+
+ ss << f.fieldName() << "_";
+ if( f.isNumber() )
+ ss << f.numberInt();
+ }
+ return ss.str();
+ }
+
bool DBClientBase::ensureIndex( const string &ns , BSONObj keys , bool unique, const string & name ) {
BSONObjBuilder toSave;
toSave.append( "ns" , ns );
@@ -567,24 +631,9 @@ namespace mongo {
cacheKey += name;
}
else {
- stringstream ss;
-
- bool first = 1;
- for ( BSONObjIterator i(keys); i.more(); ) {
- BSONElement f = i.next();
-
- if ( first )
- first = 0;
- else
- ss << "_";
-
- ss << f.fieldName() << "_";
- if( f.isNumber() )
- ss << f.numberInt();
- }
-
- toSave.append( "name" , ss.str() );
- cacheKey += ss.str();
+ string nn = genIndexName( keys );
+ toSave.append( "name" , nn );
+ cacheKey += nn;
}
if ( unique )
diff --git a/client/dbclient.h b/client/dbclient.h
index baabe8bbc20..e590e13674a 100644
--- a/client/dbclient.h
+++ b/client/dbclient.h
@@ -196,6 +196,18 @@ namespace mongo {
return o;
}
+ /**
+ iterate the rest of the cursor and return the number if items
+ */
+ int itcount(){
+ int c = 0;
+ while ( more() ){
+ next();
+ c++;
+ }
+ return c;
+ }
+
/** cursor no longer valid -- use with tailable cursors.
note you should only rely on this once more() returns false;
'dead' may be preset yet some data still queued and locally
@@ -599,6 +611,20 @@ namespace mongo {
clears the index cache, so the subsequent call to ensureIndex for any index will go to the server
*/
virtual void resetIndexCache();
+
+ virtual auto_ptr<DBClientCursor> getIndexes( const string &ns );
+
+ virtual void dropIndex( const string& ns , BSONObj keys );
+ virtual void dropIndex( const string& ns , const string& indexName );
+
+ /**
+ drops all indexes for the collection
+ */
+ virtual void dropIndexes( const string& ns );
+
+ virtual void reIndex( const string& ns );
+
+ string genIndexName( const BSONObj& keys );
virtual string getServerAddress() const = 0;