summaryrefslogtreecommitdiff
path: root/src/mongo/s/shardkey.cpp
diff options
context:
space:
mode:
authorKevin Matulef <matulef@10gen.com>2012-10-10 18:10:13 -0400
committerKevin Matulef <matulef@10gen.com>2012-10-10 18:14:40 -0400
commit532a051330efa57bcf2f27ab597f0ab3130715a1 (patch)
tree1db91ffbed79d561a900f78aad59bc246ec04fbd /src/mongo/s/shardkey.cpp
parenta50b5956fc17d1f6fdb49c1b2c718bd39ef1aaf8 (diff)
downloadmongo-532a051330efa57bcf2f27ab597f0ab3130715a1.tar.gz
SERVER-2001 change shard key validation to allow hashed shard keys
This changes the top-level shard key validation to allow shard keys such as {a : "hashed"}. It also adds some helper functions for determining when a unique index is compatible with a given shard key, and a variety of unit tests and a js test.
Diffstat (limited to 'src/mongo/s/shardkey.cpp')
-rw-r--r--src/mongo/s/shardkey.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/mongo/s/shardkey.cpp b/src/mongo/s/shardkey.cpp
index d9930b63070..83338a71fdc 100644
--- a/src/mongo/s/shardkey.cpp
+++ b/src/mongo/s/shardkey.cpp
@@ -61,6 +61,14 @@ namespace mongo {
return pattern.isPrefixOf( otherPattern );
}
+ bool ShardKeyPattern::isUniqueIndexCompatible( const BSONObj& uniqueIndexPattern ) const {
+ if ( ! uniqueIndexPattern.isEmpty() &&
+ str::equals( uniqueIndexPattern.firstElementFieldName(), "_id" ) ){
+ return true;
+ }
+ return pattern.isFieldNamePrefixOf( uniqueIndexPattern );
+ }
+
bool ShardKeyPattern::isSpecial() const {
BSONForEach(e, pattern) {
int fieldVal = e.numberInt();
@@ -210,6 +218,18 @@ namespace mongo {
}
+ void uniqueIndexCompatibleTest() {
+ ShardKeyPattern k1( BSON( "a" << 1 ) );
+ verify( k1.isUniqueIndexCompatible( BSON( "_id" << 1 ) ) );
+ verify( k1.isUniqueIndexCompatible( BSON( "a" << 1 << "b" << 1 ) ) );
+ verify( k1.isUniqueIndexCompatible( BSON( "a" << -1 ) ) );
+ verify( ! k1.isUniqueIndexCompatible( BSON( "b" << 1 ) ) );
+
+ ShardKeyPattern k2( BSON( "a" << "hashed") );
+ verify( k2.isUniqueIndexCompatible( BSON( "a" << 1 ) ) );
+ verify( ! k2.isUniqueIndexCompatible( BSON( "b" << 1 ) ) );
+ }
+
void moveToFrontBenchmark(int numFields) {
BSONObjBuilder bb;
bb.append("_id", 1);
@@ -263,6 +283,8 @@ namespace mongo {
moveToFrontTest();
+ uniqueIndexCompatibleTest();
+
if (0) { // toggle to run benchmark
moveToFrontBenchmark(0);
moveToFrontBenchmark(10);