/ Check-in [2d2f42ca]
Login

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

Overview
Comment:Simplifications and comment improvements in pager.c. (CVS 6926)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2d2f42ca0a24ed8b33f9ad560c76a6c1301c757b
User & Date: drh 2009-07-24 12:35:57
Context
2009-07-24
16:32
Simplifications to pager.c in support of structural coverage testing. (CVS 6927) check-in: 7222ad26 user: drh tags: trunk
12:35
Simplifications and comment improvements in pager.c. (CVS 6926) check-in: 2d2f42ca user: drh tags: trunk
2009-07-23
01:44
Simplifications and comment enhancements on btree.c. (CVS 6925) check-in: 5ba880dd user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.613 2009/07/22 16:41:15 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.614 2009/07/24 12:35:57 drh Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   
    26     26   /*
    27     27   ** Macros for troubleshooting.  Normally turned off
    28     28   */
................................................................................
  4396   4396       ** starting at pg1, then it needs to be set for all of them. Because
  4397   4397       ** writing to any of these nPage pages may damage the others, the
  4398   4398       ** journal file must contain sync()ed copies of all of them
  4399   4399       ** before any of them can be written out to the database file.
  4400   4400       */
  4401   4401       if( rc==SQLITE_OK && needSync ){
  4402   4402         assert( !MEMDB && pPager->noSync==0 );
  4403         -      for(ii=0; ii<nPage && needSync; ii++){
         4403  +      for(ii=0; ii<nPage; ii++){
  4404   4404           PgHdr *pPage = pager_lookup(pPager, pg1+ii);
  4405   4405           if( pPage ){
  4406   4406             pPage->flags |= PGHDR_NEED_SYNC;
  4407   4407             sqlite3PagerUnref(pPage);
  4408   4408           }
  4409   4409         }
  4410   4410         assert(pPager->needSync);
................................................................................
  4456   4456   }
  4457   4457   
  4458   4458   /*
  4459   4459   ** This routine is called to increment the value of the database file 
  4460   4460   ** change-counter, stored as a 4-byte big-endian integer starting at 
  4461   4461   ** byte offset 24 of the pager file.
  4462   4462   **
  4463         -** If the isDirect flag is zero, then this is done by calling 
         4463  +** If the isDirectMode flag is zero, then this is done by calling 
  4464   4464   ** sqlite3PagerWrite() on page 1, then modifying the contents of the
  4465   4465   ** page data. In this case the file will be updated when the current
  4466   4466   ** transaction is committed.
  4467   4467   **
  4468         -** The isDirect flag may only be non-zero if the library was compiled
         4468  +** The isDirectMode flag may only be non-zero if the library was compiled
  4469   4469   ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
  4470   4470   ** if isDirect is non-zero, then the database file is updated directly
  4471   4471   ** by writing an updated version of page 1 using a call to the 
  4472   4472   ** sqlite3OsWrite() function.
  4473   4473   */
  4474   4474   static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
  4475   4475     int rc = SQLITE_OK;
................................................................................
  4481   4481     **
  4482   4482     ** The idea is that if the atomic-write optimization is not
  4483   4483     ** enabled at compile time, the compiler can omit the tests of
  4484   4484     ** 'isDirect' below, as well as the block enclosed in the
  4485   4485     ** "if( isDirect )" condition.
  4486   4486     */
  4487   4487   #ifndef SQLITE_ENABLE_ATOMIC_WRITE
  4488         -  const int isDirect = 0;
         4488  +# define DIRECT_MODE 0
  4489   4489     assert( isDirectMode==0 );
  4490   4490     UNUSED_PARAMETER(isDirectMode);
  4491   4491   #else
  4492         -  const int isDirect = isDirectMode;
         4492  +# define DIRECT_MODE isDirectMode
  4493   4493   #endif
  4494   4494   
  4495   4495     assert( pPager->state>=PAGER_RESERVED );
  4496   4496     if( !pPager->changeCountDone && pPager->dbSize>0 ){
  4497   4497       PgHdr *pPgHdr;                /* Reference to page 1 */
  4498   4498       u32 change_counter;           /* Initial value of change-counter field */
  4499   4499   
................................................................................
  4502   4502       /* Open page 1 of the file for writing. */
  4503   4503       rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
  4504   4504       assert( pPgHdr==0 || rc==SQLITE_OK );
  4505   4505   
  4506   4506       /* If page one was fetched successfully, and this function is not
  4507   4507       ** operating in direct-mode, make page 1 writable.
  4508   4508       */
  4509         -    if( rc==SQLITE_OK && !isDirect ){
         4509  +    if( rc==SQLITE_OK && !DIRECT_MODE ){
  4510   4510         rc = sqlite3PagerWrite(pPgHdr);
  4511   4511       }
  4512   4512   
  4513   4513       if( rc==SQLITE_OK ){
  4514   4514         /* Increment the value just read and write it back to byte 24. */
  4515   4515         change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
  4516   4516         change_counter++;
  4517   4517         put32bits(((char*)pPgHdr->pData)+24, change_counter);
  4518   4518   
  4519   4519         /* If running in direct mode, write the contents of page 1 to the file. */
  4520         -      if( isDirect ){
         4520  +      if( DIRECT_MODE ){
  4521   4521           const void *zBuf = pPgHdr->pData;
  4522   4522           assert( pPager->dbFileSize>0 );
  4523   4523           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
  4524         -      }
  4525         -
  4526         -      /* If everything worked, set the changeCountDone flag. */
  4527         -      if( rc==SQLITE_OK ){
         4524  +        if( rc==SQLITE_OK ){
         4525  +          pPager->changeCountDone = 1;
         4526  +        }
         4527  +      }else{
  4528   4528           pPager->changeCountDone = 1;
  4529   4529         }
  4530   4530       }
  4531   4531   
  4532   4532       /* Release the page reference. */
  4533   4533       sqlite3PagerUnref(pPgHdr);
  4534   4534     }
................................................................................
  4581   4581   int sqlite3PagerCommitPhaseOne(
  4582   4582     Pager *pPager,                  /* Pager object */
  4583   4583     const char *zMaster,            /* If not NULL, the master journal name */
  4584   4584     int noSync                      /* True to omit the xSync on the db file */
  4585   4585   ){
  4586   4586     int rc = SQLITE_OK;             /* Return code */
  4587   4587   
  4588         -  if( pPager->errCode ){
  4589         -    return pPager->errCode;
  4590         -  }
         4588  +  /* If a prior error occurred, this routine should not be called.  ROLLBACK
         4589  +  ** is the appropriate response to an error, not COMMIT.  Guard against
         4590  +  ** coding errors by repeating the prior error. */
         4591  +  if( NEVER(pPager->errCode) ) return pPager->errCode;
  4591   4592   
  4592   4593     PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
  4593   4594         pPager->zFilename, zMaster, pPager->dbSize));
  4594   4595   
  4595         -  /* If this is an in-memory db, or no pages have been written to, or this
  4596         -  ** function has already been called, it is a no-op.
  4597         -  */
  4598   4596     if( MEMDB && pPager->dbModified ){
         4597  +    /* If this is an in-memory db, or no pages have been written to, or this
         4598  +    ** function has already been called, it is mostly a no-op.  However, any
         4599  +    ** backup in progress needs to be restarted.
         4600  +    */
  4599   4601       sqlite3BackupRestart(pPager->pBackup);
  4600   4602     }else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){
  4601   4603   
  4602   4604       /* The following block updates the change-counter. Exactly how it
  4603   4605       ** does this depends on whether or not the atomic-update optimization
  4604   4606       ** was enabled at compile time, and if this transaction meets the 
  4605   4607       ** runtime criteria to use the operation: 
................................................................................
  4718   4720       }
  4719   4721       IOTRACE(("DBSYNC %p\n", pPager))
  4720   4722   
  4721   4723       pPager->state = PAGER_SYNCED;
  4722   4724     }
  4723   4725   
  4724   4726   commit_phase_one_exit:
  4725         -  if( rc==SQLITE_IOERR_BLOCKED ){
  4726         -    /* pager_incr_changecounter() may attempt to obtain an exclusive
  4727         -    ** lock to spill the cache and return IOERR_BLOCKED. But since 
  4728         -    ** there is no chance the cache is inconsistent, it is
  4729         -    ** better to return SQLITE_BUSY.
  4730         -    **/
  4731         -    rc = SQLITE_BUSY;
  4732         -  }
  4733   4727     return rc;
  4734   4728   }
  4735   4729   
  4736   4730   
  4737   4731   /*
  4738   4732   ** When this function is called, the database file has been completely
  4739   4733   ** updated to reflect the changes made by the current transaction and
................................................................................
  4748   4742   **
  4749   4743   ** If an error occurs, an IO error code is returned and the pager
  4750   4744   ** moves into the error state. Otherwise, SQLITE_OK is returned.
  4751   4745   */
  4752   4746   int sqlite3PagerCommitPhaseTwo(Pager *pPager){
  4753   4747     int rc = SQLITE_OK;                  /* Return code */
  4754   4748   
  4755         -  /* Do not proceed if the pager is already in the error state. */
  4756         -  if( pPager->errCode ){
  4757         -    return pPager->errCode;
  4758         -  }
         4749  +  /* This routine should not be called if a prior error has occurred.
         4750  +  ** But if (due to a coding error elsewhere in the system) it does get
         4751  +  ** called, just return the same error code without doing anything. */
         4752  +  if( NEVER(pPager->errCode) ) return pPager->errCode;
  4759   4753   
  4760   4754     /* This function should not be called if the pager is not in at least
  4761   4755     ** PAGER_RESERVED state. And indeed SQLite never does this. But it is
  4762         -  ** nice to have this defensive block here anyway.
         4756  +  ** nice to have this defensive test here anyway.
  4763   4757     */
  4764         -  if( NEVER(pPager->state<PAGER_RESERVED) ){
  4765         -    return SQLITE_ERROR;
  4766         -  }
         4758  +  if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR;
  4767   4759   
  4768   4760     /* An optimization. If the database was not actually modified during
  4769   4761     ** this transaction, the pager is running in exclusive-mode and is
  4770   4762     ** using persistent journals, then this function is a no-op.
  4771   4763     **
  4772   4764     ** The start of the journal file currently contains a single journal 
  4773   4765     ** header with the nRec field set to 0. If such a journal is used as