0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

better adding of sort key to projection in mongos SERVER-1601

This commit is contained in:
Mathias Stearn 2010-08-09 22:14:06 -04:00
parent c227068d0d
commit b2569f1650
2 changed files with 50 additions and 33 deletions

View File

@ -331,30 +331,39 @@ namespace mongo {
if ( ! _sortKey.isEmpty() && ! _fields.isEmpty() ){
// we need to make sure the sort key is in the project
bool isNegative = false;
set<string> sortKeyFields;
_sortKey.getFieldNames(sortKeyFields);
BSONObjBuilder b;
bool isNegative = false;
{
BSONObjIterator i( _fields );
while ( i.more() ){
BSONElement e = i.next();
b.append( e );
if ( ! e.trueValue() )
string fieldName = e.fieldName();
// exact field
bool found = sortKeyFields.erase(fieldName);
// subfields
set<string>::const_iterator begin = sortKeyFields.lower_bound(fieldName + ".\x00");
set<string>::const_iterator end = sortKeyFields.lower_bound(fieldName + ".\xFF");
sortKeyFields.erase(begin, end);
if ( ! e.trueValue() ) {
uassert( 13431 , "have to have sort key in projection and removing it" , !found && begin == end );
} else if (!e.isABSONObj()) {
isNegative = true;
}
}
}
{
BSONObjIterator i( _sortKey );
while ( i.more() ){
BSONElement e = i.next();
BSONElement f = _fields.getField( e.fieldName() );
if ( isNegative ){
uassert( 13431 , "have to have sort key in projection and removing it" , f.eoo() );
}
else if ( f.eoo() ){
// add to projection
b.append( e );
}
if (isNegative){
for (set<string>::const_iterator it(sortKeyFields.begin()), end(sortKeyFields.end()); it != end; ++it){
b.append(*it, 1);
}
}

View File

@ -2,7 +2,7 @@
s = new ShardingTest( "sort1" , 2 , 0 , 2 )
s.adminCommand( { enablesharding : "test" } );
s.adminCommand( { shardcollection : "test.data" , key : { num : 1 } } );
s.adminCommand( { shardcollection : "test.data" , key : { 'sub.num' : 1 } } );
db = s.getDB( "test" );
@ -11,16 +11,16 @@ N = 100
forward = []
backward = []
for ( i=0; i<N; i++ ){
db.data.insert( { _id : i , num : i , x : N - i } )
db.data.insert( { _id : i , sub: {num : i , x : N - i }} )
forward.push( i )
backward.push( ( N - 1 ) - i )
}
db.getLastError();
s.adminCommand( { split : "test.data" , middle : { num : 33 } } )
s.adminCommand( { split : "test.data" , middle : { num : 66 } } )
s.adminCommand( { split : "test.data" , middle : { 'sub.num' : 33 } } )
s.adminCommand( { split : "test.data" , middle : { 'sub.num' : 66 } } )
s.adminCommand( { movechunk : "test.data" , find : { num : 50 } , to : s.getOther( s.getServer( "test" ) ).name } );
s.adminCommand( { movechunk : "test.data" , find : { 'sub.num' : 50 } , to : s.getOther( s.getServer( "test" ) ).name } );
assert.eq( 3 , s.config.chunks.find().itcount() , "A1" );
@ -28,31 +28,31 @@ temp = s.config.chunks.find().sort( { min : 1 } ).toArray();
assert.eq( temp[0].shard , temp[2].shard , "A2" );
assert.neq( temp[0].shard , temp[1].shard , "A3" );
temp = db.data.find().sort( { num : 1 } ).toArray();
temp = db.data.find().sort( { 'sub.num' : 1 } ).toArray();
assert.eq( N , temp.length , "B1" );
for ( i=0; i<100; i++ ){
assert.eq( i , temp[i].num , "B2" )
assert.eq( i , temp[i].sub.num , "B2" )
}
db.data.find().sort( { num : 1 } ).toArray();
s.getServer("test").getDB( "test" ).data.find().sort( { num : 1 } ).toArray();
db.data.find().sort( { 'sub.num' : 1 } ).toArray();
s.getServer("test").getDB( "test" ).data.find().sort( { 'sub.num' : 1 } ).toArray();
a = Date.timeFunc( function(){ z = db.data.find().sort( { num : 1 } ).toArray(); } , 200 );
a = Date.timeFunc( function(){ z = db.data.find().sort( { 'sub.num' : 1 } ).toArray(); } , 200 );
assert.eq( 100 , z.length , "C1" )
b = 1.5 * Date.timeFunc( function(){ z = s.getServer("test").getDB( "test" ).data.find().sort( { num : 1 } ).toArray(); } , 200 );
b = 1.5 * Date.timeFunc( function(){ z = s.getServer("test").getDB( "test" ).data.find().sort( { 'sub.num' : 1 } ).toArray(); } , 200 );
assert.eq( 67 , z.length , "C2" )
print( "a: " + a + " b:" + b + " mongos slow down: " + Math.ceil( 100 * ( ( a - b ) / b ) ) + "%" )
// -- secondary index sorting
function getSorted( by , want , dir , proj ){
function getSorted( by , dir , proj ){
var s = {}
s[by] = dir || 1;
printjson( s )
var cur = db.data.find( {} , proj || {} ).sort( s )
return terse( cur.map( function(z){ return z[want]; } ) );
return terse( cur.map( function(z){ return z.sub.num; } ) );
}
function terse( a ){
@ -68,14 +68,22 @@ function terse( a ){
forward = terse(forward);
backward = terse(backward);
assert.eq( forward , getSorted( "num" , "num" , 1 ) , "D1" )
assert.eq( backward , getSorted( "num" , "num" , -1 ) , "D2" )
assert.eq( forward , getSorted( "sub.num" , 1 ) , "D1" )
assert.eq( backward , getSorted( "sub.num" , -1 ) , "D2" )
assert.eq( backward , getSorted( "x" , "num" , 1 ) , "D3" )
assert.eq( forward , getSorted( "x" , "num" , -1 ) , "D4" )
assert.eq( backward , getSorted( "sub.x" , 1 ) , "D3" )
assert.eq( forward , getSorted( "sub.x" , -1 ) , "D4" )
assert.eq( backward , getSorted( "x" , "num" , 1 , { num : 1 } ) , "D5" )
assert.eq( forward , getSorted( "x" , "num" , -1 , { num : 1 } ) , "D6" )
assert.eq( backward , getSorted( "sub.x" , 1 , { 'sub.num' : 1 } ) , "D5" )
assert.eq( forward , getSorted( "sub.x" , -1 , { 'sub.num' : 1 } ) , "D6" )
assert.eq( backward , getSorted( "sub.x" , 1 , { 'sub' : 1 } ) , "D7" )
assert.eq( forward , getSorted( "sub.x" , -1 , { 'sub' : 1 } ) , "D8" )
assert.eq( backward , getSorted( "sub.x" , 1 , { '_id' : 0 } ) , "D9" )
assert.eq( forward , getSorted( "sub.x" , -1 , { '_id' : 0 } ) , "D10" )
assert.eq( backward , getSorted( "sub.x" , 1 , { '_id' : 0, 'sub.num':1 } ) , "D11" )
assert.eq( forward , getSorted( "sub.x" , -1 , { '_id' : 0, 'sub.num':1 } ) , "D12" )
s.stop();