mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 01:21:03 +01:00
Merge branch 'master' of git@github.com:mongodb/mongo
This commit is contained in:
commit
90aa429845
@ -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 )
|
||||
|
@ -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;
|
||||
|
||||
|
76
dbtests/client.cpp
Normal file
76
dbtests/client.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
// client.cpp
|
||||
|
||||
#include "../client/dbclient.h"
|
||||
#include "dbtests.h"
|
||||
|
||||
namespace ClientTests {
|
||||
|
||||
class Base {
|
||||
public:
|
||||
|
||||
Base( string coll ){
|
||||
_ns = (string)"test." + coll;
|
||||
}
|
||||
|
||||
virtual ~Base(){
|
||||
db.dropCollection( _ns );
|
||||
}
|
||||
|
||||
const char * ns(){ return _ns.c_str(); }
|
||||
|
||||
string _ns;
|
||||
DBDirectClient db;
|
||||
};
|
||||
|
||||
|
||||
class DropIndex : public Base {
|
||||
public:
|
||||
DropIndex() : Base( "dropindex" ){}
|
||||
void run(){
|
||||
|
||||
db.insert( ns() , BSON( "x" << 2 ) );
|
||||
ASSERT_EQUALS( 1 , db.getIndexes( ns() )->itcount() );
|
||||
|
||||
db.ensureIndex( ns() , BSON( "x" << 1 ) );
|
||||
ASSERT_EQUALS( 2 , db.getIndexes( ns() )->itcount() );
|
||||
|
||||
db.dropIndex( ns() , BSON( "x" << 1 ) );
|
||||
ASSERT_EQUALS( 1 , db.getIndexes( ns() )->itcount() );
|
||||
|
||||
db.ensureIndex( ns() , BSON( "x" << 1 ) );
|
||||
ASSERT_EQUALS( 2 , db.getIndexes( ns() )->itcount() );
|
||||
|
||||
db.dropIndexes( ns() );
|
||||
ASSERT_EQUALS( 1 , db.getIndexes( ns() )->itcount() );
|
||||
}
|
||||
};
|
||||
|
||||
class ReIndex : public Base {
|
||||
public:
|
||||
ReIndex() : Base( "reindex" ){}
|
||||
void run(){
|
||||
|
||||
db.insert( ns() , BSON( "x" << 2 ) );
|
||||
ASSERT_EQUALS( 1 , db.getIndexes( ns() )->itcount() );
|
||||
|
||||
db.ensureIndex( ns() , BSON( "x" << 1 ) );
|
||||
ASSERT_EQUALS( 2 , db.getIndexes( ns() )->itcount() );
|
||||
|
||||
db.reIndex( ns() );
|
||||
ASSERT_EQUALS( 2 , db.getIndexes( ns() )->itcount() );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class All : public Suite {
|
||||
public:
|
||||
All() : Suite( "client" ){
|
||||
}
|
||||
|
||||
void setupTests(){
|
||||
add<DropIndex>();
|
||||
add<ReIndex>();
|
||||
}
|
||||
|
||||
} all;
|
||||
}
|
Loading…
Reference in New Issue
Block a user