Lines Matching refs:pCur

8682 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
63175 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
63176 sqlite3BtreeEnter(pCur->pBtree);
63179 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
63180 sqlite3BtreeLeave(pCur->pBtree);
63690 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
63817 static void btreeReleaseAllCursorPages(BtCursor *pCur){
63819 if( pCur->iPage>=0 ){
63820 for(i=0; i<pCur->iPage; i++){
63821 releasePageNotNull(pCur->apPage[i]);
63823 releasePageNotNull(pCur->pPage);
63824 pCur->iPage = -1;
63841 static int saveCursorKey(BtCursor *pCur){
63843 assert( CURSOR_VALID==pCur->eState );
63844 assert( 0==pCur->pKey );
63845 assert( cursorHoldsMutex(pCur) );
63847 if( pCur->curIntKey ){
63849 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
63858 pCur->nKey = sqlite3BtreePayloadSize(pCur);
63859 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
63861 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
63863 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
63864 pCur->pKey = pKey;
63872 assert( !pCur->curIntKey || !pCur->pKey );
63883 static int saveCursorPosition(BtCursor *pCur){
63886 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
63887 assert( 0==pCur->pKey );
63888 assert( cursorHoldsMutex(pCur) );
63890 if( pCur->eState==CURSOR_SKIPNEXT ){
63891 pCur->eState = CURSOR_VALID;
63893 pCur->skipNext = 0;
63896 rc = saveCursorKey(pCur);
63898 btreeReleaseAllCursorPages(pCur);
63899 pCur->eState = CURSOR_REQUIRESEEK;
63902 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
63972 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
63973 assert( cursorHoldsMutex(pCur) );
63974 sqlite3_free(pCur->pKey);
63975 pCur->pKey = 0;
63976 pCur->eState = CURSOR_INVALID;
63985 BtCursor *pCur, /* Cursor open on the btree to be searched */
63995 KeyInfo *pKeyInfo = pCur->pKeyInfo;
64007 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
64010 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
64022 static int btreeRestoreCursorPosition(BtCursor *pCur){
64025 assert( cursorOwnsBtShared(pCur) );
64026 assert( pCur->eState>=CURSOR_REQUIRESEEK );
64027 if( pCur->eState==CURSOR_FAULT ){
64028 return pCur->skipNext;
64030 pCur->eState = CURSOR_INVALID;
64034 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
64037 sqlite3_free(pCur->pKey);
64038 pCur->pKey = 0;
64039 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
64040 if( skipNext ) pCur->skipNext = skipNext;
64041 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
64042 pCur->eState = CURSOR_SKIPNEXT;
64065 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
64066 assert( EIGHT_BYTE_ALIGNMENT(pCur)
64067 || pCur==sqlite3BtreeFakeValidCursor() );
64069 assert( sizeof(pCur->eState)==1 );
64070 return CURSOR_VALID != *(u8*)pCur;
64097 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
64100 assert( pCur!=0 );
64101 assert( pCur->eState!=CURSOR_VALID );
64102 rc = restoreCursorPosition(pCur);
64107 if( pCur->eState!=CURSOR_VALID ){
64121 SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
64129 SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
64131 pCur->hints = x;
65344 BtCursor *pCur, /* Cursor to receive the page, or NULL */
65350 assert( pCur==0 || ppPage==&pCur->pPage );
65351 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
65352 assert( pCur==0 || pCur->iPage>0 );
65375 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
65384 if( pCur ){
65385 pCur->iPage--;
65386 pCur->pPage = pCur->apPage[pCur->iPage];
65886 BtCursor *pCur;
65891 pCur = pBt->pCursor;
65892 while( pCur ){
65893 BtCursor *pTmp = pCur;
65894 pCur = pCur->pNext;
66429 BtCursor *pCur;
66431 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
66432 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
66433 && pCur->eState!=CURSOR_FAULT ) r++;
67532 BtCursor *pCur /* Space for new cursor */
67567 pCur->pgnoRoot = (Pgno)iTable;
67568 pCur->iPage = -1;
67569 pCur->pKeyInfo = pKeyInfo;
67570 pCur->pBtree = p;
67571 pCur->pBt = pBt;
67572 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
67573 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
67579 pCur->curFlags |= BTCF_Multiple;
67582 pCur->pNext = pBt->pCursor;
67583 pBt->pCursor = pCur;
67584 pCur->eState = CURSOR_INVALID;
67592 BtCursor *pCur /* Write new cursor here */
67599 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
67633 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
67634 Btree *pBtree = pCur->pBtree;
67636 BtShared *pBt = pCur->pBt;
67639 if( pBt->pCursor==pCur ){
67640 pBt->pCursor = pCur->pNext;
67644 if( pPrev->pNext==pCur ){
67645 pPrev->pNext = pCur->pNext;
67651 btreeReleaseAllCursorPages(pCur);
67653 sqlite3_free(pCur->aOverflow);
67654 sqlite3_free(pCur->pKey);
67656 pCur->pBtree = 0;
67678 static void assertCellInfo(BtCursor *pCur){
67681 btreeParseCell(pCur->pPage, pCur->ix, &info);
67682 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
67687 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
67688 if( pCur->info.nSize==0 ){
67689 pCur->curFlags |= BTCF_ValidNKey;
67690 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
67692 assertCellInfo(pCur);
67702 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
67703 return pCur && pCur->eState==CURSOR_VALID;
67706 SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
67707 assert( pCur!=0 );
67708 return pCur->eState==CURSOR_VALID;
67717 SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
67718 assert( cursorHoldsMutex(pCur) );
67719 assert( pCur->eState==CURSOR_VALID );
67720 assert( pCur->curIntKey );
67721 getCellInfo(pCur);
67722 return pCur->info.nKey;
67730 SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
67731 assert( cursorHoldsMutex(pCur) );
67732 assert( pCur->eState==CURSOR_VALID );
67733 getCellInfo(pCur);
67734 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
67735 (i64)(pCur->info.pPayload - pCur->pPage->aData);
67748 SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
67749 assert( cursorHoldsMutex(pCur) );
67750 assert( pCur->eState==CURSOR_VALID );
67751 getCellInfo(pCur);
67752 return pCur->info.nPayload;
67768 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
67769 assert( cursorHoldsMutex(pCur) );
67770 assert( pCur->eState==CURSOR_VALID );
67771 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
67912 BtCursor *pCur, /* Cursor pointing to entry to read from */
67921 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
67922 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
67929 assert( pCur->eState==CURSOR_VALID );
67930 assert( pCur->ix<pPage->nCell );
67931 assert( cursorHoldsMutex(pCur) );
67933 getCellInfo(pCur);
67934 aPayload = pCur->info.pPayload;
67935 assert( offset+amt <= pCur->info.nPayload );
67938 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
67948 if( offset<pCur->info.nLocal ){
67950 if( a+offset>pCur->info.nLocal ){
67951 a = pCur->info.nLocal - offset;
67958 offset -= pCur->info.nLocal;
67966 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
67975 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
67976 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
67977 if( pCur->aOverflow==0
67978 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
67981 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
67986 pCur->aOverflow = aNew;
67989 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
67990 pCur->curFlags |= BTCF_ValidOvfl;
67996 if( pCur->aOverflow[offset/ovflSize] ){
67998 nextPage = pCur->aOverflow[iIdx];
68006 assert( pCur->aOverflow[iIdx]==0
68007 || pCur->aOverflow[iIdx]==nextPage
68009 pCur->aOverflow[iIdx] = nextPage;
68018 assert( pCur->curFlags & BTCF_ValidOvfl );
68019 assert( pCur->pBtree->db==pBt->db );
68020 if( pCur->aOverflow[iIdx+1] ){
68021 nextPage = pCur->aOverflow[iIdx+1];
68111 SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
68112 assert( cursorHoldsMutex(pCur) );
68113 assert( pCur->eState==CURSOR_VALID );
68114 assert( pCur->iPage>=0 && pCur->pPage );
68115 assert( pCur->ix<pCur->pPage->nCell );
68116 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
68126 BtCursor *pCur,
68132 if ( pCur->eState==CURSOR_INVALID ){
68135 assert( cursorOwnsBtShared(pCur) );
68136 rc = btreeRestoreCursorPosition(pCur);
68137 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
68139 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
68140 if( pCur->eState==CURSOR_VALID ){
68141 assert( cursorOwnsBtShared(pCur) );
68142 return accessPayload(pCur, offset, amt, pBuf, 0);
68144 return accessPayloadChecked(pCur, offset, amt, pBuf);
68169 BtCursor *pCur, /* Cursor pointing to entry to read from */
68173 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
68174 assert( pCur->eState==CURSOR_VALID );
68175 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68176 assert( cursorOwnsBtShared(pCur) );
68177 assert( pCur->ix<pCur->pPage->nCell );
68178 assert( pCur->info.nSize>0 );
68179 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
68180 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
68181 amt = pCur->info.nLocal;
68182 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
68186 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
68189 return (void*)pCur->info.pPayload;
68207 SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
68208 return fetchPayload(pCur, pAmt);
68221 static int moveToChild(BtCursor *pCur, u32 newPgno){
68222 BtShared *pBt = pCur->pBt;
68224 assert( cursorOwnsBtShared(pCur) );
68225 assert( pCur->eState==CURSOR_VALID );
68226 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
68227 assert( pCur->iPage>=0 );
68228 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
68231 pCur->info.nSize = 0;
68232 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
68233 pCur->aiIdx[pCur->iPage] = pCur->ix;
68234 pCur->apPage[pCur->iPage] = pCur->pPage;
68235 pCur->ix = 0;
68236 pCur->iPage++;
68237 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
68270 static void moveToParent(BtCursor *pCur){
68272 assert( cursorOwnsBtShared(pCur) );
68273 assert( pCur->eState==CURSOR_VALID );
68274 assert( pCur->iPage>0 );
68275 assert( pCur->pPage );
68277 pCur->apPage[pCur->iPage-1],
68278 pCur->aiIdx[pCur->iPage-1],
68279 pCur->pPage->pgno
68281 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
68282 pCur->info.nSize = 0;
68283 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
68284 pCur->ix = pCur->aiIdx[pCur->iPage-1];
68285 pLeaf = pCur->pPage;
68286 pCur->pPage = pCur->apPage[--pCur->iPage];
68311 static int moveToRoot(BtCursor *pCur){
68315 assert( cursorOwnsBtShared(pCur) );
68319 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
68320 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
68322 if( pCur->iPage>=0 ){
68323 if( pCur->iPage ){
68324 releasePageNotNull(pCur->pPage);
68325 while( --pCur->iPage ){
68326 releasePageNotNull(pCur->apPage[pCur->iPage]);
68328 pCur->pPage = pCur->apPage[0];
68331 }else if( pCur->pgnoRoot==0 ){
68332 pCur->eState = CURSOR_INVALID;
68335 assert( pCur->iPage==(-1) );
68336 if( pCur->eState>=CURSOR_REQUIRESEEK ){
68337 if( pCur->eState==CURSOR_FAULT ){
68338 assert( pCur->skipNext!=SQLITE_OK );
68339 return pCur->skipNext;
68341 sqlite3BtreeClearCursor(pCur);
68343 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
68344 0, pCur->curPagerFlags);
68346 pCur->eState = CURSOR_INVALID;
68349 pCur->iPage = 0;
68350 pCur->curIntKey = pCur->pPage->intKey;
68352 pRoot = pCur->pPage;
68353 assert( pRoot->pgno==pCur->pgnoRoot );
68366 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
68367 return SQLITE_CORRUPT_PAGE(pCur->pPage);
68371 pCur->ix = 0;
68372 pCur->info.nSize = 0;
68373 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
68375 pRoot = pCur->pPage;
68377 pCur->eState = CURSOR_VALID;
68382 pCur->eState = CURSOR_VALID;
68383 rc = moveToChild(pCur, subpage);
68385 pCur->eState = CURSOR_INVALID;
68398 static int moveToLeftmost(BtCursor *pCur){
68403 assert( cursorOwnsBtShared(pCur) );
68404 assert( pCur->eState==CURSOR_VALID );
68405 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
68406 assert( pCur->ix<pPage->nCell );
68407 pgno = get4byte(findCell(pPage, pCur->ix));
68408 rc = moveToChild(pCur, pgno);
68423 static int moveToRightmost(BtCursor *pCur){
68428 assert( cursorOwnsBtShared(pCur) );
68429 assert( pCur->eState==CURSOR_VALID );
68430 while( !(pPage = pCur->pPage)->leaf ){
68432 pCur->ix = pPage->nCell;
68433 rc = moveToChild(pCur, pgno);
68436 pCur->ix = pPage->nCell-1;
68437 assert( pCur->info.nSize==0 );
68438 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
68446 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
68449 assert( cursorOwnsBtShared(pCur) );
68450 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68451 rc = moveToRoot(pCur);
68453 assert( pCur->pPage->nCell>0 );
68455 rc = moveToLeftmost(pCur);
68457 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68468 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
68471 assert( cursorOwnsBtShared(pCur) );
68472 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68475 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
68480 for(ii=0; ii<pCur->iPage; ii++){
68481 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
68483 assert( pCur->ix==pCur->pPage->nCell-1 );
68484 assert( pCur->pPage->leaf );
68489 rc = moveToRoot(pCur);
68491 assert( pCur->eState==CURSOR_VALID );
68493 rc = moveToRightmost(pCur);
68495 pCur->curFlags |= BTCF_AtLast;
68497 pCur->curFlags &= ~BTCF_AtLast;
68500 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68538 BtCursor *pCur, /* The cursor to be moved */
68547 assert( cursorOwnsBtShared(pCur) );
68548 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68550 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
68551 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
68556 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
68558 if( pCur->info.nKey==intKey ){
68562 if( pCur->info.nKey<intKey ){
68563 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
68571 if( pCur->info.nKey+1==intKey ){
68573 rc = sqlite3BtreeNext(pCur, 0);
68575 getCellInfo(pCur);
68576 if( pCur->info.nKey==intKey ){
68599 rc = moveToRoot(pCur);
68602 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68608 assert( pCur->pPage );
68609 assert( pCur->pPage->isInit );
68610 assert( pCur->eState==CURSOR_VALID );
68611 assert( pCur->pPage->nCell > 0 );
68612 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
68613 assert( pCur->curIntKey || pIdxKey );
68617 MemPage *pPage = pCur->pPage;
68632 pCur->ix = (u16)idx;
68653 pCur->ix = (u16)idx;
68658 pCur->curFlags |= BTCF_ValidNKey;
68659 pCur->info.nKey = nCellKey;
68660 pCur->info.nSize = 0;
68707 pPage->xParseCell(pPage, pCellBody, &pCur->info);
68708 nCell = (int)pCur->info.nKey;
68713 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
68722 pCur->ix = (u16)idx;
68723 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
68724 pCur->curFlags &= ~BTCF_ValidOvfl;
68734 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
68744 pCur->ix = (u16)idx;
68756 assert( pCur->ix<pCur->pPage->nCell );
68757 pCur->ix = (u16)idx;
68768 pCur->ix = (u16)lwr;
68769 rc = moveToChild(pCur, chldPg);
68773 pCur->info.nSize = 0;
68774 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
68786 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
68791 return (CURSOR_VALID!=pCur->eState);
68799 SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
68803 assert( cursorOwnsBtShared(pCur) );
68804 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68809 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
68810 if( NEVER(pCur->pPage->leaf==0) ) return -1;
68812 n = pCur->pPage->nCell;
68813 for(i=0; i<pCur->iPage; i++){
68814 n *= pCur->apPage[i]->nCell;
68839 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
68844 assert( cursorOwnsBtShared(pCur) );
68845 if( pCur->eState!=CURSOR_VALID ){
68846 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
68847 rc = restoreCursorPosition(pCur);
68851 if( CURSOR_INVALID==pCur->eState ){
68854 if( pCur->eState==CURSOR_SKIPNEXT ){
68855 pCur->eState = CURSOR_VALID;
68856 if( pCur->skipNext>0 ) return SQLITE_OK;
68860 pPage = pCur->pPage;
68861 idx = ++pCur->ix;
68882 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
68884 return moveToLeftmost(pCur);
68887 if( pCur->iPage==0 ){
68888 pCur->eState = CURSOR_INVALID;
68891 moveToParent(pCur);
68892 pPage = pCur->pPage;
68893 }while( pCur->ix>=pPage->nCell );
68895 return sqlite3BtreeNext(pCur, 0);
68903 return moveToLeftmost(pCur);
68906 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
68909 assert( cursorOwnsBtShared(pCur) );
68911 pCur->info.nSize = 0;
68912 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
68913 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
68914 pPage = pCur->pPage;
68915 if( (++pCur->ix)>=pPage->nCell ){
68916 pCur->ix--;
68917 return btreeNext(pCur);
68922 return moveToLeftmost(pCur);
68946 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
68950 assert( cursorOwnsBtShared(pCur) );
68951 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
68952 assert( pCur->info.nSize==0 );
68953 if( pCur->eState!=CURSOR_VALID ){
68954 rc = restoreCursorPosition(pCur);
68958 if( CURSOR_INVALID==pCur->eState ){
68961 if( CURSOR_SKIPNEXT==pCur->eState ){
68962 pCur->eState = CURSOR_VALID;
68963 if( pCur->skipNext<0 ) return SQLITE_OK;
68967 pPage = pCur->pPage;
68970 int idx = pCur->ix;
68971 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
68973 rc = moveToRightmost(pCur);
68975 while( pCur->ix==0 ){
68976 if( pCur->iPage==0 ){
68977 pCur->eState = CURSOR_INVALID;
68980 moveToParent(pCur);
68982 assert( pCur->info.nSize==0 );
68983 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
68985 pCur->ix--;
68986 pPage = pCur->pPage;
68988 rc = sqlite3BtreePrevious(pCur, 0);
68995 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
68996 assert( cursorOwnsBtShared(pCur) );
68999 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
69000 pCur->info.nSize = 0;
69001 if( pCur->eState!=CURSOR_VALID
69002 || pCur->ix==0
69003 || pCur->pPage->leaf==0
69005 return btreePrevious(pCur);
69007 pCur->ix--;
71471 static int balance(BtCursor *pCur){
71473 const int nMin = pCur->pBt->usableSize * 2 / 3;
71481 int iPage = pCur->iPage;
71482 MemPage *pPage = pCur->pPage;
71494 rc = balance_deeper(pPage, &pCur->apPage[1]);
71496 pCur->iPage = 1;
71497 pCur->ix = 0;
71498 pCur->aiIdx[0] = 0;
71499 pCur->apPage[0] = pPage;
71500 pCur->pPage = pCur->apPage[1];
71501 assert( pCur->pPage->nOverflow );
71509 MemPage * const pParent = pCur->apPage[iPage-1];
71510 int const iIdx = pCur->aiIdx[iPage-1];
71560 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
71562 pCur->hints&BTREE_BULKLOAD);
71582 pCur->iPage--;
71583 assert( pCur->iPage>=0 );
71584 pCur->pPage = pCur->apPage[pCur->iPage];
71640 static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
71644 MemPage *pPage = pCur->pPage; /* Page being written */
71649 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
71653 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
71654 0, pCur->info.nLocal);
71656 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
71659 iOffset = pCur->info.nLocal;
71662 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
71718 BtCursor *pCur, /* Insert data into the table of this cursor */
71728 Btree *p = pCur->pBtree;
71735 if( pCur->eState==CURSOR_FAULT ){
71736 assert( pCur->skipNext!=SQLITE_OK );
71737 return pCur->skipNext;
71740 assert( cursorOwnsBtShared(pCur) );
71741 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
71744 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
71751 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
71764 if( pCur->curFlags & BTCF_Multiple ){
71765 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
71769 if( pCur->pKeyInfo==0 ){
71773 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
71780 assert( pCur->curFlags & BTCF_ValidNKey );
71781 assert( pX->nKey==pCur->info.nKey );
71782 assert( pCur->info.nSize!=0 );
71791 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
71795 if( pCur->info.nSize!=0
71796 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
71799 return btreeOverwriteCell(pCur, pX);
71807 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
71826 r.pKeyInfo = pCur->pKeyInfo;
71834 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
71836 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
71846 getCellInfo(pCur);
71847 if( pCur->info.nKey==pX->nKey ){
71852 return btreeOverwriteCell(pCur, &x2);
71857 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
71859 pPage = pCur->pPage;
71868 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
71877 idx = pCur->ix;
71911 idx = ++pCur->ix;
71912 pCur->curFlags &= ~BTCF_ValidNKey;
71940 pCur->info.nSize = 0;
71943 pCur->curFlags &= ~(BTCF_ValidNKey);
71944 rc = balance(pCur);
71950 pCur->pPage->nOverflow = 0;
71951 pCur->eState = CURSOR_INVALID;
71953 btreeReleaseAllCursorPages(pCur);
71954 if( pCur->pKeyInfo ){
71955 assert( pCur->pKey==0 );
71956 pCur->pKey = sqlite3Malloc( pX->nKey );
71957 if( pCur->pKey==0 ){
71960 memcpy(pCur->pKey, pX->pKey, pX->nKey);
71963 pCur->eState = CURSOR_REQUIRESEEK;
71964 pCur->nKey = pX->nKey;
71967 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
71990 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
71991 Btree *p = pCur->pBtree;
72002 assert( cursorOwnsBtShared(pCur) );
72005 assert( pCur->curFlags & BTCF_WriteFlag );
72006 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
72007 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
72009 if( pCur->eState==CURSOR_REQUIRESEEK ){
72010 rc = btreeRestoreCursorPosition(pCur);
72013 assert( pCur->eState==CURSOR_VALID );
72015 iCellDepth = pCur->iPage;
72016 iCellIdx = pCur->ix;
72017 pPage = pCur->pPage;
72037 rc = saveCursorKey(pCur);
72052 rc = sqlite3BtreePrevious(pCur, 0);
72059 if( pCur->curFlags & BTCF_Multiple ){
72060 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
72066 if( pCur->pKeyInfo==0 ){
72067 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
72085 MemPage *pLeaf = pCur->pPage;
72094 if( iCellDepth<pCur->iPage-1 ){
72095 n = pCur->apPage[iCellDepth+1]->pgno;
72097 n = pCur->pPage->pgno;
72128 rc = balance(pCur);
72129 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
72130 releasePageNotNull(pCur->pPage);
72131 pCur->iPage--;
72132 while( pCur->iPage>iCellDepth ){
72133 releasePage(pCur->apPage[pCur->iPage--]);
72135 pCur->pPage = pCur->apPage[pCur->iPage];
72136 rc = balance(pCur);
72141 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
72142 assert( pPage==pCur->pPage || CORRUPT_DB );
72144 pCur->eState = CURSOR_SKIPNEXT;
72146 pCur->skipNext = -1;
72147 pCur->ix = pPage->nCell-1;
72149 pCur->skipNext = 1;
72152 rc = moveToRoot(pCur);
72154 btreeReleaseAllCursorPages(pCur);
72155 pCur->eState = CURSOR_REQUIRESEEK;
72420 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
72421 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
72621 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
72625 rc = moveToRoot(pCur);
72642 pPage = pCur->pPage;
72659 if( pCur->iPage==0 ){
72662 return moveToRoot(pCur);
72664 moveToParent(pCur);
72665 }while ( pCur->ix>=pCur->pPage->nCell );
72667 pCur->ix++;
72668 pPage = pCur->pPage;
72674 iIdx = pCur->ix;
72676 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
72678 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
73536 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
73537 pCur->curFlags |= BTCF_Incrblob;
73538 pCur->pBtree->hasIncrblobCur = 1;
75541 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
75548 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
75552 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
75564 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
75573 assert( sqlite3BtreeCursorIsValid(pCur) );
75579 zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
75587 rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
80959 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
80972 assert( sqlite3BtreeCursorIsValid(pCur) );
80973 nCellKey = sqlite3BtreePayloadSize(pCur);
80978 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
81046 BtCursor *pCur;
81050 pCur = pC->uc.pCursor;
81051 assert( sqlite3BtreeCursorIsValid(pCur) );
81052 nCellKey = sqlite3BtreePayloadSize(pCur);
81060 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
87083 VdbeCursor *pCur;
87088 pCur = p->apCsr[pOp->p1];
87089 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
87090 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
87153 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
87154 if( pCur==0 ) goto no_mem;
87155 pCur->nullRow = 1;
87156 pCur->isOrdered = 1;
87157 pCur->pgnoRoot = p2;
87159 pCur->wrFlag = wrFlag;
87161 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
87162 pCur->pKeyInfo = pKeyInfo;
87167 pCur->isTable = pOp->p4type!=P4_KEYINFO;
87176 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
90470 VdbeCursor *pCur;
90476 pCur = 0;
90492 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
90493 if( pCur ){
90494 pCur->uc.pVCur = pVCur;
90533 VdbeCursor *pCur;
90540 pCur = p->apCsr[pOp->p1];
90543 assert( pCur->eCurType==CURTYPE_VTAB );
90544 pVCur = pCur->uc.pVCur;
90563 pCur->nullRow = 0;
90591 VdbeCursor *pCur = p->apCsr[pOp->p1];
90592 assert( pCur->eCurType==CURTYPE_VTAB );
90596 if( pCur->nullRow ){
90600 pVtab = pCur->uc.pVCur->pVtab;
90613 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
90642 VdbeCursor *pCur;
90645 pCur = p->apCsr[pOp->p1];
90646 assert( pCur->eCurType==CURTYPE_VTAB );
90647 if( pCur->nullRow ){
90650 pVtab = pCur->uc.pVCur->pVtab;
90660 rc = pModule->xNext(pCur->uc.pVCur);
90663 res = pModule->xEof(pCur->uc.pVCur);
180635 JsonEachCursor *pCur;
180638 pCur = sqlite3_malloc( sizeof(*pCur) );
180639 if( pCur==0 ) return SQLITE_NOMEM;
180640 memset(pCur, 0, sizeof(*pCur));
180641 *ppCursor = &pCur->base;
180649 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
180650 pCur->bRecursive = 1;
182527 static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
182528 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
182534 static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
182536 int ii = 1 - pCur->bPoint;
182538 assert( pCur->bPoint || pCur->nPoint );
182539 if( pCur->aNode[ii]==0 ){
182541 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
182542 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
182544 return pCur->aNode[ii];
182551 RtreeCursor *pCur, /* The cursor */
182557 if( pCur->nPoint>=pCur->nPointAlloc ){
182558 int nNew = pCur->nPointAlloc*2 + 8;
182559 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
182561 pCur->aPoint = pNew;
182562 pCur->nPointAlloc = nNew;
182564 i = pCur->nPoint++;
182565 pNew = pCur->aPoint + i;
182572 pParent = pCur->aPoint + j;
182574 rtreeSearchPointSwap(pCur, j, i);
182586 RtreeCursor *pCur, /* The cursor */
182591 pFirst = rtreeSearchPointFirst(pCur);
182592 pCur->anQueue[iLevel]++;
182597 if( pCur->bPoint ){
182599 pNew = rtreeEnqueue(pCur, rScore, iLevel);
182601 ii = (int)(pNew - pCur->aPoint) + 1;
182603 assert( pCur->aNode[ii]==0 );
182604 pCur->aNode[ii] = pCur->aNode[0];
182606 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
182608 pCur->aNode[0] = 0;
182609 *pNew = pCur->sPoint;
182611 pCur->sPoint.rScore = rScore;
182612 pCur->sPoint.iLevel = iLevel;
182613 pCur->bPoint = 1;
182614 return &pCur->sPoint;
182616 return rtreeEnqueue(pCur, rScore, iLevel);
182622 static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
182629 printf(" %p\n", pCur->aNode[idx]);
182634 static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
182637 if( pCur->bPoint ){
182638 tracePoint(&pCur->sPoint, -1, pCur);
182640 for(ii=0; ii<pCur->nPoint; ii++){
182641 if( ii>0 || pCur->bPoint ) printf(" ");
182642 tracePoint(&pCur->aPoint[ii], ii, pCur);
182700 static int rtreeStepToLeaf(RtreeCursor *pCur){
182702 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
182707 int nConstraint = pCur->nConstraint;
182713 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
182714 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
182723 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
182746 RTREE_QUEUE_TRACE(pCur, "POP-S:");
182747 rtreeSearchPointPop(pCur);
182750 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
182755 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
182759 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
182760 rtreeSearchPointPop(pCur);
182763 pCur->atEOF = p==0;
222684 stmt_cursor *pCur;
222685 pCur = sqlite3_malloc( sizeof(*pCur) );
222686 if( pCur==0 ) return SQLITE_NOMEM;
222687 memset(pCur, 0, sizeof(*pCur));
222688 pCur->db = ((stmt_vtab*)p)->db;
222689 *ppCursor = &pCur->base;
222706 stmt_cursor *pCur = (stmt_cursor*)cur;
222707 pCur->iRowid++;
222708 pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
222721 stmt_cursor *pCur = (stmt_cursor*)cur;
222724 sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
222728 sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
222732 sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
222736 sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
222750 sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
222763 stmt_cursor *pCur = (stmt_cursor*)cur;
222764 *pRowid = pCur->iRowid;
222773 stmt_cursor *pCur = (stmt_cursor*)cur;
222774 return pCur->pStmt==0;
222788 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
222789 pCur->pStmt = 0;
222790 pCur->iRowid = 0;