diff options
author | Randolph Tan <randolph@10gen.com> | 2014-03-27 17:18:10 -0400 |
---|---|---|
committer | Randolph Tan <randolph@10gen.com> | 2014-03-28 11:40:44 -0400 |
commit | 1cfdc9f9e8571d529b6cc2681d89567835946911 (patch) | |
tree | 37e54f7a8200f5c3245601dc48654270a0174acc | |
parent | 32514385e1b48abced71a661d6ade2f8b7b619c4 (diff) | |
download | mongo-1cfdc9f9e8571d529b6cc2681d89567835946911.tar.gz |
SERVER-13387 CreateIndexes command allows non shard key unique indexes to be created
(cherry picked from commit a924eeb8934297f2a499bb0e50cd6835b1741281)
-rw-r--r-- | src/mongo/db/commands/create_indexes.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/db/commands/create_indexes.cpp b/src/mongo/db/commands/create_indexes.cpp index 2eb0cebe0ca..48a2546e276 100644 --- a/src/mongo/db/commands/create_indexes.cpp +++ b/src/mongo/db/commands/create_indexes.cpp @@ -35,6 +35,8 @@ #include "mongo/db/commands.h" #include "mongo/db/ops/insert.h" #include "mongo/db/repl/oplog.h" +#include "mongo/s/d_logic.h" +#include "mongo/s/shard_key_pattern.h" namespace mongo { @@ -173,6 +175,15 @@ namespace mongo { for ( size_t i = 0; i < specs.size(); i++ ) { BSONObj spec = specs[i]; + if ( spec["unique"].trueValue() ) { + status = checkUniqueIndexConstraints( ns.ns(), spec["key"].Obj() ); + + if ( !status.isOK() ) { + appendCommandStatus( result, status ); + return false; + } + } + status = collection->getIndexCatalog()->createIndex( spec, true ); if ( status.code() == ErrorCodes::IndexAlreadyExists ) { if ( !result.hasField( "note" ) ) @@ -195,6 +206,27 @@ namespace mongo { return true; } + private: + static Status checkUniqueIndexConstraints(const StringData& ns, + const BSONObj& newIdxKey) { + Lock::assertWriteLocked( ns ); + + if ( shardingState.enabled() ) { + CollectionMetadataPtr metadata( + shardingState.getCollectionMetadata( ns.toString() )); + + if ( metadata ) { + BSONObj shardKey(metadata->getKeyPattern()); + if ( !isUniqueIndexCompatible( shardKey, newIdxKey )) { + return Status(ErrorCodes::CannotCreateIndex, + str::stream() << "cannot create unique index over " << newIdxKey + << " with shard key pattern " << shardKey); + } + } + } + + return Status::OK(); + } } cmdCreateIndex; |