blob: 2de53b8b18cad7bc63f4341a208bd1d7b6456e90 [file] [log] [blame]
dan7c246102010-04-12 19:00:29 +00001/*
drh7ed91f22010-04-29 22:34:07 +00002** 2010 February 1
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains the implementation of a write-ahead log file used in
dan7c246102010-04-12 19:00:29 +000014** "journal_mode=wal" mode.
15*/
drh7ed91f22010-04-29 22:34:07 +000016#include "wal.h"
dan7c246102010-04-12 19:00:29 +000017
dan4b64c1e2010-04-27 18:49:54 +000018
dan97a31352010-04-16 13:59:31 +000019/*
drh7ed91f22010-04-29 22:34:07 +000020** WRITE-AHEAD LOG (WAL) FILE FORMAT
dan97a31352010-04-16 13:59:31 +000021**
drh7ed91f22010-04-29 22:34:07 +000022** A wal file consists of a header followed by zero or more "frames".
23** The header is 12 bytes in size and consists of the following three
dan97a31352010-04-16 13:59:31 +000024** big-endian 32-bit unsigned integer values:
25**
dan3de777f2010-04-17 12:31:37 +000026** 0: Database page size,
27** 4: Randomly selected salt value 1,
28** 8: Randomly selected salt value 2.
dan97a31352010-04-16 13:59:31 +000029**
drh7ed91f22010-04-29 22:34:07 +000030** Immediately following the header are zero or more frames. Each
dan97a31352010-04-16 13:59:31 +000031** frame itself consists of a 16-byte header followed by a <page-size> bytes
32** of page data. The header is broken into 4 big-endian 32-bit unsigned
33** integer values, as follows:
34**
dan3de777f2010-04-17 12:31:37 +000035** 0: Page number.
36** 4: For commit records, the size of the database image in pages
dan97a31352010-04-16 13:59:31 +000037** after the commit. For all other records, zero.
dan3de777f2010-04-17 12:31:37 +000038** 8: Checksum value 1.
dan97a31352010-04-16 13:59:31 +000039** 12: Checksum value 2.
40*/
41
42/*
drh7ed91f22010-04-29 22:34:07 +000043** WAL-INDEX FILE FORMAT
dan97a31352010-04-16 13:59:31 +000044**
drh7ed91f22010-04-29 22:34:07 +000045** The wal-index file consists of a 32-byte header region, followed by an
46** 8-byte region that contains no useful data (used to apply byte-range locks
danff207012010-04-24 04:49:15 +000047** to), followed by the data region.
48**
49** The contents of both the header and data region are specified in terms
50** of 1, 2 and 4 byte unsigned integers. All integers are stored in
drh7ed91f22010-04-29 22:34:07 +000051** machine-endian order. The wal-index is not a persistent file and
52** so it does not need to be portable across archtectures.
danff207012010-04-24 04:49:15 +000053**
drh7ed91f22010-04-29 22:34:07 +000054** A wal-index file is essentially a shadow-pager map. It contains a
55** mapping from database page number to the set of locations in the wal
danff207012010-04-24 04:49:15 +000056** file that contain versions of the database page. When a database
drh7ed91f22010-04-29 22:34:07 +000057** client needs to read a page of data, it first queries the wal-index
danff207012010-04-24 04:49:15 +000058** file to determine if the required version of the page is stored in
drh7ed91f22010-04-29 22:34:07 +000059** the wal. If so, the page is read from the wal. If not, the page is
60** read from the database file.
danff207012010-04-24 04:49:15 +000061**
drh7ed91f22010-04-29 22:34:07 +000062** Whenever a transaction is appended to the wal or a checkpoint transfers
63** data from the wal into the database file, the wal-index is
danff207012010-04-24 04:49:15 +000064** updated accordingly.
65**
drh7ed91f22010-04-29 22:34:07 +000066** The fields in the wal-index file header are described in the comment
67** directly above the definition of struct WalIndexHdr (see below).
68** Immediately following the fields in the WalIndexHdr structure is
danff207012010-04-24 04:49:15 +000069** an 8 byte checksum based on the contents of the header. This field is
drh7ed91f22010-04-29 22:34:07 +000070** not the same as the iCheck1 and iCheck2 fields of the WalIndexHdr.
dan97a31352010-04-16 13:59:31 +000071*/
72
drh7ed91f22010-04-29 22:34:07 +000073/* Object declarations */
74typedef struct WalIndexHdr WalIndexHdr;
75typedef struct WalIterator WalIterator;
dan7c246102010-04-12 19:00:29 +000076
77
78/*
drh7ed91f22010-04-29 22:34:07 +000079** The following object stores a copy of the wal-index header.
dan7c246102010-04-12 19:00:29 +000080**
81** Member variables iCheck1 and iCheck2 contain the checksum for the
drh7ed91f22010-04-29 22:34:07 +000082** last frame written to the wal, or 2 and 3 respectively if the log
dan7c246102010-04-12 19:00:29 +000083** is currently empty.
84*/
drh7ed91f22010-04-29 22:34:07 +000085struct WalIndexHdr {
dan7c246102010-04-12 19:00:29 +000086 u32 iChange; /* Counter incremented each transaction */
87 u32 pgsz; /* Database page size in bytes */
88 u32 iLastPg; /* Address of last valid frame in log */
89 u32 nPage; /* Size of database in pages */
90 u32 iCheck1; /* Checkpoint value 1 */
91 u32 iCheck2; /* Checkpoint value 2 */
92};
93
drh7ed91f22010-04-29 22:34:07 +000094/* Size of serialized WalIndexHdr object. */
95#define WALINDEX_HDR_NFIELD (sizeof(WalIndexHdr) / sizeof(u32))
dan7c246102010-04-12 19:00:29 +000096
drh7ed91f22010-04-29 22:34:07 +000097/* A block of 16 bytes beginning at WALINDEX_LOCK_OFFSET is reserved
danff207012010-04-24 04:49:15 +000098** for locks. Since some systems only feature mandatory file-locks, we
99** do not read or write data from the region of the file on which locks
100** are applied.
101*/
drh7ed91f22010-04-29 22:34:07 +0000102#define WALINDEX_LOCK_OFFSET ((sizeof(WalIndexHdr))+2*sizeof(u32))
103#define WALINDEX_LOCK_RESERVED 8
dan7c246102010-04-12 19:00:29 +0000104
drh7ed91f22010-04-29 22:34:07 +0000105/* Size of header before each frame in wal */
106#define WAL_FRAME_HDRSIZE 16
danff207012010-04-24 04:49:15 +0000107
drh7ed91f22010-04-29 22:34:07 +0000108/* Size of write ahead log header */
109#define WAL_HDRSIZE 12
dan97a31352010-04-16 13:59:31 +0000110
111/*
drh7ed91f22010-04-29 22:34:07 +0000112** Return the offset of frame iFrame in the write-ahead log file,
113** assuming a database page size of pgsz bytes. The offset returned
114** is to the start of the write-ahead log frame-header.
dan97a31352010-04-16 13:59:31 +0000115*/
drh7ed91f22010-04-29 22:34:07 +0000116#define walFrameOffset(iFrame, pgsz) ( \
117 WAL_HDRSIZE + ((iFrame)-1)*((pgsz)+WAL_FRAME_HDRSIZE) \
dan97a31352010-04-16 13:59:31 +0000118)
dan7c246102010-04-12 19:00:29 +0000119
120/*
drh7ed91f22010-04-29 22:34:07 +0000121** An open write-ahead log file is represented by an instance of the
122** following object.
dance4f05f2010-04-22 19:14:13 +0000123*/
drh7ed91f22010-04-29 22:34:07 +0000124struct Wal {
125 sqlite3_vfs *pVfs; /* The VFS used to create pFd */
126 sqlite3_file *pFd; /* File handle for WAL file */
127 u32 iCallback; /* Value to pass to log callback (or 0) */
128 sqlite3_shm *pWIndex; /* The open wal-index file */
129 int szWIndex; /* Size of the wal-index */
130 u32 *pWiData; /* Pointer to wal-index content in memory */
131 u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */
132 u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */
133 WalIndexHdr hdr; /* Wal-index for current snapshot */
dan7c246102010-04-12 19:00:29 +0000134};
135
dan64d039e2010-04-13 19:27:31 +0000136
dan7c246102010-04-12 19:00:29 +0000137/*
138** This structure is used to implement an iterator that iterates through
139** all frames in the log in database page order. Where two or more frames
140** correspond to the same database page, the iterator visits only the
141** frame most recently written to the log.
142**
143** The internals of this structure are only accessed by:
144**
drh7ed91f22010-04-29 22:34:07 +0000145** walIteratorInit() - Create a new iterator,
146** walIteratorNext() - Step an iterator,
147** walIteratorFree() - Free an iterator.
dan7c246102010-04-12 19:00:29 +0000148**
drh7ed91f22010-04-29 22:34:07 +0000149** This functionality is used by the checkpoint code (see walCheckpoint()).
dan7c246102010-04-12 19:00:29 +0000150*/
drh7ed91f22010-04-29 22:34:07 +0000151struct WalIterator {
152 int nSegment; /* Size of WalIterator.aSegment[] array */
dan7c246102010-04-12 19:00:29 +0000153 int nFinal; /* Elements in segment nSegment-1 */
drh7ed91f22010-04-29 22:34:07 +0000154 struct WalSegment {
dan7c246102010-04-12 19:00:29 +0000155 int iNext; /* Next aIndex index */
156 u8 *aIndex; /* Pointer to index array */
157 u32 *aDbPage; /* Pointer to db page array */
158 } aSegment[1];
159};
160
dan64d039e2010-04-13 19:27:31 +0000161
dan7c246102010-04-12 19:00:29 +0000162/*
163** Generate an 8 byte checksum based on the data in array aByte[] and the
164** initial values of aCksum[0] and aCksum[1]. The checksum is written into
165** aCksum[] before returning.
dan56d95912010-04-24 19:07:29 +0000166**
167** The range of bytes to checksum is treated as an array of 32-bit
168** little-endian unsigned integers. For each integer X in the array, from
169** start to finish, do the following:
170**
171** aCksum[0] += X;
172** aCksum[1] += aCksum[0];
173**
174** For the calculation above, use 64-bit unsigned accumulators. Before
175** returning, truncate the values to 32-bits as follows:
176**
177** aCksum[0] = (u32)(aCksum[0] + (aCksum[0]>>24));
178** aCksum[1] = (u32)(aCksum[1] + (aCksum[1]>>24));
dan7c246102010-04-12 19:00:29 +0000179*/
drh7ed91f22010-04-29 22:34:07 +0000180static void walChecksumBytes(u8 *aByte, int nByte, u32 *aCksum){
dan39c79f52010-04-15 10:58:51 +0000181 u64 sum1 = aCksum[0];
182 u64 sum2 = aCksum[1];
183 u32 *a32 = (u32 *)aByte;
184 u32 *aEnd = (u32 *)&aByte[nByte];
dan7c246102010-04-12 19:00:29 +0000185
dan7c246102010-04-12 19:00:29 +0000186 assert( (nByte&0x00000003)==0 );
187
dance4f05f2010-04-22 19:14:13 +0000188 if( SQLITE_LITTLEENDIAN ){
189#ifdef SQLITE_DEBUG
190 u8 *a = (u8 *)a32;
191 assert( *a32==(a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24)) );
192#endif
193 do {
194 sum1 += *a32;
195 sum2 += sum1;
196 } while( ++a32<aEnd );
197 }else{
198 do {
199 u8 *a = (u8*)a32;
200 sum1 += a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24);
201 sum2 += sum1;
202 } while( ++a32<aEnd );
203 }
dan7c246102010-04-12 19:00:29 +0000204
dan39c79f52010-04-15 10:58:51 +0000205 aCksum[0] = sum1 + (sum1>>24);
206 aCksum[1] = sum2 + (sum2>>24);
dan7c246102010-04-12 19:00:29 +0000207}
208
209/*
drh7ed91f22010-04-29 22:34:07 +0000210** Attempt to change the lock status.
dan7c246102010-04-12 19:00:29 +0000211**
drh7ed91f22010-04-29 22:34:07 +0000212** When changing the lock status to SQLITE_SHM_READ, store the
213** type of reader lock (either SQLITE_SHM_READ or SQLITE_SHM_READ_FULL)
214** in pWal->readerType.
dan7c246102010-04-12 19:00:29 +0000215*/
drh7ed91f22010-04-29 22:34:07 +0000216static int walSetLock(Wal *pWal, int desiredStatus){
217 int rc, got;
218 if( pWal->lockState==desiredStatus ) return SQLITE_OK;
219 rc = pWal->pVfs->xShmLock(pWal->pWIndex, desiredStatus, &got);
220 if( rc==SQLITE_OK ){
221 pWal->lockState = desiredStatus;
222 if( desiredStatus==SQLITE_SHM_READ ){
223 pWal->readerType = got;
dan7c246102010-04-12 19:00:29 +0000224 }
dan7c246102010-04-12 19:00:29 +0000225 }
226 return rc;
227}
228
drh7ed91f22010-04-29 22:34:07 +0000229/*
230** Update the header of the wal-index file.
231*/
232static void walIndexWriteHdr(Wal *pWal, WalIndexHdr *pHdr){
233 u32 *aHdr = pWal->pWiData; /* Write header here */
234 u32 *aCksum = &aHdr[WALINDEX_HDR_NFIELD]; /* Write header cksum here */
danff207012010-04-24 04:49:15 +0000235
drh7ed91f22010-04-29 22:34:07 +0000236 assert( WALINDEX_HDR_NFIELD==sizeof(WalIndexHdr)/4 );
237 assert( aHdr!=0 );
238 memcpy(aHdr, pHdr, sizeof(WalIndexHdr));
danff207012010-04-24 04:49:15 +0000239 aCksum[0] = aCksum[1] = 1;
drh7ed91f22010-04-29 22:34:07 +0000240 walChecksumBytes((u8 *)aHdr, sizeof(WalIndexHdr), aCksum);
dan7c246102010-04-12 19:00:29 +0000241}
242
243/*
244** This function encodes a single frame header and writes it to a buffer
drh7ed91f22010-04-29 22:34:07 +0000245** supplied by the caller. A frame-header is made up of a series of
dan7c246102010-04-12 19:00:29 +0000246** 4-byte big-endian integers, as follows:
247**
248** 0: Database page size in bytes.
249** 4: Page number.
250** 8: New database size (for commit frames, otherwise zero).
251** 12: Frame checksum 1.
252** 16: Frame checksum 2.
253*/
drh7ed91f22010-04-29 22:34:07 +0000254static void walEncodeFrame(
dan7c246102010-04-12 19:00:29 +0000255 u32 *aCksum, /* IN/OUT: Checksum values */
256 u32 iPage, /* Database page number for frame */
257 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
258 int nData, /* Database page size (size of aData[]) */
259 u8 *aData, /* Pointer to page data (for checksum) */
260 u8 *aFrame /* OUT: Write encoded frame here */
261){
drh7ed91f22010-04-29 22:34:07 +0000262 assert( WAL_FRAME_HDRSIZE==16 );
dan7c246102010-04-12 19:00:29 +0000263
dan97a31352010-04-16 13:59:31 +0000264 sqlite3Put4byte(&aFrame[0], iPage);
265 sqlite3Put4byte(&aFrame[4], nTruncate);
dan7c246102010-04-12 19:00:29 +0000266
drh7ed91f22010-04-29 22:34:07 +0000267 walChecksumBytes(aFrame, 8, aCksum);
268 walChecksumBytes(aData, nData, aCksum);
dan7c246102010-04-12 19:00:29 +0000269
dan97a31352010-04-16 13:59:31 +0000270 sqlite3Put4byte(&aFrame[8], aCksum[0]);
271 sqlite3Put4byte(&aFrame[12], aCksum[1]);
dan7c246102010-04-12 19:00:29 +0000272}
273
274/*
275** Return 1 and populate *piPage, *pnTruncate and aCksum if the
276** frame checksum looks Ok. Otherwise return 0.
277*/
drh7ed91f22010-04-29 22:34:07 +0000278static int walDecodeFrame(
dan7c246102010-04-12 19:00:29 +0000279 u32 *aCksum, /* IN/OUT: Checksum values */
280 u32 *piPage, /* OUT: Database page number for frame */
281 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
282 int nData, /* Database page size (size of aData[]) */
283 u8 *aData, /* Pointer to page data (for checksum) */
284 u8 *aFrame /* Frame data */
285){
drh7ed91f22010-04-29 22:34:07 +0000286 assert( WAL_FRAME_HDRSIZE==16 );
dan4a4b01d2010-04-16 11:30:18 +0000287
drh7ed91f22010-04-29 22:34:07 +0000288 walChecksumBytes(aFrame, 8, aCksum);
289 walChecksumBytes(aData, nData, aCksum);
dan7c246102010-04-12 19:00:29 +0000290
dan97a31352010-04-16 13:59:31 +0000291 if( aCksum[0]!=sqlite3Get4byte(&aFrame[8])
292 || aCksum[1]!=sqlite3Get4byte(&aFrame[12])
dan7c246102010-04-12 19:00:29 +0000293 ){
294 /* Checksum failed. */
295 return 0;
296 }
297
dan97a31352010-04-16 13:59:31 +0000298 *piPage = sqlite3Get4byte(&aFrame[0]);
299 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
dan7c246102010-04-12 19:00:29 +0000300 return 1;
301}
302
drh7ed91f22010-04-29 22:34:07 +0000303static void walMergesort8(
304 Pgno *aContent, /* Pages in wal */
dan7c246102010-04-12 19:00:29 +0000305 u8 *aBuffer, /* Buffer of at least *pnList items to use */
306 u8 *aList, /* IN/OUT: List to sort */
307 int *pnList /* IN/OUT: Number of elements in aList[] */
308){
309 int nList = *pnList;
310 if( nList>1 ){
311 int nLeft = nList / 2; /* Elements in left list */
312 int nRight = nList - nLeft; /* Elements in right list */
313 u8 *aLeft = aList; /* Left list */
314 u8 *aRight = &aList[nLeft]; /* Right list */
315 int iLeft = 0; /* Current index in aLeft */
316 int iRight = 0; /* Current index in aright */
317 int iOut = 0; /* Current index in output buffer */
318
319 /* TODO: Change to non-recursive version. */
drh7ed91f22010-04-29 22:34:07 +0000320 walMergesort8(aContent, aBuffer, aLeft, &nLeft);
321 walMergesort8(aContent, aBuffer, aRight, &nRight);
dan7c246102010-04-12 19:00:29 +0000322
323 while( iRight<nRight || iLeft<nLeft ){
324 u8 logpage;
325 Pgno dbpage;
326
327 if( (iLeft<nLeft)
328 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
329 ){
330 logpage = aLeft[iLeft++];
331 }else{
332 logpage = aRight[iRight++];
333 }
334 dbpage = aContent[logpage];
335
336 aBuffer[iOut++] = logpage;
337 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
338
339 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
340 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
341 }
342 memcpy(aList, aBuffer, sizeof(aList[0])*iOut);
343 *pnList = iOut;
344 }
345
346#ifdef SQLITE_DEBUG
347 {
348 int i;
349 for(i=1; i<*pnList; i++){
350 assert( aContent[aList[i]] > aContent[aList[i-1]] );
351 }
352 }
353#endif
354}
355
356
357/*
drh7ed91f22010-04-29 22:34:07 +0000358** Return the index in the WalIndex.aData array that corresponds to
359** frame iFrame. The wal-index file consists of a header, followed by
dan7c246102010-04-12 19:00:29 +0000360** alternating "map" and "index" blocks.
361*/
drh7ed91f22010-04-29 22:34:07 +0000362static int walIndexEntry(u32 iFrame){
danff207012010-04-24 04:49:15 +0000363 return (
drh7ed91f22010-04-29 22:34:07 +0000364 (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)/sizeof(u32)
danff207012010-04-24 04:49:15 +0000365 + (((iFrame-1)>>8)<<6) /* Indexes that occur before iFrame */
366 + iFrame-1 /* Db page numbers that occur before iFrame */
367 );
dan7c246102010-04-12 19:00:29 +0000368}
369
drh7ed91f22010-04-29 22:34:07 +0000370/*
371** Release our reference to the wal-index memory map.
372*/
373static void walIndexUnmap(Wal *pWal){
374 if( pWal->pWiData ){
375 pWal->pVfs->xShmRelease(pWal->pWIndex);
376 pWal->pWiData = 0;
377 }
378}
dan7c246102010-04-12 19:00:29 +0000379
380/*
drh7ed91f22010-04-29 22:34:07 +0000381** Map the wal-index file into memory if it isn't already.
382*/
383static int walIndexMap(Wal *pWal){
384 int rc = SQLITE_OK;
385 if( pWal->pWiData==0 ){
386 rc = pWal->pVfs->xShmSize(pWal->pWIndex, -1,
387 &pWal->szWIndex, (void**)(char*)&pWal->pWiData);
388 }
389 return rc;
390}
391
392/*
393** Resize the wal-index file.
394*/
395static int walIndexRemap(Wal *pWal, int newSize){
396 int rc;
397 walIndexUnmap(pWal);
398 rc = pWal->pVfs->xShmSize(pWal->pWIndex, newSize,
399 &pWal->szWIndex, (void**)(char*)&pWal->pWiData);
400 return rc;
401}
402
403/*
404** Increment by which to increase the wal-index file size.
405*/
406#define WALINDEX_MMAP_INCREMENT (64*1024)
407
408/*
409** Set an entry in the wal-index map to map log frame iFrame to db
410** page iPage. Values are always appended to the wal-index (i.e. the
dan7c246102010-04-12 19:00:29 +0000411** value of iFrame is always exactly one more than the value passed to
412** the previous call), but that restriction is not enforced or asserted
413** here.
414*/
drh7ed91f22010-04-29 22:34:07 +0000415static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
416 u32 iSlot = walIndexEntry(iFrame);
417
418 walIndexMap(pWal);
419 while( (iSlot+128)>=pWal->szWIndex ){
dan31f98fc2010-04-27 05:42:32 +0000420 int rc;
drh7ed91f22010-04-29 22:34:07 +0000421 int nByte = pWal->szWIndex*4 + WALINDEX_MMAP_INCREMENT;
dance4f05f2010-04-22 19:14:13 +0000422
drh7ed91f22010-04-29 22:34:07 +0000423 /* Unmap and remap the wal-index file. */
424 rc = walIndexRemap(pWal, nByte);
dan31f98fc2010-04-27 05:42:32 +0000425 if( rc!=SQLITE_OK ){
426 return rc;
427 }
dance4f05f2010-04-22 19:14:13 +0000428 }
429
drh7ed91f22010-04-29 22:34:07 +0000430 /* Set the wal-index entry itself */
431 pWal->pWiData[iSlot] = iPage;
dan7c246102010-04-12 19:00:29 +0000432
433 /* If the frame number is a multiple of 256 (frames are numbered starting
434 ** at 1), build an index of the most recently added 256 frames.
435 */
436 if( (iFrame&0x000000FF)==0 ){
437 int i; /* Iterator used while initializing aIndex */
438 u32 *aFrame; /* Pointer to array of 256 frames */
439 int nIndex; /* Number of entries in index */
440 u8 *aIndex; /* 256 bytes to build index in */
441 u8 *aTmp; /* Scratch space to use while sorting */
442
drh7ed91f22010-04-29 22:34:07 +0000443 aFrame = &pWal->pWiData[iSlot-255];
444 aIndex = (u8 *)&pWal->pWiData[iSlot+1];
dan7c246102010-04-12 19:00:29 +0000445 aTmp = &aIndex[256];
446
447 nIndex = 256;
448 for(i=0; i<256; i++) aIndex[i] = (u8)i;
drh7ed91f22010-04-29 22:34:07 +0000449 walMergesort8(aFrame, aTmp, aIndex, &nIndex);
dan7c246102010-04-12 19:00:29 +0000450 memset(&aIndex[nIndex], aIndex[nIndex-1], 256-nIndex);
451 }
dan31f98fc2010-04-27 05:42:32 +0000452
453 return SQLITE_OK;
dan7c246102010-04-12 19:00:29 +0000454}
455
456
457/*
drh7ed91f22010-04-29 22:34:07 +0000458** Recover the wal-index by reading the write-ahead log file.
459** The caller must hold RECOVER lock on the wal-index file.
dan7c246102010-04-12 19:00:29 +0000460*/
drh7ed91f22010-04-29 22:34:07 +0000461static int walIndexRecover(Wal *pWal){
dan7c246102010-04-12 19:00:29 +0000462 int rc; /* Return Code */
463 i64 nSize; /* Size of log file */
drh7ed91f22010-04-29 22:34:07 +0000464 WalIndexHdr hdr; /* Recovered wal-index header */
dan7c246102010-04-12 19:00:29 +0000465
drh7ed91f22010-04-29 22:34:07 +0000466 assert( pWal->lockState==SQLITE_SHM_RECOVER );
dan7c246102010-04-12 19:00:29 +0000467 memset(&hdr, 0, sizeof(hdr));
468
drh7ed91f22010-04-29 22:34:07 +0000469 rc = sqlite3OsFileSize(pWal->pFd, &nSize);
dan7c246102010-04-12 19:00:29 +0000470 if( rc!=SQLITE_OK ){
471 return rc;
472 }
473
drh7ed91f22010-04-29 22:34:07 +0000474 if( nSize>WAL_FRAME_HDRSIZE ){
475 u8 aBuf[WAL_FRAME_HDRSIZE]; /* Buffer to load first frame header into */
dan7c246102010-04-12 19:00:29 +0000476 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
477 int nFrame; /* Number of bytes at aFrame */
478 u8 *aData; /* Pointer to data part of aFrame buffer */
479 int iFrame; /* Index of last frame read */
480 i64 iOffset; /* Next offset to read from log file */
481 int nPgsz; /* Page size according to the log */
dan97a31352010-04-16 13:59:31 +0000482 u32 aCksum[2]; /* Running checksum */
dan7c246102010-04-12 19:00:29 +0000483
484 /* Read in the first frame header in the file (to determine the
485 ** database page size).
486 */
drh7ed91f22010-04-29 22:34:07 +0000487 rc = sqlite3OsRead(pWal->pFd, aBuf, WAL_HDRSIZE, 0);
dan7c246102010-04-12 19:00:29 +0000488 if( rc!=SQLITE_OK ){
489 return rc;
490 }
491
492 /* If the database page size is not a power of two, or is greater than
493 ** SQLITE_MAX_PAGE_SIZE, conclude that the log file contains no valid data.
494 */
495 nPgsz = sqlite3Get4byte(&aBuf[0]);
dance4f05f2010-04-22 19:14:13 +0000496 if( nPgsz&(nPgsz-1) || nPgsz>SQLITE_MAX_PAGE_SIZE || nPgsz<512 ){
dan7c246102010-04-12 19:00:29 +0000497 goto finished;
498 }
dan97a31352010-04-16 13:59:31 +0000499 aCksum[0] = sqlite3Get4byte(&aBuf[4]);
500 aCksum[1] = sqlite3Get4byte(&aBuf[8]);
dan7c246102010-04-12 19:00:29 +0000501
502 /* Malloc a buffer to read frames into. */
drh7ed91f22010-04-29 22:34:07 +0000503 nFrame = nPgsz + WAL_FRAME_HDRSIZE;
dan7c246102010-04-12 19:00:29 +0000504 aFrame = (u8 *)sqlite3_malloc(nFrame);
505 if( !aFrame ){
506 return SQLITE_NOMEM;
507 }
drh7ed91f22010-04-29 22:34:07 +0000508 aData = &aFrame[WAL_FRAME_HDRSIZE];
dan7c246102010-04-12 19:00:29 +0000509
510 /* Read all frames from the log file. */
511 iFrame = 0;
drh7ed91f22010-04-29 22:34:07 +0000512 for(iOffset=WAL_HDRSIZE; (iOffset+nFrame)<=nSize; iOffset+=nFrame){
dan7c246102010-04-12 19:00:29 +0000513 u32 pgno; /* Database page number for frame */
514 u32 nTruncate; /* dbsize field from frame header */
515 int isValid; /* True if this frame is valid */
516
517 /* Read and decode the next log frame. */
drh7ed91f22010-04-29 22:34:07 +0000518 rc = sqlite3OsRead(pWal->pFd, aFrame, nFrame, iOffset);
dan7c246102010-04-12 19:00:29 +0000519 if( rc!=SQLITE_OK ) break;
drh7ed91f22010-04-29 22:34:07 +0000520 isValid = walDecodeFrame(aCksum, &pgno, &nTruncate, nPgsz, aData, aFrame);
dan7c246102010-04-12 19:00:29 +0000521 if( !isValid ) break;
drh7ed91f22010-04-29 22:34:07 +0000522 walIndexAppend(pWal, ++iFrame, pgno);
dan7c246102010-04-12 19:00:29 +0000523
524 /* If nTruncate is non-zero, this is a commit record. */
525 if( nTruncate ){
526 hdr.iCheck1 = aCksum[0];
527 hdr.iCheck2 = aCksum[1];
528 hdr.iLastPg = iFrame;
529 hdr.nPage = nTruncate;
530 hdr.pgsz = nPgsz;
531 }
532 }
533
534 sqlite3_free(aFrame);
535 }else{
536 hdr.iCheck1 = 2;
537 hdr.iCheck2 = 3;
538 }
539
540finished:
drh7ed91f22010-04-29 22:34:07 +0000541 walIndexWriteHdr(pWal, &hdr);
dan7c246102010-04-12 19:00:29 +0000542 return rc;
543}
544
545/*
546** Open a connection to the log file associated with database zDb. The
547** database file does not actually have to exist. zDb is used only to
548** figure out the name of the log file to open. If the log file does not
549** exist it is created by this call.
dan3de777f2010-04-17 12:31:37 +0000550**
551** A SHARED lock should be held on the database file when this function
552** is called. The purpose of this SHARED lock is to prevent any other
drh7ed91f22010-04-29 22:34:07 +0000553** client from unlinking the log or wal-index file. If another process
dan3de777f2010-04-17 12:31:37 +0000554** were to do this just after this client opened one of these files, the
555** system would be badly broken.
dan7c246102010-04-12 19:00:29 +0000556*/
drhc438efd2010-04-26 00:19:45 +0000557int sqlite3WalOpen(
drh7ed91f22010-04-29 22:34:07 +0000558 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
dan7c246102010-04-12 19:00:29 +0000559 const char *zDb, /* Name of database file */
drh7ed91f22010-04-29 22:34:07 +0000560 Wal **ppWal /* OUT: Allocated Wal handle */
dan7c246102010-04-12 19:00:29 +0000561){
danb9bf16b2010-04-14 11:23:30 +0000562 int rc = SQLITE_OK; /* Return Code */
drh7ed91f22010-04-29 22:34:07 +0000563 Wal *pRet; /* Object to allocate and return */
dan7c246102010-04-12 19:00:29 +0000564 int flags; /* Flags passed to OsOpen() */
565 char *zWal = 0; /* Path to WAL file */
566 int nWal; /* Length of zWal in bytes */
567
dan7c246102010-04-12 19:00:29 +0000568 assert( zDb );
drh7ed91f22010-04-29 22:34:07 +0000569 if( pVfs->xShmOpen==0 ) return SQLITE_CANTOPEN;
dan7c246102010-04-12 19:00:29 +0000570
drh7ed91f22010-04-29 22:34:07 +0000571 /* Allocate an instance of struct Wal to return. */
572 *ppWal = 0;
573 nWal = strlen(zDb);
574 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal+11);
575 if( !pRet ) goto wal_open_out;
dan7c246102010-04-12 19:00:29 +0000576 pRet->pVfs = pVfs;
577 pRet->pFd = (sqlite3_file *)&pRet[1];
drh7ed91f22010-04-29 22:34:07 +0000578 zWal = pVfs->szOsFile + (char*)pRet->pFd;
579 sqlite3_snprintf(nWal, zWal, "%s-wal-index", zDb);
580 rc = pVfs->xShmOpen(pVfs, zWal, &pRet->pWIndex);
581 if( rc ) goto wal_open_out;
dan7c246102010-04-12 19:00:29 +0000582
drh7ed91f22010-04-29 22:34:07 +0000583 /* Open file handle on the write-ahead log file. */
584 zWal[nWal-6] = 0;
dan67032392010-04-17 15:42:43 +0000585 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL);
drh7ed91f22010-04-29 22:34:07 +0000586 rc = sqlite3OsOpen(pVfs, zWal, pRet->pFd, flags, &flags);
dan7c246102010-04-12 19:00:29 +0000587
drh7ed91f22010-04-29 22:34:07 +0000588wal_open_out:
dan7c246102010-04-12 19:00:29 +0000589 if( rc!=SQLITE_OK ){
dan7c246102010-04-12 19:00:29 +0000590 if( pRet ){
drh7ed91f22010-04-29 22:34:07 +0000591 pVfs->xShmClose(pRet->pWIndex);
dan7c246102010-04-12 19:00:29 +0000592 sqlite3OsClose(pRet->pFd);
593 sqlite3_free(pRet);
594 }
dan7c246102010-04-12 19:00:29 +0000595 }
drh7ed91f22010-04-29 22:34:07 +0000596 *ppWal = pRet;
dan7c246102010-04-12 19:00:29 +0000597 return rc;
598}
599
drh7ed91f22010-04-29 22:34:07 +0000600static int walIteratorNext(
601 WalIterator *p, /* Iterator */
602 u32 *piPage, /* OUT: Next db page to write */
603 u32 *piFrame /* OUT: Wal frame to read from */
dan7c246102010-04-12 19:00:29 +0000604){
605 u32 iMin = *piPage;
606 u32 iRet = 0xFFFFFFFF;
607 int i;
608 int nBlock = p->nFinal;
609
610 for(i=p->nSegment-1; i>=0; i--){
drh7ed91f22010-04-29 22:34:07 +0000611 struct WalSegment *pSegment = &p->aSegment[i];
dan7c246102010-04-12 19:00:29 +0000612 while( pSegment->iNext<nBlock ){
613 u32 iPg = pSegment->aDbPage[pSegment->aIndex[pSegment->iNext]];
614 if( iPg>iMin ){
615 if( iPg<iRet ){
616 iRet = iPg;
617 *piFrame = i*256 + 1 + pSegment->aIndex[pSegment->iNext];
618 }
619 break;
620 }
621 pSegment->iNext++;
622 }
623
624 nBlock = 256;
625 }
626
627 *piPage = iRet;
628 return (iRet==0xFFFFFFFF);
629}
630
drh7ed91f22010-04-29 22:34:07 +0000631static WalIterator *walIteratorInit(Wal *pWal){
632 u32 *aData; /* Content of the wal-index file */
633 WalIterator *p; /* Return value */
dan7c246102010-04-12 19:00:29 +0000634 int nSegment; /* Number of segments to merge */
635 u32 iLast; /* Last frame in log */
636 int nByte; /* Number of bytes to allocate */
637 int i; /* Iterator variable */
638 int nFinal; /* Number of unindexed entries */
drh7ed91f22010-04-29 22:34:07 +0000639 struct WalSegment *pFinal; /* Final (unindexed) segment */
dan7c246102010-04-12 19:00:29 +0000640 u8 *aTmp; /* Temp space used by merge-sort */
641
drh7ed91f22010-04-29 22:34:07 +0000642 walIndexMap(pWal);
643 aData = pWal->pWiData;
644 iLast = pWal->hdr.iLastPg;
dan7c246102010-04-12 19:00:29 +0000645 nSegment = (iLast >> 8) + 1;
646 nFinal = (iLast & 0x000000FF);
647
drh7ed91f22010-04-29 22:34:07 +0000648 nByte = sizeof(WalIterator) + (nSegment-1)*sizeof(struct WalSegment) + 512;
649 p = (WalIterator *)sqlite3_malloc(nByte);
dan7c246102010-04-12 19:00:29 +0000650 if( p ){
651 memset(p, 0, nByte);
652 p->nSegment = nSegment;
653 p->nFinal = nFinal;
654 }
655
656 for(i=0; i<nSegment-1; i++){
drh7ed91f22010-04-29 22:34:07 +0000657 p->aSegment[i].aDbPage = &aData[walIndexEntry(i*256+1)];
658 p->aSegment[i].aIndex = (u8 *)&aData[walIndexEntry(i*256+1)+256];
dan7c246102010-04-12 19:00:29 +0000659 }
660 pFinal = &p->aSegment[nSegment-1];
661
drh7ed91f22010-04-29 22:34:07 +0000662 pFinal->aDbPage = &aData[walIndexEntry((nSegment-1)*256+1)];
dan7c246102010-04-12 19:00:29 +0000663 pFinal->aIndex = (u8 *)&pFinal[1];
664 aTmp = &pFinal->aIndex[256];
665 for(i=0; i<nFinal; i++){
666 pFinal->aIndex[i] = i;
667 }
drh7ed91f22010-04-29 22:34:07 +0000668 walMergesort8(pFinal->aDbPage, aTmp, pFinal->aIndex, &nFinal);
dan7c246102010-04-12 19:00:29 +0000669 p->nFinal = nFinal;
670
671 return p;
672}
673
674/*
drh7ed91f22010-04-29 22:34:07 +0000675** Free a log iterator allocated by walIteratorInit().
dan7c246102010-04-12 19:00:29 +0000676*/
drh7ed91f22010-04-29 22:34:07 +0000677static void walIteratorFree(WalIterator *p){
dan7c246102010-04-12 19:00:29 +0000678 sqlite3_free(p);
679}
680
681/*
682** Checkpoint the contents of the log file.
683*/
drh7ed91f22010-04-29 22:34:07 +0000684static int walCheckpoint(
685 Wal *pWal, /* Wal connection */
dan7c246102010-04-12 19:00:29 +0000686 sqlite3_file *pFd, /* File descriptor open on db file */
danc5118782010-04-17 17:34:41 +0000687 int sync_flags, /* Flags for OsSync() (or 0) */
dan7c246102010-04-12 19:00:29 +0000688 u8 *zBuf /* Temporary buffer to use */
689){
690 int rc; /* Return code */
drh7ed91f22010-04-29 22:34:07 +0000691 int pgsz = pWal->hdr.pgsz; /* Database page-size */
692 WalIterator *pIter = 0; /* Wal iterator context */
dan7c246102010-04-12 19:00:29 +0000693 u32 iDbpage = 0; /* Next database page to write */
drh7ed91f22010-04-29 22:34:07 +0000694 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
dan7c246102010-04-12 19:00:29 +0000695
drh7ed91f22010-04-29 22:34:07 +0000696 if( pWal->hdr.iLastPg==0 ){
danbb2e9c92010-04-15 13:33:18 +0000697 return SQLITE_OK;
698 }
699
dan7c246102010-04-12 19:00:29 +0000700 /* Allocate the iterator */
drh7ed91f22010-04-29 22:34:07 +0000701 pIter = walIteratorInit(pWal);
dan7c246102010-04-12 19:00:29 +0000702 if( !pIter ) return SQLITE_NOMEM;
703
704 /* Sync the log file to disk */
danc5118782010-04-17 17:34:41 +0000705 if( sync_flags ){
drh7ed91f22010-04-29 22:34:07 +0000706 rc = sqlite3OsSync(pWal->pFd, sync_flags);
danc5118782010-04-17 17:34:41 +0000707 if( rc!=SQLITE_OK ) goto out;
708 }
dan7c246102010-04-12 19:00:29 +0000709
710 /* Iterate through the contents of the log, copying data to the db file. */
drh7ed91f22010-04-29 22:34:07 +0000711 while( 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
712 rc = sqlite3OsRead(pWal->pFd, zBuf, pgsz,
713 walFrameOffset(iFrame, pgsz) + WAL_FRAME_HDRSIZE
dan7c246102010-04-12 19:00:29 +0000714 );
715 if( rc!=SQLITE_OK ) goto out;
716 rc = sqlite3OsWrite(pFd, zBuf, pgsz, (iDbpage-1)*pgsz);
717 if( rc!=SQLITE_OK ) goto out;
718 }
719
720 /* Truncate the database file */
drh7ed91f22010-04-29 22:34:07 +0000721 rc = sqlite3OsTruncate(pFd, ((i64)pWal->hdr.nPage*(i64)pgsz));
dan7c246102010-04-12 19:00:29 +0000722 if( rc!=SQLITE_OK ) goto out;
723
drh7ed91f22010-04-29 22:34:07 +0000724 /* Sync the database file. If successful, update the wal-index. */
danc5118782010-04-17 17:34:41 +0000725 if( sync_flags ){
726 rc = sqlite3OsSync(pFd, sync_flags);
727 if( rc!=SQLITE_OK ) goto out;
728 }
drh7ed91f22010-04-29 22:34:07 +0000729 pWal->hdr.iLastPg = 0;
730 pWal->hdr.iCheck1 = 2;
731 pWal->hdr.iCheck2 = 3;
732 walIndexWriteHdr(pWal, &pWal->hdr);
dan7c246102010-04-12 19:00:29 +0000733
734 /* TODO: If a crash occurs and the current log is copied into the
735 ** database there is no problem. However, if a crash occurs while
736 ** writing the next transaction into the start of the log, such that:
737 **
738 ** * The first transaction currently in the log is left intact, but
739 ** * The second (or subsequent) transaction is damaged,
740 **
741 ** then the database could become corrupt.
742 **
743 ** The easiest thing to do would be to write and sync a dummy header
744 ** into the log at this point. Unfortunately, that turns out to be
745 ** an unwelcome performance hit. Alternatives are...
746 */
747#if 0
drh7ed91f22010-04-29 22:34:07 +0000748 memset(zBuf, 0, WAL_FRAME_HDRSIZE);
749 rc = sqlite3OsWrite(pWal->pFd, zBuf, WAL_FRAME_HDRSIZE, 0);
dan7c246102010-04-12 19:00:29 +0000750 if( rc!=SQLITE_OK ) goto out;
drh7ed91f22010-04-29 22:34:07 +0000751 rc = sqlite3OsSync(pWal->pFd, pWal->sync_flags);
dan7c246102010-04-12 19:00:29 +0000752#endif
753
754 out:
drh7ed91f22010-04-29 22:34:07 +0000755 walIteratorFree(pIter);
dan7c246102010-04-12 19:00:29 +0000756 return rc;
757}
758
759/*
760** Close a connection to a log file.
761*/
drhc438efd2010-04-26 00:19:45 +0000762int sqlite3WalClose(
drh7ed91f22010-04-29 22:34:07 +0000763 Wal *pWal, /* Wal to close */
dan7c246102010-04-12 19:00:29 +0000764 sqlite3_file *pFd, /* Database file */
danc5118782010-04-17 17:34:41 +0000765 int sync_flags, /* Flags to pass to OsSync() (or 0) */
dan7c246102010-04-12 19:00:29 +0000766 u8 *zBuf /* Buffer of at least page-size bytes */
767){
768 int rc = SQLITE_OK;
drh7ed91f22010-04-29 22:34:07 +0000769 if( pWal ){
770 pWal->pVfs->xShmClose(pWal->pWIndex);
771 sqlite3OsClose(pWal->pFd);
772 sqlite3_free(pWal);
dan7c246102010-04-12 19:00:29 +0000773 }
774 return rc;
775}
776
777/*
drh7ed91f22010-04-29 22:34:07 +0000778** Try to read the wal-index header. Attempt to verify the header
779** checksum. If the checksum can be verified, copy the wal-index
780** header into structure pWal->hdr. If the contents of pWal->hdr are
danb9bf16b2010-04-14 11:23:30 +0000781** modified by this and pChanged is not NULL, set *pChanged to 1.
782** Otherwise leave *pChanged unmodified.
783**
784** If the checksum cannot be verified return SQLITE_ERROR.
785*/
drh7ed91f22010-04-29 22:34:07 +0000786int walIndexTryHdr(Wal *pWal, int *pChanged){
danb9bf16b2010-04-14 11:23:30 +0000787 u32 aCksum[2] = {1, 1};
drh7ed91f22010-04-29 22:34:07 +0000788 u32 aHdr[WALINDEX_HDR_NFIELD+2];
danb9bf16b2010-04-14 11:23:30 +0000789
drh7ed91f22010-04-29 22:34:07 +0000790 /* Read the header. The caller may or may not have locked the wal-index
dancd11fb22010-04-26 10:40:52 +0000791 ** file, meaning it is possible that an inconsistent snapshot is read
792 ** from the file. If this happens, return SQLITE_ERROR. The caller will
793 ** retry. Or, if the caller has already locked the file and the header
794 ** still looks inconsistent, it will run recovery.
danb9bf16b2010-04-14 11:23:30 +0000795 */
drh7ed91f22010-04-29 22:34:07 +0000796 memcpy(aHdr, pWal->pWiData, sizeof(aHdr));
797 walChecksumBytes((u8*)aHdr, sizeof(u32)*WALINDEX_HDR_NFIELD, aCksum);
798 if( aCksum[0]!=aHdr[WALINDEX_HDR_NFIELD]
799 || aCksum[1]!=aHdr[WALINDEX_HDR_NFIELD+1]
danb9bf16b2010-04-14 11:23:30 +0000800 ){
801 return SQLITE_ERROR;
802 }
803
drh7ed91f22010-04-29 22:34:07 +0000804 if( memcmp(&pWal->hdr, aHdr, sizeof(WalIndexHdr)) ){
danb9bf16b2010-04-14 11:23:30 +0000805 if( pChanged ){
806 *pChanged = 1;
807 }
drh7ed91f22010-04-29 22:34:07 +0000808 memcpy(&pWal->hdr, aHdr, sizeof(WalIndexHdr));
danb9bf16b2010-04-14 11:23:30 +0000809 }
810 return SQLITE_OK;
811}
812
813/*
drh7ed91f22010-04-29 22:34:07 +0000814** Read the wal-index header from the wal-index file into structure
815** pWal->hdr. If attempting to verify the header checksum fails, try
danb9bf16b2010-04-14 11:23:30 +0000816** to recover the log before returning.
817**
drh7ed91f22010-04-29 22:34:07 +0000818** If the wal-index header is successfully read, return SQLITE_OK.
danb9bf16b2010-04-14 11:23:30 +0000819** Otherwise an SQLite error code.
820*/
drh7ed91f22010-04-29 22:34:07 +0000821static int walIndexReadHdr(Wal *pWal, int *pChanged){
danb9bf16b2010-04-14 11:23:30 +0000822 int rc;
823
drh7ed91f22010-04-29 22:34:07 +0000824 assert( pWal->lockState==SQLITE_SHM_READ );
825 walIndexMap(pWal);
826
danb9bf16b2010-04-14 11:23:30 +0000827 /* First try to read the header without a lock. Verify the checksum
828 ** before returning. This will almost always work.
829 */
drh7ed91f22010-04-29 22:34:07 +0000830 if( SQLITE_OK==walIndexTryHdr(pWal, pChanged) ){
danb9bf16b2010-04-14 11:23:30 +0000831 return SQLITE_OK;
832 }
833
drh7ed91f22010-04-29 22:34:07 +0000834 /* If the first attempt to read the header failed, lock the wal-index
danb9bf16b2010-04-14 11:23:30 +0000835 ** file and try again. If the header checksum verification fails this
836 ** time as well, run log recovery.
837 */
drh7ed91f22010-04-29 22:34:07 +0000838 if( SQLITE_OK==(rc = walSetLock(pWal, SQLITE_SHM_RECOVER)) ){
839 if( SQLITE_OK!=walIndexTryHdr(pWal, pChanged) ){
danb9bf16b2010-04-14 11:23:30 +0000840 if( pChanged ){
841 *pChanged = 1;
842 }
drh7ed91f22010-04-29 22:34:07 +0000843 rc = walIndexRecover(pWal);
danb9bf16b2010-04-14 11:23:30 +0000844 if( rc==SQLITE_OK ){
drh7ed91f22010-04-29 22:34:07 +0000845 rc = walIndexTryHdr(pWal, 0);
danb9bf16b2010-04-14 11:23:30 +0000846 }
847 }
drh7ed91f22010-04-29 22:34:07 +0000848 walSetLock(pWal, SQLITE_SHM_READ);
danb9bf16b2010-04-14 11:23:30 +0000849 }
850
851 return rc;
852}
853
854/*
dan64d039e2010-04-13 19:27:31 +0000855** Lock a snapshot.
dan7c246102010-04-12 19:00:29 +0000856**
857** If this call obtains a new read-lock and the database contents have been
drh7ed91f22010-04-29 22:34:07 +0000858** modified since the most recent call to WalCloseSnapshot() on this Wal
dan7c246102010-04-12 19:00:29 +0000859** connection, then *pChanged is set to 1 before returning. Otherwise, it
860** is left unmodified. This is used by the pager layer to determine whether
861** or not any cached pages may be safely reused.
862*/
drh7ed91f22010-04-29 22:34:07 +0000863int sqlite3WalOpenSnapshot(Wal *pWal, int *pChanged){
864 int rc;
dan64d039e2010-04-13 19:27:31 +0000865
drh7ed91f22010-04-29 22:34:07 +0000866 rc = walSetLock(pWal, SQLITE_SHM_READ);
867 if( rc==SQLITE_OK ){
868 pWal->lockState = SQLITE_SHM_READ;
dan64d039e2010-04-13 19:27:31 +0000869
drh7ed91f22010-04-29 22:34:07 +0000870 rc = walIndexReadHdr(pWal, pChanged);
dan64d039e2010-04-13 19:27:31 +0000871 if( rc!=SQLITE_OK ){
872 /* An error occured while attempting log recovery. */
drh7ed91f22010-04-29 22:34:07 +0000873 sqlite3WalCloseSnapshot(pWal);
dan31f98fc2010-04-27 05:42:32 +0000874 }else{
875 /* Check if the mapping needs to grow. */
drh7ed91f22010-04-29 22:34:07 +0000876 if( pWal->hdr.iLastPg
877 && walIndexEntry(pWal->hdr.iLastPg)>=pWal->szWIndex
878 ){
879 rc = walIndexRemap(pWal, 0);
880 assert( rc || walIndexEntry(pWal->hdr.iLastPg)<pWal->szWIndex );
dan31f98fc2010-04-27 05:42:32 +0000881 }
dan64d039e2010-04-13 19:27:31 +0000882 }
dan7c246102010-04-12 19:00:29 +0000883 }
884 return rc;
885}
886
887/*
888** Unlock the current snapshot.
889*/
drh7ed91f22010-04-29 22:34:07 +0000890void sqlite3WalCloseSnapshot(Wal *pWal){
891 if( pWal->lockState!=SQLITE_SHM_UNLOCK ){
892 assert( pWal->lockState==SQLITE_SHM_READ );
893 walSetLock(pWal, SQLITE_SHM_UNLOCK);
dan64d039e2010-04-13 19:27:31 +0000894 }
dan7c246102010-04-12 19:00:29 +0000895}
896
dan5e0ce872010-04-28 17:48:44 +0000897/*
dan7c246102010-04-12 19:00:29 +0000898** Read a page from the log, if it is present.
899*/
drh7ed91f22010-04-29 22:34:07 +0000900int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, u8 *pOut){
dan7c246102010-04-12 19:00:29 +0000901 u32 iRead = 0;
dancd11fb22010-04-26 10:40:52 +0000902 u32 *aData;
drh7ed91f22010-04-29 22:34:07 +0000903 int iFrame = (pWal->hdr.iLastPg & 0xFFFFFF00);
dan7c246102010-04-12 19:00:29 +0000904
drh7ed91f22010-04-29 22:34:07 +0000905 assert( pWal->lockState==SQLITE_SHM_READ );
906 walIndexMap(pWal);
dancd11fb22010-04-26 10:40:52 +0000907
dan7c246102010-04-12 19:00:29 +0000908 /* Do a linear search of the unindexed block of page-numbers (if any)
drh7ed91f22010-04-29 22:34:07 +0000909 ** at the end of the wal-index. An alternative to this would be to
dan7c246102010-04-12 19:00:29 +0000910 ** build an index in private memory each time a read transaction is
911 ** opened on a new snapshot.
912 */
drh7ed91f22010-04-29 22:34:07 +0000913 aData = pWal->pWiData;
914 if( pWal->hdr.iLastPg ){
915 u32 *pi = &aData[walIndexEntry(pWal->hdr.iLastPg)];
916 u32 *piStop = pi - (pWal->hdr.iLastPg & 0xFF);
dan7c246102010-04-12 19:00:29 +0000917 while( *pi!=pgno && pi!=piStop ) pi--;
918 if( pi!=piStop ){
919 iRead = (pi-piStop) + iFrame;
920 }
921 }
drh7ed91f22010-04-29 22:34:07 +0000922 assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno );
dan7c246102010-04-12 19:00:29 +0000923
924 while( iRead==0 && iFrame>0 ){
925 int iLow = 0;
926 int iHigh = 255;
927 u32 *aFrame;
928 u8 *aIndex;
929
930 iFrame -= 256;
drh7ed91f22010-04-29 22:34:07 +0000931 aFrame = &aData[walIndexEntry(iFrame+1)];
dan7c246102010-04-12 19:00:29 +0000932 aIndex = (u8 *)&aFrame[256];
933
934 while( iLow<=iHigh ){
935 int iTest = (iLow+iHigh)>>1;
936 u32 iPg = aFrame[aIndex[iTest]];
937
938 if( iPg==pgno ){
939 iRead = iFrame + 1 + aIndex[iTest];
940 break;
941 }
942 else if( iPg<pgno ){
943 iLow = iTest+1;
944 }else{
945 iHigh = iTest-1;
946 }
947 }
948 }
drh7ed91f22010-04-29 22:34:07 +0000949 assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno );
950 walIndexUnmap(pWal);
dancd11fb22010-04-26 10:40:52 +0000951
dan7c246102010-04-12 19:00:29 +0000952 /* If iRead is non-zero, then it is the log frame number that contains the
953 ** required page. Read and return data from the log file.
954 */
955 if( iRead ){
drh7ed91f22010-04-29 22:34:07 +0000956 i64 iOffset = walFrameOffset(iRead, pWal->hdr.pgsz) + WAL_FRAME_HDRSIZE;
957 *pInWal = 1;
958 return sqlite3OsRead(pWal->pFd, pOut, pWal->hdr.pgsz, iOffset);
dan7c246102010-04-12 19:00:29 +0000959 }
960
drh7ed91f22010-04-29 22:34:07 +0000961 *pInWal = 0;
dan7c246102010-04-12 19:00:29 +0000962 return SQLITE_OK;
963}
964
965
966/*
967** Set *pPgno to the size of the database file (or zero, if unknown).
968*/
drh7ed91f22010-04-29 22:34:07 +0000969void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){
970 assert( pWal->lockState==SQLITE_SHM_READ
971 || pWal->lockState==SQLITE_SHM_WRITE );
972 *pPgno = pWal->hdr.nPage;
dan7c246102010-04-12 19:00:29 +0000973}
974
975/*
dan7c246102010-04-12 19:00:29 +0000976** This function returns SQLITE_OK if the caller may write to the database.
977** Otherwise, if the caller is operating on a snapshot that has already
dan49320f82010-04-14 18:50:08 +0000978** been overwritten by another writer, SQLITE_BUSY is returned.
dan7c246102010-04-12 19:00:29 +0000979*/
drh7ed91f22010-04-29 22:34:07 +0000980int sqlite3WalWriteLock(Wal *pWal, int op){
981 int rc;
dan7c246102010-04-12 19:00:29 +0000982 if( op ){
drh7ed91f22010-04-29 22:34:07 +0000983 assert( pWal->lockState == SQLITE_SHM_READ );
984 rc = walSetLock(pWal, SQLITE_SHM_WRITE);
985 }else if( pWal->lockState==SQLITE_SHM_WRITE ){
986 rc = walSetLock(pWal, SQLITE_SHM_READ);
dan7c246102010-04-12 19:00:29 +0000987 }
drh7ed91f22010-04-29 22:34:07 +0000988 return rc;
dan7c246102010-04-12 19:00:29 +0000989}
990
dan74d6cd82010-04-24 18:44:05 +0000991/*
drh7ed91f22010-04-29 22:34:07 +0000992** The Wal object passed to this function must be holding the write-lock.
dan74d6cd82010-04-24 18:44:05 +0000993**
994** If any data has been written (but not committed) to the log file, this
995** function moves the write-pointer back to the start of the transaction.
996**
997** Additionally, the callback function is invoked for each frame written
998** to the log since the start of the transaction. If the callback returns
999** other than SQLITE_OK, it is not invoked again and the error code is
1000** returned to the caller.
1001**
1002** Otherwise, if the callback function does not return an error, this
1003** function returns SQLITE_OK.
1004*/
drh7ed91f22010-04-29 22:34:07 +00001005int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
dan74d6cd82010-04-24 18:44:05 +00001006 int rc = SQLITE_OK;
drh7ed91f22010-04-29 22:34:07 +00001007 Pgno iMax = pWal->hdr.iLastPg;
dan74d6cd82010-04-24 18:44:05 +00001008 Pgno iFrame;
1009
drh7ed91f22010-04-29 22:34:07 +00001010 assert( pWal->lockState==SQLITE_SHM_WRITE );
1011 walIndexReadHdr(pWal, 0);
1012 for(iFrame=pWal->hdr.iLastPg+1; iFrame<=iMax && rc==SQLITE_OK; iFrame++){
1013 rc = xUndo(pUndoCtx, pWal->pWiData[walIndexEntry(iFrame)]);
dan74d6cd82010-04-24 18:44:05 +00001014 }
drh7ed91f22010-04-29 22:34:07 +00001015 walIndexUnmap(pWal);
dan74d6cd82010-04-24 18:44:05 +00001016 return rc;
1017}
1018
drh7ed91f22010-04-29 22:34:07 +00001019/* Return an integer that records the current (uncommitted) write
1020** position in the WAL
1021*/
1022u32 sqlite3WalSavepoint(Wal *pWal){
1023 assert( pWal->lockState==SQLITE_SHM_WRITE );
1024 return pWal->hdr.iLastPg;
dan4cd78b42010-04-26 16:57:10 +00001025}
1026
drh7ed91f22010-04-29 22:34:07 +00001027/* Move the write position of the WAL back to iFrame. Called in
1028** response to a ROLLBACK TO command.
1029*/
1030int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){
dan4cd78b42010-04-26 16:57:10 +00001031 int rc = SQLITE_OK;
1032 u8 aCksum[8];
drh7ed91f22010-04-29 22:34:07 +00001033 assert( pWal->lockState==SQLITE_SHM_WRITE );
dan4cd78b42010-04-26 16:57:10 +00001034
drh7ed91f22010-04-29 22:34:07 +00001035 pWal->hdr.iLastPg = iFrame;
dan4cd78b42010-04-26 16:57:10 +00001036 if( iFrame>0 ){
drh7ed91f22010-04-29 22:34:07 +00001037 i64 iOffset = walFrameOffset(iFrame, pWal->hdr.pgsz) + sizeof(u32)*2;
1038 rc = sqlite3OsRead(pWal->pFd, aCksum, sizeof(aCksum), iOffset);
1039 pWal->hdr.iCheck1 = sqlite3Get4byte(&aCksum[0]);
1040 pWal->hdr.iCheck2 = sqlite3Get4byte(&aCksum[4]);
dan4cd78b42010-04-26 16:57:10 +00001041 }
1042
1043 return rc;
1044}
1045
dan7c246102010-04-12 19:00:29 +00001046/*
dan3306c4a2010-04-23 19:15:00 +00001047** Return true if data has been written but not committed to the log file.
1048*/
drh7ed91f22010-04-29 22:34:07 +00001049int sqlite3WalDirty(Wal *pWal){
1050 assert( pWal->lockState==SQLITE_SHM_WRITE );
1051 return( pWal->hdr.iLastPg!=((WalIndexHdr*)pWal->pWiData)->iLastPg );
dan3306c4a2010-04-23 19:15:00 +00001052}
1053
1054/*
dan4cd78b42010-04-26 16:57:10 +00001055** Write a set of frames to the log. The caller must hold the write-lock
1056** on the log file (obtained using sqlite3WalWriteLock()).
dan7c246102010-04-12 19:00:29 +00001057*/
drhc438efd2010-04-26 00:19:45 +00001058int sqlite3WalFrames(
drh7ed91f22010-04-29 22:34:07 +00001059 Wal *pWal, /* Wal handle to write to */
dan7c246102010-04-12 19:00:29 +00001060 int nPgsz, /* Database page-size in bytes */
1061 PgHdr *pList, /* List of dirty pages to write */
1062 Pgno nTruncate, /* Database size after this commit */
1063 int isCommit, /* True if this is a commit */
danc5118782010-04-17 17:34:41 +00001064 int sync_flags /* Flags to pass to OsSync() (or 0) */
dan7c246102010-04-12 19:00:29 +00001065){
dan7c246102010-04-12 19:00:29 +00001066 int rc; /* Used to catch return codes */
1067 u32 iFrame; /* Next frame address */
drh7ed91f22010-04-29 22:34:07 +00001068 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
dan7c246102010-04-12 19:00:29 +00001069 PgHdr *p; /* Iterator to run through pList with. */
dan97a31352010-04-16 13:59:31 +00001070 u32 aCksum[2]; /* Checksums */
dan7c246102010-04-12 19:00:29 +00001071 PgHdr *pLast; /* Last frame in list */
1072 int nLast = 0; /* Number of extra copies of last page */
1073
drh7ed91f22010-04-29 22:34:07 +00001074 assert( WAL_FRAME_HDRSIZE==(4 * 2 + 2*sizeof(u32)) );
dan7c246102010-04-12 19:00:29 +00001075 assert( pList );
drh7ed91f22010-04-29 22:34:07 +00001076 assert( pWal->lockState==SQLITE_SHM_WRITE );
dan7c246102010-04-12 19:00:29 +00001077
dan97a31352010-04-16 13:59:31 +00001078 /* If this is the first frame written into the log, write the log
1079 ** header to the start of the log file. See comments at the top of
1080 ** this file for a description of the log-header format.
1081 */
drh7ed91f22010-04-29 22:34:07 +00001082 assert( WAL_FRAME_HDRSIZE>=WAL_HDRSIZE );
1083 iFrame = pWal->hdr.iLastPg;
dan97a31352010-04-16 13:59:31 +00001084 if( iFrame==0 ){
1085 sqlite3Put4byte(aFrame, nPgsz);
1086 sqlite3_randomness(8, &aFrame[4]);
drh7ed91f22010-04-29 22:34:07 +00001087 pWal->hdr.iCheck1 = sqlite3Get4byte(&aFrame[4]);
1088 pWal->hdr.iCheck2 = sqlite3Get4byte(&aFrame[8]);
1089 rc = sqlite3OsWrite(pWal->pFd, aFrame, WAL_HDRSIZE, 0);
dan97a31352010-04-16 13:59:31 +00001090 if( rc!=SQLITE_OK ){
1091 return rc;
1092 }
1093 }
1094
drh7ed91f22010-04-29 22:34:07 +00001095 aCksum[0] = pWal->hdr.iCheck1;
1096 aCksum[1] = pWal->hdr.iCheck2;
dan7c246102010-04-12 19:00:29 +00001097
1098 /* Write the log file. */
dan7c246102010-04-12 19:00:29 +00001099 for(p=pList; p; p=p->pDirty){
1100 u32 nDbsize; /* Db-size field for frame header */
1101 i64 iOffset; /* Write offset in log file */
1102
drh7ed91f22010-04-29 22:34:07 +00001103 iOffset = walFrameOffset(++iFrame, nPgsz);
dan7c246102010-04-12 19:00:29 +00001104
1105 /* Populate and write the frame header */
1106 nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
drh7ed91f22010-04-29 22:34:07 +00001107 walEncodeFrame(aCksum, p->pgno, nDbsize, nPgsz, p->pData, aFrame);
1108 rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset);
dan7c246102010-04-12 19:00:29 +00001109 if( rc!=SQLITE_OK ){
1110 return rc;
1111 }
1112
1113 /* Write the page data */
drh7ed91f22010-04-29 22:34:07 +00001114 rc = sqlite3OsWrite(pWal->pFd, p->pData, nPgsz, iOffset + sizeof(aFrame));
dan7c246102010-04-12 19:00:29 +00001115 if( rc!=SQLITE_OK ){
1116 return rc;
1117 }
1118 pLast = p;
1119 }
1120
1121 /* Sync the log file if the 'isSync' flag was specified. */
danc5118782010-04-17 17:34:41 +00001122 if( sync_flags ){
drh7ed91f22010-04-29 22:34:07 +00001123 i64 iSegment = sqlite3OsSectorSize(pWal->pFd);
1124 i64 iOffset = walFrameOffset(iFrame+1, nPgsz);
dan67032392010-04-17 15:42:43 +00001125
1126 assert( isCommit );
dan7c246102010-04-12 19:00:29 +00001127
1128 if( iSegment<SQLITE_DEFAULT_SECTOR_SIZE ){
1129 iSegment = SQLITE_DEFAULT_SECTOR_SIZE;
1130 }
1131 iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
1132 while( iOffset<iSegment ){
drh7ed91f22010-04-29 22:34:07 +00001133 walEncodeFrame(aCksum,pLast->pgno,nTruncate,nPgsz,pLast->pData,aFrame);
1134 rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset);
dan7c246102010-04-12 19:00:29 +00001135 if( rc!=SQLITE_OK ){
1136 return rc;
1137 }
1138
drh7ed91f22010-04-29 22:34:07 +00001139 iOffset += WAL_FRAME_HDRSIZE;
1140 rc = sqlite3OsWrite(pWal->pFd, pLast->pData, nPgsz, iOffset);
dan7c246102010-04-12 19:00:29 +00001141 if( rc!=SQLITE_OK ){
1142 return rc;
1143 }
1144 nLast++;
1145 iOffset += nPgsz;
1146 }
dan7c246102010-04-12 19:00:29 +00001147
drh7ed91f22010-04-29 22:34:07 +00001148 rc = sqlite3OsSync(pWal->pFd, sync_flags);
dan7c246102010-04-12 19:00:29 +00001149 if( rc!=SQLITE_OK ){
1150 return rc;
1151 }
1152 }
1153
1154 /* Append data to the log summary. It is not necessary to lock the
drh7ed91f22010-04-29 22:34:07 +00001155 ** wal-index to do this as the RESERVED lock held on the db file
dan7c246102010-04-12 19:00:29 +00001156 ** guarantees that there are no other writers, and no data that may
1157 ** be in use by existing readers is being overwritten.
1158 */
drh7ed91f22010-04-29 22:34:07 +00001159 iFrame = pWal->hdr.iLastPg;
dan7c246102010-04-12 19:00:29 +00001160 for(p=pList; p; p=p->pDirty){
1161 iFrame++;
drh7ed91f22010-04-29 22:34:07 +00001162 walIndexAppend(pWal, iFrame, p->pgno);
dan7c246102010-04-12 19:00:29 +00001163 }
1164 while( nLast>0 ){
1165 iFrame++;
1166 nLast--;
drh7ed91f22010-04-29 22:34:07 +00001167 walIndexAppend(pWal, iFrame, pLast->pgno);
dan7c246102010-04-12 19:00:29 +00001168 }
1169
1170 /* Update the private copy of the header. */
drh7ed91f22010-04-29 22:34:07 +00001171 pWal->hdr.pgsz = nPgsz;
1172 pWal->hdr.iLastPg = iFrame;
dan7c246102010-04-12 19:00:29 +00001173 if( isCommit ){
drh7ed91f22010-04-29 22:34:07 +00001174 pWal->hdr.iChange++;
1175 pWal->hdr.nPage = nTruncate;
dan7c246102010-04-12 19:00:29 +00001176 }
drh7ed91f22010-04-29 22:34:07 +00001177 pWal->hdr.iCheck1 = aCksum[0];
1178 pWal->hdr.iCheck2 = aCksum[1];
dan7c246102010-04-12 19:00:29 +00001179
drh7ed91f22010-04-29 22:34:07 +00001180 /* If this is a commit, update the wal-index header too. */
1181 if( isCommit ){
1182 walIndexWriteHdr(pWal, &pWal->hdr);
1183 pWal->iCallback = iFrame;
dan7c246102010-04-12 19:00:29 +00001184 }
drh7ed91f22010-04-29 22:34:07 +00001185 walIndexUnmap(pWal);
dan7c246102010-04-12 19:00:29 +00001186
dan8d22a172010-04-19 18:03:51 +00001187 return rc;
dan7c246102010-04-12 19:00:29 +00001188}
1189
1190/*
danb9bf16b2010-04-14 11:23:30 +00001191** Checkpoint the database:
1192**
drh7ed91f22010-04-29 22:34:07 +00001193** 1. Acquire a CHECKPOINT lock
1194** 2. Copy the contents of the log into the database file.
1195** 3. Zero the wal-index header (so new readers will ignore the log).
1196** 4. Drop the CHECKPOINT lock.
dan7c246102010-04-12 19:00:29 +00001197*/
drhc438efd2010-04-26 00:19:45 +00001198int sqlite3WalCheckpoint(
drh7ed91f22010-04-29 22:34:07 +00001199 Wal *pWal, /* Wal connection */
dan7c246102010-04-12 19:00:29 +00001200 sqlite3_file *pFd, /* File descriptor open on db file */
danc5118782010-04-17 17:34:41 +00001201 int sync_flags, /* Flags to sync db file with (or 0) */
dan64d039e2010-04-13 19:27:31 +00001202 u8 *zBuf, /* Temporary buffer to use */
1203 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
1204 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
dan7c246102010-04-12 19:00:29 +00001205){
danb9bf16b2010-04-14 11:23:30 +00001206 int rc; /* Return code */
dan31c03902010-04-29 14:51:33 +00001207 int isChanged = 0; /* True if a new wal-index header is loaded */
dan7c246102010-04-12 19:00:29 +00001208
drh7ed91f22010-04-29 22:34:07 +00001209 assert( pWal->lockState==SQLITE_SHM_UNLOCK );
dan39c79f52010-04-15 10:58:51 +00001210
drh7ed91f22010-04-29 22:34:07 +00001211 /* Get the CHECKPOINT lock */
dan64d039e2010-04-13 19:27:31 +00001212 do {
drh7ed91f22010-04-29 22:34:07 +00001213 rc = walSetLock(pWal, SQLITE_SHM_CHECKPOINT);
dan64d039e2010-04-13 19:27:31 +00001214 }while( rc==SQLITE_BUSY && xBusyHandler(pBusyHandlerArg) );
danb9bf16b2010-04-14 11:23:30 +00001215 if( rc!=SQLITE_OK ){
drh7ed91f22010-04-29 22:34:07 +00001216 walSetLock(pWal, SQLITE_SHM_UNLOCK);
danb9bf16b2010-04-14 11:23:30 +00001217 return rc;
1218 }
dan64d039e2010-04-13 19:27:31 +00001219
danb9bf16b2010-04-14 11:23:30 +00001220 /* Copy data from the log to the database file. */
drh7ed91f22010-04-29 22:34:07 +00001221 rc = walIndexReadHdr(pWal, &isChanged);
danb9bf16b2010-04-14 11:23:30 +00001222 if( rc==SQLITE_OK ){
drh7ed91f22010-04-29 22:34:07 +00001223 rc = walCheckpoint(pWal, pFd, sync_flags, zBuf);
danb9bf16b2010-04-14 11:23:30 +00001224 }
dan31c03902010-04-29 14:51:33 +00001225 if( isChanged ){
1226 /* If a new wal-index header was loaded before the checkpoint was
drh7ed91f22010-04-29 22:34:07 +00001227 ** performed, then the pager-cache associated with log pWal is now
dan31c03902010-04-29 14:51:33 +00001228 ** out of date. So zero the cached wal-index header to ensure that
1229 ** next time the pager opens a snapshot on this database it knows that
1230 ** the cache needs to be reset.
1231 */
drh7ed91f22010-04-29 22:34:07 +00001232 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
dan31c03902010-04-29 14:51:33 +00001233 }
danb9bf16b2010-04-14 11:23:30 +00001234
1235 /* Release the locks. */
drh7ed91f22010-04-29 22:34:07 +00001236 walSetLock(pWal, SQLITE_SHM_UNLOCK);
dan64d039e2010-04-13 19:27:31 +00001237 return rc;
dan7c246102010-04-12 19:00:29 +00001238}
1239
drh7ed91f22010-04-29 22:34:07 +00001240/* Return the value to pass to a sqlite3_wal_hook callback, the
1241** number of frames in the WAL at the point of the last commit since
1242** sqlite3WalCallback() was called. If no commits have occurred since
1243** the last call, then return 0.
1244*/
1245int sqlite3WalCallback(Wal *pWal){
dan8d22a172010-04-19 18:03:51 +00001246 u32 ret = 0;
drh7ed91f22010-04-29 22:34:07 +00001247 if( pWal ){
1248 ret = pWal->iCallback;
1249 pWal->iCallback = 0;
dan8d22a172010-04-19 18:03:51 +00001250 }
1251 return (int)ret;
1252}