danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 1 | /* |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 2 | ** 2008 October 7 |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 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 | ** |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 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. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 16 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 17 | #include "sqliteInt.h" |
| 18 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 19 | /* Forward references to internal structures */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 20 | typedef struct MemJournal MemJournal; |
| 21 | typedef struct FilePoint FilePoint; |
| 22 | typedef struct FileChunk FileChunk; |
| 23 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 24 | /* Space to hold the rollback journal is allocated in increments of |
| 25 | ** this many bytes. |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 26 | ** |
| 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 mimimizes wasted space in power-of-two |
| 30 | ** memory allocators. |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 31 | */ |
drh | de46798 | 2009-04-02 17:22:41 +0000 | [diff] [blame] | 32 | #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*))) |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 33 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 34 | /* Macro to find the minimum of two numeric values. |
| 35 | */ |
drh | 7ab49bf | 2008-11-12 15:24:27 +0000 | [diff] [blame] | 36 | #ifndef MIN |
| 37 | # define MIN(x,y) ((x)<(y)?(x):(y)) |
| 38 | #endif |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 39 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 40 | /* |
| 41 | ** The rollback journal is composed of a linked list of these structures. |
| 42 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 43 | struct FileChunk { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 44 | FileChunk *pNext; /* Next chunk in the journal */ |
| 45 | u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 48 | /* |
| 49 | ** An instance of this object serves as a cursor into the rollback journal. |
| 50 | ** The cursor can be either for reading or writing. |
| 51 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 52 | struct FilePoint { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 53 | sqlite3_int64 iOffset; /* Offset from the beginning of the file */ |
| 54 | FileChunk *pChunk; /* Specific chunk into which cursor points */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 57 | /* |
| 58 | ** This subclass is a subclass of sqlite3_file. Each open memory-journal |
| 59 | ** is an instance of this class. |
| 60 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 61 | struct MemJournal { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 62 | sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 63 | FileChunk *pFirst; /* Head of in-memory chunk-list */ |
| 64 | FilePoint endpoint; /* Pointer to the end of the file */ |
| 65 | FilePoint readpoint; /* Pointer to the end of the last xRead() */ |
| 66 | }; |
| 67 | |
| 68 | /* |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 69 | ** Read data from the in-memory journal file. This is the implementation |
| 70 | ** of the sqlite3_vfs.xRead method. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 71 | */ |
| 72 | static int memjrnlRead( |
| 73 | sqlite3_file *pJfd, /* The journal file from which to read */ |
| 74 | void *zBuf, /* Put the results here */ |
| 75 | int iAmt, /* Number of bytes to read */ |
| 76 | sqlite_int64 iOfst /* Begin reading at this offset */ |
| 77 | ){ |
| 78 | MemJournal *p = (MemJournal *)pJfd; |
| 79 | u8 *zOut = zBuf; |
| 80 | int nRead = iAmt; |
| 81 | int iChunkOffset; |
| 82 | FileChunk *pChunk; |
| 83 | |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 84 | /* SQLite never tries to read past the end of a rollback journal file */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 85 | assert( iOfst+iAmt<=p->endpoint.iOffset ); |
| 86 | |
| 87 | if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ |
| 88 | sqlite3_int64 iOff = 0; |
| 89 | for(pChunk=p->pFirst; |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 90 | ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 91 | pChunk=pChunk->pNext |
| 92 | ){ |
| 93 | iOff += JOURNAL_CHUNKSIZE; |
| 94 | } |
| 95 | }else{ |
| 96 | pChunk = p->readpoint.pChunk; |
| 97 | } |
| 98 | |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 99 | iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 100 | do { |
| 101 | int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset; |
| 102 | int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset)); |
| 103 | memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy); |
| 104 | zOut += nCopy; |
| 105 | nRead -= iSpace; |
| 106 | iChunkOffset = 0; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 107 | } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 108 | p->readpoint.iOffset = iOfst+iAmt; |
| 109 | p->readpoint.pChunk = pChunk; |
| 110 | |
| 111 | return SQLITE_OK; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | ** Write data to the file. |
| 116 | */ |
| 117 | static int memjrnlWrite( |
| 118 | sqlite3_file *pJfd, /* The journal file into which to write */ |
| 119 | const void *zBuf, /* Take data to be written from here */ |
| 120 | int iAmt, /* Number of bytes to write */ |
| 121 | sqlite_int64 iOfst /* Begin writing at this offset into the file */ |
| 122 | ){ |
| 123 | MemJournal *p = (MemJournal *)pJfd; |
| 124 | int nWrite = iAmt; |
| 125 | u8 *zWrite = (u8 *)zBuf; |
| 126 | |
| 127 | /* An in-memory journal file should only ever be appended to. Random |
| 128 | ** access writes are not required by sqlite. |
| 129 | */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 130 | assert( iOfst==p->endpoint.iOffset ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 131 | UNUSED_PARAMETER(iOfst); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 132 | |
| 133 | while( nWrite>0 ){ |
| 134 | FileChunk *pChunk = p->endpoint.pChunk; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 135 | int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 136 | int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset); |
| 137 | |
| 138 | if( iChunkOffset==0 ){ |
| 139 | /* New chunk is required to extend the file. */ |
| 140 | FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk)); |
| 141 | if( !pNew ){ |
| 142 | return SQLITE_IOERR_NOMEM; |
| 143 | } |
| 144 | pNew->pNext = 0; |
| 145 | if( pChunk ){ |
| 146 | assert( p->pFirst ); |
| 147 | pChunk->pNext = pNew; |
| 148 | }else{ |
| 149 | assert( !p->pFirst ); |
| 150 | p->pFirst = pNew; |
| 151 | } |
| 152 | p->endpoint.pChunk = pNew; |
| 153 | } |
| 154 | |
| 155 | memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace); |
| 156 | zWrite += iSpace; |
| 157 | nWrite -= iSpace; |
| 158 | p->endpoint.iOffset += iSpace; |
| 159 | } |
| 160 | |
| 161 | return SQLITE_OK; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | ** Truncate the file. |
| 166 | */ |
| 167 | static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
| 168 | MemJournal *p = (MemJournal *)pJfd; |
| 169 | FileChunk *pChunk; |
| 170 | assert(size==0); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 171 | UNUSED_PARAMETER(size); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 172 | pChunk = p->pFirst; |
| 173 | while( pChunk ){ |
| 174 | FileChunk *pTmp = pChunk; |
| 175 | pChunk = pChunk->pNext; |
| 176 | sqlite3_free(pTmp); |
| 177 | } |
| 178 | sqlite3MemJournalOpen(pJfd); |
| 179 | return SQLITE_OK; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | ** Close the file. |
| 184 | */ |
| 185 | static int memjrnlClose(sqlite3_file *pJfd){ |
| 186 | memjrnlTruncate(pJfd, 0); |
| 187 | return SQLITE_OK; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /* |
| 192 | ** Sync the file. |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 193 | ** |
| 194 | ** Syncing an in-memory journal is a no-op. And, in fact, this routine |
| 195 | ** is never called in a working implementation. This implementation |
| 196 | ** exists purely as a contingency, in case some malfunction in some other |
| 197 | ** part of SQLite causes Sync to be called by mistake. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 198 | */ |
drh | 09c0f6d | 2010-04-12 19:44:22 +0000 | [diff] [blame] | 199 | static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ |
| 200 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 201 | return SQLITE_OK; |
| 202 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | ** Query the size of the file in bytes. |
| 206 | */ |
| 207 | static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ |
| 208 | MemJournal *p = (MemJournal *)pJfd; |
| 209 | *pSize = (sqlite_int64) p->endpoint.iOffset; |
| 210 | return SQLITE_OK; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | ** Table of methods for MemJournal sqlite3_file object. |
| 215 | */ |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 216 | static const struct sqlite3_io_methods MemJournalMethods = { |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 217 | 1, /* iVersion */ |
| 218 | memjrnlClose, /* xClose */ |
| 219 | memjrnlRead, /* xRead */ |
| 220 | memjrnlWrite, /* xWrite */ |
| 221 | memjrnlTruncate, /* xTruncate */ |
| 222 | memjrnlSync, /* xSync */ |
| 223 | memjrnlFileSize, /* xFileSize */ |
| 224 | 0, /* xLock */ |
| 225 | 0, /* xUnlock */ |
| 226 | 0, /* xCheckReservedLock */ |
| 227 | 0, /* xFileControl */ |
| 228 | 0, /* xSectorSize */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 229 | 0, /* xDeviceCharacteristics */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 230 | 0, /* xShmMap */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 231 | 0, /* xShmLock */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 232 | 0, /* xShmBarrier */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 233 | 0 /* xShmUnlock */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /* |
| 237 | ** Open a journal file. |
| 238 | */ |
| 239 | void sqlite3MemJournalOpen(sqlite3_file *pJfd){ |
| 240 | MemJournal *p = (MemJournal *)pJfd; |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 241 | assert( EIGHT_BYTE_ALIGNMENT(p) ); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 242 | memset(p, 0, sqlite3MemJournalSize()); |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 243 | p->pMethod = (sqlite3_io_methods*)&MemJournalMethods; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
| 247 | ** Return true if the file-handle passed as an argument is |
| 248 | ** an in-memory journal |
| 249 | */ |
| 250 | int sqlite3IsMemJournal(sqlite3_file *pJfd){ |
| 251 | return pJfd->pMethods==&MemJournalMethods; |
| 252 | } |
| 253 | |
| 254 | /* |
drh | 2fcc7bd | 2010-09-16 23:18:57 +0000 | [diff] [blame] | 255 | ** Return the number of bytes required to store a MemJournal file descriptor. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 256 | */ |
drh | 3a5990a | 2008-12-20 02:14:39 +0000 | [diff] [blame] | 257 | int sqlite3MemJournalSize(void){ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 258 | return sizeof(MemJournal); |
| 259 | } |