Exemple #1
0
 /*
 ** Dummy page reinitializer
 */
 static void pager_test_reiniter( DbPage pNotUsed )
 {
   return;
 }
Exemple #2
0
        /*
        ** Move the page pPg to location pgno in the file.
        **
        ** There must be no references to the page previously located at
        ** pgno (which we call pPgOld) though that page is allowed to be
        ** in cache.  If the page previously located at pgno is not already
        ** in the rollback journal, it is not put there by by this routine.
        **
        ** References to the page pPg remain valid. Updating any
        ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
        ** allocated along with the page) is the responsibility of the caller.
        **
        ** A transaction must be active when this routine is called. It used to be
        ** required that a statement transaction was not active, but this restriction
        ** has been removed (CREATE INDEX needs to move a page when a statement
        ** transaction is active).
        **
        ** If the fourth argument, isCommit, is non-zero, then this page is being
        ** moved as part of a database reorganization just before the transaction
        ** is being committed. In this case, it is guaranteed that the database page
        ** pPg refers to will not be written to again within this transaction.
        **
        ** This function may return SQLITE_NOMEM or an IO error code if an error
        ** occurs. Otherwise, it returns SQLITE_OK.
        */
        static int sqlite3PagerMovepage( Pager pPager, DbPage pPg, u32 pgno, int isCommit )
        {
            PgHdr pPgOld;                /* The page being overwritten. */
              u32 needSyncPgno = 0;        /* Old value of pPg.pgno, if sync is required */
              int rc;                      /* Return code */
              Pgno origPgno;               /* The original page number */

              Debug.Assert( pPg.nRef > 0 );
              Debug.Assert( pPager.eState == PAGER_WRITER_CACHEMOD
              || pPager.eState == PAGER_WRITER_DBMOD
              );
              Debug.Assert( assert_pager_state( pPager ) );

              /* In order to be able to rollback, an in-memory database must journal
              ** the page we are moving from.
              */
              if (
            #if SQLITE_OMIT_MEMORYDB
            1==MEMDB
            #else
             pPager.memDb != 0
            #endif
             )
              {
            rc = sqlite3PagerWrite( pPg );
            if ( rc != 0 )
              return rc;
              }

              /* If the page being moved is dirty and has not been saved by the latest
              ** savepoint, then save the current contents of the page into the
              ** sub-journal now. This is required to handle the following scenario:
              **
              **   BEGIN;
              **     <journal page X, then modify it in memory>
              **     SAVEPOINT one;
              **       <Move page X to location Y>
              **     ROLLBACK TO one;
              **
              ** If page X were not written to the sub-journal here, it would not
              ** be possible to restore its contents when the "ROLLBACK TO one"
              ** statement were is processed.
              **
              ** subjournalPage() may need to allocate space to store pPg.pgno into
              ** one or more savepoint bitvecs. This is the reason this function
              ** may return SQLITE_NOMEM.
              */
              if ( ( pPg.flags & PGHDR_DIRTY ) != 0
              && subjRequiresPage( pPg )
              && SQLITE_OK != ( rc = subjournalPage( pPg ) )
              )
              {
            return rc;
              }

              PAGERTRACE( "MOVE %d page %d (needSync=%d) moves to %d\n",
              PAGERID( pPager ), pPg.pgno, ( pPg.flags & PGHDR_NEED_SYNC ) != 0 ? 1 : 0, pgno );
              IOTRACE( "MOVE %p %d %d\n", pPager, pPg.pgno, pgno );

              /* If the journal needs to be sync()ed before page pPg.pgno can
              ** be written to, store pPg.pgno in local variable needSyncPgno.
              **
              ** If the isCommit flag is set, there is no need to remember that
              ** the journal needs to be sync()ed before database page pPg.pgno
              ** can be written to. The caller has already promised not to write to it.
              */
              if ( ( ( pPg.flags & PGHDR_NEED_SYNC ) != 0 ) && 0 == isCommit )
              {
            needSyncPgno = pPg.pgno;
            Debug.Assert( pageInJournal( pPg ) || pPg.pgno > pPager.dbOrigSize );
            Debug.Assert( ( pPg.flags & PGHDR_DIRTY ) != 0 );
              }

              /* If the cache contains a page with page-number pgno, remove it
              ** from its hash chain. Also, if the PGHDR_NEED_SYNC was set for
              ** page pgno before the 'move' operation, it needs to be retained
              ** for the page moved there.
              */
              pPg.flags &= ~PGHDR_NEED_SYNC;
              pPgOld = pager_lookup( pPager, pgno );
              Debug.Assert( null == pPgOld || pPgOld.nRef == 1 );
              if ( pPgOld != null )
              {
            pPg.flags |= ( pPgOld.flags & PGHDR_NEED_SYNC );
            if (
            #if SQLITE_OMIT_MEMORYDB
            1==MEMDB
            #else
             pPager.memDb != 0
            #endif
             )
            {
              /* Do not discard pages from an in-memory database since we might
              ** need to rollback later.  Just move the page out of the way. */
              sqlite3PcacheMove( pPgOld, pPager.dbSize + 1 );
            }
            else
            {
              sqlite3PcacheDrop( pPgOld );
            }
              }
              origPgno = pPg.pgno;
              sqlite3PcacheMove( pPg, pgno );
              sqlite3PcacheMakeDirty( pPg );

              /* For an in-memory database, make sure the original page continues
              ** to exist, in case the transaction needs to roll back.  Use pPgOld
              ** as the original page since it has already been allocated.
              */
              if (
            #if SQLITE_OMIT_MEMORYDB
            0!=MEMDB
            #else
             0 != pPager.memDb
            #endif
             )
              {
            Debug.Assert( pPgOld );
            sqlite3PcacheMove( pPgOld, origPgno );
            sqlite3PagerUnref( pPgOld );
              }

              if ( needSyncPgno != 0 )
              {
            /* If needSyncPgno is non-zero, then the journal file needs to be
            ** sync()ed before any data is written to database file page needSyncPgno.
            ** Currently, no such page exists in the page-cache and the
            ** "is journaled" bitvec flag has been set. This needs to be remedied by
            ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
            ** flag.
            **
            ** If the attempt to load the page into the page-cache fails, (due
            ** to a malloc() or IO failure), clear the bit in the pInJournal[]
            ** array. Otherwise, if the page is loaded and written again in
            ** this transaction, it may be written to the database file before
            ** it is synced into the journal file. This way, it may end up in
            ** the journal file twice, but that is not a problem.
            */
            PgHdr pPgHdr = null;
            rc = sqlite3PagerGet( pPager, needSyncPgno, ref pPgHdr );
            if ( rc != SQLITE_OK )
            {
              if ( needSyncPgno <= pPager.dbOrigSize )
              {
            Debug.Assert( pPager.pTmpSpace != null );
            u32[] pTemp = new u32[pPager.pTmpSpace.Length];
            sqlite3BitvecClear( pPager.pInJournal, needSyncPgno, pTemp );//pPager.pTmpSpace );
              }
              return rc;
            }
            pPgHdr.flags |= PGHDR_NEED_SYNC;
            sqlite3PcacheMakeDirty( pPgHdr );
            sqlite3PagerUnref( pPgHdr );
              }
              return SQLITE_OK;
        }
 /*
 ** Dummy page reinitializer
 */
 static void pager_test_reiniter(DbPage pNotUsed)
 {
     return;
 }
