blob: a55362804b2b5e2bd5a8a93e9d9670780271d5c6 [file] [log] [blame]
dan6700d022010-04-12 19:05:58 +00001/*
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
drh43f58d62016-07-09 16:14:45 +000017#ifndef SQLITE_WAL_H
18#define SQLITE_WAL_H
dan6700d022010-04-12 19:05:58 +000019
20#include "sqliteInt.h"
21
dan0420b742011-12-30 10:54:24 +000022/* Additional values that can be added to the sync_flags argument of
23** sqlite3WalFrames():
24*/
25#define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
26#define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */
27
dan5cf53532010-05-01 16:40:20 +000028#ifdef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +000029# define sqlite3WalOpen(x,y,z) 0
drh85a83752011-05-16 21:00:27 +000030# define sqlite3WalLimit(x,y)
dancdc1f042010-11-18 12:11:05 +000031# define sqlite3WalClose(w,x,y,z) 0
32# define sqlite3WalBeginReadTransaction(y,z) 0
drh73b64e42010-05-30 19:55:15 +000033# define sqlite3WalEndReadTransaction(z)
dancdc1f042010-11-18 12:11:05 +000034# define sqlite3WalDbsize(y) 0
35# define sqlite3WalBeginWriteTransaction(y) 0
36# define sqlite3WalEndWriteTransaction(x) 0
37# define sqlite3WalUndo(x,y,z) 0
dan71d89912010-05-24 13:57:42 +000038# define sqlite3WalSavepoint(y,z)
dancdc1f042010-11-18 12:11:05 +000039# define sqlite3WalSavepointUndo(y,z) 0
40# define sqlite3WalFrames(u,v,w,x,y,z) 0
41# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
42# define sqlite3WalCallback(z) 0
43# define sqlite3WalExclusiveMode(y,z) 0
44# define sqlite3WalHeapMemory(z) 0
danb3bdc722012-02-23 15:35:49 +000045# define sqlite3WalFramesize(z) 0
dan32c12fe2013-05-02 17:37:31 +000046# define sqlite3WalFindFrame(x,y,z) 0
drh21d61852016-01-08 02:27:01 +000047# define sqlite3WalFile(x) 0
dan5cf53532010-05-01 16:40:20 +000048#else
49
dan6e6bd562010-06-02 18:59:03 +000050#define WAL_SAVEPOINT_NDATA 4
dan71d89912010-05-24 13:57:42 +000051
drh833bf962010-04-28 14:42:19 +000052/* Connection to a write-ahead log (WAL) file.
53** There is one object of this type for each pager.
54*/
drh7ed91f22010-04-29 22:34:07 +000055typedef struct Wal Wal;
dan6700d022010-04-12 19:05:58 +000056
drh833bf962010-04-28 14:42:19 +000057/* Open and close a connection to a write-ahead log. */
adam2e4491d2011-06-24 20:47:06 +000058int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, int, Wal**);
drhd9e5c4f2010-05-12 18:01:39 +000059int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
dan6700d022010-04-12 19:05:58 +000060
drh85a83752011-05-16 21:00:27 +000061/* Set the limiting size of a WAL file. */
62void sqlite3WalLimit(Wal*, i64);
63
drh833bf962010-04-28 14:42:19 +000064/* 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*/
drh73b64e42010-05-30 19:55:15 +000071int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
72void sqlite3WalEndReadTransaction(Wal *pWal);
dan6700d022010-04-12 19:05:58 +000073
drh833bf962010-04-28 14:42:19 +000074/* Read a page from the write-ahead log, if it is present. */
dan99bd1092013-03-22 18:20:14 +000075int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
76int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
drh833bf962010-04-28 14:42:19 +000077
dan763afe62010-08-03 06:42:39 +000078/* If the WAL is not empty, return the size of the database. */
79Pgno sqlite3WalDbsize(Wal *pWal);
dan6700d022010-04-12 19:05:58 +000080
adam02d24932012-05-09 22:36:25 +000081/* If a WAL journal file has been created, return it */
82sqlite3_file *sqlite3WalFile(Wal *pWal);
83
dan6700d022010-04-12 19:05:58 +000084/* Obtain or release the WRITER lock. */
drh73b64e42010-05-30 19:55:15 +000085int sqlite3WalBeginWriteTransaction(Wal *pWal);
86int sqlite3WalEndWriteTransaction(Wal *pWal);
dan6700d022010-04-12 19:05:58 +000087
dan74d6cd82010-04-24 18:44:05 +000088/* Undo any frames written (but not committed) to the log */
drh7ed91f22010-04-29 22:34:07 +000089int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
dan74d6cd82010-04-24 18:44:05 +000090
drh833bf962010-04-28 14:42:19 +000091/* Return an integer that records the current (uncommitted) write
92** position in the WAL */
dan71d89912010-05-24 13:57:42 +000093void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
drh833bf962010-04-28 14:42:19 +000094
95/* Move the write position of the WAL back to iFrame. Called in
96** response to a ROLLBACK TO command. */
dan71d89912010-05-24 13:57:42 +000097int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
dan4cd78b42010-04-26 16:57:10 +000098
danc5118782010-04-17 17:34:41 +000099/* Write a frame or frames to the log. */
drh7ed91f22010-04-29 22:34:07 +0000100int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
dan6700d022010-04-12 19:05:58 +0000101
102/* Copy pages from the log to the database file */
drhc438efd2010-04-26 00:19:45 +0000103int sqlite3WalCheckpoint(
drh7ed91f22010-04-29 22:34:07 +0000104 Wal *pWal, /* Write-ahead log connection */
dancdc1f042010-11-18 12:11:05 +0000105 int eMode, /* One of PASSIVE, FULL and RESTART */
dana58f26f2010-11-16 18:56:51 +0000106 int (*xBusy)(void*), /* Function to call when busy */
107 void *pBusyArg, /* Context argument for xBusyHandler */
danc5118782010-04-17 17:34:41 +0000108 int sync_flags, /* Flags to sync db file with (or 0) */
danb6e099a2010-05-04 14:47:39 +0000109 int nBuf, /* Size of buffer nBuf */
dancdc1f042010-11-18 12:11:05 +0000110 u8 *zBuf, /* Temporary buffer to use */
111 int *pnLog, /* OUT: Number of frames in WAL */
112 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
dan6700d022010-04-12 19:05:58 +0000113);
114
drh833bf962010-04-28 14:42:19 +0000115/* Return the value to pass to a sqlite3_wal_hook callback, the
116** number of frames in the WAL at the point of the last commit since
117** sqlite3WalCallback() was called. If no commits have occurred since
118** the last call, then return 0.
119*/
drh7ed91f22010-04-29 22:34:07 +0000120int sqlite3WalCallback(Wal *pWal);
dan8d22a172010-04-19 18:03:51 +0000121
dan55437592010-05-11 12:19:26 +0000122/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
123** by the pager layer on the database file.
124*/
125int sqlite3WalExclusiveMode(Wal *pWal, int op);
126
dan8c408002010-11-01 17:38:24 +0000127/* Return true if the argument is non-NULL and the WAL module is using
128** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
129** WAL module is using shared-memory, return false.
130*/
131int sqlite3WalHeapMemory(Wal *pWal);
132
danfc1acf32015-12-05 20:51:54 +0000133#ifdef SQLITE_ENABLE_SNAPSHOT
134int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
135void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
136#endif
137
drh70708602012-02-24 14:33:28 +0000138#ifdef SQLITE_ENABLE_ZIPVFS
danb3bdc722012-02-23 15:35:49 +0000139/* If the WAL file is not empty, return the number of bytes of content
140** stored in each frame (i.e. the db page-size when the WAL was created).
141*/
142int sqlite3WalFramesize(Wal *pWal);
drh70708602012-02-24 14:33:28 +0000143#endif
danb3bdc722012-02-23 15:35:49 +0000144
drh21d61852016-01-08 02:27:01 +0000145/* Return the sqlite3_file object for the WAL file */
146sqlite3_file *sqlite3WalFile(Wal *pWal);
147
dan5cf53532010-05-01 16:40:20 +0000148#endif /* ifndef SQLITE_OMIT_WAL */
drh43f58d62016-07-09 16:14:45 +0000149#endif /* SQLITE_WAL_H */