/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This is the implementation of the page cache subsystem or "pager".
**
** The pager is used to access a database disk file. It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file. The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.101 2004/02/25 02:20:41 drh Exp $
*/
#include "os.h" /* Must be first to enable large file support */
#include "sqliteInt.h"
#include "pager.h"
#include <assert.h>
#include <string.h>
/*
** Macros for troubleshooting. Normally turned off
*/
#if 0
#else
#define SET_PAGER(X)
#define CLR_PAGER(X)
#define TRACE1(X)
#define TRACE2(X,Y)
#define TRACE3(X,Y,Z)
#endif
/*
** The page cache as a whole is always in one of the following
** states:
**
** SQLITE_UNLOCK The page cache is not currently reading or
** writing the database file. There is no
** data held in memory. This is the initial
** state.
**
** SQLITE_READLOCK The page cache is reading the database.
** Writing is not permitted. There can be
** multiple readers accessing the same database
** file at the same time.
**
** SQLITE_WRITELOCK The page cache is writing the database.
** Access is exclusive. No other processes or
** threads can be reading or writing while one
** process is writing.
**
** The page cache comes up in SQLITE_UNLOCK. The first time a
** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
** After all pages have been released using sqlite_page_unref(),
** the state transitions back to SQLITE_UNLOCK. The first time
** that sqlite_page_write() is called, the state transitions to
** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
** called on an outstanding page which means that the pager must
** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
** The sqlite_page_rollback() and sqlite_page_commit() functions
** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
*/
#define SQLITE_UNLOCK 0
/*
** Each in-memory image of a page begins with the following header.
** This header is only visible to this pager module. The client
** code that calls pager sees only the data that follows the header.
**
** Client code should call sqlitepager_write() on a page prior to making
** any modifications to that page. The first time sqlitepager_write()
** is called, the original page contents are written into the rollback
** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
** the journal page has made it onto the disk surface, PgHdr.needSync
** is cleared. The modified page cannot be written back into the original
** database file until the journal pages has been synced to disk and the
** PgHdr.needSync has been cleared.
**
** The PgHdr.dirty flag is set when sqlitepager_write() is called and
** is cleared again when the page content is written back to the original
** database file.
*/
struct PgHdr {
/* SQLITE_PAGE_SIZE bytes of page data follow this header */
/* Pager.nExtra bytes of local data follow the page data */
};
/*
** A macro used for invoking the codec if there is one
*/
#ifdef SQLITE_HAS_CODEC
#else
# define CODEC(P,D,N,X)
#endif
/*
** Convert a pointer to a PgHdr into a pointer to its data
** and back again.
*/
/*
** How big to make the hash table used for locating in-memory pages
** by page number.
*/
/*
** Hash a page number
*/
/*
** A open page cache is an instance of the following structure.
*/
struct Pager {
};
/*
** These are bits that can be set in Pager.errMask.
*/
/*
** The journal file contains page records in the following
** format.
**
** Actually, this structure is the complete page record for pager
** formats less than 3. Beginning with format 3, this record is surrounded
** by two checksums.
*/
struct PageRecord {
};
/*
** Journal files begin with the following magic string. The data
**
** There are three journal formats (so far). The 1st journal format writes
** 32-bit integers in the byte-order of the host machine. New
** formats writes integers as big-endian. All new journals use the
** new format, but we have to be able to read an older journal in order
** to rollback journals created by older versions of the library.
**
** The 3rd journal format (added for 2.8.0) adds additional sanity
** checking information to the journal. If the power fails while the
** journal is being written, semi-random garbage data might appear in
** the journal file after power is restored. If an attempt is then made
** to roll the journal back, the database could be corrupted. The additional
** sanity checking data is an attempt to discover the garbage in the
** journal and ignore it.
**
** The sanity checking information for the 3rd journal format consists
** of a 32-bit checksum on each page of data. The checksum covers both
** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
** This cksum is initialized to a 32-bit random value that appears in the
** journal file right after the header. The random initializer is important,
** because garbage data that appears at the end of a journal is likely
** data that was once in other files that have now been deleted. If the
** garbage data came from an obsolete journal file, the checksums might
** be correct. But by initializing the checksum to random value which
** is different for every journal, we minimize that risk.
*/
static const unsigned char aJournalMagic1[] = {
0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
};
static const unsigned char aJournalMagic2[] = {
0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
};
static const unsigned char aJournalMagic3[] = {
0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
};
/*
** The following integer determines what format to use when creating
** new primary journal files. By default we always use format 3.
** When testing, we can set this value to older journal formats in order to
** make sure that newer versions of the library are able to rollback older
** journal files.
**
** Note that checkpoint journals always use format 2 and omit the header.
*/
#ifdef SQLITE_TEST
#else
#endif
/*
** The size of the header and of each page in the journal varies according
** to which journal format is being used. The following macros figure out
** the sizes based on format numbers.
*/
#define JOURNAL_HDR_SZ(X) \
#define JOURNAL_PG_SZ(X) \
/*
** Enable reference count tracking here:
*/
#ifdef SQLITE_TEST
int pager_refinfo_enable = 0;
static int cnt = 0;
if( !pager_refinfo_enable ) return;
"REFCNT: %4d addr=0x%08x nRef=%d\n",
);
cnt++; /* Something to set a breakpoint on */
}
#else
# define REFINFO(X)
#endif
/*
** Read a 32-bit integer from the given file descriptor. Store the integer
** that is read in *pRes. Return SQLITE_OK if everything worked, or an
** error code is something goes wrong.
**
** If the journal format is 2 or 3, read a big-endian integer. If the
** journal format is 1, read an integer in the native byte-order of the
** host machine.
*/
int rc;
}
return rc;
}
/*
** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
** on success or an error code is something goes wrong.
**
** If the journal format is 2 or 3, write the integer as 4 big-endian
** bytes. If the journal format is 1, write the integer in the native
** byte order. In normal operation, only formats 2 and 3 are used.
** Journal format 1 is only used for testing.
*/
if( journal_format<=1 ){
}
}
/*
** Write a 32-bit integer into a page header right before the
** page data. This will overwrite the PgHdr.pDirty pointer.
**
** The integer is big-endian for formats 2 and 3 and native byte order
** for journal format 1.
*/
unsigned char *ac;
if( journal_format<=1 ){
}else{
}
}
/*
** Convert the bits in the pPager->errMask into an approprate
** return code.
*/
return rc;
}
/*
** Add or remove a page from the list of all pages that are in the
** checkpoint journal.
**
** The Pager keeps a separate list of pages that are currently in
** the checkpoint journal. This helps the sqlitepager_ckpt_commit()
** routine run MUCH faster for the common case where there are many
** pages in memory but only a few are in the checkpoint journal.
*/
}
}
}else{
}
}
}
/*
** Find a page in the hash table given its page number. Return
** a pointer to the page or NULL if not found.
*/
p = p->pNextHash;
}
return p;
}
/*
** Unlock the database and clear the in-memory cache. This routine
** sets the state of the pager back to what it was when it was first
** opened. Any outstanding pages are invalidated and subsequent attempts
** to access those pages will likely result in a coredump.
*/
}
pPager->pFirstSynced = 0;
}
}
/*
** When this routine is called, the pager has the journal file open and
** a write lock on the database. This routine releases the database
** write lock and acquires a read lock in its place. The journal file
** is deleted and closed.
**
** TODO: Consider keeping the journal file open for temporary databases.
** This might give a performance improvement on windows where opening
** a file is an expensive operation.
*/
int rc;
}
if( pPager->journalOpen ){
pPager->journalOpen = 0;
pPager->aInJournal = 0;
}
}else{
}
}else{
/* This can only happen if a process does a BEGIN, then forks and the
** child process does the COMMIT. Because of the semantics of unix
** file locking, the unlock will fail.
*/
}
return rc;
}
/*
** Compute and return a checksum for the page of data.
**
** This is not a real checksum. It is really just the sum of the
** random initial value and the page number. We considered do a checksum
** of the database, but that was found to be too slow.
*/
return cksum;
}
/*
** Read a single page from the journal file opened on file descriptor
** jfd. Playback this one page.
**
** There are three different journal formats. The format parameter determines
** which format is used by the journal that is played back.
*/
int rc;
/* Sanity checking on the page. This is more important that I originally
** thought. If a power failure occurs while the journal is being written,
** it could cause invalid data to be written into the journal. We need to
** detect this invalid data (with high probability) and ignore it.
*/
return SQLITE_DONE;
}
return SQLITE_OK;
}
if( format>=JOURNAL_FORMAT_3 ){
return SQLITE_DONE;
}
}
/* Playback the page. Update the in-memory copy of the page
** at the same time, if there is one.
*/
if( pPg ){
/* No page should ever be rolled back that is in use, except for page
** 1 which is held in use in order to keep the lock on the database
** active. However, such a page may be rolled back as a result of an
** internal error resulting in an automatic call to
** sqlitepager_rollback(), so we can't assert() it.
*/
/* assert( pPg->nRef==0 || pPg->pgno==1 ) */
}
return rc;
}
/*
** Playback the journal and thus restore the database file to
** the state it was in before we started making changes.
**
** The journal file format is as follows:
**
** * 8 byte prefix. One of the aJournalMagic123 vectors defined
** above. The format of the journal file is determined by which
** of the three prefix vectors is seen.
** * 4 byte big-endian integer which is the number of valid page records
** in the journal. If this value is 0xffffffff, then compute the
** number of page records from the journal size. This field appears
** in format 3 only.
** * 4 byte big-endian integer which is the initial value for the
** sanity checksum. This field appears in format 3 only.
** * 4 byte integer which is the number of pages to truncate the
** database to during a rollback.
** * Zero or more pages instances, each as follows:
** + 4 byte page number.
** + SQLITE_PAGE_SIZE bytes of data.
** + 4 byte checksum (format 3 only)
**
** When we speak of the journal header, we mean the first 4 bullets above.
** Each entry in the journal is an instance of the 5th bullet. Note that
** bullets 2 and 3 only appear in format-3 journals.
**
** Call the value from the second bullet "nRec". nRec is the number of
** valid page entries in the journal. In most cases, you can compute the
** value of nRec from the size of the journal file. But if a power
** failure occurred while the journal was being written, it could be the
** case that the size of the journal file had already been increased but
** the extra entries had not yet made it safely to disk. In such a case,
** the value of nRec computed from the file size would be too large. For
** that reason, we always use the nRec value in the header.
**
** If the nRec value is 0xffffffff it means that nRec should be computed
** from the file size. This value is used when the user selects the
** no-sync option for the journal. A power failure could lead to corruption
** in this case. But for things like temporary table (which will be
** deleted when the power is restored) we don't care.
**
** Journal formats 1 and 2 do not have an nRec value in the header so we
** have to compute nRec from the file size. This has risks (as described
** above) which is why all persistent tables have been changed to use
** format 3.
**
** If the file opened as the journal file is not a well-formed
** journal file then the database will likely already be
** corrupted, so the PAGER_ERR_CORRUPT bit is set in pPager->errMask
** and SQLITE_CORRUPT is returned. If it all works, then this routine
** returns SQLITE_OK.
*/
int i; /* Loop counter */
int rc;
/* Figure out how many records are in the journal. Abort early if
** the journal is empty.
*/
goto end_playback;
}
/* If the journal file is too small to contain a complete header,
** it must mean that the process that created the journal was just
** beginning to write the journal file when it died. In that case,
** the database file should have still been completely unchanged.
** Nothing needs to be rolled back. We can safely ignore this journal.
*/
goto end_playback;
}
/* Read the beginning of the journal and truncate the
** database file back to its original size.
*/
goto end_playback;
}
}else{
goto end_playback;
}
if( format>=JOURNAL_FORMAT_3 ){
/* Ignore the journal if it is too small to contain a complete
** header. We already did this test once above, but at the prior
** test, we did not know the journal format and so we had to assume
** the smallest possible header. Now we know the header is bigger
** than the minimum so we test again.
*/
goto end_playback;
}
if( rc ) goto end_playback;
if( rc ) goto end_playback;
}
}else{
}
goto end_playback;
}
goto end_playback;
}
/* Copy original pages out of the journal and back into the database file.
*/
for(i=0; i<nRec; i++){
if( rc==SQLITE_DONE ){
}
break;
}
}
/* Pages that have been written to the journal but never synced
** where not restored by the loop above. We have to restore those
** pages by reading them back from the original database.
*/
if( rc ) break;
}else{
}
}
}
}
rc = SQLITE_CORRUPT;
}else{
}
return rc;
}
/*
** Playback the checkpoint journal.
**
** This is similar to playing back the transaction journal but with
** a few extra twists.
**
** (1) The number of pages in the database file at the start of
** the checkpoint is stored in pPager->ckptSize, not in the
** journal file itself.
**
** (2) In addition to playing back the checkpoint journal, also
** playback all pages of the transaction journal beginning
** at offset pPager->ckptJSize.
*/
int i; /* Loop counter */
int rc;
/* Truncate the database back to its original size.
*/
/* Figure out how many records are in the checkpoint journal.
*/
/* Copy original pages out of the checkpoint journal and back into the
** database file. Note that the checkpoint journal always uses format
** 2 instead of format 3 since it does not need to be concerned with
** power failures corrupting the journal and can thus omit the checksums.
*/
for(i=nRec-1; i>=0; i--){
}
/* Figure out how many pages need to be copied out of the transaction
** journal.
*/
goto end_ckpt_playback;
}
goto end_ckpt_playback;
}
for(i=nRec-1; i>=0; i--){
goto end_ckpt_playback;
}
}
rc = SQLITE_CORRUPT;
}
return rc;
}
/*
** Change the maximum number of in-memory pages that are allowed.
**
** The maximum number is the absolute value of the mxPage parameter.
** If mxPage is negative, the noSync flag is also set. noSync bypasses
** calls to sqliteOsSync(). The pager runs much faster with noSync on,
** but if the operating system crashes or there is an abrupt power
** failure, the database file might be left in an inconsistent and
** unrepairable state.
*/
if( mxPage>=0 ){
}else{
}
if( mxPage>10 ){
}
}
/*
** Adjust the robustness of the database to damage due to OS crashes
** or power failures by changing the number of syncs()s when writing
** the rollback journal. There are three levels:
**
** OFF sqliteOsSync() is never called. This is the default
** for temporary and transient files.
**
** NORMAL The journal is synced once before writes begin on the
** database. This is normally adequate protection, but
** it is theoretically possible, though very unlikely,
** that an inopertune power failure could leave the journal
** in a state which would cause damage to the database
** when it is rolled back.
**
** FULL The journal is synced twice before writes begin on the
** database (with some additional information - the nRec field
** of the journal header - being written in between the two
** syncs). If we assume that writing a
** single disk sector is atomic, then this mode provides
** assurance that the journal will not be corrupted to the
** point of causing damage to the database during rollback.
**
** Numeric values associated with these states are OFF==1, NORMAL=2,
** and FULL=3.
*/
}
/*
** Open a temporary file. Write the name of the file into zName
** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
** the file descriptor into *fd. Return SQLITE_OK on success or some
** other error code if we fail.
**
** The OS will automatically delete the temporary file when it is
** closed.
*/
int rc;
do{
cnt--;
return rc;
}
/*
** Create a new page cache and put a pointer to the page cache in *ppPager.
** The file to be cached need not exist. The file is not locked until
** the first call to sqlitepager_get() and is only held open until the
** last page is released using sqlitepager_unref().
**
** If zFilename is NULL then a randomly-named temporary file is created
** and used as the file to be cached. The file will be deleted
** automatically when it is closed.
*/
int sqlitepager_open(
const char *zFilename, /* Name of the database file to open */
int mxPage, /* Max number of in-memory cache pages */
int nExtra, /* Extra bytes append to each in-memory page */
int useJournal /* TRUE to use a rollback journal on this file */
){
char *zFullPathname;
int nameLen;
int rc, i;
int tempFile;
int readOnly = 0;
*ppPager = 0;
if( sqlite_malloc_failed ){
return SQLITE_NOMEM;
}
tempFile = 0;
}else{
tempFile = 1;
}
if( sqlite_malloc_failed ){
return SQLITE_NOMEM;
}
return SQLITE_CANTOPEN;
}
if( pPager==0 ){
sqliteOsClose(&fd);
return SQLITE_NOMEM;
}
pPager->journalOpen = 0;
pPager->pFirstSynced = 0;
return SQLITE_OK;
}
/*
** Set the destructor for this pager. If not NULL, the destructor is called
** when the reference count on each page reaches zero. The destructor can
** be used to clean up information in the extra segment appended to each page.
**
** The destructor is not called as a result sqlitepager_close().
** Destructors are only called by sqlitepager_unref().
*/
}
/*
** Return the total number of pages in the disk file associated with
** pPager.
*/
off_t n;
}
return 0;
}
n /= SQLITE_PAGE_SIZE;
}
return n;
}
/*
** Forward declaration
*/
static int syncJournal(Pager*);
/*
** Truncate the file to the number of pages specified.
*/
int rc;
}
return rc;
}
return SQLITE_OK;
}
}
return rc;
}
/*
** Shutdown the page cache. Free all memory and close all files.
**
** If a transaction was in progress when this routine is called, that
** transaction is rolled back. All outstanding pages are invalidated
** and their memory is freed. Any attempt to use a page associated
** with this page cache after this function returns will likely
** result in a coredump.
*/
case SQLITE_WRITELOCK: {
break;
}
case SQLITE_READLOCK: {
break;
}
default: {
/* Do nothing */
break;
}
}
}
/* Temp files are automatically deleted by the OS
** if( pPager->tempFile ){
** sqliteOsDelete(pPager->zFilename);
** }
*/
assert( 0 ); /* Cannot happen */
}
return SQLITE_OK;
}
/*
** Return the page number for the given page data.
*/
return p->pgno;
}
/*
** Increment the reference count for a page. If the page is
** currently on the freelist (the reference count is zero) then
** remove it from the freelist.
*/
/* The page is currently on the freelist. Remove it. */
}
}else{
}
}else{
}
}
}
/*
** Increment the reference count for a page. The input pointer is
** a reference to the page data.
*/
return SQLITE_OK;
}
/*
** Sync the journal. In other words, make sure all the pages that have
** been written to the journal have actually reached the surface of the
** disk. It is not safe to modify the original database file until after
** the journal has been synced. If the original database is modified before
** the journal is synced and a power failure occurs, the unsynced journal
** data would be lost and we would be unable to completely rollback the
** database changes. Database corruption would occur.
**
** This routine also updates the nRec field in the header of the journal.
** (See comments on the pager_playback() routine for additional information.)
** If the sync mode is FULL, two syncs will occur. First the whole journal
** is synced, then the nRec field is updated, then a second sync occurs.
**
** For temporary databases, we do not care if we are able to rollback
** after a power failure, so sync occurs.
**
** This routine clears the needSync field of every page current held in
** memory.
*/
/* Sync the journal before modifying the main database
** (assuming there is a journal and it needs to be synced.)
*/
/* assert( !pPager->noSync ); // noSync might be set if synchronous
** was turned off after the transaction was started. Ticket #615 */
#ifndef NDEBUG
{
/* Make sure the pPager->nRec counter we are keeping agrees
** with the nRec computed from the size of the journal file.
*/
}
#endif
if( journal_format>=3 ){
/* Write the nRec value into the journal file header */
TRACE1("SYNC\n");
}
}
TRACE1("SYNC\n");
}
/* Erase the needSync flag from every page.
*/
}
}
#ifndef NDEBUG
/* If the Pager.needSync flag is clear then the PgHdr.needSync
** flag must also be clear for all pages. Verify that this
** invariant is true.
*/
else{
}
}
#endif
return rc;
}
/*
** Given a list of pages (connected by the PgHdr.pDirty pointer) write
** every one of those pages out to the database file and mark them all
** as clean.
*/
int rc;
while( pList ){
}
return SQLITE_OK;
}
/*
** Collect every dirty page into a dirty list and
** return a pointer to the head of that list. All pages are
** collected even if they are still in use.
*/
pList = 0;
if( p->dirty ){
pList = p;
}
}
return pList;
}
/*
** Acquire a page.
**
** A read lock on the disk file is obtained when the first page is acquired.
** This read lock is dropped when the last page is released.
**
** A _get works for any page number greater than 0. If the database
** file is smaller than the requested page, then no actual disk
** read occurs and the memory image of the page is initialized to
** all zeros. The extra data appended to a page is always initialized
** to zeros the first time a page is loaded into memory.
**
** The acquisition might fail for several reasons. In all cases,
** an appropriate error code is returned and *ppPage is set to NULL.
**
** See also sqlitepager_lookup(). 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.
*/
int rc;
/* Make sure we have not hit any critical errors.
*/
*ppPage = 0;
return pager_errcode(pPager);
}
/* If this is the first page accessed, then get a read lock
** on the database file.
*/
return rc;
}
/* If a journal file exists, try to play it back.
*/
int rc;
/* Get a write lock on the database
*/
/* This should never happen! */
}
return rc;
}
/* Open the journal for reading only. Return SQLITE_BUSY if
** we are unable to open the journal file.
**
** The journal file does not need to be locked itself. The
** journal file is never open unless the main database file holds
** a write lock, so there is never any chance of two or more
** processes opening the journal at the same time.
*/
return SQLITE_BUSY;
}
pPager->journalStarted = 0;
/* Playback and delete the journal. Drop the database write
** lock and reacquire the read lock.
*/
return rc;
}
}
pPg = 0;
}else{
/* Search for page in cache */
}
if( pPg==0 ){
/* The requested page is not in the page cache. */
int h;
/* Create a new page */
if( pPg==0 ){
return SQLITE_NOMEM;
}
}
}else{
/* Find a page to recycle. Try to locate a page that does not
** require us to do an fsync() on the journal.
*/
/* If we could not find a page that does not require an fsync()
** on the journal file then fsync the journal file. This is a
** very slow operation, so we work hard to avoid it. But sometimes
** it can't be helped.
*/
if( pPg==0 ){
if( rc!=0 ){
return SQLITE_IOERR;
}
}
/* Write the page to the database file if it is dirty.
*/
return SQLITE_IOERR;
}
}
/* If the page we are recycling is marked as alwaysRollback, then
** set the global alwaysRollback flag, thus disabling the
** sqlite_dont_rollback() optimization for the rest of this transaction.
** It is necessary to do this because the page marked alwaysRollback
** might be reloaded at a later time but at that point we won't remember
** that is was marked alwaysRollback. This means that all pages must
** be marked as alwaysRollback from here on out.
*/
if( pPg->alwaysRollback ){
}
/* Unlink the old page from the free list and the hash table
*/
pPager->pFirstSynced = p;
}
}else{
}
}else{
}
}
}else{
}
}
}else{
}
}else{
}
h = pager_hash(pgno);
}
}
return rc;
}
}else{
int rc;
return rc;
}else{
}
}
}
}else{
/* The requested page is in the page cache. */
}
return SQLITE_OK;
}
/*
** Acquire a page if it is already in the in-memory cache. Do
** not read the page from disk. Return a pointer to the page,
** or 0 if the page is not in cache.
**
** See also sqlitepager_get(). The difference between this routine
** and sqlitepager_get() is that _get() will go to the disk and read
** in the page if the page is not already in cache. This routine
** returns NULL if the page is not in cache or if a disk I/O error
** has ever happened.
*/
return 0;
}
/* if( pPager->nRef==0 ){
** return 0;
** }
*/
if( pPg==0 ) return 0;
return PGHDR_TO_DATA(pPg);
}
/*
** Release a page.
**
** 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.
*/
/* Decrement the reference count for this page
*/
/* When the number of references to a page reach 0, call the
** destructor and add the page to the freelist.
*/
}else{
}
}
if( pPager->xDestructor ){
}
/* When all pages reach the freelist, drop the read lock from
** the database file.
*/
}
}
return SQLITE_OK;
}
/*
** Create a journal file for pPager. There should already be a write
** lock on the database file when this routine is called.
**
** Return SQLITE_OK if everything. Return an error code and release the
** write lock if anything goes wrong.
*/
int rc;
if( pPager->aInJournal==0 ){
return SQLITE_NOMEM;
}
pPager->aInJournal = 0;
return SQLITE_CANTOPEN;
}
pPager->journalStarted = 0;
pPager->alwaysRollback = 0;
return rc;
}
if( journal_format==JOURNAL_FORMAT_3 ){
}
}
}else if( journal_format==JOURNAL_FORMAT_2 ){
}else{
}
}
}
rc = SQLITE_FULL;
}
}
return rc;
}
/*
** Acquire a write-lock on the database. The lock is removed when
** the any of the following happen:
**
** * sqlitepager_commit() is called.
** * sqlitepager_rollback() is called.
** * sqlitepager_close() is called.
** * sqlitepager_unref() is called to on every outstanding page.
**
** The parameter to this routine is a pointer to any open page of the
** database file. Nothing changes about the page - it is used merely
** to acquire a pointer to the Pager structure and as proof that there
** is already a read-lock on the database.
**
** A journal file is opened if this is not a temporary file. For
** temporary files, the opening of the journal file is deferred until
** there is an actual need to write to the journal.
**
** If the database is already write-locked, this routine is a no-op.
*/
return rc;
}
TRACE1("TRANSACTION\n");
}
}
return rc;
}
/*
** Mark a data page as writeable. The page is written into the journal
** if it is not there already. This routine must be called before making
** changes to a page.
**
** The first time this routine is called, the pager creates a new
** journal and acquires a write lock on the database. If the write
** lock could not be acquired, this routine returns SQLITE_BUSY. The
** calling routine must check for that return value and be careful not to
** change any page data until this routine returns SQLITE_OK.
**
** If the journal file could not be written because the disk is full,
** then this routine returns SQLITE_FULL and does an immediate rollback.
** All subsequent write attempts also return SQLITE_FULL until there
** is a call to sqlitepager_commit() or sqlitepager_rollback() to
** reset.
*/
/* Check for errors
*/
return pager_errcode(pPager);
}
return SQLITE_PERM;
}
/* Mark the page as dirty. If the page has already been written
** to the journal then we can return right away.
*/
return SQLITE_OK;
}
/* If we get this far, it means that the page needs to be
** written to the transaction journal or the ckeckpoint journal
** or both.
**
** First check to see that the transaction journal exists and
** create it if it does not.
*/
return rc;
}
}
/* The transaction journal now exists and we have a write lock on the
** main database file. Write the current page to the transaction
** journal if it is not there already.
*/
int szPg;
if( journal_format>=JOURNAL_FORMAT_3 ){
}else{
}
if( journal_format>=JOURNAL_FORMAT_3 ){
}
return rc;
}
}
}else{
}
}
}
/* If the checkpoint journal is open and the page is not in it,
** then write the current page to the checkpoint journal. Note that
** the checkpoint journal always uses the simplier format 2 that lacks
** checksums. The header is also omitted from the checkpoint journal.
*/
return rc;
}
}
/* Update the database size and return.
*/
}
return rc;
}
/*
** Return TRUE if the page given in the argument was previously passed
** to sqlitepager_write(). In other words, return TRUE if it is ok
** to change the content of the page.
*/
}
/*
** Replace the content of a single page with the information in the third
** argument.
*/
void *pPage;
int rc;
}
}
return rc;
}
/*
** A call to this routine tells the pager that it is not necessary to
** write the information on page "pgno" back to the disk, even though
** that page might be marked as dirty.
**
** The overlying software layer calls this routine when all of the data
** on the given page is unused. The pager marks the page as clean so
** that it does not get written to disk.
**
** Tests show that this optimization, together with the
** sqlitepager_dont_rollback() below, more than double the speed
** of large INSERT operations and quadruple the speed of large DELETEs.
**
** When this routine is called, set the alwaysRollback flag to true.
** Subsequent calls to sqlitepager_dont_rollback() for the same page
** will thereafter be ignored. This is necessary to avoid a problem
** where a page with data is added to the freelist during one part of
** a transaction then removed from the freelist during a later part
** of the same transaction and reused for some other purpose. When it
** is first added to the freelist, this routine is called. When reused,
** the dont_rollback() routine is called. But because the page contains
** critical data, we still need to be sure it gets rolled back in spite
** of the dont_rollback() call.
*/
/* If this pages is the last page in the file and the file has grown
** during the current transaction, then do NOT mark the page as clean.
** When the database file grows, we must make sure that the last page
** gets written at least once so that the disk file will be the correct
** size. If you do not write this page and the size of the file
** on the disk ends up being too small, that can lead to database
** corruption during the next transaction.
*/
}else{
}
}
}
/*
** A call to this routine tells the pager that if a rollback occurs,
** it is not necessary to restore the data on the given page. This
** means that the pager does not have to record the given page in the
** rollback journal.
*/
}
}
}
}
/*
** Commit all changes to the database and release the write lock.
**
** If the commit fails for any reason, a rollback attempt is made
** and an error code is returned. If the commit worked, SQLITE_OK
** is returned.
*/
int rc;
rc = SQLITE_FULL;
}
return rc;
}
return rc;
}
return SQLITE_ERROR;
}
TRACE1("COMMIT\n");
/* Exit early (without doing the time-consuming sqliteOsSync() calls)
** if there have been no changes to the database file. */
return rc;
}
goto commit_abort;
}
if( pPg ){
goto commit_abort;
}
}
return rc;
/* Jump here if anything goes wrong during the commit process.
*/
rc = SQLITE_FULL;
}
return rc;
}
/*
** Rollback all changes. The database falls back to read-only mode.
** All in-memory cache pages revert to their original data contents.
** The journal is deleted.
**
** This routine cannot fail unless some other process is not following
** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
** process is writing trash into the journal file (SQLITE_CORRUPT) or
** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
** codes are returned for all these occasions. Otherwise,
** SQLITE_OK is returned.
*/
int rc;
TRACE1("ROLLBACK\n");
return rc;
}
}
return pager_errcode(pPager);
}
return SQLITE_OK;
}
rc = SQLITE_CORRUPT;
}
return rc;
}
/*
** Return TRUE if the database file is opened read-only. Return FALSE
** if the database is (in theory) writable.
*/
}
/*
** This routine is used for testing and analysis only.
*/
static int a[9];
return a;
}
/*
** Set the checkpoint.
**
** This routine should be called with the transaction journal already
** open. A new checkpoint journal is created that can be used to rollback
** changes of a single SQL command within a larger transaction.
*/
int rc;
if( !pPager->journalOpen ){
return SQLITE_OK;
}
return SQLITE_NOMEM;
}
#ifndef NDEBUG
if( rc ) goto ckpt_begin_failed;
#endif
if( rc ) goto ckpt_begin_failed;
}
return SQLITE_OK;
}
return rc;
}
/*
** Commit a checkpoint.
*/
/* sqliteOsTruncate(&pPager->cpfd, 0); */
}
}
pPager->ckptAutoopen = 0;
return SQLITE_OK;
}
/*
** Rollback a checkpoint.
*/
int rc;
}else{
}
pPager->ckptAutoopen = 0;
return rc;
}
/*
** Return the full pathname of the database file.
*/
}
/*
** Set the codec for this pager
*/
void sqlitepager_set_codec(
void *pCodecArg
){
}
#ifdef SQLITE_TEST
/*
** Print a listing of all referenced pages and their ref count.
*/
printf("PAGE %3d addr=0x%08x nRef=%d\n",
}
}
#endif