Permalink
Browse files

SERVER-5516 SERVER-5304 SERVER-6577 new capped collections now get _i…

…d index by default (except in local db)
  • Loading branch information...
1 parent af67eaf commit b202965c2aa985232699b059c7135dd64f09700e @matulef matulef committed Aug 2, 2012
View
@@ -1,7 +1,7 @@
db.jstests_capped.drop();
db.createCollection("jstests_capped", {capped:true, size:30000});
-assert.eq( 0, db.system.indexes.find( {ns:"test.jstests_capped"} ).count(), "expected a count of zero indexes for new capped collection" );
+assert.eq( 1, db.system.indexes.find( {ns:"test.jstests_capped"} ).count(), "expected a count of one index for new capped collection" );
t = db.jstests_capped;
t.save({x:1});
View
@@ -7,7 +7,7 @@ t.drop();
db.createCollection( tn , {capped: true, size: 1024 * 1024 * 1 } );
t.insert( { _id : 5 , x : 11 , z : 52 } );
-assert.eq( 0 , t.getIndexKeys().length , "A0" )
+assert.eq( 1 , t.getIndexKeys().length , "A0" ) //now we assume _id index even on capped coll
assert.eq( 52 , t.findOne( { x : 11 } ).z , "A1" );
t.ensureIndex( { _id : 1 } )
@@ -21,23 +21,22 @@ db.createCollection( tn , {capped: true, size: 1024 * 1024 * 1 } );
t.insert( { _id : 5 , x : 11 } );
t.insert( { _id : 6 , x : 11 } );
t.ensureIndex( { x:1 }, {unique:true, dropDups:true } );
-assert.eq( 0, db.system.indexes.count( {ns:"test."+tn} ) );
+assert.eq( 1, db.system.indexes.count( {ns:"test."+tn} ) ); //now we assume _id index
assert.eq( 2, t.find().toArray().length );
t.drop();
db.createCollection( tn , {capped: true, size: 1024 * 1024 * 1 } );
t.insert( { _id : 5 , x : 11 } );
t.insert( { _id : 5 , x : 12 } );
-t.ensureIndex( { _id:1 } );
-assert.eq( 0, db.system.indexes.count( {ns:"test."+tn} ) );
-assert.eq( 2, t.find().toArray().length );
+assert.eq( 1, db.system.indexes.count( {ns:"test."+tn} ) ); //now we assume _id index
+assert.eq( 1, t.find().toArray().length ); //_id index unique, so second insert fails
t.drop();
db.createCollection( tn , {capped: true, size: 1024 * 1024 * 1 } );
t.insert( { _id : 5 , x : 11 } );
t.insert( { _id : 6 , x : 12 } );
t.ensureIndex( { x:1 }, {unique:true, dropDups:true } );
-assert.eq( 1, db.system.indexes.count( {ns:"test."+tn} ) );
+assert.eq( 2, db.system.indexes.count( {ns:"test."+tn} ) ); //now we assume _id index
assert.eq( 2, t.find().hint( {x:1} ).toArray().length );
// SERVER-525 (closed) unique indexes in capped collection
@@ -52,17 +52,17 @@ function assertNewWarning() {
oldNumWarnings = newNumWarnings;
}
-// Simple _id query without an _id index.
+// Simple _id query
t.find( { _id:0 } ).itcount();
-assertNewWarning();
+assertNoNewWarnings();
// Simple _id query without an _id index, on a non capped collection.
notCapped.find( { _id:0 } ).itcount();
assertNoNewWarnings();
// A multi field query, including _id.
t.find( { _id:0, a:0 } ).itcount();
-assertNewWarning();
+assertNoNewWarnings();
// An unsatisfiable query.
t.find( { _id:0, a:{$in:[]} } ).itcount();
@@ -74,7 +74,7 @@ assertNoNewWarnings();
// Retry a multi field query.
t.find( { _id:0, a:0 } ).itcount();
-assertNewWarning();
+assertNoNewWarnings();
// Warnings should not be printed when an index is added on _id.
t.ensureIndex( { _id:1 } );
@@ -101,8 +101,8 @@ for( testnum=0; testnum < numtests; testnum++ ){
slave2db.system.indexes.find().forEach(printjson);
print("");
- // insure each slave has _id index, but not master
- assert.eq( 0 ,
+ // insure all nodes have _id index
+ assert.eq( 1 ,
masterdb.system.indexes.find( { key:{"_id" : 1}, ns: dbname + "." + coll } ).count() ,
"master has an _id index on capped collection");
assert.eq( 1 ,
@@ -31,7 +31,7 @@ db.bar.ensureIndex({x:1});
barDocCount = db.bar.count();
assert.gt( barDocCount, 0 , "No documents inserted" );
assert.lt( db.bar.count(), 1000 , "Capped collection didn't evict documents" );
-assert.eq( 4 , db.system.indexes.count() , "Indexes weren't created right" );
+assert.eq( 5 , db.system.indexes.count() , "Indexes weren't created right" );
// Full dump/restore
@@ -52,7 +52,7 @@ for (var i = 0; i < 10; i++) {
db.bar.save({x:i});
}
assert.eq( barDocCount, db.bar.count(), "Capped collection didn't evict documents after restore." );
-assert.eq( 4 , db.system.indexes.count() , "Indexes weren't created correctly by restore" );
+assert.eq( 5 , db.system.indexes.count() , "Indexes weren't created correctly by restore" );
// Dump/restore single DB
@@ -77,7 +77,7 @@ for (var i = 0; i < 10; i++) {
db.bar.save({x:i});
}
assert.eq( barDocCount, db.bar.count(), "Capped collection didn't evict documents after restore 2." );
-assert.eq( 4 , db.system.indexes.count() , "Indexes weren't created correctly by restore 2" );
+assert.eq( 5 , db.system.indexes.count() , "Indexes weren't created correctly by restore 2" );
// Dump/restore single collection
@@ -100,6 +100,6 @@ for (var i = 0; i < 10; i++) {
db.baz.save({x:i});
}
assert.eq( barDocCount, db.baz.count(), "Capped collection didn't evict documents after restore 3." );
-assert.eq( 1 , db.system.indexes.count() , "Indexes weren't created correctly by restore 3" );
+assert.eq( 2 , db.system.indexes.count() , "Indexes weren't created correctly by restore 3" );
t.stop();
View
@@ -766,14 +766,9 @@ namespace mongo {
}
}
else {
- /* erh 10/16/2009 - this is probably not relevant any more since its auto-created, but not worth removing */
-
- // this is the old version that doesn't create _id indexes on capped collections
- //RARELY if (nsd && !nsd->isCapped()) { ensureHaveIdIndex(ns); } // otherwise updates will be slow
-
- // this version creates indexes on all collections, including capped
- // as long we apply inserts as updates, this is needed for performance
- RARELY if ( nsd ) { ensureHaveIdIndex(ns); } // otherwise updates will be slow
+ // probably don't need this since all replicated colls have _id indexes now
+ // but keep it just in case
+ RARELY if ( nsd && !nsd->isCapped() ) { ensureHaveIdIndex(ns); }
/* todo : it may be better to do an insert here, and then catch the dup key exception and do update
then. very few upserts will not be inserts...
@@ -787,8 +782,11 @@ namespace mongo {
}
else if ( *opType == 'u' ) {
opCounters->gotUpdate();
- // ensure all collections, including capped, have an _id index.
- RARELY if ( nsd ) { ensureHaveIdIndex(ns); } // otherwise updates will be super slow
+
+ // probably don't need this since all replicated colls have _id indexes now
+ // but keep it just in case
+ RARELY if ( nsd && !nsd->isCapped() ) { ensureHaveIdIndex(ns); }
+
OpDebug debug;
BSONObj updateCriteria = op.getObjectField("o2");
bool upsert = fields[3].booleanSafe();
View
@@ -313,16 +313,11 @@ namespace mongo {
NamespaceDetails *d = nsdetails(ns);
verify(d);
- bool ensure = false;
- if ( options.getField( "autoIndexId" ).type() ) {
- if ( options["autoIndexId"].trueValue() ) {
- ensure = true;
- }
- }
- else {
- if ( !newCapped ) {
- ensure=true;
- }
+ // make an _id index for all colls, except capped ones in local w/o autoIndexID
+ // (reason for the exception is for the oplog and non-replicated capped colls)
+ bool ensure = true;
+ if( newCapped && str::equals( nsToDatabase( ns ).c_str() , "local" ) ) {
+ ensure = options.getField( "autoIndexId" ).trueValue();
}
if( ensure ) {
if( deferIdIndex )
@@ -1435,8 +1430,9 @@ namespace mongo {
BSONObj io((const char *) obuf);
BSONElement idField = io.getField( "_id" );
uassert( 10099 , "_id cannot be an array", idField.type() != Array );
- // we don't add _id for capped collections as they don't have an _id index
- if( idField.eoo() && !wouldAddIndex && strstr(ns, ".local.") == 0 && d->haveIdIndex() ) {
+ // we don't add _id for capped collections in local as they don't have an _id index
+ if( idField.eoo() && !wouldAddIndex &&
+ !str::equals( nsToDatabase( ns ).c_str() , "local" ) && d->haveIdIndex() ) {
if( addedID )
*addedID = true;
addID = len;
@@ -3751,14 +3751,14 @@ namespace QueryOptimizerCursorTests {
void check( const shared_ptr<Cursor> &c ) {}
};
- class SnapshotWithoutIdIndex : public Base {
+ class SnapshotCappedColl : public Base {
public:
- SnapshotWithoutIdIndex() {
+ SnapshotCappedColl() {
_cli.dropCollection( ns() );
_cli.createCollection( ns(), 1000, true );
_cli.ensureIndex( ns(), BSON( "a" << 1 ) );
}
- string expectedType() const { return "BtreeCursor a_1"; }
+ string expectedType() const { return "BtreeCursor _id_"; }
BSONObj query() const {
return BSON( "$query" << BSON( "a" << 1 ) << "$snapshot" << true );
}
@@ -4837,7 +4837,7 @@ namespace QueryOptimizerCursorTests {
add<GetCursor::MultiIndex>();
add<GetCursor::Hint>();
add<GetCursor::Snapshot>();
- add<GetCursor::SnapshotWithoutIdIndex>();
+ add<GetCursor::SnapshotCappedColl>();
add<GetCursor::Min>();
add<GetCursor::Max>();
add<GetCursor::RequireIndex::NoConstraints>();
@@ -4854,9 +4854,10 @@ namespace QueryOptimizerCursorTests {
add<GetCursor::IdElseNatural::HintedIdForQuery>( BSON( "a" << 1 ) );
add<GetCursor::IdElseNatural::HintedIdForQuery>( BSON( "_id" << 1 << "a" << 1 ) );
add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSONObj() );
- add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSON( "_id" << 1 ) );
- add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSON( "a" << 1 ) );
- add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSON( "_id" << 1 << "a" << 1 ) );
+ // now capped collections have _id index by default, so skip these
+ //add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSON( "_id" << 1 ) );
+ //add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSON( "a" << 1 ) );
+ //add<GetCursor::IdElseNatural::HintedNaturalForQuery>( BSON( "_id" << 1 << "a" << 1 ) );
add<GetCursor::MatcherValidation>();
add<Explain::ClearRecordedIndex>();
add<Explain::Initial>();
@@ -336,8 +336,6 @@ namespace ReplSetTests {
}
};
- // check that applying ops doesn't cause _id index to be created
-
class CappedUpdate : public CappedInitialSync {
void updateSucceed() {
BSONObjBuilder b;

0 comments on commit b202965

Please sign in to comment.