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 | /* |
| 25 | ** The rollback journal is composed of a linked list of these structures. |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 26 | ** |
| 27 | ** The zChunk array is always at least 8 bytes in size - usually much more. |
| 28 | ** Its actual size is stored in the MemJournal.nChunkSize variable. |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 29 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 30 | struct FileChunk { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 31 | FileChunk *pNext; /* Next chunk in the journal */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 32 | u8 zChunk[8]; /* Content of this chunk */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 35 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 36 | ** By default, allocate this many bytes of memory for each FileChunk object. |
| 37 | */ |
| 38 | #define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024 |
| 39 | |
| 40 | /* |
| 41 | ** For chunk size nChunkSize, return the number of bytes that should |
| 42 | ** be allocated for each FileChunk structure. |
| 43 | */ |
| 44 | #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8)) |
| 45 | |
| 46 | /* |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 47 | ** An instance of this object serves as a cursor into the rollback journal. |
| 48 | ** The cursor can be either for reading or writing. |
| 49 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 50 | struct FilePoint { |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 51 | sqlite3_int64 iOffset; /* Offset from the beginning of the file */ |
| 52 | FileChunk *pChunk; /* Specific chunk into which cursor points */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 55 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 56 | ** This structure is a subclass of sqlite3_file. Each open memory-journal |
drh | 27c3bd7 | 2008-10-28 18:12:36 +0000 | [diff] [blame] | 57 | ** is an instance of this class. |
| 58 | */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 59 | struct MemJournal { |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 60 | const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ |
| 61 | int nChunkSize; /* In-memory chunk-size */ |
| 62 | |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 63 | int nSpill; /* Bytes of data before flushing */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 64 | int nSize; /* Bytes of data currently in memory */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 65 | FileChunk *pFirst; /* Head of in-memory chunk-list */ |
| 66 | FilePoint endpoint; /* Pointer to the end of the file */ |
| 67 | FilePoint readpoint; /* Pointer to the end of the last xRead() */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 68 | |
| 69 | int flags; /* xOpen flags */ |
| 70 | sqlite3_vfs *pVfs; /* The "real" underlying VFS */ |
| 71 | const char *zJournal; /* Name of the journal file */ |
| 72 | sqlite3_file *pReal; /* The "real" underlying file descriptor */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 76 | ** Read data from the in-memory journal file. This is the implementation |
| 77 | ** of the sqlite3_vfs.xRead method. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 78 | */ |
| 79 | static int memjrnlRead( |
| 80 | sqlite3_file *pJfd, /* The journal file from which to read */ |
| 81 | void *zBuf, /* Put the results here */ |
| 82 | int iAmt, /* Number of bytes to read */ |
| 83 | sqlite_int64 iOfst /* Begin reading at this offset */ |
| 84 | ){ |
| 85 | MemJournal *p = (MemJournal *)pJfd; |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 86 | if( p->pReal ){ |
| 87 | return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); |
| 88 | }else if( (iAmt+iOfst)>p->endpoint.iOffset ){ |
| 89 | return SQLITE_IOERR_SHORT_READ; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 90 | }else{ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 91 | u8 *zOut = zBuf; |
| 92 | int nRead = iAmt; |
| 93 | int iChunkOffset; |
| 94 | FileChunk *pChunk; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 95 | |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 96 | if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ |
| 97 | sqlite3_int64 iOff = 0; |
| 98 | for(pChunk=p->pFirst; |
| 99 | ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst; |
| 100 | pChunk=pChunk->pNext |
| 101 | ){ |
| 102 | iOff += p->nChunkSize; |
| 103 | } |
| 104 | }else{ |
| 105 | pChunk = p->readpoint.pChunk; |
| 106 | } |
| 107 | |
| 108 | iChunkOffset = (int)(iOfst%p->nChunkSize); |
| 109 | do { |
| 110 | int iSpace = p->nChunkSize - iChunkOffset; |
| 111 | int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset)); |
drh | 65a7e76 | 2016-03-05 15:03:31 +0000 | [diff] [blame] | 112 | memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy); |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 113 | zOut += nCopy; |
| 114 | nRead -= iSpace; |
| 115 | iChunkOffset = 0; |
| 116 | } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); |
| 117 | p->readpoint.iOffset = iOfst+iAmt; |
| 118 | p->readpoint.pChunk = pChunk; |
| 119 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 120 | |
| 121 | return SQLITE_OK; |
| 122 | } |
| 123 | |
| 124 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 125 | ** Free the list of FileChunk structures headed at MemJournal.pFirst. |
| 126 | */ |
| 127 | static void memjrnlFreeChunks(MemJournal *p){ |
| 128 | FileChunk *pIter; |
| 129 | FileChunk *pNext; |
| 130 | for(pIter=p->pFirst; pIter; pIter=pNext){ |
| 131 | pNext = pIter->pNext; |
| 132 | sqlite3_free(pIter); |
| 133 | } |
| 134 | p->pFirst = 0; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | ** Flush the contents of memory to a real file on disk. |
| 139 | */ |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 140 | static int memjrnlCreateFile(MemJournal *p){ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 141 | int rc = SQLITE_OK; |
| 142 | if( !p->pReal ){ |
| 143 | sqlite3_file *pReal = (sqlite3_file *)&p[1]; |
| 144 | rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0); |
| 145 | if( rc==SQLITE_OK ){ |
| 146 | int nChunk = p->nChunkSize; |
| 147 | i64 iOff = 0; |
| 148 | FileChunk *pIter; |
| 149 | p->pReal = pReal; |
| 150 | for(pIter=p->pFirst; pIter && rc==SQLITE_OK; pIter=pIter->pNext){ |
| 151 | int nWrite = nChunk; |
| 152 | if( pIter==p->endpoint.pChunk ){ |
| 153 | nWrite = p->endpoint.iOffset % p->nChunkSize; |
| 154 | if( nWrite==0 ) nWrite = p->nChunkSize; |
| 155 | } |
drh | 65a7e76 | 2016-03-05 15:03:31 +0000 | [diff] [blame] | 156 | rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nWrite, iOff); |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 157 | iOff += nWrite; |
| 158 | } |
| 159 | if( rc!=SQLITE_OK ){ |
| 160 | /* If an error occurred while writing to the file, close it before |
| 161 | ** returning. This way, SQLite uses the in-memory journal data to |
| 162 | ** roll back changes made to the internal page-cache before this |
| 163 | ** function was called. */ |
| 164 | sqlite3OsClose(pReal); |
| 165 | p->pReal = 0; |
| 166 | }else{ |
| 167 | /* No error has occurred. Free the in-memory buffers. */ |
| 168 | memjrnlFreeChunks(p); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 177 | ** Write data to the file. |
| 178 | */ |
| 179 | static int memjrnlWrite( |
| 180 | sqlite3_file *pJfd, /* The journal file into which to write */ |
| 181 | const void *zBuf, /* Take data to be written from here */ |
| 182 | int iAmt, /* Number of bytes to write */ |
| 183 | sqlite_int64 iOfst /* Begin writing at this offset into the file */ |
| 184 | ){ |
| 185 | MemJournal *p = (MemJournal *)pJfd; |
| 186 | int nWrite = iAmt; |
| 187 | u8 *zWrite = (u8 *)zBuf; |
| 188 | |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 189 | /* If the file has already been created on disk. */ |
| 190 | if( p->pReal ){ |
| 191 | return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); |
| 192 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 193 | |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 194 | /* If the file should be created now. */ |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 195 | else if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){ |
| 196 | int rc = memjrnlCreateFile(p); |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 197 | if( rc==SQLITE_OK ){ |
| 198 | rc = memjrnlWrite(pJfd, zBuf, iAmt, iOfst); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 199 | } |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 200 | return rc; |
| 201 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 202 | |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 203 | /* If the contents of this write should be stored in memory */ |
| 204 | else{ |
| 205 | /* An in-memory journal file should only ever be appended to. Random |
| 206 | ** access writes are not required. The only exception to this is when |
| 207 | ** the in-memory journal is being used by a connection using the |
| 208 | ** atomic-write optimization. In this case the first 28 bytes of the |
| 209 | ** journal file may be written as part of committing the transaction. */ |
| 210 | assert( iOfst==p->endpoint.iOffset || iOfst==0 ); |
| 211 | if( iOfst==0 && p->pFirst ){ |
| 212 | assert( p->nChunkSize>iAmt ); |
drh | 65a7e76 | 2016-03-05 15:03:31 +0000 | [diff] [blame] | 213 | memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 214 | }else{ |
| 215 | while( nWrite>0 ){ |
| 216 | FileChunk *pChunk = p->endpoint.pChunk; |
| 217 | int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); |
| 218 | int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); |
| 219 | |
| 220 | if( iChunkOffset==0 ){ |
| 221 | /* New chunk is required to extend the file. */ |
| 222 | FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); |
| 223 | if( !pNew ){ |
| 224 | return SQLITE_IOERR_NOMEM_BKPT; |
| 225 | } |
| 226 | pNew->pNext = 0; |
| 227 | if( pChunk ){ |
| 228 | assert( p->pFirst ); |
| 229 | pChunk->pNext = pNew; |
| 230 | }else{ |
| 231 | assert( !p->pFirst ); |
| 232 | p->pFirst = pNew; |
| 233 | } |
| 234 | p->endpoint.pChunk = pNew; |
| 235 | } |
| 236 | |
drh | 65a7e76 | 2016-03-05 15:03:31 +0000 | [diff] [blame] | 237 | memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace); |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 238 | zWrite += iSpace; |
| 239 | nWrite -= iSpace; |
| 240 | p->endpoint.iOffset += iSpace; |
| 241 | } |
| 242 | p->nSize = iAmt + iOfst; |
| 243 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return SQLITE_OK; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | ** Truncate the file. |
dan | 5f37ed5 | 2016-02-29 20:00:13 +0000 | [diff] [blame] | 251 | ** |
| 252 | ** If the journal file is already on disk, truncate it there. Or, if it |
| 253 | ** is still in main memory but is being truncated to zero bytes in size, |
| 254 | ** ignore |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 255 | */ |
| 256 | static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
| 257 | MemJournal *p = (MemJournal *)pJfd; |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 258 | if( p->pReal ){ |
| 259 | return sqlite3OsTruncate(p->pReal, size); |
| 260 | }else if( size==0 ){ |
| 261 | memjrnlFreeChunks(p); |
| 262 | p->nSize = 0; |
| 263 | p->endpoint.pChunk = 0; |
| 264 | p->endpoint.iOffset = 0; |
| 265 | p->readpoint.pChunk = 0; |
| 266 | p->readpoint.iOffset = 0; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 267 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 268 | return SQLITE_OK; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | ** Close the file. |
| 273 | */ |
| 274 | static int memjrnlClose(sqlite3_file *pJfd){ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 275 | MemJournal *p = (MemJournal *)pJfd; |
| 276 | memjrnlFreeChunks(p); |
| 277 | if( p->pReal ) sqlite3OsClose(p->pReal); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 278 | return SQLITE_OK; |
| 279 | } |
| 280 | |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 281 | /* |
| 282 | ** Sync the file. |
drh | 2206a2b | 2009-04-01 23:09:43 +0000 | [diff] [blame] | 283 | ** |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 284 | ** If the real file has been created, call its xSync method. Otherwise, |
| 285 | ** syncing an in-memory journal is a no-op. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 286 | */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 287 | static int memjrnlSync(sqlite3_file *pJfd, int flags){ |
| 288 | MemJournal *p = (MemJournal *)pJfd; |
| 289 | if( p->pReal ){ |
| 290 | return sqlite3OsSync(p->pReal, flags); |
| 291 | } |
drh | 09c0f6d | 2010-04-12 19:44:22 +0000 | [diff] [blame] | 292 | return SQLITE_OK; |
| 293 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | ** Query the size of the file in bytes. |
| 297 | */ |
| 298 | static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ |
| 299 | MemJournal *p = (MemJournal *)pJfd; |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 300 | if( p->pReal ){ |
| 301 | return sqlite3OsFileSize(p->pReal, pSize); |
| 302 | } |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 303 | *pSize = (sqlite_int64) p->endpoint.iOffset; |
| 304 | return SQLITE_OK; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | ** Table of methods for MemJournal sqlite3_file object. |
| 309 | */ |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 310 | static const struct sqlite3_io_methods MemJournalMethods = { |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 311 | 1, /* iVersion */ |
| 312 | memjrnlClose, /* xClose */ |
| 313 | memjrnlRead, /* xRead */ |
| 314 | memjrnlWrite, /* xWrite */ |
| 315 | memjrnlTruncate, /* xTruncate */ |
| 316 | memjrnlSync, /* xSync */ |
| 317 | memjrnlFileSize, /* xFileSize */ |
| 318 | 0, /* xLock */ |
| 319 | 0, /* xUnlock */ |
| 320 | 0, /* xCheckReservedLock */ |
| 321 | 0, /* xFileControl */ |
| 322 | 0, /* xSectorSize */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 323 | 0, /* xDeviceCharacteristics */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 324 | 0, /* xShmMap */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 325 | 0, /* xShmLock */ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 326 | 0, /* xShmBarrier */ |
drh | da8caa0 | 2013-04-22 23:38:50 +0000 | [diff] [blame] | 327 | 0, /* xShmUnmap */ |
| 328 | 0, /* xFetch */ |
| 329 | 0 /* xUnfetch */ |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 333 | ** Open a journal file. |
| 334 | ** |
| 335 | ** The behaviour of the journal file depends on the value of parameter |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 336 | ** nSpill. If nSpill is 0, then the journal file is always create and |
| 337 | ** accessed using the underlying VFS. If nSpill is less than zero, then |
| 338 | ** all content is always stored in main-memory. Finally, if nSpill is a |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 339 | ** positive value, then the journal file is initially created in-memory |
| 340 | ** but may be flushed to disk later on. In this case the journal file is |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 341 | ** flushed to disk either when it grows larger than nSpill bytes in size, |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 342 | ** or when sqlite3JournalCreate() is called. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 343 | */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 344 | int sqlite3JournalOpen( |
| 345 | sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ |
| 346 | const char *zName, /* Name of the journal file */ |
| 347 | sqlite3_file *pJfd, /* Preallocated, blank file handle */ |
| 348 | int flags, /* Opening flags */ |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 349 | int nSpill /* Bytes buffered before opening the file */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 350 | ){ |
| 351 | MemJournal *p = (MemJournal*)pJfd; |
| 352 | |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 353 | /* Zero the file-handle object. If nSpill was passed zero, initialize |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 354 | ** it using the sqlite3OsOpen() function of the underlying VFS. In this |
| 355 | ** case none of the code in this module is executed as a result of calls |
| 356 | ** made on the journal file-handle. */ |
dan | 6e76326 | 2016-02-29 20:18:21 +0000 | [diff] [blame] | 357 | memset(p, 0, sizeof(MemJournal) + (pVfs ? pVfs->szOsFile : 0)); |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 358 | if( nSpill==0 ){ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 359 | return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); |
| 360 | } |
| 361 | |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 362 | if( nSpill>0 ){ |
| 363 | p->nChunkSize = nSpill; |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 364 | }else{ |
| 365 | p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk); |
| 366 | assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) ); |
| 367 | } |
| 368 | |
| 369 | p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods; |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 370 | p->nSpill = nSpill; |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 371 | p->flags = flags; |
| 372 | p->zJournal = zName; |
| 373 | p->pVfs = pVfs; |
| 374 | return SQLITE_OK; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 378 | ** Open an in-memory journal file. |
| 379 | */ |
| 380 | void sqlite3MemJournalOpen(sqlite3_file *pJfd){ |
| 381 | sqlite3JournalOpen(0, 0, pJfd, 0, -1); |
| 382 | } |
| 383 | |
drh | ff6b826 | 2016-03-04 00:13:29 +0000 | [diff] [blame] | 384 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 385 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 386 | ** If the argument p points to a MemJournal structure that is not an |
dan | 5f37ed5 | 2016-02-29 20:00:13 +0000 | [diff] [blame] | 387 | ** in-memory-only journal file (i.e. is one that was opened with a +ve |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 388 | ** nSpill parameter), and the underlying file has not yet been created, |
dan | 5f37ed5 | 2016-02-29 20:00:13 +0000 | [diff] [blame] | 389 | ** create it now. |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 390 | */ |
| 391 | int sqlite3JournalCreate(sqlite3_file *p){ |
| 392 | int rc = SQLITE_OK; |
drh | c2f18ad | 2016-03-05 15:35:09 +0000 | [diff] [blame] | 393 | if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){ |
| 394 | rc = memjrnlCreateFile((MemJournal*)p); |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 395 | } |
| 396 | return rc; |
| 397 | } |
drh | ff6b826 | 2016-03-04 00:13:29 +0000 | [diff] [blame] | 398 | #endif |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 399 | |
| 400 | /* |
dan | 5f37ed5 | 2016-02-29 20:00:13 +0000 | [diff] [blame] | 401 | ** The file-handle passed as the only argument is open on a journal file. |
| 402 | ** Return true if this "journal file" is currently stored in heap memory, |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 403 | ** or false otherwise. |
| 404 | */ |
| 405 | int sqlite3JournalIsInMemory(sqlite3_file *p){ |
| 406 | return p->pMethods==&MemJournalMethods && ((MemJournal*)p)->pReal==0; |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 410 | ** Return the number of bytes required to store a JournalFile that uses vfs |
| 411 | ** pVfs to create the underlying on-disk files. |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 412 | */ |
dan | 2491de2 | 2016-02-27 20:14:55 +0000 | [diff] [blame] | 413 | int sqlite3JournalSize(sqlite3_vfs *pVfs){ |
| 414 | return pVfs->szOsFile + sizeof(MemJournal); |
danielk1977 | 39281b4 | 2008-10-17 19:13:04 +0000 | [diff] [blame] | 415 | } |