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 | ** |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 13 | ** This file contains the implementation of a write-ahead log (WAL) used in |
| 14 | ** "journal_mode=WAL" mode. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 15 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 16 | ** WRITE-AHEAD LOG (WAL) FILE FORMAT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 17 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 18 | ** A wal file consists of a header followed by zero or more "frames". |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 19 | ** Each frame records the revised content of a single page from the |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 20 | ** database file. All changes to the database are recorded by writing |
| 21 | ** frames into the WAL. Transactions commit when a frame is written that |
| 22 | ** contains a commit marker. A single WAL can and usually does record |
| 23 | ** multiple transactions. Periodically, the content of the WAL is |
| 24 | ** transferred back into the database file in an operation called a |
| 25 | ** "checkpoint". |
| 26 | ** |
| 27 | ** A single WAL file can be used multiple times. In other words, the |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 28 | ** WAL can fill up with frames and then be checkpointed and then new |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 29 | ** frames can overwrite the old ones. A WAL always grows from beginning |
| 30 | ** toward the end. Checksums and counters attached to each frame are |
| 31 | ** used to determine which frames within the WAL are valid and which |
| 32 | ** are leftovers from prior checkpoints. |
| 33 | ** |
| 34 | ** The WAL header is 12 bytes in size and consists of the following three |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 35 | ** big-endian 32-bit unsigned integer values: |
| 36 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 37 | ** 0: Database page size, |
| 38 | ** 4: Randomly selected salt value 1, |
| 39 | ** 8: Randomly selected salt value 2. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 40 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 41 | ** Immediately following the header are zero or more frames. Each |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 42 | ** frame consists of a 16-byte header followed by a <page-size> bytes |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 43 | ** of page data. The header is broken into 4 big-endian 32-bit unsigned |
| 44 | ** integer values, as follows: |
| 45 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 46 | ** 0: Page number. |
| 47 | ** 4: For commit records, the size of the database image in pages |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 48 | ** after the commit. For all other records, zero. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 49 | ** 8: Checksum value 1. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 50 | ** 12: Checksum value 2. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 51 | ** |
| 52 | ** READER ALGORITHM |
| 53 | ** |
| 54 | ** To read a page from the database (call it page number P), a reader |
| 55 | ** first checks the WAL to see if it contains page P. If so, then the |
| 56 | ** last valid instance of page P that is or is followed by a commit frame |
| 57 | ** become the value read. If the WAL contains no copies of page P that |
| 58 | ** are valid and which are or are followed by a commit frame, then page |
| 59 | ** P is read from the database file. |
| 60 | ** |
| 61 | ** The reader algorithm in the previous paragraph works correctly, but |
| 62 | ** because frames for page P can appear anywhere within the WAL, the |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 63 | ** reader has to scan the entire WAL looking for page P frames. If the |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 64 | ** WAL is large (multiple megabytes is typical) that scan can be slow, |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 65 | ** and read performance suffers. To overcome this problem, a separate |
| 66 | ** data structure called the wal-index is maintained to expedite the |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 67 | ** search for frames of a particular page. |
| 68 | ** |
| 69 | ** WAL-INDEX FORMAT |
| 70 | ** |
| 71 | ** Conceptually, the wal-index is shared memory, though VFS implementations |
| 72 | ** might choose to implement the wal-index using a mmapped file. Because |
| 73 | ** the wal-index is shared memory, SQLite does not support journal_mode=WAL |
| 74 | ** on a network filesystem. All users of the database must be able to |
| 75 | ** share memory. |
| 76 | ** |
| 77 | ** The wal-index is transient. After a crash, the wal-index can (and should |
| 78 | ** be) reconstructed from the original WAL file. In fact, the VFS is required |
| 79 | ** to either truncate or zero the header of the wal-index when the last |
| 80 | ** connection to it closes. Because the wal-index is transient, it can |
| 81 | ** use an architecture-specific format; it does not have to be cross-platform. |
| 82 | ** Hence, unlike the database and WAL file formats which store all values |
| 83 | ** as big endian, the wal-index can store multi-byte values in the native |
| 84 | ** byte order of the host computer. |
| 85 | ** |
| 86 | ** The purpose of the wal-index is to answer this question quickly: Given |
| 87 | ** a page number P, return the index of the last frame for page P in the WAL, |
| 88 | ** or return NULL if there are no frames for page P in the WAL. |
| 89 | ** |
| 90 | ** The wal-index consists of a header region, followed by an one or |
| 91 | ** more index blocks. |
| 92 | ** |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 93 | ** The wal-index header contains the total number of frames within the WAL |
| 94 | ** in the the mxFrame field. Each index block contains information on |
| 95 | ** HASHTABLE_NPAGE frames. Each index block contains two sections, a |
| 96 | ** mapping which is a database page number for each frame, and a hash |
| 97 | ** table used to look up frames by page number. The mapping section is |
| 98 | ** an array of HASHTABLE_NPAGE 32-bit page numbers. The first entry on the |
| 99 | ** array is the page number for the first frame; the second entry is the |
| 100 | ** page number for the second frame; and so forth. The last index block |
| 101 | ** holds a total of (mxFrame%HASHTABLE_NPAGE) page numbers. All index |
| 102 | ** blocks other than the last are completely full with HASHTABLE_NPAGE |
| 103 | ** page numbers. All index blocks are the same size; the mapping section |
| 104 | ** of the last index block merely contains unused entries if mxFrame is |
| 105 | ** not an even multiple of HASHTABLE_NPAGE. |
| 106 | ** |
| 107 | ** Even without using the hash table, the last frame for page P |
| 108 | ** can be found by scanning the mapping sections of each index block |
| 109 | ** starting with the last index block and moving toward the first, and |
| 110 | ** within each index block, starting at the end and moving toward the |
| 111 | ** beginning. The first entry that equals P corresponds to the frame |
| 112 | ** holding the content for that page. |
| 113 | ** |
| 114 | ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers. |
| 115 | ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the |
| 116 | ** hash table for each page number in the mapping section, so the hash |
| 117 | ** table is never more than half full. The expected number of collisions |
| 118 | ** prior to finding a match is 1. Each entry of the hash table is an |
| 119 | ** 1-based index of an entry in the mapping section of the same |
| 120 | ** index block. Let K be the 1-based index of the largest entry in |
| 121 | ** the mapping section. (For index blocks other than the last, K will |
| 122 | ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block |
| 123 | ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table |
| 124 | ** contain a value greater than K. Note that no hash table slot ever |
| 125 | ** contains a zero value. |
| 126 | ** |
| 127 | ** To look for page P in the hash table, first compute a hash iKey on |
| 128 | ** P as follows: |
| 129 | ** |
| 130 | ** iKey = (P * 383) % HASHTABLE_NSLOT |
| 131 | ** |
| 132 | ** Then start scanning entries of the hash table, starting with iKey |
| 133 | ** (wrapping around to the beginning when the end of the hash table is |
| 134 | ** reached) until an unused hash slot is found. Let the first unused slot |
| 135 | ** be at index iUnused. (iUnused might be less than iKey if there was |
| 136 | ** wrap-around.) Because the hash table is never more than half full, |
| 137 | ** the search is guaranteed to eventually hit an unused entry. Let |
| 138 | ** iMax be the value between iKey and iUnused, closest to iUnused, |
| 139 | ** where aHash[iMax]==P. If there is no iMax entry (if there exists |
| 140 | ** no hash slot such that aHash[i]==p) then page P is not in the |
| 141 | ** current index block. Otherwise the iMax-th mapping entry of the |
| 142 | ** current index block corresponds to the last entry that references |
| 143 | ** page P. |
| 144 | ** |
| 145 | ** A hash search begins with the last index block and moves toward the |
| 146 | ** first index block, looking for entries corresponding to page P. On |
| 147 | ** average, only two or three slots in each index block need to be |
| 148 | ** examined in order to either find the last entry for page P, or to |
| 149 | ** establish that no such entry exists in the block. Each index block |
| 150 | ** holds over 4000 entries. So two or three index blocks are sufficient |
| 151 | ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10 |
| 152 | ** comparisons (on average) suffice to either locate a frame in the |
| 153 | ** WAL or to establish that the frame does not exist in the WAL. This |
| 154 | ** is much faster than scanning the entire 10MB WAL. |
| 155 | ** |
| 156 | ** Note that entries are added in order of increasing K. Hence, one |
| 157 | ** reader might be using some value K0 and a second reader that started |
| 158 | ** at a later time (after additional transactions were added to the WAL |
| 159 | ** and to the wal-index) might be using a different value K1, where K1>K0. |
| 160 | ** Both readers can use the same hash table and mapping section to get |
| 161 | ** the correct result. There may be entries in the hash table with |
| 162 | ** K>K0 but to the first reader, those entries will appear to be unused |
| 163 | ** slots in the hash table and so the first reader will get an answer as |
| 164 | ** if no values greater than K0 had ever been inserted into the hash table |
| 165 | ** in the first place - which is what reader one wants. Meanwhile, the |
| 166 | ** second reader using K1 will see additional values that were inserted |
| 167 | ** later, which is exactly what reader two wants. |
| 168 | ** |
| 169 | ** When a rollback occurs, the value of K is decreased. This has the |
| 170 | ** effect of automatically removing entries from the hash table. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 171 | */ |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 172 | #ifndef SQLITE_OMIT_WAL |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 173 | |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 174 | #include "wal.h" |
| 175 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 176 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 177 | /* Object declarations */ |
| 178 | typedef struct WalIndexHdr WalIndexHdr; |
| 179 | typedef struct WalIterator WalIterator; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 183 | ** The following object stores a copy of the wal-index header. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 184 | ** |
| 185 | ** Member variables iCheck1 and iCheck2 contain the checksum for the |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 186 | ** 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] | 187 | ** is currently empty. |
| 188 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 189 | struct WalIndexHdr { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 190 | u32 iChange; /* Counter incremented each transaction */ |
| 191 | u32 pgsz; /* Database page size in bytes */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 192 | u32 mxFrame; /* Index of last valid frame in the WAL */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 193 | u32 nPage; /* Size of database in pages */ |
| 194 | u32 iCheck1; /* Checkpoint value 1 */ |
| 195 | u32 iCheck2; /* Checkpoint value 2 */ |
| 196 | }; |
| 197 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 198 | /* Size of serialized WalIndexHdr object. */ |
| 199 | #define WALINDEX_HDR_NFIELD (sizeof(WalIndexHdr) / sizeof(u32)) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 200 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 201 | /* A block of 16 bytes beginning at WALINDEX_LOCK_OFFSET is reserved |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 202 | ** for locks. Since some systems only feature mandatory file-locks, we |
| 203 | ** do not read or write data from the region of the file on which locks |
| 204 | ** are applied. |
| 205 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 206 | #define WALINDEX_LOCK_OFFSET ((sizeof(WalIndexHdr))+2*sizeof(u32)) |
| 207 | #define WALINDEX_LOCK_RESERVED 8 |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 208 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 209 | /* Size of header before each frame in wal */ |
| 210 | #define WAL_FRAME_HDRSIZE 16 |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 211 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 212 | /* Size of write ahead log header */ |
| 213 | #define WAL_HDRSIZE 12 |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 214 | |
| 215 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 216 | ** Return the offset of frame iFrame in the write-ahead log file, |
| 217 | ** assuming a database page size of pgsz bytes. The offset returned |
| 218 | ** is to the start of the write-ahead log frame-header. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 219 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 220 | #define walFrameOffset(iFrame, pgsz) ( \ |
| 221 | WAL_HDRSIZE + ((iFrame)-1)*((pgsz)+WAL_FRAME_HDRSIZE) \ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 222 | ) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 223 | |
| 224 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 225 | ** An open write-ahead log file is represented by an instance of the |
| 226 | ** following object. |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 227 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 228 | struct Wal { |
| 229 | sqlite3_vfs *pVfs; /* The VFS used to create pFd */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 230 | sqlite3_file *pDbFd; /* File handle for the database file */ |
| 231 | sqlite3_file *pWalFd; /* File handle for WAL file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 232 | u32 iCallback; /* Value to pass to log callback (or 0) */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 233 | int szWIndex; /* Size of the wal-index that is mapped in mem */ |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 234 | volatile u32 *pWiData; /* Pointer to wal-index content in memory */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 235 | u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */ |
| 236 | u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */ |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 237 | u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 238 | u8 isWindexOpen; /* True if ShmOpen() called on pDbFd */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 239 | WalIndexHdr hdr; /* Wal-index for current snapshot */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 240 | char *zWalName; /* Name of WAL file */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 243 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 244 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 245 | ** This structure is used to implement an iterator that loops through |
| 246 | ** all frames in the WAL in database page order. Where two or more frames |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 247 | ** correspond to the same database page, the iterator visits only the |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 248 | ** frame most recently written to the WAL (in other words, the frame with |
| 249 | ** the largest index). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 250 | ** |
| 251 | ** The internals of this structure are only accessed by: |
| 252 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 253 | ** walIteratorInit() - Create a new iterator, |
| 254 | ** walIteratorNext() - Step an iterator, |
| 255 | ** walIteratorFree() - Free an iterator. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 256 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 257 | ** This functionality is used by the checkpoint code (see walCheckpoint()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 258 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 259 | struct WalIterator { |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 260 | int iPrior; /* Last result returned from the iterator */ |
| 261 | int nSegment; /* Size of the aSegment[] array */ |
| 262 | int nFinal; /* Elements in aSegment[nSegment-1] */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 263 | struct WalSegment { |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 264 | int iNext; /* Next slot in aIndex[] not previously returned */ |
| 265 | u8 *aIndex; /* i0, i1, i2... such that aPgno[iN] ascending */ |
| 266 | u32 *aPgno; /* 256 page numbers. Pointer to Wal.pWiData */ |
| 267 | } aSegment[1]; /* One for every 256 entries in the WAL */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 270 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 271 | /* |
| 272 | ** Generate an 8 byte checksum based on the data in array aByte[] and the |
| 273 | ** initial values of aCksum[0] and aCksum[1]. The checksum is written into |
| 274 | ** aCksum[] before returning. |
dan | 56d9591 | 2010-04-24 19:07:29 +0000 | [diff] [blame] | 275 | ** |
| 276 | ** The range of bytes to checksum is treated as an array of 32-bit |
| 277 | ** little-endian unsigned integers. For each integer X in the array, from |
| 278 | ** start to finish, do the following: |
| 279 | ** |
| 280 | ** aCksum[0] += X; |
| 281 | ** aCksum[1] += aCksum[0]; |
| 282 | ** |
| 283 | ** For the calculation above, use 64-bit unsigned accumulators. Before |
| 284 | ** returning, truncate the values to 32-bits as follows: |
| 285 | ** |
| 286 | ** aCksum[0] = (u32)(aCksum[0] + (aCksum[0]>>24)); |
| 287 | ** aCksum[1] = (u32)(aCksum[1] + (aCksum[1]>>24)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 288 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 289 | static void walChecksumBytes(u8 *aByte, int nByte, u32 *aCksum){ |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 290 | u64 sum1 = aCksum[0]; |
| 291 | u64 sum2 = aCksum[1]; |
| 292 | u32 *a32 = (u32 *)aByte; |
| 293 | u32 *aEnd = (u32 *)&aByte[nByte]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 294 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 295 | assert( (nByte&0x00000003)==0 ); |
| 296 | |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 297 | if( SQLITE_LITTLEENDIAN ){ |
| 298 | #ifdef SQLITE_DEBUG |
| 299 | u8 *a = (u8 *)a32; |
| 300 | assert( *a32==(a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24)) ); |
| 301 | #endif |
| 302 | do { |
| 303 | sum1 += *a32; |
| 304 | sum2 += sum1; |
| 305 | } while( ++a32<aEnd ); |
| 306 | }else{ |
| 307 | do { |
| 308 | u8 *a = (u8*)a32; |
| 309 | sum1 += a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24); |
| 310 | sum2 += sum1; |
| 311 | } while( ++a32<aEnd ); |
| 312 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 313 | |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 314 | aCksum[0] = sum1 + (sum1>>24); |
| 315 | aCksum[1] = sum2 + (sum2>>24); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 319 | ** Attempt to change the lock status. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 320 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 321 | ** When changing the lock status to SQLITE_SHM_READ, store the |
| 322 | ** type of reader lock (either SQLITE_SHM_READ or SQLITE_SHM_READ_FULL) |
| 323 | ** in pWal->readerType. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 324 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 325 | static int walSetLock(Wal *pWal, int desiredStatus){ |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 326 | int rc = SQLITE_OK; /* Return code */ |
| 327 | if( pWal->exclusiveMode || pWal->lockState==desiredStatus ){ |
| 328 | pWal->lockState = desiredStatus; |
| 329 | }else{ |
| 330 | int got = pWal->lockState; |
drh | 686138f | 2010-05-12 18:10:52 +0000 | [diff] [blame] | 331 | rc = sqlite3OsShmLock(pWal->pDbFd, desiredStatus, &got); |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 332 | pWal->lockState = got; |
| 333 | if( got==SQLITE_SHM_READ_FULL || got==SQLITE_SHM_READ ){ |
| 334 | pWal->readerType = got; |
| 335 | pWal->lockState = SQLITE_SHM_READ; |
| 336 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 337 | } |
| 338 | return rc; |
| 339 | } |
| 340 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 341 | /* |
| 342 | ** Update the header of the wal-index file. |
| 343 | */ |
| 344 | static void walIndexWriteHdr(Wal *pWal, WalIndexHdr *pHdr){ |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 345 | volatile u32 *aHdr = pWal->pWiData; /* Write header here */ |
| 346 | volatile u32 *aCksum = &aHdr[WALINDEX_HDR_NFIELD]; /* Write cksum here */ |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 347 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 348 | assert( WALINDEX_HDR_NFIELD==sizeof(WalIndexHdr)/4 ); |
| 349 | assert( aHdr!=0 ); |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 350 | memcpy((void*)aHdr, pHdr, sizeof(WalIndexHdr)); |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 351 | aCksum[0] = aCksum[1] = 1; |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 352 | walChecksumBytes((u8*)aHdr, sizeof(WalIndexHdr), (u32*)aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
| 356 | ** This function encodes a single frame header and writes it to a buffer |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 357 | ** 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] | 358 | ** 4-byte big-endian integers, as follows: |
| 359 | ** |
| 360 | ** 0: Database page size in bytes. |
| 361 | ** 4: Page number. |
| 362 | ** 8: New database size (for commit frames, otherwise zero). |
| 363 | ** 12: Frame checksum 1. |
| 364 | ** 16: Frame checksum 2. |
| 365 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 366 | static void walEncodeFrame( |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 367 | u32 *aCksum, /* IN/OUT: Checksum values */ |
| 368 | u32 iPage, /* Database page number for frame */ |
| 369 | u32 nTruncate, /* New db size (or 0 for non-commit frames) */ |
| 370 | int nData, /* Database page size (size of aData[]) */ |
| 371 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 372 | u8 *aFrame /* OUT: Write encoded frame here */ |
| 373 | ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 374 | assert( WAL_FRAME_HDRSIZE==16 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 375 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 376 | sqlite3Put4byte(&aFrame[0], iPage); |
| 377 | sqlite3Put4byte(&aFrame[4], nTruncate); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 378 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 379 | walChecksumBytes(aFrame, 8, aCksum); |
| 380 | walChecksumBytes(aData, nData, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 381 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 382 | sqlite3Put4byte(&aFrame[8], aCksum[0]); |
| 383 | sqlite3Put4byte(&aFrame[12], aCksum[1]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
| 387 | ** Return 1 and populate *piPage, *pnTruncate and aCksum if the |
| 388 | ** frame checksum looks Ok. Otherwise return 0. |
| 389 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 390 | static int walDecodeFrame( |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 391 | u32 *aCksum, /* IN/OUT: Checksum values */ |
| 392 | u32 *piPage, /* OUT: Database page number for frame */ |
| 393 | u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */ |
| 394 | int nData, /* Database page size (size of aData[]) */ |
| 395 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 396 | u8 *aFrame /* Frame data */ |
| 397 | ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 398 | assert( WAL_FRAME_HDRSIZE==16 ); |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 399 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 400 | walChecksumBytes(aFrame, 8, aCksum); |
| 401 | walChecksumBytes(aData, nData, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 402 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 403 | if( aCksum[0]!=sqlite3Get4byte(&aFrame[8]) |
| 404 | || aCksum[1]!=sqlite3Get4byte(&aFrame[12]) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 405 | ){ |
| 406 | /* Checksum failed. */ |
| 407 | return 0; |
| 408 | } |
| 409 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 410 | *piPage = sqlite3Get4byte(&aFrame[0]); |
| 411 | *pnTruncate = sqlite3Get4byte(&aFrame[4]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 412 | return 1; |
| 413 | } |
| 414 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 415 | /* |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 416 | ** Define the parameters of the hash tables in the wal-index file. There |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 417 | ** is a hash-table following every HASHTABLE_NPAGE page numbers in the |
| 418 | ** wal-index. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 419 | ** |
| 420 | ** Changing any of these constants will alter the wal-index format and |
| 421 | ** create incompatibilities. |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 422 | */ |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 423 | #define HASHTABLE_NPAGE 4096 /* Must be power of 2 and multiple of 256 */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 424 | #define HASHTABLE_DATATYPE u16 |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 425 | #define HASHTABLE_HASH_1 383 /* Should be prime */ |
| 426 | #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */ |
| 427 | #define HASHTABLE_NBYTE (sizeof(HASHTABLE_DATATYPE)*HASHTABLE_NSLOT) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 428 | |
| 429 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 430 | ** Return the index in the Wal.pWiData array that corresponds to |
| 431 | ** frame iFrame. |
| 432 | ** |
| 433 | ** Wal.pWiData is an array of u32 elements that is the wal-index. |
| 434 | ** The array begins with a header and is then followed by alternating |
| 435 | ** "map" and "hash-table" blocks. Each "map" block consists of |
| 436 | ** HASHTABLE_NPAGE u32 elements which are page numbers corresponding |
| 437 | ** to frames in the WAL file. |
| 438 | ** |
| 439 | ** This routine returns an index X such that Wal.pWiData[X] is part |
| 440 | ** of a "map" block that contains the page number of the iFrame-th |
| 441 | ** frame in the WAL file. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 442 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 443 | static int walIndexEntry(u32 iFrame){ |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 444 | return ( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 445 | (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)/sizeof(u32) |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 446 | + (((iFrame-1)/HASHTABLE_NPAGE) * HASHTABLE_NBYTE)/sizeof(u32) |
| 447 | + (iFrame-1) |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 448 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 449 | } |
| 450 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 451 | /* |
dan | b7d53f5 | 2010-05-06 17:28:08 +0000 | [diff] [blame] | 452 | ** Return the minimum mapping size in bytes that can be used to read the |
| 453 | ** wal-index up to and including frame iFrame. If iFrame is the last frame |
| 454 | ** in a block of 256 frames, the returned byte-count includes the space |
| 455 | ** required by the 256-byte index block. |
| 456 | */ |
| 457 | static int walMappingSize(u32 iFrame){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 458 | const int nByte = (sizeof(u32)*HASHTABLE_NPAGE + HASHTABLE_NBYTE) ; |
| 459 | return ( WALINDEX_LOCK_OFFSET |
| 460 | + WALINDEX_LOCK_RESERVED |
| 461 | + nByte * ((iFrame + HASHTABLE_NPAGE - 1)/HASHTABLE_NPAGE) |
dan | b7d53f5 | 2010-05-06 17:28:08 +0000 | [diff] [blame] | 462 | ); |
| 463 | } |
| 464 | |
| 465 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 466 | ** Release our reference to the wal-index memory map, if we are holding |
| 467 | ** it. |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 468 | */ |
| 469 | static void walIndexUnmap(Wal *pWal){ |
| 470 | if( pWal->pWiData ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 471 | sqlite3OsShmRelease(pWal->pDbFd); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 472 | pWal->pWiData = 0; |
| 473 | } |
| 474 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 475 | |
| 476 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 477 | ** Map the wal-index file into memory if it isn't already. |
| 478 | ** |
| 479 | ** The reqSize parameter is the minimum required size of the mapping. |
dan | 998ad21 | 2010-05-07 06:59:08 +0000 | [diff] [blame] | 480 | ** A value of -1 means "don't care". |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 481 | */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 482 | static int walIndexMap(Wal *pWal, int reqSize){ |
| 483 | int rc = SQLITE_OK; |
dan | 998ad21 | 2010-05-07 06:59:08 +0000 | [diff] [blame] | 484 | if( pWal->pWiData==0 || reqSize>pWal->szWIndex ){ |
drh | 5500a1f | 2010-05-13 09:11:31 +0000 | [diff] [blame] | 485 | walIndexUnmap(pWal); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 486 | rc = sqlite3OsShmGet(pWal->pDbFd, reqSize, &pWal->szWIndex, |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 487 | (void volatile**)(char volatile*)&pWal->pWiData); |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 488 | if( rc==SQLITE_OK && pWal->pWiData==0 ){ |
| 489 | /* Make sure pWal->pWiData is not NULL while we are holding the |
| 490 | ** lock on the mapping. */ |
| 491 | assert( pWal->szWIndex==0 ); |
| 492 | pWal->pWiData = &pWal->iCallback; |
| 493 | } |
dan | 65f2ac5 | 2010-05-07 09:43:50 +0000 | [diff] [blame] | 494 | if( rc!=SQLITE_OK ){ |
| 495 | walIndexUnmap(pWal); |
| 496 | } |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 497 | } |
| 498 | return rc; |
| 499 | } |
| 500 | |
| 501 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 502 | ** Remap the wal-index so that the mapping covers the full size |
| 503 | ** of the underlying file. |
| 504 | ** |
| 505 | ** If enlargeTo is non-negative, then increase the size of the underlying |
| 506 | ** storage to be at least as big as enlargeTo before remapping. |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 507 | */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 508 | static int walIndexRemap(Wal *pWal, int enlargeTo){ |
| 509 | int rc; |
| 510 | int sz; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 511 | rc = sqlite3OsShmSize(pWal->pDbFd, enlargeTo, &sz); |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 512 | if( rc==SQLITE_OK && sz>pWal->szWIndex ){ |
| 513 | walIndexUnmap(pWal); |
| 514 | rc = walIndexMap(pWal, sz); |
| 515 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 516 | return rc; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | ** Increment by which to increase the wal-index file size. |
| 521 | */ |
| 522 | #define WALINDEX_MMAP_INCREMENT (64*1024) |
| 523 | |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | ** Compute a hash on a page number. The resulting hash value must land |
| 527 | ** between 0 and (HASHTABLE_NSLOT-1). |
| 528 | */ |
| 529 | static int walHash(u32 iPage){ |
| 530 | assert( iPage>0 ); |
| 531 | assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 ); |
| 532 | return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1); |
| 533 | } |
| 534 | static int walNextHash(int iPriorHash){ |
| 535 | return (iPriorHash+1)&(HASHTABLE_NSLOT-1); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
| 539 | /* |
| 540 | ** Find the hash table and (section of the) page number array used to |
| 541 | ** store data for WAL frame iFrame. |
| 542 | ** |
| 543 | ** Set output variable *paHash to point to the start of the hash table |
| 544 | ** in the wal-index file. Set *piZero to one less than the frame |
| 545 | ** number of the first frame indexed by this hash table. If a |
| 546 | ** slot in the hash table is set to N, it refers to frame number |
| 547 | ** (*piZero+N) in the log. |
| 548 | ** |
| 549 | ** Finally, set *paPgno such that for all frames F between (*piZero+1) and |
| 550 | ** (*piZero+HASHTABLE_NPAGE), (*paPgno)[F] is the database page number |
| 551 | ** associated with frame F. |
| 552 | */ |
| 553 | static void walHashFind( |
| 554 | Wal *pWal, /* WAL handle */ |
| 555 | u32 iFrame, /* Find the hash table indexing this frame */ |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 556 | volatile HASHTABLE_DATATYPE **paHash, /* OUT: Pointer to hash index */ |
| 557 | volatile u32 **paPgno, /* OUT: Pointer to page number array */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 558 | u32 *piZero /* OUT: Frame associated with *paPgno[0] */ |
| 559 | ){ |
| 560 | u32 iZero; |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 561 | volatile u32 *aPgno; |
| 562 | volatile HASHTABLE_DATATYPE *aHash; |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 563 | |
| 564 | iZero = ((iFrame-1)/HASHTABLE_NPAGE) * HASHTABLE_NPAGE; |
| 565 | aPgno = &pWal->pWiData[walIndexEntry(iZero+1)-iZero-1]; |
| 566 | aHash = (HASHTABLE_DATATYPE *)&aPgno[iZero+HASHTABLE_NPAGE+1]; |
| 567 | |
| 568 | /* Assert that: |
| 569 | ** |
| 570 | ** + the mapping is large enough for this hash-table, and |
| 571 | ** |
| 572 | ** + that aPgno[iZero+1] really is the database page number associated |
| 573 | ** with the first frame indexed by this hash table. |
| 574 | */ |
| 575 | assert( (u32*)(&aHash[HASHTABLE_NSLOT])<=&pWal->pWiData[pWal->szWIndex/4] ); |
| 576 | assert( walIndexEntry(iZero+1)==(&aPgno[iZero+1] - pWal->pWiData) ); |
| 577 | |
| 578 | *paHash = aHash; |
| 579 | *paPgno = aPgno; |
| 580 | *piZero = iZero; |
| 581 | } |
| 582 | |
| 583 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 584 | /* |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 585 | ** Set an entry in the wal-index that will map database page number |
| 586 | ** pPage into WAL frame iFrame. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 587 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 588 | static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 589 | int rc; /* Return code */ |
| 590 | int nMapping; /* Required mapping size in bytes */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 591 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 592 | /* Make sure the wal-index is mapped. Enlarge the mapping if required. */ |
| 593 | nMapping = walMappingSize(iFrame); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 594 | rc = walIndexMap(pWal, -1); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 595 | while( rc==SQLITE_OK && nMapping>pWal->szWIndex ){ |
dan | c9d53db | 2010-04-30 16:50:00 +0000 | [diff] [blame] | 596 | int nByte = pWal->szWIndex + WALINDEX_MMAP_INCREMENT; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 597 | rc = walIndexRemap(pWal, nByte); |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 598 | } |
| 599 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 600 | /* Assuming the wal-index file was successfully mapped, find the hash |
| 601 | ** table and section of of the page number array that pertain to frame |
| 602 | ** iFrame of the WAL. Then populate the page number array and the hash |
| 603 | ** table entry. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 604 | */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 605 | if( rc==SQLITE_OK ){ |
| 606 | int iKey; /* Hash table key */ |
| 607 | u32 iZero; /* One less than frame number of aPgno[1] */ |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 608 | volatile u32 *aPgno; /* Page number array */ |
| 609 | volatile HASHTABLE_DATATYPE *aHash; /* Hash table */ |
| 610 | int idx; /* Value to write to hash-table slot */ |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 611 | TESTONLY( int nCollide = 0; /* Number of hash collisions */ ) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 612 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 613 | walHashFind(pWal, iFrame, &aHash, &aPgno, &iZero); |
| 614 | idx = iFrame - iZero; |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 615 | if( idx==1 ) memset((void*)aHash, 0xff, HASHTABLE_NBYTE); |
| 616 | assert( idx <= HASHTABLE_NSLOT/2 + 1 ); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 617 | aPgno[iFrame] = iPage; |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 618 | for(iKey=walHash(iPage); aHash[iKey]<idx; iKey=walNextHash(iKey)){ |
| 619 | assert( nCollide++ < idx ); |
| 620 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 621 | aHash[iKey] = idx; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 622 | } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 623 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 624 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 629 | ** Recover the wal-index by reading the write-ahead log file. |
| 630 | ** The caller must hold RECOVER lock on the wal-index file. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 631 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 632 | static int walIndexRecover(Wal *pWal){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 633 | int rc; /* Return Code */ |
| 634 | i64 nSize; /* Size of log file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 635 | WalIndexHdr hdr; /* Recovered wal-index header */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 636 | |
dan | 65be0d8 | 2010-05-06 18:48:27 +0000 | [diff] [blame] | 637 | assert( pWal->lockState>SQLITE_SHM_READ ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 638 | memset(&hdr, 0, sizeof(hdr)); |
| 639 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 640 | rc = sqlite3OsFileSize(pWal->pWalFd, &nSize); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 641 | if( rc!=SQLITE_OK ){ |
| 642 | return rc; |
| 643 | } |
| 644 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 645 | if( nSize>WAL_FRAME_HDRSIZE ){ |
| 646 | u8 aBuf[WAL_FRAME_HDRSIZE]; /* Buffer to load first frame header into */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 647 | u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ |
| 648 | int nFrame; /* Number of bytes at aFrame */ |
| 649 | u8 *aData; /* Pointer to data part of aFrame buffer */ |
| 650 | int iFrame; /* Index of last frame read */ |
| 651 | i64 iOffset; /* Next offset to read from log file */ |
| 652 | int nPgsz; /* Page size according to the log */ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 653 | u32 aCksum[2]; /* Running checksum */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 654 | |
| 655 | /* Read in the first frame header in the file (to determine the |
| 656 | ** database page size). |
| 657 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 658 | rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 659 | if( rc!=SQLITE_OK ){ |
| 660 | return rc; |
| 661 | } |
| 662 | |
| 663 | /* If the database page size is not a power of two, or is greater than |
| 664 | ** SQLITE_MAX_PAGE_SIZE, conclude that the log file contains no valid data. |
| 665 | */ |
| 666 | nPgsz = sqlite3Get4byte(&aBuf[0]); |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 667 | if( nPgsz&(nPgsz-1) || nPgsz>SQLITE_MAX_PAGE_SIZE || nPgsz<512 ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 668 | goto finished; |
| 669 | } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 670 | aCksum[0] = sqlite3Get4byte(&aBuf[4]); |
| 671 | aCksum[1] = sqlite3Get4byte(&aBuf[8]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 672 | |
| 673 | /* Malloc a buffer to read frames into. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 674 | nFrame = nPgsz + WAL_FRAME_HDRSIZE; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 675 | aFrame = (u8 *)sqlite3_malloc(nFrame); |
| 676 | if( !aFrame ){ |
| 677 | return SQLITE_NOMEM; |
| 678 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 679 | aData = &aFrame[WAL_FRAME_HDRSIZE]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 680 | |
| 681 | /* Read all frames from the log file. */ |
| 682 | iFrame = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 683 | for(iOffset=WAL_HDRSIZE; (iOffset+nFrame)<=nSize; iOffset+=nFrame){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 684 | u32 pgno; /* Database page number for frame */ |
| 685 | u32 nTruncate; /* dbsize field from frame header */ |
| 686 | int isValid; /* True if this frame is valid */ |
| 687 | |
| 688 | /* Read and decode the next log frame. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 689 | rc = sqlite3OsRead(pWal->pWalFd, aFrame, nFrame, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 690 | if( rc!=SQLITE_OK ) break; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 691 | isValid = walDecodeFrame(aCksum, &pgno, &nTruncate, nPgsz, aData, aFrame); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 692 | if( !isValid ) break; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 693 | rc = walIndexAppend(pWal, ++iFrame, pgno); |
| 694 | if( rc!=SQLITE_OK ) break; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 695 | |
| 696 | /* If nTruncate is non-zero, this is a commit record. */ |
| 697 | if( nTruncate ){ |
| 698 | hdr.iCheck1 = aCksum[0]; |
| 699 | hdr.iCheck2 = aCksum[1]; |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 700 | hdr.mxFrame = iFrame; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 701 | hdr.nPage = nTruncate; |
| 702 | hdr.pgsz = nPgsz; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | sqlite3_free(aFrame); |
| 707 | }else{ |
| 708 | hdr.iCheck1 = 2; |
| 709 | hdr.iCheck2 = 3; |
| 710 | } |
| 711 | |
| 712 | finished: |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 713 | if( rc==SQLITE_OK && hdr.mxFrame==0 ){ |
dan | 576bc32 | 2010-05-06 18:04:50 +0000 | [diff] [blame] | 714 | rc = walIndexRemap(pWal, WALINDEX_MMAP_INCREMENT); |
| 715 | } |
| 716 | if( rc==SQLITE_OK ){ |
| 717 | walIndexWriteHdr(pWal, &hdr); |
| 718 | memcpy(&pWal->hdr, &hdr, sizeof(hdr)); |
| 719 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 720 | return rc; |
| 721 | } |
| 722 | |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 723 | /* |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 724 | ** Close an open wal-index. |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 725 | */ |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 726 | static void walIndexClose(Wal *pWal, int isDelete){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 727 | if( pWal->isWindexOpen ){ |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 728 | int notUsed; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 729 | sqlite3OsShmLock(pWal->pDbFd, SQLITE_SHM_UNLOCK, ¬Used); |
| 730 | sqlite3OsShmClose(pWal->pDbFd, isDelete); |
| 731 | pWal->isWindexOpen = 0; |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 735 | /* |
| 736 | ** Open a connection to the log file associated with database zDb. The |
| 737 | ** database file does not actually have to exist. zDb is used only to |
| 738 | ** figure out the name of the log file to open. If the log file does not |
| 739 | ** exist it is created by this call. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 740 | ** |
| 741 | ** A SHARED lock should be held on the database file when this function |
| 742 | ** is called. The purpose of this SHARED lock is to prevent any other |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 743 | ** client from unlinking the log or wal-index file. If another process |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 744 | ** were to do this just after this client opened one of these files, the |
| 745 | ** system would be badly broken. |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 746 | ** |
| 747 | ** If the log file is successfully opened, SQLITE_OK is returned and |
| 748 | ** *ppWal is set to point to a new WAL handle. If an error occurs, |
| 749 | ** an SQLite error code is returned and *ppWal is left unmodified. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 750 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 751 | int sqlite3WalOpen( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 752 | sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 753 | sqlite3_file *pDbFd, /* The open database file */ |
| 754 | const char *zDbName, /* Name of the database file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 755 | Wal **ppWal /* OUT: Allocated Wal handle */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 756 | ){ |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 757 | int rc; /* Return Code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 758 | Wal *pRet; /* Object to allocate and return */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 759 | int flags; /* Flags passed to OsOpen() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 760 | char *zWal; /* Name of write-ahead log file */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 761 | int nWal; /* Length of zWal in bytes */ |
| 762 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 763 | assert( zDbName && zDbName[0] ); |
| 764 | assert( pDbFd ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 765 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 766 | /* Allocate an instance of struct Wal to return. */ |
| 767 | *ppWal = 0; |
drh | 686138f | 2010-05-12 18:10:52 +0000 | [diff] [blame] | 768 | nWal = sqlite3Strlen30(zDbName) + 5; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 769 | pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal); |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 770 | if( !pRet ){ |
| 771 | return SQLITE_NOMEM; |
| 772 | } |
| 773 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 774 | pRet->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 775 | pRet->pWalFd = (sqlite3_file *)&pRet[1]; |
| 776 | pRet->pDbFd = pDbFd; |
| 777 | pRet->zWalName = zWal = pVfs->szOsFile + (char*)pRet->pWalFd; |
| 778 | sqlite3_snprintf(nWal, zWal, "%s-wal", zDbName); |
| 779 | rc = sqlite3OsShmOpen(pDbFd); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 780 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 781 | /* Open file handle on the write-ahead log file. */ |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 782 | if( rc==SQLITE_OK ){ |
dan | bd50dde | 2010-05-13 07:08:53 +0000 | [diff] [blame] | 783 | pRet->isWindexOpen = 1; |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 784 | flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 785 | rc = sqlite3OsOpen(pVfs, zWal, pRet->pWalFd, flags, &flags); |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 786 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 787 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 788 | if( rc!=SQLITE_OK ){ |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 789 | walIndexClose(pRet, 0); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 790 | sqlite3OsClose(pRet->pWalFd); |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 791 | sqlite3_free(pRet); |
| 792 | }else{ |
| 793 | *ppWal = pRet; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 794 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 795 | return rc; |
| 796 | } |
| 797 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 798 | /* |
| 799 | ** Find the smallest page number out of all pages held in the WAL that |
| 800 | ** has not been returned by any prior invocation of this method on the |
| 801 | ** same WalIterator object. Write into *piFrame the frame index where |
| 802 | ** that page was last written into the WAL. Write into *piPage the page |
| 803 | ** number. |
| 804 | ** |
| 805 | ** Return 0 on success. If there are no pages in the WAL with a page |
| 806 | ** number larger than *piPage, then return 1. |
| 807 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 808 | static int walIteratorNext( |
| 809 | WalIterator *p, /* Iterator */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 810 | u32 *piPage, /* OUT: The page number of the next page */ |
| 811 | u32 *piFrame /* OUT: Wal frame index of next page */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 812 | ){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 813 | u32 iMin; /* Result pgno must be greater than iMin */ |
| 814 | u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */ |
| 815 | int i; /* For looping through segments */ |
| 816 | int nBlock = p->nFinal; /* Number of entries in current segment */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 817 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 818 | iMin = p->iPrior; |
| 819 | assert( iMin<0xffffffff ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 820 | for(i=p->nSegment-1; i>=0; i--){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 821 | struct WalSegment *pSegment = &p->aSegment[i]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 822 | while( pSegment->iNext<nBlock ){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 823 | u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 824 | if( iPg>iMin ){ |
| 825 | if( iPg<iRet ){ |
| 826 | iRet = iPg; |
| 827 | *piFrame = i*256 + 1 + pSegment->aIndex[pSegment->iNext]; |
| 828 | } |
| 829 | break; |
| 830 | } |
| 831 | pSegment->iNext++; |
| 832 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 833 | nBlock = 256; |
| 834 | } |
| 835 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 836 | *piPage = p->iPrior = iRet; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 837 | return (iRet==0xFFFFFFFF); |
| 838 | } |
| 839 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 840 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 841 | static void walMergesort8( |
| 842 | Pgno *aContent, /* Pages in wal */ |
| 843 | u8 *aBuffer, /* Buffer of at least *pnList items to use */ |
| 844 | u8 *aList, /* IN/OUT: List to sort */ |
| 845 | int *pnList /* IN/OUT: Number of elements in aList[] */ |
| 846 | ){ |
| 847 | int nList = *pnList; |
| 848 | if( nList>1 ){ |
| 849 | int nLeft = nList / 2; /* Elements in left list */ |
| 850 | int nRight = nList - nLeft; /* Elements in right list */ |
| 851 | u8 *aLeft = aList; /* Left list */ |
| 852 | u8 *aRight = &aList[nLeft]; /* Right list */ |
| 853 | int iLeft = 0; /* Current index in aLeft */ |
| 854 | int iRight = 0; /* Current index in aright */ |
| 855 | int iOut = 0; /* Current index in output buffer */ |
| 856 | |
| 857 | /* TODO: Change to non-recursive version. */ |
| 858 | walMergesort8(aContent, aBuffer, aLeft, &nLeft); |
| 859 | walMergesort8(aContent, aBuffer, aRight, &nRight); |
| 860 | |
| 861 | while( iRight<nRight || iLeft<nLeft ){ |
| 862 | u8 logpage; |
| 863 | Pgno dbpage; |
| 864 | |
| 865 | if( (iLeft<nLeft) |
| 866 | && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) |
| 867 | ){ |
| 868 | logpage = aLeft[iLeft++]; |
| 869 | }else{ |
| 870 | logpage = aRight[iRight++]; |
| 871 | } |
| 872 | dbpage = aContent[logpage]; |
| 873 | |
| 874 | aBuffer[iOut++] = logpage; |
| 875 | if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++; |
| 876 | |
| 877 | assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage ); |
| 878 | assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); |
| 879 | } |
| 880 | memcpy(aList, aBuffer, sizeof(aList[0])*iOut); |
| 881 | *pnList = iOut; |
| 882 | } |
| 883 | |
| 884 | #ifdef SQLITE_DEBUG |
| 885 | { |
| 886 | int i; |
| 887 | for(i=1; i<*pnList; i++){ |
| 888 | assert( aContent[aList[i]] > aContent[aList[i-1]] ); |
| 889 | } |
| 890 | } |
| 891 | #endif |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | ** Map the wal-index into memory owned by this thread, if it is not |
| 896 | ** mapped already. Then construct a WalInterator object that can be |
| 897 | ** used to loop over all pages in the WAL in ascending order. |
| 898 | ** |
| 899 | ** On success, make *pp point to the newly allocated WalInterator object |
| 900 | ** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error |
| 901 | ** code. |
| 902 | ** |
| 903 | ** The calling routine should invoke walIteratorFree() to destroy the |
| 904 | ** WalIterator object when it has finished with it. The caller must |
| 905 | ** also unmap the wal-index. But the wal-index must not be unmapped |
| 906 | ** prior to the WalIterator object being destroyed. |
| 907 | */ |
| 908 | static int walIteratorInit(Wal *pWal, WalIterator **pp){ |
| 909 | u32 *aData; /* Content of the wal-index file */ |
| 910 | WalIterator *p; /* Return value */ |
| 911 | int nSegment; /* Number of segments to merge */ |
| 912 | u32 iLast; /* Last frame in log */ |
| 913 | int nByte; /* Number of bytes to allocate */ |
| 914 | int i; /* Iterator variable */ |
| 915 | int nFinal; /* Number of unindexed entries */ |
| 916 | u8 *aTmp; /* Temp space used by merge-sort */ |
| 917 | int rc; /* Return code of walIndexMap() */ |
| 918 | u8 *aSpace; /* Surplus space on the end of the allocation */ |
| 919 | |
| 920 | /* Make sure the wal-index is mapped into local memory */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 921 | rc = walIndexMap(pWal, walMappingSize(pWal->hdr.mxFrame)); |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 922 | if( rc!=SQLITE_OK ){ |
| 923 | return rc; |
| 924 | } |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 925 | |
| 926 | /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other |
| 927 | ** thread is able to write to shared memory while this routine is |
| 928 | ** running (or, indeed, while the WalIterator object exists). Hence, |
| 929 | ** we can cast off the volatile qualifacation from shared memory |
| 930 | */ |
| 931 | assert( pWal->lockState==SQLITE_SHM_CHECKPOINT ); |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 932 | aData = (u32*)pWal->pWiData; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 933 | |
| 934 | /* Allocate space for the WalIterator object */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 935 | iLast = pWal->hdr.mxFrame; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 936 | nSegment = (iLast >> 8) + 1; |
| 937 | nFinal = (iLast & 0x000000FF); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 938 | nByte = sizeof(WalIterator) + (nSegment+1)*(sizeof(struct WalSegment)+256); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 939 | p = (WalIterator *)sqlite3_malloc(nByte); |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 940 | if( !p ){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 941 | return SQLITE_NOMEM; |
| 942 | } |
| 943 | memset(p, 0, nByte); |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 944 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 945 | /* Initialize the WalIterator object. Each 256-entry segment is |
| 946 | ** presorted in order to make iterating through all entries much |
| 947 | ** faster. |
| 948 | */ |
| 949 | p->nSegment = nSegment; |
| 950 | aSpace = (u8 *)&p->aSegment[nSegment]; |
| 951 | aTmp = &aSpace[nSegment*256]; |
| 952 | for(i=0; i<nSegment; i++){ |
| 953 | int j; |
| 954 | int nIndex = (i==nSegment-1) ? nFinal : 256; |
| 955 | p->aSegment[i].aPgno = &aData[walIndexEntry(i*256+1)]; |
| 956 | p->aSegment[i].aIndex = aSpace; |
| 957 | for(j=0; j<nIndex; j++){ |
| 958 | aSpace[j] = j; |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 959 | } |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 960 | walMergesort8(p->aSegment[i].aPgno, aTmp, aSpace, &nIndex); |
| 961 | memset(&aSpace[nIndex], aSpace[nIndex-1], 256-nIndex); |
| 962 | aSpace += 256; |
| 963 | p->nFinal = nIndex; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 964 | } |
| 965 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 966 | /* Return the fully initializd WalIterator object */ |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 967 | *pp = p; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 968 | return SQLITE_OK ; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 972 | ** Free an iterator allocated by walIteratorInit(). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 973 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 974 | static void walIteratorFree(WalIterator *p){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 975 | sqlite3_free(p); |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | ** Checkpoint the contents of the log file. |
| 980 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 981 | static int walCheckpoint( |
| 982 | Wal *pWal, /* Wal connection */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 983 | int sync_flags, /* Flags for OsSync() (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 984 | int nBuf, /* Size of zBuf in bytes */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 985 | u8 *zBuf /* Temporary buffer to use */ |
| 986 | ){ |
| 987 | int rc; /* Return code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 988 | int pgsz = pWal->hdr.pgsz; /* Database page-size */ |
| 989 | WalIterator *pIter = 0; /* Wal iterator context */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 990 | u32 iDbpage = 0; /* Next database page to write */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 991 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 992 | |
| 993 | /* Allocate the iterator */ |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 994 | rc = walIteratorInit(pWal, &pIter); |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 995 | if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 996 | goto out; |
| 997 | } |
| 998 | |
| 999 | if( pWal->hdr.pgsz!=nBuf ){ |
| 1000 | rc = SQLITE_CORRUPT_BKPT; |
| 1001 | goto out; |
| 1002 | } |
| 1003 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1004 | /* Sync the log file to disk */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1005 | if( sync_flags ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1006 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1007 | if( rc!=SQLITE_OK ) goto out; |
| 1008 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* Iterate through the contents of the log, copying data to the db file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1011 | while( 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1012 | rc = sqlite3OsRead(pWal->pWalFd, zBuf, pgsz, |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1013 | walFrameOffset(iFrame, pgsz) + WAL_FRAME_HDRSIZE |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1014 | ); |
| 1015 | if( rc!=SQLITE_OK ) goto out; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1016 | rc = sqlite3OsWrite(pWal->pDbFd, zBuf, pgsz, (iDbpage-1)*pgsz); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1017 | if( rc!=SQLITE_OK ) goto out; |
| 1018 | } |
| 1019 | |
| 1020 | /* Truncate the database file */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1021 | rc = sqlite3OsTruncate(pWal->pDbFd, ((i64)pWal->hdr.nPage*(i64)pgsz)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1022 | if( rc!=SQLITE_OK ) goto out; |
| 1023 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1024 | /* Sync the database file. If successful, update the wal-index. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1025 | if( sync_flags ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1026 | rc = sqlite3OsSync(pWal->pDbFd, sync_flags); |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1027 | if( rc!=SQLITE_OK ) goto out; |
| 1028 | } |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1029 | pWal->hdr.mxFrame = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1030 | pWal->hdr.iCheck1 = 2; |
| 1031 | pWal->hdr.iCheck2 = 3; |
| 1032 | walIndexWriteHdr(pWal, &pWal->hdr); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1033 | |
| 1034 | /* TODO: If a crash occurs and the current log is copied into the |
| 1035 | ** database there is no problem. However, if a crash occurs while |
| 1036 | ** writing the next transaction into the start of the log, such that: |
| 1037 | ** |
| 1038 | ** * The first transaction currently in the log is left intact, but |
| 1039 | ** * The second (or subsequent) transaction is damaged, |
| 1040 | ** |
| 1041 | ** then the database could become corrupt. |
| 1042 | ** |
| 1043 | ** The easiest thing to do would be to write and sync a dummy header |
| 1044 | ** into the log at this point. Unfortunately, that turns out to be |
| 1045 | ** an unwelcome performance hit. Alternatives are... |
| 1046 | */ |
| 1047 | #if 0 |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1048 | memset(zBuf, 0, WAL_FRAME_HDRSIZE); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1049 | rc = sqlite3OsWrite(pWal->pWalFd, zBuf, WAL_FRAME_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1050 | if( rc!=SQLITE_OK ) goto out; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1051 | rc = sqlite3OsSync(pWal->pWalFd, pWal->sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1052 | #endif |
| 1053 | |
| 1054 | out: |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1055 | walIteratorFree(pIter); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1056 | return rc; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | ** Close a connection to a log file. |
| 1061 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1062 | int sqlite3WalClose( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1063 | Wal *pWal, /* Wal to close */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1064 | int sync_flags, /* Flags to pass to OsSync() (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1065 | int nBuf, |
| 1066 | u8 *zBuf /* Buffer of at least nBuf bytes */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1067 | ){ |
| 1068 | int rc = SQLITE_OK; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1069 | if( pWal ){ |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1070 | int isDelete = 0; /* True to unlink wal and wal-index files */ |
| 1071 | |
| 1072 | /* If an EXCLUSIVE lock can be obtained on the database file (using the |
| 1073 | ** ordinary, rollback-mode locking methods, this guarantees that the |
| 1074 | ** connection associated with this log file is the only connection to |
| 1075 | ** the database. In this case checkpoint the database and unlink both |
| 1076 | ** the wal and wal-index files. |
| 1077 | ** |
| 1078 | ** The EXCLUSIVE lock is not released before returning. |
| 1079 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1080 | rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1081 | if( rc==SQLITE_OK ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1082 | rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf, 0, 0); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1083 | if( rc==SQLITE_OK ){ |
| 1084 | isDelete = 1; |
| 1085 | } |
| 1086 | walIndexUnmap(pWal); |
| 1087 | } |
| 1088 | |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 1089 | walIndexClose(pWal, isDelete); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1090 | sqlite3OsClose(pWal->pWalFd); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1091 | if( isDelete ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1092 | sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1093 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1094 | sqlite3_free(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1095 | } |
| 1096 | return rc; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1100 | ** Try to read the wal-index header. Return 0 on success and 1 if |
| 1101 | ** there is a problem. |
| 1102 | ** |
| 1103 | ** The wal-index is in shared memory. Another thread or process might |
| 1104 | ** be writing the header at the same time this procedure is trying to |
| 1105 | ** read it, which might result in inconsistency. A dirty read is detected |
| 1106 | ** by verifying a checksum on the header. |
| 1107 | ** |
| 1108 | ** If and only if the read is consistent and the header is different from |
| 1109 | ** pWal->hdr, then pWal->hdr is updated to the content of the new header |
| 1110 | ** and *pChanged is set to 1. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1111 | ** |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1112 | ** If the checksum cannot be verified return non-zero. If the header |
| 1113 | ** is read successfully and the checksum verified, return zero. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1114 | */ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1115 | int walIndexTryHdr(Wal *pWal, int *pChanged){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1116 | int i; |
| 1117 | volatile u32 *aWiData; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1118 | u32 aCksum[2] = {1, 1}; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1119 | u32 aHdr[WALINDEX_HDR_NFIELD+2]; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1120 | |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1121 | assert( pWal->pWiData ); |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 1122 | if( pWal->szWIndex==0 ){ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1123 | /* The wal-index is of size 0 bytes. This is handled in the same way |
| 1124 | ** as an invalid header. The caller will run recovery to construct |
| 1125 | ** a valid wal-index file before accessing the database. |
| 1126 | */ |
| 1127 | return 1; |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1130 | /* Read the header. The caller may or may not have an exclusive |
| 1131 | ** (WRITE, PENDING, CHECKPOINT or RECOVER) lock on the wal-index |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 1132 | ** file, meaning it is possible that an inconsistent snapshot is read |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1133 | ** from the file. If this happens, return non-zero. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1134 | */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1135 | aWiData = pWal->pWiData; |
| 1136 | for(i=0; i<WALINDEX_HDR_NFIELD+2; i++){ |
| 1137 | aHdr[i] = aWiData[i]; |
| 1138 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1139 | walChecksumBytes((u8*)aHdr, sizeof(u32)*WALINDEX_HDR_NFIELD, aCksum); |
| 1140 | if( aCksum[0]!=aHdr[WALINDEX_HDR_NFIELD] |
| 1141 | || aCksum[1]!=aHdr[WALINDEX_HDR_NFIELD+1] |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1142 | ){ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1143 | return 1; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1146 | if( memcmp(&pWal->hdr, aHdr, sizeof(WalIndexHdr)) ){ |
dan | a861469 | 2010-05-06 14:42:34 +0000 | [diff] [blame] | 1147 | *pChanged = 1; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1148 | memcpy(&pWal->hdr, aHdr, sizeof(WalIndexHdr)); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1149 | } |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1150 | |
| 1151 | /* The header was successfully read. Return zero. */ |
| 1152 | return 0; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1156 | ** Read the wal-index header from the wal-index and into pWal->hdr. |
| 1157 | ** If the wal-header appears to be corrupt, try to recover the log |
| 1158 | ** before returning. |
| 1159 | ** |
| 1160 | ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is |
| 1161 | ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged |
| 1162 | ** to 0. |
| 1163 | ** |
| 1164 | ** This routine also maps the wal-index content into memory and assigns |
| 1165 | ** ownership of that mapping to the current thread. In some implementations, |
| 1166 | ** only one thread at a time can hold a mapping of the wal-index. Hence, |
| 1167 | ** the caller should strive to invoke walIndexUnmap() as soon as possible |
| 1168 | ** after this routine returns. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1169 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1170 | ** If the wal-index header is successfully read, return SQLITE_OK. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1171 | ** Otherwise an SQLite error code. |
| 1172 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1173 | static int walIndexReadHdr(Wal *pWal, int *pChanged){ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1174 | int rc; /* Return code */ |
| 1175 | int lockState; /* pWal->lockState before running recovery */ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1176 | |
dan | 4c97b53 | 2010-04-30 09:52:17 +0000 | [diff] [blame] | 1177 | assert( pWal->lockState>=SQLITE_SHM_READ ); |
dan | a861469 | 2010-05-06 14:42:34 +0000 | [diff] [blame] | 1178 | assert( pChanged ); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1179 | rc = walIndexMap(pWal, -1); |
| 1180 | if( rc!=SQLITE_OK ){ |
| 1181 | return rc; |
| 1182 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1183 | |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1184 | /* First attempt to read the wal-index header. This may fail for one |
| 1185 | ** of two reasons: (a) the wal-index does not yet exist or has been |
| 1186 | ** corrupted and needs to be constructed by running recovery, or (b) |
| 1187 | ** the caller is only holding a READ lock and made a dirty read of |
| 1188 | ** the wal-index header. |
| 1189 | ** |
| 1190 | ** A dirty read of the wal-index header occurs if another thread or |
| 1191 | ** process happens to be writing to the wal-index header at roughly |
| 1192 | ** the same time as this thread is reading it. In this case it is |
| 1193 | ** possible that an inconsistent header is read (which is detected |
| 1194 | ** using the header checksum mechanism). |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1195 | */ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1196 | if( walIndexTryHdr(pWal, pChanged)==0 ){ |
| 1197 | return SQLITE_OK; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1200 | /* If the first attempt to read the header failed, lock the wal-index |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1201 | ** file with an exclusive lock and try again. If the header checksum |
| 1202 | ** verification fails again, we can be sure that it is not simply a |
| 1203 | ** dirty read, but that the wal-index really does need to be |
| 1204 | ** reconstructed by running log recovery. |
| 1205 | ** |
| 1206 | ** In the paragraph above, an "exclusive lock" may be any of WRITE, |
| 1207 | ** PENDING, CHECKPOINT or RECOVER. If any of these are already held, |
| 1208 | ** no locking operations are required. If the caller currently holds |
| 1209 | ** a READ lock, then upgrade to a RECOVER lock before re-reading the |
| 1210 | ** wal-index header and revert to a READ lock before returning. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1211 | */ |
dan | 5273f58 | 2010-05-06 18:27:19 +0000 | [diff] [blame] | 1212 | lockState = pWal->lockState; |
dan | 65be0d8 | 2010-05-06 18:48:27 +0000 | [diff] [blame] | 1213 | if( lockState>SQLITE_SHM_READ |
| 1214 | || SQLITE_OK==(rc = walSetLock(pWal, SQLITE_SHM_RECOVER)) |
| 1215 | ){ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1216 | if( walIndexTryHdr(pWal, pChanged) ){ |
dan | a861469 | 2010-05-06 14:42:34 +0000 | [diff] [blame] | 1217 | *pChanged = 1; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1218 | rc = walIndexRecover(pWal); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1219 | } |
dan | 65be0d8 | 2010-05-06 18:48:27 +0000 | [diff] [blame] | 1220 | if( lockState==SQLITE_SHM_READ ){ |
| 1221 | walSetLock(pWal, SQLITE_SHM_READ); |
| 1222 | } |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | return rc; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1229 | ** Take a snapshot of the state of the WAL and wal-index for the current |
| 1230 | ** instant in time. The current thread will continue to use this snapshot. |
| 1231 | ** Other threads might containing appending to the WAL and wal-index but |
| 1232 | ** the extra content appended will be ignored by the current thread. |
| 1233 | ** |
| 1234 | ** A snapshot is like a read transaction. |
| 1235 | ** |
| 1236 | ** No other threads are allowed to run a checkpoint while this thread is |
| 1237 | ** holding the snapshot since a checkpoint would remove data out from under |
| 1238 | ** this thread. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1239 | ** |
| 1240 | ** 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] | 1241 | ** modified since the most recent call to WalCloseSnapshot() on this Wal |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1242 | ** connection, then *pChanged is set to 1 before returning. Otherwise, it |
| 1243 | ** is left unmodified. This is used by the pager layer to determine whether |
| 1244 | ** or not any cached pages may be safely reused. |
| 1245 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1246 | int sqlite3WalOpenSnapshot(Wal *pWal, int *pChanged){ |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1247 | int rc; /* Return code */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1248 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1249 | rc = walSetLock(pWal, SQLITE_SHM_READ); |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1250 | assert( rc!=SQLITE_OK || pWal->lockState==SQLITE_SHM_READ ); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1251 | |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1252 | if( rc==SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1253 | rc = walIndexReadHdr(pWal, pChanged); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1254 | if( rc!=SQLITE_OK ){ |
| 1255 | /* An error occured while attempting log recovery. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1256 | sqlite3WalCloseSnapshot(pWal); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1257 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1258 | } |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 1259 | |
| 1260 | walIndexUnmap(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1261 | return rc; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | ** Unlock the current snapshot. |
| 1266 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1267 | void sqlite3WalCloseSnapshot(Wal *pWal){ |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1268 | assert( pWal->lockState==SQLITE_SHM_READ |
| 1269 | || pWal->lockState==SQLITE_SHM_UNLOCK |
| 1270 | ); |
| 1271 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 1274 | /* |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1275 | ** Read a page from the log, if it is present. |
| 1276 | */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1277 | int sqlite3WalRead( |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1278 | Wal *pWal, /* WAL handle */ |
| 1279 | Pgno pgno, /* Database page number to read data for */ |
| 1280 | int *pInWal, /* OUT: True if data is read from WAL */ |
| 1281 | int nOut, /* Size of buffer pOut in bytes */ |
| 1282 | u8 *pOut /* Buffer to write page data to */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1283 | ){ |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1284 | int rc; /* Return code */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1285 | u32 iRead = 0; /* If !=0, WAL frame to return data from */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1286 | u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1287 | int iHash; /* Used to loop through N hash tables */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1288 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1289 | /* If the "last page" field of the wal-index header snapshot is 0, then |
| 1290 | ** no data will be read from the wal under any circumstances. Return early |
| 1291 | ** in this case to avoid the walIndexMap/Unmap overhead. |
| 1292 | */ |
| 1293 | if( iLast==0 ){ |
| 1294 | *pInWal = 0; |
| 1295 | return SQLITE_OK; |
| 1296 | } |
| 1297 | |
| 1298 | /* Ensure the wal-index is mapped. */ |
dan | 1bc6171 | 2010-04-30 10:24:54 +0000 | [diff] [blame] | 1299 | assert( pWal->lockState==SQLITE_SHM_READ||pWal->lockState==SQLITE_SHM_WRITE ); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1300 | rc = walIndexMap(pWal, walMappingSize(iLast)); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1301 | if( rc!=SQLITE_OK ){ |
| 1302 | return rc; |
| 1303 | } |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 1304 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1305 | /* Search the hash table or tables for an entry matching page number |
| 1306 | ** pgno. Each iteration of the following for() loop searches one |
| 1307 | ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). |
| 1308 | ** |
| 1309 | ** This code may run concurrently to the code in walIndexAppend() |
| 1310 | ** that adds entries to the wal-index (and possibly to this hash |
| 1311 | ** table). This means the non-zero value just read from the hash |
| 1312 | ** slot (aHash[iKey]) may have been added before or after the |
| 1313 | ** current read transaction was opened. Values added after the |
| 1314 | ** read transaction was opened may have been written incorrectly - |
| 1315 | ** i.e. these slots may contain garbage data. However, we assume |
| 1316 | ** that any slots written before the current read transaction was |
| 1317 | ** opened remain unmodified. |
| 1318 | ** |
| 1319 | ** For the reasons above, the if(...) condition featured in the inner |
| 1320 | ** loop of the following block is more stringent that would be required |
| 1321 | ** if we had exclusive access to the hash-table: |
| 1322 | ** |
| 1323 | ** (aPgno[iFrame]==pgno): |
| 1324 | ** This condition filters out normal hash-table collisions. |
| 1325 | ** |
| 1326 | ** (iFrame<=iLast): |
| 1327 | ** This condition filters out entries that were added to the hash |
| 1328 | ** table after the current read-transaction had started. |
| 1329 | ** |
| 1330 | ** (iFrame>iRead): |
| 1331 | ** This filters out a dangerous class of garbage data. The |
| 1332 | ** garbage hash slot may refer to a frame with the correct page |
| 1333 | ** number, but not the most recent version of the frame. For |
| 1334 | ** example, if at the start of the read-transaction the log |
| 1335 | ** contains three copies of the desired page in frames 2, 3 and 4, |
| 1336 | ** the hash table may contain the following: |
| 1337 | ** |
| 1338 | ** { ..., 2, 3, 4, 0, 0, ..... } |
| 1339 | ** |
| 1340 | ** The correct answer is to read data from frame 4. But a |
| 1341 | ** dirty-read may potentially cause the hash-table to appear as |
| 1342 | ** follows to the reader: |
| 1343 | ** |
| 1344 | ** { ..., 2, 3, 4, 3, 0, ..... } |
| 1345 | ** |
| 1346 | ** Without this part of the if(...) clause, the reader might |
| 1347 | ** incorrectly read data from frame 3 instead of 4. This would be |
| 1348 | ** an error. |
| 1349 | ** |
| 1350 | ** It is not actually clear to the developers that such a dirty-read |
| 1351 | ** can occur. But if it does, it should not cause any problems. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1352 | */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1353 | for(iHash=iLast; iHash>0 && iRead==0; iHash-=HASHTABLE_NPAGE){ |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 1354 | volatile HASHTABLE_DATATYPE *aHash; /* Pointer to hash table */ |
| 1355 | volatile u32 *aPgno; /* Pointer to array of page numbers */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1356 | u32 iZero; /* Frame number corresponding to aPgno[0] */ |
| 1357 | int iKey; /* Hash slot index */ |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 1358 | int mxHash; /* upper bound on aHash[] values */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1359 | |
| 1360 | walHashFind(pWal, iHash, &aHash, &aPgno, &iZero); |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 1361 | mxHash = iLast - iZero; |
| 1362 | if( mxHash > HASHTABLE_NPAGE ) mxHash = HASHTABLE_NPAGE; |
| 1363 | for(iKey=walHash(pgno); aHash[iKey]<=mxHash; iKey=walNextHash(iKey)){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1364 | u32 iFrame = aHash[iKey] + iZero; |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 1365 | if( ALWAYS(iFrame<=iLast) && aPgno[iFrame]==pgno && iFrame>iRead ){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1366 | iRead = iFrame; |
| 1367 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1368 | } |
| 1369 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1370 | assert( iRead==0 || pWal->pWiData[walIndexEntry(iRead)]==pgno ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1371 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1372 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 1373 | /* If expensive assert() statements are available, do a linear search |
| 1374 | ** of the wal-index file content. Make sure the results agree with the |
| 1375 | ** result obtained using the hash indexes above. */ |
| 1376 | { |
| 1377 | u32 iRead2 = 0; |
| 1378 | u32 iTest; |
| 1379 | for(iTest=iLast; iTest>0; iTest--){ |
| 1380 | if( pWal->pWiData[walIndexEntry(iTest)]==pgno ){ |
| 1381 | iRead2 = iTest; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1382 | break; |
| 1383 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1384 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1385 | assert( iRead==iRead2 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1386 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1387 | #endif |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 1388 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1389 | /* If iRead is non-zero, then it is the log frame number that contains the |
| 1390 | ** required page. Read and return data from the log file. |
| 1391 | */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1392 | walIndexUnmap(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1393 | if( iRead ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1394 | i64 iOffset = walFrameOffset(iRead, pWal->hdr.pgsz) + WAL_FRAME_HDRSIZE; |
| 1395 | *pInWal = 1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1396 | return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1399 | *pInWal = 0; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1400 | return SQLITE_OK; |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | /* |
| 1405 | ** Set *pPgno to the size of the database file (or zero, if unknown). |
| 1406 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1407 | void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){ |
| 1408 | assert( pWal->lockState==SQLITE_SHM_READ |
| 1409 | || pWal->lockState==SQLITE_SHM_WRITE ); |
| 1410 | *pPgno = pWal->hdr.nPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /* |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1414 | ** This function returns SQLITE_OK if the caller may write to the database. |
| 1415 | ** Otherwise, if the caller is operating on a snapshot that has already |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 1416 | ** been overwritten by another writer, SQLITE_BUSY is returned. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1417 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1418 | int sqlite3WalWriteLock(Wal *pWal, int op){ |
drh | e874d9e | 2010-05-07 20:02:23 +0000 | [diff] [blame] | 1419 | int rc = SQLITE_OK; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1420 | if( op ){ |
dan | ed36020 | 2010-05-12 06:54:31 +0000 | [diff] [blame] | 1421 | assert( pWal->lockState==SQLITE_SHM_READ ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1422 | rc = walSetLock(pWal, SQLITE_SHM_WRITE); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* If this connection is not reading the most recent database snapshot, |
| 1425 | ** it is not possible to write to the database. In this case release |
| 1426 | ** the write locks and return SQLITE_BUSY. |
| 1427 | */ |
| 1428 | if( rc==SQLITE_OK ){ |
dan | 576bc32 | 2010-05-06 18:04:50 +0000 | [diff] [blame] | 1429 | rc = walIndexMap(pWal, sizeof(WalIndexHdr)); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1430 | if( rc==SQLITE_OK |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 1431 | && memcmp(&pWal->hdr, (void*)pWal->pWiData, sizeof(WalIndexHdr)) |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1432 | ){ |
| 1433 | rc = SQLITE_BUSY; |
| 1434 | } |
| 1435 | walIndexUnmap(pWal); |
| 1436 | if( rc!=SQLITE_OK ){ |
| 1437 | walSetLock(pWal, SQLITE_SHM_READ); |
| 1438 | } |
| 1439 | } |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 1440 | }else if( pWal->lockState==SQLITE_SHM_WRITE ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1441 | rc = walSetLock(pWal, SQLITE_SHM_READ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1442 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1443 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1446 | /* |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1447 | ** If any data has been written (but not committed) to the log file, this |
| 1448 | ** function moves the write-pointer back to the start of the transaction. |
| 1449 | ** |
| 1450 | ** Additionally, the callback function is invoked for each frame written |
| 1451 | ** to the log since the start of the transaction. If the callback returns |
| 1452 | ** other than SQLITE_OK, it is not invoked again and the error code is |
| 1453 | ** returned to the caller. |
| 1454 | ** |
| 1455 | ** Otherwise, if the callback function does not return an error, this |
| 1456 | ** function returns SQLITE_OK. |
| 1457 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1458 | int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 1459 | int rc = SQLITE_OK; |
| 1460 | if( pWal->lockState==SQLITE_SHM_WRITE ){ |
| 1461 | int unused; |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1462 | Pgno iMax = pWal->hdr.mxFrame; |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 1463 | Pgno iFrame; |
| 1464 | |
| 1465 | assert( pWal->pWiData==0 ); |
| 1466 | rc = walIndexReadHdr(pWal, &unused); |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1467 | for(iFrame=pWal->hdr.mxFrame+1; rc==SQLITE_OK && iFrame<=iMax; iFrame++){ |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 1468 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
| 1469 | rc = xUndo(pUndoCtx, pWal->pWiData[walIndexEntry(iFrame)]); |
| 1470 | } |
| 1471 | walIndexUnmap(pWal); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1472 | } |
| 1473 | return rc; |
| 1474 | } |
| 1475 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1476 | /* Return an integer that records the current (uncommitted) write |
| 1477 | ** position in the WAL |
| 1478 | */ |
| 1479 | u32 sqlite3WalSavepoint(Wal *pWal){ |
| 1480 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1481 | return pWal->hdr.mxFrame; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1484 | /* Move the write position of the WAL back to iFrame. Called in |
| 1485 | ** response to a ROLLBACK TO command. |
| 1486 | */ |
| 1487 | int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){ |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1488 | int rc = SQLITE_OK; |
| 1489 | u8 aCksum[8]; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1490 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1491 | |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1492 | pWal->hdr.mxFrame = iFrame; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1493 | if( iFrame>0 ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1494 | i64 iOffset = walFrameOffset(iFrame, pWal->hdr.pgsz) + sizeof(u32)*2; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1495 | rc = sqlite3OsRead(pWal->pWalFd, aCksum, sizeof(aCksum), iOffset); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1496 | pWal->hdr.iCheck1 = sqlite3Get4byte(&aCksum[0]); |
| 1497 | pWal->hdr.iCheck2 = sqlite3Get4byte(&aCksum[4]); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | return rc; |
| 1501 | } |
| 1502 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1503 | /* |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1504 | ** Write a set of frames to the log. The caller must hold the write-lock |
| 1505 | ** on the log file (obtained using sqlite3WalWriteLock()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1506 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1507 | int sqlite3WalFrames( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1508 | Wal *pWal, /* Wal handle to write to */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1509 | int nPgsz, /* Database page-size in bytes */ |
| 1510 | PgHdr *pList, /* List of dirty pages to write */ |
| 1511 | Pgno nTruncate, /* Database size after this commit */ |
| 1512 | int isCommit, /* True if this is a commit */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1513 | int sync_flags /* Flags to pass to OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1514 | ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1515 | int rc; /* Used to catch return codes */ |
| 1516 | u32 iFrame; /* Next frame address */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1517 | u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1518 | PgHdr *p; /* Iterator to run through pList with. */ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1519 | u32 aCksum[2]; /* Checksums */ |
drh | e874d9e | 2010-05-07 20:02:23 +0000 | [diff] [blame] | 1520 | PgHdr *pLast = 0; /* Last frame in list */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1521 | int nLast = 0; /* Number of extra copies of last page */ |
| 1522 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1523 | assert( WAL_FRAME_HDRSIZE==(4 * 2 + 2*sizeof(u32)) ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1524 | assert( pList ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1525 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 1526 | assert( pWal->pWiData==0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1527 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1528 | /* If this is the first frame written into the log, write the WAL |
| 1529 | ** header to the start of the WAL file. See comments at the top of |
| 1530 | ** this source file for a description of the WAL header format. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1531 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1532 | assert( WAL_FRAME_HDRSIZE>=WAL_HDRSIZE ); |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1533 | iFrame = pWal->hdr.mxFrame; |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1534 | if( iFrame==0 ){ |
| 1535 | sqlite3Put4byte(aFrame, nPgsz); |
| 1536 | sqlite3_randomness(8, &aFrame[4]); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1537 | pWal->hdr.iCheck1 = sqlite3Get4byte(&aFrame[4]); |
| 1538 | pWal->hdr.iCheck2 = sqlite3Get4byte(&aFrame[8]); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1539 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, WAL_HDRSIZE, 0); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1540 | if( rc!=SQLITE_OK ){ |
| 1541 | return rc; |
| 1542 | } |
| 1543 | } |
| 1544 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1545 | aCksum[0] = pWal->hdr.iCheck1; |
| 1546 | aCksum[1] = pWal->hdr.iCheck2; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* Write the log file. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1549 | for(p=pList; p; p=p->pDirty){ |
| 1550 | u32 nDbsize; /* Db-size field for frame header */ |
| 1551 | i64 iOffset; /* Write offset in log file */ |
| 1552 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1553 | iOffset = walFrameOffset(++iFrame, nPgsz); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* Populate and write the frame header */ |
| 1556 | nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1557 | walEncodeFrame(aCksum, p->pgno, nDbsize, nPgsz, p->pData, aFrame); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1558 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1559 | if( rc!=SQLITE_OK ){ |
| 1560 | return rc; |
| 1561 | } |
| 1562 | |
| 1563 | /* Write the page data */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1564 | rc = sqlite3OsWrite(pWal->pWalFd, p->pData, nPgsz, iOffset+sizeof(aFrame)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1565 | if( rc!=SQLITE_OK ){ |
| 1566 | return rc; |
| 1567 | } |
| 1568 | pLast = p; |
| 1569 | } |
| 1570 | |
| 1571 | /* Sync the log file if the 'isSync' flag was specified. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1572 | if( sync_flags ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1573 | i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1574 | i64 iOffset = walFrameOffset(iFrame+1, nPgsz); |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 1575 | |
| 1576 | assert( isCommit ); |
drh | 69c4696 | 2010-05-17 20:16:50 +0000 | [diff] [blame] | 1577 | assert( iSegment>0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1578 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1579 | iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); |
| 1580 | while( iOffset<iSegment ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1581 | walEncodeFrame(aCksum,pLast->pgno,nTruncate,nPgsz,pLast->pData,aFrame); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1582 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1583 | if( rc!=SQLITE_OK ){ |
| 1584 | return rc; |
| 1585 | } |
| 1586 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1587 | iOffset += WAL_FRAME_HDRSIZE; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1588 | rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, nPgsz, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1589 | if( rc!=SQLITE_OK ){ |
| 1590 | return rc; |
| 1591 | } |
| 1592 | nLast++; |
| 1593 | iOffset += nPgsz; |
| 1594 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1595 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1596 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1597 | } |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 1598 | assert( pWal->pWiData==0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1599 | |
drh | e730fec | 2010-05-18 12:56:50 +0000 | [diff] [blame] | 1600 | /* Append data to the wal-index. It is not necessary to lock the |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1601 | ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1602 | ** guarantees that there are no other writers, and no data that may |
| 1603 | ** be in use by existing readers is being overwritten. |
| 1604 | */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1605 | iFrame = pWal->hdr.mxFrame; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1606 | for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1607 | iFrame++; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1608 | rc = walIndexAppend(pWal, iFrame, p->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1609 | } |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1610 | while( nLast>0 && rc==SQLITE_OK ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1611 | iFrame++; |
| 1612 | nLast--; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1613 | rc = walIndexAppend(pWal, iFrame, pLast->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1616 | if( rc==SQLITE_OK ){ |
| 1617 | /* Update the private copy of the header. */ |
| 1618 | pWal->hdr.pgsz = nPgsz; |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame^] | 1619 | pWal->hdr.mxFrame = iFrame; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1620 | if( isCommit ){ |
| 1621 | pWal->hdr.iChange++; |
| 1622 | pWal->hdr.nPage = nTruncate; |
| 1623 | } |
| 1624 | pWal->hdr.iCheck1 = aCksum[0]; |
| 1625 | pWal->hdr.iCheck2 = aCksum[1]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1626 | |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1627 | /* If this is a commit, update the wal-index header too. */ |
| 1628 | if( isCommit ){ |
| 1629 | walIndexWriteHdr(pWal, &pWal->hdr); |
| 1630 | pWal->iCallback = iFrame; |
| 1631 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1632 | } |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1633 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1634 | walIndexUnmap(pWal); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1635 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1639 | ** Checkpoint the database: |
| 1640 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1641 | ** 1. Acquire a CHECKPOINT lock |
| 1642 | ** 2. Copy the contents of the log into the database file. |
| 1643 | ** 3. Zero the wal-index header (so new readers will ignore the log). |
| 1644 | ** 4. Drop the CHECKPOINT lock. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1645 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1646 | int sqlite3WalCheckpoint( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1647 | Wal *pWal, /* Wal connection */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1648 | int sync_flags, /* Flags to sync db file with (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1649 | int nBuf, /* Size of temporary buffer */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1650 | u8 *zBuf, /* Temporary buffer to use */ |
| 1651 | int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ |
| 1652 | void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1653 | ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1654 | int rc; /* Return code */ |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1655 | int isChanged = 0; /* True if a new wal-index header is loaded */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1656 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 1657 | assert( pWal->pWiData==0 ); |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 1658 | |
dan | ed36020 | 2010-05-12 06:54:31 +0000 | [diff] [blame] | 1659 | /* Get the CHECKPOINT lock. |
| 1660 | ** |
| 1661 | ** Normally, the connection will be in UNLOCK state at this point. But |
| 1662 | ** if the connection is in exclusive-mode it may still be in READ state |
| 1663 | ** even though the upper layer has no active read-transaction (because |
| 1664 | ** WalCloseSnapshot() is not called in exclusive mode). The state will |
| 1665 | ** be set to UNLOCK when this function returns. This is Ok. |
| 1666 | */ |
| 1667 | assert( (pWal->lockState==SQLITE_SHM_UNLOCK) |
drh | 5cccc94 | 2010-05-13 15:44:00 +0000 | [diff] [blame] | 1668 | || (pWal->lockState==SQLITE_SHM_READ) ); |
| 1669 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1670 | do { |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1671 | rc = walSetLock(pWal, SQLITE_SHM_CHECKPOINT); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1672 | }while( rc==SQLITE_BUSY && xBusyHandler(pBusyHandlerArg) ); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1673 | if( rc!=SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1674 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1675 | return rc; |
| 1676 | } |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1677 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1678 | /* Copy data from the log to the database file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1679 | rc = walIndexReadHdr(pWal, &isChanged); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1680 | if( rc==SQLITE_OK ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1681 | rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1682 | } |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1683 | if( isChanged ){ |
| 1684 | /* If a new wal-index header was loaded before the checkpoint was |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1685 | ** performed, then the pager-cache associated with pWal is now |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1686 | ** out of date. So zero the cached wal-index header to ensure that |
| 1687 | ** next time the pager opens a snapshot on this database it knows that |
| 1688 | ** the cache needs to be reset. |
| 1689 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1690 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1691 | } |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1692 | |
| 1693 | /* Release the locks. */ |
dan | 87bfb51 | 2010-04-30 11:43:28 +0000 | [diff] [blame] | 1694 | walIndexUnmap(pWal); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1695 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1696 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1699 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 1700 | ** number of frames in the WAL at the point of the last commit since |
| 1701 | ** sqlite3WalCallback() was called. If no commits have occurred since |
| 1702 | ** the last call, then return 0. |
| 1703 | */ |
| 1704 | int sqlite3WalCallback(Wal *pWal){ |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1705 | u32 ret = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1706 | if( pWal ){ |
| 1707 | ret = pWal->iCallback; |
| 1708 | pWal->iCallback = 0; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1709 | } |
| 1710 | return (int)ret; |
| 1711 | } |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 1712 | |
| 1713 | /* |
| 1714 | ** This function is called to set or query the exclusive-mode flag |
| 1715 | ** associated with the WAL connection passed as the first argument. The |
| 1716 | ** exclusive-mode flag should be set to indicate that the caller is |
| 1717 | ** holding an EXCLUSIVE lock on the database file (it does this in |
| 1718 | ** locking_mode=exclusive mode). If the EXCLUSIVE lock is to be dropped, |
| 1719 | ** the flag set by this function should be cleared before doing so. |
| 1720 | ** |
| 1721 | ** The value of the exclusive-mode flag may only be modified when |
| 1722 | ** the WAL connection is in READ state. |
| 1723 | ** |
| 1724 | ** When the flag is set, this module does not call the VFS xShmLock() |
| 1725 | ** method to obtain any locks on the wal-index (as it assumes it |
| 1726 | ** has exclusive access to the wal and wal-index files anyhow). It |
| 1727 | ** continues to hold (and does not drop) the existing READ lock on |
| 1728 | ** the wal-index. |
| 1729 | ** |
| 1730 | ** To set or clear the flag, the "op" parameter is passed 1 or 0, |
| 1731 | ** respectively. To query the flag, pass -1. In all cases, the value |
| 1732 | ** returned is the value of the exclusive-mode flag (after its value |
| 1733 | ** has been modified, if applicable). |
| 1734 | */ |
| 1735 | int sqlite3WalExclusiveMode(Wal *pWal, int op){ |
| 1736 | if( op>=0 ){ |
| 1737 | assert( pWal->lockState==SQLITE_SHM_READ ); |
| 1738 | pWal->exclusiveMode = (u8)op; |
| 1739 | } |
| 1740 | return pWal->exclusiveMode; |
| 1741 | } |
| 1742 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 1743 | #endif /* #ifndef SQLITE_OMIT_WAL */ |