]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Modify the memjournal.c code to make it a bit smaller. memjournal-exp
authordan <dan@noemail.net>
Tue, 8 Mar 2016 17:44:08 +0000 (17:44 +0000)
committerdan <dan@noemail.net>
Tue, 8 Mar 2016 17:44:08 +0000 (17:44 +0000)
FossilOrigin-Name: d99ac4154812065eef26c298de52954d7ee0bd75

manifest
manifest.uuid
src/memjournal.c

index 95a229990c361ca4bb0be521258ef2857cb1042b..40bd34b0068fc45ba69f8cad9780965bb516debe 100644 (file)
--- a/manifest
+++ b/manifest
@@ -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".
-D 2016-03-08T15:52:43.903
+C Modify\sthe\smemjournal.c\scode\sto\smake\sit\sa\sbit\ssmaller.
+D 2016-03-08T17:44:08.043
 F Makefile.in f53429fb2f313c099283659d0df6f20f932c861f
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc df0bf9ff7f8b3f4dd9fb4cc43f92fe58f6ec5c66
@@ -321,7 +321,7 @@ F src/mem1.c 6919bcf12f221868ea066eec27e579fed95ce98b
 F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3
 F src/mem3.c 8768ac94694f31ffaf8b4d0ea5dc08af7010a35a
 F src/mem5.c 9bf955937b07f8c32541c8a9991f33ce3173d944
-F src/memjournal.c 011da5236a7250385cc74c253f14bbee04c0d61e
+F src/memjournal.c 02deb8930ae8103fbe5640433a9d55c5dd2c13f1
 F src/msvc.h d9ba56c6851227ab44b3f228a35f3f5772296495
 F src/mutex.c 8e45800ee78e0cd1f1f3fe8e398853307f4a085c
 F src/mutex.h 779d588e3b7756ec3ecf7d78cde1d84aba414f85
@@ -1455,8 +1455,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 5a847a676e756bbe33436596d4279f339bfb247c 169311c85b30f625bdb6986c9cd11db70942d73b
-R 43c0b748ba126143d737a43b13d0c445
-T +closed 169311c85b30f625bdb6986c9cd11db70942d73b
+P 1d9468d2427d2c9b7240b364554ac85a0b62fa44
+R f53a6f3e4bc637f282a01c3504067543
+T *branch * memjournal-exp
+T *sym-memjournal-exp *
+T -sym-trunk *
 U dan
-Z 99ed19537580c1e7328c1d8043178b0a
+Z e2ab5ced3facb0c6a3be08b7e7370857
index e6281cbd82e38e40dc102edcfa90cba6130fb70f..c324dea819e318e6369392c8d0bfaf6c5d4620b6 100644 (file)
@@ -1 +1 @@
-1d9468d2427d2c9b7240b364554ac85a0b62fa44
\ No newline at end of file
+d99ac4154812065eef26c298de52954d7ee0bd75
\ No newline at end of file
index 04780df99c20963707dcbedc5b3522ead68d0478..2d6922ddeb87135539425f4edc08bfd2867c3a40 100644 (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,37 +135,38 @@ 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));
 }