0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00

Initialize cursor based on query in certain sorting cases where sorting is specified (fix bug 1079).

This commit is contained in:
Aaron Staple 2008-12-04 08:18:07 -05:00
parent fa3ec70ca3
commit 0caf3b7529

View File

@ -96,6 +96,37 @@ auto_ptr<Cursor> getIndexCursor(const char *ns, BSONObj& query, BSONObj& order,
DEV cout << " using index " << d->indexes[i].indexNamespace() << '\n';
if( isSorted )
*isSorted = true;
// Initialize cursor using query, in the following simple cases.
// (quick fix for bug 1079 )
if ( orderFields.size() == 1 ) {
string field = *orderFields.begin();
BSONElement e = query.getField( field.c_str() );
if ( !e.eoo() && e.type() != RegEx ) {
int op = getGtLtOp( e );
if ( ( op == JSMatcher::Equality ) ||
( ( op == JSMatcher::LT || op == JSMatcher::LTE ) && reverse ) ||
( ( op == JSMatcher::GT || op == JSMatcher::GTE ) && !reverse ) ) {
BSONObjBuilder b;
if ( op == JSMatcher::Equality ) {
b.append( e );
} else {
BSONElement ee = e.embeddedObject().firstElement();
b.appendAs( ee, e.fieldName() );
}
BSONObj q = b.doneAndDecouple();
auto_ptr<Cursor> c( new BtreeCursor(d->indexes[i], q, reverse ? -1 : 1, query) );
if ( op == JSMatcher::GT || op == JSMatcher::LT )
for( ; c->ok(); c->advance() ) {
BSONElement f = c->current().getField( field.c_str() );
if ( f.eoo() || !( q.firstElement() == f ) )
break;
}
return c;
}
}
}
return auto_ptr<Cursor>(new BtreeCursor(d->indexes[i], reverse ? maxKey : emptyObj, reverse ? -1 : 1, query));
}
}