/ Check-in [8baa2c2c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Simplifications to the memjournal.c logic to facilitate testing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8baa2c2c7698e03418531482a8314a5d7ae2c7d3
User & Date: drh 2016-03-09 04:17:17
Context
2016-03-09
11:59
Disable the bestindex*.test modules when building without virtual table support. check-in: e0bac2f3 user: drh tags: trunk
08:08
Merge latest trunk changes with this branch. check-in: 59caca43 user: dan tags: schemalint
04:17
Simplifications to the memjournal.c logic to facilitate testing. check-in: 8baa2c2c user: drh tags: trunk
03:44
Move the write failure test in memjrnlCreateFile() to just after the actual write, thus reducing the number of instances of the test by one. check-in: 7f00d80c user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/memjournal.c.

    78     78   static int memjrnlRead(
    79     79     sqlite3_file *pJfd,    /* The journal file from which to read */
    80     80     void *zBuf,            /* Put the results here */
    81     81     int iAmt,              /* Number of bytes to read */
    82     82     sqlite_int64 iOfst     /* Begin reading at this offset */
    83     83   ){
    84     84     MemJournal *p = (MemJournal *)pJfd;
           85  +  u8 *zOut = zBuf;
           86  +  int nRead = iAmt;
           87  +  int iChunkOffset;
           88  +  FileChunk *pChunk;
           89  +
           90  +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
    85     91     if( (iAmt+iOfst)>p->endpoint.iOffset ){
    86     92       return SQLITE_IOERR_SHORT_READ;
    87         -  }else{
    88         -    u8 *zOut = zBuf;
    89         -    int nRead = iAmt;
    90         -    int iChunkOffset;
    91         -    FileChunk *pChunk;
           93  +  }
           94  +#endif
    92     95   
    93         -    if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
    94         -      sqlite3_int64 iOff = 0;
    95         -      for(pChunk=p->pFirst; 
    96         -          ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
    97         -          pChunk=pChunk->pNext
    98         -      ){
    99         -        iOff += p->nChunkSize;
   100         -      }
   101         -    }else{
   102         -      pChunk = p->readpoint.pChunk;
           96  +  assert( (iAmt+iOfst)<=p->endpoint.iOffset );
           97  +  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
           98  +    sqlite3_int64 iOff = 0;
           99  +    for(pChunk=p->pFirst; 
          100  +        ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
          101  +        pChunk=pChunk->pNext
          102  +    ){
          103  +      iOff += p->nChunkSize;
   103    104       }
   104         -
   105         -    iChunkOffset = (int)(iOfst%p->nChunkSize);
   106         -    do {
   107         -      int iSpace = p->nChunkSize - iChunkOffset;
   108         -      int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
   109         -      memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
   110         -      zOut += nCopy;
   111         -      nRead -= iSpace;
   112         -      iChunkOffset = 0;
   113         -    } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
   114         -    p->readpoint.iOffset = iOfst+iAmt;
   115         -    p->readpoint.pChunk = pChunk;
          105  +  }else{
          106  +    pChunk = p->readpoint.pChunk;
   116    107     }
          108  +
          109  +  iChunkOffset = (int)(iOfst%p->nChunkSize);
          110  +  do {
          111  +    int iSpace = p->nChunkSize - iChunkOffset;
          112  +    int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
          113  +    memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
          114  +    zOut += nCopy;
          115  +    nRead -= iSpace;
          116  +    iChunkOffset = 0;
          117  +  } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
          118  +  p->readpoint.iOffset = iOfst+iAmt;
          119  +  p->readpoint.pChunk = pChunk;
   117    120   
   118    121     return SQLITE_OK;
   119    122   }
   120    123   
   121    124   /*
   122    125   ** Free the list of FileChunk structures headed at MemJournal.pFirst.
   123    126   */
................................................................................
   142    145     memset(p, 0, sizeof(MemJournal));
   143    146     rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
   144    147     if( rc==SQLITE_OK ){
   145    148       int nChunk = copy.nChunkSize;
   146    149       i64 iOff = 0;
   147    150       FileChunk *pIter;
   148    151       for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
   149         -      int nWrite = nChunk;
   150         -      if( pIter==copy.endpoint.pChunk ){
   151         -        nWrite = copy.endpoint.iOffset % copy.nChunkSize;
   152         -        if( nWrite==0 ) nWrite = copy.nChunkSize;
          152  +      if( iOff + nChunk > copy.endpoint.iOffset ){
          153  +        nChunk = copy.endpoint.iOffset - iOff;
   153    154         }
   154         -      rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nWrite, iOff);
          155  +      rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
   155    156         if( rc ) break;
   156         -      iOff += nWrite;
          157  +      iOff += nChunk;
   157    158       }
   158    159       if( rc==SQLITE_OK ){
   159    160         /* No error has occurred. Free the in-memory buffers. */
   160    161         memjrnlFreeChunks(&copy);
   161    162       }
   162    163     }
   163    164     if( rc!=SQLITE_OK ){