Index: third_party/sqlite/src/src/memjournal.c |
diff --git a/third_party/sqlite/src/src/memjournal.c b/third_party/sqlite/src/src/memjournal.c |
index 6452cecc379ff0d8fc47126ace27a34e552d252b..cd8b87d8aaecad6d7aad524298c497aee5045cd0 100644 |
--- a/third_party/sqlite/src/src/memjournal.c |
+++ b/third_party/sqlite/src/src/memjournal.c |
@@ -13,6 +13,15 @@ |
** This file contains code use to implement an in-memory rollback journal. |
** The in-memory rollback journal is used to journal transactions for |
** ":memory:" databases and when the journal_mode=MEMORY pragma is used. |
+** |
+** Update: The in-memory journal is also used to temporarily cache |
+** smaller journals that are not critical for power-loss recovery. |
+** For example, statement journals that are not too big will be held |
+** entirely in memory, thus reducing the number of file I/O calls, and |
+** more importantly, reducing temporary file creation events. If these |
+** journals become too large for memory, they are spilled to disk. But |
+** in the common case, they are usually small and no file I/O needs to |
+** occur. |
*/ |
#include "sqliteInt.h" |
@@ -21,25 +30,29 @@ typedef struct MemJournal MemJournal; |
typedef struct FilePoint FilePoint; |
typedef struct FileChunk FileChunk; |
-/* Space to hold the rollback journal is allocated in increments of |
-** this many bytes. |
-** |
-** The size chosen is a little less than a power of two. That way, |
-** the FileChunk object will have a size that almost exactly fills |
-** a power-of-two allocation. This minimizes wasted space in power-of-two |
-** memory allocators. |
-*/ |
-#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*))) |
- |
/* |
** The rollback journal is composed of a linked list of these structures. |
+** |
+** The zChunk array is always at least 8 bytes in size - usually much more. |
+** Its actual size is stored in the MemJournal.nChunkSize variable. |
*/ |
struct FileChunk { |
FileChunk *pNext; /* Next chunk in the journal */ |
- u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */ |
+ u8 zChunk[8]; /* Content of this chunk */ |
}; |
/* |
+** By default, allocate this many bytes of memory for each FileChunk object. |
+*/ |
+#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024 |
+ |
+/* |
+** For chunk size nChunkSize, return the number of bytes that should |
+** be allocated for each FileChunk structure. |
+*/ |
+#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8)) |
+ |
+/* |
** An instance of this object serves as a cursor into the rollback journal. |
** The cursor can be either for reading or writing. |
*/ |
@@ -49,14 +62,22 @@ struct FilePoint { |
}; |
/* |
-** This subclass is a subclass of sqlite3_file. Each open memory-journal |
+** This structure is a subclass of sqlite3_file. Each open memory-journal |
** is an instance of this class. |
*/ |
struct MemJournal { |
- sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ |
+ const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ |
+ int nChunkSize; /* In-memory chunk-size */ |
+ |
+ int nSpill; /* Bytes of data before flushing */ |
+ int nSize; /* Bytes of data currently in memory */ |
FileChunk *pFirst; /* Head of in-memory chunk-list */ |
FilePoint endpoint; /* Pointer to the end of the file */ |
FilePoint readpoint; /* Pointer to the end of the last xRead() */ |
+ |
+ int flags; /* xOpen flags */ |
+ sqlite3_vfs *pVfs; /* The "real" underlying VFS */ |
+ const char *zJournal; /* Name of the journal file */ |
}; |
/* |
@@ -75,37 +96,95 @@ static int memjrnlRead( |
int iChunkOffset; |
FileChunk *pChunk; |
- /* SQLite never tries to read past the end of a rollback journal file */ |
- assert( iOfst+iAmt<=p->endpoint.iOffset ); |
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE |
+ if( (iAmt+iOfst)>p->endpoint.iOffset ){ |
+ return SQLITE_IOERR_SHORT_READ; |
+ } |
+#endif |
+ assert( (iAmt+iOfst)<=p->endpoint.iOffset ); |
+ assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 ); |
if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ |
sqlite3_int64 iOff = 0; |
for(pChunk=p->pFirst; |
- ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst; |
+ ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst; |
pChunk=pChunk->pNext |
){ |
- iOff += JOURNAL_CHUNKSIZE; |
+ iOff += p->nChunkSize; |
} |
}else{ |
pChunk = p->readpoint.pChunk; |
+ assert( pChunk!=0 ); |
} |
- iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE); |
+ iChunkOffset = (int)(iOfst%p->nChunkSize); |
do { |
- int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset; |
- int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset)); |
- memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy); |
+ int iSpace = p->nChunkSize - iChunkOffset; |
+ int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset)); |
+ memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy); |
zOut += nCopy; |
nRead -= iSpace; |
iChunkOffset = 0; |
} while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); |
- p->readpoint.iOffset = iOfst+iAmt; |
+ p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0; |
p->readpoint.pChunk = pChunk; |
return SQLITE_OK; |
} |
/* |
+** Free the list of FileChunk structures headed at MemJournal.pFirst. |
+*/ |
+static void memjrnlFreeChunks(MemJournal *p){ |
+ FileChunk *pIter; |
+ FileChunk *pNext; |
+ for(pIter=p->pFirst; pIter; pIter=pNext){ |
+ pNext = pIter->pNext; |
+ sqlite3_free(pIter); |
+ } |
+ p->pFirst = 0; |
+} |
+ |
+/* |
+** Flush the contents of memory to a real file on disk. |
+*/ |
+static int memjrnlCreateFile(MemJournal *p){ |
+ int rc; |
+ sqlite3_file *pReal = (sqlite3_file*)p; |
+ MemJournal copy = *p; |
+ |
+ memset(p, 0, sizeof(MemJournal)); |
+ rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0); |
+ if( rc==SQLITE_OK ){ |
+ int nChunk = copy.nChunkSize; |
+ i64 iOff = 0; |
+ FileChunk *pIter; |
+ for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){ |
+ if( iOff + nChunk > copy.endpoint.iOffset ){ |
+ nChunk = copy.endpoint.iOffset - iOff; |
+ } |
+ rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff); |
+ if( rc ) break; |
+ iOff += nChunk; |
+ } |
+ if( rc==SQLITE_OK ){ |
+ /* No error has occurred. Free the in-memory buffers. */ |
+ memjrnlFreeChunks(©); |
+ } |
+ } |
+ if( rc!=SQLITE_OK ){ |
+ /* If an error occurred while creating or writing to the file, restore |
+ ** the original before returning. This way, SQLite uses the in-memory |
+ ** journal data to roll back changes made to the internal page-cache |
+ ** before this function was called. */ |
+ sqlite3OsClose(pReal); |
+ *p = copy; |
+ } |
+ return rc; |
+} |
+ |
+ |
+/* |
** Write data to the file. |
*/ |
static int memjrnlWrite( |
@@ -118,38 +197,62 @@ static int memjrnlWrite( |
int nWrite = iAmt; |
u8 *zWrite = (u8 *)zBuf; |
- /* An in-memory journal file should only ever be appended to. Random |
- ** access writes are not required by sqlite. |
- */ |
- assert( iOfst==p->endpoint.iOffset ); |
- UNUSED_PARAMETER(iOfst); |
- |
- while( nWrite>0 ){ |
- FileChunk *pChunk = p->endpoint.pChunk; |
- int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE); |
- int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset); |
- |
- if( iChunkOffset==0 ){ |
- /* New chunk is required to extend the file. */ |
- FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk)); |
- if( !pNew ){ |
- return SQLITE_IOERR_NOMEM; |
- } |
- pNew->pNext = 0; |
- if( pChunk ){ |
- assert( p->pFirst ); |
- pChunk->pNext = pNew; |
- }else{ |
- assert( !p->pFirst ); |
- p->pFirst = pNew; |
- } |
- p->endpoint.pChunk = pNew; |
+ /* If the file should be created now, create it and write the new data |
+ ** into the file on disk. */ |
+ if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){ |
+ int rc = memjrnlCreateFile(p); |
+ if( rc==SQLITE_OK ){ |
+ rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst); |
} |
+ return rc; |
+ } |
+ |
+ /* If the contents of this write should be stored in memory */ |
+ else{ |
+ /* An in-memory journal file should only ever be appended to. Random |
+ ** access writes are not required. The only exception to this is when |
+ ** the in-memory journal is being used by a connection using the |
+ ** atomic-write optimization. In this case the first 28 bytes of the |
+ ** journal file may be written as part of committing the transaction. */ |
+ assert( iOfst==p->endpoint.iOffset || iOfst==0 ); |
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE |
+ if( iOfst==0 && p->pFirst ){ |
+ assert( p->nChunkSize>iAmt ); |
+ memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); |
+ }else |
+#else |
+ assert( iOfst>0 || p->pFirst==0 ); |
+#endif |
+ { |
+ while( nWrite>0 ){ |
+ FileChunk *pChunk = p->endpoint.pChunk; |
+ int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); |
+ int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); |
+ |
+ if( iChunkOffset==0 ){ |
+ /* New chunk is required to extend the file. */ |
+ FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); |
+ if( !pNew ){ |
+ return SQLITE_IOERR_NOMEM_BKPT; |
+ } |
+ pNew->pNext = 0; |
+ if( pChunk ){ |
+ assert( p->pFirst ); |
+ pChunk->pNext = pNew; |
+ }else{ |
+ assert( !p->pFirst ); |
+ p->pFirst = pNew; |
+ } |
+ p->endpoint.pChunk = pNew; |
+ } |
- memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace); |
- zWrite += iSpace; |
- nWrite -= iSpace; |
- p->endpoint.iOffset += iSpace; |
+ memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace); |
+ zWrite += iSpace; |
+ nWrite -= iSpace; |
+ p->endpoint.iOffset += iSpace; |
+ } |
+ p->nSize = iAmt + iOfst; |
+ } |
} |
return SQLITE_OK; |
@@ -157,19 +260,21 @@ static int memjrnlWrite( |
/* |
** Truncate the file. |
+** |
+** If the journal file is already on disk, truncate it there. Or, if it |
+** is still in main memory but is being truncated to zero bytes in size, |
+** ignore |
*/ |
static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
MemJournal *p = (MemJournal *)pJfd; |
- FileChunk *pChunk; |
- assert(size==0); |
- UNUSED_PARAMETER(size); |
- pChunk = p->pFirst; |
- while( pChunk ){ |
- FileChunk *pTmp = pChunk; |
- pChunk = pChunk->pNext; |
- sqlite3_free(pTmp); |
+ if( ALWAYS(size==0) ){ |
+ memjrnlFreeChunks(p); |
+ p->nSize = 0; |
+ p->endpoint.pChunk = 0; |
+ p->endpoint.iOffset = 0; |
+ p->readpoint.pChunk = 0; |
+ p->readpoint.iOffset = 0; |
} |
- sqlite3MemJournalOpen(pJfd); |
return SQLITE_OK; |
} |
@@ -177,21 +282,19 @@ static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
** Close the file. |
*/ |
static int memjrnlClose(sqlite3_file *pJfd){ |
- memjrnlTruncate(pJfd, 0); |
+ MemJournal *p = (MemJournal *)pJfd; |
+ memjrnlFreeChunks(p); |
return SQLITE_OK; |
} |
- |
/* |
** Sync the file. |
** |
-** Syncing an in-memory journal is a no-op. And, in fact, this routine |
-** is never called in a working implementation. This implementation |
-** exists purely as a contingency, in case some malfunction in some other |
-** part of SQLite causes Sync to be called by mistake. |
+** If the real file has been created, call its xSync method. Otherwise, |
+** syncing an in-memory journal is a no-op. |
*/ |
-static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ |
- UNUSED_PARAMETER2(NotUsed, NotUsed2); |
+static int memjrnlSync(sqlite3_file *pJfd, int flags){ |
+ UNUSED_PARAMETER2(pJfd, flags); |
return SQLITE_OK; |
} |
@@ -230,26 +333,86 @@ static const struct sqlite3_io_methods MemJournalMethods = { |
}; |
/* |
-** Open a journal file. |
+** Open a journal file. |
+** |
+** The behaviour of the journal file depends on the value of parameter |
+** nSpill. If nSpill is 0, then the journal file is always create and |
+** accessed using the underlying VFS. If nSpill is less than zero, then |
+** all content is always stored in main-memory. Finally, if nSpill is a |
+** positive value, then the journal file is initially created in-memory |
+** but may be flushed to disk later on. In this case the journal file is |
+** flushed to disk either when it grows larger than nSpill bytes in size, |
+** or when sqlite3JournalCreate() is called. |
+*/ |
+int sqlite3JournalOpen( |
+ sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ |
+ const char *zName, /* Name of the journal file */ |
+ sqlite3_file *pJfd, /* Preallocated, blank file handle */ |
+ int flags, /* Opening flags */ |
+ int nSpill /* Bytes buffered before opening the file */ |
+){ |
+ MemJournal *p = (MemJournal*)pJfd; |
+ |
+ /* Zero the file-handle object. If nSpill was passed zero, initialize |
+ ** it using the sqlite3OsOpen() function of the underlying VFS. In this |
+ ** case none of the code in this module is executed as a result of calls |
+ ** made on the journal file-handle. */ |
+ memset(p, 0, sizeof(MemJournal)); |
+ if( nSpill==0 ){ |
+ return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); |
+ } |
+ |
+ if( nSpill>0 ){ |
+ p->nChunkSize = nSpill; |
+ }else{ |
+ p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk); |
+ assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) ); |
+ } |
+ |
+ p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods; |
+ p->nSpill = nSpill; |
+ p->flags = flags; |
+ p->zJournal = zName; |
+ p->pVfs = pVfs; |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Open an in-memory journal file. |
*/ |
void sqlite3MemJournalOpen(sqlite3_file *pJfd){ |
- MemJournal *p = (MemJournal *)pJfd; |
- assert( EIGHT_BYTE_ALIGNMENT(p) ); |
- memset(p, 0, sqlite3MemJournalSize()); |
- p->pMethod = (sqlite3_io_methods*)&MemJournalMethods; |
+ sqlite3JournalOpen(0, 0, pJfd, 0, -1); |
+} |
+ |
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE |
+/* |
+** If the argument p points to a MemJournal structure that is not an |
+** in-memory-only journal file (i.e. is one that was opened with a +ve |
+** nSpill parameter), and the underlying file has not yet been created, |
+** create it now. |
+*/ |
+int sqlite3JournalCreate(sqlite3_file *p){ |
+ int rc = SQLITE_OK; |
+ if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){ |
+ rc = memjrnlCreateFile((MemJournal*)p); |
+ } |
+ return rc; |
} |
+#endif |
/* |
-** Return true if the file-handle passed as an argument is |
-** an in-memory journal |
+** The file-handle passed as the only argument is open on a journal file. |
+** Return true if this "journal file" is currently stored in heap memory, |
+** or false otherwise. |
*/ |
-int sqlite3IsMemJournal(sqlite3_file *pJfd){ |
- return pJfd->pMethods==&MemJournalMethods; |
+int sqlite3JournalIsInMemory(sqlite3_file *p){ |
+ return p->pMethods==&MemJournalMethods; |
} |
/* |
-** Return the number of bytes required to store a MemJournal file descriptor. |
+** Return the number of bytes required to store a JournalFile that uses vfs |
+** pVfs to create the underlying on-disk files. |
*/ |
-int sqlite3MemJournalSize(void){ |
- return sizeof(MemJournal); |
+int sqlite3JournalSize(sqlite3_vfs *pVfs){ |
+ return MAX(pVfs->szOsFile, (int)sizeof(MemJournal)); |
} |