1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Modify the memjournal.c code to make it a bit smaller.

FossilOrigin-Name: d99ac4154812065eef26c298de52954d7ee0bd75
This commit is contained in:
dan
2016-03-08 17:44:08 +00:00
parent d0b8b664bd
commit 7ed4020890
3 changed files with 49 additions and 62 deletions

View File

@@ -69,7 +69,6 @@ struct MemJournal {
int flags; /* xOpen flags */
sqlite3_vfs *pVfs; /* The "real" underlying VFS */
const char *zJournal; /* Name of the journal file */
sqlite3_file *pReal; /* The "real" underlying file descriptor */
};
/*
@@ -83,9 +82,7 @@ static int memjrnlRead(
sqlite_int64 iOfst /* Begin reading at this offset */
){
MemJournal *p = (MemJournal *)pJfd;
if( p->pReal ){
return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
}else if( (iAmt+iOfst)>p->endpoint.iOffset ){
if( (iAmt+iOfst)>p->endpoint.iOffset ){
return SQLITE_IOERR_SHORT_READ;
}else{
u8 *zOut = zBuf;
@@ -138,36 +135,37 @@ static void memjrnlFreeChunks(MemJournal *p){
** Flush the contents of memory to a real file on disk.
*/
static int memjrnlCreateFile(MemJournal *p){
int rc = SQLITE_OK;
if( !p->pReal ){
sqlite3_file *pReal = (sqlite3_file *)&p[1];
rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
if( rc==SQLITE_OK ){
int nChunk = p->nChunkSize;
i64 iOff = 0;
FileChunk *pIter;
p->pReal = pReal;
for(pIter=p->pFirst; pIter && rc==SQLITE_OK; pIter=pIter->pNext){
int nWrite = nChunk;
if( pIter==p->endpoint.pChunk ){
nWrite = p->endpoint.iOffset % p->nChunkSize;
if( nWrite==0 ) nWrite = p->nChunkSize;
}
rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nWrite, iOff);
iOff += nWrite;
}
if( rc!=SQLITE_OK ){
/* If an error occurred while writing to the file, close it before
** returning. This way, SQLite uses the in-memory journal data to
** roll back changes made to the internal page-cache before this
** function was called. */
sqlite3OsClose(pReal);
p->pReal = 0;
}else{
/* No error has occurred. Free the in-memory buffers. */
memjrnlFreeChunks(p);
int rc;
sqlite3_file *pReal = (sqlite3_file*)p;
MemJournal copy = *p;
memset(p, 0, sizeof(MemJournal));
rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
if( rc==SQLITE_OK ){
int nChunk = copy.nChunkSize;
i64 iOff = 0;
FileChunk *pIter;
for(pIter=copy.pFirst; pIter && rc==SQLITE_OK; pIter=pIter->pNext){
int nWrite = nChunk;
if( pIter==copy.endpoint.pChunk ){
nWrite = copy.endpoint.iOffset % copy.nChunkSize;
if( nWrite==0 ) nWrite = copy.nChunkSize;
}
rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nWrite, iOff);
iOff += nWrite;
}
if( rc==SQLITE_OK ){
/* No error has occurred. Free the in-memory buffers. */
memjrnlFreeChunks(&copy);
}
}
if( rc!=SQLITE_OK ){
/* If an error occurred while creating or writing to the file, restore
** the original before returning. This way, SQLite uses the in-memory
** journal data to roll back changes made to the internal page-cache
** before this function was called. */
sqlite3OsClose(pReal);
*p = copy;
}
return rc;
}
@@ -186,16 +184,12 @@ static int memjrnlWrite(
int nWrite = iAmt;
u8 *zWrite = (u8 *)zBuf;
/* If the file has already been created on disk. */
if( p->pReal ){
return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
}
/* If the file should be created now. */
else if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
/* If the file should be created now, create it and write the new data
** into the file on disk. */
if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
int rc = memjrnlCreateFile(p);
if( rc==SQLITE_OK ){
rc = memjrnlWrite(pJfd, zBuf, iAmt, iOfst);
rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
}
return rc;
}
@@ -255,9 +249,7 @@ static int memjrnlWrite(
*/
static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
MemJournal *p = (MemJournal *)pJfd;
if( p->pReal ){
return sqlite3OsTruncate(p->pReal, size);
}else if( size==0 ){
if( size==0 ){
memjrnlFreeChunks(p);
p->nSize = 0;
p->endpoint.pChunk = 0;
@@ -274,7 +266,6 @@ static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
static int memjrnlClose(sqlite3_file *pJfd){
MemJournal *p = (MemJournal *)pJfd;
memjrnlFreeChunks(p);
if( p->pReal ) sqlite3OsClose(p->pReal);
return SQLITE_OK;
}
@@ -285,10 +276,7 @@ static int memjrnlClose(sqlite3_file *pJfd){
** syncing an in-memory journal is a no-op.
*/
static int memjrnlSync(sqlite3_file *pJfd, int flags){
MemJournal *p = (MemJournal *)pJfd;
if( p->pReal ){
return sqlite3OsSync(p->pReal, flags);
}
UNUSED_PARAMETER2(pJfd, flags);
return SQLITE_OK;
}
@@ -297,9 +285,6 @@ static int memjrnlSync(sqlite3_file *pJfd, int flags){
*/
static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
MemJournal *p = (MemJournal *)pJfd;
if( p->pReal ){
return sqlite3OsFileSize(p->pReal, pSize);
}
*pSize = (sqlite_int64) p->endpoint.iOffset;
return SQLITE_OK;
}
@@ -354,7 +339,7 @@ int sqlite3JournalOpen(
** it using the sqlite3OsOpen() function of the underlying VFS. In this
** case none of the code in this module is executed as a result of calls
** made on the journal file-handle. */
memset(p, 0, sizeof(MemJournal) + (pVfs ? pVfs->szOsFile : 0));
memset(p, 0, sizeof(MemJournal));
if( nSpill==0 ){
return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
}
@@ -403,7 +388,7 @@ int sqlite3JournalCreate(sqlite3_file *p){
** or false otherwise.
*/
int sqlite3JournalIsInMemory(sqlite3_file *p){
return p->pMethods==&MemJournalMethods && ((MemJournal*)p)->pReal==0;
return p->pMethods==&MemJournalMethods;
}
/*
@@ -411,5 +396,5 @@ int sqlite3JournalIsInMemory(sqlite3_file *p){
** pVfs to create the underlying on-disk files.
*/
int sqlite3JournalSize(sqlite3_vfs *pVfs){
return pVfs->szOsFile + sizeof(MemJournal);
return MAX(pVfs->szOsFile, sizeof(MemJournal));
}