Index: third_party/sqlite/src/src/btree.c |
diff --git a/third_party/sqlite/src/src/btree.c b/third_party/sqlite/src/src/btree.c |
index 7ea66e0d3be94e88c344f06b969bffafc69ecd0c..f5feff8a4c597c8b490c9e154dfa5b056e9b93a1 100644 |
--- a/third_party/sqlite/src/src/btree.c |
+++ b/third_party/sqlite/src/src/btree.c |
@@ -175,6 +175,12 @@ static int hasSharedCacheTableLock( |
for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ |
Index *pIdx = (Index *)sqliteHashData(p); |
if( pIdx->tnum==(int)iRoot ){ |
+ if( iTab ){ |
+ /* Two or more indexes share the same root page. There must |
+ ** be imposter tables. So just return true. The assert is not |
+ ** useful in that case. */ |
+ return 1; |
+ } |
iTab = pIdx->pTable->tnum; |
} |
} |
@@ -484,13 +490,15 @@ static void invalidateIncrblobCursors( |
int isClearTable /* True if all rows are being deleted */ |
){ |
BtCursor *p; |
- BtShared *pBt = pBtree->pBt; |
+ if( pBtree->hasIncrblobCur==0 ) return; |
assert( sqlite3BtreeHoldsMutex(pBtree) ); |
- for(p=pBt->pCursor; p; p=p->pNext){ |
- if( (p->curFlags & BTCF_Incrblob)!=0 |
- && (isClearTable || p->info.nKey==iRow) |
- ){ |
- p->eState = CURSOR_INVALID; |
+ pBtree->hasIncrblobCur = 0; |
+ for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
+ if( (p->curFlags & BTCF_Incrblob)!=0 ){ |
+ pBtree->hasIncrblobCur = 1; |
+ if( isClearTable || p->info.nKey==iRow ){ |
+ p->eState = CURSOR_INVALID; |
+ } |
} |
} |
} |
@@ -583,17 +591,21 @@ static void btreeReleaseAllCursorPages(BtCursor *pCur){ |
pCur->iPage = -1; |
} |
- |
/* |
-** Save the current cursor position in the variables BtCursor.nKey |
-** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
+** The cursor passed as the only argument must point to a valid entry |
+** when this function is called (i.e. have eState==CURSOR_VALID). This |
+** function saves the current cursor key in variables pCur->nKey and |
+** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error |
+** code otherwise. |
** |
-** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) |
-** prior to calling this routine. |
+** If the cursor is open on an intkey table, then the integer key |
+** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to |
+** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is |
+** set to point to a malloced buffer pCur->nKey bytes in size containing |
+** the key. |
*/ |
-static int saveCursorPosition(BtCursor *pCur){ |
+static int saveCursorKey(BtCursor *pCur){ |
int rc; |
- |
assert( CURSOR_VALID==pCur->eState ); |
assert( 0==pCur->pKey ); |
assert( cursorHoldsMutex(pCur) ); |
@@ -605,9 +617,8 @@ static int saveCursorPosition(BtCursor *pCur){ |
** stores the integer key in pCur->nKey. In this case this value is |
** all that is required. Otherwise, if pCur is not open on an intKey |
** table, then malloc space for and store the pCur->nKey bytes of key |
- ** data. |
- */ |
- if( 0==pCur->apPage[0]->intKey ){ |
+ ** data. */ |
+ if( 0==pCur->curIntKey ){ |
void *pKey = sqlite3Malloc( pCur->nKey ); |
if( pKey ){ |
rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
@@ -620,14 +631,37 @@ static int saveCursorPosition(BtCursor *pCur){ |
rc = SQLITE_NOMEM; |
} |
} |
- assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
+ assert( !pCur->curIntKey || !pCur->pKey ); |
+ return rc; |
+} |
+ |
+/* |
+** Save the current cursor position in the variables BtCursor.nKey |
+** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
+** |
+** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) |
+** prior to calling this routine. |
+*/ |
+static int saveCursorPosition(BtCursor *pCur){ |
+ int rc; |
+ |
+ assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState ); |
+ assert( 0==pCur->pKey ); |
+ assert( cursorHoldsMutex(pCur) ); |
+ |
+ if( pCur->eState==CURSOR_SKIPNEXT ){ |
+ pCur->eState = CURSOR_VALID; |
+ }else{ |
+ pCur->skipNext = 0; |
+ } |
+ rc = saveCursorKey(pCur); |
if( rc==SQLITE_OK ){ |
btreeReleaseAllCursorPages(pCur); |
pCur->eState = CURSOR_REQUIRESEEK; |
} |
- invalidateOverflowCache(pCur); |
+ pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast); |
return rc; |
} |
@@ -642,6 +676,15 @@ static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); |
** routine is called just before cursor pExcept is used to modify the |
** table, for example in BtreeDelete() or BtreeInsert(). |
** |
+** If there are two or more cursors on the same btree, then all such |
+** cursors should have their BTCF_Multiple flag set. The btreeCursor() |
+** routine enforces that rule. This routine only needs to be called in |
+** the uncommon case when pExpect has the BTCF_Multiple flag set. |
+** |
+** If pExpect!=NULL and if no other cursors are found on the same root-page, |
+** then the BTCF_Multiple flag on pExpect is cleared, to avoid another |
+** pointless call to this routine. |
+** |
** Implementation note: This routine merely checks to see if any cursors |
** need to be saved. It calls out to saveCursorsOnList() in the (unusual) |
** event that cursors are in need to being saved. |
@@ -653,7 +696,9 @@ static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
for(p=pBt->pCursor; p; p=p->pNext){ |
if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; |
} |
- return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK; |
+ if( p ) return saveCursorsOnList(p, iRoot, pExcept); |
+ if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple; |
+ return SQLITE_OK; |
} |
/* This helper routine to saveAllCursors does the actual work of saving |
@@ -668,7 +713,7 @@ static int SQLITE_NOINLINE saveCursorsOnList( |
){ |
do{ |
if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ |
- if( p->eState==CURSOR_VALID ){ |
+ if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ |
int rc = saveCursorPosition(p); |
if( SQLITE_OK!=rc ){ |
return rc; |
@@ -740,17 +785,19 @@ static int btreeMoveto( |
*/ |
static int btreeRestoreCursorPosition(BtCursor *pCur){ |
int rc; |
+ int skipNext; |
assert( cursorHoldsMutex(pCur) ); |
assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
if( pCur->eState==CURSOR_FAULT ){ |
return pCur->skipNext; |
} |
pCur->eState = CURSOR_INVALID; |
- rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext); |
+ rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); |
if( rc==SQLITE_OK ){ |
sqlite3_free(pCur->pKey); |
pCur->pKey = 0; |
assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
+ pCur->skipNext |= skipNext; |
if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ |
pCur->eState = CURSOR_SKIPNEXT; |
} |
@@ -802,14 +849,35 @@ int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ |
*pDifferentRow = 1; |
return rc; |
} |
- if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){ |
+ if( pCur->eState!=CURSOR_VALID ){ |
*pDifferentRow = 1; |
}else{ |
+ assert( pCur->skipNext==0 ); |
*pDifferentRow = 0; |
} |
return SQLITE_OK; |
} |
+#ifdef SQLITE_ENABLE_CURSOR_HINTS |
+/* |
+** Provide hints to the cursor. The particular hint given (and the type |
+** and number of the varargs parameters) is determined by the eHintType |
+** parameter. See the definitions of the BTREE_HINT_* macros for details. |
+*/ |
+void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ |
+ /* Used only by system that substitute their own storage engine */ |
+} |
+#endif |
+ |
+/* |
+** Provide flag hints to the cursor. |
+*/ |
+void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ |
+ assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 ); |
+ pCur->hints = x; |
+} |
+ |
+ |
#ifndef SQLITE_OMIT_AUTOVACUUM |
/* |
** Given a page number of a regular database page, return the page |
@@ -863,7 +931,7 @@ static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ |
return; |
} |
iPtrmap = PTRMAP_PAGENO(pBt, key); |
- rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
+ rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); |
if( rc!=SQLITE_OK ){ |
*pRC = rc; |
return; |
@@ -906,7 +974,7 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
assert( sqlite3_mutex_held(pBt->mutex) ); |
iPtrmap = PTRMAP_PAGENO(pBt, key); |
- rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
+ rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); |
if( rc!=0 ){ |
return rc; |
} |
@@ -938,39 +1006,86 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
** the page, 1 means the second cell, and so forth) return a pointer |
** to the cell content. |
** |
+** findCellPastPtr() does the same except it skips past the initial |
+** 4-byte child pointer found on interior pages, if there is one. |
+** |
** This routine works only for pages that do not contain overflow cells. |
*/ |
#define findCell(P,I) \ |
- ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) |
-#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I))))) |
+ ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) |
+#define findCellPastPtr(P,I) \ |
+ ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) |
/* |
-** This a more complex version of findCell() that works for |
-** pages that do contain overflow cells. |
+** This is common tail processing for btreeParseCellPtr() and |
+** btreeParseCellPtrIndex() for the case when the cell does not fit entirely |
+** on a single B-tree page. Make necessary adjustments to the CellInfo |
+** structure. |
*/ |
-static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
- int i; |
- assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
- for(i=pPage->nOverflow-1; i>=0; i--){ |
- int k; |
- k = pPage->aiOvfl[i]; |
- if( k<=iCell ){ |
- if( k==iCell ){ |
- return pPage->apOvfl[i]; |
- } |
- iCell--; |
- } |
+static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( |
+ MemPage *pPage, /* Page containing the cell */ |
+ u8 *pCell, /* Pointer to the cell text. */ |
+ CellInfo *pInfo /* Fill in this structure */ |
+){ |
+ /* If the payload will not fit completely on the local page, we have |
+ ** to decide how much to store locally and how much to spill onto |
+ ** overflow pages. The strategy is to minimize the amount of unused |
+ ** space on overflow pages while keeping the amount of local storage |
+ ** in between minLocal and maxLocal. |
+ ** |
+ ** Warning: changing the way overflow payload is distributed in any |
+ ** way will result in an incompatible file format. |
+ */ |
+ int minLocal; /* Minimum amount of payload held locally */ |
+ int maxLocal; /* Maximum amount of payload held locally */ |
+ int surplus; /* Overflow payload available for local storage */ |
+ |
+ minLocal = pPage->minLocal; |
+ maxLocal = pPage->maxLocal; |
+ surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4); |
+ testcase( surplus==maxLocal ); |
+ testcase( surplus==maxLocal+1 ); |
+ if( surplus <= maxLocal ){ |
+ pInfo->nLocal = (u16)surplus; |
+ }else{ |
+ pInfo->nLocal = (u16)minLocal; |
} |
- return findCell(pPage, iCell); |
+ pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4; |
} |
/* |
-** Parse a cell content block and fill in the CellInfo structure. There |
-** are two versions of this function. btreeParseCell() takes a |
-** cell index as the second argument and btreeParseCellPtr() |
-** takes a pointer to the body of the cell as its second argument. |
+** The following routines are implementations of the MemPage.xParseCell() |
+** method. |
+** |
+** Parse a cell content block and fill in the CellInfo structure. |
+** |
+** btreeParseCellPtr() => table btree leaf nodes |
+** btreeParseCellNoPayload() => table btree internal nodes |
+** btreeParseCellPtrIndex() => index btree nodes |
+** |
+** There is also a wrapper function btreeParseCell() that works for |
+** all MemPage types and that references the cell by index rather than |
+** by pointer. |
*/ |
+static void btreeParseCellPtrNoPayload( |
+ MemPage *pPage, /* Page containing the cell */ |
+ u8 *pCell, /* Pointer to the cell text. */ |
+ CellInfo *pInfo /* Fill in this structure */ |
+){ |
+ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
+ assert( pPage->leaf==0 ); |
+ assert( pPage->noPayload ); |
+ assert( pPage->childPtrSize==4 ); |
+#ifndef SQLITE_DEBUG |
+ UNUSED_PARAMETER(pPage); |
+#endif |
+ pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
+ pInfo->nPayload = 0; |
+ pInfo->nLocal = 0; |
+ pInfo->pPayload = 0; |
+ return; |
+} |
static void btreeParseCellPtr( |
MemPage *pPage, /* Page containing the cell */ |
u8 *pCell, /* Pointer to the cell text. */ |
@@ -978,26 +1093,54 @@ static void btreeParseCellPtr( |
){ |
u8 *pIter; /* For scanning through pCell */ |
u32 nPayload; /* Number of bytes of cell payload */ |
+ u64 iKey; /* Extracted Key value */ |
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
assert( pPage->leaf==0 || pPage->leaf==1 ); |
- if( pPage->intKeyLeaf ){ |
- assert( pPage->childPtrSize==0 ); |
- pIter = pCell + getVarint32(pCell, nPayload); |
- pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
- }else if( pPage->noPayload ){ |
- assert( pPage->childPtrSize==4 ); |
- pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
- pInfo->nPayload = 0; |
- pInfo->nLocal = 0; |
- pInfo->iOverflow = 0; |
- pInfo->pPayload = 0; |
- return; |
- }else{ |
- pIter = pCell + pPage->childPtrSize; |
- pIter += getVarint32(pIter, nPayload); |
- pInfo->nKey = nPayload; |
+ assert( pPage->intKeyLeaf || pPage->noPayload ); |
+ assert( pPage->noPayload==0 ); |
+ assert( pPage->intKeyLeaf ); |
+ assert( pPage->childPtrSize==0 ); |
+ pIter = pCell; |
+ |
+ /* The next block of code is equivalent to: |
+ ** |
+ ** pIter += getVarint32(pIter, nPayload); |
+ ** |
+ ** The code is inlined to avoid a function call. |
+ */ |
+ nPayload = *pIter; |
+ if( nPayload>=0x80 ){ |
+ u8 *pEnd = &pIter[8]; |
+ nPayload &= 0x7f; |
+ do{ |
+ nPayload = (nPayload<<7) | (*++pIter & 0x7f); |
+ }while( (*pIter)>=0x80 && pIter<pEnd ); |
+ } |
+ pIter++; |
+ |
+ /* The next block of code is equivalent to: |
+ ** |
+ ** pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
+ ** |
+ ** The code is inlined to avoid a function call. |
+ */ |
+ iKey = *pIter; |
+ if( iKey>=0x80 ){ |
+ u8 *pEnd = &pIter[7]; |
+ iKey &= 0x7f; |
+ while(1){ |
+ iKey = (iKey<<7) | (*++pIter & 0x7f); |
+ if( (*pIter)<0x80 ) break; |
+ if( pIter>=pEnd ){ |
+ iKey = (iKey<<8) | *++pIter; |
+ break; |
+ } |
+ } |
} |
+ pIter++; |
+ |
+ pInfo->nKey = *(i64*)&iKey; |
pInfo->nPayload = nPayload; |
pInfo->pPayload = pIter; |
testcase( nPayload==pPage->maxLocal ); |
@@ -1009,33 +1152,46 @@ static void btreeParseCellPtr( |
pInfo->nSize = nPayload + (u16)(pIter - pCell); |
if( pInfo->nSize<4 ) pInfo->nSize = 4; |
pInfo->nLocal = (u16)nPayload; |
- pInfo->iOverflow = 0; |
}else{ |
- /* If the payload will not fit completely on the local page, we have |
- ** to decide how much to store locally and how much to spill onto |
- ** overflow pages. The strategy is to minimize the amount of unused |
- ** space on overflow pages while keeping the amount of local storage |
- ** in between minLocal and maxLocal. |
- ** |
- ** Warning: changing the way overflow payload is distributed in any |
- ** way will result in an incompatible file format. |
+ btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); |
+ } |
+} |
+static void btreeParseCellPtrIndex( |
+ MemPage *pPage, /* Page containing the cell */ |
+ u8 *pCell, /* Pointer to the cell text. */ |
+ CellInfo *pInfo /* Fill in this structure */ |
+){ |
+ u8 *pIter; /* For scanning through pCell */ |
+ u32 nPayload; /* Number of bytes of cell payload */ |
+ |
+ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
+ assert( pPage->leaf==0 || pPage->leaf==1 ); |
+ assert( pPage->intKeyLeaf==0 ); |
+ assert( pPage->noPayload==0 ); |
+ pIter = pCell + pPage->childPtrSize; |
+ nPayload = *pIter; |
+ if( nPayload>=0x80 ){ |
+ u8 *pEnd = &pIter[8]; |
+ nPayload &= 0x7f; |
+ do{ |
+ nPayload = (nPayload<<7) | (*++pIter & 0x7f); |
+ }while( *(pIter)>=0x80 && pIter<pEnd ); |
+ } |
+ pIter++; |
+ pInfo->nKey = nPayload; |
+ pInfo->nPayload = nPayload; |
+ pInfo->pPayload = pIter; |
+ testcase( nPayload==pPage->maxLocal ); |
+ testcase( nPayload==pPage->maxLocal+1 ); |
+ if( nPayload<=pPage->maxLocal ){ |
+ /* This is the (easy) common case where the entire payload fits |
+ ** on the local page. No overflow is required. |
*/ |
- int minLocal; /* Minimum amount of payload held locally */ |
- int maxLocal; /* Maximum amount of payload held locally */ |
- int surplus; /* Overflow payload available for local storage */ |
- |
- minLocal = pPage->minLocal; |
- maxLocal = pPage->maxLocal; |
- surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
- testcase( surplus==maxLocal ); |
- testcase( surplus==maxLocal+1 ); |
- if( surplus <= maxLocal ){ |
- pInfo->nLocal = (u16)surplus; |
- }else{ |
- pInfo->nLocal = (u16)minLocal; |
- } |
- pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); |
- pInfo->nSize = pInfo->iOverflow + 4; |
+ pInfo->nSize = nPayload + (u16)(pIter - pCell); |
+ if( pInfo->nSize<4 ) pInfo->nSize = 4; |
+ pInfo->nLocal = (u16)nPayload; |
+ }else{ |
+ btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); |
} |
} |
static void btreeParseCell( |
@@ -1043,14 +1199,20 @@ static void btreeParseCell( |
int iCell, /* The cell index. First cell is 0 */ |
CellInfo *pInfo /* Fill in this structure */ |
){ |
- btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
+ pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo); |
} |
/* |
+** The following routines are implementations of the MemPage.xCellSize |
+** method. |
+** |
** Compute the total number of bytes that a Cell needs in the cell |
** data area of the btree-page. The return number includes the cell |
** data header and the local payload, but not any overflow page or |
** the space used by the cell pointer. |
+** |
+** cellSizePtrNoPayload() => table internal nodes |
+** cellSizePtr() => all index nodes & table leaf nodes |
*/ |
static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
@@ -1063,18 +1225,13 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
** this function verifies that this invariant is not violated. */ |
CellInfo debuginfo; |
- btreeParseCellPtr(pPage, pCell, &debuginfo); |
+ pPage->xParseCell(pPage, pCell, &debuginfo); |
#endif |
- if( pPage->noPayload ){ |
- pEnd = &pIter[9]; |
- while( (*pIter++)&0x80 && pIter<pEnd ); |
- assert( pPage->childPtrSize==4 ); |
- return (u16)(pIter - pCell); |
- } |
+ assert( pPage->noPayload==0 ); |
nSize = *pIter; |
if( nSize>=0x80 ){ |
- pEnd = &pIter[9]; |
+ pEnd = &pIter[8]; |
nSize &= 0x7f; |
do{ |
nSize = (nSize<<7) | (*++pIter & 0x7f); |
@@ -1106,12 +1263,34 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
return (u16)nSize; |
} |
+static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ |
+ u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ |
+ u8 *pEnd; /* End mark for a varint */ |
+ |
+#ifdef SQLITE_DEBUG |
+ /* The value returned by this function should always be the same as |
+ ** the (CellInfo.nSize) value found by doing a full parse of the |
+ ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
+ ** this function verifies that this invariant is not violated. */ |
+ CellInfo debuginfo; |
+ pPage->xParseCell(pPage, pCell, &debuginfo); |
+#else |
+ UNUSED_PARAMETER(pPage); |
+#endif |
+ |
+ assert( pPage->childPtrSize==4 ); |
+ pEnd = pIter + 9; |
+ while( (*pIter++)&0x80 && pIter<pEnd ); |
+ assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB ); |
+ return (u16)(pIter - pCell); |
+} |
+ |
#ifdef SQLITE_DEBUG |
/* This variation on cellSizePtr() is used inside of assert() statements |
** only. */ |
static u16 cellSize(MemPage *pPage, int iCell){ |
- return cellSizePtr(pPage, findCell(pPage, iCell)); |
+ return pPage->xCellSize(pPage, findCell(pPage, iCell)); |
} |
#endif |
@@ -1125,9 +1304,9 @@ static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
CellInfo info; |
if( *pRC ) return; |
assert( pCell!=0 ); |
- btreeParseCellPtr(pPage, pCell, &info); |
- if( info.iOverflow ){ |
- Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
+ pPage->xParseCell(pPage, pCell, &info); |
+ if( info.nLocal<info.nPayload ){ |
+ Pgno ovfl = get4byte(&pCell[info.nSize-4]); |
ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); |
} |
} |
@@ -1139,6 +1318,11 @@ static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
** end of the page and all free space is collected into one |
** big FreeBlk that occurs in between the header and cell |
** pointer array and the cell content area. |
+** |
+** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a |
+** b-tree page so that there are no freeblocks or fragment bytes, all |
+** unused bytes are contained in the unallocated space region, and all |
+** cells are packed tightly at the end of the page. |
*/ |
static int defragmentPage(MemPage *pPage){ |
int i; /* Loop counter */ |
@@ -1151,6 +1335,7 @@ static int defragmentPage(MemPage *pPage){ |
int nCell; /* Number of cells on the page */ |
unsigned char *data; /* The page data */ |
unsigned char *temp; /* Temp area for cell content */ |
+ unsigned char *src; /* Source of content */ |
int iCellFirst; /* First allowable cell index */ |
int iCellLast; /* Last possible cell index */ |
@@ -1160,15 +1345,13 @@ static int defragmentPage(MemPage *pPage){ |
assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
assert( pPage->nOverflow==0 ); |
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
- temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
- data = pPage->aData; |
+ temp = 0; |
+ src = data = pPage->aData; |
hdr = pPage->hdrOffset; |
cellOffset = pPage->cellOffset; |
nCell = pPage->nCell; |
assert( nCell==get2byte(&data[hdr+3]) ); |
usableSize = pPage->pBt->usableSize; |
- cbrk = get2byte(&data[hdr+5]); |
- memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
cbrk = usableSize; |
iCellFirst = cellOffset + 2*nCell; |
iCellLast = usableSize - 4; |
@@ -1178,31 +1361,31 @@ static int defragmentPage(MemPage *pPage){ |
pc = get2byte(pAddr); |
testcase( pc==iCellFirst ); |
testcase( pc==iCellLast ); |
-#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
/* These conditions have already been verified in btreeInitPage() |
- ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined |
+ ** if PRAGMA cell_size_check=ON. |
*/ |
if( pc<iCellFirst || pc>iCellLast ){ |
return SQLITE_CORRUPT_BKPT; |
} |
-#endif |
assert( pc>=iCellFirst && pc<=iCellLast ); |
- size = cellSizePtr(pPage, &temp[pc]); |
+ size = pPage->xCellSize(pPage, &src[pc]); |
cbrk -= size; |
-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
- if( cbrk<iCellFirst ){ |
- return SQLITE_CORRUPT_BKPT; |
- } |
-#else |
if( cbrk<iCellFirst || pc+size>usableSize ){ |
return SQLITE_CORRUPT_BKPT; |
} |
-#endif |
assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
testcase( cbrk+size==usableSize ); |
testcase( pc+size==usableSize ); |
- memcpy(&data[cbrk], &temp[pc], size); |
put2byte(pAddr, cbrk); |
+ if( temp==0 ){ |
+ int x; |
+ if( cbrk==pc ) continue; |
+ temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
+ x = get2byte(&data[hdr+5]); |
+ memcpy(&temp[x], &data[x], (cbrk+size) - x); |
+ src = temp; |
+ } |
+ memcpy(&data[cbrk], &src[pc], size); |
} |
assert( cbrk>=iCellFirst ); |
put2byte(&data[hdr+5], cbrk); |
@@ -1218,6 +1401,70 @@ static int defragmentPage(MemPage *pPage){ |
} |
/* |
+** Search the free-list on page pPg for space to store a cell nByte bytes in |
+** size. If one can be found, return a pointer to the space and remove it |
+** from the free-list. |
+** |
+** If no suitable space can be found on the free-list, return NULL. |
+** |
+** This function may detect corruption within pPg. If corruption is |
+** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. |
+** |
+** Slots on the free list that are between 1 and 3 bytes larger than nByte |
+** will be ignored if adding the extra space to the fragmentation count |
+** causes the fragmentation count to exceed 60. |
+*/ |
+static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ |
+ const int hdr = pPg->hdrOffset; |
+ u8 * const aData = pPg->aData; |
+ int iAddr = hdr + 1; |
+ int pc = get2byte(&aData[iAddr]); |
+ int x; |
+ int usableSize = pPg->pBt->usableSize; |
+ |
+ assert( pc>0 ); |
+ do{ |
+ int size; /* Size of the free slot */ |
+ /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
+ ** increasing offset. */ |
+ if( pc>usableSize-4 || pc<iAddr+4 ){ |
+ *pRc = SQLITE_CORRUPT_BKPT; |
+ return 0; |
+ } |
+ /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each |
+ ** freeblock form a big-endian integer which is the size of the freeblock |
+ ** in bytes, including the 4-byte header. */ |
+ size = get2byte(&aData[pc+2]); |
+ if( (x = size - nByte)>=0 ){ |
+ testcase( x==4 ); |
+ testcase( x==3 ); |
+ if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){ |
+ *pRc = SQLITE_CORRUPT_BKPT; |
+ return 0; |
+ }else if( x<4 ){ |
+ /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total |
+ ** number of bytes in fragments may not exceed 60. */ |
+ if( aData[hdr+7]>57 ) return 0; |
+ |
+ /* Remove the slot from the free-list. Update the number of |
+ ** fragmented bytes within the page. */ |
+ memcpy(&aData[iAddr], &aData[pc], 2); |
+ aData[hdr+7] += (u8)x; |
+ }else{ |
+ /* The slot remains on the free-list. Reduce its size to account |
+ ** for the portion used by the new allocation. */ |
+ put2byte(&aData[pc+2], x); |
+ } |
+ return &aData[pc + x]; |
+ } |
+ iAddr = pc; |
+ pc = get2byte(&aData[pc]); |
+ }while( pc ); |
+ |
+ return 0; |
+} |
+ |
+/* |
** Allocate nByte bytes of space from within the B-Tree page passed |
** as the first argument. Write into *pIdx the index into pPage->aData[] |
** of the first byte of allocated space. Return either SQLITE_OK or |
@@ -1234,9 +1481,8 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
int top; /* First byte of cell content area */ |
+ int rc = SQLITE_OK; /* Integer return code */ |
int gap; /* First byte of gap between cell pointers and cell content */ |
- int rc; /* Integer return code */ |
- int usableSize; /* Usable size of the page */ |
assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
assert( pPage->pBt ); |
@@ -1244,15 +1490,20 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
assert( nByte>=0 ); /* Minimum cell size is 4 */ |
assert( pPage->nFree>=nByte ); |
assert( pPage->nOverflow==0 ); |
- usableSize = pPage->pBt->usableSize; |
- assert( nByte < usableSize-8 ); |
+ assert( nByte < (int)(pPage->pBt->usableSize-8) ); |
assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
gap = pPage->cellOffset + 2*pPage->nCell; |
assert( gap<=65536 ); |
+ /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size |
+ ** and the reserved space is zero (the usual value for reserved space) |
+ ** then the cell content offset of an empty page wants to be 65536. |
+ ** However, that integer is too large to be stored in a 2-byte unsigned |
+ ** integer, so a value of 0 is used in its place. */ |
top = get2byte(&data[hdr+5]); |
+ assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ |
if( gap>top ){ |
- if( top==0 ){ |
+ if( top==0 && pPage->pBt->usableSize==65536 ){ |
top = 65536; |
}else{ |
return SQLITE_CORRUPT_BKPT; |
@@ -1266,34 +1517,14 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
testcase( gap+2==top ); |
testcase( gap+1==top ); |
testcase( gap==top ); |
- if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
- int pc, addr; |
- for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
- int size; /* Size of the free slot */ |
- if( pc>usableSize-4 || pc<addr+4 ){ |
- return SQLITE_CORRUPT_BKPT; |
- } |
- size = get2byte(&data[pc+2]); |
- if( size>=nByte ){ |
- int x = size - nByte; |
- testcase( x==4 ); |
- testcase( x==3 ); |
- if( x<4 ){ |
- if( data[hdr+7]>=60 ) goto defragment_page; |
- /* Remove the slot from the free-list. Update the number of |
- ** fragmented bytes within the page. */ |
- memcpy(&data[addr], &data[pc], 2); |
- data[hdr+7] += (u8)x; |
- }else if( size+pc > usableSize ){ |
- return SQLITE_CORRUPT_BKPT; |
- }else{ |
- /* The slot remains on the free-list. Reduce its size to account |
- ** for the portion used by the new allocation. */ |
- put2byte(&data[pc+2], x); |
- } |
- *pIdx = pc + x; |
- return SQLITE_OK; |
- } |
+ if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ |
+ u8 *pSpace = pageFindSlot(pPage, nByte, &rc); |
+ if( pSpace ){ |
+ assert( pSpace>=data && (pSpace - data)<65536 ); |
+ *pIdx = (int)(pSpace - data); |
+ return SQLITE_OK; |
+ }else if( rc ){ |
+ return rc; |
} |
} |
@@ -1302,8 +1533,7 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
*/ |
testcase( gap+2+nByte==top ); |
if( gap+2+nByte>top ){ |
-defragment_page: |
- testcase( pPage->nCell==0 ); |
+ assert( pPage->nCell>0 || CORRUPT_DB ); |
rc = defragmentPage(pPage); |
if( rc ) return rc; |
top = get2byteNotZero(&data[hdr+5]); |
@@ -1349,8 +1579,8 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
assert( pPage->pBt!=0 ); |
assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
- assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
- assert( iEnd <= pPage->pBt->usableSize ); |
+ assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
+ assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
assert( iSize>=4 ); /* Minimum cell size is 4 */ |
assert( iStart<=iLast ); |
@@ -1378,7 +1608,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
/* At this point: |
** iFreeBlk: First freeblock after iStart, or zero if none |
- ** iPtr: The address of a pointer iFreeBlk |
+ ** iPtr: The address of a pointer to iFreeBlk |
** |
** Check to see if iFreeBlk should be coalesced onto the end of iStart. |
*/ |
@@ -1386,6 +1616,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
nFrag = iFreeBlk - iEnd; |
if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; |
iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); |
+ if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT; |
iSize = iEnd - iStart; |
iFreeBlk = get2byte(&data[iFreeBlk]); |
} |
@@ -1443,20 +1674,44 @@ static int decodeFlags(MemPage *pPage, int flagByte){ |
pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
flagByte &= ~PTF_LEAF; |
pPage->childPtrSize = 4-4*pPage->leaf; |
+ pPage->xCellSize = cellSizePtr; |
pBt = pPage->pBt; |
if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
+ /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior |
+ ** table b-tree page. */ |
+ assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); |
+ /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf |
+ ** table b-tree page. */ |
+ assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); |
pPage->intKey = 1; |
- pPage->intKeyLeaf = pPage->leaf; |
- pPage->noPayload = !pPage->leaf; |
+ if( pPage->leaf ){ |
+ pPage->intKeyLeaf = 1; |
+ pPage->noPayload = 0; |
+ pPage->xParseCell = btreeParseCellPtr; |
+ }else{ |
+ pPage->intKeyLeaf = 0; |
+ pPage->noPayload = 1; |
+ pPage->xCellSize = cellSizePtrNoPayload; |
+ pPage->xParseCell = btreeParseCellPtrNoPayload; |
+ } |
pPage->maxLocal = pBt->maxLeaf; |
pPage->minLocal = pBt->minLeaf; |
}else if( flagByte==PTF_ZERODATA ){ |
+ /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior |
+ ** index b-tree page. */ |
+ assert( (PTF_ZERODATA)==2 ); |
+ /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf |
+ ** index b-tree page. */ |
+ assert( (PTF_ZERODATA|PTF_LEAF)==10 ); |
pPage->intKey = 0; |
pPage->intKeyLeaf = 0; |
pPage->noPayload = 0; |
+ pPage->xParseCell = btreeParseCellPtrIndex; |
pPage->maxLocal = pBt->maxLocal; |
pPage->minLocal = pBt->minLocal; |
}else{ |
+ /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is |
+ ** an error. */ |
return SQLITE_CORRUPT_BKPT; |
} |
pPage->max1bytePayload = pBt->max1bytePayload; |
@@ -1475,6 +1730,7 @@ static int decodeFlags(MemPage *pPage, int flagByte){ |
static int btreeInitPage(MemPage *pPage){ |
assert( pPage->pBt!=0 ); |
+ assert( pPage->pBt->db!=0 ); |
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
@@ -1496,21 +1752,34 @@ static int btreeInitPage(MemPage *pPage){ |
hdr = pPage->hdrOffset; |
data = pPage->aData; |
+ /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating |
+ ** the b-tree page type. */ |
if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
pPage->maskPage = (u16)(pBt->pageSize - 1); |
pPage->nOverflow = 0; |
usableSize = pBt->usableSize; |
- pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
+ pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; |
pPage->aDataEnd = &data[usableSize]; |
pPage->aCellIdx = &data[cellOffset]; |
+ pPage->aDataOfst = &data[pPage->childPtrSize]; |
+ /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates |
+ ** the start of the cell content area. A zero value for this integer is |
+ ** interpreted as 65536. */ |
top = get2byteNotZero(&data[hdr+5]); |
+ /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
+ ** number of cells on the page. */ |
pPage->nCell = get2byte(&data[hdr+3]); |
if( pPage->nCell>MX_CELL(pBt) ){ |
/* To many cells for a single page. The page must be corrupt */ |
return SQLITE_CORRUPT_BKPT; |
} |
testcase( pPage->nCell==MX_CELL(pBt) ); |
+ /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only |
+ ** possible for a root page of a table that contains no rows) then the |
+ ** offset to the cell content area will equal the page size minus the |
+ ** bytes of reserved space. */ |
+ assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); |
/* A malformed database page might cause us to read past the end |
** of page when parsing a cell. |
@@ -1521,20 +1790,19 @@ static int btreeInitPage(MemPage *pPage){ |
*/ |
iCellFirst = cellOffset + 2*pPage->nCell; |
iCellLast = usableSize - 4; |
-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
- { |
+ if( pBt->db->flags & SQLITE_CellSizeCk ){ |
int i; /* Index into the cell pointer array */ |
int sz; /* Size of a cell */ |
if( !pPage->leaf ) iCellLast--; |
for(i=0; i<pPage->nCell; i++){ |
- pc = get2byte(&data[cellOffset+i*2]); |
+ pc = get2byteAligned(&data[cellOffset+i*2]); |
testcase( pc==iCellFirst ); |
testcase( pc==iCellLast ); |
if( pc<iCellFirst || pc>iCellLast ){ |
return SQLITE_CORRUPT_BKPT; |
} |
- sz = cellSizePtr(pPage, &data[pc]); |
+ sz = pPage->xCellSize(pPage, &data[pc]); |
testcase( pc+sz==usableSize ); |
if( pc+sz>usableSize ){ |
return SQLITE_CORRUPT_BKPT; |
@@ -1542,15 +1810,21 @@ static int btreeInitPage(MemPage *pPage){ |
} |
if( !pPage->leaf ) iCellLast++; |
} |
-#endif |
- /* Compute the total free space on the page */ |
+ /* Compute the total free space on the page |
+ ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the |
+ ** start of the first freeblock on the page, or is zero if there are no |
+ ** freeblocks. */ |
pc = get2byte(&data[hdr+1]); |
- nFree = data[hdr+7] + top; |
+ nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ |
while( pc>0 ){ |
u16 next, size; |
if( pc<iCellFirst || pc>iCellLast ){ |
- /* Start of free block is off the page */ |
+ /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will |
+ ** always be at least one cell before the first freeblock. |
+ ** |
+ ** Or, the freeblock is off the end of the page |
+ */ |
return SQLITE_CORRUPT_BKPT; |
} |
next = get2byte(&data[pc]); |
@@ -1608,6 +1882,7 @@ static void zeroPage(MemPage *pPage, int flags){ |
pPage->cellOffset = first; |
pPage->aDataEnd = &data[pBt->usableSize]; |
pPage->aCellIdx = &data[first]; |
+ pPage->aDataOfst = &data[pPage->childPtrSize]; |
pPage->nOverflow = 0; |
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
pPage->maskPage = (u16)(pBt->pageSize - 1); |
@@ -1622,20 +1897,23 @@ static void zeroPage(MemPage *pPage, int flags){ |
*/ |
static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
- pPage->aData = sqlite3PagerGetData(pDbPage); |
- pPage->pDbPage = pDbPage; |
- pPage->pBt = pBt; |
- pPage->pgno = pgno; |
- pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
+ if( pgno!=pPage->pgno ){ |
+ pPage->aData = sqlite3PagerGetData(pDbPage); |
+ pPage->pDbPage = pDbPage; |
+ pPage->pBt = pBt; |
+ pPage->pgno = pgno; |
+ pPage->hdrOffset = pgno==1 ? 100 : 0; |
+ } |
+ assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); |
return pPage; |
} |
/* |
** Get a page from the pager. Initialize the MemPage.pBt and |
-** MemPage.aData elements if needed. |
+** MemPage.aData elements if needed. See also: btreeGetUnusedPage(). |
** |
-** If the noContent flag is set, it means that we do not care about |
-** the content of the page at this time. So do not go to the disk |
+** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care |
+** about the content of the page at this time. So do not go to the disk |
** to fetch the content. Just fill in the content with zeros for now. |
** If in the future we call sqlite3PagerWrite() on this page, that |
** means we have started to be concerned about content and the disk |
@@ -1652,7 +1930,7 @@ static int btreeGetPage( |
assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); |
assert( sqlite3_mutex_held(pBt->mutex) ); |
- rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); |
+ rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); |
if( rc ) return rc; |
*ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
return SQLITE_OK; |
@@ -1687,35 +1965,63 @@ u32 sqlite3BtreeLastPage(Btree *p){ |
} |
/* |
-** Get a page from the pager and initialize it. This routine is just a |
-** convenience wrapper around separate calls to btreeGetPage() and |
-** btreeInitPage(). |
+** Get a page from the pager and initialize it. |
+** |
+** If pCur!=0 then the page is being fetched as part of a moveToChild() |
+** call. Do additional sanity checking on the page in this case. |
+** And if the fetch fails, this routine must decrement pCur->iPage. |
** |
-** If an error occurs, then the value *ppPage is set to is undefined. It |
+** The page is fetched as read-write unless pCur is not NULL and is |
+** a read-only cursor. |
+** |
+** If an error occurs, then *ppPage is undefined. It |
** may remain unchanged, or it may be set to an invalid value. |
*/ |
static int getAndInitPage( |
BtShared *pBt, /* The database file */ |
Pgno pgno, /* Number of the page to get */ |
MemPage **ppPage, /* Write the page pointer here */ |
- int bReadonly /* PAGER_GET_READONLY or 0 */ |
+ BtCursor *pCur, /* Cursor to receive the page, or NULL */ |
+ int bReadOnly /* True for a read-only page */ |
){ |
int rc; |
+ DbPage *pDbPage; |
assert( sqlite3_mutex_held(pBt->mutex) ); |
- assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 ); |
+ assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] ); |
+ assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); |
+ assert( pCur==0 || pCur->iPage>0 ); |
if( pgno>btreePagecount(pBt) ){ |
rc = SQLITE_CORRUPT_BKPT; |
- }else{ |
- rc = btreeGetPage(pBt, pgno, ppPage, bReadonly); |
- if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
- rc = btreeInitPage(*ppPage); |
- if( rc!=SQLITE_OK ){ |
- releasePage(*ppPage); |
- } |
+ goto getAndInitPage_error; |
+ } |
+ rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); |
+ if( rc ){ |
+ goto getAndInitPage_error; |
+ } |
+ *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
+ if( (*ppPage)->isInit==0 ){ |
+ btreePageFromDbPage(pDbPage, pgno, pBt); |
+ rc = btreeInitPage(*ppPage); |
+ if( rc!=SQLITE_OK ){ |
+ releasePage(*ppPage); |
+ goto getAndInitPage_error; |
} |
} |
+ assert( (*ppPage)->pgno==pgno ); |
+ assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); |
+ |
+ /* If obtaining a child page for a cursor, we must verify that the page is |
+ ** compatible with the root page. */ |
+ if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ releasePage(*ppPage); |
+ goto getAndInitPage_error; |
+ } |
+ return SQLITE_OK; |
+getAndInitPage_error: |
+ if( pCur ) pCur->iPage--; |
testcase( pgno==0 ); |
assert( pgno!=0 || rc==SQLITE_CORRUPT ); |
return rc; |
@@ -1725,18 +2031,49 @@ static int getAndInitPage( |
** Release a MemPage. This should be called once for each prior |
** call to btreeGetPage. |
*/ |
+static void releasePageNotNull(MemPage *pPage){ |
+ assert( pPage->aData ); |
+ assert( pPage->pBt ); |
+ assert( pPage->pDbPage!=0 ); |
+ assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
+ assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
+ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
+ sqlite3PagerUnrefNotNull(pPage->pDbPage); |
+} |
static void releasePage(MemPage *pPage){ |
- if( pPage ){ |
- assert( pPage->aData ); |
- assert( pPage->pBt ); |
- assert( pPage->pDbPage!=0 ); |
- assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
- assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
- assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
- sqlite3PagerUnrefNotNull(pPage->pDbPage); |
+ if( pPage ) releasePageNotNull(pPage); |
+} |
+ |
+/* |
+** Get an unused page. |
+** |
+** This works just like btreeGetPage() with the addition: |
+** |
+** * If the page is already in use for some other purpose, immediately |
+** release it and return an SQLITE_CURRUPT error. |
+** * Make sure the isInit flag is clear |
+*/ |
+static int btreeGetUnusedPage( |
+ BtShared *pBt, /* The btree */ |
+ Pgno pgno, /* Number of the page to fetch */ |
+ MemPage **ppPage, /* Return the page in this parameter */ |
+ int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ |
+){ |
+ int rc = btreeGetPage(pBt, pgno, ppPage, flags); |
+ if( rc==SQLITE_OK ){ |
+ if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
+ releasePage(*ppPage); |
+ *ppPage = 0; |
+ return SQLITE_CORRUPT_BKPT; |
+ } |
+ (*ppPage)->isInit = 0; |
+ }else{ |
+ *ppPage = 0; |
} |
+ return rc; |
} |
+ |
/* |
** During a rollback, when the pager reloads information into the cache |
** so that the cache is restored to its original state at the start of |
@@ -1859,16 +2196,18 @@ int sqlite3BtreeOpen( |
*/ |
if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){ |
if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
+ int nFilename = sqlite3Strlen30(zFilename)+1; |
int nFullPathname = pVfs->mxPathname+1; |
- char *zFullPathname = sqlite3Malloc(nFullPathname); |
+ char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)); |
MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
+ |
p->sharable = 1; |
if( !zFullPathname ){ |
sqlite3_free(p); |
return SQLITE_NOMEM; |
} |
if( isMemdb ){ |
- memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1); |
+ memcpy(zFullPathname, zFilename, nFilename); |
}else{ |
rc = sqlite3OsFullPathname(pVfs, zFilename, |
nFullPathname, zFullPathname); |
@@ -1925,8 +2264,8 @@ int sqlite3BtreeOpen( |
** the right size. This is to guard against size changes that result |
** when compiling on a different architecture. |
*/ |
- assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
- assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
+ assert( sizeof(i64)==8 ); |
+ assert( sizeof(u64)==8 ); |
assert( sizeof(u32)==4 ); |
assert( sizeof(u16)==2 ); |
assert( sizeof(Pgno)==4 ); |
@@ -1956,6 +2295,9 @@ int sqlite3BtreeOpen( |
#ifdef SQLITE_SECURE_DELETE |
pBt->btsFlags |= BTS_SECURE_DELETE; |
#endif |
+ /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
+ ** determined by the 2-byte integer located at an offset of 16 bytes from |
+ ** the beginning of the database file. */ |
pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
|| ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
@@ -1974,6 +2316,9 @@ int sqlite3BtreeOpen( |
#endif |
nReserve = 0; |
}else{ |
+ /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
+ ** determined by the one-byte unsigned integer found at an offset of 20 |
+ ** into the database file header. */ |
nReserve = zDbHeader[20]; |
pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
#ifndef SQLITE_OMIT_AUTOVACUUM |
@@ -2207,19 +2552,11 @@ int sqlite3BtreeClose(Btree *p){ |
} |
/* |
-** Change the limit on the number of pages allowed in the cache. |
-** |
-** The maximum number of cache pages is set to the absolute |
-** value of mxPage. If mxPage is negative, the pager will |
-** operate asynchronously - it will not stop to do fsync()s |
-** to insure data is written to the disk surface before |
-** continuing. Transactions still work if synchronous is off, |
-** and the database cannot be corrupted if this program |
-** crashes. But if the operating system crashes or there is |
-** an abrupt power failure when synchronous is off, the database |
-** could be left in an inconsistent and unrecoverable state. |
-** Synchronous is on by default so database corruption is not |
-** normally a worry. |
+** Change the "soft" limit on the number of pages in the cache. |
+** Unused and unmodified pages will be recycled when the number of |
+** pages in the cache exceeds this soft limit. But the size of the |
+** cache is allowed to grow larger than this limit if it contains |
+** dirty pages or pages still in active use. |
*/ |
int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
BtShared *pBt = p->pBt; |
@@ -2230,6 +2567,26 @@ int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
return SQLITE_OK; |
} |
+/* |
+** Change the "spill" limit on the number of pages in the cache. |
+** If the number of pages exceeds this limit during a write transaction, |
+** the pager might attempt to "spill" pages to the journal early in |
+** order to free up memory. |
+** |
+** The value returned is the current spill size. If zero is passed |
+** as an argument, no changes are made to the spill size setting, so |
+** using mxPage of 0 is a way to query the current spill size. |
+*/ |
+int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){ |
+ BtShared *pBt = p->pBt; |
+ int res; |
+ assert( sqlite3_mutex_held(p->db->mutex) ); |
+ sqlite3BtreeEnter(p); |
+ res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage); |
+ sqlite3BtreeLeave(p); |
+ return res; |
+} |
+ |
#if SQLITE_MAX_MMAP_SIZE>0 |
/* |
** Change the limit on the amount of the database file that may be |
@@ -2307,6 +2664,9 @@ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
BtShared *pBt = p->pBt; |
assert( nReserve>=-1 && nReserve<=255 ); |
sqlite3BtreeEnter(p); |
+#if SQLITE_HAS_CODEC |
+ if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve; |
+#endif |
if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ |
sqlite3BtreeLeave(p); |
return SQLITE_READONLY; |
@@ -2318,7 +2678,7 @@ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
((pageSize-1)&pageSize)==0 ){ |
assert( (pageSize & 7)==0 ); |
- assert( !pBt->pPage1 && !pBt->pCursor ); |
+ assert( !pBt->pCursor ); |
pBt->pageSize = (u32)pageSize; |
freeTempSpace(pBt); |
} |
@@ -2336,7 +2696,6 @@ int sqlite3BtreeGetPageSize(Btree *p){ |
return p->pBt->pageSize; |
} |
-#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG) |
/* |
** This function is similar to sqlite3BtreeGetReserve(), except that it |
** may only be called if it is guaranteed that the b-tree mutex is already |
@@ -2349,25 +2708,33 @@ int sqlite3BtreeGetPageSize(Btree *p){ |
** database handle that owns *p, causing undefined behavior. |
*/ |
int sqlite3BtreeGetReserveNoMutex(Btree *p){ |
+ int n; |
assert( sqlite3_mutex_held(p->pBt->mutex) ); |
- return p->pBt->pageSize - p->pBt->usableSize; |
+ n = p->pBt->pageSize - p->pBt->usableSize; |
+ return n; |
} |
-#endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */ |
-#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
/* |
** Return the number of bytes of space at the end of every page that |
** are intentually left unused. This is the "reserved" space that is |
** sometimes used by extensions. |
+** |
+** If SQLITE_HAS_MUTEX is defined then the number returned is the |
+** greater of the current reserved space and the maximum requested |
+** reserve space. |
*/ |
-int sqlite3BtreeGetReserve(Btree *p){ |
+int sqlite3BtreeGetOptimalReserve(Btree *p){ |
int n; |
sqlite3BtreeEnter(p); |
- n = p->pBt->pageSize - p->pBt->usableSize; |
+ n = sqlite3BtreeGetReserveNoMutex(p); |
+#ifdef SQLITE_HAS_CODEC |
+ if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve; |
+#endif |
sqlite3BtreeLeave(p); |
return n; |
} |
+ |
/* |
** Set the maximum page count for a database if mxPage is positive. |
** No changes are made if mxPage is 0 or negative. |
@@ -2398,7 +2765,6 @@ int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ |
sqlite3BtreeLeave(p); |
return b; |
} |
-#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
/* |
** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
@@ -2483,6 +2849,9 @@ static int lockBtree(BtShared *pBt){ |
u32 usableSize; |
u8 *page1 = pPage1->aData; |
rc = SQLITE_NOTADB; |
+ /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins |
+ ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d |
+ ** 61 74 20 33 00. */ |
if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
goto page1_init_failed; |
} |
@@ -2523,15 +2892,21 @@ static int lockBtree(BtShared *pBt){ |
} |
#endif |
- /* The maximum embedded fraction must be exactly 25%. And the minimum |
- ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
+ /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload |
+ ** fractions and the leaf payload fraction values must be 64, 32, and 32. |
+ ** |
** The original design allowed these amounts to vary, but as of |
** version 3.6.0, we require them to be fixed. |
*/ |
if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
goto page1_init_failed; |
} |
+ /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
+ ** determined by the 2-byte integer located at an offset of 16 bytes from |
+ ** the beginning of the database file. */ |
pageSize = (page1[16]<<8) | (page1[17]<<16); |
+ /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two |
+ ** between 512 and 65536 inclusive. */ |
if( ((pageSize-1)&pageSize)!=0 |
|| pageSize>SQLITE_MAX_PAGE_SIZE |
|| pageSize<=256 |
@@ -2539,6 +2914,13 @@ static int lockBtree(BtShared *pBt){ |
goto page1_init_failed; |
} |
assert( (pageSize & 7)==0 ); |
+ /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte |
+ ** integer at offset 20 is the number of bytes of space at the end of |
+ ** each page to reserve for extensions. |
+ ** |
+ ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
+ ** determined by the one-byte unsigned integer found at an offset of 20 |
+ ** into the database file header. */ |
usableSize = pageSize - page1[20]; |
if( (u32)pageSize!=pBt->pageSize ){ |
/* After reading the first page of the database assuming a page size |
@@ -2559,6 +2941,9 @@ static int lockBtree(BtShared *pBt){ |
rc = SQLITE_CORRUPT_BKPT; |
goto page1_init_failed; |
} |
+ /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to |
+ ** be less than 480. In other words, if the page size is 512, then the |
+ ** reserved space size cannot exceed 32. */ |
if( usableSize<480 ){ |
goto page1_init_failed; |
} |
@@ -2643,7 +3028,7 @@ static void unlockBtreeIfUnused(BtShared *pBt){ |
assert( pPage1->aData ); |
assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
pBt->pPage1 = 0; |
- releasePage(pPage1); |
+ releasePageNotNull(pPage1); |
} |
} |
@@ -2948,20 +3333,22 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
u8 isInitOrig = pPage->isInit; |
int i; |
int nCell; |
+ int rc; |
- btreeInitPage(pPage); |
+ rc = btreeInitPage(pPage); |
+ if( rc ) return rc; |
nCell = pPage->nCell; |
for(i=0; i<nCell; i++){ |
u8 *pCell = findCell(pPage, i); |
if( eType==PTRMAP_OVERFLOW1 ){ |
CellInfo info; |
- btreeParseCellPtr(pPage, pCell, &info); |
- if( info.iOverflow |
- && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage |
- && iFrom==get4byte(&pCell[info.iOverflow]) |
+ pPage->xParseCell(pPage, pCell, &info); |
+ if( info.nLocal<info.nPayload |
+ && pCell+info.nSize-1<=pPage->aData+pPage->maskPage |
+ && iFrom==get4byte(pCell+info.nSize-4) |
){ |
- put4byte(&pCell[info.iOverflow], iTo); |
+ put4byte(pCell+info.nSize-4, iTo); |
break; |
} |
}else{ |
@@ -3255,7 +3642,7 @@ int sqlite3BtreeIncrVacuum(Btree *p){ |
static int autoVacuumCommit(BtShared *pBt){ |
int rc = SQLITE_OK; |
Pager *pPager = pBt->pPager; |
- VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
+ VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) |
assert( sqlite3_mutex_held(pBt->mutex) ); |
invalidateAllOverflowCache(pBt); |
@@ -3439,6 +3826,7 @@ int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ |
sqlite3BtreeLeave(p); |
return rc; |
} |
+ p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */ |
pBt->inTransaction = TRANS_READ; |
btreeClearHasContent(pBt); |
} |
@@ -3498,7 +3886,7 @@ int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ |
for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
int i; |
if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ |
- if( p->eState==CURSOR_VALID ){ |
+ if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ |
rc = saveCursorPosition(p); |
if( rc!=SQLITE_OK ){ |
(void)sqlite3BtreeTripAllCursors(pBtree, rc, 0); |
@@ -3696,25 +4084,27 @@ static int btreeCursor( |
BtCursor *pCur /* Space for new cursor */ |
){ |
BtShared *pBt = p->pBt; /* Shared b-tree handle */ |
+ BtCursor *pX; /* Looping over other all cursors */ |
assert( sqlite3BtreeHoldsMutex(p) ); |
- assert( wrFlag==0 || wrFlag==1 ); |
+ assert( wrFlag==0 |
+ || wrFlag==BTREE_WRCSR |
+ || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) |
+ ); |
/* The following assert statements verify that if this is a sharable |
** b-tree database, the connection is holding the required table locks, |
** and that no other connection has any open cursor that conflicts with |
** this lock. */ |
- assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) ); |
+ assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) ); |
assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); |
/* Assert that the caller has opened the required transaction. */ |
assert( p->inTrans>TRANS_NONE ); |
assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); |
assert( pBt->pPage1 && pBt->pPage1->aData ); |
+ assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
- if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ |
- return SQLITE_READONLY; |
- } |
if( wrFlag ){ |
allocateTempSpace(pBt); |
if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; |
@@ -3731,12 +4121,17 @@ static int btreeCursor( |
pCur->pKeyInfo = pKeyInfo; |
pCur->pBtree = p; |
pCur->pBt = pBt; |
- assert( wrFlag==0 || wrFlag==BTCF_WriteFlag ); |
- pCur->curFlags = wrFlag; |
- pCur->pNext = pBt->pCursor; |
- if( pCur->pNext ){ |
- pCur->pNext->pPrev = pCur; |
+ pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0; |
+ pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY; |
+ /* If there are two or more cursors on the same btree, then all such |
+ ** cursors *must* have the BTCF_Multiple flag set. */ |
+ for(pX=pBt->pCursor; pX; pX=pX->pNext){ |
+ if( pX->pgnoRoot==(Pgno)iTable ){ |
+ pX->curFlags |= BTCF_Multiple; |
+ pCur->curFlags |= BTCF_Multiple; |
+ } |
} |
+ pCur->pNext = pBt->pCursor; |
pBt->pCursor = pCur; |
pCur->eState = CURSOR_INVALID; |
return SQLITE_OK; |
@@ -3749,9 +4144,13 @@ int sqlite3BtreeCursor( |
BtCursor *pCur /* Write new cursor here */ |
){ |
int rc; |
- sqlite3BtreeEnter(p); |
- rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
- sqlite3BtreeLeave(p); |
+ if( iTable<1 ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ }else{ |
+ sqlite3BtreeEnter(p); |
+ rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
+ sqlite3BtreeLeave(p); |
+ } |
return rc; |
} |
@@ -3790,19 +4189,24 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
BtShared *pBt = pCur->pBt; |
sqlite3BtreeEnter(pBtree); |
sqlite3BtreeClearCursor(pCur); |
- if( pCur->pPrev ){ |
- pCur->pPrev->pNext = pCur->pNext; |
- }else{ |
+ assert( pBt->pCursor!=0 ); |
+ if( pBt->pCursor==pCur ){ |
pBt->pCursor = pCur->pNext; |
- } |
- if( pCur->pNext ){ |
- pCur->pNext->pPrev = pCur->pPrev; |
+ }else{ |
+ BtCursor *pPrev = pBt->pCursor; |
+ do{ |
+ if( pPrev->pNext==pCur ){ |
+ pPrev->pNext = pCur->pNext; |
+ break; |
+ } |
+ pPrev = pPrev->pNext; |
+ }while( ALWAYS(pPrev) ); |
} |
for(i=0; i<=pCur->iPage; i++){ |
releasePage(pCur->apPage[i]); |
} |
unlockBtreeIfUnused(pBt); |
- sqlite3DbFree(pBtree->db, pCur->aOverflow); |
+ sqlite3_free(pCur->aOverflow); |
/* sqlite3_free(pCur); */ |
sqlite3BtreeLeave(pBtree); |
} |
@@ -3816,13 +4220,6 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
** |
** BtCursor.info is a cache of the information in the current cell. |
** Using this cache reduces the number of calls to btreeParseCell(). |
-** |
-** 2007-06-25: There is a bug in some versions of MSVC that cause the |
-** compiler to crash when getCellInfo() is implemented as a macro. |
-** But there is a measureable speed advantage to using the macro on gcc |
-** (when less compiler optimizations like -Os or -O0 are used and the |
-** compiler is not doing aggressive inlining.) So we use a real function |
-** for MSVC and a macro for everything else. Ticket #2457. |
*/ |
#ifndef NDEBUG |
static void assertCellInfo(BtCursor *pCur){ |
@@ -3835,28 +4232,15 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
#else |
#define assertCellInfo(x) |
#endif |
-#ifdef _MSC_VER |
- /* Use a real function in MSVC to work around bugs in that compiler. */ |
- static void getCellInfo(BtCursor *pCur){ |
- if( pCur->info.nSize==0 ){ |
- int iPage = pCur->iPage; |
- btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
- pCur->curFlags |= BTCF_ValidNKey; |
- }else{ |
- assertCellInfo(pCur); |
- } |
- } |
-#else /* if not _MSC_VER */ |
- /* Use a macro in all other compilers so that the function is inlined */ |
-#define getCellInfo(pCur) \ |
- if( pCur->info.nSize==0 ){ \ |
- int iPage = pCur->iPage; \ |
- btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
- pCur->curFlags |= BTCF_ValidNKey; \ |
- }else{ \ |
- assertCellInfo(pCur); \ |
+static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ |
+ if( pCur->info.nSize==0 ){ |
+ int iPage = pCur->iPage; |
+ pCur->curFlags |= BTCF_ValidNKey; |
+ btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
+ }else{ |
+ assertCellInfo(pCur); |
} |
-#endif /* _MSC_VER */ |
+} |
#ifndef NDEBUG /* The next routine used only within assert() statements */ |
/* |
@@ -3904,6 +4288,8 @@ int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
assert( cursorHoldsMutex(pCur) ); |
assert( pCur->eState==CURSOR_VALID ); |
+ assert( pCur->iPage>=0 ); |
+ assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 ); |
getCellInfo(pCur); |
*pSize = pCur->info.nPayload; |
@@ -4097,6 +4483,7 @@ static int accessPayload( |
offset -= pCur->info.nLocal; |
} |
+ |
if( rc==SQLITE_OK && amt>0 ){ |
const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
Pgno nextPage; |
@@ -4114,8 +4501,8 @@ static int accessPayload( |
if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ |
int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
if( nOvfl>pCur->nOvflAlloc ){ |
- Pgno *aNew = (Pgno*)sqlite3DbRealloc( |
- pCur->pBtree->db, pCur->aOverflow, nOvfl*2*sizeof(Pgno) |
+ Pgno *aNew = (Pgno*)sqlite3Realloc( |
+ pCur->aOverflow, nOvfl*2*sizeof(Pgno) |
); |
if( aNew==0 ){ |
rc = SQLITE_NOMEM; |
@@ -4146,7 +4533,9 @@ static int accessPayload( |
/* If required, populate the overflow page-list cache. */ |
if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ |
- assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
+ assert( pCur->aOverflow[iIdx]==0 |
+ || pCur->aOverflow[iIdx]==nextPage |
+ || CORRUPT_DB ); |
pCur->aOverflow[iIdx] = nextPage; |
} |
@@ -4162,6 +4551,7 @@ static int accessPayload( |
*/ |
assert( eOp!=2 ); |
assert( pCur->curFlags & BTCF_ValidOvfl ); |
+ assert( pCur->pBtree->db==pBt->db ); |
if( pCur->aOverflow[iIdx+1] ){ |
nextPage = pCur->aOverflow[iIdx+1]; |
}else{ |
@@ -4215,7 +4605,7 @@ static int accessPayload( |
{ |
DbPage *pDbPage; |
- rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage, |
+ rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, |
((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) |
); |
if( rc==SQLITE_OK ){ |
@@ -4310,13 +4700,18 @@ static const void *fetchPayload( |
BtCursor *pCur, /* Cursor pointing to entry to read from */ |
u32 *pAmt /* Write the number of available bytes here */ |
){ |
+ u32 amt; |
assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
assert( pCur->eState==CURSOR_VALID ); |
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
assert( cursorHoldsMutex(pCur) ); |
assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
assert( pCur->info.nSize>0 ); |
- *pAmt = pCur->info.nLocal; |
+ assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB ); |
+ assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB); |
+ amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload); |
+ if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal; |
+ *pAmt = amt; |
return (void*)pCur->info.pPayload; |
} |
@@ -4353,9 +4748,6 @@ const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ |
** vice-versa). |
*/ |
static int moveToChild(BtCursor *pCur, u32 newPgno){ |
- int rc; |
- int i = pCur->iPage; |
- MemPage *pNewPage; |
BtShared *pBt = pCur->pBt; |
assert( cursorHoldsMutex(pCur) ); |
@@ -4365,22 +4757,15 @@ static int moveToChild(BtCursor *pCur, u32 newPgno){ |
if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
return SQLITE_CORRUPT_BKPT; |
} |
- rc = getAndInitPage(pBt, newPgno, &pNewPage, |
- (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
- if( rc ) return rc; |
- pCur->apPage[i+1] = pNewPage; |
- pCur->aiIdx[i+1] = 0; |
- pCur->iPage++; |
- |
pCur->info.nSize = 0; |
pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
- if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ |
- return SQLITE_CORRUPT_BKPT; |
- } |
- return SQLITE_OK; |
+ pCur->iPage++; |
+ pCur->aiIdx[pCur->iPage] = 0; |
+ return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage], |
+ pCur, pCur->curPagerFlags); |
} |
-#if 0 |
+#if SQLITE_DEBUG |
/* |
** Page pParent is an internal (non-leaf) tree page. This function |
** asserts that page number iChild is the left-child if the iIdx'th |
@@ -4389,6 +4774,8 @@ static int moveToChild(BtCursor *pCur, u32 newPgno){ |
** the page. |
*/ |
static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
+ if( CORRUPT_DB ) return; /* The conditions tested below might not be true |
+ ** in a corrupt database */ |
assert( iIdx<=pParent->nCell ); |
if( iIdx==pParent->nCell ){ |
assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
@@ -4413,25 +4800,15 @@ static void moveToParent(BtCursor *pCur){ |
assert( pCur->eState==CURSOR_VALID ); |
assert( pCur->iPage>0 ); |
assert( pCur->apPage[pCur->iPage] ); |
- |
- /* UPDATE: It is actually possible for the condition tested by the assert |
- ** below to be untrue if the database file is corrupt. This can occur if |
- ** one cursor has modified page pParent while a reference to it is held |
- ** by a second cursor. Which can only happen if a single page is linked |
- ** into more than one b-tree structure in a corrupt database. */ |
-#if 0 |
assertParentIndex( |
pCur->apPage[pCur->iPage-1], |
pCur->aiIdx[pCur->iPage-1], |
pCur->apPage[pCur->iPage]->pgno |
); |
-#endif |
testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); |
- |
- releasePage(pCur->apPage[pCur->iPage]); |
- pCur->iPage--; |
pCur->info.nSize = 0; |
pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
+ releasePageNotNull(pCur->apPage[pCur->iPage--]); |
} |
/* |
@@ -4472,18 +4849,23 @@ static int moveToRoot(BtCursor *pCur){ |
} |
if( pCur->iPage>=0 ){ |
- while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]); |
+ while( pCur->iPage ){ |
+ assert( pCur->apPage[pCur->iPage]!=0 ); |
+ releasePageNotNull(pCur->apPage[pCur->iPage--]); |
+ } |
}else if( pCur->pgnoRoot==0 ){ |
pCur->eState = CURSOR_INVALID; |
return SQLITE_OK; |
}else{ |
+ assert( pCur->iPage==(-1) ); |
rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], |
- (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
+ 0, pCur->curPagerFlags); |
if( rc!=SQLITE_OK ){ |
pCur->eState = CURSOR_INVALID; |
return rc; |
} |
pCur->iPage = 0; |
+ pCur->curIntKey = pCur->apPage[0]->intKey; |
} |
pRoot = pCur->apPage[0]; |
assert( pRoot->pgno==pCur->pgnoRoot ); |
@@ -4667,6 +5049,8 @@ int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
** *pRes>0 The cursor is left pointing at an entry that |
** is larger than intKey/pIdxKey. |
** |
+** For index tables, the pIdxKey->eqSeen field is set to 1 if there |
+** exists an entry in the table that exactly matches pIdxKey. |
*/ |
int sqlite3BtreeMovetoUnpacked( |
BtCursor *pCur, /* The cursor to be moved */ |
@@ -4686,7 +5070,7 @@ int sqlite3BtreeMovetoUnpacked( |
/* If the cursor is already positioned at the point we are trying |
** to move to, then just return without doing any work */ |
if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 |
- && pCur->apPage[0]->intKey |
+ && pCur->curIntKey |
){ |
if( pCur->info.nKey==intKey ){ |
*pRes = 0; |
@@ -4721,7 +5105,8 @@ int sqlite3BtreeMovetoUnpacked( |
assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
return SQLITE_OK; |
} |
- assert( pCur->apPage[0]->intKey || pIdxKey ); |
+ assert( pCur->apPage[0]->intKey==pCur->curIntKey ); |
+ assert( pCur->curIntKey || pIdxKey ); |
for(;;){ |
int lwr, upr, idx, c; |
Pgno chldPg; |
@@ -4744,7 +5129,7 @@ int sqlite3BtreeMovetoUnpacked( |
if( xRecordCompare==0 ){ |
for(;;){ |
i64 nCellKey; |
- pCell = findCell(pPage, idx) + pPage->childPtrSize; |
+ pCell = findCellPastPtr(pPage, idx); |
if( pPage->intKeyLeaf ){ |
while( 0x80 <= *(pCell++) ){ |
if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; |
@@ -4776,8 +5161,8 @@ int sqlite3BtreeMovetoUnpacked( |
} |
}else{ |
for(;;){ |
- int nCell; |
- pCell = findCell(pPage, idx) + pPage->childPtrSize; |
+ int nCell; /* Size of the pCell cell in bytes */ |
+ pCell = findCellPastPtr(pPage, idx); |
/* The maximum supported page-size is 65536 bytes. This means that |
** the maximum number of record bytes stored on an index B-Tree |
@@ -4805,12 +5190,25 @@ int sqlite3BtreeMovetoUnpacked( |
/* The record flows over onto one or more overflow pages. In |
** this case the whole cell needs to be parsed, a buffer allocated |
** and accessPayload() used to retrieve the record into the |
- ** buffer before VdbeRecordCompare() can be called. */ |
+ ** buffer before VdbeRecordCompare() can be called. |
+ ** |
+ ** If the record is corrupt, the xRecordCompare routine may read |
+ ** up to two varints past the end of the buffer. An extra 18 |
+ ** bytes of padding is allocated at the end of the buffer in |
+ ** case this happens. */ |
void *pCellKey; |
u8 * const pCellBody = pCell - pPage->childPtrSize; |
- btreeParseCellPtr(pPage, pCellBody, &pCur->info); |
+ pPage->xParseCell(pPage, pCellBody, &pCur->info); |
nCell = (int)pCur->info.nKey; |
- pCellKey = sqlite3Malloc( nCell ); |
+ testcase( nCell<0 ); /* True if key size is 2^32 or more */ |
+ testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ |
+ testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ |
+ testcase( nCell==2 ); /* Minimum legal index key size */ |
+ if( nCell<2 ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto moveto_finish; |
+ } |
+ pCellKey = sqlite3Malloc( nCell+18 ); |
if( pCellKey==0 ){ |
rc = SQLITE_NOMEM; |
goto moveto_finish; |
@@ -5103,8 +5501,7 @@ int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
** sqlite3PagerUnref() on the new page when it is done. |
** |
** SQLITE_OK is returned on success. Any other return value indicates |
-** an error. *ppPage and *pPgno are undefined in the event of an error. |
-** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
+** an error. *ppPage is set to NULL in the event of an error. |
** |
** If the "nearby" parameter is not 0, then an effort is made to |
** locate a page close to the page number "nearby". This can be used in an |
@@ -5136,6 +5533,8 @@ static int allocateBtreePage( |
assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
pPage1 = pBt->pPage1; |
mxPage = btreePagecount(pBt); |
+ /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 |
+ ** stores stores the total number of pages on the freelist. */ |
n = get4byte(&pPage1->aData[36]); |
testcase( n==mxPage-1 ); |
if( n>=mxPage ){ |
@@ -5145,6 +5544,7 @@ static int allocateBtreePage( |
/* There are pages on the freelist. Reuse one of those pages. */ |
Pgno iTrunk; |
u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
+ u32 nSearch = 0; /* Count of the number of search attempts */ |
/* If eMode==BTALLOC_EXACT and a query of the pointer-map |
** shows that the page 'nearby' is somewhere on the free-list, then |
@@ -5182,15 +5582,21 @@ static int allocateBtreePage( |
do { |
pPrevTrunk = pTrunk; |
if( pPrevTrunk ){ |
+ /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page |
+ ** is the page number of the next freelist trunk page in the list or |
+ ** zero if this is the last freelist trunk page. */ |
iTrunk = get4byte(&pPrevTrunk->aData[0]); |
}else{ |
+ /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 |
+ ** stores the page number of the first page of the freelist, or zero if |
+ ** the freelist is empty. */ |
iTrunk = get4byte(&pPage1->aData[32]); |
} |
testcase( iTrunk==mxPage ); |
- if( iTrunk>mxPage ){ |
+ if( iTrunk>mxPage || nSearch++ > n ){ |
rc = SQLITE_CORRUPT_BKPT; |
}else{ |
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
+ rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); |
} |
if( rc ){ |
pTrunk = 0; |
@@ -5198,8 +5604,9 @@ static int allocateBtreePage( |
} |
assert( pTrunk!=0 ); |
assert( pTrunk->aData!=0 ); |
- |
- k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ |
+ /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page |
+ ** is the number of leaf page pointers to follow. */ |
+ k = get4byte(&pTrunk->aData[4]); |
if( k==0 && !searchList ){ |
/* The trunk has no leaves and the list is not being searched. |
** So extract the trunk page itself and use it as the newly |
@@ -5254,7 +5661,7 @@ static int allocateBtreePage( |
goto end_allocate_page; |
} |
testcase( iNewTrunk==mxPage ); |
- rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
+ rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0); |
if( rc!=SQLITE_OK ){ |
goto end_allocate_page; |
} |
@@ -5334,11 +5741,12 @@ static int allocateBtreePage( |
} |
put4byte(&aData[4], k-1); |
noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; |
- rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
+ rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent); |
if( rc==SQLITE_OK ){ |
rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
if( rc!=SQLITE_OK ){ |
releasePage(*ppPage); |
+ *ppPage = 0; |
} |
} |
searchList = 0; |
@@ -5382,7 +5790,7 @@ static int allocateBtreePage( |
MemPage *pPg = 0; |
TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
- rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); |
+ rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); |
if( rc==SQLITE_OK ){ |
rc = sqlite3PagerWrite(pPg->pDbPage); |
releasePage(pPg); |
@@ -5396,11 +5804,12 @@ static int allocateBtreePage( |
*pPgno = pBt->nPage; |
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
- rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); |
+ rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent); |
if( rc ) return rc; |
rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
if( rc!=SQLITE_OK ){ |
releasePage(*ppPage); |
+ *ppPage = 0; |
} |
TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
} |
@@ -5410,17 +5819,8 @@ static int allocateBtreePage( |
end_allocate_page: |
releasePage(pTrunk); |
releasePage(pPrevTrunk); |
- if( rc==SQLITE_OK ){ |
- if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
- releasePage(*ppPage); |
- *ppPage = 0; |
- return SQLITE_CORRUPT_BKPT; |
- } |
- (*ppPage)->isInit = 0; |
- }else{ |
- *ppPage = 0; |
- } |
- assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) ); |
+ assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 ); |
+ assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 ); |
return rc; |
} |
@@ -5445,9 +5845,10 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
int nFree; /* Initial number of pages on free-list */ |
assert( sqlite3_mutex_held(pBt->mutex) ); |
- assert( iPage>1 ); |
+ assert( CORRUPT_DB || iPage>1 ); |
assert( !pMemPage || pMemPage->pgno==iPage ); |
+ if( iPage<2 ) return SQLITE_CORRUPT_BKPT; |
if( pMemPage ){ |
pPage = pMemPage; |
sqlite3PagerRef(pPage->pDbPage); |
@@ -5517,6 +5918,11 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
** for now. At some point in the future (once everyone has upgraded |
** to 3.6.0 or later) we should consider fixing the conditional above |
** to read "usableSize/4-2" instead of "usableSize/4-8". |
+ ** |
+ ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still |
+ ** avoid using the last six entries in the freelist trunk page array in |
+ ** order that database files created by newer versions of SQLite can be |
+ ** read by older versions of SQLite. |
*/ |
rc = sqlite3PagerWrite(pTrunk->pDbPage); |
if( rc==SQLITE_OK ){ |
@@ -5582,19 +5988,21 @@ static int clearCell( |
u32 ovflPageSize; |
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
- btreeParseCellPtr(pPage, pCell, &info); |
+ pPage->xParseCell(pPage, pCell, &info); |
*pnSize = info.nSize; |
- if( info.iOverflow==0 ){ |
+ if( info.nLocal==info.nPayload ){ |
return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
} |
- if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
+ if( pCell+info.nSize-1 > pPage->aData+pPage->maskPage ){ |
return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ |
} |
- ovflPgno = get4byte(&pCell[info.iOverflow]); |
+ ovflPgno = get4byte(pCell + info.nSize - 4); |
assert( pBt->usableSize > 4 ); |
ovflPageSize = pBt->usableSize - 4; |
nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
- assert( ovflPgno==0 || nOvfl>0 ); |
+ assert( nOvfl>0 || |
+ (CORRUPT_DB && (info.nPayload + ovflPageSize)<ovflPageSize) |
+ ); |
while( nOvfl-- ){ |
Pgno iNext = 0; |
MemPage *pOvfl = 0; |
@@ -5692,9 +6100,7 @@ static int fillInCell( |
nSrc = nData; |
nData = 0; |
}else{ |
- if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
- return SQLITE_CORRUPT_BKPT; |
- } |
+ assert( nKey<=0x7fffffff && pKey!=0 ); |
nPayload = (int)nKey; |
pSrc = pKey; |
nSrc = (int)nKey; |
@@ -5734,12 +6140,11 @@ static int fillInCell( |
#if SQLITE_DEBUG |
{ |
CellInfo info; |
- btreeParseCellPtr(pPage, pCell, &info); |
+ pPage->xParseCell(pPage, pCell, &info); |
assert( nHeader=(int)(info.pPayload - pCell) ); |
assert( info.nKey==nKey ); |
assert( *pnSize == info.nSize ); |
assert( spaceLeft == info.nLocal ); |
- assert( pPrior == &pCell[info.iOverflow] ); |
} |
#endif |
@@ -5849,7 +6254,7 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
if( *pRC ) return; |
assert( idx>=0 && idx<pPage->nCell ); |
- assert( sz==cellSize(pPage, idx) ); |
+ assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); |
assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
data = pPage->aData; |
@@ -5868,9 +6273,17 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
return; |
} |
pPage->nCell--; |
- memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
- put2byte(&data[hdr+3], pPage->nCell); |
- pPage->nFree += 2; |
+ if( pPage->nCell==0 ){ |
+ memset(&data[hdr+1], 0, 4); |
+ data[hdr+7] = 0; |
+ put2byte(&data[hdr+5], pPage->pBt->usableSize); |
+ pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset |
+ - pPage->childPtrSize - 8; |
+ }else{ |
+ memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
+ put2byte(&data[hdr+3], pPage->nCell); |
+ pPage->nFree += 2; |
+ } |
} |
/* |
@@ -5896,10 +6309,8 @@ static void insertCell( |
){ |
int idx = 0; /* Where to write new cell content in data[] */ |
int j; /* Loop counter */ |
- int end; /* First byte past the last cell pointer in data[] */ |
- int ins; /* Index in data[] where new cell pointer is inserted */ |
- int cellOffset; /* Address of first cell pointer in data[] */ |
u8 *data; /* The content of the whole page */ |
+ u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ |
if( *pRC ) return; |
@@ -5914,7 +6325,7 @@ static void insertCell( |
** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
** the term after the || in the following assert(). */ |
- assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
+ assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) ); |
if( pPage->nOverflow || sz+2>pPage->nFree ){ |
if( pTemp ){ |
memcpy(pTemp, pCell, sz); |
@@ -5927,6 +6338,14 @@ static void insertCell( |
assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); |
pPage->apOvfl[j] = pCell; |
pPage->aiOvfl[j] = (u16)i; |
+ |
+ /* When multiple overflows occur, they are always sequential and in |
+ ** sorted order. This invariants arise because multiple overflows can |
+ ** only occur when inserting divider cells into the parent page during |
+ ** balancing, and the dividers are adjacent and sorted. |
+ */ |
+ assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ |
+ assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ |
}else{ |
int rc = sqlite3PagerWrite(pPage->pDbPage); |
if( rc!=SQLITE_OK ){ |
@@ -5935,24 +6354,26 @@ static void insertCell( |
} |
assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
data = pPage->aData; |
- cellOffset = pPage->cellOffset; |
- end = cellOffset + 2*pPage->nCell; |
- ins = cellOffset + 2*i; |
+ assert( &data[pPage->cellOffset]==pPage->aCellIdx ); |
rc = allocateSpace(pPage, sz, &idx); |
if( rc ){ *pRC = rc; return; } |
- /* The allocateSpace() routine guarantees the following two properties |
- ** if it returns success */ |
- assert( idx >= end+2 ); |
+ /* The allocateSpace() routine guarantees the following properties |
+ ** if it returns successfully */ |
+ assert( idx >= 0 ); |
+ assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); |
assert( idx+sz <= (int)pPage->pBt->usableSize ); |
- pPage->nCell++; |
pPage->nFree -= (u16)(2 + sz); |
memcpy(&data[idx], pCell, sz); |
if( iChild ){ |
put4byte(&data[idx], iChild); |
} |
- memmove(&data[ins+2], &data[ins], end-ins); |
- put2byte(&data[ins], idx); |
- put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
+ pIns = pPage->aCellIdx + i*2; |
+ memmove(pIns+2, pIns, 2*(pPage->nCell - i)); |
+ put2byte(pIns, idx); |
+ pPage->nCell++; |
+ /* increment the cell count */ |
+ if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; |
+ assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell ); |
#ifndef SQLITE_OMIT_AUTOVACUUM |
if( pPage->pBt->autoVacuum ){ |
/* The cell may contain a pointer to an overflow page. If so, write |
@@ -5965,45 +6386,328 @@ static void insertCell( |
} |
/* |
-** Add a list of cells to a page. The page should be initially empty. |
-** The cells are guaranteed to fit on the page. |
+** A CellArray object contains a cache of pointers and sizes for a |
+** consecutive sequence of cells that might be held multiple pages. |
*/ |
-static void assemblePage( |
- MemPage *pPage, /* The page to be assembled */ |
- int nCell, /* The number of cells to add to this page */ |
- u8 **apCell, /* Pointers to cell bodies */ |
- u16 *aSize /* Sizes of the cells */ |
+typedef struct CellArray CellArray; |
+struct CellArray { |
+ int nCell; /* Number of cells in apCell[] */ |
+ MemPage *pRef; /* Reference page */ |
+ u8 **apCell; /* All cells begin balanced */ |
+ u16 *szCell; /* Local size of all cells in apCell[] */ |
+}; |
+ |
+/* |
+** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been |
+** computed. |
+*/ |
+static void populateCellCache(CellArray *p, int idx, int N){ |
+ assert( idx>=0 && idx+N<=p->nCell ); |
+ while( N>0 ){ |
+ assert( p->apCell[idx]!=0 ); |
+ if( p->szCell[idx]==0 ){ |
+ p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); |
+ }else{ |
+ assert( CORRUPT_DB || |
+ p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); |
+ } |
+ idx++; |
+ N--; |
+ } |
+} |
+ |
+/* |
+** Return the size of the Nth element of the cell array |
+*/ |
+static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ |
+ assert( N>=0 && N<p->nCell ); |
+ assert( p->szCell[N]==0 ); |
+ p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); |
+ return p->szCell[N]; |
+} |
+static u16 cachedCellSize(CellArray *p, int N){ |
+ assert( N>=0 && N<p->nCell ); |
+ if( p->szCell[N] ) return p->szCell[N]; |
+ return computeCellSize(p, N); |
+} |
+ |
+/* |
+** Array apCell[] contains pointers to nCell b-tree page cells. The |
+** szCell[] array contains the size in bytes of each cell. This function |
+** replaces the current contents of page pPg with the contents of the cell |
+** array. |
+** |
+** Some of the cells in apCell[] may currently be stored in pPg. This |
+** function works around problems caused by this by making a copy of any |
+** such cells before overwriting the page data. |
+** |
+** The MemPage.nFree field is invalidated by this function. It is the |
+** responsibility of the caller to set it correctly. |
+*/ |
+static int rebuildPage( |
+ MemPage *pPg, /* Edit this page */ |
+ int nCell, /* Final number of cells on page */ |
+ u8 **apCell, /* Array of cells */ |
+ u16 *szCell /* Array of cell sizes */ |
){ |
- int i; /* Loop counter */ |
- u8 *pCellptr; /* Address of next cell pointer */ |
- int cellbody; /* Address of next cell body */ |
- u8 * const data = pPage->aData; /* Pointer to data for pPage */ |
- const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
- const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
+ const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
+ u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
+ const int usableSize = pPg->pBt->usableSize; |
+ u8 * const pEnd = &aData[usableSize]; |
+ int i; |
+ u8 *pCellptr = pPg->aCellIdx; |
+ u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
+ u8 *pData; |
- assert( pPage->nOverflow==0 ); |
- assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
- assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt) |
- && (int)MX_CELL(pPage->pBt)<=10921); |
- assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
+ i = get2byte(&aData[hdr+5]); |
+ memcpy(&pTmp[i], &aData[i], usableSize - i); |
+ |
+ pData = pEnd; |
+ for(i=0; i<nCell; i++){ |
+ u8 *pCell = apCell[i]; |
+ if( SQLITE_WITHIN(pCell,aData,pEnd) ){ |
+ pCell = &pTmp[pCell - aData]; |
+ } |
+ pData -= szCell[i]; |
+ put2byte(pCellptr, (pData - aData)); |
+ pCellptr += 2; |
+ if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; |
+ memcpy(pData, pCell, szCell[i]); |
+ assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); |
+ testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); |
+ } |
+ |
+ /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
+ pPg->nCell = nCell; |
+ pPg->nOverflow = 0; |
+ |
+ put2byte(&aData[hdr+1], 0); |
+ put2byte(&aData[hdr+3], pPg->nCell); |
+ put2byte(&aData[hdr+5], pData - aData); |
+ aData[hdr+7] = 0x00; |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
+** contains the size in bytes of each such cell. This function attempts to |
+** add the cells stored in the array to page pPg. If it cannot (because |
+** the page needs to be defragmented before the cells will fit), non-zero |
+** is returned. Otherwise, if the cells are added successfully, zero is |
+** returned. |
+** |
+** Argument pCellptr points to the first entry in the cell-pointer array |
+** (part of page pPg) to populate. After cell apCell[0] is written to the |
+** page body, a 16-bit offset is written to pCellptr. And so on, for each |
+** cell in the array. It is the responsibility of the caller to ensure |
+** that it is safe to overwrite this part of the cell-pointer array. |
+** |
+** When this function is called, *ppData points to the start of the |
+** content area on page pPg. If the size of the content area is extended, |
+** *ppData is updated to point to the new start of the content area |
+** before returning. |
+** |
+** Finally, argument pBegin points to the byte immediately following the |
+** end of the space required by this page for the cell-pointer area (for |
+** all cells - not just those inserted by the current call). If the content |
+** area must be extended to before this point in order to accomodate all |
+** cells in apCell[], then the cells do not fit and non-zero is returned. |
+*/ |
+static int pageInsertArray( |
+ MemPage *pPg, /* Page to add cells to */ |
+ u8 *pBegin, /* End of cell-pointer array */ |
+ u8 **ppData, /* IN/OUT: Page content -area pointer */ |
+ u8 *pCellptr, /* Pointer to cell-pointer area */ |
+ int iFirst, /* Index of first cell to add */ |
+ int nCell, /* Number of cells to add to pPg */ |
+ CellArray *pCArray /* Array of cells */ |
+){ |
+ int i; |
+ u8 *aData = pPg->aData; |
+ u8 *pData = *ppData; |
+ int iEnd = iFirst + nCell; |
+ assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
+ for(i=iFirst; i<iEnd; i++){ |
+ int sz, rc; |
+ u8 *pSlot; |
+ sz = cachedCellSize(pCArray, i); |
+ if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ |
+ pData -= sz; |
+ if( pData<pBegin ) return 1; |
+ pSlot = pData; |
+ } |
+ /* pSlot and pCArray->apCell[i] will never overlap on a well-formed |
+ ** database. But they might for a corrupt database. Hence use memmove() |
+ ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ |
+ assert( (pSlot+sz)<=pCArray->apCell[i] |
+ || pSlot>=(pCArray->apCell[i]+sz) |
+ || CORRUPT_DB ); |
+ memmove(pSlot, pCArray->apCell[i], sz); |
+ put2byte(pCellptr, (pSlot - aData)); |
+ pCellptr += 2; |
+ } |
+ *ppData = pData; |
+ return 0; |
+} |
- /* Check that the page has just been zeroed by zeroPage() */ |
- assert( pPage->nCell==0 ); |
- assert( get2byteNotZero(&data[hdr+5])==nUsable ); |
+/* |
+** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
+** contains the size in bytes of each such cell. This function adds the |
+** space associated with each cell in the array that is currently stored |
+** within the body of pPg to the pPg free-list. The cell-pointers and other |
+** fields of the page are not updated. |
+** |
+** This function returns the total number of cells added to the free-list. |
+*/ |
+static int pageFreeArray( |
+ MemPage *pPg, /* Page to edit */ |
+ int iFirst, /* First cell to delete */ |
+ int nCell, /* Cells to delete */ |
+ CellArray *pCArray /* Array of cells */ |
+){ |
+ u8 * const aData = pPg->aData; |
+ u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
+ u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
+ int nRet = 0; |
+ int i; |
+ int iEnd = iFirst + nCell; |
+ u8 *pFree = 0; |
+ int szFree = 0; |
- pCellptr = &pPage->aCellIdx[nCell*2]; |
- cellbody = nUsable; |
- for(i=nCell-1; i>=0; i--){ |
- u16 sz = aSize[i]; |
- pCellptr -= 2; |
- cellbody -= sz; |
- put2byte(pCellptr, cellbody); |
- memcpy(&data[cellbody], apCell[i], sz); |
+ for(i=iFirst; i<iEnd; i++){ |
+ u8 *pCell = pCArray->apCell[i]; |
+ if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ |
+ int sz; |
+ /* No need to use cachedCellSize() here. The sizes of all cells that |
+ ** are to be freed have already been computing while deciding which |
+ ** cells need freeing */ |
+ sz = pCArray->szCell[i]; assert( sz>0 ); |
+ if( pFree!=(pCell + sz) ){ |
+ if( pFree ){ |
+ assert( pFree>aData && (pFree - aData)<65536 ); |
+ freeSpace(pPg, (u16)(pFree - aData), szFree); |
+ } |
+ pFree = pCell; |
+ szFree = sz; |
+ if( pFree+sz>pEnd ) return 0; |
+ }else{ |
+ pFree = pCell; |
+ szFree += sz; |
+ } |
+ nRet++; |
+ } |
+ } |
+ if( pFree ){ |
+ assert( pFree>aData && (pFree - aData)<65536 ); |
+ freeSpace(pPg, (u16)(pFree - aData), szFree); |
} |
- put2byte(&data[hdr+3], nCell); |
- put2byte(&data[hdr+5], cellbody); |
- pPage->nFree -= (nCell*2 + nUsable - cellbody); |
- pPage->nCell = (u16)nCell; |
+ return nRet; |
+} |
+ |
+/* |
+** apCell[] and szCell[] contains pointers to and sizes of all cells in the |
+** pages being balanced. The current page, pPg, has pPg->nCell cells starting |
+** with apCell[iOld]. After balancing, this page should hold nNew cells |
+** starting at apCell[iNew]. |
+** |
+** This routine makes the necessary adjustments to pPg so that it contains |
+** the correct cells after being balanced. |
+** |
+** The pPg->nFree field is invalid when this function returns. It is the |
+** responsibility of the caller to set it correctly. |
+*/ |
+static int editPage( |
+ MemPage *pPg, /* Edit this page */ |
+ int iOld, /* Index of first cell currently on page */ |
+ int iNew, /* Index of new first cell on page */ |
+ int nNew, /* Final number of cells on page */ |
+ CellArray *pCArray /* Array of cells and sizes */ |
+){ |
+ u8 * const aData = pPg->aData; |
+ const int hdr = pPg->hdrOffset; |
+ u8 *pBegin = &pPg->aCellIdx[nNew * 2]; |
+ int nCell = pPg->nCell; /* Cells stored on pPg */ |
+ u8 *pData; |
+ u8 *pCellptr; |
+ int i; |
+ int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; |
+ int iNewEnd = iNew + nNew; |
+ |
+#ifdef SQLITE_DEBUG |
+ u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
+ memcpy(pTmp, aData, pPg->pBt->usableSize); |
+#endif |
+ |
+ /* Remove cells from the start and end of the page */ |
+ if( iOld<iNew ){ |
+ int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); |
+ memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
+ nCell -= nShift; |
+ } |
+ if( iNewEnd < iOldEnd ){ |
+ nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
+ } |
+ |
+ pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
+ if( pData<pBegin ) goto editpage_fail; |
+ |
+ /* Add cells to the start of the page */ |
+ if( iNew<iOld ){ |
+ int nAdd = MIN(nNew,iOld-iNew); |
+ assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
+ pCellptr = pPg->aCellIdx; |
+ memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
+ if( pageInsertArray( |
+ pPg, pBegin, &pData, pCellptr, |
+ iNew, nAdd, pCArray |
+ ) ) goto editpage_fail; |
+ nCell += nAdd; |
+ } |
+ |
+ /* Add any overflow cells */ |
+ for(i=0; i<pPg->nOverflow; i++){ |
+ int iCell = (iOld + pPg->aiOvfl[i]) - iNew; |
+ if( iCell>=0 && iCell<nNew ){ |
+ pCellptr = &pPg->aCellIdx[iCell * 2]; |
+ memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
+ nCell++; |
+ if( pageInsertArray( |
+ pPg, pBegin, &pData, pCellptr, |
+ iCell+iNew, 1, pCArray |
+ ) ) goto editpage_fail; |
+ } |
+ } |
+ |
+ /* Append cells to the end of the page */ |
+ pCellptr = &pPg->aCellIdx[nCell*2]; |
+ if( pageInsertArray( |
+ pPg, pBegin, &pData, pCellptr, |
+ iNew+nCell, nNew-nCell, pCArray |
+ ) ) goto editpage_fail; |
+ |
+ pPg->nCell = nNew; |
+ pPg->nOverflow = 0; |
+ |
+ put2byte(&aData[hdr+3], pPg->nCell); |
+ put2byte(&aData[hdr+5], pData - aData); |
+ |
+#ifdef SQLITE_DEBUG |
+ for(i=0; i<nNew && !CORRUPT_DB; i++){ |
+ u8 *pCell = pCArray->apCell[i+iNew]; |
+ int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); |
+ if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ |
+ pCell = &pTmp[pCell - aData]; |
+ } |
+ assert( 0==memcmp(pCell, &aData[iOff], |
+ pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); |
+ } |
+#endif |
+ |
+ return SQLITE_OK; |
+ editpage_fail: |
+ /* Unable to edit this page. Rebuild it from scratch instead. */ |
+ populateCellCache(pCArray, iNew, nNew); |
+ return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); |
} |
/* |
@@ -6057,7 +6761,7 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ |
assert( pPage->nOverflow==1 ); |
/* This error condition is now caught prior to reaching this function */ |
- if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; |
+ if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; |
/* Allocate a new page. This page will become the right-sibling of |
** pPage. Make the parent page writable, so that the new divider cell |
@@ -6069,13 +6773,15 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ |
u8 *pOut = &pSpace[4]; |
u8 *pCell = pPage->apOvfl[0]; |
- u16 szCell = cellSizePtr(pPage, pCell); |
+ u16 szCell = pPage->xCellSize(pPage, pCell); |
u8 *pStop; |
assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
- assemblePage(pNew, 1, &pCell, &szCell); |
+ rc = rebuildPage(pNew, 1, &pCell, &szCell); |
+ if( NEVER(rc) ) return rc; |
+ pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
/* If this is an auto-vacuum database, update the pointer map |
** with entries for the new page, and any pointer from the |
@@ -6147,9 +6853,9 @@ static int ptrmapCheckPages(MemPage **apPage, int nPage){ |
u8 *z; |
z = findCell(pPage, j); |
- btreeParseCellPtr(pPage, z, &info); |
- if( info.iOverflow ){ |
- Pgno ovfl = get4byte(&z[info.iOverflow]); |
+ pPage->xParseCell(pPage, z, &info); |
+ if( info.nLocal<info.nPayload ){ |
+ Pgno ovfl = get4byte(&z[info.nSize-4]); |
ptrmapGet(pBt, ovfl, &e, &n); |
assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); |
} |
@@ -6267,9 +6973,6 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ |
** If aOvflSpace is set to a null pointer, this function returns |
** SQLITE_NOMEM. |
*/ |
-#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) |
-#pragma optimize("", off) |
-#endif |
static int balance_nonroot( |
MemPage *pParent, /* Parent page of siblings being balanced */ |
int iParentIdx, /* Index of "the page" in pParent */ |
@@ -6278,7 +6981,6 @@ static int balance_nonroot( |
int bBulk /* True if this call is part of a bulk load */ |
){ |
BtShared *pBt; /* The whole database */ |
- int nCell = 0; /* Number of cells in apCell[] */ |
int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
int nNew = 0; /* Number of pages in apNew[] */ |
int nOld; /* Number of pages in apOld[] */ |
@@ -6289,22 +6991,27 @@ static int balance_nonroot( |
int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
int usableSpace; /* Bytes in pPage beyond the header */ |
int pageFlags; /* Value of pPage->aData[0] */ |
- int subtotal; /* Subtotal of bytes in cells on one page */ |
int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
int szScratch; /* Size of scratch memory requested */ |
MemPage *apOld[NB]; /* pPage and up to two siblings */ |
- MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
u8 *pRight; /* Location in parent of right-sibling pointer */ |
u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
- int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
- int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
- u8 **apCell = 0; /* All cells begin balanced */ |
- u16 *szCell; /* Local size of all cells in apCell[] */ |
+ int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ |
+ int cntOld[NB+2]; /* Old index in b.apCell[] */ |
+ int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
u8 *aSpace1; /* Space for copies of dividers cells */ |
Pgno pgno; /* Temp var to store a page number in */ |
- |
+ u8 abDone[NB+2]; /* True after i'th new page is populated */ |
+ Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
+ Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
+ u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
+ CellArray b; /* Parsed information on cells being balanced */ |
+ |
+ memset(abDone, 0, sizeof(abDone)); |
+ b.nCell = 0; |
+ b.apCell = 0; |
pBt = pParent->pBt; |
assert( sqlite3_mutex_held(pBt->mutex) ); |
assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
@@ -6346,7 +7053,6 @@ static int balance_nonroot( |
}else if( iParentIdx==i ){ |
nxDiv = i-2+bBulk; |
}else{ |
- assert( bBulk==0 ); |
nxDiv = iParentIdx-1; |
} |
i = 2-bBulk; |
@@ -6359,7 +7065,7 @@ static int balance_nonroot( |
} |
pgno = get4byte(pRight); |
while( 1 ){ |
- rc = getAndInitPage(pBt, pgno, &apOld[i], 0); |
+ rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); |
if( rc ){ |
memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
goto balance_cleanup; |
@@ -6370,12 +7076,12 @@ static int balance_nonroot( |
if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ |
apDiv[i] = pParent->apOvfl[0]; |
pgno = get4byte(apDiv[i]); |
- szNew[i] = cellSizePtr(pParent, apDiv[i]); |
+ szNew[i] = pParent->xCellSize(pParent, apDiv[i]); |
pParent->nOverflow = 0; |
}else{ |
apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); |
pgno = get4byte(apDiv[i]); |
- szNew[i] = cellSizePtr(pParent, apDiv[i]); |
+ szNew[i] = pParent->xCellSize(pParent, apDiv[i]); |
/* Drop the cell from the parent page. apDiv[i] still points to |
** the cell within the parent, even though it has been dropped. |
@@ -6413,138 +7119,209 @@ static int balance_nonroot( |
/* |
** Allocate space for memory structures |
*/ |
- k = pBt->pageSize + ROUND8(sizeof(MemPage)); |
szScratch = |
- nMaxCells*sizeof(u8*) /* apCell */ |
- + nMaxCells*sizeof(u16) /* szCell */ |
- + pBt->pageSize /* aSpace1 */ |
- + k*nOld; /* Page copies (apCopy) */ |
- apCell = sqlite3ScratchMalloc( szScratch ); |
- if( apCell==0 ){ |
+ nMaxCells*sizeof(u8*) /* b.apCell */ |
+ + nMaxCells*sizeof(u16) /* b.szCell */ |
+ + pBt->pageSize; /* aSpace1 */ |
+ |
+ /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
+ ** that is more than 6 times the database page size. */ |
+ assert( szScratch<=6*(int)pBt->pageSize ); |
+ b.apCell = sqlite3ScratchMalloc( szScratch ); |
+ if( b.apCell==0 ){ |
rc = SQLITE_NOMEM; |
goto balance_cleanup; |
} |
- szCell = (u16*)&apCell[nMaxCells]; |
- aSpace1 = (u8*)&szCell[nMaxCells]; |
+ b.szCell = (u16*)&b.apCell[nMaxCells]; |
+ aSpace1 = (u8*)&b.szCell[nMaxCells]; |
assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
/* |
** Load pointers to all cells on sibling pages and the divider cells |
- ** into the local apCell[] array. Make copies of the divider cells |
- ** into space obtained from aSpace1[] and remove the divider cells |
- ** from pParent. |
+ ** into the local b.apCell[] array. Make copies of the divider cells |
+ ** into space obtained from aSpace1[]. The divider cells have already |
+ ** been removed from pParent. |
** |
** If the siblings are on leaf pages, then the child pointers of the |
** divider cells are stripped from the cells before they are copied |
- ** into aSpace1[]. In this way, all cells in apCell[] are without |
+ ** into aSpace1[]. In this way, all cells in b.apCell[] are without |
** child pointers. If siblings are not leaves, then all cell in |
- ** apCell[] include child pointers. Either way, all cells in apCell[] |
+ ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] |
** are alike. |
** |
** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
** leafData: 1 if pPage holds key+data and pParent holds only keys. |
*/ |
- leafCorrection = apOld[0]->leaf*4; |
- leafData = apOld[0]->intKeyLeaf; |
+ b.pRef = apOld[0]; |
+ leafCorrection = b.pRef->leaf*4; |
+ leafData = b.pRef->intKeyLeaf; |
for(i=0; i<nOld; i++){ |
- int limit; |
- |
- /* Before doing anything else, take a copy of the i'th original sibling |
- ** The rest of this function will use data from the copies rather |
- ** that the original pages since the original pages will be in the |
- ** process of being overwritten. */ |
- MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i]; |
- memcpy(pOld, apOld[i], sizeof(MemPage)); |
- pOld->aData = (void*)&pOld[1]; |
- memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); |
- |
- limit = pOld->nCell+pOld->nOverflow; |
+ MemPage *pOld = apOld[i]; |
+ int limit = pOld->nCell; |
+ u8 *aData = pOld->aData; |
+ u16 maskPage = pOld->maskPage; |
+ u8 *piCell = aData + pOld->cellOffset; |
+ u8 *piEnd; |
+ |
+ /* Verify that all sibling pages are of the same "type" (table-leaf, |
+ ** table-interior, index-leaf, or index-interior). |
+ */ |
+ if( pOld->aData[0]!=apOld[0]->aData[0] ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto balance_cleanup; |
+ } |
+ |
+ /* Load b.apCell[] with pointers to all cells in pOld. If pOld |
+ ** constains overflow cells, include them in the b.apCell[] array |
+ ** in the correct spot. |
+ ** |
+ ** Note that when there are multiple overflow cells, it is always the |
+ ** case that they are sequential and adjacent. This invariant arises |
+ ** because multiple overflows can only occurs when inserting divider |
+ ** cells into a parent on a prior balance, and divider cells are always |
+ ** adjacent and are inserted in order. There is an assert() tagged |
+ ** with "NOTE 1" in the overflow cell insertion loop to prove this |
+ ** invariant. |
+ ** |
+ ** This must be done in advance. Once the balance starts, the cell |
+ ** offset section of the btree page will be overwritten and we will no |
+ ** long be able to find the cells if a pointer to each cell is not saved |
+ ** first. |
+ */ |
+ memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit); |
if( pOld->nOverflow>0 ){ |
+ memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow); |
+ limit = pOld->aiOvfl[0]; |
for(j=0; j<limit; j++){ |
- assert( nCell<nMaxCells ); |
- apCell[nCell] = findOverflowCell(pOld, j); |
- szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
- nCell++; |
+ b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); |
+ piCell += 2; |
+ b.nCell++; |
} |
- }else{ |
- u8 *aData = pOld->aData; |
- u16 maskPage = pOld->maskPage; |
- u16 cellOffset = pOld->cellOffset; |
- for(j=0; j<limit; j++){ |
- assert( nCell<nMaxCells ); |
- apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
- szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
- nCell++; |
+ for(k=0; k<pOld->nOverflow; k++){ |
+ assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ |
+ b.apCell[b.nCell] = pOld->apOvfl[k]; |
+ b.nCell++; |
} |
- } |
+ } |
+ piEnd = aData + pOld->cellOffset + 2*pOld->nCell; |
+ while( piCell<piEnd ){ |
+ assert( b.nCell<nMaxCells ); |
+ b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); |
+ piCell += 2; |
+ b.nCell++; |
+ } |
+ |
+ cntOld[i] = b.nCell; |
if( i<nOld-1 && !leafData){ |
u16 sz = (u16)szNew[i]; |
u8 *pTemp; |
- assert( nCell<nMaxCells ); |
- szCell[nCell] = sz; |
+ assert( b.nCell<nMaxCells ); |
+ b.szCell[b.nCell] = sz; |
pTemp = &aSpace1[iSpace1]; |
iSpace1 += sz; |
assert( sz<=pBt->maxLocal+23 ); |
assert( iSpace1 <= (int)pBt->pageSize ); |
memcpy(pTemp, apDiv[i], sz); |
- apCell[nCell] = pTemp+leafCorrection; |
+ b.apCell[b.nCell] = pTemp+leafCorrection; |
assert( leafCorrection==0 || leafCorrection==4 ); |
- szCell[nCell] = szCell[nCell] - leafCorrection; |
+ b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection; |
if( !pOld->leaf ){ |
assert( leafCorrection==0 ); |
assert( pOld->hdrOffset==0 ); |
/* The right pointer of the child page pOld becomes the left |
** pointer of the divider cell */ |
- memcpy(apCell[nCell], &pOld->aData[8], 4); |
+ memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); |
}else{ |
assert( leafCorrection==4 ); |
- if( szCell[nCell]<4 ){ |
- /* Do not allow any cells smaller than 4 bytes. */ |
- szCell[nCell] = 4; |
+ while( b.szCell[b.nCell]<4 ){ |
+ /* Do not allow any cells smaller than 4 bytes. If a smaller cell |
+ ** does exist, pad it with 0x00 bytes. */ |
+ assert( b.szCell[b.nCell]==3 || CORRUPT_DB ); |
+ assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); |
+ aSpace1[iSpace1++] = 0x00; |
+ b.szCell[b.nCell]++; |
} |
} |
- nCell++; |
+ b.nCell++; |
} |
} |
/* |
- ** Figure out the number of pages needed to hold all nCell cells. |
+ ** Figure out the number of pages needed to hold all b.nCell cells. |
** Store this number in "k". Also compute szNew[] which is the total |
** size of all cells on the i-th page and cntNew[] which is the index |
- ** in apCell[] of the cell that divides page i from page i+1. |
- ** cntNew[k] should equal nCell. |
+ ** in b.apCell[] of the cell that divides page i from page i+1. |
+ ** cntNew[k] should equal b.nCell. |
** |
** Values computed by this block: |
** |
** k: The total number of sibling pages |
** szNew[i]: Spaced used on the i-th sibling page. |
- ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
+ ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to |
** the right of the i-th sibling page. |
** usableSpace: Number of bytes of space available on each sibling. |
** |
*/ |
usableSpace = pBt->usableSize - 12 + leafCorrection; |
- for(subtotal=k=i=0; i<nCell; i++){ |
- assert( i<nMaxCells ); |
- subtotal += szCell[i] + 2; |
- if( subtotal > usableSpace ){ |
- szNew[k] = subtotal - szCell[i]; |
- cntNew[k] = i; |
- if( leafData ){ i--; } |
- subtotal = 0; |
- k++; |
- if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
- } |
- } |
- szNew[k] = subtotal; |
- cntNew[k] = nCell; |
- k++; |
+ for(i=0; i<nOld; i++){ |
+ MemPage *p = apOld[i]; |
+ szNew[i] = usableSpace - p->nFree; |
+ if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
+ for(j=0; j<p->nOverflow; j++){ |
+ szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); |
+ } |
+ cntNew[i] = cntOld[i]; |
+ } |
+ k = nOld; |
+ for(i=0; i<k; i++){ |
+ int sz; |
+ while( szNew[i]>usableSpace ){ |
+ if( i+1>=k ){ |
+ k = i+2; |
+ if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
+ szNew[k-1] = 0; |
+ cntNew[k-1] = b.nCell; |
+ } |
+ sz = 2 + cachedCellSize(&b, cntNew[i]-1); |
+ szNew[i] -= sz; |
+ if( !leafData ){ |
+ if( cntNew[i]<b.nCell ){ |
+ sz = 2 + cachedCellSize(&b, cntNew[i]); |
+ }else{ |
+ sz = 0; |
+ } |
+ } |
+ szNew[i+1] += sz; |
+ cntNew[i]--; |
+ } |
+ while( cntNew[i]<b.nCell ){ |
+ sz = 2 + cachedCellSize(&b, cntNew[i]); |
+ if( szNew[i]+sz>usableSpace ) break; |
+ szNew[i] += sz; |
+ cntNew[i]++; |
+ if( !leafData ){ |
+ if( cntNew[i]<b.nCell ){ |
+ sz = 2 + cachedCellSize(&b, cntNew[i]); |
+ }else{ |
+ sz = 0; |
+ } |
+ } |
+ szNew[i+1] -= sz; |
+ } |
+ if( cntNew[i]>=b.nCell ){ |
+ k = i+1; |
+ }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto balance_cleanup; |
+ } |
+ } |
/* |
** The packing computed by the previous block is biased toward the siblings |
- ** on the left side. The left siblings are always nearly full, while the |
- ** right-most sibling might be nearly empty. This block of code attempts |
- ** to adjust the packing of siblings to get a better balance. |
+ ** on the left side (siblings with smaller keys). The left siblings are |
+ ** always nearly full, while the right-most sibling might be nearly empty. |
+ ** The next block of code attempts to adjust the packing of siblings to |
+ ** get a better balance. |
** |
** This adjustment is more than an optimization. The packing above might |
** be so out of balance as to be illegal. For example, the right-most |
@@ -6558,46 +7335,46 @@ static int balance_nonroot( |
r = cntNew[i-1] - 1; |
d = r + 1 - leafData; |
- assert( d<nMaxCells ); |
- assert( r<nMaxCells ); |
- while( szRight==0 |
- || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) |
- ){ |
- szRight += szCell[d] + 2; |
- szLeft -= szCell[r] + 2; |
- cntNew[i-1]--; |
- r = cntNew[i-1] - 1; |
- d = r + 1 - leafData; |
- } |
+ (void)cachedCellSize(&b, d); |
+ do{ |
+ assert( d<nMaxCells ); |
+ assert( r<nMaxCells ); |
+ (void)cachedCellSize(&b, r); |
+ if( szRight!=0 |
+ && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){ |
+ break; |
+ } |
+ szRight += b.szCell[d] + 2; |
+ szLeft -= b.szCell[r] + 2; |
+ cntNew[i-1] = r; |
+ r--; |
+ d--; |
+ }while( r>=0 ); |
szNew[i] = szRight; |
szNew[i-1] = szLeft; |
+ if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto balance_cleanup; |
+ } |
} |
- /* Either we found one or more cells (cntnew[0])>0) or pPage is |
- ** a virtual root page. A virtual root page is when the real root |
- ** page is page 1 and we are the only child of that page. |
- ** |
- ** UPDATE: The assert() below is not necessarily true if the database |
- ** file is corrupt. The corruption will be detected and reported later |
- ** in this procedure so there is no need to act upon it now. |
+ /* Sanity check: For a non-corrupt database file one of the follwing |
+ ** must be true: |
+ ** (1) We found one or more cells (cntNew[0])>0), or |
+ ** (2) pPage is a virtual root page. A virtual root page is when |
+ ** the real root page is page 1 and we are the only child of |
+ ** that page. |
*/ |
-#if 0 |
- assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
-#endif |
- |
- TRACE(("BALANCE: old: %d %d %d ", |
- apOld[0]->pgno, |
- nOld>=2 ? apOld[1]->pgno : 0, |
- nOld>=3 ? apOld[2]->pgno : 0 |
+ assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); |
+ TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", |
+ apOld[0]->pgno, apOld[0]->nCell, |
+ nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, |
+ nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 |
)); |
/* |
** Allocate k new pages. Reuse old pages where possible. |
*/ |
- if( apOld[0]->pgno<=1 ){ |
- rc = SQLITE_CORRUPT_BKPT; |
- goto balance_cleanup; |
- } |
pageFlags = apOld[0]->aData[0]; |
for(i=0; i<k; i++){ |
MemPage *pNew; |
@@ -6611,8 +7388,10 @@ static int balance_nonroot( |
assert( i>0 ); |
rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
if( rc ) goto balance_cleanup; |
+ zeroPage(pNew, pageFlags); |
apNew[i] = pNew; |
nNew++; |
+ cntOld[i] = b.nCell; |
/* Set the pointer-map entry for the new sibling page. */ |
if( ISAUTOVACUUM ){ |
@@ -6624,135 +7403,249 @@ static int balance_nonroot( |
} |
} |
- /* Free any old pages that were not reused as new pages. |
- */ |
- while( i<nOld ){ |
- freePage(apOld[i], &rc); |
- if( rc ) goto balance_cleanup; |
- releasePage(apOld[i]); |
- apOld[i] = 0; |
- i++; |
- } |
- |
/* |
- ** Put the new pages in ascending order. This helps to |
- ** keep entries in the disk file in order so that a scan |
- ** of the table is a linear scan through the file. That |
- ** in turn helps the operating system to deliver pages |
- ** from the disk more rapidly. |
+ ** Reassign page numbers so that the new pages are in ascending order. |
+ ** This helps to keep entries in the disk file in order so that a scan |
+ ** of the table is closer to a linear scan through the file. That in turn |
+ ** helps the operating system to deliver pages from the disk more rapidly. |
** |
- ** An O(n^2) insertion sort algorithm is used, but since |
- ** n is never more than NB (a small constant), that should |
- ** not be a problem. |
+ ** An O(n^2) insertion sort algorithm is used, but since n is never more |
+ ** than (NB+2) (a small constant), that should not be a problem. |
** |
- ** When NB==3, this one optimization makes the database |
- ** about 25% faster for large insertions and deletions. |
+ ** When NB==3, this one optimization makes the database about 25% faster |
+ ** for large insertions and deletions. |
*/ |
- for(i=0; i<k-1; i++){ |
- int minV = apNew[i]->pgno; |
- int minI = i; |
- for(j=i+1; j<k; j++){ |
- if( apNew[j]->pgno<(unsigned)minV ){ |
- minI = j; |
- minV = apNew[j]->pgno; |
+ for(i=0; i<nNew; i++){ |
+ aPgOrder[i] = aPgno[i] = apNew[i]->pgno; |
+ aPgFlags[i] = apNew[i]->pDbPage->flags; |
+ for(j=0; j<i; j++){ |
+ if( aPgno[j]==aPgno[i] ){ |
+ /* This branch is taken if the set of sibling pages somehow contains |
+ ** duplicate entries. This can happen if the database is corrupt. |
+ ** It would be simpler to detect this as part of the loop below, but |
+ ** we do the detection here in order to avoid populating the pager |
+ ** cache with two separate objects associated with the same |
+ ** page number. */ |
+ assert( CORRUPT_DB ); |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto balance_cleanup; |
} |
} |
- if( minI>i ){ |
- MemPage *pT; |
- pT = apNew[i]; |
- apNew[i] = apNew[minI]; |
- apNew[minI] = pT; |
+ } |
+ for(i=0; i<nNew; i++){ |
+ int iBest = 0; /* aPgno[] index of page number to use */ |
+ for(j=1; j<nNew; j++){ |
+ if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; |
+ } |
+ pgno = aPgOrder[iBest]; |
+ aPgOrder[iBest] = 0xffffffff; |
+ if( iBest!=i ){ |
+ if( iBest>i ){ |
+ sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); |
+ } |
+ sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); |
+ apNew[i]->pgno = pgno; |
} |
} |
- TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
- apNew[0]->pgno, szNew[0], |
+ |
+ TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
+ "%d(%d nc=%d) %d(%d nc=%d)\n", |
+ apNew[0]->pgno, szNew[0], cntNew[0], |
nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
+ nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, |
nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
+ nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, |
nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
- nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); |
+ nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, |
+ nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, |
+ nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 |
+ )); |
assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
put4byte(pRight, apNew[nNew-1]->pgno); |
- /* |
- ** Evenly distribute the data in apCell[] across the new pages. |
- ** Insert divider cells into pParent as necessary. |
+ /* If the sibling pages are not leaves, ensure that the right-child pointer |
+ ** of the right-most new sibling page is set to the value that was |
+ ** originally in the same field of the right-most old sibling page. */ |
+ if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ |
+ MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; |
+ memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); |
+ } |
+ |
+ /* Make any required updates to pointer map entries associated with |
+ ** cells stored on sibling pages following the balance operation. Pointer |
+ ** map entries associated with divider cells are set by the insertCell() |
+ ** routine. The associated pointer map entries are: |
+ ** |
+ ** a) if the cell contains a reference to an overflow chain, the |
+ ** entry associated with the first page in the overflow chain, and |
+ ** |
+ ** b) if the sibling pages are not leaves, the child page associated |
+ ** with the cell. |
+ ** |
+ ** If the sibling pages are not leaves, then the pointer map entry |
+ ** associated with the right-child of each sibling may also need to be |
+ ** updated. This happens below, after the sibling pages have been |
+ ** populated, not here. |
*/ |
- j = 0; |
- for(i=0; i<nNew; i++){ |
- /* Assemble the new sibling page. */ |
+ if( ISAUTOVACUUM ){ |
+ MemPage *pNew = apNew[0]; |
+ u8 *aOld = pNew->aData; |
+ int cntOldNext = pNew->nCell + pNew->nOverflow; |
+ int usableSize = pBt->usableSize; |
+ int iNew = 0; |
+ int iOld = 0; |
+ |
+ for(i=0; i<b.nCell; i++){ |
+ u8 *pCell = b.apCell[i]; |
+ if( i==cntOldNext ){ |
+ MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; |
+ cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
+ aOld = pOld->aData; |
+ } |
+ if( i==cntNew[iNew] ){ |
+ pNew = apNew[++iNew]; |
+ if( !leafData ) continue; |
+ } |
+ |
+ /* Cell pCell is destined for new sibling page pNew. Originally, it |
+ ** was either part of sibling page iOld (possibly an overflow cell), |
+ ** or else the divider cell to the left of sibling page iOld. So, |
+ ** if sibling page iOld had the same page number as pNew, and if |
+ ** pCell really was a part of sibling page iOld (not a divider or |
+ ** overflow cell), we can skip updating the pointer map entries. */ |
+ if( iOld>=nNew |
+ || pNew->pgno!=aPgno[iOld] |
+ || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize]) |
+ ){ |
+ if( !leafCorrection ){ |
+ ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
+ } |
+ if( cachedCellSize(&b,i)>pNew->minLocal ){ |
+ ptrmapPutOvflPtr(pNew, pCell, &rc); |
+ } |
+ if( rc ) goto balance_cleanup; |
+ } |
+ } |
+ } |
+ |
+ /* Insert new divider cells into pParent. */ |
+ for(i=0; i<nNew-1; i++){ |
+ u8 *pCell; |
+ u8 *pTemp; |
+ int sz; |
MemPage *pNew = apNew[i]; |
+ j = cntNew[i]; |
+ |
assert( j<nMaxCells ); |
- zeroPage(pNew, pageFlags); |
- assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
- assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
- assert( pNew->nOverflow==0 ); |
+ assert( b.apCell[j]!=0 ); |
+ pCell = b.apCell[j]; |
+ sz = b.szCell[j] + leafCorrection; |
+ pTemp = &aOvflSpace[iOvflSpace]; |
+ if( !pNew->leaf ){ |
+ memcpy(&pNew->aData[8], pCell, 4); |
+ }else if( leafData ){ |
+ /* If the tree is a leaf-data tree, and the siblings are leaves, |
+ ** then there is no divider cell in b.apCell[]. Instead, the divider |
+ ** cell consists of the integer key for the right-most cell of |
+ ** the sibling-page assembled above only. |
+ */ |
+ CellInfo info; |
+ j--; |
+ pNew->xParseCell(pNew, b.apCell[j], &info); |
+ pCell = pTemp; |
+ sz = 4 + putVarint(&pCell[4], info.nKey); |
+ pTemp = 0; |
+ }else{ |
+ pCell -= 4; |
+ /* Obscure case for non-leaf-data trees: If the cell at pCell was |
+ ** previously stored on a leaf node, and its reported size was 4 |
+ ** bytes, then it may actually be smaller than this |
+ ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
+ ** any cell). But it is important to pass the correct size to |
+ ** insertCell(), so reparse the cell now. |
+ ** |
+ ** Note that this can never happen in an SQLite data file, as all |
+ ** cells are at least 4 bytes. It only happens in b-trees used |
+ ** to evaluate "IN (SELECT ...)" and similar clauses. |
+ */ |
+ if( b.szCell[j]==4 ){ |
+ assert(leafCorrection==4); |
+ sz = pParent->xCellSize(pParent, pCell); |
+ } |
+ } |
+ iOvflSpace += sz; |
+ assert( sz<=pBt->maxLocal+23 ); |
+ assert( iOvflSpace <= (int)pBt->pageSize ); |
+ insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); |
+ if( rc!=SQLITE_OK ) goto balance_cleanup; |
+ assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
+ } |
- j = cntNew[i]; |
+ /* Now update the actual sibling pages. The order in which they are updated |
+ ** is important, as this code needs to avoid disrupting any page from which |
+ ** cells may still to be read. In practice, this means: |
+ ** |
+ ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) |
+ ** then it is not safe to update page apNew[iPg] until after |
+ ** the left-hand sibling apNew[iPg-1] has been updated. |
+ ** |
+ ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) |
+ ** then it is not safe to update page apNew[iPg] until after |
+ ** the right-hand sibling apNew[iPg+1] has been updated. |
+ ** |
+ ** If neither of the above apply, the page is safe to update. |
+ ** |
+ ** The iPg value in the following loop starts at nNew-1 goes down |
+ ** to 0, then back up to nNew-1 again, thus making two passes over |
+ ** the pages. On the initial downward pass, only condition (1) above |
+ ** needs to be tested because (2) will always be true from the previous |
+ ** step. On the upward pass, both conditions are always true, so the |
+ ** upwards pass simply processes pages that were missed on the downward |
+ ** pass. |
+ */ |
+ for(i=1-nNew; i<nNew; i++){ |
+ int iPg = i<0 ? -i : i; |
+ assert( iPg>=0 && iPg<nNew ); |
+ if( abDone[iPg] ) continue; /* Skip pages already processed */ |
+ if( i>=0 /* On the upwards pass, or... */ |
+ || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ |
+ ){ |
+ int iNew; |
+ int iOld; |
+ int nNewCell; |
- /* If the sibling page assembled above was not the right-most sibling, |
- ** insert a divider cell into the parent page. |
- */ |
- assert( i<nNew-1 || j==nCell ); |
- if( j<nCell ){ |
- u8 *pCell; |
- u8 *pTemp; |
- int sz; |
+ /* Verify condition (1): If cells are moving left, update iPg |
+ ** only after iPg-1 has already been updated. */ |
+ assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); |
- assert( j<nMaxCells ); |
- pCell = apCell[j]; |
- sz = szCell[j] + leafCorrection; |
- pTemp = &aOvflSpace[iOvflSpace]; |
- if( !pNew->leaf ){ |
- memcpy(&pNew->aData[8], pCell, 4); |
- }else if( leafData ){ |
- /* If the tree is a leaf-data tree, and the siblings are leaves, |
- ** then there is no divider cell in apCell[]. Instead, the divider |
- ** cell consists of the integer key for the right-most cell of |
- ** the sibling-page assembled above only. |
- */ |
- CellInfo info; |
- j--; |
- btreeParseCellPtr(pNew, apCell[j], &info); |
- pCell = pTemp; |
- sz = 4 + putVarint(&pCell[4], info.nKey); |
- pTemp = 0; |
+ /* Verify condition (2): If cells are moving right, update iPg |
+ ** only after iPg+1 has already been updated. */ |
+ assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); |
+ |
+ if( iPg==0 ){ |
+ iNew = iOld = 0; |
+ nNewCell = cntNew[0]; |
}else{ |
- pCell -= 4; |
- /* Obscure case for non-leaf-data trees: If the cell at pCell was |
- ** previously stored on a leaf node, and its reported size was 4 |
- ** bytes, then it may actually be smaller than this |
- ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
- ** any cell). But it is important to pass the correct size to |
- ** insertCell(), so reparse the cell now. |
- ** |
- ** Note that this can never happen in an SQLite data file, as all |
- ** cells are at least 4 bytes. It only happens in b-trees used |
- ** to evaluate "IN (SELECT ...)" and similar clauses. |
- */ |
- if( szCell[j]==4 ){ |
- assert(leafCorrection==4); |
- sz = cellSizePtr(pParent, pCell); |
- } |
+ iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell; |
+ iNew = cntNew[iPg-1] + !leafData; |
+ nNewCell = cntNew[iPg] - iNew; |
} |
- iOvflSpace += sz; |
- assert( sz<=pBt->maxLocal+23 ); |
- assert( iOvflSpace <= (int)pBt->pageSize ); |
- insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); |
- if( rc!=SQLITE_OK ) goto balance_cleanup; |
- assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
- j++; |
- nxDiv++; |
+ rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b); |
+ if( rc ) goto balance_cleanup; |
+ abDone[iPg]++; |
+ apNew[iPg]->nFree = usableSpace-szNew[iPg]; |
+ assert( apNew[iPg]->nOverflow==0 ); |
+ assert( apNew[iPg]->nCell==nNewCell ); |
} |
} |
- assert( j==nCell ); |
+ |
+ /* All pages have been processed exactly once */ |
+ assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); |
+ |
assert( nOld>0 ); |
assert( nNew>0 ); |
- if( (pageFlags & PTF_LEAF)==0 ){ |
- u8 *zChild = &apCopy[nOld-1]->aData[8]; |
- memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
- } |
if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
/* The root page of the b-tree now contains no cells. The only sibling |
@@ -6765,132 +7658,56 @@ static int balance_nonroot( |
** sets all pointer-map entries corresponding to database image pages |
** for which the pointer is stored within the content being copied. |
** |
- ** The second assert below verifies that the child page is defragmented |
- ** (it must be, as it was just reconstructed using assemblePage()). This |
- ** is important if the parent page happens to be page 1 of the database |
- ** image. */ |
- assert( nNew==1 ); |
+ ** It is critical that the child page be defragmented before being |
+ ** copied into the parent, because if the parent is page 1 then it will |
+ ** by smaller than the child due to the database header, and so all the |
+ ** free space needs to be up front. |
+ */ |
+ assert( nNew==1 || CORRUPT_DB ); |
+ rc = defragmentPage(apNew[0]); |
+ testcase( rc!=SQLITE_OK ); |
assert( apNew[0]->nFree == |
- (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
+ (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
+ || rc!=SQLITE_OK |
); |
copyNodeContent(apNew[0], pParent, &rc); |
freePage(apNew[0], &rc); |
- }else if( ISAUTOVACUUM ){ |
- /* Fix the pointer-map entries for all the cells that were shifted around. |
- ** There are several different types of pointer-map entries that need to |
- ** be dealt with by this routine. Some of these have been set already, but |
- ** many have not. The following is a summary: |
- ** |
- ** 1) The entries associated with new sibling pages that were not |
- ** siblings when this function was called. These have already |
- ** been set. We don't need to worry about old siblings that were |
- ** moved to the free-list - the freePage() code has taken care |
- ** of those. |
- ** |
- ** 2) The pointer-map entries associated with the first overflow |
- ** page in any overflow chains used by new divider cells. These |
- ** have also already been taken care of by the insertCell() code. |
- ** |
- ** 3) If the sibling pages are not leaves, then the child pages of |
- ** cells stored on the sibling pages may need to be updated. |
- ** |
- ** 4) If the sibling pages are not internal intkey nodes, then any |
- ** overflow pages used by these cells may need to be updated |
- ** (internal intkey nodes never contain pointers to overflow pages). |
- ** |
- ** 5) If the sibling pages are not leaves, then the pointer-map |
- ** entries for the right-child pages of each sibling may need |
- ** to be updated. |
- ** |
- ** Cases 1 and 2 are dealt with above by other code. The next |
- ** block deals with cases 3 and 4 and the one after that, case 5. Since |
- ** setting a pointer map entry is a relatively expensive operation, this |
- ** code only sets pointer map entries for child or overflow pages that have |
- ** actually moved between pages. */ |
- MemPage *pNew = apNew[0]; |
- MemPage *pOld = apCopy[0]; |
- int nOverflow = pOld->nOverflow; |
- int iNextOld = pOld->nCell + nOverflow; |
- int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1); |
- j = 0; /* Current 'old' sibling page */ |
- k = 0; /* Current 'new' sibling page */ |
- for(i=0; i<nCell; i++){ |
- int isDivider = 0; |
- while( i==iNextOld ){ |
- /* Cell i is the cell immediately following the last cell on old |
- ** sibling page j. If the siblings are not leaf pages of an |
- ** intkey b-tree, then cell i was a divider cell. */ |
- assert( j+1 < ArraySize(apCopy) ); |
- assert( j+1 < nOld ); |
- pOld = apCopy[++j]; |
- iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
- if( pOld->nOverflow ){ |
- nOverflow = pOld->nOverflow; |
- iOverflow = i + !leafData + pOld->aiOvfl[0]; |
- } |
- isDivider = !leafData; |
- } |
- |
- assert(nOverflow>0 || iOverflow<i ); |
- assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1); |
- assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1); |
- if( i==iOverflow ){ |
- isDivider = 1; |
- if( (--nOverflow)>0 ){ |
- iOverflow++; |
- } |
- } |
- |
- if( i==cntNew[k] ){ |
- /* Cell i is the cell immediately following the last cell on new |
- ** sibling page k. If the siblings are not leaf pages of an |
- ** intkey b-tree, then cell i is a divider cell. */ |
- pNew = apNew[++k]; |
- if( !leafData ) continue; |
- } |
- assert( j<nOld ); |
- assert( k<nNew ); |
- |
- /* If the cell was originally divider cell (and is not now) or |
- ** an overflow cell, or if the cell was located on a different sibling |
- ** page before the balancing, then the pointer map entries associated |
- ** with any child or overflow pages need to be updated. */ |
- if( isDivider || pOld->pgno!=pNew->pgno ){ |
- if( !leafCorrection ){ |
- ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); |
- } |
- if( szCell[i]>pNew->minLocal ){ |
- ptrmapPutOvflPtr(pNew, apCell[i], &rc); |
- } |
- } |
+ }else if( ISAUTOVACUUM && !leafCorrection ){ |
+ /* Fix the pointer map entries associated with the right-child of each |
+ ** sibling page. All other pointer map entries have already been taken |
+ ** care of. */ |
+ for(i=0; i<nNew; i++){ |
+ u32 key = get4byte(&apNew[i]->aData[8]); |
+ ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
} |
+ } |
- if( !leafCorrection ){ |
- for(i=0; i<nNew; i++){ |
- u32 key = get4byte(&apNew[i]->aData[8]); |
- ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
- } |
- } |
+ assert( pParent->isInit ); |
+ TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
+ nOld, nNew, b.nCell)); |
+ |
+ /* Free any old pages that were not reused as new pages. |
+ */ |
+ for(i=nNew; i<nOld; i++){ |
+ freePage(apOld[i], &rc); |
+ } |
#if 0 |
+ if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ |
/* The ptrmapCheckPages() contains assert() statements that verify that |
** all pointer map pages are set correctly. This is helpful while |
** debugging. This is usually disabled because a corrupt database may |
** cause an assert() statement to fail. */ |
ptrmapCheckPages(apNew, nNew); |
ptrmapCheckPages(&pParent, 1); |
-#endif |
} |
- |
- assert( pParent->isInit ); |
- TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
- nOld, nNew, nCell)); |
+#endif |
/* |
** Cleanup before returning. |
*/ |
balance_cleanup: |
- sqlite3ScratchFree(apCell); |
+ sqlite3ScratchFree(b.apCell); |
for(i=0; i<nOld; i++){ |
releasePage(apOld[i]); |
} |
@@ -6900,9 +7717,6 @@ balance_cleanup: |
return rc; |
} |
-#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) |
-#pragma optimize("", on) |
-#endif |
/* |
@@ -7063,7 +7877,8 @@ static int balance(BtCursor *pCur){ |
** pSpace buffer passed to the latter call to balance_nonroot(). |
*/ |
u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); |
- rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints); |
+ rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, |
+ pCur->hints&BTREE_BULKLOAD); |
if( pFree ){ |
/* If pFree is not NULL, it points to the pSpace buffer used |
** by a previous call to balance_nonroot(). Its contents are |
@@ -7084,6 +7899,7 @@ static int balance(BtCursor *pCur){ |
/* The next iteration of the do-loop balances the parent page. */ |
releasePage(pPage); |
pCur->iPage--; |
+ assert( pCur->iPage>=0 ); |
} |
}while( rc==SQLITE_OK ); |
@@ -7163,24 +7979,28 @@ int sqlite3BtreeInsert( |
** doing any work. To avoid thwarting these optimizations, it is important |
** not to clear the cursor here. |
*/ |
- rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
- if( rc ) return rc; |
+ if( pCur->curFlags & BTCF_Multiple ){ |
+ rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
+ if( rc ) return rc; |
+ } |
if( pCur->pKeyInfo==0 ){ |
+ assert( pKey==0 ); |
/* If this is an insert into a table b-tree, invalidate any incrblob |
** cursors open on the row being replaced */ |
invalidateIncrblobCursors(p, nKey, 0); |
/* If the cursor is currently on the last row and we are appending a |
- ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto() |
- ** call */ |
+ ** new row onto the end, set the "loc" to avoid an unnecessary |
+ ** btreeMoveto() call */ |
if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 |
&& pCur->info.nKey==nKey-1 ){ |
- loc = -1; |
+ loc = -1; |
+ }else if( loc==0 ){ |
+ rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc); |
+ if( rc ) return rc; |
} |
- } |
- |
- if( !loc ){ |
+ }else if( loc==0 ){ |
rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); |
if( rc ) return rc; |
} |
@@ -7198,7 +8018,7 @@ int sqlite3BtreeInsert( |
assert( newCell!=0 ); |
rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
if( rc ) goto end_insert; |
- assert( szNew==cellSizePtr(pPage, newCell) ); |
+ assert( szNew==pPage->xCellSize(pPage, newCell) ); |
assert( szNew <= MX_CELL_SIZE(pBt) ); |
idx = pCur->aiIdx[pCur->iPage]; |
if( loc==0 ){ |
@@ -7263,10 +8083,15 @@ end_insert: |
} |
/* |
-** Delete the entry that the cursor is pointing to. The cursor |
-** is left pointing at an arbitrary location. |
+** Delete the entry that the cursor is pointing to. |
+** |
+** If the second parameter is zero, then the cursor is left pointing at an |
+** arbitrary location after the delete. If it is non-zero, then the cursor |
+** is left in a state such that the next call to BtreeNext() or BtreePrev() |
+** moves it to the same row as it would if the call to BtreeDelete() had |
+** been omitted. |
*/ |
-int sqlite3BtreeDelete(BtCursor *pCur){ |
+int sqlite3BtreeDelete(BtCursor *pCur, int bPreserve){ |
Btree *p = pCur->pBtree; |
BtShared *pBt = p->pBt; |
int rc; /* Return code */ |
@@ -7275,6 +8100,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){ |
int iCellIdx; /* Index of cell to delete */ |
int iCellDepth; /* Depth of node containing pCell */ |
u16 szCell; /* Size of the cell being deleted */ |
+ int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */ |
assert( cursorHoldsMutex(pCur) ); |
assert( pBt->inTransaction==TRANS_WRITE ); |
@@ -7282,12 +8108,8 @@ int sqlite3BtreeDelete(BtCursor *pCur){ |
assert( pCur->curFlags & BTCF_WriteFlag ); |
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
assert( !hasReadConflicts(p, pCur->pgnoRoot) ); |
- |
- if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) |
- || NEVER(pCur->eState!=CURSOR_VALID) |
- ){ |
- return SQLITE_ERROR; /* Something has gone awry. */ |
- } |
+ assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
+ assert( pCur->eState==CURSOR_VALID ); |
iCellDepth = pCur->iPage; |
iCellIdx = pCur->aiIdx[iCellDepth]; |
@@ -7308,12 +8130,11 @@ int sqlite3BtreeDelete(BtCursor *pCur){ |
} |
/* Save the positions of any other cursors open on this table before |
- ** making any modifications. Make the page containing the entry to be |
- ** deleted writable. Then free any overflow pages associated with the |
- ** entry and finally remove the cell itself from within the page. |
- */ |
- rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
- if( rc ) return rc; |
+ ** making any modifications. */ |
+ if( pCur->curFlags & BTCF_Multiple ){ |
+ rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
+ if( rc ) return rc; |
+ } |
/* If this is a delete operation to remove a row from a table b-tree, |
** invalidate any incrblob cursors open on the row being deleted. */ |
@@ -7321,6 +8142,31 @@ int sqlite3BtreeDelete(BtCursor *pCur){ |
invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
} |
+ /* If the bPreserve flag is set to true, then the cursor position must |
+ ** be preserved following this delete operation. If the current delete |
+ ** will cause a b-tree rebalance, then this is done by saving the cursor |
+ ** key and leaving the cursor in CURSOR_REQUIRESEEK state before |
+ ** returning. |
+ ** |
+ ** Or, if the current delete will not cause a rebalance, then the cursor |
+ ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately |
+ ** before or after the deleted entry. In this case set bSkipnext to true. */ |
+ if( bPreserve ){ |
+ if( !pPage->leaf |
+ || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3) |
+ ){ |
+ /* A b-tree rebalance will be required after deleting this entry. |
+ ** Save the cursor key. */ |
+ rc = saveCursorKey(pCur); |
+ if( rc ) return rc; |
+ }else{ |
+ bSkipnext = 1; |
+ } |
+ } |
+ |
+ /* Make the page containing the entry to be deleted writable. Then free any |
+ ** overflow pages associated with the entry and finally remove the cell |
+ ** itself from within the page. */ |
rc = sqlite3PagerWrite(pPage->pDbPage); |
if( rc ) return rc; |
rc = clearCell(pPage, pCell, &szCell); |
@@ -7339,7 +8185,8 @@ int sqlite3BtreeDelete(BtCursor *pCur){ |
unsigned char *pTmp; |
pCell = findCell(pLeaf, pLeaf->nCell-1); |
- nCell = cellSizePtr(pLeaf, pCell); |
+ if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; |
+ nCell = pLeaf->xCellSize(pLeaf, pCell); |
assert( MX_CELL_SIZE(pBt) >= nCell ); |
pTmp = pBt->pTmpSpace; |
assert( pTmp!=0 ); |
@@ -7373,7 +8220,23 @@ int sqlite3BtreeDelete(BtCursor *pCur){ |
} |
if( rc==SQLITE_OK ){ |
- moveToRoot(pCur); |
+ if( bSkipnext ){ |
+ assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) ); |
+ assert( pPage==pCur->apPage[pCur->iPage] ); |
+ assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell ); |
+ pCur->eState = CURSOR_SKIPNEXT; |
+ if( iCellIdx>=pPage->nCell ){ |
+ pCur->skipNext = -1; |
+ pCur->aiIdx[iCellDepth] = pPage->nCell-1; |
+ }else{ |
+ pCur->skipNext = 1; |
+ } |
+ }else{ |
+ rc = moveToRoot(pCur); |
+ if( bPreserve ){ |
+ pCur->eState = CURSOR_REQUIRESEEK; |
+ } |
+ } |
} |
return rc; |
} |
@@ -7431,7 +8294,8 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ |
pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
pgnoRoot++; |
} |
- assert( pgnoRoot>=3 ); |
+ assert( pgnoRoot>=3 || CORRUPT_DB ); |
+ testcase( pgnoRoot<3 ); |
/* Allocate a page. The page that currently resides at pgnoRoot will |
** be moved to the allocated page (unless the allocated page happens |
@@ -7560,9 +8424,13 @@ static int clearDatabasePage( |
if( pgno>btreePagecount(pBt) ){ |
return SQLITE_CORRUPT_BKPT; |
} |
- |
- rc = getAndInitPage(pBt, pgno, &pPage, 0); |
+ rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); |
if( rc ) return rc; |
+ if( pPage->bBusy ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto cleardatabasepage_out; |
+ } |
+ pPage->bBusy = 1; |
hdr = pPage->hdrOffset; |
for(i=0; i<pPage->nCell; i++){ |
pCell = findCell(pPage, i); |
@@ -7577,7 +8445,8 @@ static int clearDatabasePage( |
rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); |
if( rc ) goto cleardatabasepage_out; |
}else if( pnChange ){ |
- assert( pPage->intKey ); |
+ assert( pPage->intKey || CORRUPT_DB ); |
+ testcase( !pPage->intKey ); |
*pnChange += pPage->nCell; |
} |
if( freePageFlag ){ |
@@ -7587,6 +8456,7 @@ static int clearDatabasePage( |
} |
cleardatabasepage_out: |
+ pPage->bBusy = 0; |
releasePage(pPage); |
return rc; |
} |
@@ -7776,6 +8646,13 @@ int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
** The schema layer numbers meta values differently. At the schema |
** layer (and the SetCookie and ReadCookie opcodes) the number of |
** free pages is not visible. So Cookie[0] is the same as Meta[1]. |
+** |
+** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead |
+** of reading the value out of the header, it instead loads the "DataVersion" |
+** from the pager. The BTREE_DATA_VERSION value is not actually stored in the |
+** database file. It is a number computed by the pager. But its access |
+** pattern is the same as header meta values, and so it is convenient to |
+** read it from this routine. |
*/ |
void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
BtShared *pBt = p->pBt; |
@@ -7786,7 +8663,11 @@ void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
assert( pBt->pPage1 ); |
assert( idx>=0 && idx<=15 ); |
- *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); |
+ if( idx==BTREE_DATA_VERSION ){ |
+ *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion; |
+ }else{ |
+ *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); |
+ } |
/* If auto-vacuum is disabled in this build and this is an auto-vacuum |
** database, mark the database as read-only. */ |
@@ -7877,7 +8758,7 @@ int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
if( pCur->iPage==0 ){ |
/* All pages of the b-tree have been visited. Return successfully. */ |
*pnEntry = nEntry; |
- return SQLITE_OK; |
+ return moveToRoot(pCur); |
} |
moveToParent(pCur); |
}while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); |
@@ -7920,7 +8801,6 @@ static void checkAppendMsg( |
... |
){ |
va_list ap; |
- char zBuf[200]; |
if( !pCheck->mxErr ) return; |
pCheck->mxErr--; |
pCheck->nErr++; |
@@ -7929,8 +8809,7 @@ static void checkAppendMsg( |
sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
} |
if( pCheck->zPfx ){ |
- sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2); |
- sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf); |
+ sqlite3XPrintf(&pCheck->errMsg, 0, pCheck->zPfx, pCheck->v1, pCheck->v2); |
} |
sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
va_end(ap); |
@@ -8036,7 +8915,7 @@ static void checkList( |
break; |
} |
if( checkRef(pCheck, iPage) ) break; |
- if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
+ if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){ |
checkAppendMsg(pCheck, "failed to get page %d", iPage); |
break; |
} |
@@ -8079,10 +8958,65 @@ static void checkList( |
#endif |
iPage = get4byte(pOvflData); |
sqlite3PagerUnref(pOvflPage); |
+ |
+ if( isFreeList && N<(iPage!=0) ){ |
+ checkAppendMsg(pCheck, "free-page count in header is too small"); |
+ } |
} |
} |
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
+/* |
+** An implementation of a min-heap. |
+** |
+** aHeap[0] is the number of elements on the heap. aHeap[1] is the |
+** root element. The daughter nodes of aHeap[N] are aHeap[N*2] |
+** and aHeap[N*2+1]. |
+** |
+** The heap property is this: Every node is less than or equal to both |
+** of its daughter nodes. A consequence of the heap property is that the |
+** root node aHeap[1] is always the minimum value currently in the heap. |
+** |
+** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto |
+** the heap, preserving the heap property. The btreeHeapPull() routine |
+** removes the root element from the heap (the minimum value in the heap) |
+** and then moves other nodes around as necessary to preserve the heap |
+** property. |
+** |
+** This heap is used for cell overlap and coverage testing. Each u32 |
+** entry represents the span of a cell or freeblock on a btree page. |
+** The upper 16 bits are the index of the first byte of a range and the |
+** lower 16 bits are the index of the last byte of that range. |
+*/ |
+static void btreeHeapInsert(u32 *aHeap, u32 x){ |
+ u32 j, i = ++aHeap[0]; |
+ aHeap[i] = x; |
+ while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){ |
+ x = aHeap[j]; |
+ aHeap[j] = aHeap[i]; |
+ aHeap[i] = x; |
+ i = j; |
+ } |
+} |
+static int btreeHeapPull(u32 *aHeap, u32 *pOut){ |
+ u32 j, i, x; |
+ if( (x = aHeap[0])==0 ) return 0; |
+ *pOut = aHeap[1]; |
+ aHeap[1] = aHeap[x]; |
+ aHeap[x] = 0xffffffff; |
+ aHeap[0]--; |
+ i = 1; |
+ while( (j = i*2)<=aHeap[0] ){ |
+ if( aHeap[j]>aHeap[j+1] ) j++; |
+ if( aHeap[i]<aHeap[j] ) break; |
+ x = aHeap[i]; |
+ aHeap[i] = aHeap[j]; |
+ aHeap[j] = x; |
+ i = j; |
+ } |
+ return 1; |
+} |
+ |
#ifndef SQLITE_OMIT_INTEGRITY_CHECK |
/* |
** Do various sanity checks on a single page of a tree. Return |
@@ -8093,34 +9027,42 @@ static void checkList( |
** |
** 1. Make sure that cells and freeblocks do not overlap |
** but combine to completely cover the page. |
-** NO 2. Make sure cell keys are in order. |
-** NO 3. Make sure no key is less than or equal to zLowerBound. |
-** NO 4. Make sure no key is greater than or equal to zUpperBound. |
-** 5. Check the integrity of overflow pages. |
-** 6. Recursively call checkTreePage on all children. |
-** 7. Verify that the depth of all children is the same. |
-** 8. Make sure this page is at least 33% full or else it is |
-** the root of the tree. |
+** 2. Make sure integer cell keys are in order. |
+** 3. Check the integrity of overflow pages. |
+** 4. Recursively call checkTreePage on all children. |
+** 5. Verify that the depth of all children is the same. |
*/ |
static int checkTreePage( |
IntegrityCk *pCheck, /* Context for the sanity check */ |
int iPage, /* Page number of the page to check */ |
- i64 *pnParentMinKey, |
- i64 *pnParentMaxKey |
+ i64 *piMinKey, /* Write minimum integer primary key here */ |
+ i64 maxKey /* Error if integer primary key greater than this */ |
){ |
- MemPage *pPage; |
- int i, rc, depth, d2, pgno, cnt; |
- int hdr, cellStart; |
- int nCell; |
- u8 *data; |
- BtShared *pBt; |
- int usableSize; |
- char *hit = 0; |
- i64 nMinKey = 0; |
- i64 nMaxKey = 0; |
+ MemPage *pPage = 0; /* The page being analyzed */ |
+ int i; /* Loop counter */ |
+ int rc; /* Result code from subroutine call */ |
+ int depth = -1, d2; /* Depth of a subtree */ |
+ int pgno; /* Page number */ |
+ int nFrag; /* Number of fragmented bytes on the page */ |
+ int hdr; /* Offset to the page header */ |
+ int cellStart; /* Offset to the start of the cell pointer array */ |
+ int nCell; /* Number of cells */ |
+ int doCoverageCheck = 1; /* True if cell coverage checking should be done */ |
+ int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey |
+ ** False if IPK must be strictly less than maxKey */ |
+ u8 *data; /* Page content */ |
+ u8 *pCell; /* Cell content */ |
+ u8 *pCellIdx; /* Next element of the cell pointer array */ |
+ BtShared *pBt; /* The BtShared object that owns pPage */ |
+ u32 pc; /* Address of a cell */ |
+ u32 usableSize; /* Usable size of the page */ |
+ u32 contentOffset; /* Offset to the start of the cell content area */ |
+ u32 *heap = 0; /* Min-heap used for checking cell coverage */ |
+ u32 x, prev = 0; /* Next and previous entry on the min-heap */ |
const char *saved_zPfx = pCheck->zPfx; |
int saved_v1 = pCheck->v1; |
int saved_v2 = pCheck->v2; |
+ u8 savedIsInit = 0; |
/* Check that the page exists |
*/ |
@@ -8133,54 +9075,95 @@ static int checkTreePage( |
if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
checkAppendMsg(pCheck, |
"unable to get the page. error code=%d", rc); |
- depth = -1; |
goto end_of_check; |
} |
/* Clear MemPage.isInit to make sure the corruption detection code in |
** btreeInitPage() is executed. */ |
+ savedIsInit = pPage->isInit; |
pPage->isInit = 0; |
if( (rc = btreeInitPage(pPage))!=0 ){ |
assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
checkAppendMsg(pCheck, |
"btreeInitPage() returns error code %d", rc); |
- releasePage(pPage); |
- depth = -1; |
goto end_of_check; |
} |
+ data = pPage->aData; |
+ hdr = pPage->hdrOffset; |
- /* Check out all the cells. |
- */ |
- depth = 0; |
- for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
- u8 *pCell; |
- u32 sz; |
+ /* Set up for cell analysis */ |
+ pCheck->zPfx = "On tree page %d cell %d: "; |
+ contentOffset = get2byteNotZero(&data[hdr+5]); |
+ assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
+ |
+ /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
+ ** number of cells on the page. */ |
+ nCell = get2byte(&data[hdr+3]); |
+ assert( pPage->nCell==nCell ); |
+ |
+ /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
+ ** immediately follows the b-tree page header. */ |
+ cellStart = hdr + 12 - 4*pPage->leaf; |
+ assert( pPage->aCellIdx==&data[cellStart] ); |
+ pCellIdx = &data[cellStart + 2*(nCell-1)]; |
+ |
+ if( !pPage->leaf ){ |
+ /* Analyze the right-child page of internal pages */ |
+ pgno = get4byte(&data[hdr+8]); |
+#ifndef SQLITE_OMIT_AUTOVACUUM |
+ if( pBt->autoVacuum ){ |
+ pCheck->zPfx = "On page %d at right child: "; |
+ checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
+ } |
+#endif |
+ depth = checkTreePage(pCheck, pgno, &maxKey, maxKey); |
+ keyCanBeEqual = 0; |
+ }else{ |
+ /* For leaf pages, the coverage check will occur in the same loop |
+ ** as the other cell checks, so initialize the heap. */ |
+ heap = pCheck->heap; |
+ heap[0] = 0; |
+ } |
+ |
+ /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
+ ** integer offsets to the cell contents. */ |
+ for(i=nCell-1; i>=0 && pCheck->mxErr; i--){ |
CellInfo info; |
- /* Check payload overflow pages |
- */ |
- pCheck->zPfx = "On tree page %d cell %d: "; |
- pCheck->v1 = iPage; |
+ /* Check cell size */ |
pCheck->v2 = i; |
- pCell = findCell(pPage,i); |
- btreeParseCellPtr(pPage, pCell, &info); |
- sz = info.nPayload; |
- /* For intKey pages, check that the keys are in order. |
- */ |
+ assert( pCellIdx==&data[cellStart + i*2] ); |
+ pc = get2byteAligned(pCellIdx); |
+ pCellIdx -= 2; |
+ if( pc<contentOffset || pc>usableSize-4 ){ |
+ checkAppendMsg(pCheck, "Offset %d out of range %d..%d", |
+ pc, contentOffset, usableSize-4); |
+ doCoverageCheck = 0; |
+ continue; |
+ } |
+ pCell = &data[pc]; |
+ pPage->xParseCell(pPage, pCell, &info); |
+ if( pc+info.nSize>usableSize ){ |
+ checkAppendMsg(pCheck, "Extends off end of page"); |
+ doCoverageCheck = 0; |
+ continue; |
+ } |
+ |
+ /* Check for integer primary key out of range */ |
if( pPage->intKey ){ |
- if( i==0 ){ |
- nMinKey = nMaxKey = info.nKey; |
- }else if( info.nKey <= nMaxKey ){ |
- checkAppendMsg(pCheck, |
- "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
+ if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){ |
+ checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); |
} |
- nMaxKey = info.nKey; |
+ maxKey = info.nKey; |
} |
- if( (sz>info.nLocal) |
- && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
- ){ |
- int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
- Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
+ |
+ /* Check the content overflow list */ |
+ if( info.nPayload>info.nLocal ){ |
+ int nPage; /* Number of pages on the overflow chain */ |
+ Pgno pgnoOvfl; /* First page of the overflow chain */ |
+ assert( pc + info.nSize - 4 <= usableSize ); |
+ nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4); |
+ pgnoOvfl = get4byte(&pCell[info.nSize - 4]); |
#ifndef SQLITE_OMIT_AUTOVACUUM |
if( pBt->autoVacuum ){ |
checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); |
@@ -8189,134 +9172,109 @@ static int checkTreePage( |
checkList(pCheck, 0, pgnoOvfl, nPage); |
} |
- /* Check sanity of left child page. |
- */ |
if( !pPage->leaf ){ |
+ /* Check sanity of left child page for internal pages */ |
pgno = get4byte(pCell); |
#ifndef SQLITE_OMIT_AUTOVACUUM |
if( pBt->autoVacuum ){ |
checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
} |
#endif |
- d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey); |
- if( i>0 && d2!=depth ){ |
+ d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey); |
+ keyCanBeEqual = 0; |
+ if( d2!=depth ){ |
checkAppendMsg(pCheck, "Child page depth differs"); |
+ depth = d2; |
} |
- depth = d2; |
- } |
- } |
- |
- if( !pPage->leaf ){ |
- pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
- pCheck->zPfx = "On page %d at right child: "; |
- pCheck->v1 = iPage; |
-#ifndef SQLITE_OMIT_AUTOVACUUM |
- if( pBt->autoVacuum ){ |
- checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
- } |
-#endif |
- checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey); |
- } |
- |
- /* For intKey leaf pages, check that the min/max keys are in order |
- ** with any left/parent/right pages. |
- */ |
- pCheck->zPfx = "Page %d: "; |
- pCheck->v1 = iPage; |
- if( pPage->leaf && pPage->intKey ){ |
- /* if we are a left child page */ |
- if( pnParentMinKey ){ |
- /* if we are the left most child page */ |
- if( !pnParentMaxKey ){ |
- if( nMaxKey > *pnParentMinKey ){ |
- checkAppendMsg(pCheck, |
- "Rowid %lld out of order (max larger than parent min of %lld)", |
- nMaxKey, *pnParentMinKey); |
- } |
- }else{ |
- if( nMinKey <= *pnParentMinKey ){ |
- checkAppendMsg(pCheck, |
- "Rowid %lld out of order (min less than parent min of %lld)", |
- nMinKey, *pnParentMinKey); |
- } |
- if( nMaxKey > *pnParentMaxKey ){ |
- checkAppendMsg(pCheck, |
- "Rowid %lld out of order (max larger than parent max of %lld)", |
- nMaxKey, *pnParentMaxKey); |
- } |
- *pnParentMinKey = nMaxKey; |
- } |
- /* else if we're a right child page */ |
- } else if( pnParentMaxKey ){ |
- if( nMinKey <= *pnParentMaxKey ){ |
- checkAppendMsg(pCheck, |
- "Rowid %lld out of order (min less than parent max of %lld)", |
- nMinKey, *pnParentMaxKey); |
- } |
+ }else{ |
+ /* Populate the coverage-checking heap for leaf pages */ |
+ btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1)); |
} |
} |
+ *piMinKey = maxKey; |
/* Check for complete coverage of the page |
*/ |
- data = pPage->aData; |
- hdr = pPage->hdrOffset; |
- hit = sqlite3PageMalloc( pBt->pageSize ); |
pCheck->zPfx = 0; |
- if( hit==0 ){ |
- pCheck->mallocFailed = 1; |
- }else{ |
- int contentOffset = get2byteNotZero(&data[hdr+5]); |
- assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
- memset(hit+contentOffset, 0, usableSize-contentOffset); |
- memset(hit, 1, contentOffset); |
- nCell = get2byte(&data[hdr+3]); |
- cellStart = hdr + 12 - 4*pPage->leaf; |
- for(i=0; i<nCell; i++){ |
- int pc = get2byte(&data[cellStart+i*2]); |
- u32 size = 65536; |
- int j; |
- if( pc<=usableSize-4 ){ |
- size = cellSizePtr(pPage, &data[pc]); |
- } |
- if( (int)(pc+size-1)>=usableSize ){ |
- pCheck->zPfx = 0; |
- checkAppendMsg(pCheck, |
- "Corruption detected in cell %d on page %d",i,iPage); |
- }else{ |
- for(j=pc+size-1; j>=pc; j--) hit[j]++; |
+ if( doCoverageCheck && pCheck->mxErr>0 ){ |
+ /* For leaf pages, the min-heap has already been initialized and the |
+ ** cells have already been inserted. But for internal pages, that has |
+ ** not yet been done, so do it now */ |
+ if( !pPage->leaf ){ |
+ heap = pCheck->heap; |
+ heap[0] = 0; |
+ for(i=nCell-1; i>=0; i--){ |
+ u32 size; |
+ pc = get2byteAligned(&data[cellStart+i*2]); |
+ size = pPage->xCellSize(pPage, &data[pc]); |
+ btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); |
} |
} |
+ /* Add the freeblocks to the min-heap |
+ ** |
+ ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
+ ** is the offset of the first freeblock, or zero if there are no |
+ ** freeblocks on the page. |
+ */ |
i = get2byte(&data[hdr+1]); |
while( i>0 ){ |
int size, j; |
- assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
+ assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
size = get2byte(&data[i+2]); |
- assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
- for(j=i+size-1; j>=i; j--) hit[j]++; |
+ assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */ |
+ btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1)); |
+ /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a |
+ ** big-endian integer which is the offset in the b-tree page of the next |
+ ** freeblock in the chain, or zero if the freeblock is the last on the |
+ ** chain. */ |
j = get2byte(&data[i]); |
+ /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
+ ** increasing offset. */ |
assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
- assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
+ assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
i = j; |
} |
- for(i=cnt=0; i<usableSize; i++){ |
- if( hit[i]==0 ){ |
- cnt++; |
- }else if( hit[i]>1 ){ |
+ /* Analyze the min-heap looking for overlap between cells and/or |
+ ** freeblocks, and counting the number of untracked bytes in nFrag. |
+ ** |
+ ** Each min-heap entry is of the form: (start_address<<16)|end_address. |
+ ** There is an implied first entry the covers the page header, the cell |
+ ** pointer index, and the gap between the cell pointer index and the start |
+ ** of cell content. |
+ ** |
+ ** The loop below pulls entries from the min-heap in order and compares |
+ ** the start_address against the previous end_address. If there is an |
+ ** overlap, that means bytes are used multiple times. If there is a gap, |
+ ** that gap is added to the fragmentation count. |
+ */ |
+ nFrag = 0; |
+ prev = contentOffset - 1; /* Implied first min-heap entry */ |
+ while( btreeHeapPull(heap,&x) ){ |
+ if( (prev&0xffff)>=(x>>16) ){ |
checkAppendMsg(pCheck, |
- "Multiple uses for byte %d of page %d", i, iPage); |
+ "Multiple uses for byte %u of page %d", x>>16, iPage); |
break; |
+ }else{ |
+ nFrag += (x>>16) - (prev&0xffff) - 1; |
+ prev = x; |
} |
} |
- if( cnt!=data[hdr+7] ){ |
+ nFrag += usableSize - (prev&0xffff) - 1; |
+ /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments |
+ ** is stored in the fifth field of the b-tree page header. |
+ ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
+ ** number of fragmented free bytes within the cell content area. |
+ */ |
+ if( heap[0]==0 && nFrag!=data[hdr+7] ){ |
checkAppendMsg(pCheck, |
"Fragmentation of %d bytes reported as %d on page %d", |
- cnt, data[hdr+7], iPage); |
+ nFrag, data[hdr+7], iPage); |
} |
} |
- sqlite3PageFree(hit); |
- releasePage(pPage); |
end_of_check: |
+ if( !doCoverageCheck ) pPage->isInit = savedIsInit; |
+ releasePage(pPage); |
pCheck->zPfx = saved_zPfx; |
pCheck->v1 = saved_v1; |
pCheck->v2 = saved_v2; |
@@ -8346,14 +9304,15 @@ char *sqlite3BtreeIntegrityCheck( |
int *pnErr /* Write number of errors seen to this variable */ |
){ |
Pgno i; |
- int nRef; |
IntegrityCk sCheck; |
BtShared *pBt = p->pBt; |
+ int savedDbFlags = pBt->db->flags; |
char zErr[100]; |
+ VVA_ONLY( int nRef ); |
sqlite3BtreeEnter(p); |
assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
- nRef = sqlite3PagerRefcount(pBt->pPager); |
+ assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 ); |
sCheck.pBt = pBt; |
sCheck.pPager = pBt->pPager; |
sCheck.nPage = btreePagecount(sCheck.pBt); |
@@ -8363,22 +9322,26 @@ char *sqlite3BtreeIntegrityCheck( |
sCheck.zPfx = 0; |
sCheck.v1 = 0; |
sCheck.v2 = 0; |
- *pnErr = 0; |
+ sCheck.aPgRef = 0; |
+ sCheck.heap = 0; |
+ sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
if( sCheck.nPage==0 ){ |
- sqlite3BtreeLeave(p); |
- return 0; |
+ goto integrity_ck_cleanup; |
} |
sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); |
if( !sCheck.aPgRef ){ |
- *pnErr = 1; |
- sqlite3BtreeLeave(p); |
- return 0; |
+ sCheck.mallocFailed = 1; |
+ goto integrity_ck_cleanup; |
+ } |
+ sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); |
+ if( sCheck.heap==0 ){ |
+ sCheck.mallocFailed = 1; |
+ goto integrity_ck_cleanup; |
} |
+ |
i = PENDING_BYTE_PAGE(pBt); |
if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); |
- sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
- sCheck.errMsg.useMalloc = 2; |
/* Check the integrity of the freelist |
*/ |
@@ -8389,17 +9352,19 @@ char *sqlite3BtreeIntegrityCheck( |
/* Check all the tables. |
*/ |
+ testcase( pBt->db->flags & SQLITE_CellSizeCk ); |
+ pBt->db->flags &= ~SQLITE_CellSizeCk; |
for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
+ i64 notUsed; |
if( aRoot[i]==0 ) continue; |
#ifndef SQLITE_OMIT_AUTOVACUUM |
if( pBt->autoVacuum && aRoot[i]>1 ){ |
checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); |
} |
#endif |
- sCheck.zPfx = "List of tree roots: "; |
- checkTreePage(&sCheck, aRoot[i], NULL, NULL); |
- sCheck.zPfx = 0; |
+ checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); |
} |
+ pBt->db->flags = savedDbFlags; |
/* Make sure every page in the file is referenced |
*/ |
@@ -8423,28 +9388,20 @@ char *sqlite3BtreeIntegrityCheck( |
#endif |
} |
- /* Make sure this analysis did not leave any unref() pages. |
- ** This is an internal consistency check; an integrity check |
- ** of the integrity check. |
- */ |
- if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
- checkAppendMsg(&sCheck, |
- "Outstanding page count goes from %d to %d during this analysis", |
- nRef, sqlite3PagerRefcount(pBt->pPager) |
- ); |
- } |
- |
/* Clean up and report errors. |
*/ |
- sqlite3BtreeLeave(p); |
+integrity_ck_cleanup: |
+ sqlite3PageFree(sCheck.heap); |
sqlite3_free(sCheck.aPgRef); |
if( sCheck.mallocFailed ){ |
sqlite3StrAccumReset(&sCheck.errMsg); |
- *pnErr = sCheck.nErr+1; |
- return 0; |
+ sCheck.nErr++; |
} |
*pnErr = sCheck.nErr; |
if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
+ /* Make sure this analysis did not leave any unref() pages. */ |
+ assert( nRef==sqlite3PagerRefcount(pBt->pPager) ); |
+ sqlite3BtreeLeave(p); |
return sqlite3StrAccumFinish(&sCheck.errMsg); |
} |
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
@@ -8655,6 +9612,7 @@ int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
*/ |
void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ |
pCur->curFlags |= BTCF_Incrblob; |
+ pCur->pBtree->hasIncrblobCur = 1; |
} |
#endif |
@@ -8695,12 +9653,11 @@ int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ |
} |
/* |
-** set the mask of hint flags for cursor pCsr. Currently the only valid |
-** values are 0 and BTREE_BULKLOAD. |
+** Return true if the cursor has a hint specified. This routine is |
+** only used from within assert() statements |
*/ |
-void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){ |
- assert( mask==BTREE_BULKLOAD || mask==0 ); |
- pCsr->hints = mask; |
+int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){ |
+ return (pCsr->hints & mask)!=0; |
} |
/* |
@@ -8709,3 +9666,8 @@ void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){ |
int sqlite3BtreeIsReadonly(Btree *p){ |
return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
} |
+ |
+/* |
+** Return the size of the header added to each page by this module. |
+*/ |
+int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } |