Skip to content

Commit dc05a58

Browse files
committed
some cleaning of userCreateNs
1 parent eecd33e commit dc05a58

File tree

14 files changed

+58
-67
lines changed

14 files changed

+58
-67
lines changed

src/mongo/db/cloner.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,13 @@ namespace mongo {
314314
// config
315315
string temp = ctx.ctx().db()->name() + ".system.namespaces";
316316
BSONObj config = _conn->findOne(temp , BSON("name" << ns));
317-
if (config["options"].isABSONObj())
318-
if (!userCreateNS(ns.c_str(), config["options"].Obj(), errmsg, logForRepl, 0))
317+
if (config["options"].isABSONObj()) {
318+
Status status = userCreateNS(ns.c_str(), config["options"].Obj(), logForRepl, 0);
319+
if ( !status.isOK() ) {
320+
errmsg = status.toString();
319321
return false;
322+
}
323+
}
320324

321325
// main data
322326
copy(ctx.ctx(),
@@ -464,10 +468,8 @@ namespace mongo {
464468
string to_name = todb + p;
465469

466470
{
467-
string err;
468-
const char *toname = to_name.c_str();
469471
/* we defer building id index for performance - building it in batch is much faster */
470-
userCreateNS(toname, options, err, opts.logForRepl, false);
472+
userCreateNS(to_name, options, opts.logForRepl, false);
471473
}
472474
LOG(1) << "\t\t cloning " << from_name << " -> " << to_name << endl;
473475
Query q;

src/mongo/db/commands/collection_to_capped.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ namespace mongo {
6666
if ( temp )
6767
spec.appendBool( "temp", true );
6868

69-
string errmsg;
70-
if ( !userCreateNS( toNs.c_str(), spec.done(), errmsg, logForReplication ) )
71-
return Status( ErrorCodes::InternalError, errmsg );
69+
Status status = userCreateNS( toNs.c_str(), spec.done(), logForReplication );
70+
if ( !status.isOK() )
71+
return status;
7272
}
7373

7474
Collection* toCollection = db->getCollection( toNs );

src/mongo/db/commands/rename_collection.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ namespace mongo {
209209
// Create the target collection.
210210
Collection* targetColl = NULL;
211211
if ( capped ) {
212-
BSONObjBuilder spec;
213-
spec.appendBool( "capped", true );
214-
spec.append( "size", double( size ) );
215-
spec.appendBool( "autoIndexId", false );
216-
userCreateNS( target.c_str(), spec.obj(), errmsg, false );
217-
targetColl = ctx.db()->getCollection( target );
212+
CollectionOptions options;
213+
options.capped = true;
214+
options.cappedSize = size;
215+
options.setNoIdIndex();
216+
217+
targetColl = ctx.db()->createCollection( target, options );
218218
}
219219
else {
220220
CollectionOptions options;

src/mongo/db/dbcommands.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,8 @@ namespace mongo {
591591
options.hasField("$nExtents"));
592592

593593
// Create collection.
594-
string err;
595-
bool ok = userCreateNS(ns.c_str(), options, err, !fromRepl);
596-
if (!ok && !err.empty()) {
597-
errmsg = err;
598-
}
599-
return ok;
594+
return appendCommandStatus( result,
595+
userCreateNS(ns.c_str(), options, !fromRepl) );
600596
}
601597
} cmdCreate;
602598

src/mongo/db/introspect.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,13 @@ namespace {
181181

182182
// system.profile namespace doesn't exist; create it
183183
log() << "creating profile collection: " << profileName << endl;
184-
string myerrmsg;
185-
if (!userCreateNS(profileName,
186-
BSON("capped" << true << "size" << 1024 * 1024), myerrmsg , false)) {
187-
myerrmsg = str::stream() << "could not create ns " << profileName << ": " << myerrmsg;
188-
log() << myerrmsg << endl;
189-
if ( errmsg )
190-
*errmsg = myerrmsg;
191-
return NULL;
192-
}
193184

194-
collection = db->getCollection( profileName );
195-
verify( collection );
185+
CollectionOptions collectionOptions;
186+
collectionOptions.capped = true;
187+
collectionOptions.cappedSize = 1024 * 1024;
188+
189+
collection = db->createCollection( profileName, collectionOptions );
190+
invariant( collection );
196191
return collection;
197192
}
198193

src/mongo/db/pdfile.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,29 @@ namespace mongo {
107107
* @param createDefaultIndexes - if false, defers id (and other) index creation.
108108
* @return true if successful
109109
*/
110-
bool userCreateNS(const char *ns, BSONObj options, string& err,
111-
bool logForReplication, bool createDefaultIndexes ) {
110+
Status userCreateNS( const StringData& ns,
111+
BSONObj options,
112+
bool logForReplication,
113+
bool createDefaultIndexes ) {
112114

113115
LOG(1) << "create collection " << ns << ' ' << options;
114116

115-
massert(10356 ,
116-
str::stream() << "invalid ns: " << ns,
117-
NamespaceString::validCollectionComponent(ns));
117+
if ( !NamespaceString::validCollectionComponent(ns) )
118+
return Status( ErrorCodes::InvalidNamespace,
119+
str::stream() << "invalid ns: " << ns );
118120

119121
Database* db = cc().database();
120122

121123
Collection* collection = db->getCollection( ns );
122124

123-
if ( collection ) {
124-
err = "collection already exists";
125-
return false;
126-
}
125+
if ( collection )
126+
return Status( ErrorCodes::NamespaceExists,
127+
"collection already exists" );
127128

128129
CollectionOptions collectionOptions;
129130
Status status = collectionOptions.parse( options );
130-
if ( !status.isOK() ) {
131-
err = status.toString();
132-
return false;
133-
}
131+
if ( !status.isOK() )
132+
return status;
134133

135134
invariant( db->createCollection( ns, collectionOptions, true, createDefaultIndexes ) );
136135

@@ -144,7 +143,8 @@ namespace mongo {
144143
string logNs = nsToDatabase(ns) + ".$cmd";
145144
logOp("c", logNs.c_str(), options);
146145
}
147-
return true;
146+
147+
return Status::OK();
148148
}
149149

150150
void dropAllDatabasesExceptLocal() {

src/mongo/db/pdfile.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@
3939

4040
#include <string>
4141

42+
#include "mongo/base/status.h"
4243
#include "mongo/db/jsobj.h"
4344

4445
namespace mongo {
4546

4647
void dropDatabase(const std::string& db);
4748

48-
bool userCreateNS(const char *ns, BSONObj j, std::string& err,
49-
bool logForReplication, bool createDefaultIndexes = true );
49+
Status userCreateNS( const StringData& ns,
50+
BSONObj options,
51+
bool logForReplication,
52+
bool createDefaultIndexes = true );
5053

5154

5255
} // namespace mongo

src/mongo/db/repl/oplog.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ namespace mongo {
418418
}
419419

420420
/* create an oplog collection, if it doesn't yet exist. */
421-
BSONObjBuilder b;
422421
double sz;
423422
if (replSettings.oplogSize != 0)
424423
sz = (double)replSettings.oplogSize;
@@ -447,13 +446,12 @@ namespace mongo {
447446
log() << "******" << endl;
448447
log() << "creating replication oplog of size: " << (int)( sz / ( 1024 * 1024 ) ) << "MB..." << endl;
449448

450-
b.append("size", sz);
451-
b.appendBool("capped", 1);
452-
b.appendBool("autoIndexId", false);
449+
CollectionOptions options;
450+
options.capped = true;
451+
options.cappedSize = sz;
452+
options.autoIndexId = CollectionOptions::NO;
453453

454-
string err;
455-
BSONObj o = b.done();
456-
userCreateNS(ns, o, err, false);
454+
invariant( ctx.db()->createCollection( ns, options ) );
457455
if( !rs )
458456
logOp( "n", "", BSONObj() );
459457

src/mongo/dbtests/namespacetests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,7 @@ namespace NamespaceTests {
10431043
protected:
10441044
void create() {
10451045
Lock::GlobalWrite lk;
1046-
string err;
1047-
ASSERT( userCreateNS( ns(), fromjson( spec() ), err, false ) );
1046+
ASSERT( userCreateNS( ns(), fromjson( spec() ), false ).isOK() );
10481047
}
10491048
virtual string spec() const {
10501049
return "{\"capped\":true,\"size\":512,\"$nExtents\":1}";

src/mongo/dbtests/query_stage_collscan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ namespace QueryStageCollectionScan {
6161
stringstream spec;
6262
spec << "{\"capped\":true,\"size\":2000,\"$nExtents\":" << nExtents() << "}";
6363

64-
string err;
65-
ASSERT( userCreateNS( ns(), fromjson( spec.str() ), err, false ) );
64+
ASSERT( userCreateNS( ns(), fromjson( spec.str() ), false ).isOK() );
6665

6766
// Tell the test to add data/extents/etc.
6867
insertTestData();

0 commit comments

Comments
 (0)