Snapshot of upstream SQLite 3.37.2
[sqlcipher.git] / src / memjournal.c
blob63ef2ad7b94893b96ad649933ed5d60cdda48e1c
1 /*
2 ** 2008 October 7
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.
11 *************************************************************************
13 ** This file contains code use to implement an in-memory rollback journal.
14 ** The in-memory rollback journal is used to journal transactions for
15 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
17 ** Update: The in-memory journal is also used to temporarily cache
18 ** smaller journals that are not critical for power-loss recovery.
19 ** For example, statement journals that are not too big will be held
20 ** entirely in memory, thus reducing the number of file I/O calls, and
21 ** more importantly, reducing temporary file creation events. If these
22 ** journals become too large for memory, they are spilled to disk. But
23 ** in the common case, they are usually small and no file I/O needs to
24 ** occur.
26 #include "sqliteInt.h"
28 /* Forward references to internal structures */
29 typedef struct MemJournal MemJournal;
30 typedef struct FilePoint FilePoint;
31 typedef struct FileChunk FileChunk;
34 ** The rollback journal is composed of a linked list of these structures.
36 ** The zChunk array is always at least 8 bytes in size - usually much more.
37 ** Its actual size is stored in the MemJournal.nChunkSize variable.
39 struct FileChunk {
40 FileChunk *pNext; /* Next chunk in the journal */
41 u8 zChunk[8]; /* Content of this chunk */
45 ** By default, allocate this many bytes of memory for each FileChunk object.
47 #define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
50 ** For chunk size nChunkSize, return the number of bytes that should
51 ** be allocated for each FileChunk structure.
53 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
56 ** An instance of this object serves as a cursor into the rollback journal.
57 ** The cursor can be either for reading or writing.
59 struct FilePoint {
60 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
61 FileChunk *pChunk; /* Specific chunk into which cursor points */
65 ** This structure is a subclass of sqlite3_file. Each open memory-journal
66 ** is an instance of this class.
68 struct MemJournal {
69 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
70 int nChunkSize; /* In-memory chunk-size */
72 int nSpill; /* Bytes of data before flushing */
73 FileChunk *pFirst; /* Head of in-memory chunk-list */
74 FilePoint endpoint; /* Pointer to the end of the file */
75 FilePoint readpoint; /* Pointer to the end of the last xRead() */
77 int flags; /* xOpen flags */
78 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
79 const char *zJournal; /* Name of the journal file */
83 ** Read data from the in-memory journal file. This is the implementation
84 ** of the sqlite3_vfs.xRead method.
86 static int memjrnlRead(
87 sqlite3_file *pJfd, /* The journal file from which to read */
88 void *zBuf, /* Put the results here */
89 int iAmt, /* Number of bytes to read */
90 sqlite_int64 iOfst /* Begin reading at this offset */
92 MemJournal *p = (MemJournal *)pJfd;
93 u8 *zOut = zBuf;
94 int nRead = iAmt;
95 int iChunkOffset;
96 FileChunk *pChunk;
98 if( (iAmt+iOfst)>p->endpoint.iOffset ){
99 return SQLITE_IOERR_SHORT_READ;
101 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
102 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
103 sqlite3_int64 iOff = 0;
104 for(pChunk=p->pFirst;
105 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
106 pChunk=pChunk->pNext
108 iOff += p->nChunkSize;
110 }else{
111 pChunk = p->readpoint.pChunk;
112 assert( pChunk!=0 );
115 iChunkOffset = (int)(iOfst%p->nChunkSize);
116 do {
117 int iSpace = p->nChunkSize - iChunkOffset;
118 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
119 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
120 zOut += nCopy;
121 nRead -= iSpace;
122 iChunkOffset = 0;
123 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
124 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
125 p->readpoint.pChunk = pChunk;
127 return SQLITE_OK;
131 ** Free the list of FileChunk structures headed at MemJournal.pFirst.
133 static void memjrnlFreeChunks(FileChunk *pFirst){
134 FileChunk *pIter;
135 FileChunk *pNext;
136 for(pIter=pFirst; pIter; pIter=pNext){
137 pNext = pIter->pNext;
138 sqlite3_free(pIter);
143 ** Flush the contents of memory to a real file on disk.
145 static int memjrnlCreateFile(MemJournal *p){
146 int rc;
147 sqlite3_file *pReal = (sqlite3_file*)p;
148 MemJournal copy = *p;
150 memset(p, 0, sizeof(MemJournal));
151 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
152 if( rc==SQLITE_OK ){
153 int nChunk = copy.nChunkSize;
154 i64 iOff = 0;
155 FileChunk *pIter;
156 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
157 if( iOff + nChunk > copy.endpoint.iOffset ){
158 nChunk = copy.endpoint.iOffset - iOff;
160 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
161 if( rc ) break;
162 iOff += nChunk;
164 if( rc==SQLITE_OK ){
165 /* No error has occurred. Free the in-memory buffers. */
166 memjrnlFreeChunks(copy.pFirst);
169 if( rc!=SQLITE_OK ){
170 /* If an error occurred while creating or writing to the file, restore
171 ** the original before returning. This way, SQLite uses the in-memory
172 ** journal data to roll back changes made to the internal page-cache
173 ** before this function was called. */
174 sqlite3OsClose(pReal);
175 *p = copy;
177 return rc;
182 ** Write data to the file.
184 static int memjrnlWrite(
185 sqlite3_file *pJfd, /* The journal file into which to write */
186 const void *zBuf, /* Take data to be written from here */
187 int iAmt, /* Number of bytes to write */
188 sqlite_int64 iOfst /* Begin writing at this offset into the file */
190 MemJournal *p = (MemJournal *)pJfd;
191 int nWrite = iAmt;
192 u8 *zWrite = (u8 *)zBuf;
194 /* If the file should be created now, create it and write the new data
195 ** into the file on disk. */
196 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
197 int rc = memjrnlCreateFile(p);
198 if( rc==SQLITE_OK ){
199 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
201 return rc;
204 /* If the contents of this write should be stored in memory */
205 else{
206 /* An in-memory journal file should only ever be appended to. Random
207 ** access writes are not required. The only exception to this is when
208 ** the in-memory journal is being used by a connection using the
209 ** atomic-write optimization. In this case the first 28 bytes of the
210 ** journal file may be written as part of committing the transaction. */
211 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
212 #if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
213 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
214 if( iOfst==0 && p->pFirst ){
215 assert( p->nChunkSize>iAmt );
216 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
217 }else
218 #else
219 assert( iOfst>0 || p->pFirst==0 );
220 #endif
222 while( nWrite>0 ){
223 FileChunk *pChunk = p->endpoint.pChunk;
224 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
225 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
227 if( iChunkOffset==0 ){
228 /* New chunk is required to extend the file. */
229 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
230 if( !pNew ){
231 return SQLITE_IOERR_NOMEM_BKPT;
233 pNew->pNext = 0;
234 if( pChunk ){
235 assert( p->pFirst );
236 pChunk->pNext = pNew;
237 }else{
238 assert( !p->pFirst );
239 p->pFirst = pNew;
241 p->endpoint.pChunk = pNew;
244 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
245 zWrite += iSpace;
246 nWrite -= iSpace;
247 p->endpoint.iOffset += iSpace;
252 return SQLITE_OK;
256 ** Truncate the in-memory file.
258 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
259 MemJournal *p = (MemJournal *)pJfd;
260 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
261 if( size<p->endpoint.iOffset ){
262 FileChunk *pIter = 0;
263 if( size==0 ){
264 memjrnlFreeChunks(p->pFirst);
265 p->pFirst = 0;
266 }else{
267 i64 iOff = p->nChunkSize;
268 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
269 iOff += p->nChunkSize;
271 if( ALWAYS(pIter) ){
272 memjrnlFreeChunks(pIter->pNext);
273 pIter->pNext = 0;
277 p->endpoint.pChunk = pIter;
278 p->endpoint.iOffset = size;
279 p->readpoint.pChunk = 0;
280 p->readpoint.iOffset = 0;
282 return SQLITE_OK;
286 ** Close the file.
288 static int memjrnlClose(sqlite3_file *pJfd){
289 MemJournal *p = (MemJournal *)pJfd;
290 memjrnlFreeChunks(p->pFirst);
291 return SQLITE_OK;
295 ** Sync the file.
297 ** If the real file has been created, call its xSync method. Otherwise,
298 ** syncing an in-memory journal is a no-op.
300 static int memjrnlSync(sqlite3_file *pJfd, int flags){
301 UNUSED_PARAMETER2(pJfd, flags);
302 return SQLITE_OK;
306 ** Query the size of the file in bytes.
308 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
309 MemJournal *p = (MemJournal *)pJfd;
310 *pSize = (sqlite_int64) p->endpoint.iOffset;
311 return SQLITE_OK;
315 ** Table of methods for MemJournal sqlite3_file object.
317 static const struct sqlite3_io_methods MemJournalMethods = {
318 1, /* iVersion */
319 memjrnlClose, /* xClose */
320 memjrnlRead, /* xRead */
321 memjrnlWrite, /* xWrite */
322 memjrnlTruncate, /* xTruncate */
323 memjrnlSync, /* xSync */
324 memjrnlFileSize, /* xFileSize */
325 0, /* xLock */
326 0, /* xUnlock */
327 0, /* xCheckReservedLock */
328 0, /* xFileControl */
329 0, /* xSectorSize */
330 0, /* xDeviceCharacteristics */
331 0, /* xShmMap */
332 0, /* xShmLock */
333 0, /* xShmBarrier */
334 0, /* xShmUnmap */
335 0, /* xFetch */
336 0 /* xUnfetch */
340 ** Open a journal file.
342 ** The behaviour of the journal file depends on the value of parameter
343 ** nSpill. If nSpill is 0, then the journal file is always create and
344 ** accessed using the underlying VFS. If nSpill is less than zero, then
345 ** all content is always stored in main-memory. Finally, if nSpill is a
346 ** positive value, then the journal file is initially created in-memory
347 ** but may be flushed to disk later on. In this case the journal file is
348 ** flushed to disk either when it grows larger than nSpill bytes in size,
349 ** or when sqlite3JournalCreate() is called.
351 int sqlite3JournalOpen(
352 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
353 const char *zName, /* Name of the journal file */
354 sqlite3_file *pJfd, /* Preallocated, blank file handle */
355 int flags, /* Opening flags */
356 int nSpill /* Bytes buffered before opening the file */
358 MemJournal *p = (MemJournal*)pJfd;
360 /* Zero the file-handle object. If nSpill was passed zero, initialize
361 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
362 ** case none of the code in this module is executed as a result of calls
363 ** made on the journal file-handle. */
364 memset(p, 0, sizeof(MemJournal));
365 if( nSpill==0 ){
366 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
369 if( nSpill>0 ){
370 p->nChunkSize = nSpill;
371 }else{
372 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
373 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
376 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
377 p->nSpill = nSpill;
378 p->flags = flags;
379 p->zJournal = zName;
380 p->pVfs = pVfs;
381 return SQLITE_OK;
385 ** Open an in-memory journal file.
387 void sqlite3MemJournalOpen(sqlite3_file *pJfd){
388 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
391 #if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
392 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
394 ** If the argument p points to a MemJournal structure that is not an
395 ** in-memory-only journal file (i.e. is one that was opened with a +ve
396 ** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
397 ** file has not yet been created, create it now.
399 int sqlite3JournalCreate(sqlite3_file *pJfd){
400 int rc = SQLITE_OK;
401 MemJournal *p = (MemJournal*)pJfd;
402 if( pJfd->pMethods==&MemJournalMethods && (
403 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
404 p->nSpill>0
405 #else
406 /* While this appears to not be possible without ATOMIC_WRITE, the
407 ** paths are complex, so it seems prudent to leave the test in as
408 ** a NEVER(), in case our analysis is subtly flawed. */
409 NEVER(p->nSpill>0)
410 #endif
411 #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
412 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
413 #endif
415 rc = memjrnlCreateFile(p);
417 return rc;
419 #endif
422 ** The file-handle passed as the only argument is open on a journal file.
423 ** Return true if this "journal file" is currently stored in heap memory,
424 ** or false otherwise.
426 int sqlite3JournalIsInMemory(sqlite3_file *p){
427 return p->pMethods==&MemJournalMethods;
431 ** Return the number of bytes required to store a JournalFile that uses vfs
432 ** pVfs to create the underlying on-disk files.
434 int sqlite3JournalSize(sqlite3_vfs *pVfs){
435 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));