From 55be029b9de07d8f0e27834768ec9a294beabbda Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Tue, 29 Sep 2009 10:15:06 -0400 Subject: [PATCH] reIndex command - in prep for SERVER-292 does all indexes, including _id --- db/dbcommands.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp index 38da128404f..fbc278cf4c9 100644 --- a/db/dbcommands.cpp +++ b/db/dbcommands.cpp @@ -782,6 +782,61 @@ namespace mongo { } } cmdDeleteIndexes; + class CmdReIndex : public Command { + public: + virtual bool logTheOp() { + return true; + } + virtual bool slaveOk() { + return false; + } + virtual void help( stringstream& help ) const { + help << "re-index a collection"; + } + CmdReIndex() : Command("reIndex") { } + bool run(const char *ns, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) { + static DBDirectClient db; + + BSONElement e = jsobj.findElement(name.c_str()); + string toDeleteNs = database->name + '.' + e.valuestr(); + NamespaceDetails *d = nsdetails(toDeleteNs.c_str()); + log() << "CMD: reIndex " << toDeleteNs << endl; + + if ( ! d ){ + errmsg = "ns not found"; + return false; + } + + list all; + auto_ptr i = db.getIndexes( toDeleteNs ); + BSONObjBuilder b; + while ( i->more() ){ + BSONObj o = i->next().getOwned(); + b.append( BSONObjBuilder::numStr( all.size() ) , o ); + all.push_back( o ); + } + + + bool ok = deleteIndexes( d, toDeleteNs.c_str(), "*" , errmsg, result, true ); + if ( ! ok ){ + errmsg = "deleteIndexes failed"; + return false; + } + + for ( list::iterator i=all.begin(); i!=all.end(); i++ ){ + BSONObj o = *i; + db.insert( Namespace( toDeleteNs.c_str() ).getSisterNS( "system.indexes" ).c_str() , o ); + } + + result.append( "ok" , 1 ); + result.append( "nIndexes" , (int)all.size() ); + result.appendArray( "indexes" , b.obj() ); + return true; + } + } cmdReIndex; + + + class CmdListDatabases : public Command { public: virtual bool logTheOp() {