Lines Matching refs:pPage

419 static void releasePage(MemPage *pPage);  /* Forward reference */
859 static u8 *findOverflowCell(MemPage *pPage, int iCell){
861 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
862 for(i=pPage->nOverflow-1; i>=0; i--){
865 pOvfl = &pPage->aOvfl[i];
874 return findCell(pPage, iCell);
887 MemPage *pPage, /* Page containing the cell */
894 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
897 assert( pPage->leaf==0 || pPage->leaf==1 );
898 n = pPage->childPtrSize;
899 assert( n==4-4*pPage->leaf );
900 if( pPage->intKey ){
901 if( pPage->hasData ){
915 testcase( nPayload==pPage->maxLocal );
916 testcase( nPayload==pPage->maxLocal+1 );
917 if( likely(nPayload<=pPage->maxLocal) ){
938 minLocal = pPage->minLocal;
939 maxLocal = pPage->maxLocal;
940 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
952 #define parseCell(pPage, iCell, pInfo) \
953 btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
955 MemPage *pPage, /* Page containing the cell */
959 parseCell(pPage, iCell, pInfo);
968 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
969 u8 *pIter = &pCell[pPage->childPtrSize];
978 btreeParseCellPtr(pPage, pCell, &debuginfo);
981 if( pPage->intKey ){
983 if( pPage->hasData ){
998 testcase( nSize==pPage->maxLocal );
999 testcase( nSize==pPage->maxLocal+1 );
1000 if( nSize>pPage->maxLocal ){
1001 int minLocal = pPage->minLocal;
1002 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1003 testcase( nSize==pPage->maxLocal );
1004 testcase( nSize==pPage->maxLocal+1 );
1005 if( nSize>pPage->maxLocal ){
1024 static u16 cellSize(MemPage *pPage, int iCell){
1025 return cellSizePtr(pPage, findCell(pPage, iCell));
1031 ** If the cell pCell, part of page pPage contains a pointer
1035 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
1039 btreeParseCellPtr(pPage, pCell, &info);
1040 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
1043 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
1055 static int defragmentPage(MemPage *pPage){
1070 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1071 assert( pPage->pBt!=0 );
1072 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
1073 assert( pPage->nOverflow==0 );
1074 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1075 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
1076 data = pPage->aData;
1077 hdr = pPage->hdrOffset;
1078 cellOffset = pPage->cellOffset;
1079 nCell = pPage->nCell;
1081 usableSize = pPage->pBt->usableSize;
1102 size = cellSizePtr(pPage, &temp[pc]);
1125 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1126 if( cbrk-iCellFirst!=pPage->nFree ){
1134 ** as the first argument. Write into *pIdx the index into pPage->aData[]
1145 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
1146 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
1147 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
1148 int nFrag; /* Number of fragmented bytes on pPage */
1154 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1155 assert( pPage->pBt );
1156 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1158 assert( pPage->nFree>=nByte );
1159 assert( pPage->nOverflow==0 );
1160 usableSize = pPage->pBt->usableSize;
1164 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
1165 gap = pPage->cellOffset + 2*pPage->nCell;
1174 rc = defragmentPage(pPage);
1216 rc = defragmentPage(pPage);
1231 assert( top+nByte <= (int)pPage->pBt->usableSize );
1237 ** Return a section of the pPage->aData to the freelist.
1238 ** The first byte of the new free block is pPage->aDisk[start]
1244 static int freeSpace(MemPage *pPage, int start, int size){
1247 unsigned char *data = pPage->aData;
1249 assert( pPage->pBt!=0 );
1250 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1251 assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
1252 assert( (start + size) <= (int)pPage->pBt->usableSize );
1253 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1256 if( pPage->pBt->secureDelete ){
1271 hdr = pPage->hdrOffset;
1273 iLast = pPage->pBt->usableSize - 4;
1288 pPage->nFree = pPage->nFree + (u16)size;
1295 assert( pbegin <= (int)pPage->pBt->usableSize-4 );
1321 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1337 static int decodeFlags(MemPage *pPage, int flagByte){
1338 BtShared *pBt; /* A copy of pPage->pBt */
1340 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1341 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1342 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
1344 pPage->childPtrSize = 4-4*pPage->leaf;
1345 pBt = pPage->pBt;
1347 pPage->intKey = 1;
1348 pPage->hasData = pPage->leaf;
1349 pPage->maxLocal = pBt->maxLeaf;
1350 pPage->minLocal = pBt->minLeaf;
1352 pPage->intKey = 0;
1353 pPage->hasData = 0;
1354 pPage->maxLocal = pBt->maxLocal;
1355 pPage->minLocal = pBt->minLocal;
1371 static int btreeInitPage(MemPage *pPage){
1373 assert( pPage->pBt!=0 );
1374 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1375 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
1376 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
1377 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
1379 if( !pPage->isInit ){
1380 u16 pc; /* Address of a freeblock within pPage->aData[] */
1382 u8 *data; /* Equal to pPage->aData */
1391 pBt = pPage->pBt;
1393 hdr = pPage->hdrOffset;
1394 data = pPage->aData;
1395 if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
1397 pPage->maskPage = (u16)(pBt->pageSize - 1);
1398 pPage->nOverflow = 0;
1400 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
1402 pPage->nCell = get2byte(&data[hdr+3]);
1403 if( pPage->nCell>MX_CELL(pBt) ){
1407 testcase( pPage->nCell==MX_CELL(pBt) );
1416 iCellFirst = cellOffset + 2*pPage->nCell;
1423 if( !pPage->leaf ) iCellLast--;
1424 for(i=0; i<pPage->nCell; i++){
1431 sz = cellSizePtr(pPage, &data[pc]);
1437 if( !pPage->leaf ) iCellLast++;
1471 pPage->nFree = (u16)(nFree - iCellFirst);
1472 pPage->isInit = 1;
1481 static void zeroPage(MemPage *pPage, int flags){
1482 unsigned char *data = pPage->aData;
1483 BtShared *pBt = pPage->pBt;
1484 u8 hdr = pPage->hdrOffset;
1487 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
1488 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1489 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
1490 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1500 pPage->nFree = (u16)(pBt->usableSize - first);
1501 decodeFlags(pPage, flags);
1502 pPage->hdrOffset = hdr;
1503 pPage->cellOffset = first;
1504 pPage->nOverflow = 0;
1506 pPage->maskPage = (u16)(pBt->pageSize - 1);
1507 pPage->nCell = 0;
1508 pPage->isInit = 1;
1517 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
1518 pPage->aData = sqlite3PagerGetData(pDbPage);
1519 pPage->pDbPage = pDbPage;
1520 pPage->pBt = pBt;
1521 pPage->pgno = pgno;
1522 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
1523 return pPage;
1618 static void releasePage(MemPage *pPage){
1619 if( pPage ){
1620 assert( pPage->aData );
1621 assert( pPage->pBt );
1622 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1623 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
1624 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1625 sqlite3PagerUnref(pPage->pDbPage);
1638 MemPage *pPage;
1639 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
1641 if( pPage->isInit ){
1642 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1643 pPage->isInit = 0;
1645 /* pPage might not be a btree page; it might be an overflow page
1651 btreeInitPage(pPage);
2662 ** Set the pointer-map entries for all children of page pPage. Also, if
2663 ** pPage contains cells that point to overflow pages, set the pointer
2666 static int setChildPtrmaps(MemPage *pPage){
2668 int nCell; /* Number of cells in page pPage */
2670 BtShared *pBt = pPage->pBt;
2671 u8 isInitOrig = pPage->isInit;
2672 Pgno pgno = pPage->pgno;
2674 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2675 rc = btreeInitPage(pPage);
2679 nCell = pPage->nCell;
2682 u8 *pCell = findCell(pPage, i);
2684 ptrmapPutOvflPtr(pPage, pCell, &rc);
2686 if( !pPage->leaf ){
2692 if( !pPage->leaf ){
2693 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
2698 pPage->isInit = isInitOrig;
2703 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
2707 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
2708 ** page of pPage.
2710 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
2711 ** page pointed to by one of the cells on pPage.
2713 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
2716 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
2717 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2718 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
2721 if( get4byte(pPage->aData)!=iFrom ){
2724 put4byte(pPage->aData, iTo);
2726 u8 isInitOrig = pPage->isInit;
2730 btreeInitPage(pPage);
2731 nCell = pPage->nCell;
2734 u8 *pCell = findCell(pPage, i);
2737 btreeParseCellPtr(pPage, pCell, &info);
2754 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
2757 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
2760 pPage->isInit = isInitOrig;
3711 MemPage *pPage = 0;
3745 rc = btreeGetPage(pBt, ovfl, &pPage, 0);
3746 assert( rc==SQLITE_OK || pPage==0 );
3748 next = get4byte(pPage->aData);
3754 *ppPage = pPage;
3756 releasePage(pPage);
3832 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
3835 assert( pPage );
3837 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
3842 nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
3845 || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
3857 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
4027 MemPage *pPage;
4034 pPage = pCur->apPage[pCur->iPage];
4035 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
4042 if( pPage->intKey ){
4272 MemPage *pPage;
4276 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
4277 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
4278 pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
4297 MemPage *pPage = 0;
4301 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
4302 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
4303 pCur->aiIdx[pCur->iPage] = pPage->nCell;
4307 pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
4450 MemPage *pPage = pCur->apPage[pCur->iPage];
4453 /* pPage->nCell must be greater than zero. If this is the root-page
4456 ** would have already detected db corruption. Similarly, pPage must
4459 assert( pPage->nCell>0 );
4460 assert( pPage->intKey==(pIdxKey==0) );
4462 upr = pPage->nCell-1;
4469 int idx = pCur->aiIdx[pCur->iPage]; /* Index of current cell in pPage */
4470 u8 *pCell; /* Pointer to current cell in pPage */
4473 pCell = findCell(pPage, idx) + pPage->childPtrSize;
4474 if( pPage->intKey ){
4476 if( pPage->hasData ){
4501 if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
4507 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
4518 u8 * const pCellBody = pCell - pPage->childPtrSize;
4519 btreeParseCellPtr(pPage, pCellBody, &pCur->info);
4536 if( pPage->intKey && !pPage->leaf ){
4557 assert( pPage->isInit );
4558 if( pPage->leaf ){
4560 }else if( lwr>=pPage->nCell ){
4561 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
4563 chldPg = get4byte(findCell(pPage, lwr));
4606 MemPage *pPage;
4625 pPage = pCur->apPage[pCur->iPage];
4627 assert( pPage->isInit );
4628 assert( idx<=pPage->nCell );
4632 if( idx>=pPage->nCell ){
4633 if( !pPage->leaf ){
4634 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
4647 pPage = pCur->apPage[pCur->iPage];
4648 }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
4650 if( pPage->intKey ){
4658 if( pPage->leaf ){
4674 MemPage *pPage;
4693 pPage = pCur->apPage[pCur->iPage];
4694 assert( pPage->isInit );
4695 if( !pPage->leaf ){
4697 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
4715 pPage = pCur->apPage[pCur->iPage];
4716 if( pPage->intKey && !pPage->leaf ){
5030 MemPage *pPage; /* Page being freed. May be NULL. */
5039 pPage = pMemPage;
5040 sqlite3PagerRef(pPage->pDbPage);
5042 pPage = btreePageLookup(pBt, iPage);
5055 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
5056 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
5060 memset(pPage->aData, 0, pPage->pBt->pageSize);
5112 if( pPage && !pBt->secureDelete ){
5113 sqlite3PagerDontWrite(pPage->pDbPage);
5117 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
5128 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
5131 rc = sqlite3PagerWrite(pPage->pDbPage);
5135 put4byte(pPage->aData, iTrunk);
5136 put4byte(&pPage->aData[4], 0);
5138 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
5141 if( pPage ){
5142 pPage->isInit = 0;
5144 releasePage(pPage);
5148 static void freePage(MemPage *pPage, int *pRC){
5150 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
5157 static int clearCell(MemPage *pPage, unsigned char *pCell){
5158 BtShared *pBt = pPage->pBt;
5165 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5166 btreeParseCellPtr(pPage, pCell, &info);
5217 ** Create the byte sequence used to represent a cell on page pPage
5223 ** Note that pCell does not necessary need to point to the pPage->aData
5225 ** be constructed in this temporary area then copied into pPage->aData
5229 MemPage *pPage, /* The page that contains the cell */
5244 BtShared *pBt = pPage->pBt;
5249 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5251 /* pPage is not necessarily writeable since pCell might be auxiliary
5252 ** buffer space that is separate from the pPage buffer area */
5253 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
5254 || sqlite3PagerIswriteable(pPage->pDbPage) );
5258 if( !pPage->leaf ){
5261 if( pPage->hasData ){
5267 btreeParseCellPtr(pPage, pCell, &info);
5274 if( pPage->intKey ){
5332 /* If pPrior is part of the data area of pPage, then make sure pPage
5334 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
5335 || sqlite3PagerIswriteable(pPage->pDbPage) );
5352 /* If pPayload is part of the data area of pPage, then make sure pPage
5354 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
5355 || sqlite3PagerIswriteable(pPage->pDbPage) );
5379 ** Remove the i-th cell from pPage. This routine effects pPage only.
5382 ** removes the reference to the cell from pPage.
5386 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
5389 u8 *data; /* pPage->aData */
5396 assert( idx>=0 && idx<pPage->nCell );
5397 assert( sz==cellSize(pPage, idx) );
5398 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5399 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5400 data = pPage->aData;
5401 ptr = &data[pPage->cellOffset + 2*idx];
5403 hdr = pPage->hdrOffset;
5405 testcase( pc+sz==pPage->pBt->usableSize );
5406 if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
5410 rc = freeSpace(pPage, pc, sz);
5415 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
5419 pPage->nCell--;
5420 put2byte(&data[hdr+3], pPage->nCell);
5421 pPage->nFree += 2;
5425 ** Insert a new cell on pPage at cell index "i". pCell points to the
5431 ** in pPage->aOvfl[] and make it point to the cell content (either
5433 ** Allocating a new entry in pPage->aCell[] implies that
5434 ** pPage->nOverflow is incremented.
5442 MemPage *pPage, /* Page into which we are copying */
5462 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
5463 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
5464 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
5465 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5471 assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
5472 if( pPage->nOverflow || sz+2>pPage->nFree ){
5480 j = pPage->nOverflow++;
5481 assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
5482 pPage->aOvfl[j].pCell = pCell;
5483 pPage->aOvfl[j].idx = (u16)i;
5485 int rc = sqlite3PagerWrite(pPage->pDbPage);
5490 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5491 data = pPage->aData;
5492 cellOffset = pPage->cellOffset;
5493 end = cellOffset + 2*pPage->nCell;
5495 rc = allocateSpace(pPage, sz, &idx);
5500 assert( idx+sz <= (int)pPage->pBt->usableSize );
5501 pPage->nCell++;
5502 pPage->nFree -= (u16)(2 + sz);
5512 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
5514 if( pPage->pBt->autoVacuum ){
5518 ptrmapPutOvflPtr(pPage, pCell, pRC);
5529 MemPage *pPage, /* The page to be assemblied */
5537 u8 * const data = pPage->aData; /* Pointer to data for pPage */
5538 const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
5539 const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
5541 assert( pPage->nOverflow==0 );
5542 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5543 assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
5544 && (int)MX_CELL(pPage->pBt)<=10921);
5545 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5548 assert( pPage->nCell==0 );
5551 pCellptr = &data[pPage->cellOffset + nCell*2];
5561 pPage->nFree -= (nCell*2 + nUsable - cellbody);
5562 pPage->nCell = (u16)nCell;
5577 #define NN 1 /* Number of neighbors on either side of pPage */
5595 ** pPage is the leaf page which is the right-most page in the tree.
5596 ** pParent is its parent. pPage must have a single overflow entry
5605 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
5606 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
5611 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5613 assert( pPage->nOverflow==1 );
5616 if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
5619 ** pPage. Make the parent page writable, so that the new divider cell
5627 u8 *pCell = pPage->aOvfl[0].pCell;
5628 u16 szCell = cellSizePtr(pPage, pCell);
5632 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
5653 ** consists of a 4-byte page number (the page number of pPage) and
5655 ** largest key on pPage).
5657 ** To find the largest key value on pPage, first find the right-most
5658 ** cell on pPage. The first two fields of this cell are the
5663 ** cell on pPage into the pSpace buffer.
5665 pCell = findCell(pPage, pPage->nCell-1);
5673 0, pPage->pgno, &rc);
5697 MemPage *pPage = apPage[i];
5698 BtShared *pBt = pPage->pBt;
5699 assert( pPage->isInit );
5701 for(j=0; j<pPage->nCell; j++){
5705 z = findCell(pPage, j);
5706 btreeParseCellPtr(pPage, z, &info);
5710 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
5712 if( !pPage->leaf ){
5715 assert( n==pPage->pgno && e==PTRMAP_BTREE );
5718 if( !pPage->leaf ){
5719 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5721 assert( n==pPage->pgno && e==PTRMAP_BTREE );
5840 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
5841 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
5842 int usableSpace; /* Bytes in pPage beyond the header */
5843 int pageFlags; /* Value of pPage->aData[0] */
5848 MemPage *apOld[NB]; /* pPage and up to two siblings */
5850 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
5865 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
5882 ** either side of pPage. More siblings are taken from one side, however,
5993 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
5994 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
6111 /* Either we found one or more cells (cntnew[0])>0) or pPage is
6461 ** page that will become the new right-child of pPage. Copy the contents
6516 MemPage *pPage = pCur->apPage[iPage];
6519 if( pPage->nOverflow ){
6526 rc = balance_deeper(pPage, &pCur->apPage[1]);
6536 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
6545 if( pPage->hasData
6546 && pPage->nOverflow==1
6547 && pPage->aOvfl[0].idx==pPage->nCell
6551 /* Call balance_quick() to create a new sibling of pPage on which
6565 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
6570 ** between pPage and up to 2 of its sibling pages. This involves
6603 pPage->nOverflow = 0;
6606 releasePage(pPage);
6652 MemPage *pPage;
6700 pPage = pCur->apPage[pCur->iPage];
6701 assert( pPage->intKey || nKey>=0 );
6702 assert( pPage->leaf || !pPage->intKey );
6705 pCur->pgnoRoot, nKey, nData, pPage->pgno,
6707 assert( pPage->isInit );
6711 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
6713 assert( szNew==cellSizePtr(pPage, newCell) );
6718 assert( idx<pPage->nCell );
6719 rc = sqlite3PagerWrite(pPage->pDbPage);
6723 oldCell = findCell(pPage, idx);
6724 if( !pPage->leaf ){
6727 szOld = cellSizePtr(pPage, oldCell);
6728 rc = clearCell(pPage, oldCell);
6729 dropCell(pPage, idx, szOld, &rc);
6731 }else if( loc<0 && pPage->nCell>0 ){
6732 assert( pPage->leaf );
6735 assert( pPage->leaf );
6737 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
6738 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
6740 /* If no error has occured and pPage has an overflow cell, call balance()
6762 if( rc==SQLITE_OK && pPage->nOverflow ){
6786 MemPage *pPage; /* Page to delete cell from */
6812 pPage = pCur->apPage[iCellDepth];
6813 pCell = findCell(pPage, iCellIdx);
6822 if( !pPage->leaf ){
6835 rc = sqlite3PagerWrite(pPage->pDbPage);
6837 rc = clearCell(pPage, pCell);
6838 dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
6846 if( !pPage->leaf ){
6860 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
7058 MemPage *pPage;
7068 rc = getAndInitPage(pBt, pgno, &pPage);
7070 for(i=0; i<pPage->nCell; i++){
7071 pCell = findCell(pPage, i);
7072 if( !pPage->leaf ){
7076 rc = clearCell(pPage, pCell);
7079 if( !pPage->leaf ){
7080 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
7083 assert( pPage->intKey );
7084 *pnChange += pPage->nCell;
7087 freePage(pPage, &rc);
7088 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
7089 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
7093 releasePage(pPage);
7151 MemPage *pPage = 0;
7170 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
7174 releasePage(pPage);
7182 freePage(pPage, &rc);
7183 releasePage(pPage);
7193 freePage(pPage, &rc);
7194 releasePage(pPage);
7204 releasePage(pPage);
7238 freePage(pPage, &rc);
7239 releasePage(pPage);
7247 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
7248 releasePage(pPage);
7341 MemPage *pPage; /* Current page of the b-tree */
7347 pPage = pCur->apPage[pCur->iPage];
7348 if( pPage->leaf || !pPage->intKey ){
7349 nEntry += pPage->nCell;
7352 /* pPage is a leaf node. This loop navigates the cursor so that it
7362 if( pPage->leaf ){
7373 pPage = pCur->apPage[pCur->iPage];
7377 ** points at. This is the right-child if (iIdx==pPage->nCell).
7380 if( iIdx==pPage->nCell ){
7381 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
7383 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
7580 MemPage *pPage;
7600 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
7608 pPage->isInit = 0;
7609 if( (rc = btreeInitPage(pPage))!=0 ){
7613 releasePage(pPage);
7620 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
7629 pCell = findCell(pPage,i);
7630 btreeParseCellPtr(pPage, pCell, &info);
7632 if( !pPage->intKey ) sz += (int)info.nKey;
7645 && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
7659 if( !pPage->leaf ){
7674 if( !pPage->leaf ){
7675 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
7683 checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
7689 if( pPage->leaf && pPage->intKey ){
7724 data = pPage->aData;
7725 hdr = pPage->hdrOffset;
7735 cellStart = hdr + 12 - 4*pPage->leaf;
7741 size = cellSizePtr(pPage, &data[pc]);
7778 releasePage(pPage);