xref: /sqlite-3.40.0/src/wal.h (revision 861fb1e9)
1c438efd6Sdrh /*
2c438efd6Sdrh ** 2010 February 1
3c438efd6Sdrh **
4c438efd6Sdrh ** The author disclaims copyright to this source code.  In place of
5c438efd6Sdrh ** a legal notice, here is a blessing:
6c438efd6Sdrh **
7c438efd6Sdrh **    May you do good and not evil.
8c438efd6Sdrh **    May you find forgiveness for yourself and forgive others.
9c438efd6Sdrh **    May you share freely, never taking more than you give.
10c438efd6Sdrh **
11c438efd6Sdrh *************************************************************************
12c438efd6Sdrh ** This header file defines the interface to the write-ahead logging
13c438efd6Sdrh ** system. Refer to the comments below and the header comment attached to
14c438efd6Sdrh ** the implementation of each function in log.c for further details.
15c438efd6Sdrh */
16c438efd6Sdrh 
1743f58d6aSdrh #ifndef SQLITE_WAL_H
1843f58d6aSdrh #define SQLITE_WAL_H
19c438efd6Sdrh 
20c438efd6Sdrh #include "sqliteInt.h"
21c438efd6Sdrh 
22daaae7b9Sdrh /* Macros for extracting appropriate sync flags for either transaction
23daaae7b9Sdrh ** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
240420b74aSdan */
25daaae7b9Sdrh #define WAL_SYNC_FLAGS(X)   ((X)&0x03)
26daaae7b9Sdrh #define CKPT_SYNC_FLAGS(X)  (((X)>>2)&0x03)
270420b74aSdan 
285cf53537Sdan #ifdef SQLITE_OMIT_WAL
295cf53537Sdan # define sqlite3WalOpen(x,y,z)                   0
3085a83755Sdrh # define sqlite3WalLimit(x,y)
317fb89906Sdan # define sqlite3WalClose(v,w,x,y,z)              0
3273b64e4dSdrh # define sqlite3WalBeginReadTransaction(y,z)     0
3373b64e4dSdrh # define sqlite3WalEndReadTransaction(z)
34763afe62Sdan # define sqlite3WalDbsize(y)                     0
3573b64e4dSdrh # define sqlite3WalBeginWriteTransaction(y)      0
3638e1a279Sdan # define sqlite3WalEndWriteTransaction(x)        0
375cf53537Sdan # define sqlite3WalUndo(x,y,z)                   0
3871d89919Sdan # define sqlite3WalSavepoint(y,z)
395cf53537Sdan # define sqlite3WalSavepointUndo(y,z)            0
405cf53537Sdan # define sqlite3WalFrames(u,v,w,x,y,z)           0
417fb89906Sdan # define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
425cf53537Sdan # define sqlite3WalCallback(z)                   0
4338e1a279Sdan # define sqlite3WalExclusiveMode(y,z)            0
448c408004Sdan # define sqlite3WalHeapMemory(z)                 0
45b3bdc72dSdan # define sqlite3WalFramesize(z)                  0
4632c12fe2Sdan # define sqlite3WalFindFrame(x,y,z)              0
4721d61853Sdrh # define sqlite3WalFile(x)                       0
485cf53537Sdan #else
495cf53537Sdan 
506e6bd565Sdan #define WAL_SAVEPOINT_NDATA 4
5171d89919Sdan 
52833bf968Sdrh /* Connection to a write-ahead log (WAL) file.
53833bf968Sdrh ** There is one object of this type for each pager.
54833bf968Sdrh */
557ed91f23Sdrh typedef struct Wal Wal;
56c438efd6Sdrh 
57833bf968Sdrh /* Open and close a connection to a write-ahead log. */
58f23da966Sdan int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
597fb89906Sdan int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
60c438efd6Sdrh 
6185a83755Sdrh /* Set the limiting size of a WAL file. */
6285a83755Sdrh void sqlite3WalLimit(Wal*, i64);
6385a83755Sdrh 
64833bf968Sdrh /* Used by readers to open (lock) and close (unlock) a snapshot.  A
65833bf968Sdrh ** snapshot is like a read-transaction.  It is the state of the database
66833bf968Sdrh ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
67833bf968Sdrh ** preserves the current state even if the other threads or processes
68833bf968Sdrh ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
69833bf968Sdrh ** transaction and releases the lock.
70833bf968Sdrh */
7173b64e4dSdrh int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
7273b64e4dSdrh void sqlite3WalEndReadTransaction(Wal *pWal);
73c438efd6Sdrh 
74833bf968Sdrh /* Read a page from the write-ahead log, if it is present. */
7599bd1097Sdan int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
7699bd1097Sdan int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
77833bf968Sdrh 
78763afe62Sdan /* If the WAL is not empty, return the size of the database. */
79763afe62Sdan Pgno sqlite3WalDbsize(Wal *pWal);
80c438efd6Sdrh 
81c438efd6Sdrh /* Obtain or release the WRITER lock. */
8273b64e4dSdrh int sqlite3WalBeginWriteTransaction(Wal *pWal);
8373b64e4dSdrh int sqlite3WalEndWriteTransaction(Wal *pWal);
84c438efd6Sdrh 
85c438efd6Sdrh /* Undo any frames written (but not committed) to the log */
867ed91f23Sdrh int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
87c438efd6Sdrh 
88833bf968Sdrh /* Return an integer that records the current (uncommitted) write
89833bf968Sdrh ** position in the WAL */
9071d89919Sdan void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
91833bf968Sdrh 
92833bf968Sdrh /* Move the write position of the WAL back to iFrame.  Called in
93833bf968Sdrh ** response to a ROLLBACK TO command. */
9471d89919Sdan int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
954cd78b4dSdan 
96c438efd6Sdrh /* Write a frame or frames to the log. */
977ed91f23Sdrh int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
98c438efd6Sdrh 
99c438efd6Sdrh /* Copy pages from the log to the database file */
100c438efd6Sdrh int sqlite3WalCheckpoint(
1017ed91f23Sdrh   Wal *pWal,                      /* Write-ahead log connection */
1027fb89906Sdan   sqlite3 *db,                    /* Check this handle's interrupt flag */
103cdc1f049Sdan   int eMode,                      /* One of PASSIVE, FULL and RESTART */
104a58f26f9Sdan   int (*xBusy)(void*),            /* Function to call when busy */
105a58f26f9Sdan   void *pBusyArg,                 /* Context argument for xBusyHandler */
106c438efd6Sdrh   int sync_flags,                 /* Flags to sync db file with (or 0) */
107b6e099a9Sdan   int nBuf,                       /* Size of buffer nBuf */
108cdc1f049Sdan   u8 *zBuf,                       /* Temporary buffer to use */
109cdc1f049Sdan   int *pnLog,                     /* OUT: Number of frames in WAL */
110cdc1f049Sdan   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
111c438efd6Sdrh );
112c438efd6Sdrh 
113833bf968Sdrh /* Return the value to pass to a sqlite3_wal_hook callback, the
114833bf968Sdrh ** number of frames in the WAL at the point of the last commit since
115833bf968Sdrh ** sqlite3WalCallback() was called.  If no commits have occurred since
116833bf968Sdrh ** the last call, then return 0.
117833bf968Sdrh */
1187ed91f23Sdrh int sqlite3WalCallback(Wal *pWal);
119c438efd6Sdrh 
1205543759bSdan /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
1215543759bSdan ** by the pager layer on the database file.
1225543759bSdan */
1235543759bSdan int sqlite3WalExclusiveMode(Wal *pWal, int op);
1245543759bSdan 
1258c408004Sdan /* Return true if the argument is non-NULL and the WAL module is using
1268c408004Sdan ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
1278c408004Sdan ** WAL module is using shared-memory, return false.
1288c408004Sdan */
1298c408004Sdan int sqlite3WalHeapMemory(Wal *pWal);
1308c408004Sdan 
131fc1acf33Sdan #ifdef SQLITE_ENABLE_SNAPSHOT
132fc1acf33Sdan int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
133*861fb1e9Sdan void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
1341158498dSdan int sqlite3WalSnapshotRecover(Wal *pWal);
135fa3d4c19Sdan int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
136fa3d4c19Sdan void sqlite3WalSnapshotUnlock(Wal *pWal);
137fc1acf33Sdan #endif
138fc1acf33Sdan 
13970708600Sdrh #ifdef SQLITE_ENABLE_ZIPVFS
140b3bdc72dSdan /* If the WAL file is not empty, return the number of bytes of content
141b3bdc72dSdan ** stored in each frame (i.e. the db page-size when the WAL was created).
142b3bdc72dSdan */
143b3bdc72dSdan int sqlite3WalFramesize(Wal *pWal);
14470708600Sdrh #endif
145b3bdc72dSdan 
14621d61853Sdrh /* Return the sqlite3_file object for the WAL file */
14721d61853Sdrh sqlite3_file *sqlite3WalFile(Wal *pWal);
14821d61853Sdrh 
14958021b23Sdan #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
150*861fb1e9Sdan int sqlite3WalWriteLock(Wal *pWal, int bLock);
151*861fb1e9Sdan void sqlite3WalDb(Wal *pWal, sqlite3 *db);
15258021b23Sdan #endif
15358021b23Sdan 
1545cf53537Sdan #endif /* ifndef SQLITE_OMIT_WAL */
15543f58d6aSdrh #endif /* SQLITE_WAL_H */
156