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);
39148 ** the page number and the pPager->pageSize bytes of data for the page.
39165 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
39171 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
39182 # define MEMDB pPager->memDb
39196 ** if( isOpen(pPager->jfd) ){ ...
39200 ** if( pPager->jfd->pMethods ){ ...
39209 static int pagerUseWal(Pager *pPager){
39210 return (pPager->pWal!=0);
39224 ** assert( assert_pager_state(pPager) );
39230 Pager *pPager = p;
39247 assert( p->tempFile==0 || pPager->changeCountDone );
39275 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
39281 assert( pPager->errCode==SQLITE_OK );
39282 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
39286 assert( pPager->errCode==SQLITE_OK );
39293 assert( pPager->errCode==SQLITE_OK );
39294 if( !pagerUseWal(pPager) ){
39297 assert( pPager->dbSize==pPager->dbOrigSize );
39298 assert( pPager->dbOrigSize==pPager->dbFileSize );
39299 assert( pPager->dbOrigSize==pPager->dbHintSize );
39300 assert( pPager->setMaster==0 );
39305 assert( pPager->errCode==SQLITE_OK );
39306 if( !pagerUseWal(pPager) ){
39318 assert( pPager->dbOrigSize==pPager->dbFileSize );
39319 assert( pPager->dbOrigSize==pPager->dbHintSize );
39324 assert( pPager->errCode==SQLITE_OK );
39325 assert( !pagerUseWal(pPager) );
39331 assert( pPager->dbOrigSize<=pPager->dbHintSize );
39336 assert( pPager->errCode==SQLITE_OK );
39337 assert( !pagerUseWal(pPager) );
39349 assert( pPager->errCode!=SQLITE_OK );
39350 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
39363 ** to "print *pPager" in gdb:
39365 ** (gdb) printf "%s", print_pager_state(pPager)
39420 Pager *pPager = pPg->pPager;
39422 for(i=0; i<pPager->nSavepoint; i++){
39423 PagerSavepoint *p = &pPager->aSavepoint[i];
39435 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
39479 static int pagerUnlockDb(Pager *pPager, int eLock){
39482 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
39484 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
39485 if( isOpen(pPager->fd) ){
39486 assert( pPager->eLock>=eLock );
39487 rc = sqlite3OsUnlock(pPager->fd, eLock);
39488 if( pPager->eLock!=UNKNOWN_LOCK ){
39489 pPager->eLock = (u8)eLock;
39491 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
39506 static int pagerLockDb(Pager *pPager, int eLock){
39510 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
39511 rc = sqlite3OsLock(pPager->fd, eLock);
39512 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
39513 pPager->eLock = (u8)eLock;
39514 IOTRACE(("LOCK %p %d\n", pPager, eLock))
39530 ** an error to call this function if pPager is opened on an in-memory
39538 static int jrnlBufferSize(Pager *pPager){
39540 if( !pPager->tempFile ){
39545 assert( isOpen(pPager->fd) );
39546 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
39547 nSector = pPager->sectorSize;
39548 szPage = pPager->pageSize;
39557 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
39579 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
39592 Pager *pPager = pPg->pPager;
39593 assert( pPager->eState!=PAGER_ERROR );
39605 ** When this is called the journal file for pager pPager must be open.
39669 ** following the value in pPager->journalOff, assuming a sector
39670 ** size of pPager->sectorSize bytes.
39682 static i64 journalHdrOffset(Pager *pPager){
39684 i64 c = pPager->journalOff;
39686 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
39688 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
39690 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
39715 static int zeroJournalHdr(Pager *pPager, int doTruncate){
39717 assert( isOpen(pPager->jfd) );
39718 if( pPager->journalOff ){
39719 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
39721 IOTRACE(("JZEROHDR %p\n", pPager))
39723 rc = sqlite3OsTruncate(pPager->jfd, 0);
39726 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
39728 if( rc==SQLITE_OK && !pPager->noSync ){
39729 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
39740 rc = sqlite3OsFileSize(pPager->jfd, &sz);
39742 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
39764 static int writeJournalHdr(Pager *pPager){
39766 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
39767 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
39771 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
39773 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
39774 nHeader = JOURNAL_HDR_SZ(pPager);
39781 for(ii=0; ii<pPager->nSavepoint; ii++){
39782 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
39783 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
39787 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
39809 assert( isOpen(pPager->fd) || pPager->noSync );
39810 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
39811 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
39820 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
39821 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
39823 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
39825 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
39828 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
39855 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
39856 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
39857 pPager->jfd, zHeader, nHeader, pPager->journalOff);
39858 assert( pPager->journalHdr <= pPager->journalOff );
39859 pPager->journalOff += nHeader;
39869 ** pPager->journalOff. See comments above function writeJournalHdr() for
39874 ** database before the transaction began, in pages. Also, pPager->cksumInit
39883 Pager *pPager, /* Pager object */
39893 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
39899 pPager->journalOff = journalHdrOffset(pPager);
39900 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
39903 iHdrOff = pPager->journalOff;
39910 if( isHot || iHdrOff!=pPager->journalHdr ){
39911 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
39924 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
39925 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
39926 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
39931 if( pPager->journalOff==0 ){
39936 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
39937 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
39947 iPageSize = pPager->pageSize;
39971 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
39980 pPager->sectorSize = iSectorSize;
39983 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
39990 ** pPager at the current location. The master journal name must be the last
40007 static int writeMasterJournal(Pager *pPager, const char *zMaster){
40014 assert( pPager->setMaster==0 );
40015 assert( !pagerUseWal(pPager) );
40018 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
40019 || pPager->journalMode==PAGER_JOURNALMODE_OFF
40023 pPager->setMaster = 1;
40024 assert( isOpen(pPager->jfd) );
40025 assert( pPager->journalHdr <= pPager->journalOff );
40036 if( pPager->fullSync ){
40037 pPager->journalOff = journalHdrOffset(pPager);
40039 iHdrOff = pPager->journalOff;
40044 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
40045 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
40046 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
40047 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
40048 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
40052 pPager->journalOff += (nMaster+20);
40064 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
40065 && jrnlSize>pPager->journalOff
40067 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
40077 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
40083 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
40090 static void pager_reset(Pager *pPager){
40091 sqlite3BackupRestart(pPager->pBackup);
40092 sqlite3PcacheClear(pPager->pPCache);
40100 static void releaseAllSavepoints(Pager *pPager){
40102 for(ii=0; ii<pPager->nSavepoint; ii++){
40103 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
40105 if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
40106 sqlite3OsClose(pPager->sjfd);
40108 sqlite3_free(pPager->aSavepoint);
40109 pPager->aSavepoint = 0;
40110 pPager->nSavepoint = 0;
40111 pPager->nSubRec = 0;
40119 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
40123 for(ii=0; ii<pPager->nSavepoint; ii++){
40124 PagerSavepoint *p = &pPager->aSavepoint[ii];
40151 static void pager_unlock(Pager *pPager){
40153 assert( pPager->eState==PAGER_READER
40154 || pPager->eState==PAGER_OPEN
40155 || pPager->eState==PAGER_ERROR
40158 sqlite3BitvecDestroy(pPager->pInJournal);
40159 pPager->pInJournal = 0;
40160 releaseAllSavepoints(pPager);
40162 if( pagerUseWal(pPager) ){
40163 assert( !isOpen(pPager->jfd) );
40164 sqlite3WalEndReadTransaction(pPager->pWal);
40165 pPager->eState = PAGER_OPEN;
40166 }else if( !pPager->exclusiveMode ){
40168 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
40182 || 1!=(pPager->journalMode & 5)
40184 sqlite3OsClose(pPager->jfd);
40192 rc = pagerUnlockDb(pPager, NO_LOCK);
40193 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
40194 pPager->eLock = UNKNOWN_LOCK;
40201 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
40202 pPager->changeCountDone = 0;
40203 pPager->eState = PAGER_OPEN;
40211 if( pPager->errCode ){
40213 pager_reset(pPager);
40214 pPager->changeCountDone = pPager->tempFile;
40215 pPager->eState = PAGER_OPEN;
40216 pPager->errCode = SQLITE_OK;
40219 pPager->journalOff = 0;
40220 pPager->journalHdr = 0;
40221 pPager->setMaster = 0;
40243 static int pager_error(Pager *pPager, int rc){
40247 pPager->errCode==SQLITE_FULL ||
40248 pPager->errCode==SQLITE_OK ||
40249 (pPager->errCode & 0xff)==SQLITE_IOERR
40252 pPager->errCode = rc;
40253 pPager->eState = PAGER_ERROR;
40311 static int pager_end_transaction(Pager *pPager, int hasMaster){
40328 assert( assert_pager_state(pPager) );
40329 assert( pPager->eState!=PAGER_ERROR );
40330 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
40334 releaseAllSavepoints(pPager);
40335 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
40336 if( isOpen(pPager->jfd) ){
40337 assert( !pagerUseWal(pPager) );
40340 if( sqlite3IsMemJournal(pPager->jfd) ){
40341 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
40342 sqlite3OsClose(pPager->jfd);
40343 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
40344 if( pPager->journalOff==0 ){
40347 rc = sqlite3OsTruncate(pPager->jfd, 0);
40349 pPager->journalOff = 0;
40350 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
40351 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
40353 rc = zeroJournalHdr(pPager, hasMaster);
40354 pPager->journalOff = 0;
40361 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
40362 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
40363 || pPager->journalMode==PAGER_JOURNALMODE_WAL
40365 sqlite3OsClose(pPager->jfd);
40366 if( !pPager->tempFile ){
40367 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
40373 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
40374 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
40375 PgHdr *p = pager_lookup(pPager, 1);
40383 sqlite3BitvecDestroy(pPager->pInJournal);
40384 pPager->pInJournal = 0;
40385 pPager->nRec = 0;
40386 sqlite3PcacheCleanAll(pPager->pPCache);
40387 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
40389 if( pagerUseWal(pPager) ){
40394 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
40397 if( !pPager->exclusiveMode
40398 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
40400 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
40401 pPager->changeCountDone = 0;
40403 pPager->eState = PAGER_READER;
40404 pPager->setMaster = 0;
40426 static void pagerUnlockAndRollback(Pager *pPager){
40427 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
40428 assert( assert_pager_state(pPager) );
40429 if( pPager->eState>=PAGER_WRITER_LOCKED ){
40431 sqlite3PagerRollback(pPager);
40433 }else if( !pPager->exclusiveMode ){
40434 assert( pPager->eState==PAGER_READER );
40435 pager_end_transaction(pPager, 0);
40438 pager_unlock(pPager);
40442 ** Parameter aData must point to a buffer of pPager->pageSize bytes
40444 ** page of data and the current value of pPager->cksumInit.
40447 ** random initial value (pPager->cksumInit) and every 200th byte
40448 ** of the page data, starting with byte offset (pPager->pageSize%200).
40460 static u32 pager_cksum(Pager *pPager, const u8 *aData){
40461 u32 cksum = pPager->cksumInit; /* Checksum value to return */
40462 int i = pPager->pageSize-200; /* Loop counter */
40475 static void pagerReportSize(Pager *pPager){
40476 if( pPager->xCodecSizeChng ){
40477 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
40478 (int)pPager->nReserve);
40523 Pager *pPager, /* The pager being played back */
40542 aData = pPager->pTmpSpace;
40544 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
40552 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
40553 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
40555 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
40560 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
40563 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
40565 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
40572 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
40576 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
40582 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
40596 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
40597 pPager->nReserve = ((u8*)aData)[20];
40598 pagerReportSize(pPager);
40636 if( pagerUseWal(pPager) ){
40639 pPg = pager_lookup(pPager, pgno);
40642 assert( pPager->eState!=PAGER_OPEN || pPg==0 );
40644 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
40648 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
40652 if( isOpen(pPager->fd)
40653 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
40656 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
40658 assert( !pagerUseWal(pPager) );
40659 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
40660 if( pgno>pPager->dbFileSize ){
40661 pPager->dbFileSize = pgno;
40663 if( pPager->pBackup ){
40664 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
40665 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
40666 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
40686 assert( pPager->doNotSpill==0 );
40687 pPager->doNotSpill++;
40688 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
40689 assert( pPager->doNotSpill==1 );
40690 pPager->doNotSpill--;
40704 memcpy(pData, (u8*)aData, pPager->pageSize);
40705 pPager->xReiniter(pPg);
40706 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
40726 assert( !pagerUseWal(pPager) );
40734 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
40738 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
40787 static int pager_delmaster(Pager *pPager, const char *zMaster){
40788 sqlite3_vfs *pVfs = pPager->pVfs;
40884 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
40897 static int pager_truncate(Pager *pPager, Pgno nPage){
40899 assert( pPager->eState!=PAGER_ERROR );
40900 assert( pPager->eState!=PAGER_READER );
40902 if( isOpen(pPager->fd)
40903 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
40906 int szPage = pPager->pageSize;
40907 assert( pPager->eLock==EXCLUSIVE_LOCK );
40909 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
40913 rc = sqlite3OsTruncate(pPager->fd, newSize);
40915 char *pTmp = pPager->pTmpSpace;
40919 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
40922 pPager->dbFileSize = nPage;
40945 ** pPager->sectorSize is to define the "blast radius" of bytes that
40952 static void setSectorSize(Pager *pPager){
40953 assert( isOpen(pPager->fd) || pPager->tempFile );
40955 if( pPager->tempFile
40956 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
40962 pPager->sectorSize = 512;
40964 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
40965 if( pPager->sectorSize<32 ){
40966 pPager->sectorSize = 512;
40968 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
40970 pPager->sectorSize = MAX_SECTOR_SIZE;
40995 ** + pPager->pageSize bytes of data.
41032 static int pager_playback(Pager *pPager, int isHot){
41033 sqlite3_vfs *pVfs = pPager->pVfs;
41046 assert( isOpen(pPager->jfd) );
41047 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
41059 ** (pPager->pageSize >= pPager
41063 zMaster = pPager->pTmpSpace;
41064 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
41072 pPager->journalOff = 0;
41085 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
41099 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
41100 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
41118 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
41119 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
41125 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
41126 rc = pager_truncate(pPager, mxPg);
41130 pPager->dbSize = mxPg;
41138 pager_reset(pPager);
41141 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
41144 pPager->journalOff = szJ;
41175 if( pPager->fd->pMethods ){
41176 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
41189 pPager->changeCountDone = pPager->tempFile;
41192 zMaster = pPager->pTmpSpace;
41193 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
41197 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
41199 rc = sqlite3PagerSync(pPager);
41202 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
41209 rc = pager_delmaster(pPager, zMaster);
41217 setSectorSize(pPager);
41234 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
41238 int pgsz = pPager->pageSize; /* Number of bytes to read */
41240 assert( pPager->eState>=PAGER_READER && !MEMDB );
41241 assert( isOpen(pPager->fd) );
41243 if( NEVER(!isOpen(pPager->fd)) ){
41244 assert( pPager->tempFile );
41245 memset(pPg->pData, 0, pPager->pageSize);
41249 if( pagerUseWal(pPager) ){
41251 rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
41254 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
41255 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
41268 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
41275 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
41278 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
41281 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
41284 PAGER_INCR(pPager->nRead);
41285 IOTRACE(("PGIN %p %d\n", pPager, pgno));
41287 PAGERID(pPager), pgno, pager_pagehash(pPg)));
41298 ** needed, as determined by the pPager->changeCountDone state variable.
41304 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
41329 Pager *pPager = (Pager *)pCtx;
41332 pPg = sqlite3PagerLookup(pPager, iPg);
41339 pPager->xReiniter(pPg);
41353 sqlite3BackupRestart(pPager->pBackup);
41361 static int pagerRollbackWal(Pager *pPager){
41372 pPager->dbSize = pPager->dbOrigSize;
41373 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
41374 pList = sqlite3PcacheDirtyList(pPager->pPCache);
41377 rc = pagerUndoCallback((void *)pPager, pList->pgno);
41394 Pager *pPager, /* Pager object */
41404 assert( pPager->pWal );
41427 rc = sqlite3WalFrames(pPager->pWal,
41428 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
41430 if( rc==SQLITE_OK && pPager->pBackup ){
41433 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
41438 pList = sqlite3PcacheDirtyList(pPager->pPCache);
41455 static int pagerBeginReadTransaction(Pager *pPager){
41459 assert( pagerUseWal(pPager) );
41460 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
41467 sqlite3WalEndReadTransaction(pPager->pWal);
41469 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
41471 pager_reset(pPager);
41487 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
41496 assert( pPager->eState==PAGER_OPEN );
41497 assert( pPager->eLock>=SHARED_LOCK );
41498 nPage = sqlite3WalDbsize(pPager->pWal);
41508 assert( isOpen(pPager->fd) || pPager->tempFile );
41509 if( isOpen(pPager->fd) ){
41510 int rc = sqlite3OsFileSize(pPager->fd, &n);
41515 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
41522 if( nPage>pPager->mxPgno ){
41523 pPager->mxPgno = (Pgno)nPage;
41532 ** Check if the *-wal file that corresponds to the database opened by pPager
41549 static int pagerOpenWalIfPresent(Pager *pPager){
41551 assert( pPager->eState==PAGER_OPEN );
41552 assert( pPager->eLock>=SHARED_LOCK );
41554 if( !pPager->tempFile ){
41558 rc = pagerPagecount(pPager, &nPage);
41561 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
41565 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
41570 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
41571 rc = sqlite3PagerOpenWal(pPager, 0);
41572 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
41573 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
41617 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
41623 assert( pPager->eState!=PAGER_ERROR );
41624 assert( pPager->eState>=PAGER_WRITER_LOCKED );
41637 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
41638 pPager->changeCountDone = pPager->tempFile;
41640 if( !pSavepoint && pagerUseWal(pPager) ){
41641 return pagerRollbackWal(pPager);
41644 /* Use pPager->journalOff as the effective size of the main rollback
41647 ** past pPager->journalOff is off-limits to us.
41649 szJ = pPager->journalOff;
41650 assert( pagerUseWal(pPager)==0 || szJ==0 );
41655 ** greater than the current database size (pPager->dbSize) but those
41659 if( pSavepoint && !pagerUseWal(pPager) ){
41661 pPager->journalOff = pSavepoint->iOffset;
41662 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
41663 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
41667 pPager->journalOff = 0;
41675 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
41679 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
41683 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
41688 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
41690 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
41692 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
41693 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
41697 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
41705 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
41707 if( pagerUseWal(pPager) ){
41708 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
41710 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
41711 assert( offset==(i64)ii*(4+pPager->pageSize) );
41712 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
41719 pPager->journalOff = szJ;
41728 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
41729 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
41735 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
41736 sqlite3PcacheShrink(pPager->pPCache);
41784 Pager *pPager, /* The pager to set safety level for */
41790 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
41791 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
41792 if( pPager->noSync ){
41793 pPager->syncFlags = 0;
41794 pPager->ckptSyncFlags = 0;
41796 pPager->syncFlags = SQLITE_SYNC_FULL;
41797 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41799 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41800 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41802 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41803 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
41805 pPager->walSyncFlags = pPager->syncFlags;
41806 if( pPager->fullSync ){
41807 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
41837 Pager *pPager, /* The pager object */
41849 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
41876 Pager *pPager, /* Pager object */
41880 pPager->xBusyHandler = xBusyHandler;
41881 pPager->pBusyHandlerArg = pBusyHandlerArg;
41914 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
41929 if( (pPager->memDb==0 || pPager->dbSize==0)
41930 && sqlite3PcacheRefCount(pPager->pPCache)==0
41931 && pageSize && pageSize!=(u32)pPager->pageSize
41936 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
41937 rc = sqlite3OsFileSize(pPager->fd, &nByte);
41945 pager_reset(pPager);
41946 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
41947 pPager->pageSize = pageSize;
41948 sqlite3PageFree(pPager->pTmpSpace);
41949 pPager->pTmpSpace = pNew;
41950 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
41954 *pPageSize = pPager->pageSize;
41956 if( nReserve<0 ) nReserve = pPager->nReserve;
41958 pPager->nReserve = (i16)nReserve;
41959 pagerReportSize(pPager);
41972 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
41973 return pPager->pTmpSpace;
41983 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
41985 pPager->mxPgno = mxPage;
41987 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
41988 assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
41989 return pPager->mxPgno;
42030 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
42033 assert( isOpen(pPager->fd) || pPager->tempFile );
42039 assert( !pagerUseWal(pPager) );
42041 if( isOpen(pPager->fd) ){
42042 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
42043 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
42058 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
42059 assert( pPager->eState>=PAGER_READER );
42060 assert( pPager->eState!=PAGER_WRITER_FINISHED );
42061 *pnPage = (int)pPager->dbSize;
42079 static int pager_wait_on_lock(Pager *pPager, int locktype){
42087 assert( (pPager->eLock>=locktype)
42088 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
42089 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
42093 rc = pagerLockDb(pPager, locktype);
42094 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
42099 ** Function assertTruncateConstraint(pPager) checks that one of the
42123 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
42125 static void assertTruncateConstraint(Pager *pPager){
42126 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
42129 # define assertTruncateConstraint(pPager)
42138 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
42139 assert( pPager->dbSize>=nPage );
42140 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
42141 pPager->dbSize = nPage;
42142 assertTruncateConstraint(pPager);
42148 ** syncs the journal file to disk, then sets pPager->journalHdr to the
42160 static int pagerSyncHotJournal(Pager *pPager){
42162 if( !pPager->noSync ){
42163 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
42166 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
42185 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
42186 u8 *pTmp = (u8 *)pPager->pTmpSpace;
42188 assert( assert_pager_state(pPager) );
42191 /* pPager->errCode = 0; */
42192 pPager->exclusiveMode = 0;
42194 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
42195 pPager->pWal = 0;
42197 pager_reset(pPager);
42199 pager_unlock(pPager);
42212 if( isOpen(pPager->jfd) ){
42213 pager_error(pPager, pagerSyncHotJournal(pPager));
42215 pagerUnlockAndRollback(pPager);
42219 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
42220 IOTRACE(("CLOSE %p\n", pPager))
42221 sqlite3OsClose(pPager->jfd);
42222 sqlite3OsClose(pPager->fd);
42224 sqlite3PcacheClose(pPager->pPCache);
42227 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
42230 assert( !pPager->aSavepoint && !pPager->pInJournal );
42231 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
42233 sqlite3_free(pPager);
42288 static int syncJournal(Pager *pPager, int newHdr){
42291 assert( pPager->eState==PAGER_WRITER_CACHEMOD
42292 || pPager->eState==PAGER_WRITER_DBMOD
42294 assert( assert_pager_state(pPager) );
42295 assert( !pagerUseWal(pPager) );
42297 rc = sqlite3PagerExclusiveLock(pPager);
42300 if( !pPager->noSync ){
42301 assert( !pPager->tempFile );
42302 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
42303 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
42304 assert( isOpen(pPager->jfd) );
42334 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
42336 iNextHdrOffset = journalHdrOffset(pPager);
42337 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
42340 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
42357 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
42358 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
42359 IOTRACE(("JSYNC %p\n", pPager))
42360 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
42363 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
42365 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
42370 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
42371 IOTRACE(("JSYNC %p\n", pPager))
42372 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
42373 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
42378 pPager->journalHdr = pPager->journalOff;
42380 pPager->nRec = 0;
42381 rc = writeJournalHdr(pPager);
42385 pPager->journalHdr = pPager->journalOff;
42393 sqlite3PcacheClearSyncFlags(pPager->pPCache);
42394 pPager->eState = PAGER_WRITER_DBMOD;
42395 assert( assert_pager_state(pPager) );
42431 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
42435 assert( !pagerUseWal(pPager) );
42436 assert( pPager->eState==PAGER_WRITER_DBMOD );
42437 assert( pPager->eLock==EXCLUSIVE_LOCK );
42443 if( !isOpen(pPager->fd) ){
42444 assert( pPager->tempFile && rc==SQLITE_OK );
42445 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
42451 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
42452 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
42453 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
42454 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
42455 pPager->dbHintSize = pPager->dbSize;
42469 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
42470 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
42477 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
42480 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
42487 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
42489 if( pgno>pPager->dbFileSize ){
42490 pPager->dbFileSize = pgno;
42494 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
42497 PAGERID(pPager), pgno, pager_pagehash(pList)));
42498 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
42500 PAGER_INCR(pPager->nWrite);
42502 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
42519 static int openSubJournal(Pager *pPager){
42521 if( !isOpen(pPager->sjfd) ){
42522 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
42523 sqlite3MemJournalOpen(pPager->sjfd);
42525 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
42546 Pager *pPager = pPg->pPager;
42547 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
42550 assert( pPager->useJournal );
42551 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
42552 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
42553 assert( pagerUseWal(pPager)
42555 || pPg->pgno>pPager->dbOrigSize
42557 rc = openSubJournal(pPager);
42563 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
42566 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
42567 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
42568 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
42570 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
42575 pPager->nSubRec++;
42576 assert( pPager->nSavepoint>0 );
42577 rc = addToSavepointBitvecs(pPager, pPg->pgno);
42602 Pager *pPager = (Pager *)p;
42605 assert( pPg->pPager==pPager );
42623 if( NEVER(pPager->errCode) ) return SQLITE_OK;
42624 if( pPager->doNotSpill ) return SQLITE_OK;
42625 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
42630 if( pagerUseWal(pPager) ){
42636 rc = pagerWalFrames(pPager, pPg, 0, 0);
42642 || pPager->eState==PAGER_WRITER_CACHEMOD
42644 rc = syncJournal(pPager, 1);
42675 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
42683 rc = pager_write_pagelist(pPager, pPg);
42689 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
42693 return pager_error(pPager, rc);
42737 Pager *pPager = 0; /* Pager object to allocate and return */
42824 ROUND8(sizeof(*pPager)) + /* Pager structure */
42839 pPager = (Pager*)(pPtr);
42840 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
42841 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
42842 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
42843 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
42844 pPager->zFilename = (char*)(pPtr += journalFileSize);
42845 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
42850 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
42851 memcpy(pPager->zFilename, zPathname, nPathname);
42852 memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
42853 memcpy(pPager->zJournal, zPathname, nPathname);
42854 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
42855 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
42857 pPager->zWal = &pPager->zJournal[nPathname+8+1];
42858 memcpy(pPager->zWal, zPathname, nPathname);
42859 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
42860 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
42864 pPager->pVfs = pVfs;
42865 pPager->vfsFlags = vfsFlags;
42871 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
42884 setSectorSize(pPager);
42886 if( szPageDflt<pPager->sectorSize ){
42887 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
42890 szPageDflt = (u32)pPager->sectorSize;
42895 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
42918 pPager->eState = PAGER_READER;
42919 pPager->eLock = EXCLUSIVE_LOCK;
42927 assert( pPager->memDb==0 );
42928 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
42936 assert( !pPager->pTmpSpace );
42937 sqlite3OsClose(pPager->fd);
42938 sqlite3_free(pPager);
42946 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
42948 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
42949 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
42951 pPager->useJournal = (u8)useJournal;
42952 /* pPager->stmtOpen = 0; */
42953 /* pPager->stmtInUse = 0; */
42954 /* pPager->nRef = 0; */
42955 /* pPager->stmtSize = 0; */
42956 /* pPager->stmtJSize = 0; */
42957 /* pPager->nPage = 0; */
42958 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
42959 /* pPager->state = PAGER_UNLOCK; */
42961 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
42963 /* pPager->errMask = 0; */
42964 pPager->tempFile = (u8)tempFile;
42968 pPager->exclusiveMode = (u8)tempFile;
42969 pPager->changeCountDone = pPager->tempFile;
42970 pPager->memDb = (u8)memDb;
42971 pPager->readOnly = (u8)readOnly;
42972 assert( useJournal || pPager->tempFile );
42973 pPager->noSync = pPager->tempFile;
42974 if( pPager->noSync ){
42975 assert( pPager->fullSync==0 );
42976 assert( pPager->syncFlags==0 );
42977 assert( pPager->walSyncFlags==0 );
42978 assert( pPager->ckptSyncFlags==0 );
42980 pPager->fullSync = 1;
42981 pPager->syncFlags = SQLITE_SYNC_NORMAL;
42982 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
42983 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
42985 /* pPager->pFirst = 0; */
42986 /* pPager->pFirstSynced = 0; */
42987 /* pPager->pLast = 0; */
42988 pPager->nExtra = (u16)nExtra;
42989 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
42990 assert( isOpen(pPager->fd) || tempFile );
42991 setSectorSize(pPager);
42993 pPager->journalMode = PAGER_JOURNALMODE_OFF;
42995 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
42997 /* pPager->xBusyHandler = 0; */
42998 /* pPager->pBusyHandlerArg = 0; */
42999 pPager->xReiniter = xReinit;
43000 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
43002 *ppPager = pPager;
43039 static int hasHotJournal(Pager *pPager, int *pExists){
43040 sqlite3_vfs * const pVfs = pPager->pVfs;
43043 int jrnlOpen = !!isOpen(pPager->jfd);
43045 assert( pPager->useJournal );
43046 assert( isOpen(pPager->fd) );
43047 assert( pPager->eState==PAGER_OPEN );
43049 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
43055 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
43068 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
43078 rc = pagerPagecount(pPager, &nPage);
43082 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
43083 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
43084 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
43096 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
43100 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
43105 sqlite3OsClose(pPager->jfd);
43156 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
43164 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
43165 assert( assert_pager_state(pPager) );
43166 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
43167 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
43169 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
43174 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
43176 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
43183 if( pPager->eLock<=SHARED_LOCK ){
43184 rc = hasHotJournal(pPager, &bHotJournal);
43205 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
43223 if( !isOpen(pPager->jfd) ){
43224 sqlite3_vfs * const pVfs = pPager->pVfs;
43227 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
43231 assert( !pPager->tempFile );
43232 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
43233 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
43236 sqlite3OsClose(pPager->jfd);
43249 if( isOpen(pPager->jfd) ){
43251 rc = pagerSyncHotJournal(pPager);
43253 rc = pager_playback(pPager, 1);
43254 pPager->eState = PAGER_OPEN;
43256 }else if( !pPager->exclusiveMode ){
43257 pagerUnlockDb(pPager, SHARED_LOCK);
43277 pager_error(pPager, rc);
43281 assert( pPager->eState==PAGER_OPEN );
43282 assert( (pPager->eLock==SHARED_LOCK)
43283 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
43287 if( !pPager->tempFile
43288 && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
43307 char dbFileVers[sizeof(pPager->dbFileVers)];
43309 rc = pagerPagecount(pPager, &nPage);
43313 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
43314 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
43322 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
43323 pager_reset(pPager);
43330 rc = pagerOpenWalIfPresent(pPager);
43332 assert( pPager->pWal==0 || rc==SQLITE_OK );
43336 if( pagerUseWal(pPager) ){
43338 rc = pagerBeginReadTransaction(pPager);
43341 if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
43342 rc = pagerPagecount(pPager, &pPager->dbSize);
43348 pager_unlock(pPager);
43349 assert( pPager->eState==PAGER_OPEN );
43351 pPager->eState = PAGER_READER;
43364 static void pagerUnlockIfUnused(Pager *pPager){
43365 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
43366 pagerUnlockAndRollback(pPager);
43371 ** Acquire a reference to page number pgno in pager pPager (a page
43421 Pager *pPager, /* The pager open on the database file */
43429 assert( pPager->eState>=PAGER_READER );
43430 assert( assert_pager_state(pPager) );
43438 if( pPager->errCode!=SQLITE_OK ){
43439 rc = pPager->errCode;
43441 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
43452 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
43454 if( (*ppPage)->pPager && !noContent ){
43457 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
43458 pPager->nHit++;
43466 pPg->pPager = pPager;
43470 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
43475 if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
43476 if( pgno>pPager->mxPgno ){
43488 if( pgno<=pPager->dbOrigSize ){
43489 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
43492 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
43496 memset(pPg->pData, 0, pPager->pageSize);
43497 IOTRACE(("ZERO %p %d\n", pPager, pgno));
43499 assert( pPg->pPager==pPager );
43500 pPager->nMiss++;
43516 pagerUnlockIfUnused(pPager);
43533 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
43535 assert( pPager!=0 );
43537 assert( pPager->pPCache!=0 );
43538 assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
43539 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
43553 Pager *pPager = pPg->pPager;
43555 pagerUnlockIfUnused(pPager);
43564 ** Open the journal file for pager pPager and write a journal header
43581 static int pager_open_journal(Pager *pPager){
43583 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
43585 assert( pPager->eState==PAGER_WRITER_LOCKED );
43586 assert( assert_pager_state(pPager) );
43587 assert( pPager->pInJournal==0 );
43592 if( NEVER(pPager->errCode) ) return pPager->errCode;
43594 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
43595 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
43596 if( pPager->pInJournal==0 ){
43601 if( !isOpen(pPager->jfd) ){
43602 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
43603 sqlite3MemJournalOpen(pPager->jfd);
43607 (pPager->tempFile ?
43613 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
43616 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
43619 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
43628 pPager->nRec = 0;
43629 pPager->journalOff = 0;
43630 pPager->setMaster = 0;
43631 pPager->journalHdr = 0;
43632 rc = writeJournalHdr(pPager);
43637 sqlite3BitvecDestroy(pPager->pInJournal);
43638 pPager->pInJournal = 0;
43640 assert( pPager->eState==PAGER_WRITER_LOCKED );
43641 pPager->eState = PAGER_WRITER_CACHEMOD;
43661 ** sub-journal is implemented in-memory if pPager is an in-memory database,
43664 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
43667 if( pPager->errCode ) return pPager->errCode;
43668 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
43669 pPager->subjInMemory = (u8)subjInMemory;
43671 if( ALWAYS(pPager->eState==PAGER_READER) ){
43672 assert( pPager->pInJournal==0 );
43674 if( pagerUseWal(pPager) ){
43678 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
43679 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
43683 sqlite3WalExclusiveMode(pPager->pWal, 1);
43691 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
43698 rc = pagerLockDb(pPager, RESERVED_LOCK);
43700 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
43714 pPager->eState = PAGER_WRITER_LOCKED;
43715 pPager->dbHintSize = pPager->dbSize;
43716 pPager->dbFileSize = pPager->dbSize;
43717 pPager->dbOrigSize = pPager->dbSize;
43718 pPager->journalOff = 0;
43721 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
43722 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
43723 assert( assert_pager_state(pPager) );
43726 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
43739 Pager *pPager = pPg->pPager;
43746 assert( pPager->eState==PAGER_WRITER_LOCKED
43747 || pPager->eState==PAGER_WRITER_CACHEMOD
43748 || pPager->eState==PAGER_WRITER_DBMOD
43750 assert( assert_pager_state(pPager) );
43754 if( NEVER(pPager->errCode) ) return pPager->errCode;
43758 if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
43771 if( pPager->eState==PAGER_WRITER_LOCKED ){
43772 rc = pager_open_journal(pPager);
43775 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
43776 assert( assert_pager_state(pPager) );
43783 assert( !pagerUseWal(pPager) );
43790 if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
43791 assert( pagerUseWal(pPager)==0 );
43792 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
43795 i64 iOff = pPager->journalOff;
43800 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
43802 assert( pPager->journalHdr<=pPager->journalOff );
43803 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
43804 cksum = pager_cksum(pPager, (u8*)pData2);
43815 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
43817 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
43819 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
43822 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
43823 pPager->journalOff, pPager->pageSize));
43826 PAGERID(pPager), pPg->pgno,
43829 pPager->journalOff += 8 + pPager->pageSize;
43830 pPager->nRec++;
43831 assert( pPager->pInJournal!=0 );
43832 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
43835 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
43841 if( pPager->eState!=PAGER_WRITER_DBMOD ){
43845 PAGERID(pPager), pPg->pgno,
43862 if( pPager->dbSize<pPg->pgno ){
43863 pPager->dbSize = pPg->pgno;
43886 Pager *pPager = pPg->pPager;
43887 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
43889 assert( pPager->eState>=PAGER_WRITER_LOCKED );
43890 assert( pPager->eState!=PAGER_ERROR );
43891 assert( assert_pager_state(pPager) );
43905 assert( pPager->doNotSyncSpill==0 );
43906 pPager->doNotSyncSpill++;
43914 nPageCount = pPager->dbSize;
43929 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
43930 if( pg!=PAGER_MJ_PGNO(pPager) ){
43931 rc = sqlite3PagerGet(pPager, pg, &pPage);
43940 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
43957 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
43965 assert( pPager->doNotSyncSpill==1 );
43966 pPager->doNotSyncSpill--;
43999 Pager *pPager = pPg->pPager;
44000 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
44001 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
44002 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
44014 ** But this only happens if the pPager->changeCountDone flag is false.
44030 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
44033 assert( pPager->eState==PAGER_WRITER_CACHEMOD
44034 || pPager->eState==PAGER_WRITER_DBMOD
44036 assert( assert_pager_state(pPager) );
44056 if( !pPager->changeCountDone && pPager->dbSize>0 ){
44059 assert( !pPager->tempFile && isOpen(pPager->fd) );
44062 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
44081 assert( pPager->dbFileSize>0 );
44082 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
44084 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
44087 pPager->changeCountDone = 1;
44090 pPager->changeCountDone = 1;
44107 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
44109 if( !pPager->noSync ){
44111 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
44112 }else if( isOpen(pPager->fd) ){
44114 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
44133 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
44135 assert( pPager->eState==PAGER_WRITER_CACHEMOD
44136 || pPager->eState==PAGER_WRITER_DBMOD
44137 || pPager->eState==PAGER_WRITER_LOCKED
44139 assert( assert_pager_state(pPager) );
44140 if( 0==pagerUseWal(pPager) ){
44141 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
44147 ** Sync the database file for the pager pPager. zMaster points to the name
44173 Pager *pPager, /* Pager object */
44179 assert( pPager->eState==PAGER_WRITER_LOCKED
44180 || pPager->eState==PAGER_WRITER_CACHEMOD
44181 || pPager->eState==PAGER_WRITER_DBMOD
44182 || pPager->eState==PAGER_ERROR
44184 assert( assert_pager_state(pPager) );
44187 if( NEVER(pPager->errCode) ) return pPager->errCode;
44190 pPager->zFilename, zMaster, pPager->dbSize));
44193 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
44200 sqlite3BackupRestart(pPager->pBackup);
44202 if( pagerUseWal(pPager) ){
44203 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
44208 rc = sqlite3PagerGet(pPager, 1, &pPageOne);
44214 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
44218 sqlite3PcacheCleanAll(pPager->pPCache);
44246 assert( isOpen(pPager->jfd)
44247 || pPager->journalMode==PAGER_JOURNALMODE_OFF
44248 || pPager->journalMode==PAGER_JOURNALMODE_WAL
44250 if( !zMaster && isOpen(pPager->jfd)
44251 && pPager->journalOff==jrnlBufferSize(pPager)
44252 && pPager->dbSize>=pPager->dbOrigSize
44253 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
44261 rc = pager_incr_changecounter(pPager, 1);
44263 rc = sqlite3JournalCreate(pPager->jfd);
44265 rc = pager_incr_changecounter(pPager, 0);
44269 rc = pager_incr_changecounter(pPager, 0);
44284 if( pPager->dbSize<pPager->dbOrigSize
44285 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
44288 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
44289 const Pgno dbSize = pPager->dbSize; /* Database image size */
44290 pPager->dbSize = pPager->dbOrigSize;
44291 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
44292 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
44294 rc = sqlite3PagerGet(pPager, i, &pPage);
44301 pPager->dbSize = dbSize;
44309 rc = writeMasterJournal(pPager, zMaster);
44323 rc = syncJournal(pPager, 0);
44326 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
44331 sqlite3PcacheCleanAll(pPager->pPCache);
44336 if( pPager->dbSize!=pPager->dbFileSize ){
44337 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
44338 assert( pPager->eState==PAGER_WRITER_DBMOD );
44339 rc = pager_truncate(pPager, nNew);
44345 rc = sqlite3PagerSync(pPager);
44347 IOTRACE(("DBSYNC %p\n", pPager))
44352 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
44353 pPager->eState = PAGER_WRITER_FINISHED;
44374 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
44380 if( NEVER(pPager->errCode) ) return pPager->errCode;
44382 assert( pPager->eState==PAGER_WRITER_LOCKED
44383 || pPager->eState==PAGER_WRITER_FINISHED
44384 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
44386 assert( assert_pager_state(pPager) );
44399 if( pPager->eState==PAGER_WRITER_LOCKED
44400 && pPager->exclusiveMode
44401 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
44403 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
44404 pPager->eState = PAGER_READER;
44408 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
44409 rc = pager_end_transaction(pPager, pPager->setMaster);
44410 return pager_error(pPager, rc);
44439 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
44441 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
44447 assert( assert_pager_state(pPager) );
44448 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
44449 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
44451 if( pagerUseWal(pPager) ){
44453 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
44454 rc2 = pager_end_transaction(pPager, pPager->setMaster);
44456 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
44457 int eState = pPager->eState;
44458 rc = pager_end_transaction(pPager, 0);
44464 pPager->errCode = SQLITE_ABORT;
44465 pPager->eState = PAGER_ERROR;
44469 pPager, 0);
44472 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
44479 return pager_error(pPager, rc);
44486 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
44487 return pPager->readOnly;
44493 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
44494 return sqlite3PcacheRefCount(pPager->pPCache);
44501 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
44502 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
44504 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
44505 + sqlite3MallocSize(pPager)
44506 + pPager->pageSize;
44520 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
44522 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
44523 a[1] = sqlite3PcachePagecount(pPager->pPCache);
44524 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
44525 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
44526 a[4] = pPager->eState;
44527 a[5] = pPager->errCode;
44528 a[6] = pPager->nHit;
44529 a[7] = pPager->nMiss;
44530 a[8] = 0; /* Used to be pPager->nOvfl */
44531 a[9] = pPager->nRead;
44532 a[10] = pPager->nWrite;
44544 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
44551 piStat = &pPager->nHit;
44553 piStat = &pPager->nMiss;
44565 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
44579 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
44581 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
44583 assert( pPager->eState>=PAGER_WRITER_LOCKED );
44584 assert( assert_pager_state(pPager) );
44586 if( nSavepoint>nCurrent && pPager->useJournal ){
44595 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
44601 pPager->aSavepoint = aNew;
44605 aNew[ii].nOrig = pPager->dbSize;
44606 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
44607 aNew[ii].iOffset = pPager->journalOff;
44609 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
44611 aNew[ii].iSubRec = pPager->nSubRec;
44612 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
44616 if( pagerUseWal(pPager) ){
44617 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
44619 pPager->nSavepoint = ii+1;
44621 assert( pPager->nSavepoint==nSavepoint );
44622 assertTruncateConstraint(pPager);
44658 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
44659 int rc = pPager->errCode; /* Return code */
44664 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
44673 for(ii=nNew; ii<pPager->nSavepoint; ii++){
44674 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
44676 pPager->nSavepoint = nNew;
44681 if( nNew==0 && isOpen(pPager->sjfd) ){
44683 if( sqlite3IsMemJournal(pPager->sjfd) ){
44684 rc = sqlite3OsTruncate(pPager->sjfd, 0);
44687 pPager->nSubRec = 0;
44695 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
44696 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
44697 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
44708 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
44709 return pPager->zFilename;
44715 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
44716 return pPager->pVfs;
44724 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
44725 return pPager->fd;
44731 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
44732 return pPager->zJournal;
44739 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
44740 return pPager->noSync;
44748 Pager *pPager,
44754 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
44755 pPager->xCodec = pPager->memDb ? 0 : xCodec;
44756 pPager->xCodecSizeChng = xCodecSizeChng;
44757 pPager->xCodecFree = xCodecFree;
44758 pPager->pCodec = pCodec;
44759 pagerReportSize(pPager);
44761 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
44762 return pPager->pCodec;
44792 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
44799 assert( pPager->eState==PAGER_WRITER_CACHEMOD
44800 || pPager->eState==PAGER_WRITER_DBMOD
44802 assert( assert_pager_state(pPager) );
44838 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
44839 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
44850 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
44860 pPgOld = pager_lookup(pPager, pgno);
44867 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
44903 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
44905 if( needSyncPgno<=pPager->dbOrigSize ){
44906 assert( pPager->pTmpSpace!=0 );
44907 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
44924 assert( pPg->nRef>0 || pPg->pPager->memDb );
44946 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
44952 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
44953 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
44954 pPager->exclusiveMode = (u8)eMode;
44956 return (int)pPager->exclusiveMode;
44979 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
44980 u8 eOld = pPager->journalMode; /* Prior journalmode */
44985 print_pager_state(pPager);
45001 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
45016 assert( pPager->eState!=PAGER_ERROR );
45017 pPager->journalMode = (u8)eMode;
45030 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
45031 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
45041 sqlite3OsClose(pPager->jfd);
45042 if( pPager->eLock>=RESERVED_LOCK ){
45043 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
45046 int state = pPager->eState;
45049 rc = sqlite3PagerSharedLock(pPager);
45051 if( pPager->eState==PAGER_READER ){
45053 rc = pagerLockDb(pPager, RESERVED_LOCK);
45056 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
45059 pagerUnlockDb(pPager, SHARED_LOCK);
45061 pager_unlock(pPager);
45063 assert( state==pPager->eState );
45069 return (int)pPager->journalMode;
45075 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
45076 return (int)pPager->journalMode;
45084 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
45085 assert( assert_pager_state(pPager) );
45086 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
45087 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
45097 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
45099 pPager->journalSizeLimit = iLimit;
45100 sqlite3WalLimit(pPager->pWal, iLimit);
45102 return pPager->journalSizeLimit;
45106 ** Return a pointer to the pPager->pBackup variable. The backup module
45111 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
45112 return &pPager->pBackup;
45119 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
45120 if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
45132 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
45134 if( pPager->pWal ){
45135 rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
45136 pPager->xBusyHandler, pPager->pBusyHandlerArg,
45137 pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
45144 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
45145 return sqlite3WalCallback(pPager->pWal);
45152 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
45153 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
45154 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
45161 static int pagerExclusiveLock(Pager *pPager){
45164 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
45165 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
45169 pagerUnlockDb(pPager, SHARED_LOCK);
45181 static int pagerOpenWal(Pager *pPager){
45184 assert( pPager->pWal==0 && pPager->tempFile==0 );
45185 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
45192 if( pPager->exclusiveMode ){
45193 rc = pagerExclusiveLock(pPager);
45200 rc = sqlite3WalOpen(pPager->pVfs,
45201 pPager->fd, pPager->zWal, pPager->exclusiveMode,
45202 pPager->journalSizeLimit, &pPager->pWal
45226 Pager *pPager, /* Pager object */
45231 assert( assert_pager_state(pPager) );
45232 assert( pPager->eState==PAGER_OPEN || pbOpen );
45233 assert( pPager->eState==PAGER_READER || !pbOpen );
45235 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
45237 if( !pPager->tempFile && !pPager->pWal ){
45238 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
45241 sqlite3OsClose(pPager->jfd);
45243 rc = pagerOpenWal(pPager);
45245 pPager->journalMode = PAGER_JOURNALMODE_WAL;
45246 pPager->eState = PAGER_OPEN;
45264 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
45267 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
45273 if( !pPager->pWal ){
45275 rc = pagerLockDb(pPager, SHARED_LOCK);
45278 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
45282 rc = pagerOpenWal(pPager);
45289 if( rc==SQLITE_OK && pPager->pWal ){
45290 rc = pagerExclusiveLock(pPager);
45292 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
45293 pPager->pageSize, (u8*)pPager->pTmpSpace);
45294 pPager->pWal = 0;
45308 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
45309 assert( pPager->eState==PAGER_READER );
45310 return sqlite3WalFramesize(pPager->pWal);
45324 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
48819 ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
48843 Pager *pPager; /* The page cache */
49075 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
50146 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
50189 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
50446 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
50923 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
50937 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
51148 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
51149 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
51198 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
51201 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
51208 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
51213 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
51242 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
51305 if( pBt && pBt->pPager ){
51306 sqlite3PagerClose(pBt->pPager);
51317 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
51423 sqlite3PagerClose(pBt->pPager);
51462 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
51486 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
51501 assert( pBt && pBt->pPager );
51502 rc = sqlite3PagerNosync(pBt->pPager);
51547 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
51583 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
51672 rc = sqlite3PagerSharedLock(pBt->pPager);
51681 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
51719 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
51758 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
51823 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
51978 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
52038 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
52173 Pager *pPager = pBt->pPager;
52184 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
52352 sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
52397 Pager *pPager = pBt->pPager;
52398 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
52440 sqlite3PagerTruncateImage(pBt->pPager, nFin);
52444 sqlite3PagerRollback(pPager);
52448 assert( nRef==sqlite3PagerRefcount(pPager) );
52496 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
52579 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
52690 rc2 = sqlite3PagerRollback(pBt->pPager);
52701 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
52747 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
52771 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
53339 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
53353 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
56886 return p->pBt->pPager;
56996 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
57301 nRef = sqlite3PagerRefcount(pBt->pPager);
57303 sCheck.pPager = pBt->pPager;
57370 if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
57373 nRef, sqlite3PagerRefcount(pBt->pPager)
57399 assert( p->pBt->pPager!=0 );
57400 return sqlite3PagerFilename(p->pBt->pPager);
57412 assert( p->pBt->pPager!=0 );
57413 return sqlite3PagerJournalname(p->pBt->pPager);
57441 rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
65357 Pager *pPager; /* Pager associated with pBt */
65360 const char *zFilename; /* Name of database file for pPager */
70435 Pager *pPager; /* Pager associated with pBt */
70438 const char *zFilename; /* Name of database file for pPager */
70453 u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
70454 u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
70456 if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
70459 u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager);
70466 || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
70489 rc = sqlite3PagerCloseWal(u.ci.pPager);
70491 sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
70496 sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
70513 u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
80534 Pager *pPager;
80543 pPager = sqlite3BtreePager(aNew->pBt);
80544 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
92596 Pager *pPager;
92609 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
92610 sqlite3PagerLockingMode(pPager, eMode);
92614 pPager = sqlite3BtreePager(pDb->pBt);
92615 eMode = sqlite3PagerLockingMode(pPager, eMode);
92685 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92691 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
92888 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92890 sqlite3_file *pFile = sqlite3PagerFile(pPager);
92902 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92903 sqlite3_file *pFile = sqlite3PagerFile(pPager);
93559 Pager *pPager;
93565 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
112984 Pager *pPager = sqlite3BtreePager(pBt);
112985 sqlite3PagerShrink(pPager);
115148 Pager *pPager;
115151 pPager = sqlite3BtreePager(pBtree);
115152 assert( pPager!=0 );
115153 fd = sqlite3PagerFile(pPager);