Exemple #4
0
 /*
 ** Release a page reference.
 **
 ** If the number of references to the page drop to zero, then the
 ** page is added to the LRU list.  When all references to all pages
 ** are released, a rollback occurs and the lock on the database is
 ** removed.
 */
 static void sqlite3PagerUnref( DbPage pPg )
 {
     if ( pPg != null )
       {
     Pager pPager = pPg.pPager;
     sqlite3PcacheRelease( pPg );
     pagerUnlockIfUnused( pPager );
       }
 }
Exemple #5
0
        /*
        ** Mark a data page as writeable. This routine must be called before
        ** making changes to a page. The caller must check the return value
        ** of this function and be careful not to change any page data unless
        ** this routine returns SQLITE_OK.
        **
        ** The difference between this function and pager_write() is that this
        ** function also deals with the special case where 2 or more pages
        ** fit on a single disk sector. In this case all co-resident pages
        ** must have been written to the journal file before returning.
        **
        ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
        ** as appropriate. Otherwise, SQLITE_OK.
        */
        static int sqlite3PagerWrite( DbPage pDbPage )
        {
            int rc = SQLITE_OK;

              PgHdr pPg = pDbPage;
              Pager pPager = pPg.pPager;
              u32 nPagePerSector = (u32)( pPager.sectorSize / pPager.pageSize );

              Debug.Assert( pPager.eState >= PAGER_WRITER_LOCKED );
              Debug.Assert( pPager.eState != PAGER_ERROR );
              Debug.Assert( assert_pager_state( pPager ) );

              if ( nPagePerSector > 1 )
              {
            Pgno nPageCount = 0;     /* Total number of pages in database file */
            Pgno pg1;                /* First page of the sector pPg is located on. */
            Pgno nPage = 0;          /* Number of pages starting at pg1 to journal */
            int ii;                  /* Loop counter */
            bool needSync = false;   /* True if any page has PGHDR_NEED_SYNC */

            /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
            ** a journal header to be written between the pages journaled by
            ** this function.
            */
            Debug.Assert(
            #if SQLITE_OMIT_MEMORYDB
            0==MEMDB
            #else
             0 == pPager.memDb
            #endif
             );
            Debug.Assert( pPager.doNotSyncSpill == 0 );
            pPager.doNotSyncSpill++;

            /* This trick assumes that both the page-size and sector-size are
            ** an integer power of 2. It sets variable pg1 to the identifier
            ** of the first page of the sector pPg is located on.
            */
            pg1 = (u32)( ( pPg.pgno - 1 ) & ~( nPagePerSector - 1 ) ) + 1;

            nPageCount = pPager.dbSize;
            if ( pPg.pgno > nPageCount )
            {
              nPage = ( pPg.pgno - pg1 ) + 1;
            }
            else if ( ( pg1 + nPagePerSector - 1 ) > nPageCount )
            {
              nPage = nPageCount + 1 - pg1;
            }
            else
            {
              nPage = nPagePerSector;
            }
            Debug.Assert( nPage > 0 );
            Debug.Assert( pg1 <= pPg.pgno );
            Debug.Assert( ( pg1 + nPage ) > pPg.pgno );

            for ( ii = 0; ii < nPage && rc == SQLITE_OK; ii++ )
            {
              u32 pg = (u32)( pg1 + ii );
              PgHdr pPage = new PgHdr();
              if ( pg == pPg.pgno || sqlite3BitvecTest( pPager.pInJournal, pg ) == 0 )
              {
            if ( pg != ( ( PENDING_BYTE / ( pPager.pageSize ) ) + 1 ) ) //PAGER_MJ_PGNO(pPager))
            {
              rc = sqlite3PagerGet( pPager, pg, ref pPage );
              if ( rc == SQLITE_OK )
              {
                rc = pager_write( pPage );
                if ( ( pPage.flags & PGHDR_NEED_SYNC ) != 0 )
                {
                  needSync = true;
                }
                sqlite3PagerUnref( pPage );
              }
            }
              }
              else if ( ( pPage = pager_lookup( pPager, pg ) ) != null )
              {
            if ( ( pPage.flags & PGHDR_NEED_SYNC ) != 0 )
            {
              needSync = true;
            }
            sqlite3PagerUnref( pPage );
              }
            }

            /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
            ** starting at pg1, then it needs to be set for all of them. Because
            ** writing to any of these nPage pages may damage the others, the
            ** journal file must contain sync()ed copies of all of them
            ** before any of them can be written out to the database file.
            */
            if ( rc == SQLITE_OK && needSync )
            {
              Debug.Assert(
            #if SQLITE_OMIT_MEMORYDB
            0==MEMDB
            #else
             0 == pPager.memDb
            #endif
             );
              for ( ii = 0; ii < nPage; ii++ )
              {
            PgHdr pPage = pager_lookup( pPager, (u32)( pg1 + ii ) );
            if ( pPage != null )
            {
              pPage.flags |= PGHDR_NEED_SYNC;
              sqlite3PagerUnref( pPage );
            }
              }
            }

            Debug.Assert( pPager.doNotSyncSpill == 1 );
            pPager.doNotSyncSpill--;
              }
              else
              {
            rc = pager_write( pDbPage );
              }
              return rc;
        }
