Permalink
Browse files

add _id index automatically, don't allow it to be deleted

  • Loading branch information...
1 parent 7d85516 commit b1cc3f6223bb2a1f12dbd8c2712267ae01cb511b @astaple astaple committed Apr 20, 2009
Showing with 47 additions and 34 deletions.
  1. +20 −5 db/dbcommands.cpp
  2. +5 −2 db/pdfile.cpp
  3. +3 −3 dbtests/querytests.cpp
  4. +14 −9 jstests/apitest_dbcollection.js
  5. +1 −1 jstests/index4.js
  6. +0 −8 jstests/index_check1.js
  7. +1 −1 jstests/rename.js
  8. +3 −5 jstests/uniqueness.js
View
25 db/dbcommands.cpp
@@ -485,7 +485,7 @@ namespace mongo {
}
} cmdoplogging;
- bool deleteIndexes( NamespaceDetails *d, const char *ns, const char *name, string &errmsg, BSONObjBuilder &anObjBuilder ) {
+ bool deleteIndexes( NamespaceDetails *d, const char *ns, const char *name, string &errmsg, BSONObjBuilder &anObjBuilder, bool mayDeleteIdIndex ) {
d->aboutToDeleteAnIndex();
@@ -497,11 +497,21 @@ namespace mongo {
log() << " d->nIndexes was " << d->nIndexes << '\n';
anObjBuilder.append("nIndexesWas", (double)d->nIndexes);
anObjBuilder.append("msg", "all indexes deleted for collection");
+ IndexDetails *idIndex = 0;
if( d->nIndexes ) {
- for ( int i = 0; i < d->nIndexes; i++ )
- d->indexes[i].kill();
+ for ( int i = 0; i < d->nIndexes; i++ ) {
+ if ( !mayDeleteIdIndex && d->indexes[i].isIdIndex() ) {
+ idIndex = &d->indexes[i];
+ } else {
+ d->indexes[i].kill();
+ }
+ }
d->nIndexes = 0;
}
+ if ( idIndex ) {
+ d->indexes[ 0 ] = *idIndex;
+ d->nIndexes = 1;
+ }
}
else {
// delete just one index
@@ -514,6 +524,11 @@ namespace mongo {
call, otherwise, on recreate, the old one would be reused, and its
IndexDetails::info ptr would be bad info.
*/
+ IndexDetails *id = &d->indexes[x];
+ if ( !mayDeleteIdIndex && id->isIdIndex() ) {
+ errmsg = "may not delete _id index";
+ return false;
+ }
d->indexes[x].kill();
d->nIndexes--;
@@ -551,7 +566,7 @@ namespace mongo {
return false;
}
if ( d->nIndexes != 0 ) {
- assert( deleteIndexes(d, nsToDrop.c_str(), "*", errmsg, result) );
+ assert( deleteIndexes(d, nsToDrop.c_str(), "*", errmsg, result, true) );
assert( d->nIndexes == 0 );
}
result.append("ns", nsToDrop.c_str());
@@ -666,7 +681,7 @@ namespace mongo {
if ( d ) {
BSONElement f = jsobj.findElement("index");
if ( f.type() == String ) {
- return deleteIndexes( d, toDeleteNs.c_str(), f.valuestr(), errmsg, anObjBuilder );
+ return deleteIndexes( d, toDeleteNs.c_str(), f.valuestr(), errmsg, anObjBuilder, false );
}
else {
errmsg = "invalid index name spec";
View
7 db/pdfile.cpp
@@ -1066,7 +1066,7 @@ assert( !eloc.isNull() );
return loc;
}
- bool deleteIndexes( NamespaceDetails *d, const char *ns, const char *name, string &errmsg, BSONObjBuilder &anObjBuilder );
+ bool deleteIndexes( NamespaceDetails *d, const char *ns, const char *name, string &errmsg, BSONObjBuilder &anObjBuilder, bool maydeleteIdIndex );
DiskLoc DataFileMgr::insert(const char *ns, const void *obuf, int len, bool god, const BSONElement &writeId) {
bool addIndex = false;
@@ -1097,6 +1097,9 @@ assert( !eloc.isNull() );
// This creates first file in the database.
database->newestFile()->allocExtent(ns, initialExtentSize(len));
d = nsdetails(ns);
+ if ( !god && !strstr( ns, ".system." ) && !strstr( ns, ".oplog." ) ) {
+ ensureHaveIdIndex( ns );
+ }
}
d->paddingFits();
@@ -1242,7 +1245,7 @@ assert( !eloc.isNull() );
string name = idxinfo.indexName();
BSONObjBuilder b;
string errmsg;
- bool ok = deleteIndexes(tableToIndex, tabletoidxns.c_str(), name.c_str(), errmsg, b);
+ bool ok = deleteIndexes(tableToIndex, tabletoidxns.c_str(), name.c_str(), errmsg, b, true);
if( !ok ) {
log() << "failed to drop index after a unique key error building it: " << errmsg << ' ' << tabletoidxns << ' ' << name << endl;
}
View
6 dbtests/querytests.cpp
@@ -397,8 +397,8 @@ namespace QueryTests {
}
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 index() const { ASSERT( !client().findOne( idxNs(), BSON( "name" << NE << "_id_" ) ).isEmpty() ); }
+ void noIndex() const { ASSERT( client().findOne( idxNs(), BSON( "name" << NE << "_id_" ) ).isEmpty() ); }
void checkIndex() {
client().ensureIndex( ns(), BSON( "a" << 1 ) );
index();
@@ -445,7 +445,7 @@ namespace QueryTests {
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( 0U, client().count( "querytests.system.indexes", BSON( "ns" << ns ) ) );
+ ASSERT_EQUALS( 0U, client().count( "querytests.system.indexes", BSON( "ns" << ns << "name" << NE << "_id_" ) ) );
}
};
View
23 jstests/apitest_dbcollection.js
@@ -67,43 +67,48 @@ db.getCollection( "test_db" ).dropIndexes();
assert(db.getCollection( "test_db" ).getIndexes().length() == 0,13);
db.getCollection( "test_db" ).save({a:10});
+assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
+
db.getCollection( "test_db" ).ensureIndex({a:1});
db.getCollection( "test_db" ).save({a:10});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 2);
db.getCollection( "test_db" ).dropIndex({a:1});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 0);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
db.getCollection( "test_db" ).save({a:10});
db.getCollection( "test_db" ).ensureIndex({a:1});
db.getCollection( "test_db" ).save({a:10});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 2);
db.getCollection( "test_db" ).dropIndex("a_1");
-assert(db.getCollection( "test_db" ).getIndexes().length() == 0);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
db.getCollection( "test_db" ).save({a:10, b:11});
db.getCollection( "test_db" ).ensureIndex({a:1});
db.getCollection( "test_db" ).ensureIndex({b:1});
db.getCollection( "test_db" ).save({a:10, b:12});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 2);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 3);
db.getCollection( "test_db" ).dropIndex({b:1});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 2);
db.getCollection( "test_db" ).dropIndex({a:1});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 0);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
db.getCollection( "test_db" ).save({a:10, b:11});
db.getCollection( "test_db" ).ensureIndex({a:1});
db.getCollection( "test_db" ).ensureIndex({b:1});
db.getCollection( "test_db" ).save({a:10, b:12});
-assert(db.getCollection( "test_db" ).getIndexes().length() == 2);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 3);
db.getCollection( "test_db" ).dropIndexes();
-assert(db.getCollection( "test_db" ).getIndexes().length() == 0);
+assert(db.getCollection( "test_db" ).getIndexes().length() == 1);
db.getCollection( "test_db" ).find();
+
+db.getCollection( "test_db" ).drop();
+assert(db.getCollection( "test_db" ).getIndexes().length() == 0);
View
2 jstests/index4.js
@@ -20,7 +20,7 @@ t.save( { name : "clusterstock" ,
// this should fail, not allowed -- we confirm that.
t.ensureIndex( { instances : { pool : 1 } } );
-assert( !t.getIndexes().hasNext(), "no indexes should be here yet");
+assert.eq( 0, db.system.indexes.find( {ns:"test.index4",name:{$ne:"_id_"}} ).count(), "no indexes should be here yet");
t.ensureIndex( { "instances.pool" : 1 } );
View
8 jstests/index_check1.js
@@ -5,10 +5,6 @@ assert(db.system.namespaces.find({name:/somecollection/}).length() == 0, 1);
db.somecollection.save({a:1});
-assert(db.system.namespaces.find({name:/somecollection/}).length() == 1, 2);
-
-db.somecollection.ensureIndex({a:1});
-
var z = db.system.namespaces.find({name:/somecollection/}).length();
assert( z >= 1 , 3 );
@@ -19,10 +15,6 @@ db.somecollection.drop();
assert(db.system.namespaces.find({name:/somecollection/}).length() == 0, 4);
-db.somecollection.save({a:1});
-
-assert(db.system.namespaces.find({name:/somecollection/}).length() == 1, 5);
-
db.somecollection.ensureIndex({a:1});
var x = db.system.namespaces.find({name:/somecollection/}).length();
View
2 jstests/rename.js
@@ -20,5 +20,5 @@ assert.commandWorked( admin.runCommand( {renameCollection:"test.jstests_rename_a
assert.eq( 0, a.find().count() );
assert.eq( 2, b.find().count() );
-assert.eq( 2, db.system.indexes.find( {ns:"test.jstests_rename_b"} ).count() );
+assert.eq( 3, db.system.indexes.find( {ns:"test.jstests_rename_b"} ).count() );
assert( b.find( {a:1} ).explain().cursor.match( /^BtreeCursor/ ) );
View
8 jstests/uniqueness.js
@@ -5,8 +5,6 @@ t.drop();
// test uniqueness of _id
-t.ensureIndex({_id:1});
-
t.save( { _id : 3 } );
assert( !db.getLastError(), 1 );
@@ -25,8 +23,8 @@ assert( t.findOne( {_id:4} ), 5 );
/* Check for an error message when we index and there are dups */
db.bar.drop();
-db.bar.insert({_id:3});
-db.bar.insert({_id:3});
+db.bar.insert({a:3});
+db.bar.insert({a:3});
assert( db.bar.count() == 2 , 6) ;
-db.bar.ensureIndex({_id:1});
+db.bar.ensureIndex({a:1}, true);
assert( db.getLastError() , 7);

0 comments on commit b1cc3f6

Please sign in to comment.