Skip to content

Commit

Permalink
fix _id lookup with no index SERVER-608
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Feb 6, 2010
1 parent 188436f commit 2dc3d5b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
6 changes: 3 additions & 3 deletions db/dbhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ namespace mongo {
return true;
}

bool Helpers::findById(const char *ns, BSONObj query, BSONObj& result ){
int Helpers::findById(const char *ns, BSONObj query, BSONObj& result ){
NamespaceDetails *d = nsdetails(ns);
if ( ! d )
return false;
return -1;
int idxNo = d->findIdIndex();
if ( idxNo < 0 )
return false;
return -1;
IndexDetails& i = d->idx( idxNo );

BSONObj key = i.getKeyFromQuery( query );
Expand Down
7 changes: 6 additions & 1 deletion db/dbhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ namespace mongo {
*/
static bool findOne(const char *ns, BSONObj query, BSONObj& result, bool requireIndex = false);

static bool findById(const char *ns, BSONObj query, BSONObj& result );
/**
-1 no index
0 not found
1 found
*/
static int findById(const char *ns, BSONObj query, BSONObj& result );

/* Get/put the first object from a collection. Generally only useful if the collection
only ever has a single object -- which is a "singleton collection".
Expand Down
35 changes: 20 additions & 15 deletions db/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,26 +771,31 @@ namespace mongo {
uassert("bad query object", false);
}

int idxHackWorked = false;
if ( strcmp( query.firstElement().fieldName() , "_id" ) == 0 && query.nFields() == 1 && query.firstElement().isSimpleType() ){
nscanned = 1;

BSONObj resObject;
bool found = Helpers::findById( ns , query , resObject );
if ( found ){
n = 1;
fillQueryResultFromObj( bb , filter.get() , resObject );
}
qr.reset( (QueryResult *) bb.buf() );
bb.decouple();
qr->resultFlags() = 0;
qr->len = bb.len();
ss << " reslen:" << bb.len();
qr->setOperation(opReply);
qr->cursorId = cursorid;
qr->startingFrom = 0;
qr->nReturned = n;
int found = Helpers::findById( ns , query , resObject );
if ( found >= 0 ){
idxHackWorked = true;
if ( found ){
n = 1;
fillQueryResultFromObj( bb , filter.get() , resObject );
}
qr.reset( (QueryResult *) bb.buf() );
bb.decouple();
qr->resultFlags() = 0;
qr->len = bb.len();
ss << " reslen:" << bb.len();
qr->setOperation(opReply);
qr->cursorId = cursorid;
qr->startingFrom = 0;
qr->nReturned = n;
}
}
else { // non-simple _id lookup

if ( ! idxHackWorked ){
BSONObj oldPlan;
if ( explain && hint.eoo() && min.isEmpty() && max.isEmpty() ) {
QueryPlanSet qps( ns, query, order );
Expand Down

0 comments on commit 2dc3d5b

Please sign in to comment.