Skip to content

Commit

Permalink
SERVER-1185 A bit of prep-ing before the actual changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Lerner committed Jul 1, 2010
1 parent 76bb389 commit 105ab90
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions s/commands_admin.cpp
Expand Up @@ -351,11 +351,11 @@ namespace mongo {
BSONObjBuilder b;
b.append( "ns" , ns );
b.appendBool( "unique" , true );

auto_ptr<DBClientCursor> cursor = conn->query( config->getName() + ".system.indexes" , b.obj() );
while ( cursor->more() ){
BSONObj idx = cursor->next();
if ( proposedKey.uniqueAllowd( idx["key"].embeddedObjectUserCheck() ) )
if ( proposedKey.isPrefixOf( idx["key"].embeddedObjectUserCheck() ) )
continue;
errmsg = (string)"can't shard collection with unique index on: " + idx.toString();
conn.done();
Expand Down
24 changes: 13 additions & 11 deletions s/shardkey.cpp
Expand Up @@ -145,7 +145,7 @@ namespace mongo {
return dir;
}

bool ShardKeyPattern::uniqueAllowd( const BSONObj& otherPattern ) const {
bool ShardKeyPattern::isPrefixOf( const BSONObj& otherPattern ) const {
BSONObjIterator a( pattern );
BSONObjIterator b( otherPattern );

Expand All @@ -172,20 +172,20 @@ namespace mongo {
class ShardKeyUnitTest : public UnitTest {
public:

void testUniqueAllowd(){
void testIsPrefixOf(){
{
ShardKeyPattern k( BSON( "x" << 1 ) );
assert( ! k.uniqueAllowd( BSON( "a" << 1 ) ) );
assert( k.uniqueAllowd( BSON( "x" << 1 ) ) );
assert( k.uniqueAllowd( BSON( "x" << 1 << "a" << 1 ) ) );
assert( ! k.uniqueAllowd( BSON( "a" << 1 << "x" << 1 ) ) );
assert( ! k.isPrefixOf( BSON( "a" << 1 ) ) );
assert( k.isPrefixOf( BSON( "x" << 1 ) ) );
assert( k.isPrefixOf( BSON( "x" << 1 << "a" << 1 ) ) );
assert( ! k.isPrefixOf( BSON( "a" << 1 << "x" << 1 ) ) );
}
{
ShardKeyPattern k( BSON( "x" << 1 << "y" << 1 ) );
assert( ! k.uniqueAllowd( BSON( "x" << 1 ) ) );
assert( ! k.uniqueAllowd( BSON( "x" << 1 << "z" << 1 ) ) );
assert( k.uniqueAllowd( BSON( "x" << 1 << "y" << 1 ) ) );
assert( k.uniqueAllowd( BSON( "x" << 1 << "y" << 1 << "z" << 1 ) ) );
assert( ! k.isPrefixOf( BSON( "x" << 1 ) ) );
assert( ! k.isPrefixOf( BSON( "x" << 1 << "z" << 1 ) ) );
assert( k.isPrefixOf( BSON( "x" << 1 << "y" << 1 ) ) );
assert( k.isPrefixOf( BSON( "x" << 1 << "y" << 1 << "z" << 1 ) ) );
}
}

Expand Down Expand Up @@ -259,8 +259,10 @@ namespace mongo {

testCanOrder();
getfilt();
testUniqueAllowd();
testIsPrefixOf();
// add middle multitype tests

log(1) << "shardKeyTest passed" << endl;
}
} shardKeyTest;

Expand Down
8 changes: 6 additions & 2 deletions s/shardkey.h
Expand Up @@ -97,8 +97,12 @@ namespace mongo {
bool partOfShardKey(const string& key ) const {
return patternfields.count( key ) > 0;
}

bool uniqueAllowd( const BSONObj& otherPattern ) const;

/**
* @return
* true if 'this' is a prefix (not necessarily contained) of 'otherPattern'.
*/
bool isPrefixOf( const BSONObj& otherPattern ) const;

operator string() const {
return pattern.toString();
Expand Down
2 changes: 1 addition & 1 deletion s/strategy_single.cpp
Expand Up @@ -107,7 +107,7 @@ namespace mongo {
" key: " + o["key"].embeddedObjectUserCheck().toString() ,
IndexDetails::isIdIndexPattern( newIndexKey ) ||
! o["unique"].trueValue() ||
r.getConfig()->getChunkManager( ns )->getShardKey().uniqueAllowd( newIndexKey ) );
r.getConfig()->getChunkManager( ns )->getShardKey().isPrefixOf( newIndexKey ) );

ChunkManagerPtr cm = r.getConfig()->getChunkManager( ns );
assert( cm );
Expand Down

0 comments on commit 105ab90

Please sign in to comment.