1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-10 01:02:56 +03:00

Merge latest trunk changes into this branch.

FossilOrigin-Name: 0c0d0a77bc8fa68ee584ca6d7a0408dcd00734a9b6b69d127ee8477c230a749c
This commit is contained in:
dan
2020-07-31 16:01:33 +00:00
179 changed files with 7513 additions and 2017 deletions

124
src/wal.c
View File

@@ -769,7 +769,7 @@ typedef u16 ht_slot;
** This functionality is used by the checkpoint code (see walCheckpoint()).
*/
struct WalIterator {
int iPrior; /* Last result returned from the iterator */
u32 iPrior; /* Last result returned from the iterator */
int nSegment; /* Number of entries in aSegment[] */
struct WalSegment {
int iNext; /* Next slot in aIndex[] not yet returned */
@@ -851,7 +851,9 @@ static SQLITE_NOINLINE int walIndexPageRealloc(
);
assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
if( (rc&0xff)==SQLITE_READONLY ){
if( rc==SQLITE_OK ){
if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM;
}else if( (rc&0xff)==SQLITE_READONLY ){
pWal->readOnly |= WAL_SHM_RDONLY;
if( rc==SQLITE_READONLY ){
rc = SQLITE_OK;
@@ -1258,6 +1260,7 @@ static int walFramePage(u32 iFrame){
&& (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
&& (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
);
assert( iHash>=0 );
return iHash;
}
@@ -1425,7 +1428,7 @@ static int walIndexAppend(Wal *pWal, int iWal, u32 iFrame, u32 iPage){
if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
}
sLoc.aPgno[idx] = iPage;
sLoc.aHash[iKey] = (ht_slot)idx;
AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx);
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
/* Verify that the number of entries in the hash table exactly equals
@@ -1479,15 +1482,17 @@ static int walIndexRecoverOne(Wal *pWal, int iWal, u32 *pnCkpt, int *pbZero){
if( rc==SQLITE_OK ){
if( nSize>WAL_HDRSIZE ){
u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
u32 *aPrivate = 0; /* Heap copy of *-shm pg being populated */
u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
int szFrame; /* Number of bytes in buffer aFrame[] */
u8 *aData; /* Pointer to data part of aFrame buffer */
int iFrame; /* Index of last frame read */
i64 iOffset; /* Next offset to read from log file */
int szPage; /* Page size according to the log */
u32 magic; /* Magic value read from WAL header */
u32 version; /* Magic value read from WAL header */
int isValid; /* True if this frame is valid */
int iPg; /* Current 32KB wal-index page */
int iLastFrame; /* Last frame in wal, based on size alone */
int iLastPg; /* Last shm page used by this wal */
/* Read in the WAL header. */
rc = sqlite3OsRead(pWalFd, aBuf, WAL_HDRSIZE, 0);
@@ -1535,37 +1540,71 @@ static int walIndexRecoverOne(Wal *pWal, int iWal, u32 *pnCkpt, int *pbZero){
/* Malloc a buffer to read frames into. */
szFrame = szPage + WAL_FRAME_HDRSIZE;
aFrame = (u8 *)sqlite3_malloc64(szFrame);
aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ);
if( !aFrame ){
return SQLITE_NOMEM_BKPT;
}
aData = &aFrame[WAL_FRAME_HDRSIZE];
aPrivate = (u32*)&aData[szPage];
/* Read all frames from the log file. */
iFrame = 0;
for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
u32 pgno; /* Database page number for frame */
u32 nTruncate; /* dbsize field from frame header */
/* Read and decode the next log frame. */
iFrame++;
rc = sqlite3OsRead(pWalFd, aFrame, szFrame, iOffset);
if( rc!=SQLITE_OK ) break;
isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
if( !isValid ) break;
rc = walIndexAppend(pWal, iWal, iFrame, pgno);
if( rc!=SQLITE_OK ) break;
/* If nTruncate is non-zero, this is a commit record. */
if( nTruncate ){
pWal->hdr.mxFrame = iFrame;
pWal->hdr.nPage = nTruncate;
pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
testcase( szPage<=32768 );
testcase( szPage>=65536 );
aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
if( version==WAL_VERSION2 ){
iLastPg = walFramePage2(iWal, iLastFrame);
}else{
iLastPg = walFramePage(iLastFrame);
}
for(iPg=iWal; iPg<=iLastPg; iPg+=(version==WAL_VERSION2 ? 2 : 1)){
u32 *aShare;
int iFrame; /* Index of last frame read */
int iLast;
int iFirst;
int nHdr, nHdr32;
rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare);
if( rc ) break;
pWal->apWiData[iPg] = aPrivate;
if( iWal ){
assert( version==WAL_VERSION2 );
iFirst = 1 + (iPg/2)*HASHTABLE_NPAGE;
iLast = iFirst + HASHTABLE_NPAGE - 1;
}else{
int i2 = (version==WAL_VERSION2) ? (iPg/2) : iPg;
iLast = HASHTABLE_NPAGE_ONE+i2*HASHTABLE_NPAGE;
iFirst = 1 + (i2==0?0:HASHTABLE_NPAGE_ONE+(i2-1)*HASHTABLE_NPAGE);
}
iLast = MIN(iLast, iLastFrame);
for(iFrame=iFirst; iFrame<=iLast; iFrame++){
i64 iOffset = walFrameOffset(iFrame, szPage);
u32 pgno; /* Database page number for frame */
u32 nTruncate; /* dbsize field from frame header */
/* Read and decode the next log frame. */
rc = sqlite3OsRead(pWalFd, aFrame, szFrame, iOffset);
if( rc!=SQLITE_OK ) break;
isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
if( !isValid ) break;
rc = walIndexAppend(pWal, iWal, iFrame, pgno);
if( NEVER(rc!=SQLITE_OK) ) break;
/* If nTruncate is non-zero, this is a commit record. */
if( nTruncate ){
pWal->hdr.mxFrame = iFrame;
pWal->hdr.nPage = nTruncate;
pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
testcase( szPage<=32768 );
testcase( szPage>=65536 );
aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
}
}
pWal->apWiData[iPg] = aShare;
nHdr = (iPg==0 ? WALINDEX_HDR_SIZE : 0);
nHdr32 = nHdr / sizeof(u32);
memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
if( iFrame<=iLast ) break;
}
sqlite3_free(aFrame);
@@ -1634,12 +1673,6 @@ static int walIndexRecover(Wal *pWal){
assert( pWal->writeLock );
iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
if( rc==SQLITE_OK ){
rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
if( rc!=SQLITE_OK ){
walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
}
}
if( rc ){
return rc;
}
@@ -1733,16 +1766,28 @@ static int walIndexRecover(Wal *pWal){
walIndexWriteHdr(pWal);
/* Reset the checkpoint-header. This is safe because this thread is
** currently holding locks that exclude all other readers, writers and
** checkpointers. */
** currently holding locks that exclude all other writers and
** checkpointers. Then set the values of read-mark slots 1 through N.
*/
pInfo = walCkptInfo(pWal);
memset((void*)pInfo, 0, sizeof(WalCkptInfo));
if( 0==isWalMode2(pWal) ){
int i;
pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
pInfo->aReadMark[0] = 0;
for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
for(i=1; i<WAL_NREADER; i++){
rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
if( rc==SQLITE_OK ){
if( i==1 && pWal->hdr.mxFrame ){
pInfo->aReadMark[i] = pWal->hdr.mxFrame;
}else{
pInfo->aReadMark[i] = READMARK_NOT_USED;
}
walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
}else if( rc!=SQLITE_BUSY ){
break;
}
}
}
/* If more than one frame was recovered from the log file, report an
@@ -1767,7 +1812,6 @@ static int walIndexRecover(Wal *pWal){
WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
return rc;
}