dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2010 February 1 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This header file defines the interface to the write-ahead logging |
| 13 | ** system. Refer to the comments below and the header comment attached to |
| 14 | ** the implementation of each function in log.c for further details. |
| 15 | */ |
| 16 | |
drh | 43f58d6 | 2016-07-09 16:14:45 +0000 | [diff] [blame] | 17 | #ifndef SQLITE_WAL_H |
| 18 | #define SQLITE_WAL_H |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 19 | |
| 20 | #include "sqliteInt.h" |
| 21 | |
drh | daaae7b | 2017-08-25 01:14:43 +0000 | [diff] [blame] | 22 | /* Macros for extracting appropriate sync flags for either transaction |
| 23 | ** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)): |
dan | 0420b74 | 2011-12-30 10:54:24 +0000 | [diff] [blame] | 24 | */ |
drh | daaae7b | 2017-08-25 01:14:43 +0000 | [diff] [blame] | 25 | #define WAL_SYNC_FLAGS(X) ((X)&0x03) |
| 26 | #define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03) |
dan | 0420b74 | 2011-12-30 10:54:24 +0000 | [diff] [blame] | 27 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 28 | #ifdef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 29 | # define sqlite3WalOpen(x,y,z) 0 |
drh | 85a8375 | 2011-05-16 21:00:27 +0000 | [diff] [blame] | 30 | # define sqlite3WalLimit(x,y) |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 31 | # define sqlite3WalClose(v,w,x,y,z) 0 |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 32 | # define sqlite3WalBeginReadTransaction(y,z) 0 |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 33 | # define sqlite3WalEndReadTransaction(z) |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 34 | # define sqlite3WalDbsize(y) 0 |
| 35 | # define sqlite3WalBeginWriteTransaction(y) 0 |
| 36 | # define sqlite3WalEndWriteTransaction(x) 0 |
| 37 | # define sqlite3WalUndo(x,y,z) 0 |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 38 | # define sqlite3WalSavepoint(y,z) |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 39 | # define sqlite3WalSavepointUndo(y,z) 0 |
| 40 | # define sqlite3WalFrames(u,v,w,x,y,z) 0 |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 41 | # define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0 |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 42 | # define sqlite3WalCallback(z) 0 |
| 43 | # define sqlite3WalExclusiveMode(y,z) 0 |
| 44 | # define sqlite3WalHeapMemory(z) 0 |
dan | b3bdc72 | 2012-02-23 15:35:49 +0000 | [diff] [blame] | 45 | # define sqlite3WalFramesize(z) 0 |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 46 | # define sqlite3WalFindFrame(x,y,z) 0 |
drh | 21d6185 | 2016-01-08 02:27:01 +0000 | [diff] [blame] | 47 | # define sqlite3WalFile(x) 0 |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 48 | #else |
| 49 | |
dan | 6e6bd56 | 2010-06-02 18:59:03 +0000 | [diff] [blame] | 50 | #define WAL_SAVEPOINT_NDATA 4 |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 51 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 52 | /* Connection to a write-ahead log (WAL) file. |
| 53 | ** There is one object of this type for each pager. |
| 54 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 55 | typedef struct Wal Wal; |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 56 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 57 | /* Open and close a connection to a write-ahead log. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 58 | int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**); |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 59 | int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 60 | |
drh | 85a8375 | 2011-05-16 21:00:27 +0000 | [diff] [blame] | 61 | /* Set the limiting size of a WAL file. */ |
| 62 | void sqlite3WalLimit(Wal*, i64); |
| 63 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 64 | /* Used by readers to open (lock) and close (unlock) a snapshot. A |
| 65 | ** snapshot is like a read-transaction. It is the state of the database |
| 66 | ** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and |
| 67 | ** preserves the current state even if the other threads or processes |
| 68 | ** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the |
| 69 | ** transaction and releases the lock. |
| 70 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 71 | int sqlite3WalBeginReadTransaction(Wal *pWal, int *); |
| 72 | void sqlite3WalEndReadTransaction(Wal *pWal); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 73 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 74 | /* Read a page from the write-ahead log, if it is present. */ |
dan | 99bd109 | 2013-03-22 18:20:14 +0000 | [diff] [blame] | 75 | int sqlite3WalFindFrame(Wal *, Pgno, u32 *); |
| 76 | int sqlite3WalReadFrame(Wal *, u32, int, u8 *); |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 77 | |
dan | 763afe6 | 2010-08-03 06:42:39 +0000 | [diff] [blame] | 78 | /* If the WAL is not empty, return the size of the database. */ |
| 79 | Pgno sqlite3WalDbsize(Wal *pWal); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 80 | |
| 81 | /* Obtain or release the WRITER lock. */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 82 | int sqlite3WalBeginWriteTransaction(Wal *pWal); |
| 83 | int sqlite3WalEndWriteTransaction(Wal *pWal); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 84 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 85 | /* Undo any frames written (but not committed) to the log */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 86 | int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 87 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 88 | /* Return an integer that records the current (uncommitted) write |
| 89 | ** position in the WAL */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 90 | void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData); |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 91 | |
| 92 | /* Move the write position of the WAL back to iFrame. Called in |
| 93 | ** response to a ROLLBACK TO command. */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 94 | int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 95 | |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 96 | /* Write a frame or frames to the log. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 97 | int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 98 | |
| 99 | /* Copy pages from the log to the database file */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 100 | int sqlite3WalCheckpoint( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 101 | Wal *pWal, /* Write-ahead log connection */ |
dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 102 | sqlite3 *db, /* Check this handle's interrupt flag */ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 103 | int eMode, /* One of PASSIVE, FULL and RESTART */ |
dan | a58f26f | 2010-11-16 18:56:51 +0000 | [diff] [blame] | 104 | int (*xBusy)(void*), /* Function to call when busy */ |
| 105 | void *pBusyArg, /* Context argument for xBusyHandler */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 106 | int sync_flags, /* Flags to sync db file with (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 107 | int nBuf, /* Size of buffer nBuf */ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 108 | u8 *zBuf, /* Temporary buffer to use */ |
| 109 | int *pnLog, /* OUT: Number of frames in WAL */ |
| 110 | int *pnCkpt /* OUT: Number of backfilled frames in WAL */ |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 111 | ); |
| 112 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 113 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 114 | ** number of frames in the WAL at the point of the last commit since |
| 115 | ** sqlite3WalCallback() was called. If no commits have occurred since |
| 116 | ** the last call, then return 0. |
| 117 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 118 | int sqlite3WalCallback(Wal *pWal); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 119 | |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 120 | /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released) |
| 121 | ** by the pager layer on the database file. |
| 122 | */ |
| 123 | int sqlite3WalExclusiveMode(Wal *pWal, int op); |
| 124 | |
dan | 8c40800 | 2010-11-01 17:38:24 +0000 | [diff] [blame] | 125 | /* Return true if the argument is non-NULL and the WAL module is using |
| 126 | ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the |
| 127 | ** WAL module is using shared-memory, return false. |
| 128 | */ |
| 129 | int sqlite3WalHeapMemory(Wal *pWal); |
| 130 | |
dan | fc1acf3 | 2015-12-05 20:51:54 +0000 | [diff] [blame] | 131 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 132 | int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); |
| 133 | void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); |
dan | 1158498 | 2016-11-18 20:49:43 +0000 | [diff] [blame] | 134 | int sqlite3WalSnapshotRecover(Wal *pWal); |
dan | fc1acf3 | 2015-12-05 20:51:54 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
drh | 7070860 | 2012-02-24 14:33:28 +0000 | [diff] [blame] | 137 | #ifdef SQLITE_ENABLE_ZIPVFS |
dan | b3bdc72 | 2012-02-23 15:35:49 +0000 | [diff] [blame] | 138 | /* If the WAL file is not empty, return the number of bytes of content |
| 139 | ** stored in each frame (i.e. the db page-size when the WAL was created). |
| 140 | */ |
| 141 | int sqlite3WalFramesize(Wal *pWal); |
drh | 7070860 | 2012-02-24 14:33:28 +0000 | [diff] [blame] | 142 | #endif |
dan | b3bdc72 | 2012-02-23 15:35:49 +0000 | [diff] [blame] | 143 | |
drh | 21d6185 | 2016-01-08 02:27:01 +0000 | [diff] [blame] | 144 | /* Return the sqlite3_file object for the WAL file */ |
| 145 | sqlite3_file *sqlite3WalFile(Wal *pWal); |
| 146 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 147 | #endif /* ifndef SQLITE_OMIT_WAL */ |
drh | 43f58d6 | 2016-07-09 16:14:45 +0000 | [diff] [blame] | 148 | #endif /* SQLITE_WAL_H */ |