Index: src/btree.c ================================================================== --- src/btree.c +++ src/btree.c @@ -7,11 +7,11 @@ ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.343 2007/03/27 14:05:23 drh Exp $ +** $Id: btree.c,v 1.344 2007/03/29 04:43:26 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to ** ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: @@ -3303,16 +3303,14 @@ ** *pRes>0 The cursor is left pointing at an entry that ** is larger than pKey. */ int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ int rc; - int tryRightmost; rc = moveToRoot(pCur); if( rc ) return rc; assert( pCur->pPage ); assert( pCur->pPage->isInit ); - tryRightmost = pCur->pPage->intKey; if( pCur->eState==CURSOR_INVALID ){ *pRes = -1; assert( pCur->pPage->nCell==0 ); return SQLITE_OK; } @@ -3324,20 +3322,17 @@ lwr = 0; upr = pPage->nCell-1; if( !pPage->intKey && pKey==0 ){ return SQLITE_CORRUPT_BKPT; } - while( lwr<=upr ){ + pCur->idx = upr; + if( lwr<=upr ) for(;;){ void *pCellKey; i64 nCellKey; - pCur->idx = (lwr+upr)/2; pCur->info.nSize = 0; if( pPage->intKey ){ u8 *pCell; - if( tryRightmost ){ - pCur->idx = upr; - } pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; if( pPage->hasData ){ u32 dummy; pCell += getVarint32(pCell, &dummy); } @@ -3344,11 +3339,10 @@ getVarint(pCell, (u64 *)&nCellKey); if( nCellKeynKey ){ c = +1; - tryRightmost = 0; }else{ c = 0; } }else{ int available; @@ -3378,10 +3372,14 @@ if( c<0 ){ lwr = pCur->idx+1; }else{ upr = pCur->idx-1; } + if( lwr>upr ){ + break; + } + pCur->idx = (lwr+upr)/2; } assert( lwr==upr+1 ); assert( pPage->isInit ); if( pPage->leaf ){ chldPg = 0;