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

@ -1,5 +1,5 @@
C Add\sa\snew\srow\stype\sto\sRBU\s(a\speer\sof\sinsert,\supdate\sand\sdelete)\s-\s"delete\sthen\sinsert". C Modify\sthe\smemjournal.c\scode\sto\smake\sit\sa\sbit\ssmaller.
D 2016-03-08T15:52:43.903 D 2016-03-08T17:44:08.043
F Makefile.in f53429fb2f313c099283659d0df6f20f932c861f F Makefile.in f53429fb2f313c099283659d0df6f20f932c861f
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc df0bf9ff7f8b3f4dd9fb4cc43f92fe58f6ec5c66 F Makefile.msc df0bf9ff7f8b3f4dd9fb4cc43f92fe58f6ec5c66
@ -321,7 +321,7 @@ F src/mem1.c 6919bcf12f221868ea066eec27e579fed95ce98b
F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3 F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3
F src/mem3.c 8768ac94694f31ffaf8b4d0ea5dc08af7010a35a F src/mem3.c 8768ac94694f31ffaf8b4d0ea5dc08af7010a35a
F src/mem5.c 9bf955937b07f8c32541c8a9991f33ce3173d944 F src/mem5.c 9bf955937b07f8c32541c8a9991f33ce3173d944
F src/memjournal.c 011da5236a7250385cc74c253f14bbee04c0d61e F src/memjournal.c 02deb8930ae8103fbe5640433a9d55c5dd2c13f1
F src/msvc.h d9ba56c6851227ab44b3f228a35f3f5772296495 F src/msvc.h d9ba56c6851227ab44b3f228a35f3f5772296495
F src/mutex.c 8e45800ee78e0cd1f1f3fe8e398853307f4a085c F src/mutex.c 8e45800ee78e0cd1f1f3fe8e398853307f4a085c
F src/mutex.h 779d588e3b7756ec3ecf7d78cde1d84aba414f85 F src/mutex.h 779d588e3b7756ec3ecf7d78cde1d84aba414f85
@ -1455,8 +1455,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 5a847a676e756bbe33436596d4279f339bfb247c 169311c85b30f625bdb6986c9cd11db70942d73b P 1d9468d2427d2c9b7240b364554ac85a0b62fa44
R 43c0b748ba126143d737a43b13d0c445 R f53a6f3e4bc637f282a01c3504067543
T +closed 169311c85b30f625bdb6986c9cd11db70942d73b T *branch * memjournal-exp
T *sym-memjournal-exp *
T -sym-trunk *
U dan U dan
Z 99ed19537580c1e7328c1d8043178b0a Z e2ab5ced3facb0c6a3be08b7e7370857

View File

@ -1 +1 @@
1d9468d2427d2c9b7240b364554ac85a0b62fa44 d99ac4154812065eef26c298de52954d7ee0bd75

View File

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