4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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 #include "sqliteInt.h"
19 /* Forward references to internal structures */
20 typedef struct MemJournal MemJournal
;
21 typedef struct FilePoint FilePoint
;
22 typedef struct FileChunk FileChunk
;
24 /* Space to hold the rollback journal is allocated in increments of
27 ** The size chosen is a little less than a power of two. That way,
28 ** the FileChunk object will have a size that almost exactly fills
29 ** a power-of-two allocation. This minimizes wasted space in power-of-two
32 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
35 ** The rollback journal is composed of a linked list of these structures.
38 FileChunk
*pNext
; /* Next chunk in the journal */
39 u8 zChunk
[JOURNAL_CHUNKSIZE
]; /* Content of this chunk */
43 ** An instance of this object serves as a cursor into the rollback journal.
44 ** The cursor can be either for reading or writing.
47 sqlite3_int64 iOffset
; /* Offset from the beginning of the file */
48 FileChunk
*pChunk
; /* Specific chunk into which cursor points */
52 ** This subclass is a subclass of sqlite3_file. Each open memory-journal
53 ** is an instance of this class.
56 sqlite3_io_methods
*pMethod
; /* Parent class. MUST BE FIRST */
57 FileChunk
*pFirst
; /* Head of in-memory chunk-list */
58 FilePoint endpoint
; /* Pointer to the end of the file */
59 FilePoint readpoint
; /* Pointer to the end of the last xRead() */
63 ** Read data from the in-memory journal file. This is the implementation
64 ** of the sqlite3_vfs.xRead method.
66 static int memjrnlRead(
67 sqlite3_file
*pJfd
, /* The journal file from which to read */
68 void *zBuf
, /* Put the results here */
69 int iAmt
, /* Number of bytes to read */
70 sqlite_int64 iOfst
/* Begin reading at this offset */
72 MemJournal
*p
= (MemJournal
*)pJfd
;
78 /* SQLite never tries to read past the end of a rollback journal file */
79 assert( iOfst
+iAmt
<=p
->endpoint
.iOffset
);
81 if( p
->readpoint
.iOffset
!=iOfst
|| iOfst
==0 ){
82 sqlite3_int64 iOff
= 0;
84 ALWAYS(pChunk
) && (iOff
+JOURNAL_CHUNKSIZE
)<=iOfst
;
87 iOff
+= JOURNAL_CHUNKSIZE
;
90 pChunk
= p
->readpoint
.pChunk
;
93 iChunkOffset
= (int)(iOfst
%JOURNAL_CHUNKSIZE
);
95 int iSpace
= JOURNAL_CHUNKSIZE
- iChunkOffset
;
96 int nCopy
= MIN(nRead
, (JOURNAL_CHUNKSIZE
- iChunkOffset
));
97 memcpy(zOut
, &pChunk
->zChunk
[iChunkOffset
], nCopy
);
101 } while( nRead
>=0 && (pChunk
=pChunk
->pNext
)!=0 && nRead
>0 );
102 p
->readpoint
.iOffset
= iOfst
+iAmt
;
103 p
->readpoint
.pChunk
= pChunk
;
109 ** Write data to the file.
111 static int memjrnlWrite(
112 sqlite3_file
*pJfd
, /* The journal file into which to write */
113 const void *zBuf
, /* Take data to be written from here */
114 int iAmt
, /* Number of bytes to write */
115 sqlite_int64 iOfst
/* Begin writing at this offset into the file */
117 MemJournal
*p
= (MemJournal
*)pJfd
;
119 u8
*zWrite
= (u8
*)zBuf
;
121 /* An in-memory journal file should only ever be appended to. Random
122 ** access writes are not required by sqlite.
124 assert( iOfst
==p
->endpoint
.iOffset
);
125 UNUSED_PARAMETER(iOfst
);
128 FileChunk
*pChunk
= p
->endpoint
.pChunk
;
129 int iChunkOffset
= (int)(p
->endpoint
.iOffset
%JOURNAL_CHUNKSIZE
);
130 int iSpace
= MIN(nWrite
, JOURNAL_CHUNKSIZE
- iChunkOffset
);
132 if( iChunkOffset
==0 ){
133 /* New chunk is required to extend the file. */
134 FileChunk
*pNew
= sqlite3_malloc(sizeof(FileChunk
));
136 return SQLITE_IOERR_NOMEM
;
141 pChunk
->pNext
= pNew
;
143 assert( !p
->pFirst
);
146 p
->endpoint
.pChunk
= pNew
;
149 memcpy(&p
->endpoint
.pChunk
->zChunk
[iChunkOffset
], zWrite
, iSpace
);
152 p
->endpoint
.iOffset
+= iSpace
;
159 ** Truncate the file.
161 static int memjrnlTruncate(sqlite3_file
*pJfd
, sqlite_int64 size
){
162 MemJournal
*p
= (MemJournal
*)pJfd
;
165 UNUSED_PARAMETER(size
);
168 FileChunk
*pTmp
= pChunk
;
169 pChunk
= pChunk
->pNext
;
172 sqlite3MemJournalOpen(pJfd
);
179 static int memjrnlClose(sqlite3_file
*pJfd
){
180 memjrnlTruncate(pJfd
, 0);
188 ** Syncing an in-memory journal is a no-op. And, in fact, this routine
189 ** is never called in a working implementation. This implementation
190 ** exists purely as a contingency, in case some malfunction in some other
191 ** part of SQLite causes Sync to be called by mistake.
193 static int memjrnlSync(sqlite3_file
*NotUsed
, int NotUsed2
){
194 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
199 ** Query the size of the file in bytes.
201 static int memjrnlFileSize(sqlite3_file
*pJfd
, sqlite_int64
*pSize
){
202 MemJournal
*p
= (MemJournal
*)pJfd
;
203 *pSize
= (sqlite_int64
) p
->endpoint
.iOffset
;
208 ** Table of methods for MemJournal sqlite3_file object.
210 static const struct sqlite3_io_methods MemJournalMethods
= {
212 memjrnlClose
, /* xClose */
213 memjrnlRead
, /* xRead */
214 memjrnlWrite
, /* xWrite */
215 memjrnlTruncate
, /* xTruncate */
216 memjrnlSync
, /* xSync */
217 memjrnlFileSize
, /* xFileSize */
220 0, /* xCheckReservedLock */
221 0, /* xFileControl */
223 0, /* xDeviceCharacteristics */
233 ** Open a journal file.
235 void sqlite3MemJournalOpen(sqlite3_file
*pJfd
){
236 MemJournal
*p
= (MemJournal
*)pJfd
;
237 assert( EIGHT_BYTE_ALIGNMENT(p
) );
238 memset(p
, 0, sqlite3MemJournalSize());
239 p
->pMethod
= (sqlite3_io_methods
*)&MemJournalMethods
;
243 ** Return true if the file-handle passed as an argument is
244 ** an in-memory journal
246 int sqlite3IsMemJournal(sqlite3_file
*pJfd
){
247 return pJfd
->pMethods
==&MemJournalMethods
;
251 ** Return the number of bytes required to store a MemJournal file descriptor.
253 int sqlite3MemJournalSize(void){
254 return sizeof(MemJournal
);