Lines Matching refs:pCur
6732 SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int reset…
52926 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
52927 sqlite3BtreeEnter(pCur->pBtree);
52929 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
52930 sqlite3BtreeLeave(pCur->pBtree);
53510 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
53634 static void btreeReleaseAllCursorPages(BtCursor *pCur){
53636 for(i=0; i<=pCur->iPage; i++){
53637 releasePage(pCur->apPage[i]);
53638 pCur->apPage[i] = 0;
53640 pCur->iPage = -1;
53651 static int saveCursorPosition(BtCursor *pCur){
53654 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
53655 assert( 0==pCur->pKey );
53656 assert( cursorHoldsMutex(pCur) );
53658 if( pCur->eState==CURSOR_SKIPNEXT ){
53659 pCur->eState = CURSOR_VALID;
53661 pCur->skipNext = 0;
53663 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
53672 if( 0==pCur->apPage[0]->intKey ){
53673 void *pKey = sqlite3Malloc( pCur->nKey );
53675 rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
53677 pCur->pKey = pKey;
53685 assert( !pCur->apPage[0]->intKey || !pCur->pKey );
53688 btreeReleaseAllCursorPages(pCur);
53689 pCur->eState = CURSOR_REQUIRESEEK;
53692 invalidateOverflowCache(pCur);
53751 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
53752 assert( cursorHoldsMutex(pCur) );
53753 sqlite3_free(pCur->pKey);
53754 pCur->pKey = 0;
53755 pCur->eState = CURSOR_INVALID;
53764 BtCursor *pCur, /* Cursor open on the btree to be searched */
53778 pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
53781 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
53783 sqlite3DbFree(pCur->pKeyInfo->db, pFree);
53789 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
53791 sqlite3DbFree(pCur->pKeyInfo->db, pFree);
53803 static int btreeRestoreCursorPosition(BtCursor *pCur){
53806 assert( cursorHoldsMutex(pCur) );
53807 assert( pCur->eState>=CURSOR_REQUIRESEEK );
53808 if( pCur->eState==CURSOR_FAULT ){
53809 return pCur->skipNext;
53811 pCur->eState = CURSOR_INVALID;
53812 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
53814 sqlite3_free(pCur->pKey);
53815 pCur->pKey = 0;
53816 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
53817 pCur->skipNext |= skipNext;
53818 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
53819 pCur->eState = CURSOR_SKIPNEXT;
53842 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
53843 return pCur->eState!=CURSOR_VALID;
53859 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
53862 assert( pCur!=0 );
53863 assert( pCur->eState!=CURSOR_VALID );
53864 rc = restoreCursorPosition(pCur);
53869 if( pCur->eState!=CURSOR_VALID ){
53872 assert( pCur->skipNext==0 );
55315 BtCursor *pCur;
55320 pCur = pBt->pCursor;
55321 while( pCur ){
55322 BtCursor *pTmp = pCur;
55323 pCur = pCur->pNext;
55807 BtCursor *pCur;
55809 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
55810 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
55811 && pCur->eState!=CURSOR_FAULT ) r++;
56884 BtCursor *pCur /* Space for new cursor */
56917 pCur->pgnoRoot = (Pgno)iTable;
56918 pCur->iPage = -1;
56919 pCur->pKeyInfo = pKeyInfo;
56920 pCur->pBtree = p;
56921 pCur->pBt = pBt;
56923 pCur->curFlags = wrFlag;
56924 pCur->pNext = pBt->pCursor;
56925 if( pCur->pNext ){
56926 pCur->pNext->pPrev = pCur;
56928 pBt->pCursor = pCur;
56929 pCur->eState = CURSOR_INVALID;
56937 BtCursor *pCur /* Write new cursor here */
56941 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
56974 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
56975 Btree *pBtree = pCur->pBtree;
56978 BtShared *pBt = pCur->pBt;
56980 sqlite3BtreeClearCursor(pCur);
56981 if( pCur->pPrev ){
56982 pCur->pPrev->pNext = pCur->pNext;
56984 pBt->pCursor = pCur->pNext;
56986 if( pCur->pNext ){
56987 pCur->pNext->pPrev = pCur->pPrev;
56989 for(i=0; i<=pCur->iPage; i++){
56990 releasePage(pCur->apPage[i]);
56993 sqlite3_free(pCur->aOverflow);
57016 static void assertCellInfo(BtCursor *pCur){
57018 int iPage = pCur->iPage;
57020 btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
57021 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
57028 static void getCellInfo(BtCursor *pCur){
57029 if( pCur->info.nSize==0 ){
57030 int iPage = pCur->iPage;
57031 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
57032 pCur->curFlags |= BTCF_ValidNKey;
57034 assertCellInfo(pCur);
57039 #define getCellInfo(pCur) \
57040 if( pCur->info.nSize==0 ){ \
57041 int iPage = pCur->iPage; \
57042 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
57043 pCur->curFlags |= BTCF_ValidNKey; \
57045 assertCellInfo(pCur); \
57055 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
57056 return pCur && pCur->eState==CURSOR_VALID;
57072 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
57073 assert( cursorHoldsMutex(pCur) );
57074 assert( pCur->eState==CURSOR_VALID );
57075 getCellInfo(pCur);
57076 *pSize = pCur->info.nKey;
57092 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
57093 assert( cursorHoldsMutex(pCur) );
57094 assert( pCur->eState==CURSOR_VALID );
57095 assert( pCur->iPage>=0 );
57096 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
57097 assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
57098 getCellInfo(pCur);
57099 *pSize = pCur->info.nPayload;
57242 BtCursor *pCur, /* Cursor pointing to entry to read from */
57251 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
57252 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
57259 assert( pCur->eState==CURSOR_VALID );
57260 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
57261 assert( cursorHoldsMutex(pCur) );
57264 getCellInfo(pCur);
57265 aPayload = pCur->info.pPayload;
57267 bEnd = offset+amt==pCur->info.nPayload;
57269 assert( offset+amt <= pCur->info.nPayload );
57271 if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
57277 if( offset<pCur->info.nLocal ){
57279 if( a+offset>pCur->info.nLocal ){
57280 a = pCur->info.nLocal - offset;
57287 offset -= pCur->info.nLocal;
57295 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
57305 if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
57306 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
57307 if( nOvfl>pCur->nOvflAlloc ){
57309 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
57314 pCur->nOvflAlloc = nOvfl*2;
57315 pCur->aOverflow = aNew;
57319 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
57320 pCur->curFlags |= BTCF_ValidOvfl;
57328 if( (pCur->curFlags & BTCF_ValidOvfl)!=0
57329 && pCur->aOverflow[offset/ovflSize]
57332 nextPage = pCur->aOverflow[iIdx];
57339 if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
57340 assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
57341 pCur->aOverflow[iIdx] = nextPage;
57355 assert( pCur->curFlags & BTCF_ValidOvfl );
57356 assert( pCur->pBtree->db==pBt->db );
57357 if( pCur->aOverflow[iIdx+1] ){
57358 nextPage = pCur->aOverflow[iIdx+1];
57445 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
57446 assert( cursorHoldsMutex(pCur) );
57447 assert( pCur->eState==CURSOR_VALID );
57448 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
57449 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
57450 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
57462 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
57466 if ( pCur->eState==CURSOR_INVALID ){
57471 assert( cursorHoldsMutex(pCur) );
57472 rc = restoreCursorPosition(pCur);
57474 assert( pCur->eState==CURSOR_VALID );
57475 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
57476 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
57477 rc = accessPayload(pCur, offset, amt, pBuf, 0);
57502 BtCursor *pCur, /* Cursor pointing to entry to read from */
57506 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
57507 assert( pCur->eState==CURSOR_VALID );
57508 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
57509 assert( cursorHoldsMutex(pCur) );
57510 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
57511 assert( pCur->info.nSize>0 );
57512 assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
57513 assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
57514 amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
57515 if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
57517 return (void*)pCur->info.pPayload;
57535 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
57536 return fetchPayload(pCur, pAmt);
57538 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
57539 return fetchPayload(pCur, pAmt);
57552 static int moveToChild(BtCursor *pCur, u32 newPgno){
57554 int i = pCur->iPage;
57556 BtShared *pBt = pCur->pBt;
57558 assert( cursorHoldsMutex(pCur) );
57559 assert( pCur->eState==CURSOR_VALID );
57560 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
57561 assert( pCur->iPage>=0 );
57562 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
57566 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
57568 pCur->apPage[i+1] = pNewPage;
57569 pCur->aiIdx[i+1] = 0;
57570 pCur->iPage++;
57572 pCur->info.nSize = 0;
57573 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
57574 if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
57610 static void moveToParent(BtCursor *pCur){
57611 assert( cursorHoldsMutex(pCur) );
57612 assert( pCur->eState==CURSOR_VALID );
57613 assert( pCur->iPage>0 );
57614 assert( pCur->apPage[pCur->iPage] );
57616 pCur->apPage[pCur->iPage-1],
57617 pCur->aiIdx[pCur->iPage-1],
57618 pCur->apPage[pCur->iPage]->pgno
57620 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
57622 releasePage(pCur->apPage[pCur->iPage]);
57623 pCur->iPage--;
57624 pCur->info.nSize = 0;
57625 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
57649 static int moveToRoot(BtCursor *pCur){
57653 assert( cursorHoldsMutex(pCur) );
57657 if( pCur->eState>=CURSOR_REQUIRESEEK ){
57658 if( pCur->eState==CURSOR_FAULT ){
57659 assert( pCur->skipNext!=SQLITE_OK );
57660 return pCur->skipNext;
57662 sqlite3BtreeClearCursor(pCur);
57665 if( pCur->iPage>=0 ){
57666 while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
57667 }else if( pCur->pgnoRoot==0 ){
57668 pCur->eState = CURSOR_INVALID;
57671 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
57672 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
57674 pCur->eState = CURSOR_INVALID;
57677 pCur->iPage = 0;
57679 pRoot = pCur->apPage[0];
57680 assert( pRoot->pgno==pCur->pgnoRoot );
57693 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
57697 pCur->aiIdx[0] = 0;
57698 pCur->info.nSize = 0;
57699 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
57702 pCur->eState = CURSOR_VALID;
57707 pCur->eState = CURSOR_VALID;
57708 rc = moveToChild(pCur, subpage);
57710 pCur->eState = CURSOR_INVALID;
57722 static int moveToLeftmost(BtCursor *pCur){
57727 assert( cursorHoldsMutex(pCur) );
57728 assert( pCur->eState==CURSOR_VALID );
57729 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
57730 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
57731 pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
57732 rc = moveToChild(pCur, pgno);
57747 static int moveToRightmost(BtCursor *pCur){
57752 assert( cursorHoldsMutex(pCur) );
57753 assert( pCur->eState==CURSOR_VALID );
57754 while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
57756 pCur->aiIdx[pCur->iPage] = pPage->nCell;
57757 rc = moveToChild(pCur, pgno);
57760 pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
57761 assert( pCur->info.nSize==0 );
57762 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
57770 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
57773 assert( cursorHoldsMutex(pCur) );
57774 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
57775 rc = moveToRoot(pCur);
57777 if( pCur->eState==CURSOR_INVALID ){
57778 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
57781 assert( pCur->apPage[pCur->iPage]->nCell>0 );
57783 rc = moveToLeftmost(pCur);
57793 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
57796 assert( cursorHoldsMutex(pCur) );
57797 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
57800 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
57805 for(ii=0; ii<pCur->iPage; ii++){
57806 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
57808 assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
57809 assert( pCur->apPage[pCur->iPage]->leaf );
57814 rc = moveToRoot(pCur);
57816 if( CURSOR_INVALID==pCur->eState ){
57817 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
57820 assert( pCur->eState==CURSOR_VALID );
57822 rc = moveToRightmost(pCur);
57824 pCur->curFlags |= BTCF_AtLast;
57826 pCur->curFlags &= ~BTCF_AtLast;
57863 BtCursor *pCur, /* The cursor to be moved */
57872 assert( cursorHoldsMutex(pCur) );
57873 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
57875 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
57879 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
57880 && pCur->apPage[0]->intKey
57882 if( pCur->info.nKey==intKey ){
57886 if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
57903 rc = moveToRoot(pCur);
57907 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
57908 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
57909 assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
57910 if( pCur->eState==CURSOR_INVALID ){
57912 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
57915 assert( pCur->apPage[0]->intKey || pIdxKey );
57919 MemPage *pPage = pCur->apPage[pCur->iPage];
57934 pCur->aiIdx[pCur->iPage] = (u16)idx;
57953 pCur->curFlags |= BTCF_ValidNKey;
57954 pCur->info.nKey = nCellKey;
57955 pCur->aiIdx[pCur->iPage] = (u16)idx;
58002 btreeParseCellPtr(pPage, pCellBody, &pCur->info);
58003 nCell = (int)pCur->info.nKey;
58009 pCur->aiIdx[pCur->iPage] = (u16)idx;
58010 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
58020 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
58030 pCur->aiIdx[pCur->iPage] = (u16)idx;
58042 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
58043 pCur->aiIdx[pCur->iPage] = (u16)idx;
58054 pCur->aiIdx[pCur->iPage] = (u16)lwr;
58055 rc = moveToChild(pCur, chldPg);
58059 pCur->info.nSize = 0;
58060 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58072 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
58077 return (CURSOR_VALID!=pCur->eState);
58101 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
58106 assert( cursorHoldsMutex(pCur) );
58107 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
58109 if( pCur->eState!=CURSOR_VALID ){
58110 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
58111 rc = restoreCursorPosition(pCur);
58115 if( CURSOR_INVALID==pCur->eState ){
58119 if( pCur->skipNext ){
58120 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
58121 pCur->eState = CURSOR_VALID;
58122 if( pCur->skipNext>0 ){
58123 pCur->skipNext = 0;
58126 pCur->skipNext = 0;
58130 pPage = pCur->apPage[pCur->iPage];
58131 idx = ++pCur->aiIdx[pCur->iPage];
58143 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
58145 return moveToLeftmost(pCur);
58148 if( pCur->iPage==0 ){
58150 pCur->eState = CURSOR_INVALID;
58153 moveToParent(pCur);
58154 pPage = pCur->apPage[pCur->iPage];
58155 }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
58157 return sqlite3BtreeNext(pCur, pRes);
58165 return moveToLeftmost(pCur);
58168 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
58170 assert( cursorHoldsMutex(pCur) );
58173 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
58174 pCur->info.nSize = 0;
58175 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58177 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
58178 pPage = pCur->apPage[pCur->iPage];
58179 if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){
58180 pCur->aiIdx[pCur->iPage]--;
58181 return btreeNext(pCur, pRes);
58186 return moveToLeftmost(pCur);
58211 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){
58215 assert( cursorHoldsMutex(pCur) );
58218 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
58219 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
58220 assert( pCur->info.nSize==0 );
58221 if( pCur->eState!=CURSOR_VALID ){
58222 rc = restoreCursorPosition(pCur);
58226 if( CURSOR_INVALID==pCur->eState ){
58230 if( pCur->skipNext ){
58231 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
58232 pCur->eState = CURSOR_VALID;
58233 if( pCur->skipNext<0 ){
58234 pCur->skipNext = 0;
58237 pCur->skipNext = 0;
58241 pPage = pCur->apPage[pCur->iPage];
58244 int idx = pCur->aiIdx[pCur->iPage];
58245 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
58247 rc = moveToRightmost(pCur);
58249 while( pCur->aiIdx[pCur->iPage]==0 ){
58250 if( pCur->iPage==0 ){
58251 pCur->eState = CURSOR_INVALID;
58255 moveToParent(pCur);
58257 assert( pCur->info.nSize==0 );
58258 assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 );
58260 pCur->aiIdx[pCur->iPage]--;
58261 pPage = pCur->apPage[pCur->iPage];
58263 rc = sqlite3BtreePrevious(pCur, pRes);
58270 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
58271 assert( cursorHoldsMutex(pCur) );
58274 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
58276 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
58277 pCur->info.nSize = 0;
58278 if( pCur->eState!=CURSOR_VALID
58279 || pCur->aiIdx[pCur->iPage]==0
58280 || pCur->apPage[pCur->iPage]->leaf==0
58282 return btreePrevious(pCur, pRes);
58284 pCur->aiIdx[pCur->iPage]--;
60462 static int balance(BtCursor *pCur){
60464 const int nMin = pCur->pBt->usableSize * 2 / 3;
60472 int iPage = pCur->iPage;
60473 MemPage *pPage = pCur->apPage[iPage];
60483 rc = balance_deeper(pPage, &pCur->apPage[1]);
60485 pCur->iPage = 1;
60486 pCur->aiIdx[0] = 0;
60487 pCur->aiIdx[1] = 0;
60488 assert( pCur->apPage[1]->nOverflow );
60496 MemPage * const pParent = pCur->apPage[iPage-1];
60497 int const iIdx = pCur->aiIdx[iPage-1];
60543 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
60545 pCur->hints&BTREE_BULKLOAD);
60565 pCur->iPage--;
60566 assert( pCur->iPage>=0 );
60600 BtCursor *pCur, /* Insert data into the table of this cursor */
60612 Btree *p = pCur->pBtree;
60617 if( pCur->eState==CURSOR_FAULT ){
60618 assert( pCur->skipNext!=SQLITE_OK );
60619 return pCur->skipNext;
60622 assert( cursorHoldsMutex(pCur) );
60623 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
60626 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
60633 assert( (pKey==0)==(pCur->pKeyInfo==0) );
60646 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
60649 if( pCur->pKeyInfo==0 ){
60657 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
60658 && pCur->info.nKey==nKey-1 ){
60664 rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
60667 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
60669 pPage = pCur->apPage[pCur->iPage];
60674 pCur->pgnoRoot, nKey, nData, pPage->pgno,
60683 idx = pCur->aiIdx[pCur->iPage];
60700 idx = ++pCur->aiIdx[pCur->iPage];
60727 pCur->info.nSize = 0;
60729 pCur->curFlags &= ~(BTCF_ValidNKey);
60730 rc = balance(pCur);
60736 pCur->apPage[pCur->iPage]->nOverflow = 0;
60737 pCur->eState = CURSOR_INVALID;
60739 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
60749 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
60750 Btree *p = pCur->pBtree;
60759 assert( cursorHoldsMutex(pCur) );
60762 assert( pCur->curFlags & BTCF_WriteFlag );
60763 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
60764 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
60766 if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
60767 || NEVER(pCur->eState!=CURSOR_VALID)
60772 iCellDepth = pCur->iPage;
60773 iCellIdx = pCur->aiIdx[iCellDepth];
60774 pPage = pCur->apPage[iCellDepth];
60786 rc = sqlite3BtreePrevious(pCur, ¬Used);
60795 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
60800 if( pCur->pKeyInfo==0 ){
60801 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
60816 MemPage *pLeaf = pCur->apPage[pCur->iPage];
60818 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
60847 rc = balance(pCur);
60848 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
60849 while( pCur->iPage>iCellDepth ){
60850 releasePage(pCur->apPage[pCur->iPage--]);
60852 rc = balance(pCur);
60856 moveToRoot(pCur);
61116 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
61117 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
61335 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
61339 if( pCur->pgnoRoot==0 ){
61343 rc = moveToRoot(pCur);
61356 pPage = pCur->apPage[pCur->iPage];
61373 if( pCur->iPage==0 ){
61376 return moveToRoot(pCur);
61378 moveToParent(pCur);
61379 }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
61381 pCur->aiIdx[pCur->iPage]++;
61382 pPage = pCur->apPage[pCur->iPage];
61388 iIdx = pCur->aiIdx[pCur->iPage];
61390 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
61392 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
62228 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
62229 pCur->curFlags |= BTCF_Incrblob;
64037 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
64047 assert( sqlite3BtreeCursorIsValid(pCur) );
64054 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
64056 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
64068 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
64070 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
68773 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
68786 assert( sqlite3BtreeCursorIsValid(pCur) );
68787 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
68793 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
68859 BtCursor *pCur = pC->pCursor;
68862 assert( sqlite3BtreeCursorIsValid(pCur) );
68863 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
74108 VdbeCursor *pCur;
74113 pCur = p->apCsr[pOp->p1];
74114 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
74115 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
74180 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
74181 if( pCur==0 ) goto no_mem;
74182 pCur->nullRow = 1;
74183 pCur->isOrdered = 1;
74184 pCur->pgnoRoot = p2;
74185 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
74186 pCur->pKeyInfo = pKeyInfo;
74191 pCur->isTable = pOp->p4type!=P4_KEYINFO;
74196 sqlite3BtreeCursorHints(pCur->pCursor,
76916 VdbeCursor *pCur;
76922 pCur = 0;
76937 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
76938 if( pCur ){
76939 pCur->pVtabCursor = pVtabCursor;
76979 VdbeCursor *pCur;
76986 pCur = p->apCsr[pOp->p1];
76989 assert( pCur->pVtabCursor );
76990 pVtabCursor = pCur->pVtabCursor;
77010 pCur->nullRow = 0;
77031 VdbeCursor *pCur = p->apCsr[pOp->p1];
77032 assert( pCur->pVtabCursor );
77036 if( pCur->nullRow ){
77040 pVtab = pCur->pVtabCursor->pVtab;
77046 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
77073 VdbeCursor *pCur;
77076 pCur = p->apCsr[pOp->p1];
77077 assert( pCur->pVtabCursor );
77078 if( pCur->nullRow ){
77081 pVtab = pCur->pVtabCursor->pVtab;
77091 rc = pModule->xNext(pCur->pVtabCursor);
77094 res = pModule->xEof(pCur->pVtabCursor);
152112 static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
152113 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
152119 static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
152121 int ii = 1 - pCur->bPoint;
152123 assert( pCur->bPoint || pCur->nPoint );
152124 if( pCur->aNode[ii]==0 ){
152126 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
152127 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
152129 return pCur->aNode[ii];
152136 RtreeCursor *pCur, /* The cursor */
152142 if( pCur->nPoint>=pCur->nPointAlloc ){
152143 int nNew = pCur->nPointAlloc*2 + 8;
152144 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
152146 pCur->aPoint = pNew;
152147 pCur->nPointAlloc = nNew;
152149 i = pCur->nPoint++;
152150 pNew = pCur->aPoint + i;
152157 pParent = pCur->aPoint + j;
152159 rtreeSearchPointSwap(pCur, j, i);
152171 RtreeCursor *pCur, /* The cursor */
152176 pFirst = rtreeSearchPointFirst(pCur);
152177 pCur->anQueue[iLevel]++;
152182 if( pCur->bPoint ){
152184 pNew = rtreeEnqueue(pCur, rScore, iLevel);
152186 ii = (int)(pNew - pCur->aPoint) + 1;
152188 assert( pCur->aNode[ii]==0 );
152189 pCur->aNode[ii] = pCur->aNode[0];
152191 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
152193 pCur->aNode[0] = 0;
152194 *pNew = pCur->sPoint;
152196 pCur->sPoint.rScore = rScore;
152197 pCur->sPoint.iLevel = iLevel;
152198 pCur->bPoint = 1;
152199 return &pCur->sPoint;
152201 return rtreeEnqueue(pCur, rScore, iLevel);
152207 static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
152214 printf(" %p\n", pCur->aNode[idx]);
152219 static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
152222 if( pCur->bPoint ){
152223 tracePoint(&pCur->sPoint, -1, pCur);
152225 for(ii=0; ii<pCur->nPoint; ii++){
152226 if( ii>0 || pCur->bPoint ) printf(" ");
152227 tracePoint(&pCur->aPoint[ii], ii, pCur);
152285 static int rtreeStepToLeaf(RtreeCursor *pCur){
152287 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
152292 int nConstraint = pCur->nConstraint;
152298 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
152299 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
152308 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
152331 RTREE_QUEUE_TRACE(pCur, "POP-S:");
152332 rtreeSearchPointPop(pCur);
152335 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
152340 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
152344 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
152345 rtreeSearchPointPop(pCur);
152348 pCur->atEOF = p==0;