diff --git a/db/database.cpp b/db/database.cpp index b40005f1779..fb243302083 100644 --- a/db/database.cpp +++ b/db/database.cpp @@ -24,4 +24,32 @@ namespace mongo { bool Database::_openAllFiles = false; + bool Database::setProfilingLevel( int newLevel , string& errmsg ){ + if ( profile == newLevel ) + return true; + + if ( newLevel < 0 || newLevel > 2 ){ + errmsg = "profiling level has to be >=0 and <= 2"; + return false; + } + + if ( newLevel == 0 ){ + profile = 0; + return true; + } + + assert( cc().database() == this ); + + if ( ! nsdetails( profileName.c_str() ) ){ + BSONObjBuilder spec; + spec.appendBool( "capped", true ); + spec.append( "size", 131072.0 ); + if ( ! userCreateNS( profileName.c_str(), spec.done(), errmsg , true ) ){ + return false; + } + } + profile = newLevel; + return true; + } + } // namespace mongo diff --git a/db/database.h b/db/database.h index 99864d829a8..3fd162b6158 100644 --- a/db/database.h +++ b/db/database.h @@ -158,13 +158,18 @@ namespace mongo { if( e ) return e; return suitableFile( size )->createExtent( ns, size, capped ); } - + MongoDataFile* newestFile() { int n = (int) files.size(); if ( n > 0 ) n--; return getFile(n); } + /** + * @return true if success, false otherwise + */ + bool setProfilingLevel( int newLevel , string& errmsg ); + vector files; string name; // "alleyinsider" string path; diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp index 57afa00b10c..f7159562bfb 100644 --- a/db/dbcommands.cpp +++ b/db/dbcommands.cpp @@ -294,17 +294,7 @@ namespace mongo { if ( p == -1 ) ok = true; else if ( p >= 0 && p <= 2 ) { - if( p && nsdetails(cc().database()->profileName.c_str()) == 0 ) { - BSONObjBuilder spec; - spec.appendBool( "capped", true ); - spec.append( "size", 131072.0 ); - - if ( !userCreateNS( cc().database()->profileName.c_str(), spec.done(), errmsg, true ) ) { - return false; - } - } - ok = true; - cc().database()->profile = p; + ok = cc().database()->setProfilingLevel( p , errmsg ); } return ok; } diff --git a/jstests/profile1.js b/jstests/profile1.js index d9abffe16b7..ea53b09f09f 100644 --- a/jstests/profile1.js +++ b/jstests/profile1.js @@ -13,6 +13,8 @@ var capped_size = db.system.profile.storageSize(); assert.gt(capped_size, 999, "D"); assert.lt(capped_size, 2000, "E"); +assert.eq( 4 , db.system.profile.find().count() , "E2" ); + /* Make sure we can't drop if profiling is still on */ assert.throws( function(z){ db.getCollection("system.profile").drop(); } )