Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Make use of the extra information in the WAL header and frame header to enhance robustness. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
9580ecb7e3beb1949a71784a3dcd1823 |
User & Date: | drh 2010-05-20 21:21:10.000 |
Context
2010-05-20
| ||
23:51 | Add a new xShmBarrier method to the VFS - a shared-memory fence operation. Implement the same in both unix and win32. Use it to make the WAL subsystem more robust. (check-in: 1bd011c9fe user: drh tags: trunk) | |
21:21 | Make use of the extra information in the WAL header and frame header to enhance robustness. (check-in: 9580ecb7e3 user: drh tags: trunk) | |
16:45 | Convert the wal-header and frame-header to 24 bytes. Extra information in both headers is designed to enhance robustness after crashes, though the extra information is currently unused. This is a snapshot of a work in progress. (check-in: 669706431f user: drh tags: trunk) | |
Changes
Changes to src/wal.c.
︙ | ︙ | |||
11 12 13 14 15 16 17 | ************************************************************************* ** ** This file contains the implementation of a write-ahead log (WAL) used in ** "journal_mode=WAL" mode. ** ** WRITE-AHEAD LOG (WAL) FILE FORMAT ** | | | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ************************************************************************* ** ** This file contains the implementation of a write-ahead log (WAL) used in ** "journal_mode=WAL" mode. ** ** WRITE-AHEAD LOG (WAL) FILE FORMAT ** ** A WAL file consists of a header followed by zero or more "frames". ** Each frame records the revised content of a single page from the ** database file. All changes to the database are recorded by writing ** frames into the WAL. Transactions commit when a frame is written that ** contains a commit marker. A single WAL can and usually does record ** multiple transactions. Periodically, the content of the WAL is ** transferred back into the database file in an operation called a ** "checkpoint". |
︙ | ︙ | |||
34 35 36 37 38 39 40 | ** The WAL header is 24 bytes in size and consists of the following six ** big-endian 32-bit unsigned integer values: ** ** 0: Magic number. 0x377f0682 (big endian) ** 4: File format version. Currently 3007000 ** 8: Database page size. Example: 1024 ** 12: Checkpoint sequence number | | | | | > > > > > > > > > > > > > > > > > > > > > > | 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | ** The WAL header is 24 bytes in size and consists of the following six ** big-endian 32-bit unsigned integer values: ** ** 0: Magic number. 0x377f0682 (big endian) ** 4: File format version. Currently 3007000 ** 8: Database page size. Example: 1024 ** 12: Checkpoint sequence number ** 16: Salt-1, random integer incremented with each checkpoint ** 20: Salt-2, a different random integer changing with each ckpt ** ** Immediately following the wal-header are zero or more frames. Each ** frame consists of a 24-byte frame-header followed by a <page-size> bytes ** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned ** integer values, as follows: ** ** 0: Page number. ** 4: For commit records, the size of the database image in pages ** after the commit. For all other records, zero. ** 8: Salt-1 (copied from the header) ** 12: Salt-2 (copied from the header) ** 16: Checksum-1. ** 20: Checksum-2. ** ** A frame is considered valid if and only if the following conditions are ** true: ** ** (1) The salt-1 and salt-2 values in the frame-header match ** salt values in the wal-header ** ** (2) The checksum values in the final 8 bytes of the frame-header ** exactly match the checksum computed consecutively on ** (a) the first 16 bytes of the frame-header, and ** (b) the frame data. ** ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the ** WAL is transferred into the database, then the database is VFS.xSync-ed. ** The VFS.xSync operations server as write barriers - all writes launched ** before the xSync must complete before any write that launches after the ** xSync begins. ** ** After each checkpoint, the salt-1 value is incremented and the salt-2 ** value is randomized. This prevents old and new frames in the WAL from ** being considered valid at the same time and being checkpointing together ** following a crash. ** ** READER ALGORITHM ** ** To read a page from the database (call it page number P), a reader ** first checks the WAL to see if it contains page P. If so, then the ** last valid instance of page P that is or is followed by a commit frame ** become the value read. If the WAL contains no copies of page P that |
︙ | ︙ | |||
181 182 183 184 185 186 187 | /* Object declarations */ typedef struct WalIndexHdr WalIndexHdr; typedef struct WalIterator WalIterator; /* | < < | < < < < | | | | | | < < | | | | < | | 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | /* Object declarations */ typedef struct WalIndexHdr WalIndexHdr; typedef struct WalIterator WalIterator; /* ** The following object holds an exact copy of the wal-index header. */ struct WalIndexHdr { u32 iChange; /* Counter incremented each transaction */ u32 szPage; /* Database page size in bytes */ u32 mxFrame; /* Index of last valid frame in the WAL */ u32 nPage; /* Size of database in pages */ u32 aSalt[2]; /* Salt-1 and salt-2 values copied from WAL header */ u32 aCksum[2]; /* Checksum over all prior fields */ }; /* A block of WALINDEX_LOCK_RESERVED bytes beginning at ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems ** only support mandatory file-locks, we do not read or write data ** from the region of the file on which locks are applied. */ #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)) #define WALINDEX_LOCK_RESERVED 8 /* Size of header before each frame in wal */ #define WAL_FRAME_HDRSIZE 24 /* Size of write ahead log header */ #define WAL_HDRSIZE 24 |
︙ | ︙ | |||
241 242 243 244 245 246 247 | volatile u32 *pWiData; /* Pointer to wal-index content in memory */ u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */ u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */ u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ u8 isWindexOpen; /* True if ShmOpen() called on pDbFd */ WalIndexHdr hdr; /* Wal-index for current snapshot */ char *zWalName; /* Name of WAL file */ | > | < | 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | volatile u32 *pWiData; /* Pointer to wal-index content in memory */ u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */ u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */ u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ u8 isWindexOpen; /* True if ShmOpen() called on pDbFd */ WalIndexHdr hdr; /* Wal-index for current snapshot */ char *zWalName; /* Name of WAL file */ int szPage; /* Database page size */ u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ }; /* ** This structure is used to implement an iterator that loops through ** all frames in the WAL in database page order. Where two or more frames ** correspond to the same database page, the iterator visits only the |
︙ | ︙ | |||
274 275 276 277 278 279 280 | u8 *aIndex; /* i0, i1, i2... such that aPgno[iN] ascending */ u32 *aPgno; /* 256 page numbers. Pointer to Wal.pWiData */ } aSegment[1]; /* One for every 256 entries in the WAL */ }; /* | | > | > | > > | > > > > > | > > | > > > | | | 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | u8 *aIndex; /* i0, i1, i2... such that aPgno[iN] ascending */ u32 *aPgno; /* 256 page numbers. Pointer to Wal.pWiData */ } aSegment[1]; /* One for every 256 entries in the WAL */ }; /* ** Generate or extend an 8 byte checksum based on the data in ** array aByte[] and the initial values of aIn[0] and aIn[1] (or ** initial values of 0 and 0 if aIn==NULL). ** ** The checksum is written back into aOut[] before returning. ** ** nByte must be a positive multiple of 8. */ static void walChecksumBytes( u8 *a, /* Content to be checksummed */ int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */ const u32 *aIn, /* Initial checksum value input */ u32 *aOut /* OUT: Final checksum value output */ ){ u32 s1, s2; if( aIn ){ s1 = aIn[0]; s2 = aIn[1]; }else{ s1 = s2 = 0; } u8 *aEnd = (u8*)&a[nByte]; assert( nByte>=8 ); assert( (nByte&0x00000003)==0 ); do { s1 += (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3] + s2; s2 += (a[4]<<24) + (a[5]<<16) + (a[6]<<8) + a[7] + s1; a += 8; }while( a<aEnd ); aOut[0] = s1; aOut[1] = s2; } /* ** Attempt to change the lock status. ** ** When changing the lock status to SQLITE_SHM_READ, store the ** type of reader lock (either SQLITE_SHM_READ or SQLITE_SHM_READ_FULL) |
︙ | ︙ | |||
319 320 321 322 323 324 325 | pWal->lockState = SQLITE_SHM_READ; } } return rc; } /* | | > > | < < | < < | | | | | < < | > < | < | | > > | < < < > < > > > | < < | < | > > > | | > > > | 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | pWal->lockState = SQLITE_SHM_READ; } } return rc; } /* ** Write the header information in pWal->hdr into the wal-index. ** ** The checksum on pWal->hdr is updated before it is written. */ static void walIndexWriteHdr(Wal *pWal){ walChecksumBytes((u8*)&pWal->hdr, sizeof(pWal->hdr) - sizeof(pWal->hdr.aCksum), 0, pWal->hdr.aCksum); memcpy((void*)pWal->pWiData, &pWal->hdr, sizeof(pWal->hdr)); } /* ** This function encodes a single frame header and writes it to a buffer ** supplied by the caller. A frame-header is made up of a series of ** 4-byte big-endian integers, as follows: ** ** 0: Page number. ** 4: For commit records, the size of the database image in pages ** after the commit. For all other records, zero. ** 8: Salt-1 (copied from the wal-header) ** 12: Salt-2 (copied from the wal-header) ** 16: Checksum-1. ** 20: Checksum-2. */ static void walEncodeFrame( Wal *pWal, /* The write-ahead log */ u32 iPage, /* Database page number for frame */ u32 nTruncate, /* New db size (or 0 for non-commit frames) */ u8 *aData, /* Pointer to page data */ u8 *aFrame /* OUT: Write encoded frame here */ ){ u32 aCksum[2]; assert( WAL_FRAME_HDRSIZE==24 ); sqlite3Put4byte(&aFrame[0], iPage); sqlite3Put4byte(&aFrame[4], nTruncate); memcpy(&aFrame[8], pWal->hdr.aSalt, 8); walChecksumBytes(aFrame, 16, 0, aCksum); walChecksumBytes(aData, pWal->szPage, aCksum, aCksum); sqlite3Put4byte(&aFrame[16], aCksum[0]); sqlite3Put4byte(&aFrame[20], aCksum[1]); } /* ** Check to see if the frame with header in aFrame[] and content ** in aData[] is valid. If it is a valid frame, fill *piPage and ** *pnTruncate and return true. Return if the frame is not valid. */ static int walDecodeFrame( Wal *pWal, /* The write-ahead log */ u32 *piPage, /* OUT: Database page number for frame */ u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */ u8 *aData, /* Pointer to page data (for checksum) */ u8 *aFrame /* Frame data */ ){ u32 aCksum[2]; assert( WAL_FRAME_HDRSIZE==24 ); /* A frame is only valid if the salt values in the frame-header ** match the salt values in the wal-header. */ if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){ return 0; } /* A frame is only valid if a checksum of the first 16 bytes ** of the frame-header, and the frame-data matches ** the checksum in the last 8 bytes of the frame-header. */ walChecksumBytes(aFrame, 16, 0, aCksum); walChecksumBytes(aData, pWal->szPage, aCksum, aCksum); if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) ){ /* Checksum failed. */ return 0; } /* If we reach this point, the frame is valid. Return the page number ** and the new database size. */ *piPage = sqlite3Get4byte(&aFrame[0]); *pnTruncate = sqlite3Get4byte(&aFrame[4]); return 1; } /* ** Define the parameters of the hash tables in the wal-index file. There |
︙ | ︙ | |||
644 645 646 647 648 649 650 | u8 aBuf[WAL_HDRSIZE]; /* Buffer to load first frame header into */ 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 */ | < > | < | < < < | < < > | 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | u8 aBuf[WAL_HDRSIZE]; /* Buffer to load first frame header into */ 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 */ /* Read in the first frame header in the file (to determine the ** database page size). */ rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); if( rc!=SQLITE_OK ){ return rc; } /* If the database page size is not a power of two, or is greater than ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid data. */ szPage = sqlite3Get4byte(&aBuf[8]); if( szPage&(szPage-1) || szPage>SQLITE_MAX_PAGE_SIZE || szPage<512 ){ goto finished; } pWal->szPage = szPage; pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); /* Malloc a buffer to read frames into. */ szFrame = szPage + WAL_FRAME_HDRSIZE; aFrame = (u8 *)sqlite3_malloc(szFrame); if( !aFrame ){ return SQLITE_NOMEM; } aData = &aFrame[WAL_FRAME_HDRSIZE]; /* 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 */ int isValid; /* True if this frame is valid */ /* Read and decode the next log frame. */ rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); if( rc!=SQLITE_OK ) break; isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); if( !isValid ) break; rc = walIndexAppend(pWal, ++iFrame, pgno); if( rc!=SQLITE_OK ) break; /* If nTruncate is non-zero, this is a commit record. */ if( nTruncate ){ hdr.mxFrame = iFrame; hdr.nPage = nTruncate; hdr.szPage = szPage; } } sqlite3_free(aFrame); }else{ memset(&hdr, 0, sizeof(hdr)); } finished: if( rc==SQLITE_OK && hdr.mxFrame==0 ){ rc = walIndexRemap(pWal, WALINDEX_MMAP_INCREMENT); } if( rc==SQLITE_OK ){ memcpy(&pWal->hdr, &hdr, sizeof(hdr)); walIndexWriteHdr(pWal); } return rc; } /* ** Close an open wal-index. */ |
︙ | ︙ | |||
770 771 772 773 774 775 776 777 778 779 780 781 782 783 | if( !pRet ){ return SQLITE_NOMEM; } pRet->pVfs = pVfs; pRet->pWalFd = (sqlite3_file *)&pRet[1]; pRet->pDbFd = pDbFd; pRet->zWalName = zWal = pVfs->szOsFile + (char*)pRet->pWalFd; sqlite3_snprintf(nWal, zWal, "%s-wal", zDbName); rc = sqlite3OsShmOpen(pDbFd); /* Open file handle on the write-ahead log file. */ if( rc==SQLITE_OK ){ pRet->isWindexOpen = 1; | > | 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | if( !pRet ){ return SQLITE_NOMEM; } pRet->pVfs = pVfs; pRet->pWalFd = (sqlite3_file *)&pRet[1]; pRet->pDbFd = pDbFd; sqlite3_randomness(8, &pRet->hdr.aSalt); pRet->zWalName = zWal = pVfs->szOsFile + (char*)pRet->pWalFd; sqlite3_snprintf(nWal, zWal, "%s-wal", zDbName); rc = sqlite3OsShmOpen(pDbFd); /* Open file handle on the write-ahead log file. */ if( rc==SQLITE_OK ){ pRet->isWindexOpen = 1; |
︙ | ︙ | |||
1023 1024 1025 1026 1027 1028 1029 | /* Sync the database file. If successful, update the wal-index. */ if( sync_flags ){ rc = sqlite3OsSync(pWal->pDbFd, sync_flags); if( rc!=SQLITE_OK ) goto out; } pWal->hdr.mxFrame = 0; | < < < | < < < < < < < < < < < < < < < | < | | | 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | /* Sync the database file. If successful, update the wal-index. */ if( sync_flags ){ rc = sqlite3OsSync(pWal->pDbFd, sync_flags); if( rc!=SQLITE_OK ) goto out; } pWal->hdr.mxFrame = 0; pWal->nCkpt++; sqlite3Put4byte((u8*)pWal->hdr.aSalt, 1 + sqlite3Get4byte((u8*)pWal->hdr.aSalt)); sqlite3_randomness(4, &pWal->hdr.aSalt[1]); walIndexWriteHdr(pWal); out: walIteratorFree(pIter); return rc; } /* |
︙ | ︙ | |||
1110 1111 1112 1113 1114 1115 1116 | ** pWal->hdr, then pWal->hdr is updated to the content of the new header ** and *pChanged is set to 1. ** ** If the checksum cannot be verified return non-zero. If the header ** is read successfully and the checksum verified, return zero. */ int walIndexTryHdr(Wal *pWal, int *pChanged){ | < < | | | < < < | | < < | | > | 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 | ** pWal->hdr, then pWal->hdr is updated to the content of the new header ** and *pChanged is set to 1. ** ** If the checksum cannot be verified return non-zero. If the header ** is read successfully and the checksum verified, return zero. */ int walIndexTryHdr(Wal *pWal, int *pChanged){ u32 aCksum[2]; WalIndexHdr hdr; assert( pWal->pWiData ); if( pWal->szWIndex==0 ){ /* The wal-index is of size 0 bytes. This is handled in the same way ** as an invalid header. The caller will run recovery to construct ** a valid wal-index file before accessing the database. */ return 1; } /* Read the header. The caller may or may not have an exclusive ** (WRITE, PENDING, CHECKPOINT or RECOVER) lock on the wal-index ** file, meaning it is possible that an inconsistent snapshot is read ** from the file. If this happens, return non-zero. */ memcpy(&hdr, (void*)pWal->pWiData, sizeof(hdr)); walChecksumBytes((u8*)&hdr, sizeof(hdr)-sizeof(hdr.aCksum), 0, aCksum); if( aCksum[0]!=hdr.aCksum[0] || aCksum[1]!=hdr.aCksum[1] ){ return 1; } if( memcmp(&pWal->hdr, &hdr, sizeof(WalIndexHdr)) ){ *pChanged = 1; memcpy(&pWal->hdr, &hdr, sizeof(WalIndexHdr)); pWal->szPage = pWal->hdr.szPage; } /* The header was successfully read. Return zero. */ return 0; } /* |
︙ | ︙ | |||
1483 1484 1485 1486 1487 1488 1489 | } /* Move the write position of the WAL back to iFrame. Called in ** response to a ROLLBACK TO command. */ int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){ int rc = SQLITE_OK; | < < < < < < < < < > | < < | < < | | | 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 | } /* Move the write position of the WAL back to iFrame. Called in ** response to a ROLLBACK TO command. */ int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){ int rc = SQLITE_OK; assert( pWal->lockState==SQLITE_SHM_WRITE ); pWal->hdr.mxFrame = iFrame; return rc; } /* ** Write a set of frames to the log. The caller must hold the write-lock ** on the log file (obtained using sqlite3WalWriteLock()). */ int sqlite3WalFrames( Wal *pWal, /* Wal handle to write to */ int szPage, /* Database page-size in bytes */ PgHdr *pList, /* List of dirty pages to write */ Pgno nTruncate, /* Database size after this commit */ int isCommit, /* True if this is a commit */ int sync_flags /* Flags to pass to OsSync() (or 0) */ ){ int rc; /* Used to catch return codes */ u32 iFrame; /* Next frame address */ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ PgHdr *p; /* Iterator to run through pList with. */ PgHdr *pLast = 0; /* Last frame in list */ int nLast = 0; /* Number of extra copies of last page */ assert( pList ); assert( pWal->lockState==SQLITE_SHM_WRITE ); assert( pWal->pWiData==0 ); /* If this is the first frame written into the log, write the WAL ** header to the start of the WAL file. See comments at the top of ** this source file for a description of the WAL header format. */ iFrame = pWal->hdr.mxFrame; if( iFrame==0 ){ u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */ sqlite3Put4byte(&aWalHdr[0], 0x377f0682); sqlite3Put4byte(&aWalHdr[4], 3007000); sqlite3Put4byte(&aWalHdr[8], szPage); pWal->szPage = szPage; sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt); memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); if( rc!=SQLITE_OK ){ return rc; } } assert( pWal->szPage==szPage ); /* Write the log file. */ for(p=pList; p; p=p->pDirty){ u32 nDbsize; /* Db-size field for frame header */ i64 iOffset; /* Write offset in log file */ iOffset = walFrameOffset(++iFrame, szPage); /* Populate and write the frame header */ nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0; walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame); rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); if( rc!=SQLITE_OK ){ return rc; } /* Write the page data */ rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame)); |
︙ | ︙ | |||
1577 1578 1579 1580 1581 1582 1583 | i64 iOffset = walFrameOffset(iFrame+1, szPage); assert( isCommit ); assert( iSegment>0 ); iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); while( iOffset<iSegment ){ | | < | 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 | i64 iOffset = walFrameOffset(iFrame+1, szPage); assert( isCommit ); assert( iSegment>0 ); iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); while( iOffset<iSegment ){ walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame); rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); if( rc!=SQLITE_OK ){ return rc; } iOffset += WAL_FRAME_HDRSIZE; rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset); |
︙ | ︙ | |||
1621 1622 1623 1624 1625 1626 1627 | /* Update the private copy of the header. */ pWal->hdr.szPage = szPage; pWal->hdr.mxFrame = iFrame; if( isCommit ){ pWal->hdr.iChange++; pWal->hdr.nPage = nTruncate; } | < < < | | 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 | /* Update the private copy of the header. */ pWal->hdr.szPage = szPage; pWal->hdr.mxFrame = iFrame; if( isCommit ){ pWal->hdr.iChange++; pWal->hdr.nPage = nTruncate; } /* If this is a commit, update the wal-index header too. */ if( isCommit ){ walIndexWriteHdr(pWal); pWal->iCallback = iFrame; } } walIndexUnmap(pWal); return rc; } |
︙ | ︙ |
Changes to test/wal.test.
︙ | ︙ | |||
552 553 554 555 556 557 558 | execsql { BEGIN ; SELECT * FROM t1 } } {1 2 3 4 5 6} do_test wal-10.$tn.9 { sql2 COMMIT catchsql { INSERT INTO t1 VALUES(9, 10) } } {1 {database is locked}} do_test wal-10.$tn.10 { | | > > > | 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | execsql { BEGIN ; SELECT * FROM t1 } } {1 2 3 4 5 6} do_test wal-10.$tn.9 { sql2 COMMIT catchsql { INSERT INTO t1 VALUES(9, 10) } } {1 {database is locked}} do_test wal-10.$tn.10 { execsql { COMMIT } execsql { BEGIN } execsql { INSERT INTO t1 VALUES(9, 10) } execsql { COMMIT } execsql { SELECT * FROM t1 } } {1 2 3 4 5 6 7 8 9 10} # Open a read transaction with [db2]. Check that this prevents [db] from # checkpointing the database. But not from writing to it. # do_test wal-10.$tn.11 { |
︙ | ︙ | |||
1332 1333 1334 1335 1336 1337 1338 | # The page-size in the log file header is set to $pgsz. If the # WAL code considers $pgsz to be a valid SQLite database file page-size, # the database will be corrupt (because the garbage frame contents # will be treated as valid content). If $pgsz is invalid (too small # or too large), the db will not be corrupt as the log file will # be ignored. # | < < | < | > > | | 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 | # The page-size in the log file header is set to $pgsz. If the # WAL code considers $pgsz to be a valid SQLite database file page-size, # the database will be corrupt (because the garbage frame contents # will be treated as valid content). If $pgsz is invalid (too small # or too large), the db will not be corrupt as the log file will # be ignored. # set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23] set framebody [randomblob $pgsz] set framehdr [binary format IIII $pg 5 22 23] set c1 0 set c2 0 logcksum c1 c2 $framehdr logcksum c1 c2 $framebody set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2] set fd [open test.db-wal w] fconfigure $fd -encoding binary -translation binary puts -nonewline $fd $walhdr puts -nonewline $fd $framehdr puts -nonewline $fd $framebody close $fd |
︙ | ︙ |
Changes to test/wal2.test.
︙ | ︙ | |||
111 112 113 114 115 116 117 118 119 120 121 122 123 124 | } } if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] } return SQLITE_OK } execsql { SELECT count(a), sum(a) FROM t1 } db2 } $res do_test wal2-1.$tn.2 { set ::locks } $wal_locks } | > | 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | } } if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] } return SQLITE_OK } breakpoint execsql { SELECT count(a), sum(a) FROM t1 } db2 } $res do_test wal2-1.$tn.2 { set ::locks } $wal_locks } |
︙ | ︙ | |||
174 175 176 177 178 179 180 | 4 7 {6 21} {7 28} 2 5 8 {7 28} {8 36} 3 6 9 {8 36} {9 45} 4 7 10 {9 45} {10 55} 5 8 11 {10 55} {11 66} 6 9 12 {11 66} {12 78} 7 } { | | | 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 4 7 {6 21} {7 28} 2 5 8 {7 28} {8 36} 3 6 9 {8 36} {9 45} 4 7 10 {9 45} {10 55} 5 8 11 {10 55} {11 66} 6 9 12 {11 66} {12 78} 7 } { do_test wal2-2.$tn.1 { set oldhdr [set_tvfs_hdr $::shm_file] execsql { INSERT INTO t1 VALUES($iInsert) } execsql { SELECT count(a), sum(a) FROM t1 } } $res1 do_test wal2-2.$tn.2 { set ::locks [list] |
︙ | ︙ | |||
599 600 601 602 603 604 605 | do_test wal2-6.5.3 { execsql { PRAGMA wal_checkpoint } } {} db close finish_test | < | 600 601 602 603 604 605 606 | do_test wal2-6.5.3 { execsql { PRAGMA wal_checkpoint } } {} db close finish_test |