blob: 6452cecc379ff0d8fc47126ace27a34e552d252b [file] [log] [blame]
danielk197739281b42008-10-17 19:13:04 +00001/*
drh27c3bd72008-10-28 18:12:36 +00002** 2008 October 7
danielk197739281b42008-10-17 19:13:04 +00003**
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**
drh27c3bd72008-10-28 18:12:36 +000013** 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.
danielk197739281b42008-10-17 19:13:04 +000016*/
danielk197739281b42008-10-17 19:13:04 +000017#include "sqliteInt.h"
18
drh27c3bd72008-10-28 18:12:36 +000019/* Forward references to internal structures */
danielk197739281b42008-10-17 19:13:04 +000020typedef struct MemJournal MemJournal;
21typedef struct FilePoint FilePoint;
22typedef struct FileChunk FileChunk;
23
drh27c3bd72008-10-28 18:12:36 +000024/* Space to hold the rollback journal is allocated in increments of
25** this many bytes.
drh2206a2b2009-04-01 23:09:43 +000026**
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
peter.d.reid60ec9142014-09-06 16:39:46 +000029** a power-of-two allocation. This minimizes wasted space in power-of-two
drh2206a2b2009-04-01 23:09:43 +000030** memory allocators.
drh27c3bd72008-10-28 18:12:36 +000031*/
drhde467982009-04-02 17:22:41 +000032#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
danielk197739281b42008-10-17 19:13:04 +000033
drh27c3bd72008-10-28 18:12:36 +000034/*
35** The rollback journal is composed of a linked list of these structures.
36*/
danielk197739281b42008-10-17 19:13:04 +000037struct FileChunk {
drh27c3bd72008-10-28 18:12:36 +000038 FileChunk *pNext; /* Next chunk in the journal */
39 u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */
danielk197739281b42008-10-17 19:13:04 +000040};
41
drh27c3bd72008-10-28 18:12:36 +000042/*
43** An instance of this object serves as a cursor into the rollback journal.
44** The cursor can be either for reading or writing.
45*/
danielk197739281b42008-10-17 19:13:04 +000046struct FilePoint {
drh27c3bd72008-10-28 18:12:36 +000047 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
48 FileChunk *pChunk; /* Specific chunk into which cursor points */
danielk197739281b42008-10-17 19:13:04 +000049};
50
drh27c3bd72008-10-28 18:12:36 +000051/*
52** This subclass is a subclass of sqlite3_file. Each open memory-journal
53** is an instance of this class.
54*/
danielk197739281b42008-10-17 19:13:04 +000055struct MemJournal {
drh27c3bd72008-10-28 18:12:36 +000056 sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
danielk197739281b42008-10-17 19:13:04 +000057 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() */
60};
61
62/*
drh2206a2b2009-04-01 23:09:43 +000063** Read data from the in-memory journal file. This is the implementation
64** of the sqlite3_vfs.xRead method.
danielk197739281b42008-10-17 19:13:04 +000065*/
66static 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 */
71){
72 MemJournal *p = (MemJournal *)pJfd;
73 u8 *zOut = zBuf;
74 int nRead = iAmt;
75 int iChunkOffset;
76 FileChunk *pChunk;
77
drh2206a2b2009-04-01 23:09:43 +000078 /* SQLite never tries to read past the end of a rollback journal file */
danielk197739281b42008-10-17 19:13:04 +000079 assert( iOfst+iAmt<=p->endpoint.iOffset );
80
81 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
82 sqlite3_int64 iOff = 0;
83 for(pChunk=p->pFirst;
drh2206a2b2009-04-01 23:09:43 +000084 ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
danielk197739281b42008-10-17 19:13:04 +000085 pChunk=pChunk->pNext
86 ){
87 iOff += JOURNAL_CHUNKSIZE;
88 }
89 }else{
90 pChunk = p->readpoint.pChunk;
91 }
92
drh1bd10f82008-12-10 21:19:56 +000093 iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
danielk197739281b42008-10-17 19:13:04 +000094 do {
95 int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
96 int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
97 memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
98 zOut += nCopy;
99 nRead -= iSpace;
100 iChunkOffset = 0;
drhea678832008-12-10 19:26:22 +0000101 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
danielk197739281b42008-10-17 19:13:04 +0000102 p->readpoint.iOffset = iOfst+iAmt;
103 p->readpoint.pChunk = pChunk;
104
105 return SQLITE_OK;
106}
107
108/*
109** Write data to the file.
110*/
111static 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 */
116){
117 MemJournal *p = (MemJournal *)pJfd;
118 int nWrite = iAmt;
119 u8 *zWrite = (u8 *)zBuf;
120
121 /* An in-memory journal file should only ever be appended to. Random
122 ** access writes are not required by sqlite.
123 */
danielk1977de630352009-05-04 11:42:29 +0000124 assert( iOfst==p->endpoint.iOffset );
danielk1977f3d3c272008-11-19 16:52:44 +0000125 UNUSED_PARAMETER(iOfst);
danielk197739281b42008-10-17 19:13:04 +0000126
127 while( nWrite>0 ){
128 FileChunk *pChunk = p->endpoint.pChunk;
drh1bd10f82008-12-10 21:19:56 +0000129 int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
danielk197739281b42008-10-17 19:13:04 +0000130 int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
131
132 if( iChunkOffset==0 ){
133 /* New chunk is required to extend the file. */
134 FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
135 if( !pNew ){
136 return SQLITE_IOERR_NOMEM;
137 }
138 pNew->pNext = 0;
139 if( pChunk ){
140 assert( p->pFirst );
141 pChunk->pNext = pNew;
142 }else{
143 assert( !p->pFirst );
144 p->pFirst = pNew;
145 }
146 p->endpoint.pChunk = pNew;
147 }
148
149 memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
150 zWrite += iSpace;
151 nWrite -= iSpace;
152 p->endpoint.iOffset += iSpace;
153 }
154
155 return SQLITE_OK;
156}
157
158/*
159** Truncate the file.
160*/
161static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
162 MemJournal *p = (MemJournal *)pJfd;
163 FileChunk *pChunk;
164 assert(size==0);
danielk1977f3d3c272008-11-19 16:52:44 +0000165 UNUSED_PARAMETER(size);
danielk197739281b42008-10-17 19:13:04 +0000166 pChunk = p->pFirst;
167 while( pChunk ){
168 FileChunk *pTmp = pChunk;
169 pChunk = pChunk->pNext;
170 sqlite3_free(pTmp);
171 }
172 sqlite3MemJournalOpen(pJfd);
173 return SQLITE_OK;
174}
175
176/*
177** Close the file.
178*/
179static int memjrnlClose(sqlite3_file *pJfd){
180 memjrnlTruncate(pJfd, 0);
181 return SQLITE_OK;
182}
183
184
185/*
186** Sync the file.
drh2206a2b2009-04-01 23:09:43 +0000187**
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.
danielk197739281b42008-10-17 19:13:04 +0000192*/
drh09c0f6d2010-04-12 19:44:22 +0000193static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
194 UNUSED_PARAMETER2(NotUsed, NotUsed2);
195 return SQLITE_OK;
196}
danielk197739281b42008-10-17 19:13:04 +0000197
198/*
199** Query the size of the file in bytes.
200*/
201static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
202 MemJournal *p = (MemJournal *)pJfd;
203 *pSize = (sqlite_int64) p->endpoint.iOffset;
204 return SQLITE_OK;
205}
206
207/*
208** Table of methods for MemJournal sqlite3_file object.
209*/
drhf83dc1e2010-06-03 12:09:52 +0000210static const struct sqlite3_io_methods MemJournalMethods = {
danielk197739281b42008-10-17 19:13:04 +0000211 1, /* iVersion */
212 memjrnlClose, /* xClose */
213 memjrnlRead, /* xRead */
214 memjrnlWrite, /* xWrite */
215 memjrnlTruncate, /* xTruncate */
216 memjrnlSync, /* xSync */
217 memjrnlFileSize, /* xFileSize */
218 0, /* xLock */
219 0, /* xUnlock */
220 0, /* xCheckReservedLock */
221 0, /* xFileControl */
222 0, /* xSectorSize */
drhff828942010-06-26 21:34:06 +0000223 0, /* xDeviceCharacteristics */
drhff828942010-06-26 21:34:06 +0000224 0, /* xShmMap */
drh6e1f4822010-07-13 23:41:40 +0000225 0, /* xShmLock */
drhff828942010-06-26 21:34:06 +0000226 0, /* xShmBarrier */
drhda8caa02013-04-22 23:38:50 +0000227 0, /* xShmUnmap */
228 0, /* xFetch */
229 0 /* xUnfetch */
danielk197739281b42008-10-17 19:13:04 +0000230};
231
232/*
233** Open a journal file.
234*/
235void sqlite3MemJournalOpen(sqlite3_file *pJfd){
236 MemJournal *p = (MemJournal *)pJfd;
drhea598cb2009-04-05 12:22:08 +0000237 assert( EIGHT_BYTE_ALIGNMENT(p) );
danielk197739281b42008-10-17 19:13:04 +0000238 memset(p, 0, sqlite3MemJournalSize());
drhf83dc1e2010-06-03 12:09:52 +0000239 p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
danielk197739281b42008-10-17 19:13:04 +0000240}
241
242/*
243** Return true if the file-handle passed as an argument is
244** an in-memory journal
245*/
246int sqlite3IsMemJournal(sqlite3_file *pJfd){
247 return pJfd->pMethods==&MemJournalMethods;
248}
249
250/*
drh2fcc7bd2010-09-16 23:18:57 +0000251** Return the number of bytes required to store a MemJournal file descriptor.
danielk197739281b42008-10-17 19:13:04 +0000252*/
drh3a5990a2008-12-20 02:14:39 +0000253int sqlite3MemJournalSize(void){
danielk197739281b42008-10-17 19:13:04 +0000254 return sizeof(MemJournal);
255}