Exemple #6
0
 /*
 ** Increment the reference count for page pPg.
 */
 static void sqlite3PagerRef( DbPage pPg )
 {
     sqlite3PcacheRef( pPg );
 }
Exemple #7
0
 static Pgno sqlite3PagerPagenumber( DbPage pPg )
 {
     return pPg.pgno;
 }
Exemple #8
0
 /*
 ** Return a pointer to the Pager.nExtra bytes of "extra" space
 ** allocated along with the specified page.
 */
 static MemPage sqlite3PagerGetExtra( DbPage pPg )
 {
     return pPg.pExtra;
 }
Exemple #9
0
 /*
 ** Return the number of references to the specified page.
 */
 static int sqlite3PagerPageRefcount( DbPage pPage )
 {
     return sqlite3PcachePageRefcount( pPage );
 }
Exemple #10
0
 /*
 ** Return a pointer to the data for the specified page.
 */
 static byte[] sqlite3PagerGetData( DbPage pPg )
 {
     Debug.Assert( pPg.nRef > 0 || pPg.pPager.memDb != 0 );
       return pPg.pData;
 }
Exemple #11
0
 /*
 ** Acquire a reference to page number pgno in pager pPager (a page
 ** reference has type DbPage*). If the requested reference is
 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
 **
 ** If the requested page is already in the cache, it is returned.
 ** Otherwise, a new page object is allocated and populated with data
 ** read from the database file. In some cases, the pcache module may
 ** choose not to allocate a new page object and may reuse an existing
 ** object with no outstanding references.
 **
 ** The extra data appended to a page is always initialized to zeros the
 ** first time a page is loaded into memory. If the page requested is
 ** already in the cache when this function is called, then the extra
 ** data is left as it was when the page object was last used.
 **
 ** If the database image is smaller than the requested page or if a
 ** non-zero value is passed as the noContent parameter and the
 ** requested page is not already stored in the cache, then no
 ** actual disk read occurs. In this case the memory image of the
 ** page is initialized to all zeros.
 **
 ** If noContent is true, it means that we do not care about the contents
 ** of the page. This occurs in two seperate scenarios:
 **
 **   a) When reading a free-list leaf page from the database, and
 **
 **   b) When a savepoint is being rolled back and we need to load
 **      a new page into the cache to be filled with the data read
 **      from the savepoint journal.
 **
 ** If noContent is true, then the data returned is zeroed instead of
 ** being read from the database. Additionally, the bits corresponding
 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
 ** savepoints are set. This means if the page is made writable at any
 ** point in the future, using a call to sqlite3PagerWrite(), its contents
 ** will not be journaled. This saves IO.
 **
 ** The acquisition might fail for several reasons.  In all cases,
 ** an appropriate error code is returned and *ppPage is set to NULL.
 **
 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
 ** to find a page in the in-memory cache first.  If the page is not already
 ** in memory, this routine goes to disk to read it in whereas Lookup()
 ** just returns 0.  This routine acquires a read-lock the first time it
 ** has to go to disk, and could also playback an old journal if necessary.
 ** Since Lookup() never goes to disk, it never has to deal with locks
 ** or journal files.
 */
 // Under C# from the header file
 //#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
 static int sqlite3PagerGet(
     Pager pPager,       /* The pager open on the database file */
     u32 pgno,          /* Page number to fetch */
     ref DbPage ppPage   /* Write a pointer to the page here */
     )
 {
     return sqlite3PagerAcquire( pPager, pgno, ref ppPage, 0 );
 }
