drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This is the implementation of the page cache subsystem or "pager". |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 13 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 14 | ** 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 20 | ** |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 21 | ** @(#) $Id: pager.c,v 1.42 2002/03/05 01:11:14 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 22 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 23 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 24 | #include "pager.h" |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 25 | #include "os.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 26 | #include <assert.h> |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 27 | #include <string.h> |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 28 | |
| 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 |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 41 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 42 | ** |
| 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 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 48 | ** The page cache comes up in SQLITE_UNLOCK. The first time a |
| 49 | ** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 50 | ** After all pages have been released using sqlite_page_unref(), |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 51 | ** the state transitions back to SQLITE_UNLOCK. The first time |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 52 | ** that sqlite_page_write() is called, the state transitions to |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 53 | ** 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 58 | */ |
| 59 | #define SQLITE_UNLOCK 0 |
| 60 | #define SQLITE_READLOCK 1 |
| 61 | #define SQLITE_WRITELOCK 2 |
| 62 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 63 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 64 | /* |
| 65 | ** Each in-memory image of a page begins with the following header. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 66 | ** This header is only visible to this pager module. The client |
| 67 | ** code that calls pager sees only the data that follows the header. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 68 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 69 | typedef struct PgHdr PgHdr; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 70 | struct PgHdr { |
| 71 | Pager *pPager; /* The pager to which this page belongs */ |
| 72 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 73 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 74 | int nRef; /* Number of users of this page */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 75 | PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */ |
| 76 | PgHdr *pNextAll, *pPrevAll; /* A list of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 77 | char inJournal; /* TRUE if has been written to journal */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 78 | char inCkpt; /* TRUE if written to the checkpoint journal */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 79 | char dirty; /* TRUE if we need to write back changes */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 80 | /* SQLITE_PAGE_SIZE bytes of page data follow this header */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 81 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 85 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 86 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 87 | */ |
| 88 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 89 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 90 | #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 93 | ** How big to make the hash table used for locating in-memory pages |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 94 | ** by page number. Knuth says this should be a prime number. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 95 | */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 96 | #define N_PG_HASH 2003 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | ** A open page cache is an instance of the following structure. |
| 100 | */ |
| 101 | struct Pager { |
| 102 | char *zFilename; /* Name of the database file */ |
| 103 | char *zJournal; /* Name of the journal file */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 104 | OsFile fd, jfd; /* File descriptors for database and journal */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 105 | OsFile cpfd; /* File descriptor for the checkpoint journal */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 106 | int dbSize; /* Number of pages in the file */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 107 | int origDbSize; /* dbSize before the current change */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 108 | int ckptSize, ckptJSize; /* Size of database and journal at ckpt_begin() */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 109 | int nExtra; /* Add this many bytes to each in-memory page */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 110 | void (*xDestructor)(void*); /* Call this routine when freeing pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 111 | int nPage; /* Total number of in-memory pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 112 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 113 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 114 | int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 115 | u8 journalOpen; /* True if journal file descriptors is valid */ |
| 116 | u8 ckptOpen; /* True if the checkpoint journal is open */ |
| 117 | u8 noSync; /* Do not sync the journal if true */ |
| 118 | u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */ |
| 119 | u8 errMask; /* One of several kinds of errors */ |
| 120 | u8 tempFile; /* zFilename is a temporary file */ |
| 121 | u8 readOnly; /* True for a read-only database */ |
| 122 | u8 needSync; /* True if an fsync() is needed on the journal */ |
| 123 | u8 *aInJournal; /* One bit for each page in the database file */ |
| 124 | u8 *aInCkpt; /* One bit for each page in the database */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 125 | PgHdr *pFirst, *pLast; /* List of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 126 | PgHdr *pAll; /* List of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 127 | PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* |
| 131 | ** These are bits that can be set in Pager.errMask. |
| 132 | */ |
| 133 | #define PAGER_ERR_FULL 0x01 /* a write() failed */ |
| 134 | #define PAGER_ERR_MEM 0x02 /* malloc() failed */ |
| 135 | #define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */ |
| 136 | #define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 137 | #define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | ** The journal file contains page records in the following |
| 141 | ** format. |
| 142 | */ |
| 143 | typedef struct PageRecord PageRecord; |
| 144 | struct PageRecord { |
| 145 | Pgno pgno; /* The page number */ |
| 146 | char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */ |
| 147 | }; |
| 148 | |
| 149 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 150 | ** Journal files begin with the following magic string. The data |
| 151 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 152 | */ |
| 153 | static const unsigned char aJournalMagic[] = { |
| 154 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /* |
| 158 | ** Hash a page number |
| 159 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 160 | #define pager_hash(PN) ((PN)%N_PG_HASH) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 163 | ** Enable reference count tracking here: |
| 164 | */ |
| 165 | #if SQLITE_TEST |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 166 | int pager_refinfo_enable = 0; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 167 | static void pager_refinfo(PgHdr *p){ |
| 168 | static int cnt = 0; |
| 169 | if( !pager_refinfo_enable ) return; |
| 170 | printf( |
| 171 | "REFCNT: %4d addr=0x%08x nRef=%d\n", |
| 172 | p->pgno, (int)PGHDR_TO_DATA(p), p->nRef |
| 173 | ); |
| 174 | cnt++; /* Something to set a breakpoint on */ |
| 175 | } |
| 176 | # define REFINFO(X) pager_refinfo(X) |
| 177 | #else |
| 178 | # define REFINFO(X) |
| 179 | #endif |
| 180 | |
| 181 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 182 | ** Convert the bits in the pPager->errMask into an approprate |
| 183 | ** return code. |
| 184 | */ |
| 185 | static int pager_errcode(Pager *pPager){ |
| 186 | int rc = SQLITE_OK; |
| 187 | if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 188 | if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 189 | if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL; |
| 190 | if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM; |
| 191 | if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT; |
| 192 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* |
| 196 | ** Find a page in the hash table given its page number. Return |
| 197 | ** a pointer to the page or NULL if not found. |
| 198 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 199 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 200 | PgHdr *p = pPager->aHash[pgno % N_PG_HASH]; |
| 201 | while( p && p->pgno!=pgno ){ |
| 202 | p = p->pNextHash; |
| 203 | } |
| 204 | return p; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | ** Unlock the database and clear the in-memory cache. This routine |
| 209 | ** sets the state of the pager back to what it was when it was first |
| 210 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 211 | ** to access those pages will likely result in a coredump. |
| 212 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 213 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 214 | PgHdr *pPg, *pNext; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 215 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 216 | pNext = pPg->pNextAll; |
| 217 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 218 | } |
| 219 | pPager->pFirst = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 220 | pPager->pLast = 0; |
| 221 | pPager->pAll = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 222 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 223 | pPager->nPage = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 224 | if( pPager->state>=SQLITE_WRITELOCK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 225 | sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 226 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 227 | sqliteOsUnlock(&pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 228 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 229 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 230 | pPager->nRef = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 231 | assert( pPager->journalOpen==0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | ** When this routine is called, the pager has the journal file open and |
| 236 | ** a write lock on the database. This routine releases the database |
| 237 | ** write lock and acquires a read lock in its place. The journal file |
| 238 | ** is deleted and closed. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 239 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 240 | static int pager_unwritelock(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 241 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 242 | PgHdr *pPg; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 243 | if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 244 | sqlitepager_ckpt_commit(pPager); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 245 | sqliteOsClose(&pPager->jfd); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 246 | pPager->journalOpen = 0; |
| 247 | sqliteOsDelete(pPager->zJournal); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 248 | rc = sqliteOsReadLock(&pPager->fd); |
| 249 | assert( rc==SQLITE_OK ); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 250 | sqliteFree( pPager->aInJournal ); |
| 251 | pPager->aInJournal = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 252 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 253 | pPg->inJournal = 0; |
| 254 | pPg->dirty = 0; |
| 255 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 256 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 257 | return rc; |
| 258 | } |
| 259 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 260 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 261 | ** Read a single page from the journal file opened on file descriptor |
| 262 | ** jfd. Playback this one page. |
| 263 | */ |
| 264 | static int pager_playback_one_page(Pager *pPager, OsFile *jfd){ |
| 265 | int rc; |
| 266 | PgHdr *pPg; /* An existing page in the cache */ |
| 267 | PageRecord pgRec; |
| 268 | |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 269 | rc = sqliteOsRead(jfd, &pgRec, sizeof(pgRec)); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 270 | if( rc!=SQLITE_OK ) return rc; |
| 271 | |
| 272 | /* Sanity checking on the page */ |
| 273 | if( pgRec.pgno>pPager->dbSize || pgRec.pgno==0 ) return SQLITE_CORRUPT; |
| 274 | |
| 275 | /* Playback the page. Update the in-memory copy of the page |
| 276 | ** at the same time, if there is one. |
| 277 | */ |
| 278 | pPg = pager_lookup(pPager, pgRec.pgno); |
| 279 | if( pPg ){ |
| 280 | memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE); |
| 281 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 282 | } |
| 283 | rc = sqliteOsSeek(&pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE); |
| 284 | if( rc==SQLITE_OK ){ |
| 285 | rc = sqliteOsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE); |
| 286 | } |
| 287 | return rc; |
| 288 | } |
| 289 | |
| 290 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 291 | ** Playback the journal and thus restore the database file to |
| 292 | ** the state it was in before we started making changes. |
| 293 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 294 | ** The journal file format is as follows: There is an initial |
| 295 | ** file-type string for sanity checking. Then there is a single |
| 296 | ** Pgno number which is the number of pages in the database before |
| 297 | ** changes were made. The database is truncated to this size. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 298 | ** Next come zero or more page records where each page record |
| 299 | ** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See |
| 300 | ** the PageRecord structure for details. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 301 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 302 | ** If the file opened as the journal file is not a well-formed |
| 303 | ** journal file (as determined by looking at the magic number |
| 304 | ** at the beginning) then this routine returns SQLITE_PROTOCOL. |
| 305 | ** If any other errors occur during playback, the database will |
| 306 | ** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in |
| 307 | ** pPager->errMask and SQLITE_CORRUPT is returned. If it all |
| 308 | ** works, then this routine returns SQLITE_OK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 309 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 310 | static int pager_playback(Pager *pPager){ |
| 311 | int nRec; /* Number of Records */ |
| 312 | int i; /* Loop counter */ |
| 313 | Pgno mxPg = 0; /* Size of the original file in pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 314 | unsigned char aMagic[sizeof(aJournalMagic)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 315 | int rc; |
| 316 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 317 | /* Figure out how many records are in the journal. Abort early if |
| 318 | ** the journal is empty. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 319 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 320 | assert( pPager->journalOpen ); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 321 | sqliteOsSeek(&pPager->jfd, 0); |
| 322 | rc = sqliteOsFileSize(&pPager->jfd, &nRec); |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 323 | if( rc!=SQLITE_OK ){ |
| 324 | goto end_playback; |
| 325 | } |
| 326 | nRec = (nRec - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord); |
| 327 | if( nRec<=0 ){ |
| 328 | goto end_playback; |
| 329 | } |
| 330 | |
| 331 | /* Read the beginning of the journal and truncate the |
| 332 | ** database file back to its original size. |
| 333 | */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 334 | rc = sqliteOsRead(&pPager->jfd, aMagic, sizeof(aMagic)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 335 | if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 336 | rc = SQLITE_PROTOCOL; |
| 337 | goto end_playback; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 338 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 339 | rc = sqliteOsRead(&pPager->jfd, &mxPg, sizeof(mxPg)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 340 | if( rc!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 341 | goto end_playback; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 342 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 343 | rc = sqliteOsTruncate(&pPager->fd, mxPg*SQLITE_PAGE_SIZE); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 344 | if( rc!=SQLITE_OK ){ |
| 345 | goto end_playback; |
| 346 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 347 | pPager->dbSize = mxPg; |
| 348 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 349 | /* Copy original pages out of the journal and back into the database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 350 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 351 | for(i=nRec-1; i>=0; i--){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 352 | rc = pager_playback_one_page(pPager, &pPager->jfd); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 353 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 354 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 355 | |
| 356 | end_playback: |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 357 | if( rc!=SQLITE_OK ){ |
| 358 | pager_unwritelock(pPager); |
| 359 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 360 | rc = SQLITE_CORRUPT; |
| 361 | }else{ |
| 362 | rc = pager_unwritelock(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 363 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 364 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 368 | ** Playback the checkpoint journal. |
| 369 | ** |
| 370 | ** This is similar to playing back the transaction journal but with |
| 371 | ** a few extra twists. |
| 372 | ** |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 373 | ** (1) The number of pages in the database file at the start of |
| 374 | ** the checkpoint is stored in pPager->ckptSize, not in the |
| 375 | ** journal file itself. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 376 | ** |
| 377 | ** (2) In addition to playing back the checkpoint journal, also |
| 378 | ** playback all pages of the transaction journal beginning |
| 379 | ** at offset pPager->ckptJSize. |
| 380 | */ |
| 381 | static int pager_ckpt_playback(Pager *pPager){ |
| 382 | int nRec; /* Number of Records */ |
| 383 | int i; /* Loop counter */ |
| 384 | int rc; |
| 385 | |
| 386 | /* Truncate the database back to its original size. |
| 387 | */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 388 | rc = sqliteOsTruncate(&pPager->fd, pPager->ckptSize*SQLITE_PAGE_SIZE); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 389 | pPager->dbSize = pPager->ckptSize; |
| 390 | |
| 391 | /* Figure out how many records are in the checkpoint journal. |
| 392 | */ |
| 393 | assert( pPager->ckptOpen && pPager->journalOpen ); |
| 394 | sqliteOsSeek(&pPager->cpfd, 0); |
| 395 | rc = sqliteOsFileSize(&pPager->cpfd, &nRec); |
| 396 | if( rc!=SQLITE_OK ){ |
| 397 | goto end_ckpt_playback; |
| 398 | } |
| 399 | nRec /= sizeof(PageRecord); |
| 400 | |
| 401 | /* Copy original pages out of the checkpoint journal and back into the |
| 402 | ** database file. |
| 403 | */ |
| 404 | for(i=nRec-1; i>=0; i--){ |
| 405 | rc = pager_playback_one_page(pPager, &pPager->cpfd); |
| 406 | if( rc!=SQLITE_OK ) goto end_ckpt_playback; |
| 407 | } |
| 408 | |
| 409 | /* Figure out how many pages need to be copied out of the transaction |
| 410 | ** journal. |
| 411 | */ |
| 412 | rc = sqliteOsSeek(&pPager->jfd, pPager->ckptJSize); |
| 413 | if( rc!=SQLITE_OK ){ |
| 414 | goto end_ckpt_playback; |
| 415 | } |
| 416 | rc = sqliteOsFileSize(&pPager->jfd, &nRec); |
| 417 | if( rc!=SQLITE_OK ){ |
| 418 | goto end_ckpt_playback; |
| 419 | } |
| 420 | nRec = (nRec - pPager->ckptJSize)/sizeof(PageRecord); |
| 421 | for(i=nRec-1; i>=0; i--){ |
| 422 | rc = pager_playback_one_page(pPager, &pPager->jfd); |
| 423 | if( rc!=SQLITE_OK ) goto end_ckpt_playback; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | end_ckpt_playback: |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 428 | if( rc!=SQLITE_OK ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 429 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 430 | rc = SQLITE_CORRUPT; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 431 | } |
| 432 | return rc; |
| 433 | } |
| 434 | |
| 435 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 436 | ** Change the maximum number of in-memory pages that are allowed. |
| 437 | */ |
| 438 | void sqlitepager_set_cachesize(Pager *pPager, int mxPage){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 439 | if( mxPage>=0 ){ |
| 440 | pPager->noSync = 0; |
| 441 | }else{ |
| 442 | pPager->noSync = 1; |
| 443 | mxPage = -mxPage; |
| 444 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 445 | if( mxPage>10 ){ |
| 446 | pPager->mxPage = mxPage; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /* |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 451 | ** Open a temporary file. Write the name of the file into zName |
| 452 | ** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write |
| 453 | ** the file descriptor into *fd. Return SQLITE_OK on success or some |
| 454 | ** other error code if we fail. |
| 455 | ** |
| 456 | ** The OS will automatically delete the temporary file when it is |
| 457 | ** closed. |
| 458 | */ |
| 459 | static int sqlitepager_opentemp(char *zFile, OsFile *fd){ |
| 460 | int cnt = 8; |
| 461 | int rc; |
| 462 | do{ |
| 463 | cnt--; |
| 464 | sqliteOsTempFileName(zFile); |
| 465 | rc = sqliteOsOpenExclusive(zFile, fd, 1); |
| 466 | }while( cnt>0 && rc!=SQLITE_OK ); |
| 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 471 | ** Create a new page cache and put a pointer to the page cache in *ppPager. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 472 | ** The file to be cached need not exist. The file is not locked until |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 473 | ** the first call to sqlitepager_get() and is only held open until the |
| 474 | ** last page is released using sqlitepager_unref(). |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 475 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 476 | ** If zFilename is NULL then a randomly-named temporary file is created |
| 477 | ** and used as the file to be cached. The file will be deleted |
| 478 | ** automatically when it is closed. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 479 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 480 | int sqlitepager_open( |
| 481 | Pager **ppPager, /* Return the Pager structure here */ |
| 482 | const char *zFilename, /* Name of the database file to open */ |
| 483 | int mxPage, /* Max number of in-memory cache pages */ |
| 484 | int nExtra /* Extra bytes append to each in-memory page */ |
| 485 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 486 | Pager *pPager; |
| 487 | int nameLen; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 488 | OsFile fd; |
| 489 | int rc; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 490 | int tempFile; |
| 491 | int readOnly = 0; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 492 | char zTemp[SQLITE_TEMPNAME_SIZE]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 493 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 494 | *ppPager = 0; |
| 495 | if( sqlite_malloc_failed ){ |
| 496 | return SQLITE_NOMEM; |
| 497 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 498 | if( zFilename ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 499 | rc = sqliteOsOpenReadWrite(zFilename, &fd, &readOnly); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 500 | tempFile = 0; |
| 501 | }else{ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 502 | rc = sqlitepager_opentemp(zTemp, &fd); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 503 | zFilename = zTemp; |
| 504 | tempFile = 1; |
| 505 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 506 | if( rc!=SQLITE_OK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 507 | return SQLITE_CANTOPEN; |
| 508 | } |
| 509 | nameLen = strlen(zFilename); |
| 510 | pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 511 | if( pPager==0 ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 512 | sqliteOsClose(&fd); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 513 | return SQLITE_NOMEM; |
| 514 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 515 | pPager->zFilename = (char*)&pPager[1]; |
| 516 | pPager->zJournal = &pPager->zFilename[nameLen+1]; |
| 517 | strcpy(pPager->zFilename, zFilename); |
| 518 | strcpy(pPager->zJournal, zFilename); |
| 519 | strcpy(&pPager->zJournal[nameLen], "-journal"); |
| 520 | pPager->fd = fd; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 521 | pPager->journalOpen = 0; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 522 | pPager->ckptOpen = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 523 | pPager->nRef = 0; |
| 524 | pPager->dbSize = -1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 525 | pPager->ckptSize = 0; |
| 526 | pPager->ckptJSize = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 527 | pPager->nPage = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 528 | pPager->mxPage = mxPage>5 ? mxPage : 10; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 529 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 530 | pPager->errMask = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 531 | pPager->tempFile = tempFile; |
| 532 | pPager->readOnly = readOnly; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 533 | pPager->needSync = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 534 | pPager->pFirst = 0; |
| 535 | pPager->pLast = 0; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 536 | pPager->nExtra = nExtra; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 537 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 538 | *ppPager = pPager; |
| 539 | return SQLITE_OK; |
| 540 | } |
| 541 | |
| 542 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 543 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 544 | ** when the reference count on each page reaches zero. The destructor can |
| 545 | ** be used to clean up information in the extra segment appended to each page. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 546 | ** |
| 547 | ** The destructor is not called as a result sqlitepager_close(). |
| 548 | ** Destructors are only called by sqlitepager_unref(). |
| 549 | */ |
| 550 | void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){ |
| 551 | pPager->xDestructor = xDesc; |
| 552 | } |
| 553 | |
| 554 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 555 | ** Return the total number of pages in the disk file associated with |
| 556 | ** pPager. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 557 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 558 | int sqlitepager_pagecount(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 559 | int n; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 560 | assert( pPager!=0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 561 | if( pPager->dbSize>=0 ){ |
| 562 | return pPager->dbSize; |
| 563 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 564 | if( sqliteOsFileSize(&pPager->fd, &n)!=SQLITE_OK ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 565 | pPager->errMask |= PAGER_ERR_DISK; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 566 | return 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 567 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 568 | n /= SQLITE_PAGE_SIZE; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 569 | if( pPager->state!=SQLITE_UNLOCK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 570 | pPager->dbSize = n; |
| 571 | } |
| 572 | return n; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** Shutdown the page cache. Free all memory and close all files. |
| 577 | ** |
| 578 | ** If a transaction was in progress when this routine is called, that |
| 579 | ** transaction is rolled back. All outstanding pages are invalidated |
| 580 | ** and their memory is freed. Any attempt to use a page associated |
| 581 | ** with this page cache after this function returns will likely |
| 582 | ** result in a coredump. |
| 583 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 584 | int sqlitepager_close(Pager *pPager){ |
| 585 | PgHdr *pPg, *pNext; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 586 | switch( pPager->state ){ |
| 587 | case SQLITE_WRITELOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 588 | sqlitepager_rollback(pPager); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 589 | sqliteOsUnlock(&pPager->fd); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 590 | assert( pPager->journalOpen==0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | case SQLITE_READLOCK: { |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 594 | sqliteOsUnlock(&pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 595 | break; |
| 596 | } |
| 597 | default: { |
| 598 | /* Do nothing */ |
| 599 | break; |
| 600 | } |
| 601 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 602 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 603 | pNext = pPg->pNextAll; |
| 604 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 605 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 606 | sqliteOsClose(&pPager->fd); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 607 | assert( pPager->journalOpen==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 608 | if( pPager->tempFile ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 609 | /* sqliteOsDelete(pPager->zFilename); */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 610 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 611 | sqliteFree(pPager); |
| 612 | return SQLITE_OK; |
| 613 | } |
| 614 | |
| 615 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 616 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 617 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 618 | Pgno sqlitepager_pagenumber(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 619 | PgHdr *p = DATA_TO_PGHDR(pData); |
| 620 | return p->pgno; |
| 621 | } |
| 622 | |
| 623 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 624 | ** Increment the reference count for a page. If the page is |
| 625 | ** currently on the freelist (the reference count is zero) then |
| 626 | ** remove it from the freelist. |
| 627 | */ |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 628 | static void page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 629 | if( pPg->nRef==0 ){ |
| 630 | /* The page is currently on the freelist. Remove it. */ |
| 631 | if( pPg->pPrevFree ){ |
| 632 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
| 633 | }else{ |
| 634 | pPg->pPager->pFirst = pPg->pNextFree; |
| 635 | } |
| 636 | if( pPg->pNextFree ){ |
| 637 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 638 | }else{ |
| 639 | pPg->pPager->pLast = pPg->pPrevFree; |
| 640 | } |
| 641 | pPg->pPager->nRef++; |
| 642 | } |
| 643 | pPg->nRef++; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 644 | REFINFO(pPg); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* |
| 648 | ** Increment the reference count for a page. The input pointer is |
| 649 | ** a reference to the page data. |
| 650 | */ |
| 651 | int sqlitepager_ref(void *pData){ |
| 652 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 653 | page_ref(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 654 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 658 | ** Sync the journal and then write all free dirty pages to the database |
| 659 | ** file. |
| 660 | ** |
| 661 | ** Writing all free dirty pages to the database after the sync is a |
| 662 | ** non-obvious optimization. fsync() is an expensive operation so we |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 663 | ** want to minimize the number ot times it is called. After an fsync() call, |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 664 | ** we are free to write dirty pages back to the database. It is best |
| 665 | ** to go ahead and write as many dirty pages as possible to minimize |
| 666 | ** the risk of having to do another fsync() later on. Writing dirty |
| 667 | ** free pages in this way was observed to make database operations go |
| 668 | ** up to 10 times faster. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 669 | ** |
| 670 | ** If we are writing to temporary database, there is no need to preserve |
| 671 | ** the integrity of the journal file, so we can save time and skip the |
| 672 | ** fsync(). |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 673 | */ |
| 674 | static int syncAllPages(Pager *pPager){ |
| 675 | PgHdr *pPg; |
| 676 | int rc = SQLITE_OK; |
| 677 | if( pPager->needSync ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 678 | if( !pPager->tempFile ){ |
| 679 | rc = sqliteOsSync(&pPager->jfd); |
| 680 | if( rc!=0 ) return rc; |
| 681 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 682 | pPager->needSync = 0; |
| 683 | } |
| 684 | for(pPg=pPager->pFirst; pPg; pPg=pPg->pNextFree){ |
| 685 | if( pPg->dirty ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 686 | sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 687 | rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 688 | if( rc!=SQLITE_OK ) break; |
| 689 | pPg->dirty = 0; |
| 690 | } |
| 691 | } |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 692 | return rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 696 | ** Acquire a page. |
| 697 | ** |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 698 | ** A read lock on the disk file is obtained when the first page is acquired. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 699 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 700 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 701 | ** A _get works for any page number greater than 0. If the database |
| 702 | ** file is smaller than the requested page, then no actual disk |
| 703 | ** read occurs and the memory image of the page is initialized to |
| 704 | ** all zeros. The extra data appended to a page is always initialized |
| 705 | ** to zeros the first time a page is loaded into memory. |
| 706 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 707 | ** The acquisition might fail for several reasons. In all cases, |
| 708 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 709 | ** |
| 710 | ** See also sqlitepager_lookup(). Both this routine and _lookup() attempt |
| 711 | ** to find a page in the in-memory cache first. If the page is not already |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 712 | ** in memory, this routine goes to disk to read it in whereas _lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 713 | ** just returns 0. This routine acquires a read-lock the first time it |
| 714 | ** has to go to disk, and could also playback an old journal if necessary. |
| 715 | ** Since _lookup() never goes to disk, it never has to deal with locks |
| 716 | ** or journal files. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 717 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 718 | int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 719 | PgHdr *pPg; |
| 720 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 721 | /* Make sure we have not hit any critical errors. |
| 722 | */ |
| 723 | if( pPager==0 || pgno==0 ){ |
| 724 | return SQLITE_ERROR; |
| 725 | } |
| 726 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 727 | return pager_errcode(pPager); |
| 728 | } |
| 729 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 730 | /* If this is the first page accessed, then get a read lock |
| 731 | ** on the database file. |
| 732 | */ |
| 733 | if( pPager->nRef==0 ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 734 | if( sqliteOsReadLock(&pPager->fd)!=SQLITE_OK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 735 | *ppPage = 0; |
| 736 | return SQLITE_BUSY; |
| 737 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 738 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 739 | |
| 740 | /* If a journal file exists, try to play it back. |
| 741 | */ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 742 | if( sqliteOsFileExists(pPager->zJournal) ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 743 | int rc, dummy; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 744 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 745 | /* Get a write lock on the database |
| 746 | */ |
| 747 | rc = sqliteOsWriteLock(&pPager->fd); |
| 748 | if( rc!=SQLITE_OK ){ |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 749 | rc = sqliteOsUnlock(&pPager->fd); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 750 | assert( rc==SQLITE_OK ); |
| 751 | *ppPage = 0; |
| 752 | return SQLITE_BUSY; |
| 753 | } |
| 754 | pPager->state = SQLITE_WRITELOCK; |
| 755 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 756 | /* Open the journal for exclusive access. Return SQLITE_BUSY if |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 757 | ** we cannot get exclusive access to the journal file. |
| 758 | ** |
| 759 | ** Even though we will only be reading from the journal, not writing, |
| 760 | ** we have to open the journal for writing in order to obtain an |
| 761 | ** exclusive access lock. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 762 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 763 | rc = sqliteOsOpenReadWrite(pPager->zJournal, &pPager->jfd, &dummy); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 764 | if( rc!=SQLITE_OK ){ |
| 765 | rc = sqliteOsUnlock(&pPager->fd); |
| 766 | assert( rc==SQLITE_OK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 767 | *ppPage = 0; |
| 768 | return SQLITE_BUSY; |
| 769 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 770 | pPager->journalOpen = 1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 771 | |
| 772 | /* Playback and delete the journal. Drop the database write |
| 773 | ** lock and reacquire the read lock. |
| 774 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 775 | rc = pager_playback(pPager); |
| 776 | if( rc!=SQLITE_OK ){ |
| 777 | return rc; |
| 778 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 779 | } |
| 780 | pPg = 0; |
| 781 | }else{ |
| 782 | /* Search for page in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 783 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 784 | } |
| 785 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 786 | /* The requested page is not in the page cache. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 787 | int h; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 788 | pPager->nMiss++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 789 | if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){ |
| 790 | /* Create a new page */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 791 | pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 792 | if( pPg==0 ){ |
| 793 | *ppPage = 0; |
| 794 | pager_unwritelock(pPager); |
| 795 | pPager->errMask |= PAGER_ERR_MEM; |
| 796 | return SQLITE_NOMEM; |
| 797 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 798 | pPg->pPager = pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 799 | pPg->pNextAll = pPager->pAll; |
| 800 | if( pPager->pAll ){ |
| 801 | pPager->pAll->pPrevAll = pPg; |
| 802 | } |
| 803 | pPg->pPrevAll = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 804 | pPager->pAll = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 805 | pPager->nPage++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 806 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 807 | /* Recycle an older page. First locate the page to be recycled. |
| 808 | ** Try to find one that is not dirty and is near the head of |
| 809 | ** of the free list */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 810 | pPg = pPager->pFirst; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 811 | while( pPg && pPg->dirty ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 812 | pPg = pPg->pNextFree; |
| 813 | } |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 814 | |
| 815 | /* If we could not find a page that has not been used recently |
| 816 | ** and which is not dirty, then sync the journal and write all |
| 817 | ** dirty free pages into the database file, thus making them |
| 818 | ** clean pages and available for recycling. |
| 819 | ** |
| 820 | ** We have to sync the journal before writing a page to the main |
| 821 | ** database. But syncing is a very slow operation. So after a |
| 822 | ** sync, it is best to write everything we can back to the main |
| 823 | ** database to minimize the risk of having to sync again in the |
| 824 | ** near future. That is way we write all dirty pages after a |
| 825 | ** sync. |
| 826 | */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 827 | if( pPg==0 ){ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 828 | int rc = syncAllPages(pPager); |
| 829 | if( rc!=0 ){ |
| 830 | sqlitepager_rollback(pPager); |
| 831 | *ppPage = 0; |
| 832 | return SQLITE_IOERR; |
| 833 | } |
| 834 | pPg = pPager->pFirst; |
| 835 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 836 | assert( pPg->nRef==0 ); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 837 | assert( pPg->dirty==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 838 | |
| 839 | /* Unlink the old page from the free list and the hash table |
| 840 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 841 | if( pPg->pPrevFree ){ |
| 842 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 843 | }else{ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 844 | assert( pPager->pFirst==pPg ); |
| 845 | pPager->pFirst = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 846 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 847 | if( pPg->pNextFree ){ |
| 848 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 849 | }else{ |
| 850 | assert( pPager->pLast==pPg ); |
| 851 | pPager->pLast = pPg->pPrevFree; |
| 852 | } |
| 853 | pPg->pNextFree = pPg->pPrevFree = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 854 | if( pPg->pNextHash ){ |
| 855 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 856 | } |
| 857 | if( pPg->pPrevHash ){ |
| 858 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 859 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 860 | h = pager_hash(pPg->pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 861 | assert( pPager->aHash[h]==pPg ); |
| 862 | pPager->aHash[h] = pPg->pNextHash; |
| 863 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 864 | pPg->pNextHash = pPg->pPrevHash = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 865 | pPager->nOvfl++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 866 | } |
| 867 | pPg->pgno = pgno; |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 868 | if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 869 | pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; |
| 870 | }else{ |
| 871 | pPg->inJournal = 0; |
| 872 | } |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 873 | if( pPager->aInCkpt && (int)pgno<=pPager->ckptSize ){ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 874 | pPg->inCkpt = (pPager->aInCkpt[pgno/8] & (1<<(pgno&7)))!=0; |
| 875 | }else{ |
| 876 | pPg->inCkpt = 0; |
| 877 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 878 | pPg->dirty = 0; |
| 879 | pPg->nRef = 1; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 880 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 881 | pPager->nRef++; |
| 882 | h = pager_hash(pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 883 | pPg->pNextHash = pPager->aHash[h]; |
| 884 | pPager->aHash[h] = pPg; |
| 885 | if( pPg->pNextHash ){ |
| 886 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 887 | pPg->pNextHash->pPrevHash = pPg; |
| 888 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 889 | if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager); |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 890 | if( pPager->dbSize<(int)pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 891 | memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE); |
| 892 | }else{ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 893 | int rc; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 894 | sqliteOsSeek(&pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE); |
| 895 | rc = sqliteOsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 896 | if( rc!=SQLITE_OK ){ |
| 897 | return rc; |
| 898 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 899 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 900 | if( pPager->nExtra>0 ){ |
| 901 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 902 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 903 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 904 | /* The requested page is in the page cache. */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 905 | pPager->nHit++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 906 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 907 | } |
| 908 | *ppPage = PGHDR_TO_DATA(pPg); |
| 909 | return SQLITE_OK; |
| 910 | } |
| 911 | |
| 912 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 913 | ** Acquire a page if it is already in the in-memory cache. Do |
| 914 | ** not read the page from disk. Return a pointer to the page, |
| 915 | ** or 0 if the page is not in cache. |
| 916 | ** |
| 917 | ** See also sqlitepager_get(). The difference between this routine |
| 918 | ** and sqlitepager_get() is that _get() will go to the disk and read |
| 919 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 920 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 921 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 922 | */ |
| 923 | void *sqlitepager_lookup(Pager *pPager, Pgno pgno){ |
| 924 | PgHdr *pPg; |
| 925 | |
| 926 | /* Make sure we have not hit any critical errors. |
| 927 | */ |
| 928 | if( pPager==0 || pgno==0 ){ |
| 929 | return 0; |
| 930 | } |
| 931 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 932 | return 0; |
| 933 | } |
| 934 | if( pPager->nRef==0 ){ |
| 935 | return 0; |
| 936 | } |
| 937 | pPg = pager_lookup(pPager, pgno); |
| 938 | if( pPg==0 ) return 0; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 939 | page_ref(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 940 | return PGHDR_TO_DATA(pPg); |
| 941 | } |
| 942 | |
| 943 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 944 | ** Release a page. |
| 945 | ** |
| 946 | ** If the number of references to the page drop to zero, then the |
| 947 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 948 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 949 | ** removed. |
| 950 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 951 | int sqlitepager_unref(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 952 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 953 | |
| 954 | /* Decrement the reference count for this page |
| 955 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 956 | pPg = DATA_TO_PGHDR(pData); |
| 957 | assert( pPg->nRef>0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 958 | pPg->nRef--; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 959 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 960 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 961 | /* When the number of references to a page reach 0, call the |
| 962 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 963 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 964 | if( pPg->nRef==0 ){ |
drh | 1eaa269 | 2001-09-18 02:02:23 +0000 | [diff] [blame] | 965 | Pager *pPager; |
| 966 | pPager = pPg->pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 967 | pPg->pNextFree = 0; |
| 968 | pPg->pPrevFree = pPager->pLast; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 969 | pPager->pLast = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 970 | if( pPg->pPrevFree ){ |
| 971 | pPg->pPrevFree->pNextFree = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 972 | }else{ |
| 973 | pPager->pFirst = pPg; |
| 974 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 975 | if( pPager->xDestructor ){ |
| 976 | pPager->xDestructor(pData); |
| 977 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 978 | |
| 979 | /* When all pages reach the freelist, drop the read lock from |
| 980 | ** the database file. |
| 981 | */ |
| 982 | pPager->nRef--; |
| 983 | assert( pPager->nRef>=0 ); |
| 984 | if( pPager->nRef==0 ){ |
| 985 | pager_reset(pPager); |
| 986 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 987 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 988 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /* |
| 992 | ** Mark a data page as writeable. The page is written into the journal |
| 993 | ** if it is not there already. This routine must be called before making |
| 994 | ** changes to a page. |
| 995 | ** |
| 996 | ** The first time this routine is called, the pager creates a new |
| 997 | ** journal and acquires a write lock on the database. If the write |
| 998 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 999 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1000 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1001 | ** |
| 1002 | ** If the journal file could not be written because the disk is full, |
| 1003 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 1004 | ** All subsequent write attempts also return SQLITE_FULL until there |
| 1005 | ** is a call to sqlitepager_commit() or sqlitepager_rollback() to |
| 1006 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1007 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1008 | int sqlitepager_write(void *pData){ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1009 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1010 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 1011 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1012 | |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1013 | /* Check for errors |
| 1014 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1015 | if( pPager->errMask ){ |
| 1016 | return pager_errcode(pPager); |
| 1017 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1018 | if( pPager->readOnly ){ |
| 1019 | return SQLITE_PERM; |
| 1020 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1021 | |
| 1022 | /* Mark the page as dirty. If the page has already been written |
| 1023 | ** to the journal then we can return right away. |
| 1024 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1025 | pPg->dirty = 1; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1026 | if( pPg->inJournal && (pPg->inCkpt || pPager->ckptOpen==0) ){ |
| 1027 | return SQLITE_OK; |
| 1028 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* If we get this far, it means that the page needs to be |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1031 | ** written to the transaction journal or the ckeckpoint journal |
| 1032 | ** or both. |
| 1033 | ** |
| 1034 | ** First check to see that the transaction journal exists and |
| 1035 | ** create it if it does not. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1036 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1037 | assert( pPager->state!=SQLITE_UNLOCK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1038 | if( pPager->state==SQLITE_READLOCK ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1039 | assert( pPager->aInJournal==0 ); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1040 | rc = sqliteOsWriteLock(&pPager->fd); |
| 1041 | if( rc!=SQLITE_OK ){ |
| 1042 | return rc; |
| 1043 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1044 | pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); |
| 1045 | if( pPager->aInJournal==0 ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1046 | sqliteOsReadLock(&pPager->fd); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1047 | return SQLITE_NOMEM; |
| 1048 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1049 | rc = sqliteOsOpenExclusive(pPager->zJournal, &pPager->jfd, 0); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1050 | if( rc!=SQLITE_OK ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1051 | sqliteFree(pPager->aInJournal); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1052 | pPager->aInJournal = 0; |
| 1053 | sqliteOsReadLock(&pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1054 | return SQLITE_CANTOPEN; |
| 1055 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1056 | pPager->journalOpen = 1; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1057 | pPager->needSync = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1058 | pPager->state = SQLITE_WRITELOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1059 | sqlitepager_pagecount(pPager); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1060 | pPager->origDbSize = pPager->dbSize; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1061 | rc = sqliteOsWrite(&pPager->jfd, aJournalMagic, sizeof(aJournalMagic)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1062 | if( rc==SQLITE_OK ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1063 | rc = sqliteOsWrite(&pPager->jfd, &pPager->dbSize, sizeof(Pgno)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1064 | } |
| 1065 | if( rc!=SQLITE_OK ){ |
| 1066 | rc = pager_unwritelock(pPager); |
| 1067 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 1068 | return rc; |
| 1069 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1070 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1071 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1072 | assert( pPager->journalOpen ); |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1073 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1074 | /* The transaction journal now exists and we have a write lock on the |
| 1075 | ** main database file. Write the current page to the transaction |
| 1076 | ** journal if it is not there already. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1077 | */ |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1078 | if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1079 | rc = sqliteOsWrite(&pPager->jfd, &pPg->pgno, sizeof(Pgno)); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1080 | if( rc==SQLITE_OK ){ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1081 | rc = sqliteOsWrite(&pPager->jfd, pData, SQLITE_PAGE_SIZE); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1082 | } |
| 1083 | if( rc!=SQLITE_OK ){ |
| 1084 | sqlitepager_rollback(pPager); |
| 1085 | pPager->errMask |= PAGER_ERR_FULL; |
| 1086 | return rc; |
| 1087 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1088 | assert( pPager->aInJournal!=0 ); |
| 1089 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 1090 | pPager->needSync = !pPager->noSync; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1091 | pPg->inJournal = 1; |
| 1092 | if( pPager->ckptOpen ){ |
| 1093 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 1094 | pPg->inCkpt = 1; |
| 1095 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1096 | } |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1097 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1098 | /* If the checkpoint journal is open and the page is not in it, |
| 1099 | ** then write the current page to the checkpoint journal. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1100 | */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1101 | if( pPager->ckptOpen && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){ |
drh | 1e336b4 | 2002-02-14 12:50:33 +0000 | [diff] [blame] | 1102 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1103 | rc = sqliteOsWrite(&pPager->cpfd, &pPg->pgno, sizeof(Pgno)); |
| 1104 | if( rc==SQLITE_OK ){ |
| 1105 | rc = sqliteOsWrite(&pPager->cpfd, pData, SQLITE_PAGE_SIZE); |
| 1106 | } |
| 1107 | if( rc!=SQLITE_OK ){ |
| 1108 | sqlitepager_rollback(pPager); |
| 1109 | pPager->errMask |= PAGER_ERR_FULL; |
| 1110 | return rc; |
| 1111 | } |
| 1112 | assert( pPager->aInCkpt!=0 ); |
| 1113 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 1114 | pPg->inCkpt = 1; |
| 1115 | } |
| 1116 | |
| 1117 | /* Update the database size and return. |
| 1118 | */ |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 1119 | if( pPager->dbSize<(int)pPg->pgno ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1120 | pPager->dbSize = pPg->pgno; |
| 1121 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1122 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /* |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 1126 | ** Return TRUE if the page given in the argument was previously passed |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1127 | ** to sqlitepager_write(). In other words, return TRUE if it is ok |
| 1128 | ** to change the content of the page. |
| 1129 | */ |
| 1130 | int sqlitepager_iswriteable(void *pData){ |
| 1131 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1132 | return pPg->dirty; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1136 | ** A call to this routine tells the pager that it is not necessary to |
| 1137 | ** write the information on page "pgno" back to the disk, even though |
| 1138 | ** that page might be marked as dirty. |
| 1139 | ** |
| 1140 | ** The overlying software layer calls this routine when all of the data |
| 1141 | ** on the given page is unused. The pager marks the page as clean so |
| 1142 | ** that it does not get written to disk. |
| 1143 | ** |
| 1144 | ** Tests show that this optimization, together with the |
| 1145 | ** sqlitepager_dont_rollback() below, more than double the speed |
| 1146 | ** of large INSERT operations and quadruple the speed of large DELETEs. |
| 1147 | */ |
| 1148 | void sqlitepager_dont_write(Pager *pPager, Pgno pgno){ |
| 1149 | PgHdr *pPg; |
| 1150 | pPg = pager_lookup(pPager, pgno); |
| 1151 | if( pPg && pPg->dirty ){ |
| 1152 | pPg->dirty = 0; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | ** A call to this routine tells the pager that if a rollback occurs, |
| 1158 | ** it is not necessary to restore the data on the given page. This |
| 1159 | ** means that the pager does not have to record the given page in the |
| 1160 | ** rollback journal. |
| 1161 | */ |
| 1162 | void sqlitepager_dont_rollback(void *pData){ |
| 1163 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1164 | Pager *pPager = pPg->pPager; |
| 1165 | |
| 1166 | if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return; |
| 1167 | if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){ |
| 1168 | assert( pPager->aInJournal!=0 ); |
| 1169 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 1170 | pPg->inJournal = 1; |
| 1171 | if( pPager->ckptOpen ){ |
| 1172 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 1173 | pPg->inCkpt = 1; |
| 1174 | } |
| 1175 | } |
| 1176 | if( pPager->ckptOpen && !pPg->inCkpt && (int)pPg->pgno<=pPager->ckptSize ){ |
| 1177 | assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); |
| 1178 | assert( pPager->aInCkpt!=0 ); |
| 1179 | pPager->aInCkpt[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
| 1180 | pPg->inCkpt = 1; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1185 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1186 | ** |
| 1187 | ** If the commit fails for any reason, a rollback attempt is made |
| 1188 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 1189 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1190 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1191 | int sqlitepager_commit(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1192 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1193 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1194 | |
| 1195 | if( pPager->errMask==PAGER_ERR_FULL ){ |
| 1196 | rc = sqlitepager_rollback(pPager); |
| 1197 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 1198 | return rc; |
| 1199 | } |
| 1200 | if( pPager->errMask!=0 ){ |
| 1201 | rc = pager_errcode(pPager); |
| 1202 | return rc; |
| 1203 | } |
| 1204 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1205 | return SQLITE_ERROR; |
| 1206 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 1207 | assert( pPager->journalOpen ); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1208 | if( pPager->needSync && sqliteOsSync(&pPager->jfd)!=SQLITE_OK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1209 | goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1210 | } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1211 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1212 | if( pPg->dirty==0 ) continue; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1213 | rc = sqliteOsSeek(&pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1214 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1215 | rc = sqliteOsWrite(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1216 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1217 | } |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame^] | 1218 | if( !pPager->noSync && sqliteOsSync(&pPager->fd)!=SQLITE_OK ){ |
| 1219 | goto commit_abort; |
| 1220 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1221 | rc = pager_unwritelock(pPager); |
| 1222 | pPager->dbSize = -1; |
| 1223 | return rc; |
| 1224 | |
| 1225 | /* Jump here if anything goes wrong during the commit process. |
| 1226 | */ |
| 1227 | commit_abort: |
| 1228 | rc = sqlitepager_rollback(pPager); |
| 1229 | if( rc==SQLITE_OK ){ |
| 1230 | rc = SQLITE_FULL; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1231 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1232 | return rc; |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | ** Rollback all changes. The database falls back to read-only mode. |
| 1237 | ** All in-memory cache pages revert to their original data contents. |
| 1238 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1239 | ** |
| 1240 | ** This routine cannot fail unless some other process is not following |
| 1241 | ** the correct locking protocol (SQLITE_PROTOCOL) or unless some other |
| 1242 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 1243 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 1244 | ** codes are returned for all these occasions. Otherwise, |
| 1245 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1246 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1247 | int sqlitepager_rollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1248 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1249 | if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){ |
| 1250 | return pager_errcode(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1251 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1252 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1253 | return SQLITE_OK; |
| 1254 | } |
| 1255 | rc = pager_playback(pPager); |
| 1256 | if( rc!=SQLITE_OK ){ |
| 1257 | rc = SQLITE_CORRUPT; |
| 1258 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 1259 | } |
| 1260 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1261 | return rc; |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 1262 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1265 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 1266 | ** if the database is (in theory) writable. |
| 1267 | */ |
| 1268 | int sqlitepager_isreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1269 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1273 | ** This routine is used for testing and analysis only. |
| 1274 | */ |
| 1275 | int *sqlitepager_stats(Pager *pPager){ |
| 1276 | static int a[9]; |
| 1277 | a[0] = pPager->nRef; |
| 1278 | a[1] = pPager->nPage; |
| 1279 | a[2] = pPager->mxPage; |
| 1280 | a[3] = pPager->dbSize; |
| 1281 | a[4] = pPager->state; |
| 1282 | a[5] = pPager->errMask; |
| 1283 | a[6] = pPager->nHit; |
| 1284 | a[7] = pPager->nMiss; |
| 1285 | a[8] = pPager->nOvfl; |
| 1286 | return a; |
| 1287 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1288 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1289 | /* |
| 1290 | ** Set the checkpoint. |
| 1291 | ** |
| 1292 | ** This routine should be called with the transaction journal already |
| 1293 | ** open. A new checkpoint journal is created that can be used to rollback |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 1294 | ** changes of a single SQL command within a larger transaction. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1295 | */ |
| 1296 | int sqlitepager_ckpt_begin(Pager *pPager){ |
| 1297 | int rc; |
| 1298 | char zTemp[SQLITE_TEMPNAME_SIZE]; |
| 1299 | assert( pPager->journalOpen ); |
| 1300 | assert( !pPager->ckptOpen ); |
| 1301 | pPager->aInCkpt = sqliteMalloc( pPager->dbSize/8 + 1 ); |
| 1302 | if( pPager->aInCkpt==0 ){ |
| 1303 | sqliteOsReadLock(&pPager->fd); |
| 1304 | return SQLITE_NOMEM; |
| 1305 | } |
| 1306 | rc = sqliteOsFileSize(&pPager->jfd, &pPager->ckptJSize); |
| 1307 | if( rc ) goto ckpt_begin_failed; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1308 | pPager->ckptSize = pPager->dbSize; |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1309 | rc = sqlitepager_opentemp(zTemp, &pPager->cpfd); |
| 1310 | if( rc ) goto ckpt_begin_failed; |
| 1311 | pPager->ckptOpen = 1; |
| 1312 | return SQLITE_OK; |
| 1313 | |
| 1314 | ckpt_begin_failed: |
| 1315 | if( pPager->aInCkpt ){ |
| 1316 | sqliteFree(pPager->aInCkpt); |
| 1317 | pPager->aInCkpt = 0; |
| 1318 | } |
| 1319 | return rc; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | ** Commit a checkpoint. |
| 1324 | */ |
| 1325 | int sqlitepager_ckpt_commit(Pager *pPager){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1326 | if( pPager->ckptOpen ){ |
| 1327 | PgHdr *pPg; |
| 1328 | sqliteOsClose(&pPager->cpfd); |
| 1329 | pPager->ckptOpen = 0; |
| 1330 | sqliteFree( pPager->aInCkpt ); |
| 1331 | pPager->aInCkpt = 0; |
| 1332 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1333 | pPg->inCkpt = 0; |
| 1334 | } |
| 1335 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1336 | return SQLITE_OK; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | ** Rollback a checkpoint. |
| 1341 | */ |
| 1342 | int sqlitepager_ckpt_rollback(Pager *pPager){ |
| 1343 | int rc; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1344 | if( pPager->ckptOpen ){ |
| 1345 | rc = pager_ckpt_playback(pPager); |
| 1346 | sqlitepager_ckpt_commit(pPager); |
| 1347 | }else{ |
| 1348 | rc = SQLITE_OK; |
| 1349 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1350 | return rc; |
| 1351 | } |
| 1352 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1353 | #if SQLITE_TEST |
| 1354 | /* |
| 1355 | ** Print a listing of all referenced pages and their ref count. |
| 1356 | */ |
| 1357 | void sqlitepager_refdump(Pager *pPager){ |
| 1358 | PgHdr *pPg; |
| 1359 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1360 | if( pPg->nRef<=0 ) continue; |
| 1361 | printf("PAGE %3d addr=0x%08x nRef=%d\n", |
| 1362 | pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef); |
| 1363 | } |
| 1364 | } |
| 1365 | #endif |