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 | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 17 | #ifndef _WAL_H_ |
| 18 | #define _WAL_H_ |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 19 | |
| 20 | #include "sqliteInt.h" |
| 21 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 22 | #ifdef SQLITE_OMIT_WAL |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 23 | # define sqlite3WalOpen(x,y,z) 0 |
| 24 | # define sqlite3WalClose(w,x,y,z) 0 |
| 25 | # define sqlite3WalBeginReadTransaction(y,z) 0 |
| 26 | # define sqlite3WalEndReadTransaction(z) |
| 27 | # define sqlite3WalRead(v,w,x,y,z) 0 |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 28 | # define sqlite3WalDbsize(y,z) |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 29 | # define sqlite3WalBeginWriteTransaction(y) 0 |
| 30 | # define sqlite3WalEndWRiteTransaction(x) 0 |
| 31 | # define sqlite3WalUndo(x,y,z) 0 |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 32 | # define sqlite3WalSavepoint(y,z) |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 33 | # define sqlite3WalSavepointUndo(y,z) 0 |
| 34 | # define sqlite3WalFrames(u,v,w,x,y,z) 0 |
| 35 | # define sqlite3WalCheckpoint(u,v,w,x) 0 |
| 36 | # define sqlite3WalCallback(z) 0 |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 37 | #else |
| 38 | |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 39 | #define WAL_SAVEPOINT_NDATA 3 |
| 40 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 41 | /* Connection to a write-ahead log (WAL) file. |
| 42 | ** There is one object of this type for each pager. |
| 43 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 44 | typedef struct Wal Wal; |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 45 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 46 | /* Open and close a connection to a write-ahead log. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 47 | int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *zName, Wal**); |
| 48 | int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 49 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 50 | /* Used by readers to open (lock) and close (unlock) a snapshot. A |
| 51 | ** snapshot is like a read-transaction. It is the state of the database |
| 52 | ** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and |
| 53 | ** preserves the current state even if the other threads or processes |
| 54 | ** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the |
| 55 | ** transaction and releases the lock. |
| 56 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 57 | int sqlite3WalBeginReadTransaction(Wal *pWal, int *); |
| 58 | void sqlite3WalEndReadTransaction(Wal *pWal); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 59 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 60 | /* Read a page from the write-ahead log, if it is present. */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 61 | int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut); |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 62 | |
| 63 | /* Return the size of the database as it existed at the beginning |
| 64 | ** of the snapshot */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 65 | void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 66 | |
| 67 | /* Obtain or release the WRITER lock. */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 68 | int sqlite3WalBeginWriteTransaction(Wal *pWal); |
| 69 | int sqlite3WalEndWriteTransaction(Wal *pWal); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 70 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 71 | /* Undo any frames written (but not committed) to the log */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 72 | int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 73 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 74 | /* Return an integer that records the current (uncommitted) write |
| 75 | ** position in the WAL */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 76 | void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData); |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 77 | |
| 78 | /* Move the write position of the WAL back to iFrame. Called in |
| 79 | ** response to a ROLLBACK TO command. */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 80 | int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 81 | |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 82 | /* Write a frame or frames to the log. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 83 | int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int); |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 84 | |
| 85 | /* Copy pages from the log to the database file */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 86 | int sqlite3WalCheckpoint( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 87 | Wal *pWal, /* Write-ahead log connection */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 88 | int sync_flags, /* Flags to sync db file with (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 89 | int nBuf, /* Size of buffer nBuf */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 90 | u8 *zBuf /* Temporary buffer to use */ |
dan | 6700d02 | 2010-04-12 19:05:58 +0000 | [diff] [blame] | 91 | ); |
| 92 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 93 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 94 | ** number of frames in the WAL at the point of the last commit since |
| 95 | ** sqlite3WalCallback() was called. If no commits have occurred since |
| 96 | ** the last call, then return 0. |
| 97 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 98 | int sqlite3WalCallback(Wal *pWal); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 99 | |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 100 | /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released) |
| 101 | ** by the pager layer on the database file. |
| 102 | */ |
| 103 | int sqlite3WalExclusiveMode(Wal *pWal, int op); |
| 104 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 105 | #endif /* ifndef SQLITE_OMIT_WAL */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 106 | #endif /* _WAL_H_ */ |