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 |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 29 | ** a power-of-two allocation. This minimizes wasted space in power-of-two |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 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 | /* |
| 35 | ** The rollback journal is composed of a linked list of these structures. |
| 36 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 37 | struct FileChunk { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 38 | FileChunk *pNext; /* Next chunk in the journal */ |
| 39 | u8 zChunk[JOURNAL_CHUNKSIZE]; /* Content of this chunk */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 42 | /* |
| 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 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 46 | struct FilePoint { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 47 | sqlite3_int64 iOffset; /* Offset from the beginning of the file */ |
| 48 | FileChunk *pChunk; /* Specific chunk into which cursor points */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 51 | /* |
| 52 | ** This subclass is a subclass of sqlite3_file. Each open memory-journal |
| 53 | ** is an instance of this class. |
| 54 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 55 | struct MemJournal { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 56 | sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 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() */ |
| 60 | }; |
| 61 | |
| 62 | /* |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 63 | ** Read data from the in-memory journal file. This is the implementation |
| 64 | ** of the sqlite3_vfs.xRead method. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 65 | */ |
| 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 */ |
| 71 | ){ |
| 72 | MemJournal *p = (MemJournal *)pJfd; |
| 73 | u8 *zOut = zBuf; |
| 74 | int nRead = iAmt; |
| 75 | int iChunkOffset; |
| 76 | FileChunk *pChunk; |
| 77 | |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 78 | /* SQLite never tries to read past the end of a rollback journal file */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 79 | 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; |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 84 | ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 85 | pChunk=pChunk->pNext |
| 86 | ){ |
| 87 | iOff += JOURNAL_CHUNKSIZE; |
| 88 | } |
| 89 | }else{ |
| 90 | pChunk = p->readpoint.pChunk; |
| 91 | } |
| 92 | |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 93 | iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 94 | 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; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 101 | } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 102 | 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 | */ |
| 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 */ |
| 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 | */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 124 | assert( iOfst==p->endpoint.iOffset ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 125 | UNUSED_PARAMETER(iOfst); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 126 | |
| 127 | while( nWrite>0 ){ |
| 128 | FileChunk *pChunk = p->endpoint.pChunk; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 129 | int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 130 | 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 | */ |
| 161 | static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
| 162 | MemJournal *p = (MemJournal *)pJfd; |
| 163 | FileChunk *pChunk; |
| 164 | assert(size==0); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 165 | UNUSED_PARAMETER(size); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 166 | 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 | */ |
| 179 | static int memjrnlClose(sqlite3_file *pJfd){ |
| 180 | memjrnlTruncate(pJfd, 0); |
| 181 | return SQLITE_OK; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | ** Sync the file. |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 187 | ** |
| 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. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 192 | */ |
drh | 09c0f6d | 2010-04-12 19:44:22 +0000 | [diff] [blame] | 193 | static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ |
| 194 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 195 | return SQLITE_OK; |
| 196 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | ** Query the size of the file in bytes. |
| 200 | */ |
| 201 | static 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 | */ |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 210 | static const struct sqlite3_io_methods MemJournalMethods = { |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 211 | 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 */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 223 | 0, /* xDeviceCharacteristics */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 224 | 0, /* xShmMap */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 225 | 0, /* xShmLock */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 226 | 0, /* xShmBarrier */ |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 227 | 0, /* xShmUnmap */ |
| 228 | 0, /* xFetch */ |
| 229 | 0 /* xUnfetch */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | /* |
| 233 | ** Open a journal file. |
| 234 | */ |
| 235 | void sqlite3MemJournalOpen(sqlite3_file *pJfd){ |
| 236 | MemJournal *p = (MemJournal *)pJfd; |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 237 | assert( EIGHT_BYTE_ALIGNMENT(p) ); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 238 | memset(p, 0, sqlite3MemJournalSize()); |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 239 | p->pMethod = (sqlite3_io_methods*)&MemJournalMethods; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | ** Return true if the file-handle passed as an argument is |
| 244 | ** an in-memory journal |
| 245 | */ |
| 246 | int sqlite3IsMemJournal(sqlite3_file *pJfd){ |
| 247 | return pJfd->pMethods==&MemJournalMethods; |
| 248 | } |
| 249 | |
| 250 | /* |
drh | 2fcc7bd | 2010-09-16 23:18:57 +0000 | [diff] [blame] | 251 | ** Return the number of bytes required to store a MemJournal file descriptor. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 252 | */ |
drh | 3a5990a | 2008-12-20 02:14:39 +0000 | [diff] [blame] | 253 | int sqlite3MemJournalSize(void){ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 254 | return sizeof(MemJournal); |
| 255 | } |