Lines Matching full:pcur
6819 ** ^The current value of the requested parameter is written into *pCur
6829 SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
54395 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
54396 sqlite3BtreeEnter(pCur->pBtree);
54398 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
54399 sqlite3BtreeLeave(pCur->pBtree);
54980 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
55106 static void btreeReleaseAllCursorPages(BtCursor *pCur){
55108 for(i=0; i<=pCur->iPage; i++){
55109 releasePage(pCur->apPage[i]);
55110 pCur->apPage[i] = 0;
55112 pCur
55118 ** function saves the current cursor key in variables pCur->nKey and
55119 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
55123 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
55124 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
55125 ** set to point to a malloced buffer pCur->nKey bytes in size containing
55128 static int saveCursorKey(BtCursor *pCur){
55130 assert( CURSOR_VALID==pCur->eState );
55131 assert( 0==pCur->pKey );
55132 assert( cursorHoldsMutex(pCur) );
55134 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
55138 ** stores the integer key in pCur->nKey. In this case this value is
55139 ** all that is required. Otherwise, if pCur is not open on an intKey
55140 ** table, then malloc space for and store the pCur->nKey bytes of key
55142 if( 0==pCur->curIntKey ){
55143 void *pKey = sqlite3Malloc( pCur->nKey );
55145 rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
55147 pCur->pKey = pKey;
55155 assert( !pCur->curIntKey || !pCur->pKey );
55166 static int saveCursorPosition(BtCursor *pCur){
55169 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
55170 assert( 0==pCur->pKey );
55171 assert( cursorHoldsMutex(pCur) );
55173 if( pCur->eState==CURSOR_SKIPNEXT ){
55174 pCur->eState = CURSOR_VALID;
55176 pCur->skipNext = 0;
55179 rc = saveCursorKey(pCur);
55181 btreeReleaseAllCursorPages(pCur);
55182 pCur->eState = CURSOR_REQUIRESEEK;
55185 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
55255 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
55256 assert( cursorHoldsMutex(pCur) );
55257 sqlite3_free(pCur->pKey);
55258 pCur->pKey = 0;
55259 pCur->eState = CURSOR_INVALID;
55268 BtCursor *pCur, /* Cursor open on the btree to be searched */
55282 pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
55285 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
55287 sqlite3DbFree(pCur->pKeyInfo->db, pFree);
55293 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
55295 sqlite3DbFree(pCur->pKeyInfo->db, pFree);
55307 static int btreeRestoreCursorPosition(BtCursor *pCur){
55310 assert( cursorHoldsMutex(pCur) );
55311 assert( pCur->eState>=CURSOR_REQUIRESEEK );
55312 if( pCur->eState==CURSOR_FAULT ){
55313 return pCur->skipNext;
55315 pCur->eState = CURSOR_INVALID;
55316 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
55318 sqlite3_free(pCur->pKey);
55319 pCur->pKey = 0;
55320 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
55321 pCur->skipNext |= skipNext;
55322 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
55323 pCur->eState = CURSOR_SKIPNEXT;
55346 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
55347 return pCur->eState!=CURSOR_VALID;
55363 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
55366 assert( pCur!=0 );
55367 assert( pCur->eState!=CURSOR_VALID );
55368 rc = restoreCursorPosition(pCur);
55373 if( pCur->eState!=CURSOR_VALID ){
55376 assert( pCur->skipNext==0 );
56472 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
56474 ** And if the fetch fails, this routine must decrement pCur->iPage.
56476 ** The page is fetched as read-write unless pCur is not NULL and is
56486 BtCursor *pCur, /* Cursor to receive the page, or NULL */
56492 assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
56493 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
56494 assert( pCur==0 || pCur->iPage>0 );
56515 if( pCur
56516 && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey)
56525 if( pCur ) pCur->iPage--;
57002 BtCursor *pCur;
57007 pCur = pBt->pCursor;
57008 while( pCur ){
57009 BtCursor *pTmp = pCur;
57010 pCur = pCur->pNext;
57494 BtCursor *pCur;
57496 pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
57497 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
57498 && pCur->eState!=CURSOR_FAULT ) r++;
58566 ** on pCur to initialize the memory space prior to invoking this routine.
58573 BtCursor *pCur /* Space for new cursor */
58605 pCur->pgnoRoot = (Pgno)iTable;
58606 pCur->iPage = -1;
58607 pCur->pKeyInfo = pKeyInfo;
58608 pCur->pBtree = p;
58609 pCur->pBt = pBt;
58611 pCur->curFlags = wrFlag;
58612 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
58618 pCur->curFlags |= BTCF_Multiple;
58621 pCur->pNext = pBt->pCursor;
58622 pBt->pCursor = pCur;
58623 pCur->eState = CURSOR_INVALID;
58631 BtCursor *pCur /* Write new cursor here */
58638 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
58672 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
58673 Btree *pBtree = pCur->pBtree;
58676 BtShared *pBt = pCur->pBt;
58678 sqlite3BtreeClearCursor(pCur);
58680 if( pBt->pCursor==pCur ){
58681 pBt->pCursor = pCur->pNext;
58685 if( pPrev->pNext==pCur ){
58686 pPrev->pNext = pCur->pNext;
58692 for(i=0; i<=pCur->iPage; i++){
58693 releasePage(pCur->apPage[i]);
58696 sqlite3_free(pCur->aOverflow);
58697 /* sqlite3_free(pCur); */
58712 static void assertCellInfo(BtCursor *pCur){
58714 int iPage = pCur->iPage;
58716 btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
58717 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
58722 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
58723 if( pCur->info.nSize==0 ){
58724 int iPage = pCur->iPage;
58725 pCur->curFlags |= BTCF_ValidNKey;
58726 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
58728 assertCellInfo(pCur);
58738 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
58739 return pCur && pCur->eState==CURSOR_VALID;
58755 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
58756 assert( cursorHoldsMutex(pCur) );
58757 assert( pCur->eState==CURSOR_VALID );
58758 getCellInfo(pCur);
58759 *pSize = pCur->info.nKey;
58775 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
58776 assert( cursorHoldsMutex(pCur) );
58777 assert( pCur->eState==CURSOR_VALID );
58778 assert( pCur->iPage>=0 );
58779 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
58780 assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
58781 getCellInfo(pCur);
58782 *pSize = pCur->info.nPayload;
58896 ** for the entry that the pCur cursor is pointing to. The eOp
58925 BtCursor *pCur, /* Cursor pointing to entry to read from */
58934 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
58935 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
58942 assert( pCur->eState==CURSOR_VALID );
58943 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
58944 assert( cursorHoldsMutex(pCur) );
58947 getCellInfo(pCur);
58948 aPayload = pCur->info.pPayload;
58950 bEnd = offset+amt==pCur->info.nPayload;
58952 assert( offset+amt <= pCur->info.nPayload );
58954 if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
58960 if( offset<pCur->info.nLocal ){
58962 if( a+offset>pCur->info.nLocal ){
58963 a = pCur->info.nLocal - offset;
58970 offset -= pCur->info.nLocal;
58978 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
58988 if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
58989 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
58990 if( nOvfl>pCur->nOvflAlloc ){
58992 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
58997 pCur->nOvflAlloc = nOvfl*2;
58998 pCur->aOverflow = aNew;
59002 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
59003 pCur->curFlags |= BTCF_ValidOvfl;
59011 if( (pCur->curFlags & BTCF_ValidOvfl)!=0
59012 && pCur->aOverflow[offset/ovflSize]
59015 nextPage = pCur->aOverflow[iIdx];
59022 if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
59023 assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
59024 pCur->aOverflow[iIdx] = nextPage;
59038 assert( pCur->curFlags & BTCF_ValidOvfl );
59039 assert( pCur->pBtree->db==pBt->db );
59040 if( pCur->aOverflow[iIdx+1] ){
59041 nextPage = pCur->aOverflow[iIdx+1];
59117 ** Read part of the key associated with cursor pCur. Exactly
59121 ** The caller must ensure that pCur is pointing to a valid row
59128 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
59129 assert( cursorHoldsMutex(pCur) );
59130 assert( pCur->eState==CURSOR_VALID );
59131 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
59132 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
59133 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
59137 ** Read part of the data associated with cursor pCur. Exactly
59145 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
59149 if ( pCur->eState==CURSOR_INVALID ){
59154 assert( cursorHoldsMutex(pCur) );
59155 rc = restoreCursorPosition(pCur);
59157 assert( pCur->eState==CURSOR_VALID );
59158 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
59159 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
59160 rc = accessPayload(pCur, offset, amt, pBuf, 0);
59167 ** pCur cursor is pointing to. The pointer is to the beginning of
59185 BtCursor *pCur, /* Cursor pointing to entry to read from */
59189 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
59190 assert( pCur->eState==CURSOR_VALID );
59191 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
59192 assert( cursorHoldsMutex(pCur) );
59193 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
59194 assert( pCur->info.nSize>0 );
59195 assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
59196 assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
59197 amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
59198 if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
59200 return (void*)pCur->info.pPayload;
59205 ** For the entry that cursor pCur is point to, return as
59218 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
59219 return fetchPayload(pCur, pAmt);
59221 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
59222 return fetchPayload(pCur, pAmt);
59235 static int moveToChild(BtCursor *pCur, u32 newPgno){
59236 BtShared *pBt = pCur->pBt;
59238 assert( cursorHoldsMutex(pCur) );
59239 assert( pCur->eState==CURSOR_VALID );
59240 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
59241 assert( pCur->iPage>=0 );
59242 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
59245 pCur->info.nSize = 0;
59246 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
59247 pCur->iPage++;
59248 pCur->aiIdx[pCur->iPage] = 0;
59249 return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
59250 pCur, pCur->curPagerFlags);
59278 ** pCur->idx is set to the cell index that contains the pointer
59280 ** right-most child page then pCur->idx is set to one more than
59283 static void moveToParent(BtCursor *pCur){
59284 assert( cursorHoldsMutex(pCur) );
59285 assert( pCur->eState==CURSOR_VALID );
59286 assert( pCur->iPage>0 );
59287 assert( pCur->apPage[pCur->iPage] );
59289 pCur->apPage[pCur->iPage-1],
59290 pCur->aiIdx[pCur->iPage-1],
59291 pCur->apPage[pCur->iPage]->pgno
59293 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
59294 pCur->info.nSize = 0;
59295 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
59296 releasePageNotNull(pCur->apPage[pCur->iPage--]);
59320 static int moveToRoot(BtCursor *pCur){
59324 assert( cursorHoldsMutex(pCur) );
59328 if( pCur->eState>=CURSOR_REQUIRESEEK ){
59329 if( pCur->eState==CURSOR_FAULT ){
59330 assert( pCur->skipNext!=SQLITE_OK );
59331 return pCur->skipNext;
59333 sqlite3BtreeClearCursor(pCur);
59336 if( pCur->iPage>=0 ){
59337 while( pCur->iPage ){
59338 assert( pCur->apPage[pCur->iPage]!=0 );
59339 releasePageNotNull(pCur->apPage[pCur->iPage--]);
59341 }else if( pCur->pgnoRoot==0 ){
59342 pCur->eState = CURSOR_INVALID;
59345 assert( pCur->iPage==(-1) );
59346 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
59347 0, pCur->curPagerFlags);
59349 pCur->eState = CURSOR_INVALID;
59352 pCur->iPage = 0;
59353 pCur->curIntKey = pCur->apPage[0]->intKey;
59355 pRoot = pCur->apPage[0];
59356 assert( pRoot->pgno==pCur->pgnoRoot );
59358 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
59365 ** if pCur->iPage>=0). But this is not so if the database is corrupted
59369 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
59373 pCur->aiIdx[0] = 0;
59374 pCur
59375 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
59378 pCur->eState = CURSOR_VALID;
59383 pCur->eState = CURSOR_VALID;
59384 rc = moveToChild(pCur, subpage);
59386 pCur->eState = CURSOR_INVALID;
59398 static int moveToLeftmost(BtCursor *pCur){
59403 assert( cursorHoldsMutex(pCur) );
59404 assert( pCur->eState==CURSOR_VALID );
59405 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
59406 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
59407 pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
59408 rc = moveToChild(pCur, pgno);
59423 static int moveToRightmost(BtCursor *pCur){
59428 assert( cursorHoldsMutex(pCur) );
59429 assert( pCur->eState==CURSOR_VALID );
59430 while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
59432 pCur->aiIdx[pCur->iPage] = pPage->nCell;
59433 rc = moveToChild(pCur, pgno);
59436 pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
59437 assert( pCur->info.nSize==0 );
59438 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
59446 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
59449 assert( cursorHoldsMutex(pCur) );
59450 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
59451 rc = moveToRoot(pCur);
59453 if( pCur->eState==CURSOR_INVALID ){
59454 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
59457 assert( pCur->apPage[pCur->iPage]->nCell>0 );
59459 rc = moveToLeftmost(pCur);
59469 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
59472 assert( cursorHoldsMutex(pCur) );
59473 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
59476 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
59481 for(ii=0; ii<pCur->iPage; ii++){
59482 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
59484 assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
59485 assert( pCur->apPage[pCur->iPage]->leaf );
59490 rc = moveToRoot(pCur);
59492 if( CURSOR_INVALID==pCur->eState ){
59493 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
59496 assert( pCur->eState==CURSOR_VALID );
59498 rc = moveToRightmost(pCur);
59500 pCur->curFlags |= BTCF_AtLast;
59502 pCur->curFlags &= ~BTCF_AtLast;
59539 BtCursor *pCur, /* The cursor to be moved */
59548 assert( cursorHoldsMutex(pCur) );
59549 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
59551 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
59555 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
59556 && pCur->curIntKey
59558 if( pCur->info.nKey==intKey ){
59562 if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
59579 rc = moveToRoot(pCur);
59583 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
59584 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
59585 assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
59586 if( pCur->eState==CURSOR_INVALID ){
59588 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
59591 assert( pCur->apPage[0]->intKey==pCur->curIntKey );
59592 assert( pCur->curIntKey || pIdxKey );
59596 MemPage *pPage = pCur->apPage[pCur->iPage];
59611 pCur->aiIdx[pCur->iPage] = (u16)idx;
59630 pCur->curFlags |= BTCF_ValidNKey;
59631 pCur->info.nKey = nCellKey;
59632 pCur->aiIdx[pCur->iPage] = (u16)idx;
59684 pPage->xParseCell(pPage, pCellBody, &pCur->info);
59685 nCell = (int)pCur->info.nKey;
59699 pCur->aiIdx[pCur->iPage] = (u16)idx;
59700 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
59710 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
59720 pCur->aiIdx[pCur->iPage] = (u16)idx;
59732 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
59733 pCur->aiIdx[pCur->iPage] = (u16)idx;
59744 pCur->aiIdx[pCur->iPage] = (u16)lwr;
59745 rc = moveToChild(pCur, chldPg);
59749 pCur->info.nSize = 0;
59750 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
59762 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
59767 return (CURSOR_VALID!=pCur->eState);
59791 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
59796 assert( cursorHoldsMutex(pCur) );
59797 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59799 if( pCur->eState!=CURSOR_VALID ){
59800 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
59801 rc = restoreCursorPosition(pCur);
59805 if( CURSOR_INVALID==pCur->eState ){
59809 if( pCur->skipNext ){
59810 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
59811 pCur->eState = CURSOR_VALID;
59812 if( pCur->skipNext>0 ){
59813 pCur->skipNext = 0;
59816 pCur->skipNext = 0;
59820 pPage = pCur->apPage[pCur->iPage];
59821 idx = ++pCur->aiIdx[pCur->iPage];
59826 ** the page while cursor pCur is holding a reference to it. Which can
59833 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
59835 return moveToLeftmost(pCur);
59838 if( pCur->iPage==0 ){
59840 pCur->eState = CURSOR_INVALID;
59843 moveToParent(pCur);
59844 pPage = pCurpCur->iPage];
59845 }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
59847 return sqlite3BtreeNext(pCur, pRes);
59855 return moveToLeftmost(pCur);
59858 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
59860 assert( cursorHoldsMutex(pCur) );
59863 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59864 pCur->info.nSize = 0;
59865 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
59867 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
59868 pPage = pCur->apPage[pCur->iPage];
59869 if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){
59870 pCur->aiIdx[pCur->iPage]--;
59871 return btreeNext(pCur, pRes);
59876 return moveToLeftmost(pCur);
59901 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){
59905 assert( cursorHoldsMutex(pCur) );
59908 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59909 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
59910 assert( pCur->info.nSize==0 );
59911 if( pCur->eState!=CURSOR_VALID ){
59912 rc = restoreCursorPosition(pCur);
59916 if( CURSOR_INVALID==pCur->eState ){
59920 if( pCur->skipNext ){
59921 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
59922 pCur->eState = CURSOR_VALID;
59923 if( pCur->skipNext<0 ){
59924 pCur->skipNext = 0;
59927 pCur->skipNext = 0;
59931 pPage = pCur->apPage[pCur->iPage];
59934 int idx = pCur->aiIdx[pCur->iPage];
59935 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
59937 rc = moveToRightmost(pCur);
59939 while( pCur->aiIdx[pCur->iPage]==0 ){
59940 if( pCur->iPage==0 ){
59941 pCur->eState = CURSOR_INVALID;
59945 moveToParent(pCur);
59947 assert( pCur->info.nSize==0 );
59948 assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 );
59950 pCur->aiIdx[pCur->iPage]--;
59951 pPage = pCur->apPage[pCur->iPage];
59953 rc = sqlite3BtreePrevious(pCur, pRes);
59960 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
59961 assert( cursorHoldsMutex(pCur) );
59964 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59966 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
59967 pCur->info.nSize = 0;
59968 if( pCur->eState!=CURSOR_VALID
59969 || pCur->aiIdx[pCur->iPage]==0
59970 || pCur->apPage[pCur->iPage]->leaf==0
59972 return btreePrevious(pCur, pRes);
59974 pCur->aiIdx[pCur->iPage]--;
62280 ** The page that pCur currently points to has just been modified in
62289 static int balance(BtCursor *pCur){
62291 const int nMin = pCur->pBt->usableSize * 2 / 3;
62299 int iPage = pCur->iPage;
62300 MemPage *pPage = pCur->apPage[iPage];
62310 rc = balance_deeper(pPage, &pCur->apPage[1]);
62312 pCur->iPage = 1;
62313 pCur->aiIdx[0] = 0;
62314 pCur->aiIdx[1] = 0;
62315 assert( pCur->apPage[1]->nOverflow );
62323 MemPage * const pParent = pCur->apPage[iPage-1];
62324 int const iIdx = pCur->aiIdx[iPage-1];
62370 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
62372 pCur->hints&BTREE_BULKLOAD);
62392 pCur->iPage--;
62393 assert( pCur->iPage>=0 );
62414 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
62416 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
62417 ** a positive value if pCur points at an entry that is larger than
62421 ** cursor pCur is pointing at the existing copy of a row that is to be
62422 ** overwritten. If the seekResult parameter is 0, then cursor pCur may
62427 BtCursor *pCur, /* Insert data into the table of this cursor */
62439 Btree *p = pCur->pBtree;
62444 if( pCur->eState==CURSOR_FAULT ){
62445 assert( pCur->skipNext!=SQLITE_OK );
62446 return pCur->skipNext;
62449 assert( cursorHoldsMutex(pCur) );
62450 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
62453 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
62460 assert( (pKey==0)==(pCur->pKeyInfo==0) );
62473 if( pCur->curFlags & BTCF_Multiple ){
62474 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
62478 if( pCur->pKeyInfo==0 ){
62487 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
62488 && pCur->info.nKey==nKey-1 ){
62491 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
62495 rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
62498 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
62500 pPage = pCur->apPage[pCur->iPage];
62505 pCur->pgnoRoot, nKey, nData, pPage->pgno,
62514 idx = pCur->aiIdx[pCur->iPage];
62531 idx = ++pCur->aiIdx[pCur->iPage];
62558 pCur->info.nSize = 0;
62560 pCur->curFlags &= ~(BTCF_ValidNKey);
62561 rc = balance(pCur);
62567 pCur->apPage[pCur->iPage]->nOverflow = 0;
62568 pCur->eState = CURSOR_INVALID;
62570 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
62585 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, int bPreserve){
62586 Btree *p = pCur->pBtree;
62596 assert( cursorHoldsMutex(pCur) );
62599 assert( pCur->curFlags & BTCF_WriteFlag );
62600 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
62601 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
62602 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
62603 assert( pCur->eState==CURSOR_VALID );
62605 iCellDepth = pCur->iPage;
62606 iCellIdx = pCur->aiIdx[iCellDepth];
62607 pPage = pCur->apPage[iCellDepth];
62619 rc = sqlite3BtreePrevious(pCur, ¬Used);
62625 if( pCur->curFlags & BTCF_Multiple ){
62626 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
62632 if( pCur->pKeyInfo==0 ){
62633 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
62651 rc = saveCursorKey(pCur);
62673 MemPage *pLeaf = pCur->apPage[pCur->iPage];
62675 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
62696 ** pCur is pointing to the leaf page from which a cell was removed to
62705 rc = balance(pCur);
62706 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
62707 while( pCur->iPage>iCellDepth ){
62708 releasePage(pCur->apPage[pCur->iPage--]);
62710 rc = balance(pCur);
62715 assert( bPreserve && pCur->iPage==iCellDepth );
62716 assert( pPage==pCur->apPage[pCur->iPage] );
62718 pCur->eState = CURSOR_SKIPNEXT;
62720 pCur->skipNext = -1;
62721 pCur->aiIdx[iCellDepth] = pPage->nCell-1;
62723 pCur->skipNext = 1;
62726 rc = moveToRoot(pCur);
62728 pCur->eState = CURSOR_REQUIRESEEK;
62988 ** Delete all information from the single table that pCur is open on.
62990 ** This routine only work for pCur on an ephemeral table.
62992 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
62993 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
63204 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
63211 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
63215 if( pCur->pgnoRoot==0 ){
63219 rc = moveToRoot(pCur);
63232 pPage = pCur->apPage[pCur->iPage];
63240 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
63249 if( pCur->iPage==0 ){
63252 return moveToRoot(pCur);
63254 moveToParent(pCur);
63255 }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
63257 pCur->aiIdx[pCur->iPage]++;
63258 pPage = pCur->apPage[pCur->iPage];
63264 iIdx = pCur->aiIdx[pCur->iPage];
63266 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
63268 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
64104 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
64105 pCur->curFlags |= BTCF_Incrblob;
64106 pCur->pBtree->hasIncrblobCur = 1;
65918 ** The data or key is taken from the entry that pCur is currently pointing
65933 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
65943 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
65945 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
65959 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
65969 assert( sqlite3BtreeCursorIsValid(pCur) );
65976 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
65978 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
65987 rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
70755 ** pCur points at an index entry created using the OP_MakeRecord opcode.
70759 ** pCur might be pointing to text obtained from a corrupt database file.
70762 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
70775 assert( sqlite3BtreeCursorIsValid(pCur) );
70776 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
70777 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
70782 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
70848 BtCursor *pCur = pC->pCursor;
70851 assert( sqlite3BtreeCursorIsValid(pCur) );
70852 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
70853 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
76218 VdbeCursor *pCur;
76223 pCur = p->apCsr[pOp->p1];
76224 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
76225 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
76290 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
76291 if( pCur==0 ) goto no_mem;
76292 pCur->nullRow = 1;
76293 pCur->isOrdered = 1;
76294 pCur->pgnoRoot = p2;
76295 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
76296 pCur->pKeyInfo = pKeyInfo;
76301 pCur->isTable = pOp->p4type!=P4_KEYINFO;
76306 sqlite3BtreeCursorHints(pCur->pCursor,
79109 VdbeCursor *pCur;
79115 pCur = 0;
79130 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
79131 if( pCur ){
79132 pCur->pVtabCursor = pVtabCursor;
79172 VdbeCursor *pCur;
79179 pCur = p->apCsr[pOp->p1];
79182 assert( pCur->pVtabCursor );
79183 pVtabCursor = pCur->pVtabCursor;
79203 pCur->nullRow = 0;
79224 VdbeCursor *pCur = p->apCsr[pOp->p1];
79225 assert( pCur->pVtabCursor );
79229 if( pCur->nullRow ){
79233 pVtab = pCur->pVtabCursor->pVtab;
79239 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
79266 VdbeCursor *pCur;
79269 pCur = p->apCsr[pOp->p1];
79270 assert( pCur->pVtabCursor );
79271 if( pCur->nullRow ){
79274 pVtab = pCur->pVtabCursor->pVtab;
79284 rc = pModule->xNext(pCur->pVtabCursor);
79287 res = pModule->xEof(pCur->pVtabCursor);
155212 static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
155213 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
155219 static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
155221 int ii = 1 - pCur->bPoint;
155223 assert( pCur->bPoint || pCur->nPoint );
155224 if( pCur->aNode[ii]==0 ){
155226 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
155227 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
155229 return pCur->aNode[ii];
155236 RtreeCursor *pCur, /* The cursor */
155242 if( pCur->nPoint>=pCur->nPointAlloc ){
155243 int nNew = pCur->nPointAlloc*2 + 8;
155244 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
155246 pCur->aPoint = pNew;
155247 pCur->nPointAlloc = nNew;
155249 i = pCur->nPoint++;
155250 pNew = pCur->aPoint + i;
155257 pParent = pCur->aPoint + j;
155259 rtreeSearchPointSwap(pCur, j, i);
155271 RtreeCursor *pCur, /* The cursor */
155276 pFirst = rtreeSearchPointFirst(pCur);
155277 pCur->anQueue[iLevel]++;
155282 if( pCur->bPoint ){
155284 pNew = rtreeEnqueue(pCur, rScore, iLevel);
155286 ii = (int)(pNew - pCur->aPoint) + 1;
155288 assert( pCur->aNode[ii]==0 );
155289 pCur->aNode[ii] = pCur->aNode[0];
155291 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
155293 pCur->aNode[0] = 0;
155294 *pNew = pCur->sPoint;
155296 pCur->sPoint.rScore = rScore;
155297 pCur->sPoint.iLevel = iLevel;
155298 pCur->bPoint = 1;
155299 return &pCur->sPoint;
155301 return rtreeEnqueue(pCur, rScore, iLevel);
155307 static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
155314 printf(" %p\n", pCur->aNode[idx]);
155319 static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
155322 if( pCur->bPoint ){
155323 tracePoint(&pCur->sPoint, -1, pCur);
155325 for(ii=0; ii<pCur->nPoint; ii++){
155326 if( ii>0 || pCur->bPoint ) printf(" ");
155327 tracePoint(&pCur->aPoint[ii], ii, pCur);
155380 ** Continue the search on cursor pCur until the front of the queue
155385 static int rtreeStepToLeaf(RtreeCursor *pCur){
155387 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
155392 int nConstraint = pCur->nConstraint;
155398 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
155399 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
155408 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
155431 RTREE_QUEUE_TRACE(pCur, "POP-S:");
155432 rtreeSearchPointPop(pCur);
155435 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
155440 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
155444 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
155445 rtreeSearchPointPop(pCur);
155448 pCur->atEOF = p==0;
165164 JsonEachCursor *pCur;
165167 pCur = sqlite3_malloc( sizeof(*pCur) );
165168 if( pCur==0 ) return SQLITE_NOMEM;
165169 memset(pCur, 0, sizeof(*pCur));
165170 *ppCursor = &pCur->base;
165178 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
165179 pCur->bRecursive = 1;