Home | History | Annotate | Download | only in dist

Lines Matching refs:pPager

8945 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
8963 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
8965 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
8982 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
8985 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
8986 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
8987 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
8989 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
8990 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
8991 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
8992 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
8993 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
8995 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
9069 Pager *pPager; /* The pager this page is part of */
13434 Pager *pPager = sqlite3BtreePager(pBt);
13435 totalUsed += sqlite3PagerMemUsed(pPager);
13521 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
13522 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
39176 ** the page number and the pPager->pageSize bytes of data for the page.
39193 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
39199 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
39210 # define MEMDB pPager->memDb
39224 ** if( isOpen(pPager->jfd) ){ ...
39228 ** if( pPager->jfd->pMethods ){ ...
39237 static int pagerUseWal(Pager *pPager){
39238 return (pPager->pWal!=0);
39252 ** assert( assert_pager_state(pPager) );
39258 Pager *pPager = p;
39275 assert( p->tempFile==0 || pPager->changeCountDone );
39303 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
39309 assert( pPager->errCode==SQLITE_OK );
39310 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
39314 assert( pPager->errCode==SQLITE_OK );
39321 assert( pPager->errCode==SQLITE_OK );
39322 if( !pagerUseWal(pPager) ){
39325 assert( pPager->dbSize==pPager->dbOrigSize );
39326 assert( pPager->dbOrigSize==pPager->dbFileSize );
39327 assert( pPager->dbOrigSize==pPager->dbHintSize );
39328 assert( pPager->setMaster==0 );
39333 assert( pPager->errCode==SQLITE_OK );
39334 if( !pagerUseWal(pPager) ){
39346 assert( pPager->dbOrigSize==pPager->dbFileSize );
39347 assert( pPager->dbOrigSize==pPager->dbHintSize );
39352 assert( pPager->errCode==SQLITE_OK );
39353 assert( !pagerUseWal(pPager) );
39359 assert( pPager->dbOrigSize<=pPager->dbHintSize );
39364 assert( pPager->errCode==SQLITE_OK );
39365 assert( !pagerUseWal(pPager) );
39377 assert( pPager->errCode!=SQLITE_OK );
39378 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
39391 ** to "print *pPager" in gdb:
39393 ** (gdb) printf "%s", print_pager_state(pPager)
39448 Pager *pPager = pPg->pPager;
39450 for(i=0; i<pPager->nSavepoint; i++){
39451 PagerSavepoint *p = &pPager->aSavepoint[i];
39463 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
39507 static int pagerUnlockDb(Pager *pPager, int eLock){
39510 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
39512 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
39513 if( isOpen(pPager->fd) ){
39514 assert( pPager->eLock>=eLock );
39515 rc = sqlite3OsUnlock(pPager->fd, eLock);
39516 if( pPager->eLock!=UNKNOWN_LOCK ){
39517 pPager->eLock = (u8)eLock;
39519 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
39534 static int pagerLockDb(Pager *pPager, int eLock){
39538 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
39539 rc = sqlite3OsLock(pPager->fd, eLock);
39540 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
39541 pPager->eLock = (u8)eLock;
39542 IOTRACE(("LOCK %p %d\n", pPager, eLock))
39558 ** an error to call this function if pPager is opened on an in-memory
39566 static int jrnlBufferSize(Pager *pPager){
39568 if( !pPager->tempFile ){
39573 assert( isOpen(pPager->fd) );
39574 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
39575 nSector = pPager->sectorSize;
39576 szPage = pPager->pageSize;
39585 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
39607 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
39620 Pager *pPager = pPg->pPager;
39621 assert( pPager->eState!=PAGER_ERROR );
39633 ** When this is called the journal file for pager pPager must be open.
39697 ** following the value in pPager->journalOff, assuming a sector
39698 ** size of pPager->sectorSize bytes.
39710 static i64 journalHdrOffset(Pager *pPager){
39712 i64 c = pPager->journalOff;
39714 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
39716 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
39718 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
39743 static int zeroJournalHdr(Pager *pPager, int doTruncate){
39745 assert( isOpen(pPager->jfd) );
39746 if( pPager->journalOff ){
39747 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
39749 IOTRACE(("JZEROHDR %p\n", pPager))
39751 rc = sqlite3OsTruncate(pPager->jfd, 0);
39754 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
39756 if( rc==SQLITE_OK && !pPager->noSync ){
39757 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
39768 rc = sqlite3OsFileSize(pPager->jfd, &sz);
39770 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
39792 static int writeJournalHdr(Pager *pPager){
39794 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
39795 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
39799 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
39801 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
39802 nHeader = JOURNAL_HDR_SZ(pPager);
39809 for(ii=0; ii<pPager->nSavepoint; ii++){
39810 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
39811 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
39815 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
39837 assert( isOpen(pPager->fd) || pPager->noSync );
39838 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
39839 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
39848 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
39849 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
39851 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
39853 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
39856 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
39883 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
39884 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
39885 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
39886 assert( pPager->journalHdr <= pPager->journalOff );
39887 pPager->journalOff += nHeader;
39897 ** pPager->journalOff. See comments above function writeJournalHdr() for
39902 ** database before the transaction began, in pages. Also, pPager->cksumInit
39911 Pager *pPager, /* Pager object */
39921 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
39927 pPager->journalOff = journalHdrOffset(pPager);
39928 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
39931 iHdrOff = pPager->journalOff;
39938 if( isHot || iHdrOff!=pPager->journalHdr ){
39939 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
39952 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
39953 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
39954 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
39959 if( pPager->journalOff==0 ){
39964 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
39965 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
39975 iPageSize = pPager->pageSize;
39999 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
40008 pPager->sectorSize = iSectorSize;
40011 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
40018 ** pPager at the current location. The master journal name must be the last
40035 static int writeMasterJournal(Pager *pPager, const char *zMaster){
40042 assert( pPager->setMaster==0 );
40043 assert( !pagerUseWal(pPager) );
40046 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
40047 || pPager->journalMode==PAGER_JOURNALMODE_OFF
40051 pPager->setMaster = 1;
40052 assert( isOpen(pPager->jfd) );
40053 assert( pPager->journalHdr <= pPager->journalOff );
40064 if( pPager->fullSync ){
40065 pPager->journalOff = journalHdrOffset(pPager);
40067 iHdrOff = pPager->journalOff;
40072 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
40073 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
40074 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
40075 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
40076 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
40080 pPager->journalOff += (nMaster+20);
40092 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
40093 && jrnlSize>pPager->journalOff
40095 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
40105 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
40111 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
40118 static void pager_reset(Pager *pPager){
40119 sqlite3BackupRestart(pPager->pBackup);
40120 sqlite3PcacheClear(pPager->pPCache);
40128 static void releaseAllSavepoints(Pager *pPager){
40130 for(ii=0; ii<pPager->nSavepoint; ii++){
40131 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
40133 if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
40134 sqlite3OsClose(pPager->sjfd);
40136 sqlite3_free(pPager->aSavepoint);
40137 pPager->aSavepoint = 0;
40138 pPager->nSavepoint = 0;
40139 pPager->nSubRec = 0;
40147 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
40151 for(ii=0; ii<pPager->nSavepoint; ii++){
40152 PagerSavepoint *p = &pPager->aSavepoint[ii];
40179 static void pager_unlock(Pager *pPager){
40181 assert( pPager->eState==PAGER_READER
40182 || pPager->eState==PAGER_OPEN
40183 || pPager->eState==PAGER_ERROR
40186 sqlite3BitvecDestroy(pPager->pInJournal);
40187 pPager->pInJournal = 0;
40188 releaseAllSavepoints(pPager);
40190 if( pagerUseWal(pPager) ){
40191 assert( !isOpen(pPager->jfd) );
40192 sqlite3WalEndReadTransaction(pPager->pWal);
40193 pPager->eState = PAGER_OPEN;
40194 }else if( !pPager->exclusiveMode ){
40196 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
40210 || 1!=(pPager->journalMode & 5)
40212 sqlite3OsClose(pPager->jfd);
40220 rc = pagerUnlockDb(pPager, NO_LOCK);
40221 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
40222 pPager->eLock = UNKNOWN_LOCK;
40229 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
40230 pPager->changeCountDone = 0;
40231 pPager->eState = PAGER_OPEN;
40239 if( pPager->errCode ){
40241 pager_reset(pPager);
40242 pPager->changeCountDone = pPager->tempFile;
40243 pPager->eState = PAGER_OPEN;
40244 pPager->errCode = SQLITE_OK;
40247 pPager->journalOff = 0;
40248 pPager->journalHdr = 0;
40249 pPager->setMaster = 0;
40271 static int pager_error(Pager *pPager, int rc){
40275 pPager->errCode==SQLITE_FULL ||
40276 pPager->errCode==SQLITE_OK ||
40277 (pPager->errCode & 0xff)==SQLITE_IOERR
40280 pPager->errCode = rc;
40281 pPager->eState = PAGER_ERROR;
40339 static int pager_end_transaction(Pager *pPager, int hasMaster){
40356 assert( assert_pager_state(pPager) );
40357 assert( pPager->eState!=PAGER_ERROR );
40358 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
40362 releaseAllSavepoints(pPager);
40363 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
40364 if( isOpen(pPager->jfd) ){
40365 assert( !pagerUseWal(pPager) );
40368 if( sqlite3IsMemJournal(pPager->jfd) ){
40369 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
40370 sqlite3OsClose(pPager->jfd);
40371 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
40372 if( pPager->journalOff==0 ){
40375 rc = sqlite3OsTruncate(pPager->jfd, 0);
40377 pPager->journalOff = 0;
40378 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
40379 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
40381 rc = zeroJournalHdr(pPager, hasMaster);
40382 pPager->journalOff = 0;
40389 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
40390 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
40391 || pPager->journalMode==PAGER_JOURNALMODE_WAL
40393 sqlite3OsClose(pPager->jfd);
40394 if( !pPager->tempFile ){
40395 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
40401 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
40402 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
40403 PgHdr *p = pager_lookup(pPager, 1);
40411 sqlite3BitvecDestroy(pPager->pInJournal);
40412 pPager->pInJournal = 0;
40413 pPager->nRec = 0;
40414 sqlite3PcacheCleanAll(pPager->pPCache);
40415 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
40417 if( pagerUseWal(pPager) ){
40422 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
40425 if( !pPager->exclusiveMode
40426 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
40428 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
40429 pPager->changeCountDone = 0;
40431 pPager->eState = PAGER_READER;
40432 pPager->setMaster = 0;
40454 static void pagerUnlockAndRollback(Pager *pPager){
40455 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
40456 assert( assert_pager_state(pPager) );
40457 if( pPager->eState>=PAGER_WRITER_LOCKED ){
40459 sqlite3PagerRollback(pPager);
40461 }else if( !pPager->exclusiveMode ){
40462 assert( pPager->eState==PAGER_READER );
40463 pager_end_transaction(pPager, 0);
40466 pager_unlock(pPager);
40470 ** Parameter aData must point to a buffer of pPager->pageSize bytes
40472 ** page of data and the current value of pPager->cksumInit.
40475 ** random initial value (pPager->cksumInit) and every 200th byte
40476 ** of the page data, starting with byte offset (pPager->pageSize%200).
40488 static u32 pager_cksum(Pager *pPager, const u8 *aData){
40489 u32 cksum = pPager->cksumInit; /* Checksum value to return */
40490 int i = pPager->pageSize-200; /* Loop counter */
40503 static void pagerReportSize(Pager *pPager){
40504 if( pPager->xCodecSizeChng ){
40505 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
40506 (int)pPager->nReserve);
40551 Pager *pPager, /* The pager being played back */
40570 aData = pPager->pTmpSpace;
40572 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
40580 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
40581 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
40583 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
40588 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
40591 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
40593 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
40600 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
40604 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
40610 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
40624 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
40625 pPager->nReserve = ((u8*)aData)[20];
40626 pagerReportSize(pPager);
40664 if( pagerUseWal(pPager) ){
40667 pPg = pager_lookup(pPager, pgno);
40670 assert( pPager->eState!=PAGER_OPEN || pPg==0 );
40672 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
40676 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
40680 if( isOpen(pPager->fd)
40681 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
40684 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
40686 assert( !pagerUseWal(pPager) );
40687 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
40688 if( pgno>pPager->dbFileSize ){
40689 pPager->dbFileSize = pgno;
40691 if( pPager->pBackup ){
40692 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
40693 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
40694 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
40714 assert( pPager->doNotSpill==0 );
40715 pPager->doNotSpill++;
40716 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
40717 assert( pPager->doNotSpill==1 );
40718 pPager->doNotSpill--;
40732 memcpy(pData, (u8*)aData, pPager->pageSize);
40733 pPager->xReiniter(pPg);
40734 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
40754 assert( !pagerUseWal(pPager) );
40762 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
40766 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
40815 static int pager_delmaster(Pager *pPager, const char *zMaster){
40816 sqlite3_vfs *pVfs = pPager->pVfs;
40912 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
40925 static int pager_truncate(Pager *pPager, Pgno nPage){
40927 assert( pPager->eState!=PAGER_ERROR );
40928 assert( pPager->eState!=PAGER_READER );
40930 if( isOpen(pPager->fd)
40931 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
40934 int szPage = pPager->pageSize;
40935 assert( pPager->eLock==EXCLUSIVE_LOCK );
40937 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
40941 rc = sqlite3OsTruncate(pPager->fd, newSize);
40943 char *pTmp = pPager->pTmpSpace;
40947 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
40950 pPager->dbFileSize = nPage;
40973 ** pPager->sectorSize is to define the "blast radius" of bytes that
40980 static void setSectorSize(Pager *pPager){
40981 assert( isOpen(pPager->fd) || pPager->tempFile );
40983 if( pPager->tempFile
40984 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
40990 pPager->sectorSize = 512;
40992 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
40993 if( pPager->sectorSize<32 ){
40994 pPager->sectorSize = 512;
40996 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
40998 pPager->sectorSize = MAX_SECTOR_SIZE;
41023 ** + pPager->pageSize bytes of data.
41060 static int pager_playback(Pager *pPager, int isHot){
41061 sqlite3_vfs *pVfs = pPager->pVfs;
41074 assert( isOpen(pPager->jfd) );
41075 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
41087 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
41091 zMaster = pPager->pTmpSpace;
41092 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
41100 pPager->journalOff = 0;
41113 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
41127 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
41128 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
41146 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
41147 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
41153 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
41154 rc = pager_truncate(pPager, mxPg);
41158 pPager->dbSize = mxPg;
41166 pager_reset(pPager);
41169 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
41172 pPager->journalOff = szJ;
41203 if( pPager->fd->pMethods ){
41204 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
41217 pPager->changeCountDone = pPager->tempFile;
41220 zMaster = pPager->pTmpSpace;
41221 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
41225 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
41227 rc = sqlite3PagerSync(pPager);
41230 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
41237 rc = pager_delmaster(pPager, zMaster);
41245 setSectorSize(pPager);
41262 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
41266 int pgsz = pPager->pageSize; /* Number of bytes to read */
41268 assert( pPager->eState>=PAGER_READER && !MEMDB );
41269 assert( isOpen(pPager->fd) );
41271 if( NEVER(!isOpen(pPager->fd)) ){
41272 assert( pPager->tempFile );
41273 memset(pPg->pData, 0, pPager->pageSize);
41277 if( pagerUseWal(pPager) ){
41279 rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
41282 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
41283 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
41296 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
41303 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
41306 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
41309 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
41312 PAGER_INCR(pPager->nRead);
41313 IOTRACE(("PGIN %p %d\n", pPager, pgno));
41315 PAGERID(pPager), pgno, pager_pagehash(pPg)));
41326 ** needed, as determined by the pPager->changeCountDone state variable.
41332 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
41357 Pager *pPager = (Pager *)pCtx;
41360 pPg = sqlite3PagerLookup(pPager, iPg);
41367 pPager->xReiniter(pPg);
41381 sqlite3BackupRestart(pPager->pBackup);
41389 static int pagerRollbackWal(Pager *pPager){
41400 pPager->dbSize = pPager->dbOrigSize;
41401 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
41402 pList = sqlite3PcacheDirtyList(pPager->pPCache);
41405 rc = pagerUndoCallback((void *)pPager, pList->pgno);
41422 Pager *pPager, /* Pager object */
41432 assert( pPager->pWal );
41455 rc = sqlite3WalFrames(pPager->pWal,
41456 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
41458 if( rc==SQLITE_OK && pPager->pBackup ){
41461 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
41466 pList = sqlite3PcacheDirtyList(pPager->pPCache);
41483 static int pagerBeginReadTransaction(Pager *pPager){
41487 assert( pagerUseWal(pPager) );
41488 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
41495 sqlite3WalEndReadTransaction(pPager->pWal);
41497 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
41499 pager_reset(pPager);
41515 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
41524 assert( pPager->eState==PAGER_OPEN );
41525 assert( pPager->eLock>=SHARED_LOCK );
41526 nPage = sqlite3WalDbsize(pPager->pWal);
41536 assert( isOpen(pPager->fd) || pPager->tempFile );
41537 if( isOpen(pPager->fd) ){
41538 int rc = sqlite3OsFileSize(pPager->fd, &n);
41543 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
41550 if( nPage>pPager->mxPgno ){
41551 pPager->mxPgno = (Pgno)nPage;
41560 ** Check if the *-wal file that corresponds to the database opened by pPager
41577 static int pagerOpenWalIfPresent(Pager *pPager){
41579 assert( pPager->eState==PAGER_OPEN );
41580 assert( pPager->eLock>=SHARED_LOCK );
41582 if( !pPager->tempFile ){
41586 rc = pagerPagecount(pPager, &nPage);
41589 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
41593 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
41598 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
41599 rc = sqlite3PagerOpenWal(pPager, 0);
41600 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
41601 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
41645 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
41651 assert( pPager->eState!=PAGER_ERROR );
41652 assert( pPager->eState>=PAGER_WRITER_LOCKED );
41665 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
41666 pPager->changeCountDone = pPager->tempFile;
41668 if( !pSavepoint && pagerUseWal(pPager) ){
41669 return pagerRollbackWal(pPager);
41672 /* Use pPager->journalOff as the effective size of the main rollback
41675 ** past pPager->journalOff is off-limits to us.
41677 szJ = pPager->journalOff;
41678 assert( pagerUseWal(pPager)==0 || szJ==0 );
41683 ** greater than the current database size (pPager->dbSize) but those
41687 if( pSavepoint && !pagerUseWal(pPager) ){
41689 pPager->journalOff = pSavepoint->iOffset;
41690 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
41691 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
41695 pPager->journalOff = 0;
41703 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
41707 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
41711 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
41716 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
41718 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
41720 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
41721 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
41725 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
41733 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
41735 if( pagerUseWal(pPager) ){
41736 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
41738 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
41739 assert( offset==(i64)ii*(4+pPager->pageSize) );
41740 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
41747 pPager->journalOff = szJ;
41756 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
41757 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
41763 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
41764 sqlite3PcacheShrink(pPager->pPCache);
41812 Pager *pPager, /* The pager to set safety level for */
41818 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
41819 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
41820 if( pPager->noSync ){
41821 pPager->syncFlags = 0;
41822 pPager->ckptSyncFlags = 0;
41824 pPager->syncFlags = SQLITE_SYNC_FULL;
41825 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41827 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41828 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41830 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41831 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
41833 pPager->walSyncFlags = pPager->syncFlags;
41834 if( pPager->fullSync ){
41835 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
41865 Pager *pPager, /* The pager object */
41877 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
41904 Pager *pPager, /* Pager object */
41908 pPager->xBusyHandler = xBusyHandler;
41909 pPager->pBusyHandlerArg = pBusyHandlerArg;
41942 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
41957 if( (pPager->memDb==0 || pPager->dbSize==0)
41958 && sqlite3PcacheRefCount(pPager->pPCache)==0
41959 && pageSize && pageSize!=(u32)pPager->pageSize
41964 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
41965 rc = sqlite3OsFileSize(pPager->fd, &nByte);
41973 pager_reset(pPager);
41974 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
41975 pPager->pageSize = pageSize;
41976 sqlite3PageFree(pPager->pTmpSpace);
41977 pPager->pTmpSpace = pNew;
41978 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
41982 *pPageSize = pPager->pageSize;
41984 if( nReserve<0 ) nReserve = pPager->nReserve;
41986 pPager->nReserve = (i16)nReserve;
41987 pagerReportSize(pPager);
42000 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
42001 return pPager->pTmpSpace;
42011 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
42013 pPager->mxPgno = mxPage;
42015 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
42016 assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
42017 return pPager->mxPgno;
42058 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
42061 assert( isOpen(pPager->fd) || pPager->tempFile );
42067 assert( !pagerUseWal(pPager) );
42069 if( isOpen(pPager->fd) ){
42070 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
42071 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
42086 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
42087 assert( pPager->eState>=PAGER_READER );
42088 assert( pPager->eState!=PAGER_WRITER_FINISHED );
42089 *pnPage = (int)pPager->dbSize;
42107 static int pager_wait_on_lock(Pager *pPager, int locktype){
42115 assert( (pPager->eLock>=locktype)
42116 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
42117 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
42121 rc = pagerLockDb(pPager, locktype);
42122 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
42127 ** Function assertTruncateConstraint(pPager) checks that one of the
42151 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
42153 static void assertTruncateConstraint(Pager *pPager){
42154 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
42157 # define assertTruncateConstraint(pPager)
42166 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
42167 assert( pPager->dbSize>=nPage );
42168 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
42169 pPager->dbSize = nPage;
42170 assertTruncateConstraint(pPager);
42176 ** syncs the journal file to disk, then sets pPager->journalHdr to the
42188 static int pagerSyncHotJournal(Pager *pPager){
42190 if( !pPager->noSync ){
42191 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
42194 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
42213 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
42214 u8 *pTmp = (u8 *)pPager->pTmpSpace;
42216 assert( assert_pager_state(pPager) );
42219 /* pPager->errCode = 0; */
42220 pPager->exclusiveMode = 0;
42222 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
42223 pPager->pWal = 0;
42225 pager_reset(pPager);
42227 pager_unlock(pPager);
42240 if( isOpen(pPager->jfd) ){
42241 pager_error(pPager, pagerSyncHotJournal(pPager));
42243 pagerUnlockAndRollback(pPager);
42247 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
42248 IOTRACE(("CLOSE %p\n", pPager))
42249 sqlite3OsClose(pPager->jfd);
42250 sqlite3OsClose(pPager->fd);
42252 sqlite3PcacheClose(pPager->pPCache);
42255 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
42258 assert( !pPager->aSavepoint && !pPager->pInJournal );
42259 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
42261 sqlite3_free(pPager);
42316 static int syncJournal(Pager *pPager, int newHdr){
42319 assert( pPager->eState==PAGER_WRITER_CACHEMOD
42320 || pPager->eState==PAGER_WRITER_DBMOD
42322 assert( assert_pager_state(pPager) );
42323 assert( !pagerUseWal(pPager) );
42325 rc = sqlite3PagerExclusiveLock(pPager);
42328 if( !pPager->noSync ){
42329 assert( !pPager->tempFile );
42330 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
42331 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
42332 assert( isOpen(pPager->jfd) );
42362 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
42364 iNextHdrOffset = journalHdrOffset(pPager);
42365 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
42368 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
42385 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
42386 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
42387 IOTRACE(("JSYNC %p\n", pPager))
42388 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
42391 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
42393 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
42398 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
42399 IOTRACE(("JSYNC %p\n", pPager))
42400 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
42401 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
42406 pPager->journalHdr = pPager->journalOff;
42408 pPager->nRec = 0;
42409 rc = writeJournalHdr(pPager);
42413 pPager->journalHdr = pPager->journalOff;
42421 sqlite3PcacheClearSyncFlags(pPager->pPCache);
42422 pPager->eState = PAGER_WRITER_DBMOD;
42423 assert( assert_pager_state(pPager) );
42459 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
42463 assert( !pagerUseWal(pPager) );
42464 assert( pPager->eState==PAGER_WRITER_DBMOD );
42465 assert( pPager->eLock==EXCLUSIVE_LOCK );
42471 if( !isOpen(pPager->fd) ){
42472 assert( pPager->tempFile && rc==SQLITE_OK );
42473 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
42479 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
42480 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
42481 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
42482 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
42483 pPager->dbHintSize = pPager->dbSize;
42497 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
42498 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
42505 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
42508 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
42515 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
42517 if( pgno>pPager->dbFileSize ){
42518 pPager->dbFileSize = pgno;
42522 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
42525 PAGERID(pPager), pgno, pager_pagehash(pList)));
42526 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
42528 PAGER_INCR(pPager->nWrite);
42530 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
42547 static int openSubJournal(Pager *pPager){
42549 if( !isOpen(pPager->sjfd) ){
42550 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
42551 sqlite3MemJournalOpen(pPager->sjfd);
42553 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
42574 Pager *pPager = pPg->pPager;
42575 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
42578 assert( pPager->useJournal );
42579 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
42580 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
42581 assert( pagerUseWal(pPager)
42583 || pPg->pgno>pPager->dbOrigSize
42585 rc = openSubJournal(pPager);
42591 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
42594 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
42595 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
42596 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
42598 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
42603 pPager->nSubRec++;
42604 assert( pPager->nSavepoint>0 );
42605 rc = addToSavepointBitvecs(pPager, pPg->pgno);
42630 Pager *pPager = (Pager *)p;
42633 assert( pPg->pPager==pPager );
42651 if( NEVER(pPager->errCode) ) return SQLITE_OK;
42652 if( pPager->doNotSpill ) return SQLITE_OK;
42653 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
42658 if( pagerUseWal(pPager) ){
42664 rc = pagerWalFrames(pPager, pPg, 0, 0);
42670 || pPager->eState==PAGER_WRITER_CACHEMOD
42672 rc = syncJournal(pPager, 1);
42703 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
42711 rc = pager_write_pagelist(pPager, pPg);
42717 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
42721 return pager_error(pPager, rc);
42765 Pager *pPager = 0; /* Pager object to allocate and return */
42852 ROUND8(sizeof(*pPager)) + /* Pager structure */
42867 pPager = (Pager*)(pPtr);
42868 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
42869 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
42870 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
42871 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
42872 pPager->zFilename = (char*)(pPtr += journalFileSize);
42873 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
42878 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
42879 memcpy(pPager->zFilename, zPathname, nPathname);
42880 memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
42881 memcpy(pPager->zJournal, zPathname, nPathname);
42882 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
42883 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
42885 pPager->zWal = &pPager->zJournal[nPathname+8+1];
42886 memcpy(pPager->zWal, zPathname, nPathname);
42887 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
42888 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
42892 pPager->pVfs = pVfs;
42893 pPager->vfsFlags = vfsFlags;
42899 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
42912 setSectorSize(pPager);
42914 if( szPageDflt<pPager->sectorSize ){
42915 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
42918 szPageDflt = (u32)pPager->sectorSize;
42923 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
42946 pPager->eState = PAGER_READER;
42947 pPager->eLock = EXCLUSIVE_LOCK;
42955 assert( pPager->memDb==0 );
42956 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
42964 assert( !pPager->pTmpSpace );
42965 sqlite3OsClose(pPager->fd);
42966 sqlite3_free(pPager);
42974 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
42976 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
42977 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
42979 pPager->useJournal = (u8)useJournal;
42980 /* pPager->stmtOpen = 0; */
42981 /* pPager->stmtInUse = 0; */
42982 /* pPager->nRef = 0; */
42983 /* pPager->stmtSize = 0; */
42984 /* pPager->stmtJSize = 0; */
42985 /* pPager->nPage = 0; */
42986 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
42987 /* pPager->state = PAGER_UNLOCK; */
42989 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
42991 /* pPager->errMask = 0; */
42992 pPager->tempFile = (u8)tempFile;
42996 pPager->exclusiveMode = (u8)tempFile;
42997 pPager->changeCountDone = pPager->tempFile;
42998 pPager->memDb = (u8)memDb;
42999 pPager->readOnly = (u8)readOnly;
43000 assert( useJournal || pPager->tempFile );
43001 pPager->noSync = pPager->tempFile;
43002 if( pPager->noSync ){
43003 assert( pPager->fullSync==0 );
43004 assert( pPager->syncFlags==0 );
43005 assert( pPager->walSyncFlags==0 );
43006 assert( pPager->ckptSyncFlags==0 );
43008 pPager->fullSync = 1;
43009 pPager->syncFlags = SQLITE_SYNC_NORMAL;
43010 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
43011 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
43013 /* pPager->pFirst = 0; */
43014 /* pPager->pFirstSynced = 0; */
43015 /* pPager->pLast = 0; */
43016 pPager->nExtra = (u16)nExtra;
43017 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
43018 assert( isOpen(pPager->fd) || tempFile );
43019 setSectorSize(pPager);
43021 pPager->journalMode = PAGER_JOURNALMODE_OFF;
43023 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
43025 /* pPager->xBusyHandler = 0; */
43026 /* pPager->pBusyHandlerArg = 0; */
43027 pPager->xReiniter = xReinit;
43028 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
43030 *ppPager = pPager;
43067 static int hasHotJournal(Pager *pPager, int *pExists){
43068 sqlite3_vfs * const pVfs = pPager->pVfs;
43071 int jrnlOpen = !!isOpen(pPager->jfd);
43073 assert( pPager->useJournal );
43074 assert( isOpen(pPager->fd) );
43075 assert( pPager->eState==PAGER_OPEN );
43077 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
43083 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
43096 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
43106 rc = pagerPagecount(pPager, &nPage);
43110 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
43111 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
43112 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
43124 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
43128 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
43133 sqlite3OsClose(pPager->jfd);
43184 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
43192 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
43193 assert( assert_pager_state(pPager) );
43194 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
43195 pPager->errCode) ){ return pPager->errCode; }
43197 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
43202 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
43204 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
43211 if( pPager->eLock<=SHARED_LOCK ){
43212 rc = hasHotJournal(pPager, &bHotJournal);
43233 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
43251 if( !isOpen(pPager->jfd) ){
43252 sqlite3_vfs * const pVfs = pPager->pVfs;
43255 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
43259 assert( !pPager->tempFile );
43260 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
43261 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
43264 sqlite3OsClose(pPager->jfd);
43277 if( isOpen(pPager->jfd) ){
43279 rc = pagerSyncHotJournal(pPager);
43281 rc = pager_playback(pPager, 1);
43282 pPager->eState = PAGER_OPEN;
43284 }else if( !pPager->exclusiveMode ){
43285 pagerUnlockDb(pPager, SHARED_LOCK);
43305 pager_error(pPager, rc);
43309 assert( pPager->eState==PAGER_OPEN );
43310 assert( (pPager->eLock==SHARED_LOCK)
43311 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
43315 if( !pPager->tempFile
43316 && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
43335 char dbFileVers[sizeof(pPager->dbFileVers)];
43337 rc = pagerPagecount(pPager, &nPage);
43341 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
43342 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
43350 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
43351 pager_reset(pPager);
43358 rc = pagerOpenWalIfPresent(pPager);
43360 assert( pPager->pWal==0 || rc==SQLITE_OK );
43364 if( pagerUseWal(pPager) ){
43366 rc = pagerBeginReadTransaction(pPager);
43369 if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
43370 rc = pagerPagecount(pPager, &pPager->dbSize);
43376 pager_unlock(pPager);
43377 assert( pPager->eState==PAGER_OPEN );
43379 pPager->eState = PAGER_READER;
43392 static void pagerUnlockIfUnused(Pager *pPager){
43393 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
43394 pagerUnlockAndRollback(pPager);
43399 ** Acquire a reference to page number pgno in pager pPager (a page
43449 Pager *pPager, /* The pager open on the database file */
43457 assert( pPager->eState>=PAGER_READER );
43458 assert( assert_pager_state(pPager) );
43466 if( pPager->errCode!=SQLITE_OK ){
43467 rc = pPager->errCode;
43469 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
43480 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
43482 if( (*ppPage)->pPager && !noContent ){
43485 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
43486 pPager->nHit++;
43494 pPg->pPager = pPager;
43498 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
43503 if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
43504 if( pgno>pPager->mxPgno ){
43516 if( pgno<=pPager->dbOrigSize ){
43517 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
43520 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
43524 memset(pPg->pData, 0, pPager->pageSize);
43525 IOTRACE(("ZERO %p %d\n", pPager, pgno));
43527 assert( pPg->pPager==pPager );
43528 pPager->nMiss++;
43544 pagerUnlockIfUnused(pPager);
43561 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
43563 assert( pPager!=0 );
43565 assert( pPager->pPCache!=0 );
43566 assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
43567 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
43581 Pager *pPager = pPg->pPager;
43583 pagerUnlockIfUnused(pPager);
43592 ** Open the journal file for pager pPager and write a journal header
43609 static int pager_open_journal(Pager *pPager){
43611 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
43613 assert( pPager->eState==PAGER_WRITER_LOCKED );
43614 assert( assert_pager_state(pPager) );
43615 assert( pPager->pInJournal==0 );
43620 if( NEVER(pPager->errCode) ) return pPager->errCode;
43622 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
43623 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
43624 if( pPager->pInJournal==0 ){
43629 if( !isOpen(pPager->jfd) ){
43630 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
43631 sqlite3MemJournalOpen(pPager->jfd);
43635 (pPager->tempFile ?
43641 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
43644 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
43647 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
43656 pPager->nRec = 0;
43657 pPager->journalOff = 0;
43658 pPager->setMaster = 0;
43659 pPager->journalHdr = 0;
43660 rc = writeJournalHdr(pPager);
43665 sqlite3BitvecDestroy(pPager->pInJournal);
43666 pPager->pInJournal = 0;
43668 assert( pPager->eState==PAGER_WRITER_LOCKED );
43669 pPager->eState = PAGER_WRITER_CACHEMOD;
43689 ** sub-journal is implemented in-memory if pPager is an in-memory database,
43692 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
43695 if( pPager->errCode ) return pPager->errCode;
43696 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
43697 pPager->subjInMemory = (u8)subjInMemory;
43699 if( ALWAYS(pPager->eState==PAGER_READER) ){
43700 assert( pPager->pInJournal==0 );
43702 if( pagerUseWal(pPager) ){
43706 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
43707 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
43711 sqlite3WalExclusiveMode(pPager->pWal, 1);
43719 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
43726 rc = pagerLockDb(pPager, RESERVED_LOCK);
43728 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
43742 pPager->eState = PAGER_WRITER_LOCKED;
43743 pPager->dbHintSize = pPager->dbSize;
43744 pPager->dbFileSize = pPager->dbSize;
43745 pPager->dbOrigSize = pPager->dbSize;
43746 pPager->journalOff = 0;
43749 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
43750 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
43751 assert( assert_pager_state(pPager) );
43754 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
43767 Pager *pPager = pPg->pPager;
43774 assert( pPager->eState==PAGER_WRITER_LOCKED
43775 || pPager->eState==PAGER_WRITER_CACHEMOD
43776 || pPager->eState==PAGER_WRITER_DBMOD
43778 assert( assert_pager_state(pPager) );
43782 if( NEVER(pPager->errCode) ) return pPager->errCode;
43786 if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
43799 if( pPager->eState==PAGER_WRITER_LOCKED ){
43800 rc = pager_open_journal(pPager);
43803 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
43804 assert( assert_pager_state(pPager) );
43811 assert( !pagerUseWal(pPager) );
43818 if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
43819 assert( pagerUseWal(pPager)==0 );
43820 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
43823 i64 iOff = pPager->journalOff;
43828 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
43830 assert( pPager->journalHdr<=pPager->journalOff );
43831 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
43832 cksum = pager_cksum(pPager, (u8*)pData2);
43843 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
43845 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
43847 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
43850 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
43851 pPager->journalOff, pPager->pageSize));
43854 PAGERID(pPager), pPg->pgno,
43857 pPager->journalOff += 8 + pPager->pageSize;
43858 pPager->nRec++;
43859 assert( pPager->pInJournal!=0 );
43860 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
43863 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
43869 if( pPager->eState!=PAGER_WRITER_DBMOD ){
43873 PAGERID(pPager), pPg->pgno,
43890 if( pPager->dbSize<pPg->pgno ){
43891 pPager->dbSize = pPg->pgno;
43914 Pager *pPager = pPg->pPager;
43915 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
43917 assert( pPager->eState>=PAGER_WRITER_LOCKED );
43918 assert( pPager->eState!=PAGER_ERROR );
43919 assert( assert_pager_state(pPager) );
43933 assert( pPager->doNotSyncSpill==0 );
43934 pPager->doNotSyncSpill++;
43942 nPageCount = pPager->dbSize;
43957 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
43958 if( pg!=PAGER_MJ_PGNO(pPager) ){
43959 rc = sqlite3PagerGet(pPager, pg, &pPage);
43968 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
43985 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
43993 assert( pPager->doNotSyncSpill==1 );
43994 pPager->doNotSyncSpill--;
44027 Pager *pPager = pPg->pPager;
44028 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
44029 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
44030 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
44042 ** But this only happens if the pPager->changeCountDone flag is false.
44058 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
44061 assert( pPager->eState==PAGER_WRITER_CACHEMOD
44062 || pPager->eState==PAGER_WRITER_DBMOD
44064 assert( assert_pager_state(pPager) );
44084 if( !pPager->changeCountDone && pPager->dbSize>0 ){
44087 assert( !pPager->tempFile && isOpen(pPager->fd) );
44090 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
44109 assert( pPager->dbFileSize>0 );
44110 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
44112 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
44115 pPager->changeCountDone = 1;
44118 pPager->changeCountDone = 1;
44135 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
44137 if( !pPager->noSync ){
44139 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
44140 }else if( isOpen(pPager->fd) ){
44142 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
44161 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
44163 assert( pPager->eState==PAGER_WRITER_CACHEMOD
44164 || pPager->eState==PAGER_WRITER_DBMOD
44165 || pPager->eState==PAGER_WRITER_LOCKED
44167 assert( assert_pager_state(pPager) );
44168 if( 0==pagerUseWal(pPager) ){
44169 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
44175 ** Sync the database file for the pager pPager. zMaster points to the name
44201 Pager *pPager, /* Pager object */
44207 assert( pPager->eState==PAGER_WRITER_LOCKED
44208 || pPager->eState==PAGER_WRITER_CACHEMOD
44209 || pPager->eState==PAGER_WRITER_DBMOD
44210 || pPager->eState==PAGER_ERROR
44212 assert( assert_pager_state(pPager) );
44215 if( NEVER(pPager->errCode) ) return pPager->errCode;
44218 pPager->zFilename, zMaster, pPager->dbSize));
44221 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
44228 sqlite3BackupRestart(pPager->pBackup);
44230 if( pagerUseWal(pPager) ){
44231 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
44236 rc = sqlite3PagerGet(pPager, 1, &pPageOne);
44242 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
44246 sqlite3PcacheCleanAll(pPager->pPCache);
44274 assert( isOpen(pPager->jfd)
44275 || pPager->journalMode==PAGER_JOURNALMODE_OFF
44276 || pPager->journalMode==PAGER_JOURNALMODE_WAL
44278 if( !zMaster && isOpen(pPager->jfd)
44279 && pPager->journalOff==jrnlBufferSize(pPager)
44280 && pPager->dbSize>=pPager->dbOrigSize
44281 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
44289 rc = pager_incr_changecounter(pPager, 1);
44291 rc = sqlite3JournalCreate(pPager->jfd);
44293 rc = pager_incr_changecounter(pPager, 0);
44297 rc = pager_incr_changecounter(pPager, 0);
44312 if( pPager->dbSize<pPager->dbOrigSize
44313 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
44316 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
44317 const Pgno dbSize = pPager->dbSize; /* Database image size */
44318 pPager->dbSize = pPager->dbOrigSize;
44319 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
44320 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
44322 rc = sqlite3PagerGet(pPager, i, &pPage);
44329 pPager->dbSize = dbSize;
44337 rc = writeMasterJournal(pPager, zMaster);
44351 rc = syncJournal(pPager, 0);
44354 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
44359 sqlite3PcacheCleanAll(pPager->pPCache);
44364 if( pPager->dbSize!=pPager->dbFileSize ){
44365 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
44366 assert( pPager->eState==PAGER_WRITER_DBMOD );
44367 rc = pager_truncate(pPager, nNew);
44373 rc = sqlite3PagerSync(pPager);
44375 IOTRACE(("DBSYNC %p\n", pPager))
44380 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
44381 pPager->eState = PAGER_WRITER_FINISHED;
44402 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
44408 if( NEVER(pPager->errCode) ) return pPager->errCode;
44410 assert( pPager->eState==PAGER_WRITER_LOCKED
44411 || pPager->eState==PAGER_WRITER_FINISHED
44412 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
44414 assert( assert_pager_state(pPager) );
44427 if( pPager->eState==PAGER_WRITER_LOCKED
44428 && pPager->exclusiveMode
44429 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
44431 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
44432 pPager->eState = PAGER_READER;
44436 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
44437 rc = pager_end_transaction(pPager, pPager->setMaster);
44438 return pager_error(pPager, rc);
44467 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
44469 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
44475 assert( assert_pager_state(pPager) );
44476 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
44477 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
44479 if( pagerUseWal(pPager) ){
44481 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
44482 rc2 = pager_end_transaction(pPager, pPager->setMaster);
44484 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
44485 int eState = pPager->eState;
44486 rc = pager_end_transaction(pPager, 0);
44492 pPager->errCode = SQLITE_ABORT;
44493 pPager->eState = PAGER_ERROR;
44497 rc = pager_playback(pPager, 0);
44500 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
44507 return pager_error(pPager, rc);
44514 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
44515 return pPager->readOnly;
44521 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
44522 return sqlite3PcacheRefCount(pPager->pPCache);
44529 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
44530 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
44532 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
44533 + sqlite3MallocSize(pPager)
44534 + pPager->pageSize;
44548 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
44550 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
44551 a[1] = sqlite3PcachePagecount(pPager->pPCache);
44552 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
44553 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
44554 a[4] = pPager->eState;
44555 a[5] = pPager->errCode;
44556 a[6] = pPager->nHit;
44557 a[7] = pPager->nMiss;
44558 a[8] = 0; /* Used to be pPager->nOvfl */
44559 a[9] = pPager->nRead;
44560 a[10] = pPager->nWrite;
44572 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
44579 piStat = &pPager->nHit;
44581 piStat = &pPager->nMiss;
44593 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
44607 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
44609 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
44611 assert( pPager->eState>=PAGER_WRITER_LOCKED );
44612 assert( assert_pager_state(pPager) );
44614 if( nSavepoint>nCurrent && pPager->useJournal ){
44623 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
44629 pPager->aSavepoint = aNew;
44633 aNew[ii].nOrig = pPager->dbSize;
44634 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
44635 aNew[ii].iOffset = pPager->journalOff;
44637 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
44639 aNew[ii].iSubRec = pPager->nSubRec;
44640 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
44644 if( pagerUseWal(pPager) ){
44645 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
44647 pPager->nSavepoint = ii+1;
44649 assert( pPager->nSavepoint==nSavepoint );
44650 assertTruncateConstraint(pPager);
44686 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
44687 int rc = pPager->errCode; /* Return code */
44692 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
44701 for(ii=nNew; ii<pPager->nSavepoint; ii++){
44702 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
44704 pPager->nSavepoint = nNew;
44709 if( nNew==0 && isOpen(pPager->sjfd) ){
44711 if( sqlite3IsMemJournal(pPager->sjfd) ){
44712 rc = sqlite3OsTruncate(pPager->sjfd, 0);
44715 pPager->nSubRec = 0;
44723 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
44724 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
44725 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
44736 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
44737 return pPager->zFilename;
44743 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
44744 return pPager->pVfs;
44752 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
44753 return pPager->fd;
44759 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
44760 return pPager->zJournal;
44767 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
44768 return pPager->noSync;
44776 Pager *pPager,
44782 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
44783 pPager->xCodec = pPager->memDb ? 0 : xCodec;
44784 pPager->xCodecSizeChng = xCodecSizeChng;
44785 pPager->xCodecFree = xCodecFree;
44786 pPager->pCodec = pCodec;
44787 pagerReportSize(pPager);
44789 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
44790 return pPager->pCodec;
44820 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
44827 assert( pPager->eState==PAGER_WRITER_CACHEMOD
44828 || pPager->eState==PAGER_WRITER_DBMOD
44830 assert( assert_pager_state(pPager) );
44866 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
44867 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
44878 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
44888 pPgOld = pager_lookup(pPager, pgno);
44895 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
44931 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
44933 if( needSyncPgno<=pPager->dbOrigSize ){
44934 assert( pPager->pTmpSpace!=0 );
44935 pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
44952 assert( pPg->nRef>0 || pPg->pPager->memDb );
44974 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
44980 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
44981 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
44982 pPager->exclusiveMode = (u8)eMode;
44984 return (int)pPager->exclusiveMode;
45007 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
45008 u8 eOld = pPager->journalMode; /* Prior journalmode */
45013 print_pager_state(pPager);
45029 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
45044 assert( pPager->eState!=PAGER_ERROR );
45045 pPager->journalMode = (u8)eMode;
45058 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
45059 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
45069 sqlite3OsClose(pPager->jfd);
45070 if( pPager->eLock>=RESERVED_LOCK ){
45071 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
45074 int state = pPager->eState;
45077 rc = sqlite3PagerSharedLock(pPager);
45079 if( pPager->eState==PAGER_READER ){
45081 rc = pagerLockDb(pPager, RESERVED_LOCK);
45084 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
45087 pagerUnlockDb(pPager, SHARED_LOCK);
45089 pager_unlock(pPager);
45091 assert( state==pPager->eState );
45097 return (int)pPager->journalMode;
45103 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
45104 return (int)pPager->journalMode;
45112 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
45113 assert( assert_pager_state(pPager) );
45114 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
45115 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
45125 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
45127 pPager->journalSizeLimit = iLimit;
45128 sqlite3WalLimit(pPager->pWal, iLimit);
45130 return pPager->journalSizeLimit;
45134 ** Return a pointer to the pPager->pBackup variable. The backup module
45139 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
45140 return &pPager->pBackup;
45147 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
45148 if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
45160 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
45162 if( pPager->pWal ){
45163 rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
45164 pPager->xBusyHandler, pPager->pBusyHandlerArg,
45165 pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
45172 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
45173 return sqlite3WalCallback(pPager->pWal);
45180 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
45181 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
45182 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
45189 static int pagerExclusiveLock(Pager *pPager){
45192 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
45193 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
45197 pagerUnlockDb(pPager, SHARED_LOCK);
45209 static int pagerOpenWal(Pager *pPager){
45212 assert( pPager->pWal==0 && pPager->tempFile==0 );
45213 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
45220 if( pPager->exclusiveMode ){
45221 rc = pagerExclusiveLock(pPager);
45228 rc = sqlite3WalOpen(pPager->pVfs,
45229 pPager->fd, pPager->zWal, pPager->exclusiveMode,
45230 pPager->journalSizeLimit, &pPager->pWal
45254 Pager *pPager, /* Pager object */
45259 assert( assert_pager_state(pPager) );
45260 assert( pPager->eState==PAGER_OPEN || pbOpen );
45261 assert( pPager->eState==PAGER_READER || !pbOpen );
45263 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
45265 if( !pPager->tempFile && !pPager->pWal ){
45266 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
45269 sqlite3OsClose(pPager->jfd);
45271 rc = pagerOpenWal(pPager);
45273 pPager->journalMode = PAGER_JOURNALMODE_WAL;
45274 pPager->eState = PAGER_OPEN;
45292 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
45295 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
45301 if( !pPager->pWal ){
45303 rc = pagerLockDb(pPager, SHARED_LOCK);
45306 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
45310 rc = pagerOpenWal(pPager);
45317 if( rc==SQLITE_OK && pPager->pWal ){
45318 rc = pagerExclusiveLock(pPager);
45320 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
45321 pPager->pageSize, (u8*)pPager->pTmpSpace);
45322 pPager->pWal = 0;
45336 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
45337 assert( pPager->eState==PAGER_READER );
45338 return sqlite3WalFramesize(pPager->pWal);
45352 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
48847 ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
48871 Pager *pPager; /* The page cache */
49103 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
50174 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
50217 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
50474 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
50951 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
50965 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
51176 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
51177 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
51226 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
51229 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
51236 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
51241 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
51270 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
51333 if( pBt && pBt->pPager ){
51334 sqlite3PagerClose(pBt->pPager);
51345 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
51451 sqlite3PagerClose(pBt->pPager);
51490 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
51514 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
51529 assert( pBt && pBt->pPager );
51530 rc = sqlite3PagerNosync(pBt->pPager);
51575 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
51611 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
51700 rc = sqlite3PagerSharedLock(pBt->pPager);
51709 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
51747 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
51786 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
51851 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
52006 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
52066 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
52201 Pager *pPager = pBt->pPager;
52212 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
52380 sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
52425 Pager *pPager = pBt->pPager;
52426 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
52468 sqlite3PagerTruncateImage(pBt->pPager, nFin);
52472 sqlite3PagerRollback(pPager);
52476 assert( nRef==sqlite3PagerRefcount(pPager) );
52524 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
52607 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
52718 rc2 = sqlite3PagerRollback(pBt->pPager);
52729 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
52775 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
52799 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
53367 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
53381 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
56914 return p->pBt->pPager;
57024 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
57329 nRef = sqlite3PagerRefcount(pBt->pPager);
57331 sCheck.pPager = pBt->pPager;
57398 if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
57401 nRef, sqlite3PagerRefcount(pBt->pPager)
57427 assert( p->pBt->pPager!=0 );
57428 return sqlite3PagerFilename(p->pBt->pPager);
57440 assert( p->pBt->pPager!=0 );
57441 return sqlite3PagerJournalname(p->pBt->pPager);
57469 rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
65385 Pager *pPager; /* Pager associated with pBt */
65388 const char *zFilename; /* Name of database file for pPager */
70463 Pager *pPager; /* Pager associated with pBt */
70466 const char *zFilename; /* Name of database file for pPager */
70481 u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
70482 u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
70484 if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
70487 u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager);
70494 || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
70517 rc = sqlite3PagerCloseWal(u.ci.pPager);
70519 sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
70524 sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
70541 u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
80570 Pager *pPager;
80579 pPager = sqlite3BtreePager(aNew->pBt);
80580 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
92632 Pager *pPager;
92645 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
92646 sqlite3PagerLockingMode(pPager, eMode);
92650 pPager = sqlite3BtreePager(pDb->pBt);
92651 eMode = sqlite3PagerLockingMode(pPager, eMode);
92721 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92727 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
92924 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92926 sqlite3_file *pFile = sqlite3PagerFile(pPager);
92938 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92939 sqlite3_file *pFile = sqlite3PagerFile(pPager);
93595 Pager *pPager;
93601 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
113020 Pager *pPager = sqlite3BtreePager(pBt);
113021 sqlite3PagerShrink(pPager);
115184 Pager *pPager;
115187 pPager = sqlite3BtreePager(pBtree);
115188 assert( pPager!=0 );
115189 fd = sqlite3PagerFile(pPager);