From ee6ef61ca43670127cd8c5d3bc56cc7a9f83221b Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 16 Apr 2009 11:27:10 -0400 Subject: [PATCH] clear index cache in c++ client on dropping collection / database --- client/dbclient.h | 18 ++++++++++++++++-- dbtests/querytests.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/client/dbclient.h b/client/dbclient.h index 75e21d1efd0..cb6825a9f9a 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -352,12 +352,12 @@ namespace mongo { bool resetError() { return simpleCommand("admin", 0, "reseterror"); } /** Erase / drop an entire database */ - bool dropDatabase(const string &dbname, BSONObj *info = 0) { + virtual bool dropDatabase(const string &dbname, BSONObj *info = 0) { return simpleCommand(dbname, info, "dropDatabase"); } /** Delete the specified collection. */ - bool dropCollection( const string &ns ){ + virtual bool dropCollection( const string &ns ){ string db = nsGetDB( ns ); string coll = nsGetCollection( ns ); assert( coll.size() ); @@ -555,6 +555,20 @@ namespace mongo { virtual void resetIndexCache(); virtual string getServerAddress() const = 0; + + /** Erase / drop an entire database */ + virtual bool dropDatabase(const string &dbname, BSONObj *info = 0) { + bool ret = DBClientWithCommands::dropDatabase( dbname, info ); + resetIndexCache(); + return ret; + } + + /** Delete the specified collection. */ + virtual bool dropCollection( const string &ns ){ + bool ret = DBClientWithCommands::dropCollection( ns ); + resetIndexCache(); + return ret; + } private: set _seenIndexes; diff --git a/dbtests/querytests.cpp b/dbtests/querytests.cpp index 6702e5595cb..a6046e2ab89 100644 --- a/dbtests/querytests.cpp +++ b/dbtests/querytests.cpp @@ -389,6 +389,32 @@ namespace QueryTests { ASSERT( client().findOne( ns, fromjson( "{'a.b':{$ne:1}}" ) ).isEmpty() ); } }; + + class AutoResetIndexCache : public ClientBase { + public: + ~AutoResetIndexCache() { + client().dropCollection( "querytests.AutoResetIndexCache" ); + } + static const char *ns() { return "querytests.AutoResetIndexCache"; } + static const char *idxNs() { return "querytests.system.indexes"; } + void index() const { ASSERT( !client().findOne( idxNs(), BSONObj() ).isEmpty() ); } + void noIndex() const { ASSERT( client().findOne( idxNs(), BSONObj() ).isEmpty() ); } + void checkIndex() { + client().ensureIndex( ns(), BSON( "a" << 1 ) ); + index(); + } + void run() { + client().dropDatabase( "querytests" ); + noIndex(); + checkIndex(); + client().dropCollection( ns() ); + noIndex(); + checkIndex(); + client().dropDatabase( "querytests" ); + noIndex(); + checkIndex(); + } + }; class All : public UnitTest::Suite { public: @@ -411,6 +437,7 @@ namespace QueryTests { add< EmptyFieldSpec >(); add< MultiNe >(); add< EmbeddedNe >(); + add< AutoResetIndexCache >(); } };