Exemple #12
0
        static int sqlite3PagerAcquire(
            Pager pPager,      /* The pager open on the database file */
            u32 pgno,          /* Page number to fetch */
            ref DbPage ppPage, /* Write a pointer to the page here */
            u8 noContent     /* Do not bother reading content from disk if true */
            )
        {
            int rc;
              PgHdr pPg = null;

              Debug.Assert( pPager.eState >= PAGER_READER );
              Debug.Assert( assert_pager_state( pPager ) );
              if ( pgno == 0 )
              {
            return SQLITE_CORRUPT_BKPT();
              }

              /* If the pager is in the error state, return an error immediately.
              ** Otherwise, request the page from the PCache layer. */
              if ( pPager.errCode != SQLITE_OK )
              {
            rc = pPager.errCode;
              }
              else
              {
            rc = sqlite3PcacheFetch( pPager.pPCache, pgno, 1, ref  ppPage );
              }

              if ( rc != SQLITE_OK )
              {
            /* Either the call to sqlite3PcacheFetch() returned an error or the
            ** pager was already in the error-state when this function was called.
            ** Set pPg to 0 and jump to the exception handler.  */
            pPg = null;
            goto pager_acquire_err;
              }
              Debug.Assert( ( ppPage ).pgno == pgno );
              Debug.Assert( ( ppPage ).pPager == pPager || ( ppPage ).pPager == null );

              if ( ( ppPage ).pPager != null && 0 == noContent )
              {
            /* In this case the pcache already contains an initialized copy of
            ** the page. Return without further ado.  */
            Debug.Assert( pgno <= PAGER_MAX_PGNO && pgno != PAGER_MJ_PGNO( pPager ) );
            PAGER_INCR( ref pPager.nHit );
            return SQLITE_OK;

              }
              else
              {
            /* The pager cache has created a new page. Its content needs to
            ** be initialized.  */
            #if SQLITE_TEST
            PAGER_INCR( ref pPager.nMiss );
            #endif
            pPg = ppPage;
            pPg.pPager = pPager;
            pPg.pExtra = new MemPage();//memset(pPg.pExtra, 0, pPager.nExtra);

            /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
            ** number greater than this, or the unused locking-page, is requested. */
            if ( pgno > PAGER_MAX_PGNO || pgno == PAGER_MJ_PGNO( pPager ) )
            {
              rc = SQLITE_CORRUPT_BKPT();
              goto pager_acquire_err;
            }
            if (
            #if SQLITE_OMIT_MEMORYDB
            1==MEMDB
            #else
             pPager.memDb != 0
            #endif
             || pPager.dbSize < pgno || noContent != 0 || !isOpen( pPager.fd ) )
            {
              if ( pgno > pPager.mxPgno )
              {
            rc = SQLITE_FULL;
            goto pager_acquire_err;
              }
              if ( noContent != 0 )
              {
            /* Failure to set the bits in the InJournal bit-vectors is benign.
            ** It merely means that we might do some extra work to journal a
            ** page that does not need to be journaled.  Nevertheless, be sure
            ** to test the case where a malloc error occurs while trying to set
            ** a bit in a bit vector.
            */
            sqlite3BeginBenignMalloc();
            if ( pgno <= pPager.dbOrigSize )
            {
            #if !NDEBUG || SQLITE_COVERAGE_TEST
              rc = sqlite3BitvecSet( pPager.pInJournal, pgno );          //TESTONLY( rc = ) sqlite3BitvecSet(pPager.pInJournal, pgno);
            #else
            sqlite3BitvecSet(pPager.pInJournal, pgno);
            #endif
              testcase( rc == SQLITE_NOMEM );
            }
            #if !NDEBUG || SQLITE_COVERAGE_TEST
            rc = addToSavepointBitvecs( pPager, pgno ); //TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
            #else
            addToSavepointBitvecs(pPager, pgno);
            #endif

            testcase( rc == SQLITE_NOMEM );
            sqlite3EndBenignMalloc();
              }
              //memset(pPg.pData, 0, pPager.pageSize);
              Array.Clear( pPg.pData, 0, pPager.pageSize );
              IOTRACE( "ZERO %p %d\n", pPager, pgno );
            }
            else
            {
              Debug.Assert( pPg.pPager == pPager );
              rc = readDbPage( pPg );
              if ( rc != SQLITE_OK )
              {
            goto pager_acquire_err;
              }
            }

            pager_set_pagehash( pPg );
              }
              return SQLITE_OK;

            pager_acquire_err:
              Debug.Assert( rc != SQLITE_OK );
              if ( pPg != null )
              {
            sqlite3PcacheDrop( pPg );
              }
              pagerUnlockIfUnused( pPager );

              ppPage = null;
              return rc;
        }
Exemple #13
0
 static bool sqlite3PagerIswriteable( DbPage pPg )
 {
     return true;
 }
Exemple #14
0
 /*
 ** Return TRUE if the page given in the argument was previously passed
 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
 ** to change the content of the page.
 */
 static bool sqlite3PagerIswriteable( DbPage pPg )
 {
     return ( pPg.flags & PGHDR_DIRTY ) != 0;
 }