dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 2 | ** 2010 February 1 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains the implementation of a write-ahead log file used in |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 14 | ** "journal_mode=wal" mode. |
| 15 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 16 | #include "wal.h" |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 17 | |
dan | 4b64c1e | 2010-04-27 18:49:54 +0000 | [diff] [blame] | 18 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 19 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 20 | ** WRITE-AHEAD LOG (WAL) FILE FORMAT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 21 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 22 | ** A wal file consists of a header followed by zero or more "frames". |
| 23 | ** The header is 12 bytes in size and consists of the following three |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 24 | ** big-endian 32-bit unsigned integer values: |
| 25 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 26 | ** 0: Database page size, |
| 27 | ** 4: Randomly selected salt value 1, |
| 28 | ** 8: Randomly selected salt value 2. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 29 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 30 | ** Immediately following the header are zero or more frames. Each |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 31 | ** frame itself consists of a 16-byte header followed by a <page-size> bytes |
| 32 | ** of page data. The header is broken into 4 big-endian 32-bit unsigned |
| 33 | ** integer values, as follows: |
| 34 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 35 | ** 0: Page number. |
| 36 | ** 4: For commit records, the size of the database image in pages |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 37 | ** after the commit. For all other records, zero. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 38 | ** 8: Checksum value 1. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 39 | ** 12: Checksum value 2. |
| 40 | */ |
| 41 | |
| 42 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 43 | ** WAL-INDEX FILE FORMAT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 44 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 45 | ** The wal-index file consists of a 32-byte header region, followed by an |
| 46 | ** 8-byte region that contains no useful data (used to apply byte-range locks |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 47 | ** to), followed by the data region. |
| 48 | ** |
| 49 | ** The contents of both the header and data region are specified in terms |
| 50 | ** of 1, 2 and 4 byte unsigned integers. All integers are stored in |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 51 | ** machine-endian order. The wal-index is not a persistent file and |
| 52 | ** so it does not need to be portable across archtectures. |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 53 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 54 | ** A wal-index file is essentially a shadow-pager map. It contains a |
| 55 | ** mapping from database page number to the set of locations in the wal |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 56 | ** file that contain versions of the database page. When a database |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 57 | ** client needs to read a page of data, it first queries the wal-index |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 58 | ** file to determine if the required version of the page is stored in |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 59 | ** the wal. If so, the page is read from the wal. If not, the page is |
| 60 | ** read from the database file. |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 61 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 62 | ** Whenever a transaction is appended to the wal or a checkpoint transfers |
| 63 | ** data from the wal into the database file, the wal-index is |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 64 | ** updated accordingly. |
| 65 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 66 | ** The fields in the wal-index file header are described in the comment |
| 67 | ** directly above the definition of struct WalIndexHdr (see below). |
| 68 | ** Immediately following the fields in the WalIndexHdr structure is |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 69 | ** an 8 byte checksum based on the contents of the header. This field is |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 70 | ** not the same as the iCheck1 and iCheck2 fields of the WalIndexHdr. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 71 | */ |
| 72 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 73 | /* Object declarations */ |
| 74 | typedef struct WalIndexHdr WalIndexHdr; |
| 75 | typedef struct WalIterator WalIterator; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 79 | ** The following object stores a copy of the wal-index header. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 80 | ** |
| 81 | ** Member variables iCheck1 and iCheck2 contain the checksum for the |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 82 | ** last frame written to the wal, or 2 and 3 respectively if the log |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 83 | ** is currently empty. |
| 84 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 85 | struct WalIndexHdr { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 86 | u32 iChange; /* Counter incremented each transaction */ |
| 87 | u32 pgsz; /* Database page size in bytes */ |
| 88 | u32 iLastPg; /* Address of last valid frame in log */ |
| 89 | u32 nPage; /* Size of database in pages */ |
| 90 | u32 iCheck1; /* Checkpoint value 1 */ |
| 91 | u32 iCheck2; /* Checkpoint value 2 */ |
| 92 | }; |
| 93 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 94 | /* Size of serialized WalIndexHdr object. */ |
| 95 | #define WALINDEX_HDR_NFIELD (sizeof(WalIndexHdr) / sizeof(u32)) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 96 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 97 | /* A block of 16 bytes beginning at WALINDEX_LOCK_OFFSET is reserved |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 98 | ** for locks. Since some systems only feature mandatory file-locks, we |
| 99 | ** do not read or write data from the region of the file on which locks |
| 100 | ** are applied. |
| 101 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 102 | #define WALINDEX_LOCK_OFFSET ((sizeof(WalIndexHdr))+2*sizeof(u32)) |
| 103 | #define WALINDEX_LOCK_RESERVED 8 |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 104 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 105 | /* Size of header before each frame in wal */ |
| 106 | #define WAL_FRAME_HDRSIZE 16 |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 107 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 108 | /* Size of write ahead log header */ |
| 109 | #define WAL_HDRSIZE 12 |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 112 | ** Return the offset of frame iFrame in the write-ahead log file, |
| 113 | ** assuming a database page size of pgsz bytes. The offset returned |
| 114 | ** is to the start of the write-ahead log frame-header. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 115 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 116 | #define walFrameOffset(iFrame, pgsz) ( \ |
| 117 | WAL_HDRSIZE + ((iFrame)-1)*((pgsz)+WAL_FRAME_HDRSIZE) \ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 118 | ) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 121 | ** An open write-ahead log file is represented by an instance of the |
| 122 | ** following object. |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 123 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 124 | struct Wal { |
| 125 | sqlite3_vfs *pVfs; /* The VFS used to create pFd */ |
| 126 | sqlite3_file *pFd; /* File handle for WAL file */ |
| 127 | u32 iCallback; /* Value to pass to log callback (or 0) */ |
| 128 | sqlite3_shm *pWIndex; /* The open wal-index file */ |
| 129 | int szWIndex; /* Size of the wal-index */ |
| 130 | u32 *pWiData; /* Pointer to wal-index content in memory */ |
| 131 | u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */ |
| 132 | u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */ |
| 133 | WalIndexHdr hdr; /* Wal-index for current snapshot */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 136 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 137 | /* |
| 138 | ** This structure is used to implement an iterator that iterates through |
| 139 | ** all frames in the log in database page order. Where two or more frames |
| 140 | ** correspond to the same database page, the iterator visits only the |
| 141 | ** frame most recently written to the log. |
| 142 | ** |
| 143 | ** The internals of this structure are only accessed by: |
| 144 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 145 | ** walIteratorInit() - Create a new iterator, |
| 146 | ** walIteratorNext() - Step an iterator, |
| 147 | ** walIteratorFree() - Free an iterator. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 148 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 149 | ** This functionality is used by the checkpoint code (see walCheckpoint()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 150 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 151 | struct WalIterator { |
| 152 | int nSegment; /* Size of WalIterator.aSegment[] array */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 153 | int nFinal; /* Elements in segment nSegment-1 */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 154 | struct WalSegment { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 155 | int iNext; /* Next aIndex index */ |
| 156 | u8 *aIndex; /* Pointer to index array */ |
| 157 | u32 *aDbPage; /* Pointer to db page array */ |
| 158 | } aSegment[1]; |
| 159 | }; |
| 160 | |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 161 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 162 | /* |
| 163 | ** Generate an 8 byte checksum based on the data in array aByte[] and the |
| 164 | ** initial values of aCksum[0] and aCksum[1]. The checksum is written into |
| 165 | ** aCksum[] before returning. |
dan | 56d9591 | 2010-04-24 19:07:29 +0000 | [diff] [blame] | 166 | ** |
| 167 | ** The range of bytes to checksum is treated as an array of 32-bit |
| 168 | ** little-endian unsigned integers. For each integer X in the array, from |
| 169 | ** start to finish, do the following: |
| 170 | ** |
| 171 | ** aCksum[0] += X; |
| 172 | ** aCksum[1] += aCksum[0]; |
| 173 | ** |
| 174 | ** For the calculation above, use 64-bit unsigned accumulators. Before |
| 175 | ** returning, truncate the values to 32-bits as follows: |
| 176 | ** |
| 177 | ** aCksum[0] = (u32)(aCksum[0] + (aCksum[0]>>24)); |
| 178 | ** aCksum[1] = (u32)(aCksum[1] + (aCksum[1]>>24)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 179 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 180 | static void walChecksumBytes(u8 *aByte, int nByte, u32 *aCksum){ |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 181 | u64 sum1 = aCksum[0]; |
| 182 | u64 sum2 = aCksum[1]; |
| 183 | u32 *a32 = (u32 *)aByte; |
| 184 | u32 *aEnd = (u32 *)&aByte[nByte]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 185 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 186 | assert( (nByte&0x00000003)==0 ); |
| 187 | |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 188 | if( SQLITE_LITTLEENDIAN ){ |
| 189 | #ifdef SQLITE_DEBUG |
| 190 | u8 *a = (u8 *)a32; |
| 191 | assert( *a32==(a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24)) ); |
| 192 | #endif |
| 193 | do { |
| 194 | sum1 += *a32; |
| 195 | sum2 += sum1; |
| 196 | } while( ++a32<aEnd ); |
| 197 | }else{ |
| 198 | do { |
| 199 | u8 *a = (u8*)a32; |
| 200 | sum1 += a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24); |
| 201 | sum2 += sum1; |
| 202 | } while( ++a32<aEnd ); |
| 203 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 204 | |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 205 | aCksum[0] = sum1 + (sum1>>24); |
| 206 | aCksum[1] = sum2 + (sum2>>24); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 210 | ** Attempt to change the lock status. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 211 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 212 | ** When changing the lock status to SQLITE_SHM_READ, store the |
| 213 | ** type of reader lock (either SQLITE_SHM_READ or SQLITE_SHM_READ_FULL) |
| 214 | ** in pWal->readerType. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 215 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 216 | static int walSetLock(Wal *pWal, int desiredStatus){ |
| 217 | int rc, got; |
| 218 | if( pWal->lockState==desiredStatus ) return SQLITE_OK; |
| 219 | rc = pWal->pVfs->xShmLock(pWal->pWIndex, desiredStatus, &got); |
| 220 | if( rc==SQLITE_OK ){ |
| 221 | pWal->lockState = desiredStatus; |
| 222 | if( desiredStatus==SQLITE_SHM_READ ){ |
| 223 | pWal->readerType = got; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 224 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 225 | } |
| 226 | return rc; |
| 227 | } |
| 228 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 229 | /* |
| 230 | ** Update the header of the wal-index file. |
| 231 | */ |
| 232 | static void walIndexWriteHdr(Wal *pWal, WalIndexHdr *pHdr){ |
| 233 | u32 *aHdr = pWal->pWiData; /* Write header here */ |
| 234 | u32 *aCksum = &aHdr[WALINDEX_HDR_NFIELD]; /* Write header cksum here */ |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 235 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 236 | assert( WALINDEX_HDR_NFIELD==sizeof(WalIndexHdr)/4 ); |
| 237 | assert( aHdr!=0 ); |
| 238 | memcpy(aHdr, pHdr, sizeof(WalIndexHdr)); |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 239 | aCksum[0] = aCksum[1] = 1; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 240 | walChecksumBytes((u8 *)aHdr, sizeof(WalIndexHdr), aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | ** This function encodes a single frame header and writes it to a buffer |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 245 | ** supplied by the caller. A frame-header is made up of a series of |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 246 | ** 4-byte big-endian integers, as follows: |
| 247 | ** |
| 248 | ** 0: Database page size in bytes. |
| 249 | ** 4: Page number. |
| 250 | ** 8: New database size (for commit frames, otherwise zero). |
| 251 | ** 12: Frame checksum 1. |
| 252 | ** 16: Frame checksum 2. |
| 253 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 254 | static void walEncodeFrame( |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 255 | u32 *aCksum, /* IN/OUT: Checksum values */ |
| 256 | u32 iPage, /* Database page number for frame */ |
| 257 | u32 nTruncate, /* New db size (or 0 for non-commit frames) */ |
| 258 | int nData, /* Database page size (size of aData[]) */ |
| 259 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 260 | u8 *aFrame /* OUT: Write encoded frame here */ |
| 261 | ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 262 | assert( WAL_FRAME_HDRSIZE==16 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 263 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 264 | sqlite3Put4byte(&aFrame[0], iPage); |
| 265 | sqlite3Put4byte(&aFrame[4], nTruncate); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 266 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 267 | walChecksumBytes(aFrame, 8, aCksum); |
| 268 | walChecksumBytes(aData, nData, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 269 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 270 | sqlite3Put4byte(&aFrame[8], aCksum[0]); |
| 271 | sqlite3Put4byte(&aFrame[12], aCksum[1]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
| 275 | ** Return 1 and populate *piPage, *pnTruncate and aCksum if the |
| 276 | ** frame checksum looks Ok. Otherwise return 0. |
| 277 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 278 | static int walDecodeFrame( |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 279 | u32 *aCksum, /* IN/OUT: Checksum values */ |
| 280 | u32 *piPage, /* OUT: Database page number for frame */ |
| 281 | u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */ |
| 282 | int nData, /* Database page size (size of aData[]) */ |
| 283 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 284 | u8 *aFrame /* Frame data */ |
| 285 | ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 286 | assert( WAL_FRAME_HDRSIZE==16 ); |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 287 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 288 | walChecksumBytes(aFrame, 8, aCksum); |
| 289 | walChecksumBytes(aData, nData, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 290 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 291 | if( aCksum[0]!=sqlite3Get4byte(&aFrame[8]) |
| 292 | || aCksum[1]!=sqlite3Get4byte(&aFrame[12]) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 293 | ){ |
| 294 | /* Checksum failed. */ |
| 295 | return 0; |
| 296 | } |
| 297 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 298 | *piPage = sqlite3Get4byte(&aFrame[0]); |
| 299 | *pnTruncate = sqlite3Get4byte(&aFrame[4]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 303 | static void walMergesort8( |
| 304 | Pgno *aContent, /* Pages in wal */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 305 | u8 *aBuffer, /* Buffer of at least *pnList items to use */ |
| 306 | u8 *aList, /* IN/OUT: List to sort */ |
| 307 | int *pnList /* IN/OUT: Number of elements in aList[] */ |
| 308 | ){ |
| 309 | int nList = *pnList; |
| 310 | if( nList>1 ){ |
| 311 | int nLeft = nList / 2; /* Elements in left list */ |
| 312 | int nRight = nList - nLeft; /* Elements in right list */ |
| 313 | u8 *aLeft = aList; /* Left list */ |
| 314 | u8 *aRight = &aList[nLeft]; /* Right list */ |
| 315 | int iLeft = 0; /* Current index in aLeft */ |
| 316 | int iRight = 0; /* Current index in aright */ |
| 317 | int iOut = 0; /* Current index in output buffer */ |
| 318 | |
| 319 | /* TODO: Change to non-recursive version. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 320 | walMergesort8(aContent, aBuffer, aLeft, &nLeft); |
| 321 | walMergesort8(aContent, aBuffer, aRight, &nRight); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 322 | |
| 323 | while( iRight<nRight || iLeft<nLeft ){ |
| 324 | u8 logpage; |
| 325 | Pgno dbpage; |
| 326 | |
| 327 | if( (iLeft<nLeft) |
| 328 | && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) |
| 329 | ){ |
| 330 | logpage = aLeft[iLeft++]; |
| 331 | }else{ |
| 332 | logpage = aRight[iRight++]; |
| 333 | } |
| 334 | dbpage = aContent[logpage]; |
| 335 | |
| 336 | aBuffer[iOut++] = logpage; |
| 337 | if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++; |
| 338 | |
| 339 | assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage ); |
| 340 | assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); |
| 341 | } |
| 342 | memcpy(aList, aBuffer, sizeof(aList[0])*iOut); |
| 343 | *pnList = iOut; |
| 344 | } |
| 345 | |
| 346 | #ifdef SQLITE_DEBUG |
| 347 | { |
| 348 | int i; |
| 349 | for(i=1; i<*pnList; i++){ |
| 350 | assert( aContent[aList[i]] > aContent[aList[i-1]] ); |
| 351 | } |
| 352 | } |
| 353 | #endif |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 358 | ** Return the index in the WalIndex.aData array that corresponds to |
| 359 | ** frame iFrame. The wal-index file consists of a header, followed by |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 360 | ** alternating "map" and "index" blocks. |
| 361 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 362 | static int walIndexEntry(u32 iFrame){ |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 363 | return ( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 364 | (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)/sizeof(u32) |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 365 | + (((iFrame-1)>>8)<<6) /* Indexes that occur before iFrame */ |
| 366 | + iFrame-1 /* Db page numbers that occur before iFrame */ |
| 367 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 368 | } |
| 369 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 370 | /* |
| 371 | ** Release our reference to the wal-index memory map. |
| 372 | */ |
| 373 | static void walIndexUnmap(Wal *pWal){ |
| 374 | if( pWal->pWiData ){ |
| 375 | pWal->pVfs->xShmRelease(pWal->pWIndex); |
| 376 | pWal->pWiData = 0; |
| 377 | } |
| 378 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 379 | |
| 380 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 381 | ** Map the wal-index file into memory if it isn't already. |
| 382 | */ |
| 383 | static int walIndexMap(Wal *pWal){ |
| 384 | int rc = SQLITE_OK; |
| 385 | if( pWal->pWiData==0 ){ |
| 386 | rc = pWal->pVfs->xShmSize(pWal->pWIndex, -1, |
| 387 | &pWal->szWIndex, (void**)(char*)&pWal->pWiData); |
| 388 | } |
| 389 | return rc; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | ** Resize the wal-index file. |
| 394 | */ |
| 395 | static int walIndexRemap(Wal *pWal, int newSize){ |
| 396 | int rc; |
| 397 | walIndexUnmap(pWal); |
| 398 | rc = pWal->pVfs->xShmSize(pWal->pWIndex, newSize, |
| 399 | &pWal->szWIndex, (void**)(char*)&pWal->pWiData); |
| 400 | return rc; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | ** Increment by which to increase the wal-index file size. |
| 405 | */ |
| 406 | #define WALINDEX_MMAP_INCREMENT (64*1024) |
| 407 | |
| 408 | /* |
| 409 | ** Set an entry in the wal-index map to map log frame iFrame to db |
| 410 | ** page iPage. Values are always appended to the wal-index (i.e. the |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 411 | ** value of iFrame is always exactly one more than the value passed to |
| 412 | ** the previous call), but that restriction is not enforced or asserted |
| 413 | ** here. |
| 414 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 415 | static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ |
| 416 | u32 iSlot = walIndexEntry(iFrame); |
| 417 | |
| 418 | walIndexMap(pWal); |
| 419 | while( (iSlot+128)>=pWal->szWIndex ){ |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 420 | int rc; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 421 | int nByte = pWal->szWIndex*4 + WALINDEX_MMAP_INCREMENT; |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 422 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 423 | /* Unmap and remap the wal-index file. */ |
| 424 | rc = walIndexRemap(pWal, nByte); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 425 | if( rc!=SQLITE_OK ){ |
| 426 | return rc; |
| 427 | } |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 428 | } |
| 429 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 430 | /* Set the wal-index entry itself */ |
| 431 | pWal->pWiData[iSlot] = iPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 432 | |
| 433 | /* If the frame number is a multiple of 256 (frames are numbered starting |
| 434 | ** at 1), build an index of the most recently added 256 frames. |
| 435 | */ |
| 436 | if( (iFrame&0x000000FF)==0 ){ |
| 437 | int i; /* Iterator used while initializing aIndex */ |
| 438 | u32 *aFrame; /* Pointer to array of 256 frames */ |
| 439 | int nIndex; /* Number of entries in index */ |
| 440 | u8 *aIndex; /* 256 bytes to build index in */ |
| 441 | u8 *aTmp; /* Scratch space to use while sorting */ |
| 442 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 443 | aFrame = &pWal->pWiData[iSlot-255]; |
| 444 | aIndex = (u8 *)&pWal->pWiData[iSlot+1]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 445 | aTmp = &aIndex[256]; |
| 446 | |
| 447 | nIndex = 256; |
| 448 | for(i=0; i<256; i++) aIndex[i] = (u8)i; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 449 | walMergesort8(aFrame, aTmp, aIndex, &nIndex); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 450 | memset(&aIndex[nIndex], aIndex[nIndex-1], 256-nIndex); |
| 451 | } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 452 | |
| 453 | return SQLITE_OK; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 458 | ** Recover the wal-index by reading the write-ahead log file. |
| 459 | ** The caller must hold RECOVER lock on the wal-index file. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 460 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 461 | static int walIndexRecover(Wal *pWal){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 462 | int rc; /* Return Code */ |
| 463 | i64 nSize; /* Size of log file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 464 | WalIndexHdr hdr; /* Recovered wal-index header */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 465 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 466 | assert( pWal->lockState==SQLITE_SHM_RECOVER ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 467 | memset(&hdr, 0, sizeof(hdr)); |
| 468 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 469 | rc = sqlite3OsFileSize(pWal->pFd, &nSize); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 470 | if( rc!=SQLITE_OK ){ |
| 471 | return rc; |
| 472 | } |
| 473 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 474 | if( nSize>WAL_FRAME_HDRSIZE ){ |
| 475 | u8 aBuf[WAL_FRAME_HDRSIZE]; /* Buffer to load first frame header into */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 476 | u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ |
| 477 | int nFrame; /* Number of bytes at aFrame */ |
| 478 | u8 *aData; /* Pointer to data part of aFrame buffer */ |
| 479 | int iFrame; /* Index of last frame read */ |
| 480 | i64 iOffset; /* Next offset to read from log file */ |
| 481 | int nPgsz; /* Page size according to the log */ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 482 | u32 aCksum[2]; /* Running checksum */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 483 | |
| 484 | /* Read in the first frame header in the file (to determine the |
| 485 | ** database page size). |
| 486 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 487 | rc = sqlite3OsRead(pWal->pFd, aBuf, WAL_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 488 | if( rc!=SQLITE_OK ){ |
| 489 | return rc; |
| 490 | } |
| 491 | |
| 492 | /* If the database page size is not a power of two, or is greater than |
| 493 | ** SQLITE_MAX_PAGE_SIZE, conclude that the log file contains no valid data. |
| 494 | */ |
| 495 | nPgsz = sqlite3Get4byte(&aBuf[0]); |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 496 | if( nPgsz&(nPgsz-1) || nPgsz>SQLITE_MAX_PAGE_SIZE || nPgsz<512 ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 497 | goto finished; |
| 498 | } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 499 | aCksum[0] = sqlite3Get4byte(&aBuf[4]); |
| 500 | aCksum[1] = sqlite3Get4byte(&aBuf[8]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 501 | |
| 502 | /* Malloc a buffer to read frames into. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 503 | nFrame = nPgsz + WAL_FRAME_HDRSIZE; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 504 | aFrame = (u8 *)sqlite3_malloc(nFrame); |
| 505 | if( !aFrame ){ |
| 506 | return SQLITE_NOMEM; |
| 507 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 508 | aData = &aFrame[WAL_FRAME_HDRSIZE]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 509 | |
| 510 | /* Read all frames from the log file. */ |
| 511 | iFrame = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 512 | for(iOffset=WAL_HDRSIZE; (iOffset+nFrame)<=nSize; iOffset+=nFrame){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 513 | u32 pgno; /* Database page number for frame */ |
| 514 | u32 nTruncate; /* dbsize field from frame header */ |
| 515 | int isValid; /* True if this frame is valid */ |
| 516 | |
| 517 | /* Read and decode the next log frame. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 518 | rc = sqlite3OsRead(pWal->pFd, aFrame, nFrame, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 519 | if( rc!=SQLITE_OK ) break; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 520 | isValid = walDecodeFrame(aCksum, &pgno, &nTruncate, nPgsz, aData, aFrame); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 521 | if( !isValid ) break; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 522 | walIndexAppend(pWal, ++iFrame, pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 523 | |
| 524 | /* If nTruncate is non-zero, this is a commit record. */ |
| 525 | if( nTruncate ){ |
| 526 | hdr.iCheck1 = aCksum[0]; |
| 527 | hdr.iCheck2 = aCksum[1]; |
| 528 | hdr.iLastPg = iFrame; |
| 529 | hdr.nPage = nTruncate; |
| 530 | hdr.pgsz = nPgsz; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | sqlite3_free(aFrame); |
| 535 | }else{ |
| 536 | hdr.iCheck1 = 2; |
| 537 | hdr.iCheck2 = 3; |
| 538 | } |
| 539 | |
| 540 | finished: |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 541 | walIndexWriteHdr(pWal, &hdr); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 542 | return rc; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | ** Open a connection to the log file associated with database zDb. The |
| 547 | ** database file does not actually have to exist. zDb is used only to |
| 548 | ** figure out the name of the log file to open. If the log file does not |
| 549 | ** exist it is created by this call. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 550 | ** |
| 551 | ** A SHARED lock should be held on the database file when this function |
| 552 | ** is called. The purpose of this SHARED lock is to prevent any other |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 553 | ** client from unlinking the log or wal-index file. If another process |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 554 | ** were to do this just after this client opened one of these files, the |
| 555 | ** system would be badly broken. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 556 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 557 | int sqlite3WalOpen( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 558 | sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 559 | const char *zDb, /* Name of database file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 560 | Wal **ppWal /* OUT: Allocated Wal handle */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 561 | ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 562 | int rc = SQLITE_OK; /* Return Code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 563 | Wal *pRet; /* Object to allocate and return */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 564 | int flags; /* Flags passed to OsOpen() */ |
| 565 | char *zWal = 0; /* Path to WAL file */ |
| 566 | int nWal; /* Length of zWal in bytes */ |
| 567 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 568 | assert( zDb ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 569 | if( pVfs->xShmOpen==0 ) return SQLITE_CANTOPEN; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 570 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 571 | /* Allocate an instance of struct Wal to return. */ |
| 572 | *ppWal = 0; |
| 573 | nWal = strlen(zDb); |
| 574 | pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal+11); |
| 575 | if( !pRet ) goto wal_open_out; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 576 | pRet->pVfs = pVfs; |
| 577 | pRet->pFd = (sqlite3_file *)&pRet[1]; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 578 | zWal = pVfs->szOsFile + (char*)pRet->pFd; |
| 579 | sqlite3_snprintf(nWal, zWal, "%s-wal-index", zDb); |
| 580 | rc = pVfs->xShmOpen(pVfs, zWal, &pRet->pWIndex); |
| 581 | if( rc ) goto wal_open_out; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 582 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 583 | /* Open file handle on the write-ahead log file. */ |
| 584 | zWal[nWal-6] = 0; |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 585 | flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 586 | rc = sqlite3OsOpen(pVfs, zWal, pRet->pFd, flags, &flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 587 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 588 | wal_open_out: |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 589 | if( rc!=SQLITE_OK ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 590 | if( pRet ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 591 | pVfs->xShmClose(pRet->pWIndex); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 592 | sqlite3OsClose(pRet->pFd); |
| 593 | sqlite3_free(pRet); |
| 594 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 595 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 596 | *ppWal = pRet; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 597 | return rc; |
| 598 | } |
| 599 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 600 | static int walIteratorNext( |
| 601 | WalIterator *p, /* Iterator */ |
| 602 | u32 *piPage, /* OUT: Next db page to write */ |
| 603 | u32 *piFrame /* OUT: Wal frame to read from */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 604 | ){ |
| 605 | u32 iMin = *piPage; |
| 606 | u32 iRet = 0xFFFFFFFF; |
| 607 | int i; |
| 608 | int nBlock = p->nFinal; |
| 609 | |
| 610 | for(i=p->nSegment-1; i>=0; i--){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 611 | struct WalSegment *pSegment = &p->aSegment[i]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 612 | while( pSegment->iNext<nBlock ){ |
| 613 | u32 iPg = pSegment->aDbPage[pSegment->aIndex[pSegment->iNext]]; |
| 614 | if( iPg>iMin ){ |
| 615 | if( iPg<iRet ){ |
| 616 | iRet = iPg; |
| 617 | *piFrame = i*256 + 1 + pSegment->aIndex[pSegment->iNext]; |
| 618 | } |
| 619 | break; |
| 620 | } |
| 621 | pSegment->iNext++; |
| 622 | } |
| 623 | |
| 624 | nBlock = 256; |
| 625 | } |
| 626 | |
| 627 | *piPage = iRet; |
| 628 | return (iRet==0xFFFFFFFF); |
| 629 | } |
| 630 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 631 | static WalIterator *walIteratorInit(Wal *pWal){ |
| 632 | u32 *aData; /* Content of the wal-index file */ |
| 633 | WalIterator *p; /* Return value */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 634 | int nSegment; /* Number of segments to merge */ |
| 635 | u32 iLast; /* Last frame in log */ |
| 636 | int nByte; /* Number of bytes to allocate */ |
| 637 | int i; /* Iterator variable */ |
| 638 | int nFinal; /* Number of unindexed entries */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 639 | struct WalSegment *pFinal; /* Final (unindexed) segment */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 640 | u8 *aTmp; /* Temp space used by merge-sort */ |
| 641 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 642 | walIndexMap(pWal); |
| 643 | aData = pWal->pWiData; |
| 644 | iLast = pWal->hdr.iLastPg; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 645 | nSegment = (iLast >> 8) + 1; |
| 646 | nFinal = (iLast & 0x000000FF); |
| 647 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 648 | nByte = sizeof(WalIterator) + (nSegment-1)*sizeof(struct WalSegment) + 512; |
| 649 | p = (WalIterator *)sqlite3_malloc(nByte); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 650 | if( p ){ |
| 651 | memset(p, 0, nByte); |
| 652 | p->nSegment = nSegment; |
| 653 | p->nFinal = nFinal; |
| 654 | } |
| 655 | |
| 656 | for(i=0; i<nSegment-1; i++){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 657 | p->aSegment[i].aDbPage = &aData[walIndexEntry(i*256+1)]; |
| 658 | p->aSegment[i].aIndex = (u8 *)&aData[walIndexEntry(i*256+1)+256]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 659 | } |
| 660 | pFinal = &p->aSegment[nSegment-1]; |
| 661 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 662 | pFinal->aDbPage = &aData[walIndexEntry((nSegment-1)*256+1)]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 663 | pFinal->aIndex = (u8 *)&pFinal[1]; |
| 664 | aTmp = &pFinal->aIndex[256]; |
| 665 | for(i=0; i<nFinal; i++){ |
| 666 | pFinal->aIndex[i] = i; |
| 667 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 668 | walMergesort8(pFinal->aDbPage, aTmp, pFinal->aIndex, &nFinal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 669 | p->nFinal = nFinal; |
| 670 | |
| 671 | return p; |
| 672 | } |
| 673 | |
| 674 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 675 | ** Free a log iterator allocated by walIteratorInit(). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 676 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 677 | static void walIteratorFree(WalIterator *p){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 678 | sqlite3_free(p); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | ** Checkpoint the contents of the log file. |
| 683 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 684 | static int walCheckpoint( |
| 685 | Wal *pWal, /* Wal connection */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 686 | sqlite3_file *pFd, /* File descriptor open on db file */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 687 | int sync_flags, /* Flags for OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 688 | u8 *zBuf /* Temporary buffer to use */ |
| 689 | ){ |
| 690 | int rc; /* Return code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 691 | int pgsz = pWal->hdr.pgsz; /* Database page-size */ |
| 692 | WalIterator *pIter = 0; /* Wal iterator context */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 693 | u32 iDbpage = 0; /* Next database page to write */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 694 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 695 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 696 | if( pWal->hdr.iLastPg==0 ){ |
dan | bb2e9c9 | 2010-04-15 13:33:18 +0000 | [diff] [blame] | 697 | return SQLITE_OK; |
| 698 | } |
| 699 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 700 | /* Allocate the iterator */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 701 | pIter = walIteratorInit(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 702 | if( !pIter ) return SQLITE_NOMEM; |
| 703 | |
| 704 | /* Sync the log file to disk */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 705 | if( sync_flags ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 706 | rc = sqlite3OsSync(pWal->pFd, sync_flags); |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 707 | if( rc!=SQLITE_OK ) goto out; |
| 708 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 709 | |
| 710 | /* Iterate through the contents of the log, copying data to the db file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 711 | while( 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ |
| 712 | rc = sqlite3OsRead(pWal->pFd, zBuf, pgsz, |
| 713 | walFrameOffset(iFrame, pgsz) + WAL_FRAME_HDRSIZE |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 714 | ); |
| 715 | if( rc!=SQLITE_OK ) goto out; |
| 716 | rc = sqlite3OsWrite(pFd, zBuf, pgsz, (iDbpage-1)*pgsz); |
| 717 | if( rc!=SQLITE_OK ) goto out; |
| 718 | } |
| 719 | |
| 720 | /* Truncate the database file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 721 | rc = sqlite3OsTruncate(pFd, ((i64)pWal->hdr.nPage*(i64)pgsz)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 722 | if( rc!=SQLITE_OK ) goto out; |
| 723 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 724 | /* Sync the database file. If successful, update the wal-index. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 725 | if( sync_flags ){ |
| 726 | rc = sqlite3OsSync(pFd, sync_flags); |
| 727 | if( rc!=SQLITE_OK ) goto out; |
| 728 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 729 | pWal->hdr.iLastPg = 0; |
| 730 | pWal->hdr.iCheck1 = 2; |
| 731 | pWal->hdr.iCheck2 = 3; |
| 732 | walIndexWriteHdr(pWal, &pWal->hdr); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 733 | |
| 734 | /* TODO: If a crash occurs and the current log is copied into the |
| 735 | ** database there is no problem. However, if a crash occurs while |
| 736 | ** writing the next transaction into the start of the log, such that: |
| 737 | ** |
| 738 | ** * The first transaction currently in the log is left intact, but |
| 739 | ** * The second (or subsequent) transaction is damaged, |
| 740 | ** |
| 741 | ** then the database could become corrupt. |
| 742 | ** |
| 743 | ** The easiest thing to do would be to write and sync a dummy header |
| 744 | ** into the log at this point. Unfortunately, that turns out to be |
| 745 | ** an unwelcome performance hit. Alternatives are... |
| 746 | */ |
| 747 | #if 0 |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 748 | memset(zBuf, 0, WAL_FRAME_HDRSIZE); |
| 749 | rc = sqlite3OsWrite(pWal->pFd, zBuf, WAL_FRAME_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 750 | if( rc!=SQLITE_OK ) goto out; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 751 | rc = sqlite3OsSync(pWal->pFd, pWal->sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 752 | #endif |
| 753 | |
| 754 | out: |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 755 | walIteratorFree(pIter); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 756 | return rc; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | ** Close a connection to a log file. |
| 761 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 762 | int sqlite3WalClose( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 763 | Wal *pWal, /* Wal to close */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 764 | sqlite3_file *pFd, /* Database file */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 765 | int sync_flags, /* Flags to pass to OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 766 | u8 *zBuf /* Buffer of at least page-size bytes */ |
| 767 | ){ |
| 768 | int rc = SQLITE_OK; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 769 | if( pWal ){ |
| 770 | pWal->pVfs->xShmClose(pWal->pWIndex); |
| 771 | sqlite3OsClose(pWal->pFd); |
| 772 | sqlite3_free(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 773 | } |
| 774 | return rc; |
| 775 | } |
| 776 | |
| 777 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 778 | ** Try to read the wal-index header. Attempt to verify the header |
| 779 | ** checksum. If the checksum can be verified, copy the wal-index |
| 780 | ** header into structure pWal->hdr. If the contents of pWal->hdr are |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 781 | ** modified by this and pChanged is not NULL, set *pChanged to 1. |
| 782 | ** Otherwise leave *pChanged unmodified. |
| 783 | ** |
| 784 | ** If the checksum cannot be verified return SQLITE_ERROR. |
| 785 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 786 | int walIndexTryHdr(Wal *pWal, int *pChanged){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 787 | u32 aCksum[2] = {1, 1}; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 788 | u32 aHdr[WALINDEX_HDR_NFIELD+2]; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 789 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 790 | /* Read the header. The caller may or may not have locked the wal-index |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 791 | ** file, meaning it is possible that an inconsistent snapshot is read |
| 792 | ** from the file. If this happens, return SQLITE_ERROR. The caller will |
| 793 | ** retry. Or, if the caller has already locked the file and the header |
| 794 | ** still looks inconsistent, it will run recovery. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 795 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 796 | memcpy(aHdr, pWal->pWiData, sizeof(aHdr)); |
| 797 | walChecksumBytes((u8*)aHdr, sizeof(u32)*WALINDEX_HDR_NFIELD, aCksum); |
| 798 | if( aCksum[0]!=aHdr[WALINDEX_HDR_NFIELD] |
| 799 | || aCksum[1]!=aHdr[WALINDEX_HDR_NFIELD+1] |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 800 | ){ |
| 801 | return SQLITE_ERROR; |
| 802 | } |
| 803 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 804 | if( memcmp(&pWal->hdr, aHdr, sizeof(WalIndexHdr)) ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 805 | if( pChanged ){ |
| 806 | *pChanged = 1; |
| 807 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 808 | memcpy(&pWal->hdr, aHdr, sizeof(WalIndexHdr)); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 809 | } |
| 810 | return SQLITE_OK; |
| 811 | } |
| 812 | |
| 813 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 814 | ** Read the wal-index header from the wal-index file into structure |
| 815 | ** pWal->hdr. If attempting to verify the header checksum fails, try |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 816 | ** to recover the log before returning. |
| 817 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 818 | ** If the wal-index header is successfully read, return SQLITE_OK. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 819 | ** Otherwise an SQLite error code. |
| 820 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 821 | static int walIndexReadHdr(Wal *pWal, int *pChanged){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 822 | int rc; |
| 823 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 824 | assert( pWal->lockState==SQLITE_SHM_READ ); |
| 825 | walIndexMap(pWal); |
| 826 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 827 | /* First try to read the header without a lock. Verify the checksum |
| 828 | ** before returning. This will almost always work. |
| 829 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 830 | if( SQLITE_OK==walIndexTryHdr(pWal, pChanged) ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 831 | return SQLITE_OK; |
| 832 | } |
| 833 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 834 | /* If the first attempt to read the header failed, lock the wal-index |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 835 | ** file and try again. If the header checksum verification fails this |
| 836 | ** time as well, run log recovery. |
| 837 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 838 | if( SQLITE_OK==(rc = walSetLock(pWal, SQLITE_SHM_RECOVER)) ){ |
| 839 | if( SQLITE_OK!=walIndexTryHdr(pWal, pChanged) ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 840 | if( pChanged ){ |
| 841 | *pChanged = 1; |
| 842 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 843 | rc = walIndexRecover(pWal); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 844 | if( rc==SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 845 | rc = walIndexTryHdr(pWal, 0); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 848 | walSetLock(pWal, SQLITE_SHM_READ); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return rc; |
| 852 | } |
| 853 | |
| 854 | /* |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 855 | ** Lock a snapshot. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 856 | ** |
| 857 | ** If this call obtains a new read-lock and the database contents have been |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 858 | ** modified since the most recent call to WalCloseSnapshot() on this Wal |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 859 | ** connection, then *pChanged is set to 1 before returning. Otherwise, it |
| 860 | ** is left unmodified. This is used by the pager layer to determine whether |
| 861 | ** or not any cached pages may be safely reused. |
| 862 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 863 | int sqlite3WalOpenSnapshot(Wal *pWal, int *pChanged){ |
| 864 | int rc; |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 865 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 866 | rc = walSetLock(pWal, SQLITE_SHM_READ); |
| 867 | if( rc==SQLITE_OK ){ |
| 868 | pWal->lockState = SQLITE_SHM_READ; |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 869 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 870 | rc = walIndexReadHdr(pWal, pChanged); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 871 | if( rc!=SQLITE_OK ){ |
| 872 | /* An error occured while attempting log recovery. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 873 | sqlite3WalCloseSnapshot(pWal); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 874 | }else{ |
| 875 | /* Check if the mapping needs to grow. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 876 | if( pWal->hdr.iLastPg |
| 877 | && walIndexEntry(pWal->hdr.iLastPg)>=pWal->szWIndex |
| 878 | ){ |
| 879 | rc = walIndexRemap(pWal, 0); |
| 880 | assert( rc || walIndexEntry(pWal->hdr.iLastPg)<pWal->szWIndex ); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 881 | } |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 882 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 883 | } |
| 884 | return rc; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | ** Unlock the current snapshot. |
| 889 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 890 | void sqlite3WalCloseSnapshot(Wal *pWal){ |
| 891 | if( pWal->lockState!=SQLITE_SHM_UNLOCK ){ |
| 892 | assert( pWal->lockState==SQLITE_SHM_READ ); |
| 893 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 894 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 895 | } |
| 896 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 897 | /* |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 898 | ** Read a page from the log, if it is present. |
| 899 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 900 | int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, u8 *pOut){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 901 | u32 iRead = 0; |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 902 | u32 *aData; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 903 | int iFrame = (pWal->hdr.iLastPg & 0xFFFFFF00); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 904 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 905 | assert( pWal->lockState==SQLITE_SHM_READ ); |
| 906 | walIndexMap(pWal); |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 907 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 908 | /* Do a linear search of the unindexed block of page-numbers (if any) |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 909 | ** at the end of the wal-index. An alternative to this would be to |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 910 | ** build an index in private memory each time a read transaction is |
| 911 | ** opened on a new snapshot. |
| 912 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 913 | aData = pWal->pWiData; |
| 914 | if( pWal->hdr.iLastPg ){ |
| 915 | u32 *pi = &aData[walIndexEntry(pWal->hdr.iLastPg)]; |
| 916 | u32 *piStop = pi - (pWal->hdr.iLastPg & 0xFF); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 917 | while( *pi!=pgno && pi!=piStop ) pi--; |
| 918 | if( pi!=piStop ){ |
| 919 | iRead = (pi-piStop) + iFrame; |
| 920 | } |
| 921 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 922 | assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 923 | |
| 924 | while( iRead==0 && iFrame>0 ){ |
| 925 | int iLow = 0; |
| 926 | int iHigh = 255; |
| 927 | u32 *aFrame; |
| 928 | u8 *aIndex; |
| 929 | |
| 930 | iFrame -= 256; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 931 | aFrame = &aData[walIndexEntry(iFrame+1)]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 932 | aIndex = (u8 *)&aFrame[256]; |
| 933 | |
| 934 | while( iLow<=iHigh ){ |
| 935 | int iTest = (iLow+iHigh)>>1; |
| 936 | u32 iPg = aFrame[aIndex[iTest]]; |
| 937 | |
| 938 | if( iPg==pgno ){ |
| 939 | iRead = iFrame + 1 + aIndex[iTest]; |
| 940 | break; |
| 941 | } |
| 942 | else if( iPg<pgno ){ |
| 943 | iLow = iTest+1; |
| 944 | }else{ |
| 945 | iHigh = iTest-1; |
| 946 | } |
| 947 | } |
| 948 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 949 | assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno ); |
| 950 | walIndexUnmap(pWal); |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 951 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 952 | /* If iRead is non-zero, then it is the log frame number that contains the |
| 953 | ** required page. Read and return data from the log file. |
| 954 | */ |
| 955 | if( iRead ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 956 | i64 iOffset = walFrameOffset(iRead, pWal->hdr.pgsz) + WAL_FRAME_HDRSIZE; |
| 957 | *pInWal = 1; |
| 958 | return sqlite3OsRead(pWal->pFd, pOut, pWal->hdr.pgsz, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 959 | } |
| 960 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 961 | *pInWal = 0; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 962 | return SQLITE_OK; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | /* |
| 967 | ** Set *pPgno to the size of the database file (or zero, if unknown). |
| 968 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 969 | void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){ |
| 970 | assert( pWal->lockState==SQLITE_SHM_READ |
| 971 | || pWal->lockState==SQLITE_SHM_WRITE ); |
| 972 | *pPgno = pWal->hdr.nPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 976 | ** This function returns SQLITE_OK if the caller may write to the database. |
| 977 | ** Otherwise, if the caller is operating on a snapshot that has already |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 978 | ** been overwritten by another writer, SQLITE_BUSY is returned. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 979 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 980 | int sqlite3WalWriteLock(Wal *pWal, int op){ |
| 981 | int rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 982 | if( op ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 983 | assert( pWal->lockState == SQLITE_SHM_READ ); |
| 984 | rc = walSetLock(pWal, SQLITE_SHM_WRITE); |
| 985 | }else if( pWal->lockState==SQLITE_SHM_WRITE ){ |
| 986 | rc = walSetLock(pWal, SQLITE_SHM_READ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 987 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 988 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 989 | } |
| 990 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 991 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 992 | ** The Wal object passed to this function must be holding the write-lock. |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 993 | ** |
| 994 | ** If any data has been written (but not committed) to the log file, this |
| 995 | ** function moves the write-pointer back to the start of the transaction. |
| 996 | ** |
| 997 | ** Additionally, the callback function is invoked for each frame written |
| 998 | ** to the log since the start of the transaction. If the callback returns |
| 999 | ** other than SQLITE_OK, it is not invoked again and the error code is |
| 1000 | ** returned to the caller. |
| 1001 | ** |
| 1002 | ** Otherwise, if the callback function does not return an error, this |
| 1003 | ** function returns SQLITE_OK. |
| 1004 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1005 | int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1006 | int rc = SQLITE_OK; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1007 | Pgno iMax = pWal->hdr.iLastPg; |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1008 | Pgno iFrame; |
| 1009 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1010 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
| 1011 | walIndexReadHdr(pWal, 0); |
| 1012 | for(iFrame=pWal->hdr.iLastPg+1; iFrame<=iMax && rc==SQLITE_OK; iFrame++){ |
| 1013 | rc = xUndo(pUndoCtx, pWal->pWiData[walIndexEntry(iFrame)]); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1014 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1015 | walIndexUnmap(pWal); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1016 | return rc; |
| 1017 | } |
| 1018 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1019 | /* Return an integer that records the current (uncommitted) write |
| 1020 | ** position in the WAL |
| 1021 | */ |
| 1022 | u32 sqlite3WalSavepoint(Wal *pWal){ |
| 1023 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
| 1024 | return pWal->hdr.iLastPg; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1027 | /* Move the write position of the WAL back to iFrame. Called in |
| 1028 | ** response to a ROLLBACK TO command. |
| 1029 | */ |
| 1030 | int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){ |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1031 | int rc = SQLITE_OK; |
| 1032 | u8 aCksum[8]; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1033 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1034 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1035 | pWal->hdr.iLastPg = iFrame; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1036 | if( iFrame>0 ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1037 | i64 iOffset = walFrameOffset(iFrame, pWal->hdr.pgsz) + sizeof(u32)*2; |
| 1038 | rc = sqlite3OsRead(pWal->pFd, aCksum, sizeof(aCksum), iOffset); |
| 1039 | pWal->hdr.iCheck1 = sqlite3Get4byte(&aCksum[0]); |
| 1040 | pWal->hdr.iCheck2 = sqlite3Get4byte(&aCksum[4]); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | return rc; |
| 1044 | } |
| 1045 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1046 | /* |
dan | 3306c4a | 2010-04-23 19:15:00 +0000 | [diff] [blame] | 1047 | ** Return true if data has been written but not committed to the log file. |
| 1048 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1049 | int sqlite3WalDirty(Wal *pWal){ |
| 1050 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
| 1051 | return( pWal->hdr.iLastPg!=((WalIndexHdr*)pWal->pWiData)->iLastPg ); |
dan | 3306c4a | 2010-04-23 19:15:00 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1055 | ** Write a set of frames to the log. The caller must hold the write-lock |
| 1056 | ** on the log file (obtained using sqlite3WalWriteLock()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1057 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1058 | int sqlite3WalFrames( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1059 | Wal *pWal, /* Wal handle to write to */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1060 | int nPgsz, /* Database page-size in bytes */ |
| 1061 | PgHdr *pList, /* List of dirty pages to write */ |
| 1062 | Pgno nTruncate, /* Database size after this commit */ |
| 1063 | int isCommit, /* True if this is a commit */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1064 | int sync_flags /* Flags to pass to OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1065 | ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1066 | int rc; /* Used to catch return codes */ |
| 1067 | u32 iFrame; /* Next frame address */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1068 | u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1069 | PgHdr *p; /* Iterator to run through pList with. */ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1070 | u32 aCksum[2]; /* Checksums */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1071 | PgHdr *pLast; /* Last frame in list */ |
| 1072 | int nLast = 0; /* Number of extra copies of last page */ |
| 1073 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1074 | assert( WAL_FRAME_HDRSIZE==(4 * 2 + 2*sizeof(u32)) ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1075 | assert( pList ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1076 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1077 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1078 | /* If this is the first frame written into the log, write the log |
| 1079 | ** header to the start of the log file. See comments at the top of |
| 1080 | ** this file for a description of the log-header format. |
| 1081 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1082 | assert( WAL_FRAME_HDRSIZE>=WAL_HDRSIZE ); |
| 1083 | iFrame = pWal->hdr.iLastPg; |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1084 | if( iFrame==0 ){ |
| 1085 | sqlite3Put4byte(aFrame, nPgsz); |
| 1086 | sqlite3_randomness(8, &aFrame[4]); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1087 | pWal->hdr.iCheck1 = sqlite3Get4byte(&aFrame[4]); |
| 1088 | pWal->hdr.iCheck2 = sqlite3Get4byte(&aFrame[8]); |
| 1089 | rc = sqlite3OsWrite(pWal->pFd, aFrame, WAL_HDRSIZE, 0); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1090 | if( rc!=SQLITE_OK ){ |
| 1091 | return rc; |
| 1092 | } |
| 1093 | } |
| 1094 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1095 | aCksum[0] = pWal->hdr.iCheck1; |
| 1096 | aCksum[1] = pWal->hdr.iCheck2; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1097 | |
| 1098 | /* Write the log file. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1099 | for(p=pList; p; p=p->pDirty){ |
| 1100 | u32 nDbsize; /* Db-size field for frame header */ |
| 1101 | i64 iOffset; /* Write offset in log file */ |
| 1102 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1103 | iOffset = walFrameOffset(++iFrame, nPgsz); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1104 | |
| 1105 | /* Populate and write the frame header */ |
| 1106 | nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1107 | walEncodeFrame(aCksum, p->pgno, nDbsize, nPgsz, p->pData, aFrame); |
| 1108 | rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1109 | if( rc!=SQLITE_OK ){ |
| 1110 | return rc; |
| 1111 | } |
| 1112 | |
| 1113 | /* Write the page data */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1114 | rc = sqlite3OsWrite(pWal->pFd, p->pData, nPgsz, iOffset + sizeof(aFrame)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1115 | if( rc!=SQLITE_OK ){ |
| 1116 | return rc; |
| 1117 | } |
| 1118 | pLast = p; |
| 1119 | } |
| 1120 | |
| 1121 | /* Sync the log file if the 'isSync' flag was specified. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1122 | if( sync_flags ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1123 | i64 iSegment = sqlite3OsSectorSize(pWal->pFd); |
| 1124 | i64 iOffset = walFrameOffset(iFrame+1, nPgsz); |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 1125 | |
| 1126 | assert( isCommit ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1127 | |
| 1128 | if( iSegment<SQLITE_DEFAULT_SECTOR_SIZE ){ |
| 1129 | iSegment = SQLITE_DEFAULT_SECTOR_SIZE; |
| 1130 | } |
| 1131 | iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); |
| 1132 | while( iOffset<iSegment ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1133 | walEncodeFrame(aCksum,pLast->pgno,nTruncate,nPgsz,pLast->pData,aFrame); |
| 1134 | rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1135 | if( rc!=SQLITE_OK ){ |
| 1136 | return rc; |
| 1137 | } |
| 1138 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1139 | iOffset += WAL_FRAME_HDRSIZE; |
| 1140 | rc = sqlite3OsWrite(pWal->pFd, pLast->pData, nPgsz, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1141 | if( rc!=SQLITE_OK ){ |
| 1142 | return rc; |
| 1143 | } |
| 1144 | nLast++; |
| 1145 | iOffset += nPgsz; |
| 1146 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1147 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1148 | rc = sqlite3OsSync(pWal->pFd, sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1149 | if( rc!=SQLITE_OK ){ |
| 1150 | return rc; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | /* Append data to the log summary. It is not necessary to lock the |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1155 | ** wal-index to do this as the RESERVED lock held on the db file |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1156 | ** guarantees that there are no other writers, and no data that may |
| 1157 | ** be in use by existing readers is being overwritten. |
| 1158 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1159 | iFrame = pWal->hdr.iLastPg; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1160 | for(p=pList; p; p=p->pDirty){ |
| 1161 | iFrame++; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1162 | walIndexAppend(pWal, iFrame, p->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1163 | } |
| 1164 | while( nLast>0 ){ |
| 1165 | iFrame++; |
| 1166 | nLast--; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1167 | walIndexAppend(pWal, iFrame, pLast->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /* Update the private copy of the header. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1171 | pWal->hdr.pgsz = nPgsz; |
| 1172 | pWal->hdr.iLastPg = iFrame; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1173 | if( isCommit ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1174 | pWal->hdr.iChange++; |
| 1175 | pWal->hdr.nPage = nTruncate; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1176 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1177 | pWal->hdr.iCheck1 = aCksum[0]; |
| 1178 | pWal->hdr.iCheck2 = aCksum[1]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1179 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1180 | /* If this is a commit, update the wal-index header too. */ |
| 1181 | if( isCommit ){ |
| 1182 | walIndexWriteHdr(pWal, &pWal->hdr); |
| 1183 | pWal->iCallback = iFrame; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1184 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1185 | walIndexUnmap(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1186 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1187 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1191 | ** Checkpoint the database: |
| 1192 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1193 | ** 1. Acquire a CHECKPOINT lock |
| 1194 | ** 2. Copy the contents of the log into the database file. |
| 1195 | ** 3. Zero the wal-index header (so new readers will ignore the log). |
| 1196 | ** 4. Drop the CHECKPOINT lock. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1197 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1198 | int sqlite3WalCheckpoint( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1199 | Wal *pWal, /* Wal connection */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1200 | sqlite3_file *pFd, /* File descriptor open on db file */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1201 | int sync_flags, /* Flags to sync db file with (or 0) */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1202 | u8 *zBuf, /* Temporary buffer to use */ |
| 1203 | int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ |
| 1204 | void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1205 | ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1206 | int rc; /* Return code */ |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1207 | int isChanged = 0; /* True if a new wal-index header is loaded */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1208 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1209 | assert( pWal->lockState==SQLITE_SHM_UNLOCK ); |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 1210 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1211 | /* Get the CHECKPOINT lock */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1212 | do { |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1213 | rc = walSetLock(pWal, SQLITE_SHM_CHECKPOINT); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1214 | }while( rc==SQLITE_BUSY && xBusyHandler(pBusyHandlerArg) ); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1215 | if( rc!=SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1216 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1217 | return rc; |
| 1218 | } |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1219 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1220 | /* Copy data from the log to the database file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1221 | rc = walIndexReadHdr(pWal, &isChanged); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1222 | if( rc==SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1223 | rc = walCheckpoint(pWal, pFd, sync_flags, zBuf); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1224 | } |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1225 | if( isChanged ){ |
| 1226 | /* If a new wal-index header was loaded before the checkpoint was |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1227 | ** performed, then the pager-cache associated with log pWal is now |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1228 | ** out of date. So zero the cached wal-index header to ensure that |
| 1229 | ** next time the pager opens a snapshot on this database it knows that |
| 1230 | ** the cache needs to be reset. |
| 1231 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1232 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1233 | } |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1234 | |
| 1235 | /* Release the locks. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1236 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1237 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1240 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 1241 | ** number of frames in the WAL at the point of the last commit since |
| 1242 | ** sqlite3WalCallback() was called. If no commits have occurred since |
| 1243 | ** the last call, then return 0. |
| 1244 | */ |
| 1245 | int sqlite3WalCallback(Wal *pWal){ |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1246 | u32 ret = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame^] | 1247 | if( pWal ){ |
| 1248 | ret = pWal->iCallback; |
| 1249 | pWal->iCallback = 0; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1250 | } |
| 1251 | return (int)ret; |
| 1252 | } |