diff options
author | gregs <greg@10gen.com> | 2011-03-18 13:04:38 -0400 |
---|---|---|
committer | gregs <greg@10gen.com> | 2011-03-30 19:20:18 -0400 |
commit | 69cf8bee0b7ae3135560a1baa5d7f292995129c6 (patch) | |
tree | f95ed3a000e5fa40fd672fea6e07cad16cbe254e /s | |
parent | 95c0b3b5cfe834f6f6251adb7c4bb92e09125565 (diff) | |
download | mongo-69cf8bee0b7ae3135560a1baa5d7f292995129c6.tar.gz |
fix for SERVER-2326, unique : true semantics
Diffstat (limited to 's')
-rw-r--r-- | s/commands_admin.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/s/commands_admin.cpp b/s/commands_admin.cpp index 439428d8290..525be305a7e 100644 --- a/s/commands_admin.cpp +++ b/s/commands_admin.cpp @@ -397,9 +397,12 @@ namespace mongo { // // We enforce both these conditions in what comes next. + bool careAboutUnique = cmdObj["unique"].trueValue(); + { ShardKeyPattern proposedKey( key ); bool hasShardIndex = false; + bool hasUniqueShardIndex = false; ScopedDbConnection conn( config->getPrimary() ); BSONObjBuilder b; @@ -409,13 +412,21 @@ namespace mongo { while ( cursor->more() ) { BSONObj idx = cursor->next(); + log() << "Index: " << idx << endl; + + bool idIndex = ! idx["name"].eoo() && idx["name"].String() == "_id_"; + bool uniqueIndex = ( ! idx["unique"].eoo() && idx["unique"].trueValue() ) || + idIndex; + // Is index key over the sharding key? Remember that. if ( key.woCompare( idx["key"].embeddedObjectUserCheck() ) == 0 ) { hasShardIndex = true; + hasUniqueShardIndex = uniqueIndex; + continue; } // Not a unique index? Move on. - if ( idx["unique"].eoo() || ! idx["unique"].trueValue() ) + if ( ! uniqueIndex || idIndex ) continue; // Shard key is prefix of unique index? Move on. @@ -427,6 +438,12 @@ namespace mongo { return false; } + if( careAboutUnique && hasShardIndex && ! hasUniqueShardIndex ){ + errmsg = (string)"can't shard collection " + ns + ", index not unique"; + conn.done(); + return false; + } + BSONObj res = conn->findOne( config->getName() + ".system.namespaces" , BSON( "name" << ns ) ); if ( res["options"].type() == Object && res["options"].embeddedObject()["capped"].trueValue() ) { errmsg = "can't shard capped collection"; @@ -458,7 +475,7 @@ namespace mongo { tlog() << "CMD: shardcollection: " << cmdObj << endl; - config->shardCollection( ns , key , cmdObj["unique"].trueValue() ); + config->shardCollection( ns , key , careAboutUnique ); result << "collectionsharded" << ns; return true; |