void run() { OperationContextImpl txn; DBDirectClient client(&txn); for ( int i = 0; i < 10; ++i ) { client.insert( ns, BSON( "_id" << i ) ); } { // Remove _id range [_min, _max). Lock::DBWrite lk(txn.lockState(), ns); WriteUnitOfWork wunit(txn.recoveryUnit()); Client::Context ctx(&txn, ns ); KeyRange range( ns, BSON( "_id" << _min ), BSON( "_id" << _max ), BSON( "_id" << 1 ) ); mongo::WriteConcernOptions dummyWriteConcern; Helpers::removeRange(&txn, range, false, dummyWriteConcern); wunit.commit(); } // Check that the expected documents remain. ASSERT_EQUALS( expected(), docs(&txn) ); }
void doDBUpgrade( const string& dbName, DataFileHeader* h ) { OperationContextImpl txn; DBDirectClient db(&txn); if ( h->version == 4 && h->versionMinor == 4 ) { verify( PDFILE_VERSION == 4 ); verify( PDFILE_VERSION_MINOR_22_AND_OLDER == 5 ); list<string> colls = db.getCollectionNames( dbName ); for ( list<string>::iterator i=colls.begin(); i!=colls.end(); i++) { string c = *i; log() << "\t upgrading collection:" << c << endl; BSONObj out; bool ok = db.runCommand( dbName , BSON( "reIndex" << c.substr( dbName.size() + 1 ) ) , out ); if ( ! ok ) { log() << "\t\t reindex failed: " << out; fassertFailed( 17393 ); } } txn.recoveryUnit()->writingInt(h->versionMinor) = 5; return; } // do this in the general case fassert( 17401, repairDatabase( &txn, dbName ) ); }
// ran at startup. static void repairDatabasesAndCheckVersion(bool shouldClearNonLocalTmpCollections) { LOG(1) << "enter repairDatabases (to check pdfile version #)" << endl; OperationContextImpl txn; Lock::GlobalWrite lk(txn.lockState()); WriteUnitOfWork wunit(txn.recoveryUnit()); vector< string > dbNames; globalStorageEngine->listDatabases( &dbNames ); for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) { string dbName = *i; LOG(1) << "\t" << dbName << endl; Client::Context ctx(&txn, dbName ); if (repl::replSettings.usingReplSets()) { // we only care about the _id index if we are in a replset checkForIdIndexes(&txn, ctx.db()); } if (shouldClearNonLocalTmpCollections || dbName == "local") ctx.db()->clearTmpCollections(&txn); if ( mongodGlobalParams.repair ) { fassert(18506, globalStorageEngine->repairDatabase(&txn, dbName)); } else if (!ctx.db()->getDatabaseCatalogEntry()->currentFilesCompatible(&txn)) { log() << "****"; log() << "cannot do this upgrade without an upgrade in the middle"; log() << "please do a --repair with 2.6 and then start this version"; dbexit( EXIT_NEED_UPGRADE ); invariant( false ); return; } else { // major versions match, check indexes const string systemIndexes = ctx.db()->name() + ".system.indexes"; Collection* coll = ctx.db()->getCollection( &txn, systemIndexes ); auto_ptr<Runner> runner(InternalPlanner::collectionScan(&txn, systemIndexes,coll)); BSONObj index; Runner::RunnerState state; while (Runner::RUNNER_ADVANCED == (state = runner->getNext(&index, NULL))) { const BSONObj key = index.getObjectField("key"); const string plugin = IndexNames::findPluginName(key); if (ctx.db()->getDatabaseCatalogEntry()->isOlderThan24(&txn)) { if (IndexNames::existedBefore24(plugin)) continue; log() << "Index " << index << " claims to be of type '" << plugin << "', " << "which is either invalid or did not exist before v2.4. " << "See the upgrade section: " << "http://dochub.mongodb.org/core/upgrade-2.4" << startupWarningsLog; } const Status keyStatus = validateKeyPattern(key); if (!keyStatus.isOK()) { log() << "Problem with index " << index << ": " << keyStatus.reason() << " This index can still be used however it cannot be rebuilt." << " For more info see" << " http://dochub.mongodb.org/core/index-validation" << startupWarningsLog; } } if (Runner::RUNNER_EOF != state) { warning() << "Internal error while reading collection " << systemIndexes; } Database::closeDatabase(&txn, dbName.c_str()); } } wunit.commit(); LOG(1) << "done repairDatabases" << endl; }