/library/Library/WP7/SQLiteDriver/sqlite/wal_c.cs
C# | 2751 lines | 1384 code | 225 blank | 1142 comment | 244 complexity | c71f94acbce0b78bc9e2a56f8e0c2e0a MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- using System;
- using System.Diagnostics;
- using System.Text;
- using Bitmask = System.UInt64;
- using u32 = System.UInt32;
- namespace Community.CsharpSqlite
- {
- public partial class Sqlite3
- {
- /*
- ** 2010 February 1
- **
- ** The author disclaims copyright to this source code. In place of
- ** a legal notice, here is a blessing:
- **
- ** May you do good and not evil.
- ** May you find forgiveness for yourself and forgive others.
- ** May you share freely, never taking more than you give.
- **
- *************************************************************************
- **
- ** 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".
- **
- ** A single WAL file can be used multiple times. In other words, the
- ** WAL can fill up with frames and then be checkpointed and then new
- ** frames can overwrite the old ones. A WAL always grows from beginning
- ** toward the end. Checksums and counters attached to each frame are
- ** used to determine which frames within the WAL are valid and which
- ** are leftovers from prior checkpoints.
- **
- ** The WAL header is 32 bytes in size and consists of the following eight
- ** big-endian 32-bit unsigned integer values:
- **
- ** 0: Magic number. 0x377f0682 or 0x377f0683
- ** 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
- ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
- ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
- **
- ** 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 six 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 the
- ** WAL header and the first 8 bytes and the content of all frames
- ** up to and including the current frame.
- **
- ** The checksum is computed using 32-bit big-endian integers if the
- ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
- ** is computed using little-endian if the magic number is 0x377f0682.
- ** The checksum values are always stored in the frame header in a
- ** big-endian format regardless of which byte order is used to compute
- ** the checksum. The checksum is computed by interpreting the input as
- ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
- ** algorithm used for the checksum is as follows:
- **
- ** for i from 0 to n-1 step 2:
- ** s0 += x[i] + s1;
- ** s1 += x[i+1] + s0;
- ** endfor
- **
- ** Note that s0 and s1 are both weighted checksums using fibonacci weights
- ** in reverse order (the largest fibonacci weight occurs on the first element
- ** of the sequence being summed.) The s1 value spans all 32-bit
- ** terms of the sequence whereas s0 omits the final term.
- **
- ** 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 serve 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 a followed by a commit frame
- ** or is a commit frame itself becomes the value read. If the WAL
- ** contains no copies of page P that are valid and which are a commit
- ** frame or are followed by a commit frame, then page P is read from
- ** the database file.
- **
- ** To start a read transaction, the reader records the index of the last
- ** valid frame in the WAL. The reader uses this recorded "mxFrame" value
- ** for all subsequent read operations. New transactions can be appended
- ** to the WAL, but as long as the reader uses its original mxFrame value
- ** and ignores the newly appended content, it will see a consistent snapshot
- ** of the database from a single point in time. This technique allows
- ** multiple concurrent readers to view different versions of the database
- ** content simultaneously.
- **
- ** The reader algorithm in the previous paragraphs works correctly, but
- ** because frames for page P can appear anywhere within the WAL, the
- ** reader has to scan the entire WAL looking for page P frames. If the
- ** WAL is large (multiple megabytes is typical) that scan can be slow,
- ** and read performance suffers. To overcome this problem, a separate
- ** data structure called the wal-index is maintained to expedite the
- ** search for frames of a particular page.
- **
- ** WAL-INDEX FORMAT
- **
- ** Conceptually, the wal-index is shared memory, though VFS implementations
- ** might choose to implement the wal-index using a mmapped file. Because
- ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
- ** on a network filesystem. All users of the database must be able to
- ** share memory.
- **
- ** The wal-index is transient. After a crash, the wal-index can (and should
- ** be) reconstructed from the original WAL file. In fact, the VFS is required
- ** to either truncate or zero the header of the wal-index when the last
- ** connection to it closes. Because the wal-index is transient, it can
- ** use an architecture-specific format; it does not have to be cross-platform.
- ** Hence, unlike the database and WAL file formats which store all values
- ** as big endian, the wal-index can store multi-byte values in the native
- ** byte order of the host computer.
- **
- ** The purpose of the wal-index is to answer this question quickly: Given
- ** a page number P, return the index of the last frame for page P in the WAL,
- ** or return NULL if there are no frames for page P in the WAL.
- **
- ** The wal-index consists of a header region, followed by an one or
- ** more index blocks.
- **
- ** The wal-index header contains the total number of frames within the WAL
- ** in the the mxFrame field.
- **
- ** Each index block except for the first contains information on
- ** HASHTABLE_NPAGE frames. The first index block contains information on
- ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
- ** HASHTABLE_NPAGE are selected so that together the wal-index header and
- ** first index block are the same size as all other index blocks in the
- ** wal-index.
- **
- ** Each index block contains two sections, a page-mapping that contains the
- ** database page number associated with each wal frame, and a hash-table
- ** that allows readers to query an index block for a specific page number.
- ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
- ** for the first index block) 32-bit page numbers. The first entry in the
- ** first index-block contains the database page number corresponding to the
- ** first frame in the WAL file. The first entry in the second index block
- ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
- ** the log, and so on.
- **
- ** The last index block in a wal-index usually contains less than the full
- ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
- ** depending on the contents of the WAL file. This does not change the
- ** allocated size of the page-mapping array - the page-mapping array merely
- ** contains unused entries.
- **
- ** Even without using the hash table, the last frame for page P
- ** can be found by scanning the page-mapping sections of each index block
- ** starting with the last index block and moving toward the first, and
- ** within each index block, starting at the end and moving toward the
- ** beginning. The first entry that equals P corresponds to the frame
- ** holding the content for that page.
- **
- ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
- ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
- ** hash table for each page number in the mapping section, so the hash
- ** table is never more than half full. The expected number of collisions
- ** prior to finding a match is 1. Each entry of the hash table is an
- ** 1-based index of an entry in the mapping section of the same
- ** index block. Let K be the 1-based index of the largest entry in
- ** the mapping section. (For index blocks other than the last, K will
- ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
- ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
- ** contain a value of 0.
- **
- ** To look for page P in the hash table, first compute a hash iKey on
- ** P as follows:
- **
- ** iKey = (P * 383) % HASHTABLE_NSLOT
- **
- ** Then start scanning entries of the hash table, starting with iKey
- ** (wrapping around to the beginning when the end of the hash table is
- ** reached) until an unused hash slot is found. Let the first unused slot
- ** be at index iUnused. (iUnused might be less than iKey if there was
- ** wrap-around.) Because the hash table is never more than half full,
- ** the search is guaranteed to eventually hit an unused entry. Let
- ** iMax be the value between iKey and iUnused, closest to iUnused,
- ** where aHash[iMax]==P. If there is no iMax entry (if there exists
- ** no hash slot such that aHash[i]==p) then page P is not in the
- ** current index block. Otherwise the iMax-th mapping entry of the
- ** current index block corresponds to the last entry that references
- ** page P.
- **
- ** A hash search begins with the last index block and moves toward the
- ** first index block, looking for entries corresponding to page P. On
- ** average, only two or three slots in each index block need to be
- ** examined in order to either find the last entry for page P, or to
- ** establish that no such entry exists in the block. Each index block
- ** holds over 4000 entries. So two or three index blocks are sufficient
- ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
- ** comparisons (on average) suffice to either locate a frame in the
- ** WAL or to establish that the frame does not exist in the WAL. This
- ** is much faster than scanning the entire 10MB WAL.
- **
- ** Note that entries are added in order of increasing K. Hence, one
- ** reader might be using some value K0 and a second reader that started
- ** at a later time (after additional transactions were added to the WAL
- ** and to the wal-index) might be using a different value K1, where K1>K0.
- ** Both readers can use the same hash table and mapping section to get
- ** the correct result. There may be entries in the hash table with
- ** K>K0 but to the first reader, those entries will appear to be unused
- ** slots in the hash table and so the first reader will get an answer as
- ** if no values greater than K0 had ever been inserted into the hash table
- ** in the first place - which is what reader one wants. Meanwhile, the
- ** second reader using K1 will see additional values that were inserted
- ** later, which is exactly what reader two wants.
- **
- ** When a rollback occurs, the value of K is decreased. Hash table entries
- ** that correspond to frames greater than the new K value are removed
- ** from the hash table at this point.
- *************************************************************************
- ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
- ** C#-SQLite is an independent reimplementation of the SQLite software library
- **
- ** SQLITE_SOURCE_ID: 2010-12-07 20:14:09 a586a4deeb25330037a49df295b36aaf624d0f45
- **
- *************************************************************************
- */
- #if !SQLITE_OMIT_WAL
- //#include "wal.h"
- /*
- ** Trace output macros
- */
- #if (SQLITE_TEST) && (SQLITE_DEBUG)
- int sqlite3WalTrace = 0;
- //# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
- static void WALTRACE(params object[] X)
- {
- if(sqlite3WalTrace) sqlite3DebugPrintf(X);
- }
- #else
- //# define WALTRACE(X)
- static void WALTRACE(params object[] X) {}
- #endif
- /*
- ** The maximum (and only) versions of the wal and wal-index formats
- ** that may be interpreted by this version of SQLite.
- **
- ** If a client begins recovering a WAL file and finds that (a) the checksum
- ** values in the wal-header are correct and (b) the version field is not
- ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
- **
- ** Similarly, if a client successfully reads a wal-index header (i.e. the
- ** checksum test is successful) and finds that the version field is not
- ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
- ** returns SQLITE_CANTOPEN.
- */
- //#define WAL_MAX_VERSION 3007000
- //#define WALINDEX_MAX_VERSION 3007000
- const int WAL_MAX_VERSION = 3007000;
- const int WALINDEX_MAX_VERSION = 3007000;
- /*
- ** Indices of various locking bytes. WAL_NREADER is the number
- ** of available reader locks and should be at least 3.
- */
- //#define WAL_WRITE_LOCK 0
- //#define WAL_ALL_BUT_WRITE 1
- //#define WAL_CKPT_LOCK 1
- //#define WAL_RECOVER_LOCK 2
- //#define WAL_READ_LOCK(I) (3+(I))
- //#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
- const int WAL_WRITE_LOCK = 0;
- const int WAL_ALL_BUT_WRITE = 1;
- const int WAL_CKPT_LOCK = 1;
- const int WAL_RECOVER_LOCK = 2;
- const int WAL_READ_LOCK(I) = (3+(I));
- const int WAL_NREADER = (SQLITE_SHM_NLOCK-3);
- /* Object declarations */
- typedef struct WalIndexHdr WalIndexHdr;
- typedef struct WalIterator WalIterator;
- typedef struct WalCkptInfo WalCkptInfo;
- /*
- ** The following object holds a copy of the wal-index header content.
- **
- ** The actual header in the wal-index consists of two copies of this
- ** object.
- */
- struct WalIndexHdr {
- u32 iVersion; /* Wal-index version */
- u32 unused; /* Unused (padding) field */
- u32 iChange; /* Counter incremented each transaction */
- u8 isInit; /* 1 when initialized */
- u8 bigEndCksum; /* True if checksums in WAL are big-endian */
- u16 szPage; /* Database page size in bytes */
- u32 mxFrame; /* Index of last valid frame in the WAL */
- u32 nPage; /* Size of database in pages */
- u32 aFrameCksum[2]; /* Checksum of last frame in log */
- u32 aSalt[2]; /* Two salt values copied from WAL header */
- u32 aCksum[2]; /* Checksum over all prior fields */
- };
- /*
- ** A copy of the following object occurs in the wal-index immediately
- ** following the second copy of the WalIndexHdr. This object stores
- ** information used by checkpoint.
- **
- ** nBackfill is the number of frames in the WAL that have been written
- ** back into the database. (We call the act of moving content from WAL to
- ** database "backfilling".) The nBackfill number is never greater than
- ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
- ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
- ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
- ** mxFrame back to zero when the WAL is reset.
- **
- ** There is one entry in aReadMark[] for each reader lock. If a reader
- ** holds read-lock K, then the value in aReadMark[K] is no greater than
- ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
- ** for any aReadMark[] means that entry is unused. aReadMark[0] is
- ** a special case; its value is never used and it exists as a place-holder
- ** to avoid having to offset aReadMark[] indexs by one. Readers holding
- ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
- ** directly from the database.
- **
- ** The value of aReadMark[K] may only be changed by a thread that
- ** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
- ** aReadMark[K] cannot changed while there is a reader is using that mark
- ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
- **
- ** The checkpointer may only transfer frames from WAL to database where
- ** the frame numbers are less than or equal to every aReadMark[] that is
- ** in use (that is, every aReadMark[j] for which there is a corresponding
- ** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
- ** largest value and will increase an unused aReadMark[] to mxFrame if there
- ** is not already an aReadMark[] equal to mxFrame. The exception to the
- ** previous sentence is when nBackfill equals mxFrame (meaning that everything
- ** in the WAL has been backfilled into the database) then new readers
- ** will choose aReadMark[0] which has value 0 and hence such reader will
- ** get all their all content directly from the database file and ignore
- ** the WAL.
- **
- ** Writers normally append new frames to the end of the WAL. However,
- ** if nBackfill equals mxFrame (meaning that all WAL content has been
- ** written back into the database) and if no readers are using the WAL
- ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
- ** the writer will first "reset" the WAL back to the beginning and start
- ** writing new content beginning at frame 1.
- **
- ** We assume that 32-bit loads are atomic and so no locks are needed in
- ** order to read from any aReadMark[] entries.
- */
- struct WalCkptInfo {
- u32 nBackfill; /* Number of WAL frames backfilled into DB */
- u32 aReadMark[WAL_NREADER]; /* Reader marks */
- };
- //#define READMARK_NOT_USED 0xffffffff
- const int READMARK_NOT_USED = 0xffffffff;
- /* 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)*2 + sizeof(WalCkptInfo))
- //#define WALINDEX_LOCK_RESERVED 16
- //#define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
- const int WALINDEX_LOCK_OFFSET = (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo));
- const int WALINDEX_LOCK_RESERVED= 16;
- const int WALINDEX_HDR_SIZE = (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED);
- /* Size of header before each frame in wal */
- //#define WAL_FRAME_HDRSIZE 24
- const int WAL_FRAME_HDRSIZE =24;
- /* Size of write ahead log header, including checksum. */
- /* #define WAL_HDRSIZE 24 */
- //#define WAL_HDRSIZE 32
- const int WAL_HDRSIZE =32;
- /* WAL magic value. Either this value, or the same value with the least
- ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
- ** big-endian format in the first 4 bytes of a WAL file.
- **
- ** If the LSB is set, then the checksums for each frame within the WAL
- ** file are calculated by treating all data as an array of 32-bit
- ** big-endian words. Otherwise, they are calculated by interpreting
- ** all data as 32-bit little-endian words.
- */
- //#define WAL_MAGIC 0x377f0682
- const int WAL_MAGIC = 0x377f0682;
- /*
- ** Return the offset of frame iFrame in the write-ahead log file,
- ** assuming a database page size of szPage bytes. The offset returned
- ** is to the start of the write-ahead log frame-header.
- */
- //#define walFrameOffset(iFrame, szPage) ( \
- // WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
- //)
- static int walFrameOffset(iFrame, szPage) {
- return WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE);
- }
- /*
- ** An open write-ahead log file is represented by an instance of the
- ** following object.
- */
- struct Wal {
- sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
- sqlite3_file *pDbFd; /* File handle for the database file */
- sqlite3_file *pWalFd; /* File handle for WAL file */
- u32 iCallback; /* Value to pass to log callback (or 0) */
- int nWiData; /* Size of array apWiData */
- volatile u32 **apWiData; /* Pointer to wal-index content in memory */
- u16 szPage; /* Database page size */
- i16 readLock; /* Which read lock is being held. -1 for none */
- u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
- u8 writeLock; /* True if in a write transaction */
- u8 ckptLock; /* True if holding a checkpoint lock */
- u8 readOnly; /* True if the WAL file is open read-only */
- WalIndexHdr hdr; /* Wal-index header for current transaction */
- const char *zWalName; /* Name of WAL file */
- u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
- #if SQLITE_DEBUG
- u8 lockError; /* True if a locking error has occurred */
- #endif
- };
- /*
- ** Each page of the wal-index mapping contains a hash-table made up of
- ** an array of HASHTABLE_NSLOT elements of the following type.
- */
- typedef u16 ht_slot;
- /*
- ** 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
- ** frame most recently written to the WAL (in other words, the frame with
- ** the largest index).
- **
- ** The internals of this structure are only accessed by:
- **
- ** walIteratorInit() - Create a new iterator,
- ** walIteratorNext() - Step an iterator,
- ** walIteratorFree() - Free an iterator.
- **
- ** This functionality is used by the checkpoint code (see walCheckpoint()).
- */
- struct WalIterator {
- int iPrior; /* Last result returned from the iterator */
- int nSegment; /* Size of the aSegment[] array */
- struct WalSegment {
- int iNext; /* Next slot in aIndex[] not yet returned */
- ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
- u32 *aPgno; /* Array of page numbers. */
- int nEntry; /* Max size of aPgno[] and aIndex[] arrays */
- int iZero; /* Frame number associated with aPgno[0] */
- } aSegment[1]; /* One for every 32KB page in the WAL */
- };
- /*
- ** Define the parameters of the hash tables in the wal-index file. There
- ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
- ** wal-index.
- **
- ** Changing any of these constants will alter the wal-index format and
- ** create incompatibilities.
- */
- //#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
- //#define HASHTABLE_HASH_1 383 /* Should be prime */
- //#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
- const int HASHTABLE_NPAGE = 4096 ;
- const int HASHTABLE_HASH_1 = 383 ;
- const int HASHTABLE_NSLOT = (HASHTABLE_NPAGE*2);
- /*
- ** The block of page numbers associated with the first hash-table in a
- ** wal-index is smaller than usual. This is so that there is a complete
- ** hash-table on each aligned 32KB page of the wal-index.
- */
- //#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
- const int HASHTABLE_NPAGE_ONE =(HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)));
- /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
- //#define WALINDEX_PGSZ ( \
- // sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
- //)
- static int WALINDEX_PGSZ(){
- return sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32);
- }
- /*
- ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
- ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
- ** numbered from zero.
- **
- ** If this call is successful, *ppPage is set to point to the wal-index
- ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
- ** then an SQLite error code is returned and *ppPage is set to 0.
- */
- static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
- int rc = SQLITE_OK;
- /* Enlarge the pWal.apWiData[] array if required */
- if( pWal.nWiData<=iPage ){
- int nByte = sizeof(u32*)*(iPage+1);
- volatile u32 **apNew;
- apNew = (volatile u32 **)sqlite3_realloc((void *)pWal.apWiData, nByte);
- if( !apNew ){
- *ppPage = 0;
- return SQLITE_NOMEM;
- }
- memset((void*)&apNew[pWal.nWiData], 0,
- sizeof(u32*)*(iPage+1-pWal.nWiData));
- pWal.apWiData = apNew;
- pWal.nWiData = iPage+1;
- }
- /* Request a pointer to the required page from the VFS */
- if( pWal.apWiData[iPage]==0 ){
- if( pWal.exclusiveMode==WAL_HEAPMEMORY_MODE ){
- pWal.apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
- if( !pWal.apWiData[iPage] ) rc = SQLITE_NOMEM;
- }else{
- rc = sqlite3OsShmMap(pWal.pDbFd, iPage, WALINDEX_PGSZ,
- pWal.writeLock, (void volatile **)&pWal.apWiData[iPage]
- );
- }
- }
- *ppPage = pWal.apWiData[iPage];
- Debug.Assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
- return rc;
- }
- /*
- ** Return a pointer to the WalCkptInfo structure in the wal-index.
- */
- static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
- Debug.Assert( pWal.nWiData>0 && pWal.apWiData[0] );
- return (volatile WalCkptInfo*)&(pWal.apWiData[0][sizeof(WalIndexHdr)/2]);
- }
- /*
- ** Return a pointer to the WalIndexHdr structure in the wal-index.
- */
- static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
- Debug.Assert( pWal.nWiData>0 && pWal.apWiData[0] );
- return (volatile WalIndexHdr*)pWal.apWiData[0];
- }
- /*
- ** The argument to this macro must be of type u32. On a little-endian
- ** architecture, it returns the u32 value that results from interpreting
- ** the 4 bytes as a big-endian value. On a big-endian architecture, it
- ** returns the value that would be produced by intepreting the 4 bytes
- ** of the input value as a little-endian integer.
- */
- //#define BYTESWAP32(x) ( \
- // (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
- // + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
- //)
- static int BYTESWAP32(int x) { return
- (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)
- + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) ;
- }
- /*
- ** 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(
- int nativeCksum, /* True for native byte-order, false for non-native */
- 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;
- u32 *aData = (u32 *)a;
- u32 *aEnd = (u32 *)&a[nByte];
- if( aIn ){
- s1 = aIn[0];
- s2 = aIn[1];
- }else{
- s1 = s2 = 0;
- }
- Debug.Assert( nByte>=8 );
- Debug.Assert( (nByte&0x00000007)==0 );
- if( nativeCksum ){
- do {
- s1 += *aData++ + s2;
- s2 += *aData++ + s1;
- }while( aData<aEnd );
- }else{
- do {
- s1 += BYTESWAP32(aData[0]) + s2;
- s2 += BYTESWAP32(aData[1]) + s1;
- aData += 2;
- }while( aData<aEnd );
- }
- aOut[0] = s1;
- aOut[1] = s2;
- }
- static void walShmBarrier(Wal *pWal){
- if( pWal.exclusiveMode!=WAL_HEAPMEMORY_MODE ){
- sqlite3OsShmBarrier(pWal.pDbFd);
- }
- }
- /*
- ** 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){
- volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
- const int nCksum = offsetof(WalIndexHdr, aCksum);
- Debug.Assert( pWal.writeLock );
- pWal.hdr.isInit = 1;
- pWal.hdr.iVersion = WALINDEX_MAX_VERSION;
- walChecksumBytes(1, (u8*)&pWal.hdr, nCksum, 0, pWal.hdr.aCksum);
- memcpy((void *)&aHdr[1], (void *)&pWal.hdr, sizeof(WalIndexHdr));
- walShmBarrier(pWal);
- memcpy((void *)&aHdr[0], (void *)&pWal.hdr, sizeof(WalIndexHdr));
- }
- /*
- ** 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 */
- ){
- int nativeCksum; /* True for native byte-order checksums */
- u32 *aCksum = pWal.hdr.aFrameCksum;
- Debug.Assert( WAL_FRAME_HDRSIZE==24 );
- sqlite3Put4byte(&aFrame[0], iPage);
- sqlite3Put4byte(&aFrame[4], nTruncate);
- memcpy(&aFrame[8], pWal.hdr.aSalt, 8);
- nativeCksum = (pWal.hdr.bigEndCksum==SQLITE_BIGENDIAN);
- walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
- walChecksumBytes(nativeCksum, 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 */
- ){
- int nativeCksum; /* True for native byte-order checksums */
- u32 *aCksum = pWal.hdr.aFrameCksum;
- u32 pgno; /* Page number of the frame */
- Debug.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 the page number is creater than zero.
- */
- pgno = sqlite3Get4byte(&aFrame[0]);
- if( pgno==0 ){
- return 0;
- }
- /* A frame is only valid if a checksum of the WAL header,
- ** all prior frams, the first 16 bytes of this frame-header,
- ** and the frame-data matches the checksum in the last 8
- ** bytes of this frame-header.
- */
- nativeCksum = (pWal.hdr.bigEndCksum==SQLITE_BIGENDIAN);
- walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
- walChecksumBytes(nativeCksum, 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 = pgno;
- *pnTruncate = sqlite3Get4byte(&aFrame[4]);
- return 1;
- }
- #if (SQLITE_TEST) && (SQLITE_DEBUG)
- /*
- ** Names of locks. This routine is used to provide debugging output and is not
- ** a part of an ordinary build.
- */
- static const char *walLockName(int lockIdx){
- if( lockIdx==WAL_WRITE_LOCK ){
- return "WRITE-LOCK";
- }else if( lockIdx==WAL_CKPT_LOCK ){
- return "CKPT-LOCK";
- }else if( lockIdx==WAL_RECOVER_LOCK ){
- return "RECOVER-LOCK";
- }else{
- static char zName[15];
- sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
- lockIdx-WAL_READ_LOCK(0));
- return zName;
- }
- }
- #endif //*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
-
- /*
- ** Set or release locks on the WAL. Locks are either shared or exclusive.
- ** A lock cannot be moved directly between shared and exclusive - it must go
- ** through the unlocked state first.
- **
- ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
- */
- static int walLockShared(Wal *pWal, int lockIdx){
- int rc;
- if( pWal.exclusiveMode ) return SQLITE_OK;
- rc = sqlite3OsShmLock(pWal.pDbFd, lockIdx, 1,
- SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
- WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
- walLockName(lockIdx), rc ? "failed" : "ok"));
- VVA_ONLY( pWal.lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
- return rc;
- }
- static void walUnlockShared(Wal *pWal, int lockIdx){
- if( pWal.exclusiveMode ) return;
- (void)sqlite3OsShmLock(pWal.pDbFd, lockIdx, 1,
- SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
- WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
- }
- static int walLockExclusive(Wal *pWal, int lockIdx, int n){
- int rc;
- if( pWal.exclusiveMode ) return SQLITE_OK;
- rc = sqlite3OsShmLock(pWal.pDbFd, lockIdx, n,
- SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
- WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
- walLockName(lockIdx), n, rc ? "failed" : "ok"));
- VVA_ONLY( pWal.lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
- return rc;
- }
- static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
- if( pWal.exclusiveMode ) return;
- (void)sqlite3OsShmLock(pWal.pDbFd, lockIdx, n,
- SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
- WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
- walLockName(lockIdx), n));
- }
- /*
- ** Compute a hash on a page number. The resulting hash value must land
- ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
- ** the hash to the next value in the event of a collision.
- */
- static int walHash(u32 iPage){
- Debug.Assert( iPage>0 );
- Debug.Assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
- return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
- }
- static int walNextHash(int iPriorHash){
- return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
- }
- /*
- ** Return pointers to the hash table and page number array stored on
- ** page iHash of the wal-index. The wal-index is broken into 32KB pages
- ** numbered starting from 0.
- **
- ** Set output variable *paHash to point to the start of the hash table
- ** in the wal-index file. Set *piZero to one less than the frame
- ** number of the first frame indexed by this hash table. If a
- ** slot in the hash table is set to N, it refers to frame number
- ** (*piZero+N) in the log.
- **
- ** Finally, set *paPgno so that *paPgno[1] is the page number of the
- ** first frame indexed by the hash table, frame (*piZero+1).
- */
- static int walHashGet(
- Wal *pWal, /* WAL handle */
- int iHash, /* Find the iHash'th table */
- volatile ht_slot **paHash, /* OUT: Pointer to hash index */
- volatile u32 **paPgno, /* OUT: Pointer to page number array */
- u32 *piZero /* OUT: Frame associated with *paPgno[0] */
- ){
- int rc; /* Return code */
- volatile u32 *aPgno;
- rc = walIndexPage(pWal, iHash, &aPgno);
- Debug.Assert( rc==SQLITE_OK || iHash>0 );
- if( rc==SQLITE_OK ){
- u32 iZero;
- volatile ht_slot *aHash;
- aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
- if( iHash==0 ){
- aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
- iZero = 0;
- }else{
- iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
- }
-
- *paPgno = &aPgno[-1];
- *paHash = aHash;
- *piZero = iZero;
- }
- return rc;
- }
- /*
- ** Return the number of the wal-index page that contains the hash-table
- ** and page-number array that contain entries corresponding to WAL frame
- ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
- ** are numbered starting from 0.
- */
- static int walFramePage(u32 iFrame){
- int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
- Debug.Assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
- && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
- && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
- && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
- && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
- );
- return iHash;
- }
- /*
- ** Return the page number associated with frame iFrame in this WAL.
- */
- static u32 walFramePgno(Wal *pWal, u32 iFrame){
- int iHash = walFramePage(iFrame);
- if( iHash==0 ){
- return pWal.apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
- }
- return pWal.apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
- }
- /*
- ** Remove entries from the hash table that point to WAL slots greater
- ** than pWal.hdr.mxFrame.
- **
- ** This function is called whenever pWal.hdr.mxFrame is decreased due
- ** to a rollback or savepoint.
- **
- ** At most only the hash table containing pWal.hdr.mxFrame needs to be
- ** updated. Any later hash tables will be automatically cleared when
- ** pWal.hdr.mxFrame advances to the point where those hash tables are
- ** actually needed.
- */
- static void walCleanupHash(Wal *pWal){
- volatile ht_slot *aHash = 0; /* Pointer to hash table to clear */
- volatile u32 *aPgno = 0; /* Page number array for hash table */
- u32 iZero = 0; /* frame == (aHash[x]+iZero) */
- int iLimit = 0; /* Zero values greater than this */
- int nByte; /* Number of bytes to zero in aPgno[] */
- int i; /* Used to iterate through aHash[] */
- Debug.Assert( pWal.writeLock );
- testcase( pWal.hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
- testcase( pWal.hdr.mxFrame==HASHTABLE_NPAGE_ONE );
- testcase( pWal.hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
- if( pWal.hdr.mxFrame==0 ) return;
- /* Obtain pointers to the hash-table and page-number array containing
- ** the entry that corresponds to frame pWal.hdr.mxFrame. It is guaranteed
- ** that the page said hash-table and array reside on is already mapped.
- */
- Debug.Assert( pWal.nWiData>walFramePage(pWal.hdr.mxFrame) );
- Debug.Assert( pWal.apWiData[walFramePage(pWal.hdr.mxFrame)] );
- walHashGet(pWal, walFramePage(pWal.hdr.mxFrame), &aHash, &aPgno, &iZero);
- /* Zero all hash-table entries that correspond to frame numbers greater
- ** than pWal.hdr.mxFrame.
- */
- iLimit = pWal.hdr.mxFrame - iZero;
- Debug.Assert( iLimit>0 );
- for(i=0; i<HASHTABLE_NSLOT; i++){
- if( aHash[i]>iLimit ){
- aHash[i] = 0;
- }
- }
-
- /* Zero the entries in the aPgno array that correspond to frames with
- ** frame numbers greater than pWal.hdr.mxFrame.
- */
- nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
- memset((void *)&aPgno[iLimit+1], 0, nByte);
- #if SQLITE_ENABLE_EXPENSIVE_ASSERT
- /* Verify that the every entry in the mapping region is still reachable
- ** via the hash table even after the cleanup.
- */
- if( iLimit ){
- int i; /* Loop counter */
- int iKey; /* Hash key */
- for(i=1; i<=iLimit; i++){
- for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
- if( aHash[iKey]==i ) break;
- }
- Debug.Assert( aHash[iKey]==i );
- }
- }
- #endif //* SQLITE_ENABLE_EXPENSIVE_ASSERT */
- }
- /*
- ** Set an entry in the wal-index that will map database page number
- ** pPage into WAL frame iFrame.
- */
- static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
- int rc; /* Return code */
- u32 iZero = 0; /* One less than frame number of aPgno[1] */
- volatile u32 *aPgno = 0; /* Page number array */
- volatile ht_slot *aHash = 0; /* Hash table */
- rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
- /* Assuming the wal-index file was successfully mapped, populate the
- ** page number array and hash table entry.
- */
- if( rc==SQLITE_OK ){
- int iKey; /* Hash table key */
- int idx; /* Value to write to hash-table slot */
- int nCollide; /* Number of hash collisions */
- idx = iFrame - iZero;
- Debug.Assert( idx <= HASHTABLE_NSLOT/2 + 1 );
-
- /* If this is the first entry to be added to this hash-table, zero the
- ** entire hash table and aPgno[] array before proceding.
- */
- if( idx==1 ){
- int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
- memset((void*)&aPgno[1], 0, nByte);
- }
- /* If the entry in aPgno[] is already set, then the previous writer
- ** must have exited unexpectedly in the middle of a transaction (after
- ** writing one or more dirty pages to the WAL to free up memory).
- ** Remove the remnants of that writers uncommitted transaction from
- ** the hash-table before writing any new entries.
- */
- if( aPgno[idx] ){
- walCleanupHash(pWal);
- Debug.Assert( !aPgno[idx] );
- }
- /* Write the aPgno[] array entry and the hash-table slot. */
- nCollide = idx;
- for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
- if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
- }
- aPgno[idx] = iPage;
- aHash[iKey] = (ht_slot)idx;
- #if SQLITE_ENABLE_EXPENSIVE_ASSERT
- /* Verify that the number of entries in the hash table exactly equals
- ** the number of entries in the mapping region.
- */
- {
- int i; /* Loop counter */
- int nEntry = 0; /* Number of entries in the hash table */
- for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
- Debug.Assert( nEntry==idx );
- }
- /* Verify that the every entry in the mapping region is reachable
- ** via the hash table. This turns out to be a really, really expensive
- ** thing to check, so only do this occasionally - not on every
- ** iteration.
- */
- if( (idx&0x3ff)==0 ){
- int i; /* Loop counter */
- for(i=1; i<=idx; i++){
- for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
- if( aHash[iKey]==i ) break;
- }
- Debug.Assert( aHash[iKey]==i );
- }
- }
- #endif //* SQLITE_ENABLE_EXPENSIVE_ASSERT */
- }
- return rc;
- }
- /*
- ** Recover the wal-index by reading the write-ahead log file.
- **
- ** This routine first tries to establish an exclusive lock on the
- ** wal-index to prevent other threads/processes from doing anything
- ** with the WAL or wal-index while recovery is running. The
- ** WAL_RECOVER_LOCK is also held so that other threads will know
- ** that this thread is running recovery. If unable to establish
- ** the necessary locks, this routine returns SQLITE_BUSY.
- */
- static int walIndexRecover(Wal *pWal){
- int rc; /* Return Code */
- i64 nSize; /* Size of log file */
- u32 aFrameCksum[2] = {0, 0};
- int iLock; /* Lock offset to lock for checkpoint */
- int nLock; /* Number of locks to hold */
- /* Obtain an exclusive lock on all byte in the locking range not already
- ** locked by the caller. The caller is guaranteed to have locked the
- ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
- ** If successful, the same bytes that are locked here are unlocked before
- ** this function returns.
- */
- Debug.Assert( pWal.ckptLock==1 || pWal.ckptLock==0 );
- Debug.Assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
- Debug.Assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
- Debug.Assert( pWal.writeLock );
- iLock = WAL_ALL_BUT_WRITE + pWal.ckptLock;
- nLock = SQLITE_SHM_NLOCK - iLock;
- rc = walLockExclusive(pWal, iLock, nLock);
- if( rc ){
- return rc;
- }
- WALTRACE(("WAL%p: recovery begin...\n", pWal));
- memset(&pWal.hdr, 0, sizeof(WalIndexHdr));
- rc = sqlite3OsFileSize(pWal.pWalFd, &nSize);
- if( rc!=SQLITE_OK ){
- goto recovery_error;
- }
- if( nSize>WAL_HDRSIZE ){
- u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL 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 */
- u32 magic; /* Magic value read from WAL header */
- u32 version; /* Magic value read from WAL header */
- /* Read in the WAL header. */
- rc = sqlite3OsRead(pWal.pWalFd, aBuf, WAL_HDRSIZE, 0);
- if( rc!=SQLITE_OK ){
- goto recovery_error;
- }
- /* 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. Similarly, if the 'magic' value is invalid, ignore the whole
- ** WAL file.
- */
- magic = sqlite3Get4byte(&aBuf[0]);
- szPage = sqlite3Get4byte(&aBuf[8]);
- if( (magic&0xFFFFFFFE)!=WAL_MAGIC
- || szPage&(szPage-1)
- || szPage>SQLITE_MAX_PAGE_SIZE
- || szPage<512
- ){
- goto finished;
- }
- pWal.hdr.bigEndCksum = (u8)(magic&0x00000001);
- pWal.szPage = (u16)szPage;
- pWal.nCkpt = sqlite3Get4byte(&aBuf[12]);
- memcpy(&pWal.hdr.aSalt, &aBuf[16], 8);
- /* Verify that the WAL header checksum is correct */
- walChecksumBytes(pWal.hdr.bigEndCksum==SQLITE_BIGENDIAN,
- aBuf, WAL_HDRSIZE-2*4, 0, pWal.hdr.aFrameCksum
- );
- if( pWal.hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
- || pWal.hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
- ){
- goto finished;
- }
- /* Verify that the version number on the WAL format is one that
- ** are able to understand */
- version = sqlite3Get4byte(&aBuf[4]);
- if( version!=WAL_MAX_VERSION ){
- rc = SQLITE_CANTOPEN_BKPT;
- goto finished;
- }
- /* Malloc a buffer to read frames into. */
- szFrame = szPage + WAL_FRAME_HDRSIZE;
- aFrame = (u8 *)sqlite3_malloc(szFrame);
- if( !aFrame ){
- rc = SQLITE_NOMEM;
- goto recovery_error;
- }
- 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 ){
- pWal.hdr.mxFrame = iFrame;
- pWal.hdr.nPage = nTruncate;
- pWal.hdr.szPage = (u16)szPage;
- aFrameCksum[0] = pWal.hdr.aFrameCksum[0];
- aFrameCksum[1] = pWal.hdr.aFrameCksum[1];
- }
- }
- sqlite3_free(aFrame);
- }
- finished:
- if( rc==SQLITE_OK ){
- volatile WalCkptInfo *pInfo;
- int i;
- pWal.hdr.aFrameCksum[0] = aFrameCksum[0];
- pWal.hdr.aFrameCksum[1] = aFrameCksum[1];
- walIndexWriteHdr(pWal);
- /* Reset the checkpoint-header. This is safe because this thread is
- ** currently holding locks that exclude all other readers, writers and
- ** checkpointers.
- */
- pInfo = walCkptInfo(pWal);
- pInfo.nBackfill = 0;
- pInfo.aReadMark[0] = 0;
- for(i=1; i<WAL_NREADER; i++) pInfo.aReadMark[i] = READMARK_NOT_USED;
- }
- recovery_error:
- WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
- walUnlockExclusive(pWal, iLock, nLock);
- return rc;
- }
- /*
- ** Close an open wal-index.
- */
- sta…
Large files files are truncated, but you can click here to view the full file