diff options
author | Andy Schwerin <schwerin@mongodb.com> | 2014-11-04 16:17:48 -0500 |
---|---|---|
committer | Andy Schwerin <schwerin@mongodb.com> | 2014-11-05 15:38:53 -0500 |
commit | d324cff2fccfd7f2f1397093e01a1eb4af9a3e52 (patch) | |
tree | 6b789f95a95ba33d6889c9ba898b77c03508ea8c /src/mongo | |
parent | ca0d6d5d907ebe39437b4032ccb4795946573183 (diff) | |
download | mongo-d324cff2fccfd7f2f1397093e01a1eb4af9a3e52.tar.gz |
SERVER-15882 Get rid of ensureIndex in dbtests.
Diffstat (limited to 'src/mongo')
23 files changed, 229 insertions, 191 deletions
diff --git a/src/mongo/client/dbclientinterface.h b/src/mongo/client/dbclientinterface.h index 28f8810df54..86746888b06 100644 --- a/src/mongo/client/dbclientinterface.h +++ b/src/mongo/client/dbclientinterface.h @@ -986,7 +986,7 @@ namespace mongo { virtual void reIndex( const std::string& ns ); - std::string genIndexName( const BSONObj& keys ); + static std::string genIndexName( const BSONObj& keys ); /** Erase / drop an entire database */ virtual bool dropDatabase(const std::string &dbname, BSONObj *info = 0) { diff --git a/src/mongo/dbtests/clienttests.cpp b/src/mongo/dbtests/clienttests.cpp index f3cd6792b1a..29e5b95d47f 100644 --- a/src/mongo/dbtests/clienttests.cpp +++ b/src/mongo/dbtests/clienttests.cpp @@ -71,13 +71,13 @@ namespace ClientTests { db.insert( ns() , BSON( "x" << 2 ) ); ASSERT_EQUALS( 1u , db.getIndexSpecs(ns()).size() ); - db.ensureIndex( ns() , BSON( "x" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &txn, ns(), BSON( "x" << 1 ) )); ASSERT_EQUALS( 2u , db.getIndexSpecs(ns()).size() ); db.dropIndex( ns() , BSON( "x" << 1 ) ); ASSERT_EQUALS( 1u , db.getIndexSpecs(ns()).size() ); - db.ensureIndex( ns() , BSON( "x" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &txn, ns(), BSON( "x" << 1 ) )); ASSERT_EQUALS( 2u , db.getIndexSpecs(ns()).size() ); db.dropIndexes( ns() ); @@ -85,46 +85,6 @@ namespace ClientTests { } }; - class ReIndex : public Base { - public: - ReIndex() : Base( "reindex" ) {} - void run() { - OperationContextImpl txn; - DBDirectClient db(&txn); - - db.insert( ns() , BSON( "x" << 2 ) ); - ASSERT_EQUALS( 1u , db.getIndexSpecs(ns()).size() ); - - db.ensureIndex( ns() , BSON( "x" << 1 ) ); - ASSERT_EQUALS( 2u , db.getIndexSpecs(ns()).size() ); - - db.reIndex( ns() ); - ASSERT_EQUALS( 2u , db.getIndexSpecs(ns()).size() ); - } - - }; - - class ReIndex2 : public Base { - public: - ReIndex2() : Base( "reindex2" ) {} - void run() { - OperationContextImpl txn; - DBDirectClient db(&txn); - - db.insert( ns() , BSON( "x" << 2 ) ); - ASSERT_EQUALS( 1u , db.getIndexSpecs(ns()).size() ); - - db.ensureIndex( ns() , BSON( "x" << 1 ) ); - ASSERT_EQUALS( 2u , db.getIndexSpecs(ns()).size() ); - - BSONObj out; - ASSERT( db.runCommand( "test" , BSON( "reIndex" << "reindex2" ) , out ) ); - ASSERT_EQUALS( 2 , out["nIndexes"].number() ); - ASSERT_EQUALS( 2u , db.getIndexSpecs(ns()).size() ); - } - - }; - /** * Check that nIndexes is incremented correctly when an index builds (and that it is not * incremented when an index fails to build), system.indexes has an entry added (or not), and @@ -150,12 +110,13 @@ namespace ClientTests { // _id index ASSERT_EQUALS(1U, db.getIndexSpecs(ns()).size()); - db.ensureIndex(ns(), BSON("y" << 1), true); + ASSERT_EQUALS(ErrorCodes::DuplicateKey, + dbtests::createIndex(&txn, ns(), BSON("y" << 1), true)); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(&txn)); ASSERT_EQUALS(1U, db.getIndexSpecs(ns()).size()); - db.ensureIndex(ns(), BSON("x" << 1), true); + ASSERT_OK(dbtests::createIndex(&txn, ns(), BSON("x" << 1), true)); ASSERT_EQUALS(2, indexCatalog->numIndexesReady(&txn)); ASSERT_EQUALS(2U, db.getIndexSpecs(ns()).size()); @@ -174,7 +135,7 @@ namespace ClientTests { db.insert(ns(), BSON("a" << i << "b" << longs)); } - db.ensureIndex( ns(), BSON( "a" << 1 << "b" << 1 ) ); + ASSERT_OK(dbtests::createIndex(&txn, ns(), BSON( "a" << 1 << "b" << 1 ) )); auto_ptr< DBClientCursor > c = db.query( ns(), Query().sort( BSON( "a" << 1 << "b" << 1 ) ) ); ASSERT_EQUALS( 1111, c->itcount() ); @@ -263,8 +224,6 @@ namespace ClientTests { void setupTests() { add<DropIndex>(); - add<ReIndex>(); - add<ReIndex2>(); add<BuildIndex>(); add<CS_10>(); add<PushBack>(); diff --git a/src/mongo/dbtests/commandtests.cpp b/src/mongo/dbtests/commandtests.cpp index faba4d41b9e..c653925664f 100644 --- a/src/mongo/dbtests/commandtests.cpp +++ b/src/mongo/dbtests/commandtests.cpp @@ -41,7 +41,7 @@ namespace CommandTests { struct Base { Base() : db(&_txn) { db.dropCollection(ns()); - db.ensureIndex(ns(), BSON( "files_id" << 1 << "n" << 1 )); + ASSERT_OK(dbtests::createIndex(&_txn, ns(),BSON( "files_id" << 1 << "n" << 1 ))); } const char* ns() { return "test.fs.chunks"; } diff --git a/src/mongo/dbtests/config_server_fixture.cpp b/src/mongo/dbtests/config_server_fixture.cpp index a98bb3dba10..cdbdddec227 100644 --- a/src/mongo/dbtests/config_server_fixture.cpp +++ b/src/mongo/dbtests/config_server_fixture.cpp @@ -32,6 +32,7 @@ #include <list> +#include "mongo/dbtests/dbtests.h" #include "mongo/s/config.h" #include "mongo/s/distlock.h" #include "mongo/s/type_changelog.h" @@ -62,10 +63,10 @@ namespace mongo { client().dropCollection("config.test"); // Create an index over the chunks, to allow correct diffing - client().ensureIndex( ChunkType::ConfigNS, // br - BSON( ChunkType::ns() << 1 << // br - ChunkType::DEPRECATED_lastmod() << 1 ) ); - + ASSERT_OK(dbtests::createIndex(&_client._txn, + ChunkType::ConfigNS, + BSON( ChunkType::ns() << 1 << + ChunkType::DEPRECATED_lastmod() << 1 ))); configServer.init(configSvr().toString()); } diff --git a/src/mongo/dbtests/dbtests.cpp b/src/mongo/dbtests/dbtests.cpp index 5ec745a8451..dc92aee5375 100644 --- a/src/mongo/dbtests/dbtests.cpp +++ b/src/mongo/dbtests/dbtests.cpp @@ -29,34 +29,81 @@ * then also delete it in the license file. */ -#include "mongo/pch.h" +#include "mongo/platform/basic.h" + +#include "mongo/dbtests/dbtests.h" #include "mongo/base/initializer.h" -#include "mongo/db/commands.h" #include "mongo/db/auth/authorization_manager.h" #include "mongo/db/auth/authorization_manager_global.h" #include "mongo/db/auth/authz_manager_external_state_mock.h" +#include "mongo/db/catalog/index_create.h" +#include "mongo/db/commands.h" #include "mongo/db/global_environment_d.h" #include "mongo/db/global_environment_experiment.h" #include "mongo/db/repl/repl_coordinator_global.h" #include "mongo/db/repl/repl_coordinator_mock.h" -#include "mongo/dbtests/dbtests.h" #include "mongo/dbtests/framework.h" -#include "mongo/util/exception_filter_win32.h" #include "mongo/util/gcov.h" +#include "mongo/util/quick_exit.h" +#include "mongo/util/signal_handlers_synchronous.h" #include "mongo/util/startup_test.h" #include "mongo/util/text.h" -#include "mongo/util/quick_exit.h" namespace mongo { +namespace dbtests { // This specifies default dbpath for our testing framework const std::string default_test_dbpath = "/tmp/unittest"; + + Status createIndex(OperationContext* txn, + const StringData &ns, + const BSONObj& keys, + bool unique) { + BSONObjBuilder specBuilder; + specBuilder << + "name" << DBClientBase::genIndexName(keys) << + "ns" << ns << + "key" << keys; + if (unique) { + specBuilder << "unique" << true; + } + return createIndexFromSpec(txn, ns, specBuilder.done()); + } + + Status createIndexFromSpec(OperationContext* txn, const StringData& ns, const BSONObj& spec) { + AutoGetOrCreateDb autoDb(txn, nsToDatabaseSubstring(ns), MODE_X); + Collection* coll; + { + WriteUnitOfWork wunit(txn); + coll = autoDb.getDb()->getOrCreateCollection(txn, ns); + invariant(coll); + wunit.commit(); + } + MultiIndexBlock indexer(txn, coll); + Status status = indexer.init(spec); + if (status == ErrorCodes::IndexAlreadyExists) { + return Status::OK(); + } + if (!status.isOK()) { + return status; + } + status = indexer.insertAllDocumentsInCollection(); + if (!status.isOK()) { + return status; + } + WriteUnitOfWork wunit(txn); + indexer.commit(); + wunit.commit(); + return Status::OK(); + } + +} // namespace dbtests } // namespace mongo int dbtestsMain( int argc, char** argv, char** envp ) { static StaticObserver StaticObserver; - setWindowsUnhandledExceptionFilter(); + ::mongo::setupSynchronousSignalHandlers(); setGlobalEnvironment(new GlobalEnvironmentMongoD()); repl::ReplSettings replSettings; replSettings.oplogSize = 10 * 1024 * 1024; diff --git a/src/mongo/dbtests/dbtests.h b/src/mongo/dbtests/dbtests.h index bd62dc15b99..c3dff859a0f 100644 --- a/src/mongo/dbtests/dbtests.h +++ b/src/mongo/dbtests/dbtests.h @@ -39,6 +39,26 @@ using namespace mongo::unittest; using boost::shared_ptr; namespace mongo { + class BSONObj; + class OperationContext; + class Status; + class StringData; + +namespace dbtests { // This specifies default dbpath for our testing framework extern const std::string default_test_dbpath; -} + + /** + * Creates an index if it does not already exist. + */ + Status createIndex(OperationContext* txn, + const StringData &ns, + const BSONObj& keys, + bool unique = false); + + /** + * Creates an index from a BSON spec, if it does not already exist. + */ + Status createIndexFromSpec(OperationContext* txn, const StringData& ns, const BSONObj& spec); +} // namespace dbtests +} // namespace mongo diff --git a/src/mongo/dbtests/executor_registry.cpp b/src/mongo/dbtests/executor_registry.cpp index 5efaf842934..50319bbaf8a 100644 --- a/src/mongo/dbtests/executor_registry.cpp +++ b/src/mongo/dbtests/executor_registry.cpp @@ -214,7 +214,7 @@ namespace ExecutorRegistry { auto_ptr<PlanExecutor> run(getCollscan()); BSONObj obj; - _client.ensureIndex(ns(), BSON("foo" << 1)); + ASSERT_OK(dbtests::createIndex(&_opCtx, ns(), BSON("foo" << 1))); // Read some of it. for (int i = 0; i < 10; ++i) { @@ -245,7 +245,7 @@ namespace ExecutorRegistry { auto_ptr<PlanExecutor> run(getCollscan()); BSONObj obj; - _client.ensureIndex(ns(), BSON("foo" << 1)); + ASSERT_OK(dbtests::createIndex(&_opCtx, ns(), BSON("foo" << 1))); // Read some of it. for (int i = 0; i < 10; ++i) { diff --git a/src/mongo/dbtests/framework_options.cpp b/src/mongo/dbtests/framework_options.cpp index 8e44ca87a47..b410fd46f62 100644 --- a/src/mongo/dbtests/framework_options.cpp +++ b/src/mongo/dbtests/framework_options.cpp @@ -54,7 +54,7 @@ namespace mongo { options->addOptionChaining("dbpath", "dbpath", moe::String, "db data path for this test run. NOTE: the contents of this directory will " "be overwritten if it already exists") - .setDefault(moe::Value(default_test_dbpath)); + .setDefault(moe::Value(dbtests::default_test_dbpath)); options->addOptionChaining("debug", "debug", moe::Switch, "run tests with verbose output"); diff --git a/src/mongo/dbtests/indexupdatetests.cpp b/src/mongo/dbtests/indexupdatetests.cpp index 1515df90c65..1b43af2a093 100644 --- a/src/mongo/dbtests/indexupdatetests.cpp +++ b/src/mongo/dbtests/indexupdatetests.cpp @@ -93,6 +93,8 @@ namespace IndexUpdateTests { } #endif + Status createIndex(const std::string& dbname, const BSONObj& indexSpec); + bool buildIndexInterrupted(const BSONObj& key, bool allowInterruption) { try { MultiIndexBlock indexer(&_txn, collection()); @@ -600,31 +602,6 @@ namespace IndexUpdateTests { } }; - /** DBDirectClient::ensureIndex() is not interrupted. */ - class DirectClientEnsureIndexInterruptDisallowed : public IndexBuildBase { - public: - void run() { - // Insert some documents. - int32_t nDocs = 1000; - for( int32_t i = 0; i < nDocs; ++i ) { - _client.insert( _ns, BSON( "a" << i ) ); - } - // Start with just _id - ASSERT_EQUALS( 1U, _client.getIndexSpecs(_ns).size()); - // Initialize curop. - _txn.getCurOp()->reset(); - // Request an interrupt. killAll() rather than kill() is required because the direct - // client will build the index using a new opid. - getGlobalEnvironment()->setKillAllOperations(); - // The call is not interrupted. - _client.ensureIndex( _ns, BSON( "a" << 1 ) ); - // only want to interrupt the index build - getGlobalEnvironment()->unsetKillAllOperations(); - // The new index is listed in getIndexSpecs because the index build completed. - ASSERT_EQUALS( 2U, _client.getIndexSpecs(_ns).size()); - } - }; - /** Helpers::ensureIndex() is not interrupted. */ class HelpersEnsureIndexInterruptDisallowed : public IndexBuildBase { public: @@ -707,54 +684,78 @@ namespace IndexUpdateTests { }; #endif + Status IndexBuildBase::createIndex(const std::string& dbname, const BSONObj& indexSpec) { + MultiIndexBlock indexer(&_txn, collection()); + Status status = indexer.init(indexSpec); + if (status == ErrorCodes::IndexAlreadyExists) { + return Status::OK(); + } + if (!status.isOK()) { + return status; + } + status = indexer.insertAllDocumentsInCollection(); + if (!status.isOK()) { + return status; + } + WriteUnitOfWork wunit(&_txn); + indexer.commit(); + wunit.commit(); + return Status::OK(); + } + /** * Fixture class that has a basic compound index. */ class SimpleCompoundIndex: public IndexBuildBase { public: SimpleCompoundIndex() { - _client.insert("unittests.system.indexes", - BSON("name" << "x" - << "ns" << _ns - << "key" << BSON("x" << 1 << "y" << 1))); + ASSERT_OK( + createIndex( + "unittest", + BSON("name" << "x" + << "ns" << _ns + << "key" << BSON("x" << 1 << "y" << 1)))); } }; class SameSpecDifferentOption: public SimpleCompoundIndex { public: void run() { - _client.insert("unittests.system.indexes", - BSON("name" << "x" - << "ns" << _ns - << "unique" << true - << "key" << BSON("x" << 1 << "y" << 1))); // Cannot have same key spec with an option different from the existing one. - ASSERT_NOT_EQUALS(_client.getLastError(), ""); + ASSERT_EQUALS( + ErrorCodes::IndexOptionsConflict, + createIndex( + "unittest", + BSON("name" << "x" + << "ns" << _ns + << "unique" << true + << "key" << BSON("x" << 1 << "y" << 1)))); } }; class SameSpecSameOptions: public SimpleCompoundIndex { public: void run() { - _client.insert("unittests.system.indexes", - BSON("name" << "x" - << "ns" << _ns - << "key" << BSON("x" << 1 << "y" << 1))); - // It is okay to try to create an index with the exact same specs (will be - // ignored, but should not raise an error). - ASSERT_EQUALS(_client.getLastError(), ""); + ASSERT_OK( + createIndex( + "unittest", + BSON("name" << "x" + << "ns" << _ns + << "key" << BSON("x" << 1 << "y" << 1)))); } }; class DifferentSpecSameName: public SimpleCompoundIndex { public: void run() { - _client.insert("unittests.system.indexes", - BSON("name" << "x" - << "ns" << _ns - << "key" << BSON("y" << 1 << "x" << 1))); // Cannot create a different index with the same name as the existing one. - ASSERT_NOT_EQUALS(_client.getLastError(), ""); + ASSERT_EQUALS( + ErrorCodes::IndexKeySpecsConflict, + createIndex( + "unittest", + BSON("name" << "x" + << "ns" << _ns + << "key" << BSON("y" << 1 << "x" << 1)))); } }; @@ -764,13 +765,15 @@ namespace IndexUpdateTests { class ComplexIndex: public IndexBuildBase { public: ComplexIndex() { - _client.insert("unittests.system.indexes", - BSON("name" << "super" - << "ns" << _ns - << "unique" << 1 - << "sparse" << true - << "expireAfterSeconds" << 3600 - << "key" << BSON("superIdx" << "2d"))); + ASSERT_OK( + createIndex( + "unittests", + BSON("name" << "super" + << "ns" << _ns + << "unique" << 1 + << "sparse" << true + << "expireAfterSeconds" << 3600 + << "key" << BSON("superIdx" << "2d")))); } }; @@ -779,14 +782,15 @@ namespace IndexUpdateTests { void run() { // Exactly the same specs with the existing one, only // specified in a different order than the original. - _client.insert("unittests.system.indexes", - BSON("name" << "super2" - << "ns" << _ns - << "expireAfterSeconds" << 3600 - << "sparse" << true - << "unique" << 1 - << "key" << BSON("superIdx" << "2d"))); - ASSERT_EQUALS(_client.getLastError(), ""); + ASSERT_OK( + createIndex( + "unittests", + BSON("name" << "super2" + << "ns" << _ns + << "expireAfterSeconds" << 3600 + << "sparse" << true + << "unique" << 1 + << "key" << BSON("superIdx" << "2d")))); } }; @@ -796,43 +800,49 @@ namespace IndexUpdateTests { class SameSpecDifferentUnique: public ComplexIndex { public: void run() { - _client.insert("unittests.system.indexes", - BSON("name" << "super2" - << "ns" << _ns - << "unique" << false - << "sparse" << true - << "expireAfterSeconds" << 3600 - << "key" << BSON("superIdx" << "2d"))); - ASSERT_NOT_EQUALS(_client.getLastError(), ""); + ASSERT_EQUALS( + ErrorCodes::IndexOptionsConflict, + createIndex( + "unittest", + BSON("name" << "super2" + << "ns" << _ns + << "unique" << false + << "sparse" << true + << "expireAfterSeconds" << 3600 + << "key" << BSON("superIdx" << "2d")))); } }; class SameSpecDifferentSparse: public ComplexIndex { public: void run() { - _client.insert("unittests.system.indexes", - BSON("name" << "super2" - << "ns" << _ns - << "unique" << 1 - << "sparse" << false - << "background" << true - << "expireAfterSeconds" << 3600 - << "key" << BSON("superIdx" << "2d"))); - ASSERT_NOT_EQUALS(_client.getLastError(), ""); + ASSERT_EQUALS( + ErrorCodes::IndexOptionsConflict, + createIndex( + "unittest", + BSON("name" << "super2" + << "ns" << _ns + << "unique" << 1 + << "sparse" << false + << "background" << true + << "expireAfterSeconds" << 3600 + << "key" << BSON("superIdx" << "2d")))); } }; class SameSpecDifferentTTL: public ComplexIndex { public: void run() { - _client.insert("unittests.system.indexes", - BSON("name" << "super2" - << "ns" << _ns - << "unique" << 1 - << "sparse" << true - << "expireAfterSeconds" << 2400 - << "key" << BSON("superIdx" << "2d"))); - ASSERT_NOT_EQUALS(_client.getLastError(), ""); + ASSERT_EQUALS( + ErrorCodes::IndexOptionsConflict, + createIndex( + "unittest", + BSON("name" << "super2" + << "ns" << _ns + << "unique" << 1 + << "sparse" << true + << "expireAfterSeconds" << 2400 + << "key" << BSON("superIdx" << "2d")))); } }; @@ -874,7 +884,6 @@ namespace IndexUpdateTests { add<InsertBuildIndexInterruptDisallowed>(); add<InsertBuildIdIndexInterrupt>(); add<InsertBuildIdIndexInterruptDisallowed>(); - add<DirectClientEnsureIndexInterruptDisallowed>(); add<HelpersEnsureIndexInterruptDisallowed>(); //add<IndexBuildInProgressTest>(); add<SameSpecDifferentOption>(); diff --git a/src/mongo/dbtests/perftests.cpp b/src/mongo/dbtests/perftests.cpp index e90b11b440e..6a54f3ddc20 100644 --- a/src/mongo/dbtests/perftests.cpp +++ b/src/mongo/dbtests/perftests.cpp @@ -95,6 +95,7 @@ namespace PerfTests { } DBClientBase* client() { return &_client; } + OperationContext* txn() { return &_txn; } private: LastError* _prevError; @@ -1039,7 +1040,7 @@ namespace PerfTests { string name() { return "random-inserts"; } void prep() { client()->insert( ns(), BSONObj() ); - client()->ensureIndex(ns(), BSON("x"<<1)); + ASSERT_OK(dbtests::createIndex(txn(), ns(), BSON("x"<<1))); } void timed() { int x = rand(); @@ -1059,7 +1060,7 @@ namespace PerfTests { virtual string name() { return "random-upserts"; } void prep() { client()->insert( ns(), BSONObj() ); - client()->ensureIndex(ns(), BSON("x"<<1)); + ASSERT_OK(dbtests::createIndex(txn(), ns(), BSON("x"<<1))); } void timed() { int x = rand(); @@ -1087,8 +1088,8 @@ namespace PerfTests { string name() { return T::name() + "-more-indexes"; } void prep() { T::prep(); - this->client()->ensureIndex(this->ns(), BSON("y"<<1)); - this->client()->ensureIndex(this->ns(), BSON("z"<<1)); + ASSERT_OK(dbtests::createIndex(this->txn(), this->ns(), BSON("y"<<1))); + ASSERT_OK(dbtests::createIndex(this->txn(), this->ns(), BSON("z"<<1))); } }; diff --git a/src/mongo/dbtests/plan_ranking.cpp b/src/mongo/dbtests/plan_ranking.cpp index e8b39d6f63b..a8bd8516031 100644 --- a/src/mongo/dbtests/plan_ranking.cpp +++ b/src/mongo/dbtests/plan_ranking.cpp @@ -86,8 +86,7 @@ namespace PlanRankingTests { } void addIndex(const BSONObj& obj) { - Client::WriteContext ctx(&_txn, ns); - _client.ensureIndex(ns, obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns, obj)); } /** diff --git a/src/mongo/dbtests/query_multi_plan_runner.cpp b/src/mongo/dbtests/query_multi_plan_runner.cpp index 32f3dbf0f7d..d8bbd50f8fa 100644 --- a/src/mongo/dbtests/query_multi_plan_runner.cpp +++ b/src/mongo/dbtests/query_multi_plan_runner.cpp @@ -80,8 +80,7 @@ namespace QueryMultiPlanRunner { } void addIndex(const BSONObj& obj) { - Client::WriteContext ctx(&_txn, ns()); - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } void insert(const BSONObj& obj) { diff --git a/src/mongo/dbtests/query_plan_executor.cpp b/src/mongo/dbtests/query_plan_executor.cpp index edb5653d33b..79f63a4a842 100644 --- a/src/mongo/dbtests/query_plan_executor.cpp +++ b/src/mongo/dbtests/query_plan_executor.cpp @@ -57,7 +57,7 @@ namespace QueryPlanExecutor { } void addIndex(const BSONObj& obj) { - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } void insert(const BSONObj& obj) { diff --git a/src/mongo/dbtests/query_stage_and.cpp b/src/mongo/dbtests/query_stage_and.cpp index d6398ee0869..392c296ca2d 100644 --- a/src/mongo/dbtests/query_stage_and.cpp +++ b/src/mongo/dbtests/query_stage_and.cpp @@ -61,7 +61,7 @@ namespace QueryStageAnd { } void addIndex(const BSONObj& obj) { - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } IndexDescriptor* getIndex(const BSONObj& obj, Collection* coll) { diff --git a/src/mongo/dbtests/query_stage_count_scan.cpp b/src/mongo/dbtests/query_stage_count_scan.cpp index 517bbc1b005..0301f391359 100644 --- a/src/mongo/dbtests/query_stage_count_scan.cpp +++ b/src/mongo/dbtests/query_stage_count_scan.cpp @@ -58,7 +58,7 @@ namespace QueryStageCountScan { } void addIndex(const BSONObj& obj) { - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } void insert(const BSONObj& obj) { diff --git a/src/mongo/dbtests/query_stage_distinct.cpp b/src/mongo/dbtests/query_stage_distinct.cpp index 2d66731f15b..049ecb7caf2 100644 --- a/src/mongo/dbtests/query_stage_distinct.cpp +++ b/src/mongo/dbtests/query_stage_distinct.cpp @@ -55,7 +55,7 @@ namespace QueryStageDistinct { } void addIndex(const BSONObj& obj) { - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } void insert(const BSONObj& obj) { diff --git a/src/mongo/dbtests/query_stage_merge_sort.cpp b/src/mongo/dbtests/query_stage_merge_sort.cpp index 5038ea91c8b..e0cb3676077 100644 --- a/src/mongo/dbtests/query_stage_merge_sort.cpp +++ b/src/mongo/dbtests/query_stage_merge_sort.cpp @@ -57,7 +57,7 @@ namespace QueryStageMergeSortTests { } void addIndex(const BSONObj& obj) { - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } IndexDescriptor* getIndex(const BSONObj& obj, Collection* coll) { diff --git a/src/mongo/dbtests/query_stage_subplan.cpp b/src/mongo/dbtests/query_stage_subplan.cpp index 184b1010a0c..18cc6e9b2f9 100644 --- a/src/mongo/dbtests/query_stage_subplan.cpp +++ b/src/mongo/dbtests/query_stage_subplan.cpp @@ -50,7 +50,7 @@ namespace QueryStageSubplan { } void addIndex(const BSONObj& obj) { - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } void insert(const BSONObj& doc) { diff --git a/src/mongo/dbtests/query_stage_tests.cpp b/src/mongo/dbtests/query_stage_tests.cpp index 4b1ef66dd50..bc87aaef668 100644 --- a/src/mongo/dbtests/query_stage_tests.cpp +++ b/src/mongo/dbtests/query_stage_tests.cpp @@ -67,8 +67,7 @@ namespace QueryStageTests { } void addIndex(const BSONObj& obj) { - Client::WriteContext ctx(&_txn, ns()); - _client.ensureIndex(ns(), obj); + ASSERT_OK(dbtests::createIndex(&_txn, ns(), obj)); } int countResults(const IndexScanParams& params, BSONObj filterObj = BSONObj()) { diff --git a/src/mongo/dbtests/querytests.cpp b/src/mongo/dbtests/querytests.cpp index decd6867e99..c9cf3886711 100644 --- a/src/mongo/dbtests/querytests.cpp +++ b/src/mongo/dbtests/querytests.cpp @@ -196,6 +196,7 @@ namespace QueryTests { _prevError = mongo::lastError._get( false ); mongo::lastError.release(); mongo::lastError.reset( new LastError() ); + _txn.getCurOp()->reset(); } virtual ~ClientBase() { mongo::lastError.reset( _prevError ); @@ -231,7 +232,7 @@ namespace QueryTests { a.appendMaxKey( "$lt" ); BSONObj limit = a.done(); ASSERT( !_client.findOne( ns, QUERY( "a" << limit ) ).isEmpty() ); - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns, BSON( "a" << 1 ) )); ASSERT( !_client.findOne( ns, QUERY( "a" << limit ).hint( BSON( "a" << 1 ) ) ).isEmpty() ); } }; @@ -691,7 +692,7 @@ namespace QueryTests { } void run() { const char *ns = "unittests.querytests.BasicCount"; - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex(&_txn, ns, BSON( "a" << 1 ) )); count( 0 ); insert( ns, BSON( "a" << 3 ) ); count( 0 ); @@ -715,7 +716,7 @@ namespace QueryTests { } void run() { const char *ns = "unittests.querytests.ArrayId"; - _client.ensureIndex( ns, BSON( "_id" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns, BSON( "_id" << 1 ) )); ASSERT( !error() ); _client.insert( ns, fromjson( "{'_id':[1,2]}" ) ); ASSERT( error() ); @@ -786,7 +787,7 @@ namespace QueryTests { const char *ns = "unittests.querytests.NumericEmbedded"; _client.insert( ns, BSON( "a" << BSON ( "b" << 1 ) ) ); ASSERT( ! _client.findOne( ns, BSON( "a" << BSON ( "b" << 1.0 ) ) ).isEmpty() ); - _client.ensureIndex( ns , BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ) )); ASSERT( ! _client.findOne( ns, BSON( "a" << BSON ( "b" << 1.0 ) ) ).isEmpty() ); } }; @@ -801,7 +802,7 @@ namespace QueryTests { void index() { ASSERT_EQUALS(2u, _client.getIndexSpecs(ns()).size()); } void noIndex() { ASSERT_EQUALS(0u, _client.getIndexSpecs(ns()).size()); } void checkIndex() { - _client.ensureIndex( ns(), BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns() , BSON( "a" << 1 ) )); index(); } void run() { @@ -824,12 +825,12 @@ namespace QueryTests { } void run() { const char *ns = "unittests.querytests.UniqueIndex"; - _client.ensureIndex( ns, BSON( "a" << 1 ), true ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ), true )); _client.insert( ns, BSON( "a" << 4 << "b" << 2 ) ); _client.insert( ns, BSON( "a" << 4 << "b" << 3 ) ); ASSERT_EQUALS( 1U, _client.count( ns, BSONObj() ) ); _client.dropCollection( ns ); - _client.ensureIndex( ns, BSON( "b" << 1 ), true ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "b" << 1 ), true )); _client.insert( ns, BSON( "a" << 4 << "b" << 2 ) ); _client.insert( ns, BSON( "a" << 4 << "b" << 3 ) ); ASSERT_EQUALS( 2U, _client.count( ns, BSONObj() ) ); @@ -845,7 +846,8 @@ namespace QueryTests { const char *ns = "unittests.querytests.UniqueIndexPreexistingData"; _client.insert( ns, BSON( "a" << 4 << "b" << 2 ) ); _client.insert( ns, BSON( "a" << 4 << "b" << 3 ) ); - _client.ensureIndex( ns, BSON( "a" << 1 ), true ); + ASSERT_EQUALS(ErrorCodes::DuplicateKey, + dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ), true )); ASSERT_EQUALS( 0U, _client.count( "unittests.system.indexes", BSON( "ns" << ns << "name" << NE << "_id_" ) ) ); } }; @@ -871,7 +873,7 @@ namespace QueryTests { void run() { const char *ns = "unittests.querytests.Size"; _client.insert( ns, fromjson( "{a:[1,2,3]}" ) ); - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ) )); ASSERT( _client.query( ns, QUERY( "a" << mongo::BSIZE << 3 ).hint( BSON( "a" << 1 ) ) )->more() ); } }; @@ -885,7 +887,7 @@ namespace QueryTests { const char *ns = "unittests.querytests.IndexedArray"; _client.insert( ns, fromjson( "{a:[1,2,3]}" ) ); ASSERT( _client.query( ns, Query( "{a:[1,2,3]}" ) )->more() ); - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ) )); ASSERT( _client.query( ns, Query( "{a:{$in:[1,[1,2,3]]}}" ).hint( BSON( "a" << 1 ) ) )->more() ); ASSERT( _client.query( ns, Query( "{a:[1,2,3]}" ).hint( BSON( "a" << 1 ) ) )->more() ); // SERVER-146 } @@ -900,7 +902,7 @@ namespace QueryTests { const char *ns = "unittests.querytests.InsideArray"; _client.insert( ns, fromjson( "{a:[[1],2]}" ) ); check( "$natural" ); - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ) )); check( "a" ); // SERVER-146 } private: @@ -922,7 +924,7 @@ namespace QueryTests { const char *ns = "unittests.querytests.IndexInsideArrayCorrect"; _client.insert( ns, fromjson( "{'_id':1,a:[1]}" ) ); _client.insert( ns, fromjson( "{'_id':2,a:[[1]]}" ) ); - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ) )); ASSERT_EQUALS( 1, _client.query( ns, Query( "{a:[1]}" ).hint( BSON( "a" << 1 ) ) )->next().getIntField( "_id" ) ); } }; @@ -936,7 +938,7 @@ namespace QueryTests { const char *ns = "unittests.querytests.SubobjArr"; _client.insert( ns, fromjson( "{a:[{b:[1]}]}" ) ); check( "$natural" ); - _client.ensureIndex( ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "a" << 1 ) )); check( "a" ); } private: @@ -954,7 +956,7 @@ namespace QueryTests { _client.dropCollection( "unittests.querytests.MinMax" ); } void run() { - _client.ensureIndex( ns, BSON( "a" << 1 << "b" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns, BSON( "a" << 1 << "b" << 1 ) )); _client.insert( ns, BSON( "a" << 1 << "b" << 1 ) ); _client.insert( ns, BSON( "a" << 1 << "b" << 2 ) ); _client.insert( ns, BSON( "a" << 2 << "b" << 1 ) ); @@ -1010,7 +1012,7 @@ namespace QueryTests { } void run() { checkMatch(); - _client.ensureIndex( _ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, _ns, BSON( "a" << 1 ) )); checkMatch(); } private: @@ -1047,7 +1049,7 @@ namespace QueryTests { } void run() { checkMatch(); - _client.ensureIndex( _ns, BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, _ns, BSON( "a" << 1 ) )); checkMatch(); } private: @@ -1085,7 +1087,7 @@ namespace QueryTests { void run() { const char *ns = "unittests.querytests.FastCountIn"; _client.insert( ns, BSON( "i" << "a" ) ); - _client.ensureIndex( ns, BSON( "i" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns, BSON( "i" << 1 ) )); ASSERT_EQUALS( 1U, _client.count( ns, fromjson( "{i:{$in:['a']}}" ) ) ); } }; @@ -1130,7 +1132,7 @@ namespace QueryTests { { BSONObjBuilder b; b.append( "7" , (double)3.7 ); _client.insert( ns , b.obj() ); } t(ns); - _client.ensureIndex( ns , BSON( "7" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns , BSON( "7" << 1 ) )); t(ns); } }; @@ -1181,7 +1183,7 @@ namespace QueryTests { ASSERT_EQUALS( 17 , _client.findOne( ns() , b.obj() )["z"].number() ); } ASSERT_EQUALS( 17 , _client.findOne( ns() , BSON( "x" << "eliot" ) )["z"].number() ); - _client.ensureIndex( ns() , BSON( "x" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns() , BSON( "x" << 1 ) )); ASSERT_EQUALS( 17 , _client.findOne( ns() , BSON( "x" << "eliot" ) )["z"].number() ); } }; diff --git a/src/mongo/dbtests/repltests.cpp b/src/mongo/dbtests/repltests.cpp index 63b88451705..2c7ce959c9b 100644 --- a/src/mongo/dbtests/repltests.cpp +++ b/src/mongo/dbtests/repltests.cpp @@ -1246,7 +1246,7 @@ namespace ReplTests { void reset() const { deleteAll( ns() ); // Add an index on 'a'. This prevents the update from running 'in place'. - _client.ensureIndex( ns(), BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns(), BSON( "a" << 1 ) )); insert( fromjson( "{'_id':0,z:1}" ) ); } }; diff --git a/src/mongo/dbtests/sharding.cpp b/src/mongo/dbtests/sharding.cpp index b1c6c8ec7b7..14c2f694bfc 100644 --- a/src/mongo/dbtests/sharding.cpp +++ b/src/mongo/dbtests/sharding.cpp @@ -117,9 +117,11 @@ namespace ShardingTests { Shard::installShard(_shard.getName(), _shard); // Create an index so that diffing works correctly, otherwise no cursors from S&O - _client.ensureIndex( ChunkType::ConfigNS, // br - BSON( ChunkType::ns() << 1 << // br - ChunkType::DEPRECATED_lastmod() << 1 ) ); + ASSERT_OK(dbtests::createIndex( + &_txn, + ChunkType::ConfigNS, + BSON( ChunkType::ns() << 1 << // br + ChunkType::DEPRECATED_lastmod() << 1 ) )); configServer.init("$dummy:1000"); } diff --git a/src/mongo/dbtests/updatetests.cpp b/src/mongo/dbtests/updatetests.cpp index cbdde5d2976..270c5254c45 100644 --- a/src/mongo/dbtests/updatetests.cpp +++ b/src/mongo/dbtests/updatetests.cpp @@ -1632,7 +1632,7 @@ namespace UpdateTests { class IndexParentOfMod : public SetBase { public: void run() { - _client.ensureIndex( ns(), BSON( "a" << 1 ) ); + ASSERT_OK(dbtests::createIndex( &_txn, ns(), BSON( "a" << 1 ) )); _client.insert( ns(), fromjson( "{'_id':0}" ) ); _client.update( ns(), Query(), fromjson( "{$set:{'a.b':4}}" ) ); ASSERT_EQUALS( fromjson( "{'_id':0,a:{b:4}}" ) , _client.findOne( ns(), Query() ) ); |