blob: 37d458b44883e1c40111b0c5b355228f7bf8f96f [file] [log] [blame]
drhed7c8552001-04-11 14:29:21 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhed7c8552001-04-11 14:29:21 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhed7c8552001-04-11 14:29:21 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This is the implementation of the page cache subsystem or "pager".
drhed7c8552001-04-11 14:29:21 +000013**
drhb19a2bc2001-09-16 00:13:26 +000014** The pager is used to access a database disk file. It implements
15** atomic commit and rollback through the use of a journal file that
16** is separate from the database file. The pager also implements file
17** locking to prevent two processes from writing the same database
18** file simultaneously, or one process from reading the database while
19** another is writing.
drhed7c8552001-04-11 14:29:21 +000020**
drh0f892532002-05-30 12:27:03 +000021** @(#) $Id: pager.c,v 1.46 2002/05/30 12:27:03 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drhd9b02572001-04-15 00:37:09 +000023#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000024#include "pager.h"
drh8cfbf082001-09-19 13:22:39 +000025#include "os.h"
drhed7c8552001-04-11 14:29:21 +000026#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000027#include <string.h>
drhed7c8552001-04-11 14:29:21 +000028
29/*
30** The page cache as a whole is always in one of the following
31** states:
32**
33** SQLITE_UNLOCK The page cache is not currently reading or
34** writing the database file. There is no
35** data held in memory. This is the initial
36** state.
37**
38** SQLITE_READLOCK The page cache is reading the database.
39** Writing is not permitted. There can be
40** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000041** file at the same time.
drhed7c8552001-04-11 14:29:21 +000042**
43** SQLITE_WRITELOCK The page cache is writing the database.
44** Access is exclusive. No other processes or
45** threads can be reading or writing while one
46** process is writing.
47**
drh306dc212001-05-21 13:45:10 +000048** The page cache comes up in SQLITE_UNLOCK. The first time a
49** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000050** After all pages have been released using sqlite_page_unref(),
drh306dc212001-05-21 13:45:10 +000051** the state transitions back to SQLITE_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000052** that sqlite_page_write() is called, the state transitions to
drh306dc212001-05-21 13:45:10 +000053** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
54** called on an outstanding page which means that the pager must
55** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
56** The sqlite_page_rollback() and sqlite_page_commit() functions
57** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000058*/
59#define SQLITE_UNLOCK 0
60#define SQLITE_READLOCK 1
61#define SQLITE_WRITELOCK 2
62
drhd9b02572001-04-15 00:37:09 +000063
drhed7c8552001-04-11 14:29:21 +000064/*
65** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +000066** This header is only visible to this pager module. The client
67** code that calls pager sees only the data that follows the header.
drhed7c8552001-04-11 14:29:21 +000068*/
drhd9b02572001-04-15 00:37:09 +000069typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +000070struct PgHdr {
71 Pager *pPager; /* The pager to which this page belongs */
72 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +000073 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhed7c8552001-04-11 14:29:21 +000074 int nRef; /* Number of users of this page */
drhd9b02572001-04-15 00:37:09 +000075 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
76 PgHdr *pNextAll, *pPrevAll; /* A list of all pages */
drhed7c8552001-04-11 14:29:21 +000077 char inJournal; /* TRUE if has been written to journal */
drhfa86c412002-02-02 15:01:15 +000078 char inCkpt; /* TRUE if written to the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +000079 char dirty; /* TRUE if we need to write back changes */
drh69688d52001-04-14 16:38:23 +000080 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh7e3b0a02001-04-28 16:52:40 +000081 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +000082};
83
84/*
drh69688d52001-04-14 16:38:23 +000085** Convert a pointer to a PgHdr into a pointer to its data
86** and back again.
drhed7c8552001-04-11 14:29:21 +000087*/
88#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
89#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh7e3b0a02001-04-28 16:52:40 +000090#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhed7c8552001-04-11 14:29:21 +000091
92/*
drhed7c8552001-04-11 14:29:21 +000093** How big to make the hash table used for locating in-memory pages
drh306dc212001-05-21 13:45:10 +000094** by page number. Knuth says this should be a prime number.
drhed7c8552001-04-11 14:29:21 +000095*/
drh603240c2002-03-05 01:11:12 +000096#define N_PG_HASH 2003
drhed7c8552001-04-11 14:29:21 +000097
98/*
99** A open page cache is an instance of the following structure.
100*/
101struct Pager {
102 char *zFilename; /* Name of the database file */
103 char *zJournal; /* Name of the journal file */
drh8cfbf082001-09-19 13:22:39 +0000104 OsFile fd, jfd; /* File descriptors for database and journal */
drhfa86c412002-02-02 15:01:15 +0000105 OsFile cpfd; /* File descriptor for the checkpoint journal */
drhed7c8552001-04-11 14:29:21 +0000106 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000107 int origDbSize; /* dbSize before the current change */
drhfa86c412002-02-02 15:01:15 +0000108 int ckptSize, ckptJSize; /* Size of database and journal at ckpt_begin() */
drh7e3b0a02001-04-28 16:52:40 +0000109 int nExtra; /* Add this many bytes to each in-memory page */
drh72f82862001-05-24 21:06:34 +0000110 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000111 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000112 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000113 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000114 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh603240c2002-03-05 01:11:12 +0000115 u8 journalOpen; /* True if journal file descriptors is valid */
116 u8 ckptOpen; /* True if the checkpoint journal is open */
drh0f892532002-05-30 12:27:03 +0000117 u8 ckptInUse; /* True we are in a checkpoint */
drh603240c2002-03-05 01:11:12 +0000118 u8 noSync; /* Do not sync the journal if true */
119 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
120 u8 errMask; /* One of several kinds of errors */
121 u8 tempFile; /* zFilename is a temporary file */
122 u8 readOnly; /* True for a read-only database */
123 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000124 u8 dirtyFile; /* True if database file has changed in any way */
drh603240c2002-03-05 01:11:12 +0000125 u8 *aInJournal; /* One bit for each page in the database file */
126 u8 *aInCkpt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000127 PgHdr *pFirst, *pLast; /* List of free pages */
drhd9b02572001-04-15 00:37:09 +0000128 PgHdr *pAll; /* List of all pages */
drhed7c8552001-04-11 14:29:21 +0000129 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000130};
131
132/*
133** These are bits that can be set in Pager.errMask.
134*/
135#define PAGER_ERR_FULL 0x01 /* a write() failed */
136#define PAGER_ERR_MEM 0x02 /* malloc() failed */
137#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
138#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000139#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000140
141/*
142** The journal file contains page records in the following
143** format.
144*/
145typedef struct PageRecord PageRecord;
146struct PageRecord {
147 Pgno pgno; /* The page number */
148 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
149};
150
151/*
drh5e00f6c2001-09-13 13:46:56 +0000152** Journal files begin with the following magic string. The data
153** was obtained from /dev/random. It is used only as a sanity check.
drhd9b02572001-04-15 00:37:09 +0000154*/
155static const unsigned char aJournalMagic[] = {
156 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000157};
158
159/*
160** Hash a page number
161*/
drhd9b02572001-04-15 00:37:09 +0000162#define pager_hash(PN) ((PN)%N_PG_HASH)
drhed7c8552001-04-11 14:29:21 +0000163
164/*
drhdd793422001-06-28 01:54:48 +0000165** Enable reference count tracking here:
166*/
167#if SQLITE_TEST
drh5e00f6c2001-09-13 13:46:56 +0000168 int pager_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000169 static void pager_refinfo(PgHdr *p){
170 static int cnt = 0;
171 if( !pager_refinfo_enable ) return;
172 printf(
173 "REFCNT: %4d addr=0x%08x nRef=%d\n",
174 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
175 );
176 cnt++; /* Something to set a breakpoint on */
177 }
178# define REFINFO(X) pager_refinfo(X)
179#else
180# define REFINFO(X)
181#endif
182
183/*
drhd9b02572001-04-15 00:37:09 +0000184** Convert the bits in the pPager->errMask into an approprate
185** return code.
186*/
187static int pager_errcode(Pager *pPager){
188 int rc = SQLITE_OK;
189 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000190 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000191 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
192 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
193 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
194 return rc;
drhed7c8552001-04-11 14:29:21 +0000195}
196
197/*
198** Find a page in the hash table given its page number. Return
199** a pointer to the page or NULL if not found.
200*/
drhd9b02572001-04-15 00:37:09 +0000201static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drhed7c8552001-04-11 14:29:21 +0000202 PgHdr *p = pPager->aHash[pgno % N_PG_HASH];
203 while( p && p->pgno!=pgno ){
204 p = p->pNextHash;
205 }
206 return p;
207}
208
209/*
210** Unlock the database and clear the in-memory cache. This routine
211** sets the state of the pager back to what it was when it was first
212** opened. Any outstanding pages are invalidated and subsequent attempts
213** to access those pages will likely result in a coredump.
214*/
drhd9b02572001-04-15 00:37:09 +0000215static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000216 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000217 for(pPg=pPager->pAll; pPg; pPg=pNext){
218 pNext = pPg->pNextAll;
219 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000220 }
221 pPager->pFirst = 0;
drhd9b02572001-04-15 00:37:09 +0000222 pPager->pLast = 0;
223 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000224 memset(pPager->aHash, 0, sizeof(pPager->aHash));
225 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000226 if( pPager->state>=SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000227 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000228 }
drha7fcb052001-12-14 15:09:55 +0000229 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000230 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000231 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000232 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000233 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000234}
235
236/*
237** When this routine is called, the pager has the journal file open and
238** a write lock on the database. This routine releases the database
239** write lock and acquires a read lock in its place. The journal file
240** is deleted and closed.
drhed7c8552001-04-11 14:29:21 +0000241*/
drhd9b02572001-04-15 00:37:09 +0000242static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000243 int rc;
drhd9b02572001-04-15 00:37:09 +0000244 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000245 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh663fc632002-02-02 18:49:19 +0000246 sqlitepager_ckpt_commit(pPager);
drh0f892532002-05-30 12:27:03 +0000247 if( pPager->ckptOpen ){
248 sqliteOsClose(&pPager->cpfd);
249 pPager->ckptOpen = 0;
250 }
drha7fcb052001-12-14 15:09:55 +0000251 sqliteOsClose(&pPager->jfd);
drh8cfbf082001-09-19 13:22:39 +0000252 pPager->journalOpen = 0;
253 sqliteOsDelete(pPager->zJournal);
drha7fcb052001-12-14 15:09:55 +0000254 rc = sqliteOsReadLock(&pPager->fd);
255 assert( rc==SQLITE_OK );
drh6019e162001-07-02 17:51:45 +0000256 sqliteFree( pPager->aInJournal );
257 pPager->aInJournal = 0;
drhd9b02572001-04-15 00:37:09 +0000258 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
259 pPg->inJournal = 0;
260 pPg->dirty = 0;
261 }
drha7fcb052001-12-14 15:09:55 +0000262 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +0000263 return rc;
264}
265
drhed7c8552001-04-11 14:29:21 +0000266/*
drhfa86c412002-02-02 15:01:15 +0000267** Read a single page from the journal file opened on file descriptor
268** jfd. Playback this one page.
269*/
270static int pager_playback_one_page(Pager *pPager, OsFile *jfd){
271 int rc;
272 PgHdr *pPg; /* An existing page in the cache */
273 PageRecord pgRec;
274
drh663fc632002-02-02 18:49:19 +0000275 rc = sqliteOsRead(jfd, &pgRec, sizeof(pgRec));
drhfa86c412002-02-02 15:01:15 +0000276 if( rc!=SQLITE_OK ) return rc;
277
278 /* Sanity checking on the page */
279 if( pgRec.pgno>pPager->dbSize || pgRec.pgno==0 ) return SQLITE_CORRUPT;
280
281 /* Playback the page. Update the in-memory copy of the page
282 ** at the same time, if there is one.
283 */
284 pPg = pager_lookup(pPager, pgRec.pgno);
285 if( pPg ){
286 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
287 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
288 }
289 rc = sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE);
290 if( rc==SQLITE_OK ){
291 rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
292 }
293 return rc;
294}
295
296/*
drhed7c8552001-04-11 14:29:21 +0000297** Playback the journal and thus restore the database file to
298** the state it was in before we started making changes.
299**
drhd9b02572001-04-15 00:37:09 +0000300** The journal file format is as follows: There is an initial
301** file-type string for sanity checking. Then there is a single
302** Pgno number which is the number of pages in the database before
303** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000304** Next come zero or more page records where each page record
305** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
306** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000307**
drhd9b02572001-04-15 00:37:09 +0000308** If the file opened as the journal file is not a well-formed
309** journal file (as determined by looking at the magic number
310** at the beginning) then this routine returns SQLITE_PROTOCOL.
311** If any other errors occur during playback, the database will
312** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
313** pPager->errMask and SQLITE_CORRUPT is returned. If it all
314** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000315*/
drhd9b02572001-04-15 00:37:09 +0000316static int pager_playback(Pager *pPager){
317 int nRec; /* Number of Records */
318 int i; /* Loop counter */
319 Pgno mxPg = 0; /* Size of the original file in pages */
drhd9b02572001-04-15 00:37:09 +0000320 unsigned char aMagic[sizeof(aJournalMagic)];
drhed7c8552001-04-11 14:29:21 +0000321 int rc;
322
drhc3a64ba2001-11-22 00:01:27 +0000323 /* Figure out how many records are in the journal. Abort early if
324 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000325 */
drh8cfbf082001-09-19 13:22:39 +0000326 assert( pPager->journalOpen );
drha7fcb052001-12-14 15:09:55 +0000327 sqliteOsSeek(&pPager->jfd, 0);
328 rc = sqliteOsFileSize(&pPager->jfd, &nRec);
drhc3a64ba2001-11-22 00:01:27 +0000329 if( rc!=SQLITE_OK ){
330 goto end_playback;
331 }
332 nRec = (nRec - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord);
333 if( nRec<=0 ){
334 goto end_playback;
335 }
336
337 /* Read the beginning of the journal and truncate the
338 ** database file back to its original size.
339 */
drha7fcb052001-12-14 15:09:55 +0000340 rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drhd9b02572001-04-15 00:37:09 +0000341 if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){
drh81a20f22001-10-12 17:30:04 +0000342 rc = SQLITE_PROTOCOL;
343 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000344 }
drha7fcb052001-12-14 15:09:55 +0000345 rc = sqliteOsRead(&pPager->jfd, &mxPg, sizeof(mxPg));
drhd9b02572001-04-15 00:37:09 +0000346 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000347 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000348 }
drha7fcb052001-12-14 15:09:55 +0000349 rc = sqliteOsTruncate(&pPager->fd, mxPg*SQLITE_PAGE_SIZE);
drh81a20f22001-10-12 17:30:04 +0000350 if( rc!=SQLITE_OK ){
351 goto end_playback;
352 }
drhd9b02572001-04-15 00:37:09 +0000353 pPager->dbSize = mxPg;
354
drhfa86c412002-02-02 15:01:15 +0000355 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000356 */
drhd9b02572001-04-15 00:37:09 +0000357 for(i=nRec-1; i>=0; i--){
drhfa86c412002-02-02 15:01:15 +0000358 rc = pager_playback_one_page(pPager, &pPager->jfd);
drhd9b02572001-04-15 00:37:09 +0000359 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000360 }
drh81a20f22001-10-12 17:30:04 +0000361
362end_playback:
drhd9b02572001-04-15 00:37:09 +0000363 if( rc!=SQLITE_OK ){
364 pager_unwritelock(pPager);
365 pPager->errMask |= PAGER_ERR_CORRUPT;
366 rc = SQLITE_CORRUPT;
367 }else{
368 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000369 }
drhd9b02572001-04-15 00:37:09 +0000370 return rc;
drhed7c8552001-04-11 14:29:21 +0000371}
372
373/*
drhfa86c412002-02-02 15:01:15 +0000374** Playback the checkpoint journal.
375**
376** This is similar to playing back the transaction journal but with
377** a few extra twists.
378**
drh663fc632002-02-02 18:49:19 +0000379** (1) The number of pages in the database file at the start of
380** the checkpoint is stored in pPager->ckptSize, not in the
381** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000382**
383** (2) In addition to playing back the checkpoint journal, also
384** playback all pages of the transaction journal beginning
385** at offset pPager->ckptJSize.
386*/
387static int pager_ckpt_playback(Pager *pPager){
388 int nRec; /* Number of Records */
389 int i; /* Loop counter */
390 int rc;
391
392 /* Truncate the database back to its original size.
393 */
drh663fc632002-02-02 18:49:19 +0000394 rc = sqliteOsTruncate(&pPager->fd, pPager->ckptSize*SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000395 pPager->dbSize = pPager->ckptSize;
396
397 /* Figure out how many records are in the checkpoint journal.
398 */
drh0f892532002-05-30 12:27:03 +0000399 assert( pPager->ckptInUse && pPager->journalOpen );
drhfa86c412002-02-02 15:01:15 +0000400 sqliteOsSeek(&pPager->cpfd, 0);
401 rc = sqliteOsFileSize(&pPager->cpfd, &nRec);
402 if( rc!=SQLITE_OK ){
403 goto end_ckpt_playback;
404 }
405 nRec /= sizeof(PageRecord);
406
407 /* Copy original pages out of the checkpoint journal and back into the
408 ** database file.
409 */
410 for(i=nRec-1; i>=0; i--){
411 rc = pager_playback_one_page(pPager, &pPager->cpfd);
412 if( rc!=SQLITE_OK ) goto end_ckpt_playback;
413 }
414
415 /* Figure out how many pages need to be copied out of the transaction
416 ** journal.
417 */
418 rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize);
419 if( rc!=SQLITE_OK ){
420 goto end_ckpt_playback;
421 }
422 rc = sqliteOsFileSize(&pPager->jfd, &nRec);
423 if( rc!=SQLITE_OK ){
424 goto end_ckpt_playback;
425 }
426 nRec = (nRec - pPager->ckptJSize)/sizeof(PageRecord);
427 for(i=nRec-1; i>=0; i--){
428 rc = pager_playback_one_page(pPager, &pPager->jfd);
429 if( rc!=SQLITE_OK ) goto end_ckpt_playback;
430 }
431
432
433end_ckpt_playback:
drhfa86c412002-02-02 15:01:15 +0000434 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000435 pPager->errMask |= PAGER_ERR_CORRUPT;
436 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000437 }
438 return rc;
439}
440
441/*
drhf57b14a2001-09-14 18:54:08 +0000442** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000443**
444** The maximum number is the absolute value of the mxPage parameter.
445** If mxPage is negative, the noSync flag is also set. noSync bypasses
446** calls to sqliteOsSync(). The pager runs much faster with noSync on,
447** but if the operating system crashes or there is an abrupt power
448** failure, the database file might be left in an inconsistent and
449** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000450*/
451void sqlitepager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000452 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000453 pPager->noSync = pPager->tempFile;
drh603240c2002-03-05 01:11:12 +0000454 }else{
455 pPager->noSync = 1;
456 mxPage = -mxPage;
457 }
drhf57b14a2001-09-14 18:54:08 +0000458 if( mxPage>10 ){
459 pPager->mxPage = mxPage;
460 }
461}
462
463/*
drhfa86c412002-02-02 15:01:15 +0000464** Open a temporary file. Write the name of the file into zName
465** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
466** the file descriptor into *fd. Return SQLITE_OK on success or some
467** other error code if we fail.
468**
469** The OS will automatically delete the temporary file when it is
470** closed.
471*/
472static int sqlitepager_opentemp(char *zFile, OsFile *fd){
473 int cnt = 8;
474 int rc;
475 do{
476 cnt--;
477 sqliteOsTempFileName(zFile);
478 rc = sqliteOsOpenExclusive(zFile, fd, 1);
479 }while( cnt>0 && rc!=SQLITE_OK );
480 return rc;
481}
482
483/*
drhed7c8552001-04-11 14:29:21 +0000484** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000485** The file to be cached need not exist. The file is not locked until
drhd9b02572001-04-15 00:37:09 +0000486** the first call to sqlitepager_get() and is only held open until the
487** last page is released using sqlitepager_unref().
drh382c0242001-10-06 16:33:02 +0000488**
drh6446c4d2001-12-15 14:22:18 +0000489** If zFilename is NULL then a randomly-named temporary file is created
490** and used as the file to be cached. The file will be deleted
491** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000492*/
drh7e3b0a02001-04-28 16:52:40 +0000493int sqlitepager_open(
494 Pager **ppPager, /* Return the Pager structure here */
495 const char *zFilename, /* Name of the database file to open */
496 int mxPage, /* Max number of in-memory cache pages */
497 int nExtra /* Extra bytes append to each in-memory page */
498){
drhed7c8552001-04-11 14:29:21 +0000499 Pager *pPager;
500 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000501 OsFile fd;
502 int rc;
drh5e00f6c2001-09-13 13:46:56 +0000503 int tempFile;
504 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000505 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000506
drhd9b02572001-04-15 00:37:09 +0000507 *ppPager = 0;
508 if( sqlite_malloc_failed ){
509 return SQLITE_NOMEM;
510 }
drh5e00f6c2001-09-13 13:46:56 +0000511 if( zFilename ){
drh8cfbf082001-09-19 13:22:39 +0000512 rc = sqliteOsOpenReadWrite(zFilename, &fd, &readOnly);
drh5e00f6c2001-09-13 13:46:56 +0000513 tempFile = 0;
514 }else{
drhfa86c412002-02-02 15:01:15 +0000515 rc = sqlitepager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000516 zFilename = zTemp;
517 tempFile = 1;
518 }
drh8cfbf082001-09-19 13:22:39 +0000519 if( rc!=SQLITE_OK ){
drhed7c8552001-04-11 14:29:21 +0000520 return SQLITE_CANTOPEN;
521 }
522 nameLen = strlen(zFilename);
523 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000524 if( pPager==0 ){
drha7fcb052001-12-14 15:09:55 +0000525 sqliteOsClose(&fd);
drhd9b02572001-04-15 00:37:09 +0000526 return SQLITE_NOMEM;
527 }
drhed7c8552001-04-11 14:29:21 +0000528 pPager->zFilename = (char*)&pPager[1];
529 pPager->zJournal = &pPager->zFilename[nameLen+1];
530 strcpy(pPager->zFilename, zFilename);
531 strcpy(pPager->zJournal, zFilename);
532 strcpy(&pPager->zJournal[nameLen], "-journal");
533 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +0000534 pPager->journalOpen = 0;
drhfa86c412002-02-02 15:01:15 +0000535 pPager->ckptOpen = 0;
drh0f892532002-05-30 12:27:03 +0000536 pPager->ckptInUse = 0;
drhed7c8552001-04-11 14:29:21 +0000537 pPager->nRef = 0;
538 pPager->dbSize = -1;
drhfa86c412002-02-02 15:01:15 +0000539 pPager->ckptSize = 0;
540 pPager->ckptJSize = 0;
drhed7c8552001-04-11 14:29:21 +0000541 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000542 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000543 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000544 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +0000545 pPager->tempFile = tempFile;
546 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +0000547 pPager->needSync = 0;
drha1680452002-04-18 01:56:57 +0000548 pPager->noSync = pPager->tempFile;
drhed7c8552001-04-11 14:29:21 +0000549 pPager->pFirst = 0;
550 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +0000551 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +0000552 memset(pPager->aHash, 0, sizeof(pPager->aHash));
553 *ppPager = pPager;
554 return SQLITE_OK;
555}
556
557/*
drh72f82862001-05-24 21:06:34 +0000558** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +0000559** when the reference count on each page reaches zero. The destructor can
560** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +0000561**
562** The destructor is not called as a result sqlitepager_close().
563** Destructors are only called by sqlitepager_unref().
564*/
565void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
566 pPager->xDestructor = xDesc;
567}
568
569/*
drh5e00f6c2001-09-13 13:46:56 +0000570** Return the total number of pages in the disk file associated with
571** pPager.
drhed7c8552001-04-11 14:29:21 +0000572*/
drhd9b02572001-04-15 00:37:09 +0000573int sqlitepager_pagecount(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000574 int n;
drhd9b02572001-04-15 00:37:09 +0000575 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000576 if( pPager->dbSize>=0 ){
577 return pPager->dbSize;
578 }
drha7fcb052001-12-14 15:09:55 +0000579 if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000580 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +0000581 return 0;
drhed7c8552001-04-11 14:29:21 +0000582 }
drh8cfbf082001-09-19 13:22:39 +0000583 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +0000584 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000585 pPager->dbSize = n;
586 }
587 return n;
588}
589
590/*
591** Shutdown the page cache. Free all memory and close all files.
592**
593** If a transaction was in progress when this routine is called, that
594** transaction is rolled back. All outstanding pages are invalidated
595** and their memory is freed. Any attempt to use a page associated
596** with this page cache after this function returns will likely
597** result in a coredump.
598*/
drhd9b02572001-04-15 00:37:09 +0000599int sqlitepager_close(Pager *pPager){
600 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000601 switch( pPager->state ){
602 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000603 sqlitepager_rollback(pPager);
drha7fcb052001-12-14 15:09:55 +0000604 sqliteOsUnlock(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000605 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000606 break;
607 }
608 case SQLITE_READLOCK: {
drha7fcb052001-12-14 15:09:55 +0000609 sqliteOsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000610 break;
611 }
612 default: {
613 /* Do nothing */
614 break;
615 }
616 }
drhd9b02572001-04-15 00:37:09 +0000617 for(pPg=pPager->pAll; pPg; pPg=pNext){
618 pNext = pPg->pNextAll;
619 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000620 }
drha7fcb052001-12-14 15:09:55 +0000621 sqliteOsClose(&pPager->fd);
drh8cfbf082001-09-19 13:22:39 +0000622 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +0000623 /* Temp files are automatically deleted by the OS
624 ** if( pPager->tempFile ){
625 ** sqliteOsDelete(pPager->zFilename);
626 ** }
627 */
drhed7c8552001-04-11 14:29:21 +0000628 sqliteFree(pPager);
629 return SQLITE_OK;
630}
631
632/*
drh5e00f6c2001-09-13 13:46:56 +0000633** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +0000634*/
drhd9b02572001-04-15 00:37:09 +0000635Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000636 PgHdr *p = DATA_TO_PGHDR(pData);
637 return p->pgno;
638}
639
640/*
drh7e3b0a02001-04-28 16:52:40 +0000641** Increment the reference count for a page. If the page is
642** currently on the freelist (the reference count is zero) then
643** remove it from the freelist.
644*/
drhdf0b3b02001-06-23 11:36:20 +0000645static void page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +0000646 if( pPg->nRef==0 ){
647 /* The page is currently on the freelist. Remove it. */
648 if( pPg->pPrevFree ){
649 pPg->pPrevFree->pNextFree = pPg->pNextFree;
650 }else{
651 pPg->pPager->pFirst = pPg->pNextFree;
652 }
653 if( pPg->pNextFree ){
654 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
655 }else{
656 pPg->pPager->pLast = pPg->pPrevFree;
657 }
658 pPg->pPager->nRef++;
659 }
660 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +0000661 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +0000662}
663
664/*
665** Increment the reference count for a page. The input pointer is
666** a reference to the page data.
667*/
668int sqlitepager_ref(void *pData){
669 PgHdr *pPg = DATA_TO_PGHDR(pData);
670 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +0000671 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000672}
673
674/*
drhb19a2bc2001-09-16 00:13:26 +0000675** Sync the journal and then write all free dirty pages to the database
676** file.
677**
678** Writing all free dirty pages to the database after the sync is a
679** non-obvious optimization. fsync() is an expensive operation so we
drhaaab5722002-02-19 13:39:21 +0000680** want to minimize the number ot times it is called. After an fsync() call,
drh6446c4d2001-12-15 14:22:18 +0000681** we are free to write dirty pages back to the database. It is best
682** to go ahead and write as many dirty pages as possible to minimize
683** the risk of having to do another fsync() later on. Writing dirty
684** free pages in this way was observed to make database operations go
685** up to 10 times faster.
drhfa86c412002-02-02 15:01:15 +0000686**
687** If we are writing to temporary database, there is no need to preserve
688** the integrity of the journal file, so we can save time and skip the
689** fsync().
drh50e5dad2001-09-15 00:57:28 +0000690*/
691static int syncAllPages(Pager *pPager){
692 PgHdr *pPg;
693 int rc = SQLITE_OK;
694 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +0000695 if( !pPager->tempFile ){
696 rc = sqliteOsSync(&pPager->jfd);
697 if( rc!=0 ) return rc;
698 }
drh50e5dad2001-09-15 00:57:28 +0000699 pPager->needSync = 0;
700 }
701 for(pPg=pPager->pFirst; pPg; pPg=pPg->pNextFree){
702 if( pPg->dirty ){
drha7fcb052001-12-14 15:09:55 +0000703 sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
704 rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh50e5dad2001-09-15 00:57:28 +0000705 if( rc!=SQLITE_OK ) break;
706 pPg->dirty = 0;
707 }
708 }
drh81a20f22001-10-12 17:30:04 +0000709 return rc;
drh50e5dad2001-09-15 00:57:28 +0000710}
711
712/*
drhd9b02572001-04-15 00:37:09 +0000713** Acquire a page.
714**
drh58a11682001-11-10 13:51:08 +0000715** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +0000716** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +0000717**
drh306dc212001-05-21 13:45:10 +0000718** A _get works for any page number greater than 0. If the database
719** file is smaller than the requested page, then no actual disk
720** read occurs and the memory image of the page is initialized to
721** all zeros. The extra data appended to a page is always initialized
722** to zeros the first time a page is loaded into memory.
723**
drhd9b02572001-04-15 00:37:09 +0000724** The acquisition might fail for several reasons. In all cases,
725** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +0000726**
727** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
728** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +0000729** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +0000730** just returns 0. This routine acquires a read-lock the first time it
731** has to go to disk, and could also playback an old journal if necessary.
732** Since _lookup() never goes to disk, it never has to deal with locks
733** or journal files.
drhed7c8552001-04-11 14:29:21 +0000734*/
drhd9b02572001-04-15 00:37:09 +0000735int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +0000736 PgHdr *pPg;
737
drhd9b02572001-04-15 00:37:09 +0000738 /* Make sure we have not hit any critical errors.
739 */
740 if( pPager==0 || pgno==0 ){
741 return SQLITE_ERROR;
742 }
743 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
744 return pager_errcode(pPager);
745 }
746
drhed7c8552001-04-11 14:29:21 +0000747 /* If this is the first page accessed, then get a read lock
748 ** on the database file.
749 */
750 if( pPager->nRef==0 ){
drha7fcb052001-12-14 15:09:55 +0000751 if( sqliteOsReadLock(&pPager->fd)!=SQLITE_OK ){
drhed7c8552001-04-11 14:29:21 +0000752 *ppPage = 0;
753 return SQLITE_BUSY;
754 }
drhd9b02572001-04-15 00:37:09 +0000755 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +0000756
757 /* If a journal file exists, try to play it back.
758 */
drh8cfbf082001-09-19 13:22:39 +0000759 if( sqliteOsFileExists(pPager->zJournal) ){
drhf57b3392001-10-08 13:22:32 +0000760 int rc, dummy;
drhed7c8552001-04-11 14:29:21 +0000761
drha7fcb052001-12-14 15:09:55 +0000762 /* Get a write lock on the database
763 */
764 rc = sqliteOsWriteLock(&pPager->fd);
765 if( rc!=SQLITE_OK ){
drh6446c4d2001-12-15 14:22:18 +0000766 rc = sqliteOsUnlock(&pPager->fd);
drha7fcb052001-12-14 15:09:55 +0000767 assert( rc==SQLITE_OK );
768 *ppPage = 0;
769 return SQLITE_BUSY;
770 }
771 pPager->state = SQLITE_WRITELOCK;
772
drhed7c8552001-04-11 14:29:21 +0000773 /* Open the journal for exclusive access. Return SQLITE_BUSY if
drhf57b3392001-10-08 13:22:32 +0000774 ** we cannot get exclusive access to the journal file.
775 **
776 ** Even though we will only be reading from the journal, not writing,
777 ** we have to open the journal for writing in order to obtain an
778 ** exclusive access lock.
drhed7c8552001-04-11 14:29:21 +0000779 */
drhf57b3392001-10-08 13:22:32 +0000780 rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy);
drha7fcb052001-12-14 15:09:55 +0000781 if( rc!=SQLITE_OK ){
782 rc = sqliteOsUnlock(&pPager->fd);
783 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +0000784 *ppPage = 0;
785 return SQLITE_BUSY;
786 }
drha7fcb052001-12-14 15:09:55 +0000787 pPager->journalOpen = 1;
drhed7c8552001-04-11 14:29:21 +0000788
789 /* Playback and delete the journal. Drop the database write
790 ** lock and reacquire the read lock.
791 */
drhd9b02572001-04-15 00:37:09 +0000792 rc = pager_playback(pPager);
793 if( rc!=SQLITE_OK ){
794 return rc;
795 }
drhed7c8552001-04-11 14:29:21 +0000796 }
797 pPg = 0;
798 }else{
799 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +0000800 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +0000801 }
802 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +0000803 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +0000804 int h;
drh7e3b0a02001-04-28 16:52:40 +0000805 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +0000806 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
807 /* Create a new page */
drh7e3b0a02001-04-28 16:52:40 +0000808 pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +0000809 if( pPg==0 ){
810 *ppPage = 0;
811 pager_unwritelock(pPager);
812 pPager->errMask |= PAGER_ERR_MEM;
813 return SQLITE_NOMEM;
814 }
drhed7c8552001-04-11 14:29:21 +0000815 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +0000816 pPg->pNextAll = pPager->pAll;
817 if( pPager->pAll ){
818 pPager->pAll->pPrevAll = pPg;
819 }
820 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +0000821 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +0000822 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +0000823 }else{
drhd9b02572001-04-15 00:37:09 +0000824 /* Recycle an older page. First locate the page to be recycled.
825 ** Try to find one that is not dirty and is near the head of
826 ** of the free list */
drhed7c8552001-04-11 14:29:21 +0000827 pPg = pPager->pFirst;
drh603240c2002-03-05 01:11:12 +0000828 while( pPg && pPg->dirty ){
drhd9b02572001-04-15 00:37:09 +0000829 pPg = pPg->pNextFree;
830 }
drhb19a2bc2001-09-16 00:13:26 +0000831
832 /* If we could not find a page that has not been used recently
833 ** and which is not dirty, then sync the journal and write all
834 ** dirty free pages into the database file, thus making them
835 ** clean pages and available for recycling.
836 **
837 ** We have to sync the journal before writing a page to the main
838 ** database. But syncing is a very slow operation. So after a
839 ** sync, it is best to write everything we can back to the main
840 ** database to minimize the risk of having to sync again in the
841 ** near future. That is way we write all dirty pages after a
842 ** sync.
843 */
drh603240c2002-03-05 01:11:12 +0000844 if( pPg==0 ){
drh50e5dad2001-09-15 00:57:28 +0000845 int rc = syncAllPages(pPager);
846 if( rc!=0 ){
847 sqlitepager_rollback(pPager);
848 *ppPage = 0;
849 return SQLITE_IOERR;
850 }
851 pPg = pPager->pFirst;
852 }
drhd9b02572001-04-15 00:37:09 +0000853 assert( pPg->nRef==0 );
drh50e5dad2001-09-15 00:57:28 +0000854 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +0000855
856 /* Unlink the old page from the free list and the hash table
857 */
drh6019e162001-07-02 17:51:45 +0000858 if( pPg->pPrevFree ){
859 pPg->pPrevFree->pNextFree = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +0000860 }else{
drh6019e162001-07-02 17:51:45 +0000861 assert( pPager->pFirst==pPg );
862 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +0000863 }
drh6019e162001-07-02 17:51:45 +0000864 if( pPg->pNextFree ){
865 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
866 }else{
867 assert( pPager->pLast==pPg );
868 pPager->pLast = pPg->pPrevFree;
869 }
870 pPg->pNextFree = pPg->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +0000871 if( pPg->pNextHash ){
872 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
873 }
874 if( pPg->pPrevHash ){
875 pPg->pPrevHash->pNextHash = pPg->pNextHash;
876 }else{
drhd9b02572001-04-15 00:37:09 +0000877 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +0000878 assert( pPager->aHash[h]==pPg );
879 pPager->aHash[h] = pPg->pNextHash;
880 }
drh6019e162001-07-02 17:51:45 +0000881 pPg->pNextHash = pPg->pPrevHash = 0;
drhd9b02572001-04-15 00:37:09 +0000882 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +0000883 }
884 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +0000885 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
drh6019e162001-07-02 17:51:45 +0000886 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
887 }else{
888 pPg->inJournal = 0;
889 }
drh663fc632002-02-02 18:49:19 +0000890 if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize ){
drhfa86c412002-02-02 15:01:15 +0000891 pPg->inCkpt = (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0;
892 }else{
893 pPg->inCkpt = 0;
894 }
drhed7c8552001-04-11 14:29:21 +0000895 pPg->dirty = 0;
896 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +0000897 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +0000898 pPager->nRef++;
899 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +0000900 pPg->pNextHash = pPager->aHash[h];
901 pPager->aHash[h] = pPg;
902 if( pPg->pNextHash ){
903 assert( pPg->pNextHash->pPrevHash==0 );
904 pPg->pNextHash->pPrevHash = pPg;
905 }
drh306dc212001-05-21 13:45:10 +0000906 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
drh1ab43002002-01-14 09:28:19 +0000907 if( pPager->dbSize<(int)pgno ){
drh306dc212001-05-21 13:45:10 +0000908 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
909 }else{
drh81a20f22001-10-12 17:30:04 +0000910 int rc;
drha7fcb052001-12-14 15:09:55 +0000911 sqliteOsSeek(&pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE);
912 rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh81a20f22001-10-12 17:30:04 +0000913 if( rc!=SQLITE_OK ){
914 return rc;
915 }
drh306dc212001-05-21 13:45:10 +0000916 }
drh7e3b0a02001-04-28 16:52:40 +0000917 if( pPager->nExtra>0 ){
918 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
919 }
drhed7c8552001-04-11 14:29:21 +0000920 }else{
drhd9b02572001-04-15 00:37:09 +0000921 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +0000922 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +0000923 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +0000924 }
925 *ppPage = PGHDR_TO_DATA(pPg);
926 return SQLITE_OK;
927}
928
929/*
drh7e3b0a02001-04-28 16:52:40 +0000930** Acquire a page if it is already in the in-memory cache. Do
931** not read the page from disk. Return a pointer to the page,
932** or 0 if the page is not in cache.
933**
934** See also sqlitepager_get(). The difference between this routine
935** and sqlitepager_get() is that _get() will go to the disk and read
936** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +0000937** returns NULL if the page is not in cache or if a disk I/O error
938** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +0000939*/
940void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
941 PgHdr *pPg;
942
943 /* Make sure we have not hit any critical errors.
944 */
945 if( pPager==0 || pgno==0 ){
946 return 0;
947 }
948 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
949 return 0;
950 }
951 if( pPager->nRef==0 ){
952 return 0;
953 }
954 pPg = pager_lookup(pPager, pgno);
955 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +0000956 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +0000957 return PGHDR_TO_DATA(pPg);
958}
959
960/*
drhed7c8552001-04-11 14:29:21 +0000961** Release a page.
962**
963** If the number of references to the page drop to zero, then the
964** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +0000965** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +0000966** removed.
967*/
drhd9b02572001-04-15 00:37:09 +0000968int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +0000969 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000970
971 /* Decrement the reference count for this page
972 */
drhed7c8552001-04-11 14:29:21 +0000973 pPg = DATA_TO_PGHDR(pData);
974 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +0000975 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +0000976 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +0000977
drh72f82862001-05-24 21:06:34 +0000978 /* When the number of references to a page reach 0, call the
979 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +0000980 */
drhed7c8552001-04-11 14:29:21 +0000981 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +0000982 Pager *pPager;
983 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +0000984 pPg->pNextFree = 0;
985 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +0000986 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +0000987 if( pPg->pPrevFree ){
988 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +0000989 }else{
990 pPager->pFirst = pPg;
991 }
drh72f82862001-05-24 21:06:34 +0000992 if( pPager->xDestructor ){
993 pPager->xDestructor(pData);
994 }
drhd9b02572001-04-15 00:37:09 +0000995
996 /* When all pages reach the freelist, drop the read lock from
997 ** the database file.
998 */
999 pPager->nRef--;
1000 assert( pPager->nRef>=0 );
1001 if( pPager->nRef==0 ){
1002 pager_reset(pPager);
1003 }
drhed7c8552001-04-11 14:29:21 +00001004 }
drhd9b02572001-04-15 00:37:09 +00001005 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001006}
1007
1008/*
drh4b845d72002-03-05 12:41:19 +00001009** Acquire a write-lock on the database. The lock is removed when
1010** the any of the following happen:
1011**
1012** * sqlitepager_commit() is called.
1013** * sqlitepager_rollback() is called.
1014** * sqlitepager_close() is called.
1015** * sqlitepager_unref() is called to on every outstanding page.
1016**
1017** The parameter to this routine is a pointer to any open page of the
1018** database file. Nothing changes about the page - it is used merely
1019** to acquire a pointer to the Pager structure and as proof that there
1020** is already a read-lock on the database.
1021**
1022** If the database is already write-locked, this routine is a no-op.
1023*/
1024int sqlitepager_begin(void *pData){
1025 PgHdr *pPg = DATA_TO_PGHDR(pData);
1026 Pager *pPager = pPg->pPager;
1027 int rc = SQLITE_OK;
1028 assert( pPg->nRef>0 );
1029 assert( pPager->state!=SQLITE_UNLOCK );
1030 if( pPager->state==SQLITE_READLOCK ){
1031 assert( pPager->aInJournal==0 );
1032 rc = sqliteOsWriteLock(&pPager->fd);
1033 if( rc!=SQLITE_OK ){
1034 return rc;
1035 }
1036 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1037 if( pPager->aInJournal==0 ){
1038 sqliteOsReadLock(&pPager->fd);
1039 return SQLITE_NOMEM;
1040 }
1041 rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd, 0);
1042 if( rc!=SQLITE_OK ){
1043 sqliteFree(pPager->aInJournal);
1044 pPager->aInJournal = 0;
1045 sqliteOsReadLock(&pPager->fd);
1046 return SQLITE_CANTOPEN;
1047 }
1048 pPager->journalOpen = 1;
drha1680452002-04-18 01:56:57 +00001049 pPager->needSync = 0;
1050 pPager->dirtyFile = 0;
drh4b845d72002-03-05 12:41:19 +00001051 pPager->state = SQLITE_WRITELOCK;
1052 sqlitepager_pagecount(pPager);
1053 pPager->origDbSize = pPager->dbSize;
1054 rc = sqliteOsWrite(&pPager->jfd, aJournalMagic, sizeof(aJournalMagic));
1055 if( rc==SQLITE_OK ){
1056 rc = sqliteOsWrite(&pPager->jfd, &pPager->dbSize, sizeof(Pgno));
1057 }
1058 if( rc!=SQLITE_OK ){
1059 rc = pager_unwritelock(pPager);
1060 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
1061 }
1062 }
1063 return rc;
1064}
1065
1066/*
drhed7c8552001-04-11 14:29:21 +00001067** Mark a data page as writeable. The page is written into the journal
1068** if it is not there already. This routine must be called before making
1069** changes to a page.
1070**
1071** The first time this routine is called, the pager creates a new
1072** journal and acquires a write lock on the database. If the write
1073** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001074** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001075** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001076**
1077** If the journal file could not be written because the disk is full,
1078** then this routine returns SQLITE_FULL and does an immediate rollback.
1079** All subsequent write attempts also return SQLITE_FULL until there
1080** is a call to sqlitepager_commit() or sqlitepager_rollback() to
1081** reset.
drhed7c8552001-04-11 14:29:21 +00001082*/
drhd9b02572001-04-15 00:37:09 +00001083int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001084 PgHdr *pPg = DATA_TO_PGHDR(pData);
1085 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001086 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001087
drh6446c4d2001-12-15 14:22:18 +00001088 /* Check for errors
1089 */
drhd9b02572001-04-15 00:37:09 +00001090 if( pPager->errMask ){
1091 return pager_errcode(pPager);
1092 }
drh5e00f6c2001-09-13 13:46:56 +00001093 if( pPager->readOnly ){
1094 return SQLITE_PERM;
1095 }
drh6446c4d2001-12-15 14:22:18 +00001096
1097 /* Mark the page as dirty. If the page has already been written
1098 ** to the journal then we can return right away.
1099 */
drhd9b02572001-04-15 00:37:09 +00001100 pPg->dirty = 1;
drh0f892532002-05-30 12:27:03 +00001101 if( pPg->inJournal && (pPg->inCkpt || pPager->ckptInUse==0) ){
drha1680452002-04-18 01:56:57 +00001102 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001103 return SQLITE_OK;
1104 }
drh6446c4d2001-12-15 14:22:18 +00001105
1106 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001107 ** written to the transaction journal or the ckeckpoint journal
1108 ** or both.
1109 **
1110 ** First check to see that the transaction journal exists and
1111 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001112 */
drhd9b02572001-04-15 00:37:09 +00001113 assert( pPager->state!=SQLITE_UNLOCK );
drh4b845d72002-03-05 12:41:19 +00001114 rc = sqlitepager_begin(pData);
drha1680452002-04-18 01:56:57 +00001115 pPager->dirtyFile = 1;
drh4b845d72002-03-05 12:41:19 +00001116 if( rc!=SQLITE_OK ) return rc;
drhd9b02572001-04-15 00:37:09 +00001117 assert( pPager->state==SQLITE_WRITELOCK );
drh8cfbf082001-09-19 13:22:39 +00001118 assert( pPager->journalOpen );
drh6446c4d2001-12-15 14:22:18 +00001119
drhfa86c412002-02-02 15:01:15 +00001120 /* The transaction journal now exists and we have a write lock on the
1121 ** main database file. Write the current page to the transaction
1122 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001123 */
drhfa86c412002-02-02 15:01:15 +00001124 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
drha7fcb052001-12-14 15:09:55 +00001125 rc = sqliteOsWrite(&pPager->jfd, &pPg->pgno, sizeof(Pgno));
drhd9b02572001-04-15 00:37:09 +00001126 if( rc==SQLITE_OK ){
drha7fcb052001-12-14 15:09:55 +00001127 rc = sqliteOsWrite(&pPager->jfd, pData, SQLITE_PAGE_SIZE);
drhd9b02572001-04-15 00:37:09 +00001128 }
1129 if( rc!=SQLITE_OK ){
1130 sqlitepager_rollback(pPager);
1131 pPager->errMask |= PAGER_ERR_FULL;
1132 return rc;
1133 }
drh6019e162001-07-02 17:51:45 +00001134 assert( pPager->aInJournal!=0 );
1135 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh603240c2002-03-05 01:11:12 +00001136 pPager->needSync = !pPager->noSync;
drhfa86c412002-02-02 15:01:15 +00001137 pPg->inJournal = 1;
drh0f892532002-05-30 12:27:03 +00001138 if( pPager->ckptInUse ){
drhfa86c412002-02-02 15:01:15 +00001139 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1140 pPg->inCkpt = 1;
1141 }
drh69688d52001-04-14 16:38:23 +00001142 }
drh6446c4d2001-12-15 14:22:18 +00001143
drhfa86c412002-02-02 15:01:15 +00001144 /* If the checkpoint journal is open and the page is not in it,
1145 ** then write the current page to the checkpoint journal.
drh6446c4d2001-12-15 14:22:18 +00001146 */
drh0f892532002-05-30 12:27:03 +00001147 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh1e336b42002-02-14 12:50:33 +00001148 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhfa86c412002-02-02 15:01:15 +00001149 rc = sqliteOsWrite(&pPager->cpfd, &pPg->pgno, sizeof(Pgno));
1150 if( rc==SQLITE_OK ){
1151 rc = sqliteOsWrite(&pPager->cpfd, pData, SQLITE_PAGE_SIZE);
1152 }
1153 if( rc!=SQLITE_OK ){
1154 sqlitepager_rollback(pPager);
1155 pPager->errMask |= PAGER_ERR_FULL;
1156 return rc;
1157 }
1158 assert( pPager->aInCkpt!=0 );
1159 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1160 pPg->inCkpt = 1;
1161 }
1162
1163 /* Update the database size and return.
1164 */
drh1ab43002002-01-14 09:28:19 +00001165 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001166 pPager->dbSize = pPg->pgno;
1167 }
drh69688d52001-04-14 16:38:23 +00001168 return rc;
drhed7c8552001-04-11 14:29:21 +00001169}
1170
1171/*
drhaacc5432002-01-06 17:07:40 +00001172** Return TRUE if the page given in the argument was previously passed
drh6019e162001-07-02 17:51:45 +00001173** to sqlitepager_write(). In other words, return TRUE if it is ok
1174** to change the content of the page.
1175*/
1176int sqlitepager_iswriteable(void *pData){
1177 PgHdr *pPg = DATA_TO_PGHDR(pData);
1178 return pPg->dirty;
1179}
1180
1181/*
drh30e58752002-03-02 20:41:57 +00001182** A call to this routine tells the pager that it is not necessary to
1183** write the information on page "pgno" back to the disk, even though
1184** that page might be marked as dirty.
1185**
1186** The overlying software layer calls this routine when all of the data
1187** on the given page is unused. The pager marks the page as clean so
1188** that it does not get written to disk.
1189**
1190** Tests show that this optimization, together with the
1191** sqlitepager_dont_rollback() below, more than double the speed
1192** of large INSERT operations and quadruple the speed of large DELETEs.
1193*/
1194void sqlitepager_dont_write(Pager *pPager, Pgno pgno){
1195 PgHdr *pPg;
1196 pPg = pager_lookup(pPager, pgno);
1197 if( pPg && pPg->dirty ){
1198 pPg->dirty = 0;
1199 }
1200}
1201
1202/*
1203** A call to this routine tells the pager that if a rollback occurs,
1204** it is not necessary to restore the data on the given page. This
1205** means that the pager does not have to record the given page in the
1206** rollback journal.
1207*/
1208void sqlitepager_dont_rollback(void *pData){
1209 PgHdr *pPg = DATA_TO_PGHDR(pData);
1210 Pager *pPager = pPg->pPager;
1211
1212 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
1213 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
1214 assert( pPager->aInJournal!=0 );
1215 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1216 pPg->inJournal = 1;
drh0f892532002-05-30 12:27:03 +00001217 if( pPager->ckptInUse ){
drh30e58752002-03-02 20:41:57 +00001218 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1219 pPg->inCkpt = 1;
1220 }
1221 }
drh0f892532002-05-30 12:27:03 +00001222 if( pPager->ckptInUse && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){
drh30e58752002-03-02 20:41:57 +00001223 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
1224 assert( pPager->aInCkpt!=0 );
1225 pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1226 pPg->inCkpt = 1;
1227 }
1228}
1229
1230/*
drhed7c8552001-04-11 14:29:21 +00001231** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00001232**
1233** If the commit fails for any reason, a rollback attempt is made
1234** and an error code is returned. If the commit worked, SQLITE_OK
1235** is returned.
drhed7c8552001-04-11 14:29:21 +00001236*/
drhd9b02572001-04-15 00:37:09 +00001237int sqlitepager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00001238 int rc;
drhed7c8552001-04-11 14:29:21 +00001239 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001240
1241 if( pPager->errMask==PAGER_ERR_FULL ){
1242 rc = sqlitepager_rollback(pPager);
1243 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
1244 return rc;
1245 }
1246 if( pPager->errMask!=0 ){
1247 rc = pager_errcode(pPager);
1248 return rc;
1249 }
1250 if( pPager->state!=SQLITE_WRITELOCK ){
1251 return SQLITE_ERROR;
1252 }
drh8cfbf082001-09-19 13:22:39 +00001253 assert( pPager->journalOpen );
drha1680452002-04-18 01:56:57 +00001254 if( pPager->dirtyFile==0 ){
1255 /* Exit early (without doing the time-consuming sqliteOsSync() calls)
1256 ** if there have been no changes to the database file. */
1257 rc = pager_unwritelock(pPager);
1258 pPager->dbSize = -1;
1259 return rc;
1260 }
drha7fcb052001-12-14 15:09:55 +00001261 if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00001262 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00001263 }
drha1b351a2001-09-14 16:42:12 +00001264 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1265 if( pPg->dirty==0 ) continue;
drha7fcb052001-12-14 15:09:55 +00001266 rc = sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
drha1b351a2001-09-14 16:42:12 +00001267 if( rc!=SQLITE_OK ) goto commit_abort;
drha7fcb052001-12-14 15:09:55 +00001268 rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drha1b351a2001-09-14 16:42:12 +00001269 if( rc!=SQLITE_OK ) goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00001270 }
drh603240c2002-03-05 01:11:12 +00001271 if( !pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK ){
1272 goto commit_abort;
1273 }
drhd9b02572001-04-15 00:37:09 +00001274 rc = pager_unwritelock(pPager);
1275 pPager->dbSize = -1;
1276 return rc;
1277
1278 /* Jump here if anything goes wrong during the commit process.
1279 */
1280commit_abort:
1281 rc = sqlitepager_rollback(pPager);
1282 if( rc==SQLITE_OK ){
1283 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00001284 }
drhed7c8552001-04-11 14:29:21 +00001285 return rc;
1286}
1287
1288/*
1289** Rollback all changes. The database falls back to read-only mode.
1290** All in-memory cache pages revert to their original data contents.
1291** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00001292**
1293** This routine cannot fail unless some other process is not following
1294** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
1295** process is writing trash into the journal file (SQLITE_CORRUPT) or
1296** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1297** codes are returned for all these occasions. Otherwise,
1298** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001299*/
drhd9b02572001-04-15 00:37:09 +00001300int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001301 int rc;
drhd9b02572001-04-15 00:37:09 +00001302 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00001303 if( pPager->state>=SQLITE_WRITELOCK ){
1304 pager_playback(pPager);
1305 }
drhd9b02572001-04-15 00:37:09 +00001306 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001307 }
drhd9b02572001-04-15 00:37:09 +00001308 if( pPager->state!=SQLITE_WRITELOCK ){
1309 return SQLITE_OK;
1310 }
1311 rc = pager_playback(pPager);
1312 if( rc!=SQLITE_OK ){
1313 rc = SQLITE_CORRUPT;
1314 pPager->errMask |= PAGER_ERR_CORRUPT;
1315 }
1316 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001317 return rc;
drh98808ba2001-10-18 12:34:46 +00001318}
drhd9b02572001-04-15 00:37:09 +00001319
1320/*
drh5e00f6c2001-09-13 13:46:56 +00001321** Return TRUE if the database file is opened read-only. Return FALSE
1322** if the database is (in theory) writable.
1323*/
1324int sqlitepager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00001325 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00001326}
1327
1328/*
drhd9b02572001-04-15 00:37:09 +00001329** This routine is used for testing and analysis only.
1330*/
1331int *sqlitepager_stats(Pager *pPager){
1332 static int a[9];
1333 a[0] = pPager->nRef;
1334 a[1] = pPager->nPage;
1335 a[2] = pPager->mxPage;
1336 a[3] = pPager->dbSize;
1337 a[4] = pPager->state;
1338 a[5] = pPager->errMask;
1339 a[6] = pPager->nHit;
1340 a[7] = pPager->nMiss;
1341 a[8] = pPager->nOvfl;
1342 return a;
1343}
drhdd793422001-06-28 01:54:48 +00001344
drhfa86c412002-02-02 15:01:15 +00001345/*
1346** Set the checkpoint.
1347**
1348** This routine should be called with the transaction journal already
1349** open. A new checkpoint journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00001350** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00001351*/
1352int sqlitepager_ckpt_begin(Pager *pPager){
1353 int rc;
1354 char zTemp[SQLITE_TEMPNAME_SIZE];
1355 assert( pPager->journalOpen );
drh0f892532002-05-30 12:27:03 +00001356 assert( !pPager->ckptInUse );
drhfa86c412002-02-02 15:01:15 +00001357 pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 );
1358 if( pPager->aInCkpt==0 ){
1359 sqliteOsReadLock(&pPager->fd);
1360 return SQLITE_NOMEM;
1361 }
1362 rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize);
1363 if( rc ) goto ckpt_begin_failed;
drh663fc632002-02-02 18:49:19 +00001364 pPager->ckptSize = pPager->dbSize;
drh0f892532002-05-30 12:27:03 +00001365 if( !pPager->ckptOpen ){
1366 rc = sqlitepager_opentemp(zTemp, &pPager->cpfd);
1367 if( rc ) goto ckpt_begin_failed;
1368 pPager->ckptOpen = 1;
1369 }
1370 pPager->ckptInUse = 1;
drhfa86c412002-02-02 15:01:15 +00001371 return SQLITE_OK;
1372
1373ckpt_begin_failed:
1374 if( pPager->aInCkpt ){
1375 sqliteFree(pPager->aInCkpt);
1376 pPager->aInCkpt = 0;
1377 }
1378 return rc;
1379}
1380
1381/*
1382** Commit a checkpoint.
1383*/
1384int sqlitepager_ckpt_commit(Pager *pPager){
drh0f892532002-05-30 12:27:03 +00001385 if( pPager->ckptInUse ){
drh663fc632002-02-02 18:49:19 +00001386 PgHdr *pPg;
drh0f892532002-05-30 12:27:03 +00001387 sqliteOsTruncate(&pPager->cpfd, 0);
1388 pPager->ckptInUse = 0;
drh663fc632002-02-02 18:49:19 +00001389 sqliteFree( pPager->aInCkpt );
1390 pPager->aInCkpt = 0;
1391 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1392 pPg->inCkpt = 0;
1393 }
1394 }
drhfa86c412002-02-02 15:01:15 +00001395 return SQLITE_OK;
1396}
1397
1398/*
1399** Rollback a checkpoint.
1400*/
1401int sqlitepager_ckpt_rollback(Pager *pPager){
1402 int rc;
drh0f892532002-05-30 12:27:03 +00001403 if( pPager->ckptInUse ){
drh663fc632002-02-02 18:49:19 +00001404 rc = pager_ckpt_playback(pPager);
1405 sqlitepager_ckpt_commit(pPager);
1406 }else{
1407 rc = SQLITE_OK;
1408 }
drhfa86c412002-02-02 15:01:15 +00001409 return rc;
1410}
1411
drhdd793422001-06-28 01:54:48 +00001412#if SQLITE_TEST
1413/*
1414** Print a listing of all referenced pages and their ref count.
1415*/
1416void sqlitepager_refdump(Pager *pPager){
1417 PgHdr *pPg;
1418 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1419 if( pPg->nRef<=0 ) continue;
1420 printf("PAGE %3d addr=0x%08x nRef=%d\n",
1421 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
1422 }
1423}
1424#endif