Home | History | Annotate | Download | only in dist

Lines Matching full:page

210 /* Maximum page size.  The upper bound on this value is 32768.  This a limit
212 ** and the fact that the page size must be a power of 2.
216 ** a process operating on a database with a page-size of 65536 bytes
217 ** crashes, then an instance of SQLite compiled with the default page-size
227 ** The default size of a database page.
239 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
1264 ** file will be doing page-aligned sector reads and writes in a random
1681 ** page size. ^If SQLite needs needs additional scratch memory beyond
1687 ** the database page cache with the default page cache implemenation.
1688 ** This configuration should not be used if an application-define page
1691 ** memory, the size of each page buffer (sz), and the number of pages (N).
1692 ** The sz argument should be the size of the largest database page
1694 ** page header. ^The page header size is 20 to 40 bytes depending on
1700 ** page cache memory is needed beyond what is provided by this option, then
1760 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1761 ** object and uses it for page cache memory allocations.</dd>
1766 ** page cache implementation into that object.)^ </dd>
2081 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
5383 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5384 #define SQLITE_MUTEX_STATIC_LRU2 7 /* lru page list */
5516 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
5535 ** <dd>This parameter returns the number of bytes of page cache
5541 ** no space was left in the page cache.</dd>)^
5684 ** CAPI3REF: Custom Page Cache Object
5698 ** CAPI3REF: Application Defined Page Cache.
5699 ** KEYWORDS: {page cache}
5703 ** register an alternative page cache implementation by passing in an
5705 ** heap memory used by SQLite is used by the page cache to cache data read
5707 ** custom page cache using this API, an application can control more
5722 ** required by the custom page cache implementation.
5742 ** will the page size of the database file that is to be cached plus an
5744 ** extra R bytes on each page to store metadata about the underlying
5745 ** database page on disk. The value of R depends
5753 ** never invoke xUnpin() except to deliberately delete a page.
5767 ** ^The xFetch() method is used to fetch a page and return a pointer to it.
5768 ** ^A 'page', in this context, is a buffer of szPage bytes aligned at an
5769 ** 8-byte boundary. ^The page to be fetched is determined by the key. ^The
5770 ** mimimum key value is 1. After it has been retrieved using xFetch, the page
5773 ** ^If the requested page is already in the page cache, then the page cache
5774 ** implementation must return a pointer to the page buffer with its content
5775 ** intact. ^(If the requested page is not already in the cache, then the
5780 ** <tr><th> createFlag <th> Behaviour when page is not already in cache
5781 ** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
5782 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
5784 ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
5785 ** NULL if allocating a new page is effectively impossible.
5795 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
5797 ** then the page should be evicted from the cache. In this case SQLite
5798 ** assumes that the next time the page is retrieved from the cache using
5800 ** zero, then the page is considered to be unpinned. ^The cache implementation
5804 ** call to xUnpin() unpins the page regardless of the number of prior calls
5808 ** page passed as the second argument from oldKey to newKey. ^If the cache
5814 ** existing cache entries with page numbers (keys) greater than or equal
5926 ** the destination is an in-memory database with a different page size
6705 ** The root-page of the master database table.
6952 ** For example, the free-page-count field is located at byte offset 36 of
6967 int iTable, /* Index of root page */
7496 ** This header file defines the interface that the sqlite page cache
7497 ** subsystem. The page cache subsystem reads and writes a file a page
7514 ** The type used to represent a page number. The first page in a file
7515 ** is called page 1. 0 is used to represent "not a page".
7530 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
7594 /* Functions used to obtain and release page references. */
7601 /* Operations on page references. */
7666 ** This header file defines the interface that the sqlite page cache
7676 ** Every page in the cache is controlled by an instance of the following
7680 void *pData; /* Content of this page */
7683 Pgno pgno; /* Page number for this page */
7684 Pager *pPager; /* The pager this page is part of */
7686 u32 pageHash; /* Hash of page content */
7694 i16 nRef; /* Number of users of this page */
7695 PCache *pCache; /* Cache that owns this page */
7702 #define PGHDR_DIRTY 0x002 /* Page has changed */
7704 ** writing this page to the database */
7709 /* Initialize and shutdown the page cache subsystem */
7713 /* Page cache buffer management:
7723 int szPage, /* Size of every page */
7724 int szExtra, /* Extra space associated with each page */
7731 /* Modify the page-size after the cache has been created. */
7739 /* One release per successful fetch. Page is pinned until released.
7745 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
7746 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
7747 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
7750 /* Change a page number. Used by incr-vacuum. */
7756 /* Get a list of all dirty pages in the cache, sorted by page number */
7762 /* Clear flags from pages of the page cache */
7768 /* Return the total number of outstanding page references */
7771 /* Increment the reference count of an existing page */
8022 ** that all locks will fit on a single page even at the minimum page size.
8024 ** is set high so that we don't have to allocate an unused page except
8025 ** for very large databases. But one should test the page skipping logic
8734 ** Table.tnum is the page number for the root BTree page of the table in the
8741 ** page number. Transient tables are used to hold the results of a
8953 int tnum; /* Page containing root of this index in database file */
9668 int regRoot; /* Register holding root page number for new objects */
9879 sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
9886 void *pPage; /* Page cache memory */
9887 int szPage; /* Size of each page in pPage[] */
10792 ** the database page that contains the pending byte. It never attempts
10793 ** to read or write that page. The pending byte page is set assign
11170 ** Reference: Meeus page 61
15688 /* Number of free pages for scratch and page-cache memory */
17602 u8 *aRow; /* Data for the current row, if all on one page */
22495 ** the first page of the database, no damage is done.
22679 ** the range of bytes on the locking page between SHARED_FIRST and
29661 ** This file implements that page cache.
29665 ** A complete page cache is an instance of this structure.
29669 PgHdr *pSynced; /* Last synced page in dirty page list */
29672 int szPage; /* Size of every page in this cache */
29673 int szExtra; /* Size of extra space for each page */
29675 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
29678 PgHdr *pPage1; /* Reference to page 1 */
29713 ** Remove page pPage from the list of dirty pages.
29749 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
29788 ** Initialize and shutdown the page cache subsystem. Neither of these
29815 int szPage, /* Size of every page */
29816 int szExtra, /* Extra space associated with each page */
29832 ** Change the page size for PCache object. The caller must ensure that there
29833 ** are no outstanding page references when this function is called.
29846 ** Try to obtain a page from the cache.
29849 PCache *pCache, /* Obtain the page from this cache */
29850 Pgno pgno, /* Page number to obtain */
29851 int createFlag, /* If true, create page if it does not exist already */
29852 PgHdr **ppPage /* Write the page here */
29884 /* Find a dirty page to write-out and recycle. First try to find a
29885 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
29887 ** unreferenced dirty page.
29934 ** Decrement the reference count on a page. If the page is clean and the
29946 /* Move the page to the head of the dirty list. */
29954 ** Increase the reference count of a supplied page by 1.
29962 ** Drop a page from the cache. There must be exactly one reference to the
29963 ** page. This function deletes that reference, so after it returns the
29964 ** page pointed to by p is invalid.
29981 ** Make sure the page is marked as dirty. If it isn't dirty already,
29994 ** Make sure the page is marked as clean. If it isn't clean already,
30008 ** Make every page in the cache clean.
30029 ** Change the page number of page p to newPgno.
30044 ** Drop every cache entry whose page number is greater than "pgno". The
30046 ** other than page 1 with a page number greater than pgno.
30048 ** If there is a reference to page 1 and the pgno parameter passed to this
30049 ** function is 0, then the data area associated with page 1 is zeroed, but
30050 ** the page object is not dropped.
30158 ** Return a list of all dirty pages in the cache, sorted by page number.
30176 ** Return the number of references to the page supplied as an argument.
30240 ** This file implements the default page cache implementation (the
30243 ** If the default page cache implementation is overriden, then neither of
30256 /* Cache configuration parameters. Page size (szPage) and the purgeable
30285 unsigned int iKey; /* Key value (page number) */
30287 PCache1 *pCache; /* Cache that currently owns this page */
30314 PgFreeslot *pFree; /* Free page blocks */
30347 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
30351 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
30426 ** Allocate a new page object initially associated with cache pCache.
30444 ** Free a page object allocated by pcache1AllocPage().
30528 ** This function is used internally to remove the page pPage from the
30557 ** Remove the page supplied as an argument from the hash table
30589 ** Discard all pages from cache pCache with a page number (key value)
30701 ** Fetch a page by key value.
30703 ** Whether or not a new page may be allocated by this function depends on
30705 ** page. 1 means allocate a new page if space is easily available. 2
30706 ** means to try really hard to allocate a new page.
30713 ** There are three different approaches to obtaining space for a page,
30717 ** copy of the requested page. If one is found, it is returned.
30719 ** 2. If createFlag==0 and the page is not already in the cache, NULL is
30722 ** 3. If createFlag is 1, and the page is not already in the cache,
30741 ** then attempt to recycle a page from the LRU list. If it is the right
30745 ** 5. Otherwise, allocate and return a new page buffer.
30780 /* Step 4. Try to recycle a page buffer if appropriate. */
30795 /* Step 5. If a usable page buffer has still not been found,
30827 ** Mark a page as unpinned (eligible for asynchronous recycling).
30836 /* It is an error to call this function if the page is already
30846 /* Add the page to the global LRU list. Normally, the page is added to
30847 ** the head of the list (last page to be recycled). However, if the
30848 ** reuseUnlikely flag passed to this function is true, the page is added
30849 ** to the tail of the list (first page to be recycled).
30904 ** Discard all unpinned pages in the cache with a page number equal to
30905 ** or greater than parameter iLimit. Any pinned pages with a page number
31444 ** This is the implementation of the page cache subsystem or "pager".
31478 ** The page cache as a whole is always in one of the following
31481 ** PAGER_UNLOCK The page cache is not currently reading or
31486 ** PAGER_SHARED The page cache is reading the database.
31498 ** PAGER_EXCLUSIVE The page cache is writing the database.
31510 ** The page cache comes up in PAGER_UNLOCK. The first time a
31516 ** called on an outstanding page which means that the pager must
31576 ** A open page cache is an instance of the following structure.
31623 ** The dbModified flag is set whenever a database page is dirtied.
31711 Bitvec *pInJournal; /* One bit for each page in the database file */
31722 u16 nExtra; /* Add this many bytes to each in-memory page */
31723 i16 nReserve; /* Number of unused bytes at end of each page */
31725 int pageSize; /* Number of bytes in a page */
31738 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
31744 PCache *pPCache; /* Pointer to page cache object */
31777 ** of a 32-bit checksum on each page of data. The checksum covers both
31778 ** the page number and the pPager->pageSize bytes of data for the page.
31792 ** The size of the of each page record in the journal is given by
31816 ** The maximum legal page number is (2^31 - 1).
31839 ** Return true if it is necessary to write page *pPg into the sub-journal.
31840 ** A page needs to be written into the sub-journal if there exists one
31843 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
31844 ** * The bit corresponding to the page-number is not set in
31861 ** Return true if the page is already in the journal file.
31927 ** a database page may be written atomically, and
31929 ** to the page size.
31937 ** contains rollback data for exactly one page.
31945 int szPage; /* Page size */
31970 ** Return a 32-bit hash of the page data for pPage.
31990 ** that the page is either dirty or still matches the calculated page-hash.
32158 ** - 4 bytes: Random number used for page hash.
32159 ** - 4 bytes: Initial database page count.
32161 ** - 4 bytes: Database page size.
32191 ** Write the nRec Field - the number of page records that follow this
32199 ** rest of the journal file contains valid page records. This assumption
32228 /* The page size */
32252 ** database page size. Since the zHeader buffer is only Pager.pageSize
32273 ** page records following this header and *pDbSize is set to the size of the
32332 u32 iPageSize; /* Page-size field of journal header */
32336 /* Read the page-size and sector-size journal header fields. */
32343 /* Check that the values read from the page-size and sector-size fields
32352 /* If the either the page-size or sector-size in the journal-header is
32360 /* Update the page-size to match the value read from the journal.
32396 ** The master journal page checksum is the sum of the bytes in the master
32424 ** the master journal name. This is in case the previous page written to
32465 ** Find a page in the hash table given its page number. Return
32466 ** a pointer to the page or NULL if the requested page is not
32780 ** page of data and the current value of pPager->cksumInit.
32784 ** of the page data, starting with byte offset (pPager->pageSize%200).
32807 ** Read a single page from either the journal file (if isMainJrnl==1) or
32808 ** from the sub-journal (if isMainJrnl==0) and playback that page.
32809 ** The page begins at offset *pOffset into the file. The *pOffset
32810 ** value is increased to the start of the next page in the journal.
32816 ** If the page number of the page record read from the (sub-)journal file
32821 ** been played back. If the page at *pOffset has already been played back
32823 ** Make sure the pDone bit corresponding to the *pOffset page is set
32826 ** If the page record is successfully read from the (sub-)journal file
32834 ** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
32853 PgHdr *pPg; /* An existing page in the cache */
32854 Pgno pgno; /* The page number of a page in journal */
32856 char *aData; /* Temporary storage for the page */
32867 /* Read the page number and page data from the journal or sub-journal
32877 /* Sanity checking on the page. This is more important that I originally
32904 ** page in the pager cache. In this case just update the pager cache,
32905 ** not the database file. The page is left marked dirty in this case.
32908 ** and a page is moved during an incremental vacuum then the page may
32910 ** during a Movepage() call, then the page may not be in the cache
32915 ** and the main file. The page is then marked not dirty.
32917 ** Ticket #1171: The statement journal might contain page content that is
32918 ** different from the page content at the start of the transaction.
32919 ** This occurs when a page is changed prior to the start of a statement
32922 ** for certain that original page contents are synced into the main rollback
32927 ** locked. (2) we know that the original page content is fully synced
32928 ** in the main journal either because the page is not in cache or else
32929 ** the page is marked as needSync==0.
32937 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
32958 ** the database and the page is not in-memory, there is a potential
32959 ** problem. When the page is next fetched by the b-tree layer, it
32965 ** if the page is on the free-list at the start of the transaction, then
32968 ** The solution is to add an in-memory page to the cache containing
32969 ** the data just read from the sub-journal. Mark the page as dirty
32970 ** and if the pager requires a journal-sync, then mark the page as
32981 /* No page should ever be explicitly rolled back that is in use, except
32982 ** for page 1 which is held in use in order to keep the lock on the
32983 ** database active. However such a page may be rolled back as a result
32992 /* If the contents of this page were just restored from the main
32994 ** transaction was first opened. In this case we can mark the page
32997 ** There is one exception to this rule. If the page is being rolled
33000 ** to mark the page as clean. This is because marking the page as
33001 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
33003 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
33015 /* If this was page 1, then restore the value of Pager.dbFileVers.
33021 /* Decode the page just read from disk */
33068 ** a couple of kilobytes or so - potentially larger than the page
33244 ** (2) 4 byte big-endian integer which is the number of valid page records
33246 ** number of page records from the journal size.
33253 ** (6) 4 byte big-endian integer which is the page size.
33256 ** + 4 byte page number.
33264 ** valid page entries in the journal. In most cases, you can compute the
33279 ** journal file then all pages up to the first corrupted page are rolled
33303 int needPagerReset; /* True to reset page prior to first page rollback */
33399 ** database file and/or page cache.
33499 ** Throughout the rollback process, each time a page is rolled back, the
33501 ** implementation below). This is used to ensure that a page is only
33509 ** (or transaction). No page with a page-number greater than this value
33542 ** There might be records in the main journal that have a page number
33587 /* Finally, rollback pages from the sub-journal. Page that were
33723 ** Report the current page size and number of reserved bytes back
33738 ** Change the page size used by the Pager object. The new page size
33747 ** * the new page size (value of *pPageSize) is valid (a power
33750 ** * there are no outstanding page references, and
33755 ** then the pager object page size is set to *pPageSize.
33757 ** If the page size is changed, then this function uses sqlite3PagerMalloc()
33759 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
33762 ** If the page size is not changed, either because one of the enumerated
33765 ** then *pPageSize is set to the old, retained page size before returning.
33798 ** Return a pointer to the "temporary page" buffer held internally
33800 ** entire content of a database page. This buffer is used internally
33810 ** Attempt to set the maximum database page count if mxPage is positive.
33812 ** maximum page count below the current size of the database.
33814 ** Regardless of mxPage, return the current maximum page count.
33878 ** with pPager. Normally, this is calculated as (<db file size>/<page-size>).
33879 ** However, if the file is between 1 and <page-size> bytes in size, then
33880 ** this is considered a 1 page file.
33993 ** following is true for all dirty pages currently in the page-cache:
33995 ** a) The page number is less than or equal to the size of the
33998 ** b) if the page content were written at this time, it would not
34003 ** dirty page were to be discarded from the cache via the pagerStress()
34004 ** routine, pagerStress() would not write the current page content to
34007 ** content of the page. However, since this content is not present in either
34040 ** Shutdown the page cache. Free all memory and close all files.
34044 ** and their memory is freed. Any attempt to use a page associated
34045 ** with this page cache after this function returns will likely
34092 ** Return the page number for page pPg.
34100 ** Increment the reference count for page pPg.
34141 ** page currently held in memory before returning SQLITE_OK. If an IO
34254 ** a page is skipped if it meets either of the following criteria:
34256 ** * The page number is greater than Pager.dbSize, or
34257 ** * The PGHDR_DONT_WRITE flag is set on the page.
34259 ** If writing out a page causes the database file to grow, Pager.dbFileSize
34260 ** is updated accordingly. If page 1 is written out, then the value cached
34306 /* If there are dirty pages in the page cache with page numbers greater
34311 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
34323 /* Write out the page data. */
34326 /* If page 1 was just written, update Pager.dbFileVers to match
34328 ** page caused the database file to grow, update dbFileSize.
34340 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
34346 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
34358 ** Append a record of the current state of page pPg to the sub-journal.
34379 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
34400 ** database). The second argument is a reference to a page that is
34401 ** currently dirty but has no outstanding references. The page
34409 ** If successful, sqlite3PcacheMakeClean() is called on the page and
34411 ** page clean, the IO error code is returned. If the page cannot be
34426 ** set of pages are synced to disk together. So, if the page this function
34429 ** just have to go ahead and allocate a new page buffer instead of
34453 /* If the page number of this page is larger than the current size of
34461 ** <journal page X>
34462 ** <modify page X>
34465 ** pagerStress(page X)
34468 ** If (X>Y), then when pagerStress is called page X will not be written
34470 ** following the "ROLLBACK TO sp" statement, reading page X will read
34471 ** data from the database file. This will be the copy of page X as it
34475 ** The solution is to write the current data for page X into the
34486 /* Write the contents of the page out to the database file. */
34492 /* Mark the page as clean. */
34494 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
34515 ** along with each page reference. This space is available to the user
34536 int nExtra, /* Extra bytes append to each in-memory page */
34553 u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
34662 ** choose a default page size in case we have to create the
34663 ** database file. The default page size is the maximum of:
34667 ** + The largest page size that can be written atomically.
34696 ** In this case we accept the default page size and delay actually
34897 ** Read the content for page pPg out of the database file and into
34901 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
34908 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
34909 Pgno pgno = pPg->pgno; /* Page number to read */
34935 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
34961 ** the contents of the page cache and rolling back any open journal
35168 ** Acquire a reference to page number pgno in pager pPager (a page
35172 ** If the requested page is already in the cache, it is returned.
35173 ** Otherwise, a new page object is allocated and populated with data
35175 ** choose not to allocate a new page object and may reuse an existing
35178 ** The extra data appended to a page is always initialized to zeros the
35179 ** first time a page is loaded into memory. If the page requested is
35181 ** data is left as it was when the page object was last used.
35183 ** If the database image is smaller than the requested page or if a
35185 ** requested page is not already stored in the cache, then no
35187 ** page is initialized to all zeros.
35190 ** of the page. This occurs in two seperate scenarios:
35192 ** a) When reading a free-list leaf page from the database, and
35195 ** a new page into the cache to populate with the data read
35202 ** savepoints are set. This means if the page is made writable at any
35210 ** to find a page in the in-memory cache first. If the page is not already
35219 Pgno pgno, /* Page number to fetch */
35220 DbPage **ppPage, /* Write a pointer to the page here */
35234 ** Otherwise, request the page from the PCache layer. */
35253 ** the page. Return without further ado. */
35259 /* The pager cache has created a new page. Its content needs to
35267 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
35268 ** number greater than this, or the unused locking-page, is requested. */
35287 ** page that does not need to be journaled. Nevertheless, be sure
35328 ** Acquire a page if it is already in the in-memory cache. Do
35329 ** not read the page from disk. Return a pointer to the page,
35330 ** or 0 if the page is not in cache. Also, return 0 if the
35336 ** in the page if the page is not already in cache. This routine
35337 ** returns NULL if the page is not in cache or if a disk I/O error
35351 ** Release a page reference.
35353 ** If the number of references to the page drop to zero, then the
35354 ** page is added to the LRU list. When all references to all pages
35562 ** Mark a single data page as writeable. The page is written into the
35563 ** main journal or sub-journal as required. If the page is written into
35591 /* Mark the page as dirty. If the page has already been written
35599 /* If we get this far, it means that the page needs to be
35619 ** EXCLUSIVE lock on the main database file. Write the current page to
35627 /* We should never write to the journal file the page that
35646 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
35651 ** page in the block above, set the need-sync flag for the page.
35653 ** playback_one_page() will think that the page needs to be restored
35684 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
35690 /* If the statement journal is open and the page is not in it,
35691 ** then write the current page to the statement journal. Note that
35710 ** Mark a data page as writeable. This routine must be called before
35711 ** making changes to a page. The caller must check the return value
35712 ** of this function and be careful not to change any page data unless
35732 Pgno pg1; /* First page of the sector pPg is located on. */
35735 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
35744 /* This trick assumes that both the page-size and sector-size are
35746 ** of the first page of the sector pPg is located on.
35812 ** Return TRUE if the page given in the argument was previously passed
35814 ** to change the content of the page.
35825 ** write the information on page pPg back to the disk, even though
35826 ** that page might be marked as dirty. This happens, for example, when
35827 ** the page has been added as a leaf of the freelist and so its
35831 ** on the given page is unused. The pager marks the page as clean so
35840 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
35856 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
35857 ** page data. In this case the file will be updated when the current
35863 ** by writing an updated version of page 1 using a call to the
35889 PgHdr *pPgHdr; /* Reference to page 1 */
35894 /* Open page 1 of the file for writing. */
35898 /* If page one was fetched successfully, and this function is not
35899 ** operating in direct-mode, make page 1 writable. When not in
35900 ** direct mode, page 1 is always held in cache and hence the PagerGet()
35913 /* If running in direct mode, write the contents of page 1 to the file. */
35926 /* Release the page reference. */
36008 ** blocks of size page-size, and
36010 ** * Exactly one page has been modified and store in the journal file.
36034 ** following call will modify the in-memory representation of page 1
36035 ** to include the updated change counter and then write page 1
36055 ** Before reading the pages with page numbers larger than the
36069 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
36074 PgHdr *pPage; /* Page to journal */
36279 ** Return the number of references to the specified page.
36513 ** Move the page pPg to location pgno in the file.
36515 ** There must be no references to the page previously located at
36516 ** pgno (which we call pPgOld) though that page is allowed to be
36517 ** in cache. If the page previously located at pgno is not already
36520 ** References to the page pPg remain valid. Updating any
36522 ** allocated along with the page) is the responsibility of the caller.
36526 ** has been removed (CREATE INDEX needs to move a page when a statement
36529 ** If the fourth argument, isCommit, is non-zero, then this page is being
36531 ** is being committed. In this case, it is guaranteed that the database page
36538 PgHdr *pPgOld; /* The page being overwritten. */
36541 Pgno origPgno; /* The original page number */
36546 ** the page we are moving from.
36553 /* If the page being moved is dirty and has not been saved by the latest
36554 ** savepoint, then save the current contents of the page into the
36558 ** <journal page X, then modify it in memory>
36560 ** <Move page X to location Y>
36563 ** If page X were not written to the sub-journal here, it would not
36578 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
36582 /* If the journal needs to be sync()ed before page pPg->pgno can
36586 ** the journal needs to be sync()ed before database page pPg->pgno
36596 /* If the cache contains a page with page-number pgno, remove it
36598 ** page pgno before the 'move' operation, it needs to be retained
36599 ** for the page moved there.
36608 ** need to rollback later. Just move the page out of the way. */
36623 ** sync()ed before any data is written to database file page needSyncPgno.
36624 ** Currently, no such page exists in the page-cache and the
36626 ** loading the page into the pager-cache and setting the PgHdr.needSync
36629 ** If the attempt to load the page into the page-cache fails, (due
36631 ** array. Otherwise, if the page is loaded and written again in
36657 ** For an in-memory database, make sure the original page continues
36659 ** as the original page since it has already been allocated.
36671 ** Return a pointer to the data for the specified page.
36680 ** allocated along with the specified page.
36815 ** The basic idea is that each page of the file contains N database
36822 ** All of the keys on the page that Ptr(0) points to have values less
36823 ** than Key(0). All of the keys on page Ptr(1) and its subpages have
36832 ** BTrees. Each BTree is identified by the index of its root page. The
36835 ** page. If the payload is larger than the preset amount then surplus
36838 ** page has a small header which contains the Ptr(N) pointer and other
36843 ** The file is divided into pages. The first page is called page 1,
36844 ** the second is page 2, and so forth. A page number of zero indicates
36845 ** "no such page". The page size can be any power of 2 between 512 and 32768.
36846 ** Each page can be either a btree page, a freelist page, an overflow
36847 ** page, or a pointer-map page.
36849 ** The first page is always a btree page. The first 100 bytes of the first
36850 ** page contain a special header (the "file header") that describes the file.
36855 ** 16 2 Page size in bytes.
36858 ** 20 1 Bytes of unused space at the end of each page
36864 ** 32 4 First freelist page
36870 ** 48 4 Size of page cache
36871 ** 52 4 Largest root-page (auto/incr_vacuum)
36886 ** space in a page that can be consumed by a single cell for standard
36889 ** on one page. Thus the default max embedded payload fraction is 64.
36892 ** payload is spilled to overflow pages. Once an overflow page is allocated,
36902 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
36903 ** file header that occurs before the page header.
36906 ** | file header | 100 bytes. Page 1 only.
36908 ** | page header | 8 bytes for leaves. 12 bytes for interior nodes
36921 ** The page headers looks like this:
36926 ** 3 2 number of cells on this page
36931 ** The flags define the format of this btree page. The leaf flag means that
36932 ** this page has no children. The zerodata flag means that this page carries
36937 ** The cell pointer array begins on the first byte after the page header.
36939 ** offsets from the beginning of the page to the cell content in the cell
36942 ** be easily added without having to defragment the page.
36944 ** Cell content is stored at the very end of the page and grows toward the
36945 ** beginning of the page.
36954 ** in the page header at offset 7.
36961 ** the end of the page. Pointers to the cells are in the cell pointer array
36962 ** that immediately follows the page header. Cells is not necessarily
36987 ** 4 Page number of the left child. Omitted if leaf flag is set.
36991 ** 4 First page of the overflow chain. Omitted if no overflow
36993 ** Overflow pages form a linked list. Each page except the last is completely
36994 ** filled with data (pagesize - 4 bytes). The last page can have as little
36998 ** 4 Page number of next overflow page
37002 ** file header points to the first in a linked list of trunk page. Each trunk
37003 ** page points to multiple leaf pages. The content of a leaf page is
37004 ** unspecified. A trunk page looks like this:
37007 ** 4 Page number of next trunk page
37008 ** 4 Number of leaf pointers on this page
37013 /* The following value is the maximum cell size assuming a maximum page
37018 /* The maximum number of cells on a single page of the database. This
37020 ** plus 2 bytes for the index to the cell in the page header). Such
37046 ** Page type flags. An ORed combination of these flags appear as the
37047 ** first byte of on-disk image of every BTree page.
37055 ** As each page of the file is loaded into memory, an instance of the following
37057 ** information about the page that is decoded from the raw file page.
37059 ** The pParent field points back to the parent page. This allows us to
37061 ** unref() the parent page pointer when this page is no longer referenced.
37072 u8 hasData; /* True if this page stores data */
37073 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
37078 u16 nFree; /* Number of free bytes on the page */
37079 u16 nCell; /* Number of cells on this page, local and ovfl */
37080 u16 maskPage; /* Mask for page offset */
37085 BtShared *pBt; /* Pointer to BtShared that this page is part of */
37086 u8 *aData; /* Pointer to disk image of the page data */
37087 DbPage *pDbPage; /* Pager page handle */
37088 Page number for this page */
37092 ** The in-memory image of a disk page has the auxiliary information appended
37101 ** is opened on the table with root page BtShared.iTable. Locks are removed
37107 Pgno iTable; /* Root page of table */
37148 BtLock lock; /* Object used to lock page 1 */
37199 Pager *pPager; /* The page cache */
37202 MemPage *pPage1; /* First page of the database */
37204 u8 pageSizeFixed; /* True if the page size can no longer be changed */
37209 u16 pageSize; /* Total number of bytes on a page */
37210 u16 usableSize; /* Number of usable bytes on each page */
37235 ** based on information extract from the raw disk page.
37245 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
37246 u16 nSize; /* Size of the cell content on the main b-tree page */
37279 Pgno pgnoRoot; /* The root page of this tree */
37291 Pgno *aOverflow; /* Cache of overflow page locations */
37293 i16 iPage; /* Index of current page in apPage */
37294 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
37329 ** The database page the PENDING_BYTE occupies. This page is never used.
37335 ** database page. The first argument to each is the number of usable
37336 ** bytes on each page of the database (often 1024). The second is the
37337 ** page number to look up in the pointer map.
37339 ** PTRMAP_PAGENO returns the database page number of the pointer-map
37340 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
37343 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
37345 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
37353 ** The pointer map is a lookup table that identifies the parent page for
37354 ** each child page in the database file. The parent page is the page that
37355 ** contains a pointer to the child. Every page in the database contains
37356 ** 0 or 1 parent pages. (In this context 'database page' refers
37357 ** to any page that is not part of the pointer map itself.) Each pointer map
37358 ** entry consists of a single byte 'type' and a 4 byte parent page number.
37362 ** position in the file to another as part of autovacuum. When a page
37364 ** new location. The pointer map is used to locate the parent page quickly.
37366 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
37369 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
37372 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
37373 ** overflow pages. The page number identifies the page that
37374 ** contains the cell with a pointer to this overflow page.
37376 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
37377 ** overflow pages. The page-number identifies the previous
37378 ** page in the overflow page list.
37380 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
37381 ** identifies the parent page in the btree.
37420 int *anRef; /* Number of times each page is referenced */
37872 ** table with root page iRoot. Return 1 if it does and 0 if not.
37874 ** For example, when writing to a table with root-page iRoot via
37880 ** caller should have first obtained a lock specifying the root page of
37886 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
37887 ** hold a write-lock on the schema table (root page 1). This is also
37892 Pgno iRoot, /* Root page of b-tree */
37919 /* Figure out the root-page that the lock should be held on. For table
37920 ** b-trees, this is just the root page of the b-tree being read or
37921 ** written. For index b-trees, it is the root page of the associated
37935 /* Search for the required lock. Either a write-lock on root-page iTab, a
37967 ** rooted at page iRoot, one should call:
37987 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
38047 ** Add a lock on the table with root-page iTable to the shared-btree used
38204 ** Invalidate the overflow page-list cache for cursor pCur, if any.
38213 ** Invalidate the overflow page-list cache for all cursors opened
38231 ** cursors open on any row within the table with root-page pgnoRoot.
38261 ** when a page that previously contained data becomes a free-list leaf
38262 ** page.
38268 ** 1) When all data is deleted from a page and the page becomes
38269 ** a free-list leaf page, the page is not written to the database
38271 ** such a page is not even journalled (as it will not be modified,
38274 ** 2) When a free-list leaf page is reused, its content is not read
38280 ** a page is moved to the free-list and then reused within the same
38281 ** transaction, a problem comes up. If the page is not journalled when
38287 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
38288 ** moved to become a free-list leaf page, the corresponding bit is
38289 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
38315 ** This function is called when a free-list leaf page is removed from the
38317 ** page from the pager layer with the 'no-content' flag set. True otherwise.
38387 ** the table with root-page iRoot. Usually, this is called just before cursor
38502 ** Given a page number of a regular database page, return the page
38503 ** number for the pointer-map page that contains the entry for the
38504 ** input page number.
38522 ** This routine updates the pointer map entry for page number 'key'
38523 ** so that it maps to type 'eType' and parent page number 'pgno'.
38530 DbPage *pDbPage; /* The pointer map page */
38532 Pgno iPtrmap; /* The pointer map page number */
38533 int offset; /* Offset in pointer map page */
38539 /* The master-journal page number must never be used as a pointer map page */
38576 ** This routine retrieves the pointer map entry for page 'key', writing
38577 ** the type and parent page number to *pEType and *pPgno respectively.
38581 DbPage *pDbPage; /* The pointer map page */
38582 int iPtrmap; /* Pointer map page index */
38583 u8 *pPtrmap; /* Pointer map page data */
38613 ** Given a btree page and a cell index (0 means the first cell on
38614 ** the page, 1 means the second cell, and so forth) return a pointer
38654 MemPage *pPage, /* Page containing the cell */
38686 ** on the local page. No overflow is required.
38697 /* If the payload will not fit completely on the local page, we have
38727 MemPage *pPage, /* Page containing the cell */
38736 ** data area of the btree-page. The return number includes the cell
38737 ** data header and the local payload, but not any overflow page or
38803 ** If the cell pCell, part of page pPage contains a pointer
38804 ** to an overflow page, insert an entry into the pointer-map
38805 ** for the overflow page.
38822 ** Defragment the page given. All Cells are moved to the
38823 ** end of the page and all free space is collected into one
38830 int hdr; /* Offset to the page header */
38832 int usableSize; /* Number of usable bytes on a page */
38835 int nCell; /* Number of cells on the page */
38836 unsigned char *data; /* The page data */
38905 ** Allocate nByte bytes of space from within the B-Tree page passed
38924 int usableSize; /* Usable size of the page */
38967 ** fragmented bytes within the page. */
38998 ** is no way that the allocation can extend off the end of the page.
39038 ** cell content area exceeds the value in the page header. If these
39098 ** Decode the flags byte (the first byte of the header) for a page
39137 ** Return SQLITE_OK on success. If we see that the page does
39138 ** not contain a well-formed database page, then return
39140 ** guarantee that the page is well-formed. It only shows that
39153 u8 hdr; /* Offset to beginning of page header */
39156 u16 usableSize; /* Amount of usable space on each page */
39157 u16 cellOffset; /* Offset from start of page to first cell pointer */
39158 u16 nFree; /* Number of unused bytes on the page */
39176 /* To many cells for a single page. The page must be corrupt */
39181 /* A malformed database page might cause us to read past the end
39182 ** of page when parsing a cell.
39185 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
39213 /* Compute the total free space on the page */
39219 /* Start of free block is off the page */
39226 ** the free-block must lie on the database page. */
39236 ** of the page, then the page must be corrupted. This check also
39238 ** area, according to the page header, lies within the page.
39250 ** Set up a raw page so that it looks like a database page holding
39299 ** Get a page from the pager. Initialize the MemPage.pBt and
39303 ** the content of the page at this time. So do not go to the disk
39305 ** If in the future we call sqlite3PagerWrite() on this page, that
39311 Pgno pgno, /* Number of the page to fetch */
39312 MemPage **ppPage, /* Return the page in this parameter */
39313 int noContent /* Do not load page content if true */
39326 ** Retrieve a page from the pager cache. If the requested page is not
39354 ** Get a page from the pager and initialize it. This routine is just a
39363 Pgno pgno, /* Number of the page to get */
39364 MemPage **ppPage /* Write the page pointer here */
39378 /* If the requested page number was either 0 or greater than the page
39379 ** number of the last page in the database, this function should return
39407 ** the transaction, for each page restored this routine is called.
39410 ** page to agree with the restored data.
39420 /* pPage might not be a btree page; it might be an overflow page
39421 ** or ptrmap page or a free page. In those cases, the following
39424 ** btreeInitPage() be called on every btree page so we make
39425 ** the call for every page that comes in for re-initing. */
39468 u8 nReserve; /* Byte of unused space on each page */
39864 ** Change the default pages size and the number of reserved bytes per page.
39865 ** Or, if the page size has already been fixed, return SQLITE_READONLY
39868 ** The page size must be a power of 2 between 512 and 65536. If the page
39869 ** size supplied does not meet this constraint then the page size is not
39872 ** Page sizes are constrained to be a power of two so that the region
39875 ** at the beginning of a page.
39878 ** bytes per page is left unchanged.
39880 ** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
39911 ** Return the currently defined page size
39918 ** Return the number of bytes of space at the end of every page that
39931 ** Set the maximum page count for a database if mxPage is positive.
39933 ** Regardless of the value of mxPage, return the maximum page count.
40050 /* After reading the first page of the database assuming a page size
40051 ** of BtShared.pageSize, we have discovered that the page-size is
40054 ** again with the correct page-size.
40077 ** cells can will fit on one page. We assume a 10-byte page header.
40083 ** 4-byte overflow page pointer
40086 ** page pointer.
40105 ** this routine unrefs the first page of the database file which
40124 ** into a new empty database by initializing the first page of
40246 ** page 1. So if some other shared-cache client already has a write-lock
40247 ** on page 1, the transaction cannot be opened. */
40255 ** reading page 1 it discovers that the page-size of the database
40257 ** pBt->pageSize to the page-size of the file on disk.
40321 ** Set the pointer-map entries for all children of page pPage. Also, if
40327 int nCell; /* Number of cells in page pPage */
40362 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
40366 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
40367 ** page of pPage.
40369 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
40370 ** page pointed to by one of the cells on pPage.
40372 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
40373 ** overflow page in the list.
40379 /* The pointer is always the first 4 bytes of the page in this case. */
40426 ** Move the open database page pDbPage to location iFreePage in the
40430 ** the journal needs to be sync()ed before database page pDbPage->pgno
40432 ** page.
40436 MemPage *pDbPage, /* Open page to move */
40438 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
40442 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
40452 /* Move page iDbPage from its current location to page number iFreePage */
40453 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
40461 /* If pDbPage was a btree-page, then it may have child pages and/or cells
40465 ** If pDbPage is an overflow page, then the first 4 bytes may store a
40466 ** pointer to a subsequent overflow page. If this is the case, then
40467 ** the pointer map needs to be updated for the subsequent overflow page.
40484 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
40516 ** database so that the last page of the file currently in use
40554 /* Remove the page from the files free-list. This is not required
40569 Pgno iFreePg; /* Index of free page to move pLastPg to */
40577 /* If nFin is zero, this loop runs exactly once and page pLastPg
40578 ** is swapped with the first free page pulled off the free list.
40581 ** looping until a free-page located within the first nFin pages
40673 Pgno iFree; /* The next page to be freed */
40674 int nEntry; /* Number of entries on one ptrmap page */
40679 /* It is not possible to create a database for which the final page
40680 ** is either a pointer-map page or the pending-byte page. If one
40953 ** call btreeGetPage() on page 1 again to make
41037 ** Create a new cursor for the BTree whose root is on the page
41061 ** No checking is done to make sure that page iTable really is the
41062 ** root page of a b-tree. If it is not, then the cursor acquired
41070 int iTable, /* Root page of table to open */
41118 int iTable, /* Root page of table to open */
41156 ** as pCur and having the same root page number as pCur. The value is
41319 ** Given the page number of an overflow page in the database (parameter
41320 ** ovfl), this function finds the page number of the next page in the
41322 ** pointer-map data instead of reading the content of page ovfl to do so.
41326 ** The page number of the next overflow page in the linked list is
41327 ** written to *pPgnoNext. If page ovfl is the last page in its linked
41331 ** to page number pOvfl was obtained, then *ppPage is set to point to that
41339 Pgno ovfl, /* Current overflow page number */
41341 Pgno *pPgnoNext /* OUT: Next overflow page number */
41351 /* Try to find the next page in the overflow list using the
41352 ** autovacuum pointer-map pages. Guess that the next page in
41353 ** the overflow list is page number (ovfl+1). If that guess turns
41354 ** out to be wrong, fall back to loading the data of page
41355 ** number ovfl to determine the next page number.
41395 ** Copy data from a buffer to a page, or from a page to a buffer.
41397 ** pPayload is a pointer to data stored on database page pDbPage.
41406 void *pPayload, /* Pointer to page data */
41409 int eOp, /* 0 -> copy from page, 1 -> copy to page */
41410 DbPage *pDbPage /* Page containing pPayload */
41413 /* Copy data from buffer to page (a write operation) */
41420 /* Copy data from page to buffer (a read operation) */
41436 page
41441 ** allocates space for and lazily popluates the overflow page-list
41445 ** Once an overflow page-list cache has been allocated, it may be
41448 ** mode, the following events may invalidate an overflow page-list cache.
41452 ** * Creating a table (may require moving an overflow page).
41465 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
41484 /* Check if data must be read/written to/from the btree page itself. */
41499 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
41507 ** one entry for each overflow page in the overflow chain. The
41508 ** page number of the first overflow page is stored in aOverflow[0],
41522 /* If the overflow page-list cache has been allocated and the
41523 ** entry for the first required overflow page is valid, skip
41536 /* If required, populate the overflow page-list cache. */
41544 /* The only reason to read this page is to obtain the page
41545 ** number for the next page in the overflow chain. The page
41547 ** page-list cache, if any, then fall back to the getOverflowPage()
41558 /* Need to read this page properly. It contains some of the
41644 ** and data to fit on the local page and for there to be no overflow
41651 ** page of the database. The data might change or move the next time
41695 ** b-tree page. Write the number of available bytes into *pAmt.
41727 ** Move the cursor down to a new child page. The newPgno argument is the
41728 ** page number of the child page to move to.
41730 ** This function returns SQLITE_CORRUPT if the page-header flags field of
41731 ** the new child page does not match the flags field of the parent (i.e.
41732 ** if an intkey page appears to be the parent of a non-intkey page, or
41763 ** Page pParent is an internal (non-leaf) tree page. This function
41764 ** asserts that page number iChild is the left-child if the iIdx'th
41765 ** cell in page pParent. Or, if iIdx is equal to the total number of
41766 ** cells in pParent, that page number iChild is the right-child of
41767 ** the page.
41782 ** Move the cursor up to the parent page.
41785 ** to the page we are coming from. If we are coming from the
41786 ** right-most child page then pCur->idx is set to one more than
41806 ** Move the cursor to point to the root page of its b-tree structure.
41808 ** If the table has a virtual root page, then the cursor is moved to point
41809 ** to the virtual root page instead of the actual root page. A table has a
41810 ** virtual root page when the actual root page contains no cells and a
41811 ** single child page. This can only happen with the table rooted at page 1.
41815 ** cell located on the root (or virtual root) page and the cursor state
41819 ** page-header flags indicate that the [virtual] root-page is the expected
41820 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
41868 /* Assert that the root page is of the correct type. This must be the
41869 ** case as the call to this function that loaded the root-page (either
41873 ** to the page. */
41919 ** page to which it is currently pointing. Notice the difference
41922 ** finds the right-most entry beneath the *page*.
42019 ** left pointing at a leaf page which would hold the entry if it
42087 /* pPage->nCell must be greater than zero. If this is the root-page
42089 ** not run. If this is not the root-page, then the moveToChild() routine
42091 ** be the right kind (index or table) of b-tree page. Otherwise
42126 /* The maximum supported page-size is 32768 bytes. This means that
42128 ** page is at most 8198 bytes, which may be stored as a 2-byte
42131 ** stored entirely within the b-tree page by inspecting the first
42138 ** b-tree page. */
42144 ** fits entirely on the main b-tree page. */
42361 ** Allocate a new page from the database file.
42363 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
42364 ** has already been called on the new page.) The new page has also
42366 ** sqlite3PagerUnref() on the new page when it is done.
42373 ** locate a page close to the page number "nearby". This can be used in an
42377 ** If the "exact" parameter is not 0, and the page-number nearby exists
42410 ** shows that the page 'nearby' is somewhere on the free-list, then
42411 ** the entire-list will be searched for that page.
42428 ** first free-list trunk page. iPrevTrunk is initially 1.
42435 ** is not true. Otherwise, it runs once for each trunk-page on the
42436 ** free-list until the page 'nearby' is located.
42459 ** So extract the trunk page itself and use it as the newly
42460 ** allocated page */
42477 /* The list is being searched and this trunk page is the page
42494 /* The trunk page is required by the caller but it contains
42496 ** page in this case.
42592 /* There are no pages on the freelist, so create a new page at the
42603 /* If *pPgno refers to a pointer-map page, allocate two new pages
42604 ** at the end of the file instead of one. The first allocated page
42605 ** becomes a new pointer-map page, the second is used by the caller.
42608 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
42649 ** This function is used to add page iPage to the database file free-list.
42650 ** It is assumed that the page is not already a part of the free-list.
42654 ** corresponding to page iPage handy, it may pass it as the second value.
42661 MemPage *pTrunk = 0; /* Free-list trunk page */
42662 Pgno iTrunk = 0; /* Page number of free-list trunk page */
42663 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
42664 MemPage *pPage; /* Page being freed. May be NULL. */
42679 /* Increment the free page count on pPage1 */
42698 ** to indicate that the page is free.
42707 ** trunk page in the free-list is full, then this page will become a
42708 ** new free-list trunk page. Otherwise, it will become a leaf of the
42709 ** first trunk page in the current free-list. This block tests if it
42710 ** is possible to add the page as a new free-list leaf.
42713 u32 nLeaf; /* Initial number of leaf cells on trunk page */
42728 /* In this case there is room on the trunk page to insert the page
42731 ** Note that the trunk page is not really full until it contains
42753 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
42759 ** the page being freed as a leaf page of the first trunk in the free-list.
42761 ** first trunk in the free-list is full. Either way, the page being freed
42762 ** will become the new first trunk page in the free-list.
42774 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
42815 /* 0 is not a legal page number and page 1 cannot be an
42816 ** overflow page. Therefore if ovflPgno<2 or past the end of the
42835 ** Create the byte sequence used to represent a cell on page pPage
42847 MemPage *pPage, /* The page that contains the cell */
42912 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
42924 ** overflow page is being allocated, add an entry to the pointer-map
42925 ** for that page now.
42927 ** If this is the first overflow page, then write a partial entry
43010 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
43046 ** If the cell content will fit on the page, then put it there. If it
43060 MemPage *pPage, /* Page into which we are copying */
43061 int i, /* New cell becomes the i-th cell of the page */
43073 u8 *data; /* The content of the whole page */
43085 ** malformed cell from a leaf page to an interior page, if the cell size
43133 /* The cell may contain a pointer to an overflow page. If so, write
43134 ** the entry for the overflow page into the pointer map.
43143 ** Add a list of cells to a page. The page should be initially empty.
43144 ** The cells are guaranteed to fit on the page.
43147 MemPage *pPage, /* The page to be assemblied */
43148 int nCell, /* The number of cells to add to this page */
43157 const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
43164 /* Check that the page has just been zeroed by zeroPage() */
43185 ** of the page that participate in the balancing operation. NB is the
43186 ** total number of pages that participate, including the target page and
43206 ** a new page to the right-hand side and put the one new entry in
43207 ** that page. This leaves the right side of the tree somewhat
43209 ** at the end soon afterwards so the nearly empty page will quickly
43212 ** pPage is the leaf page which is the right-most page in the tree.
43214 ** which is also the right-most entry on the page.
43218 ** byte page number followed by a variable length integer. In other
43224 MemPage *pNew; /* Newly allocated page */
43226 Pgno pgnoNew; /* Page number of pNew */
43234 /* Allocate a new page. This page will become the right-sibling of
43235 ** pPage. Make the parent page writable, so that the new divider cell
43253 ** with entries for the new page, and any pointer from the
43254 ** cell on the page to an overflow page. If either of these
43256 ** of the parent page are still manipulated by thh code below.
43257 ** That is Ok, at this point the parent page is guaranteed to
43259 ** rollback, undoing any changes made to the parent page.
43269 ** consists of a 4-byte page number (the page number of pPage) and
43291 /* Set the right-child pointer of pParent to point to the new page. */
43294 /* Release the reference to the new page. */
43346 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
43347 ** the pointer-map entries for each child page are updated so that the
43348 ** parent page stored in the pointer map is page pTo. If pFrom contained
43349 ** any cells with overflow page pointers, then the corresponding pointer
43350 ** map entries are also updated so that the parent page is page pTo.
43355 ** Before returning, page pTo is reinitialized using btreeInitPage().
43376 /* Copy the b-tree node content from page pFrom to page pTo. */
43381 /* Reinitialize page pTo so that the contents of the MemPage structure
43384 ** page pFrom.
43404 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
43406 ** page are used in the balancing, though both siblings might come from one
43407 ** side if the page is the first or last child of its parent. If the page
43408 ** has fewer than 2 siblings (something which can only happen if the page
43409 ** is a root page or a child of a root page) then all available siblings
43412 ** The number of siblings of the page might be increased or decreased by
43415 ** Note that when this routine is called, some of the cells on the page
43417 ** if the page is overfull. This routine ensures that all cells allocated
43418 ** to the page and its siblings fit into MemPage.aData[] before returning.
43420 ** In the course of balancing the page and its siblings, cells may be
43421 ** inserted into or removed from the parent page (pParent). Doing so
43422 ** may cause the parent page to become overfull or underfull. If this
43431 ** buffer big enough to hold one page. If while inserting cells into the parent
43432 ** page (pParent) the parent page becomes overfull, this buffer is
43434 ** a maximum of four divider cells into the parent page, and the maximum
43436 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
43443 MemPage *pParent, /* Parent page of siblings being balanced */
43444 int iParentIdx, /* Index of "the page" in pParent */
43445 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
43446 int isRoot /* True if pParent is a root-page */
43460 int subtotal; /* Subtotal of bytes in cells on one page */
43469 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
43470 int szNew[NB+2]; /* Combined size of cells place on i-th page */
43474 Pgno pgno; /* Temp var to store a page number in */
43481 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
43502 ** This loop also drops the divider cells from the parent page. This
43504 ** overflow cells in the parent page, since if any existed they will
43547 /* Drop the cell from the parent page. apDiv[i] still points to
43579 + k*nOld; /* Page copies (apCopy) */
43642 /* The right pointer of the child page pOld becomes the left
43659 ** size of all cells on the i-th page and cntNew[] which is the index
43660 ** in apCell[] of the cell that divides page i from page i+1.
43666 ** szNew[i]: Spaced used on the i-th sibling page.
43668 ** the right of the i-th sibling page.
43721 ** a virtual root page. A virtual root page is when the real root
43722 ** page is page 1 and we are the only child of that page.
43755 /* Set the pointer-map entry for the new sibling page. */
43823 /* Assemble the new sibling page. */
43833 /* If the sibling page assembled above was not the right-most sibling,
43834 ** insert a divider cell into the parent page.
43852 ** the sibling-page assembled above only.
43898 /* The root page of the b-tree now contains no cells. The only sibling
43899 ** page is the right-child of the parent. Copy the contents of the
43900 ** child page into the parent, decreasing the overall height of the
43908 ** The second assert below verifies that the child page is defragmented
43910 ** is important if the parent page happens to be page 1 of the database
43931 ** page in any overflow chains used by new divider cells. These
43955 j = 0; /* Current 'old' sibling page */
43956 k = 0; /* Current 'new' sibling page */
43961 ** sibling page j. If the siblings are not leaf pages of an
43984 ** sibling page k. If the siblings are not leaf pages of an
43994 ** page before the balancing, then the pointer map entries associated
44044 ** This function is called when the root page of a b-tree structure is
44047 ** A new child page is allocated and the contents of the current root
44048 ** page, including overflow cells, are copied into the child. The root
44049 ** page is then overwritten to make it an empty page with the right-child
44050 ** pointer pointing to the new page.
44053 ** that the new child-page now contains pointers to are updated. The
44055 ** page is also updated.
44058 ** page and SQLITE_OK is returned. In this case the caller is required
44064 MemPage *pChild = 0; /* Pointer to a new child page */
44065 Pgno pgnoChild = 0; /* Page number of the new child page */
44071 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
44072 ** page that will become the new right-child of pPage. Copy the contents
44073 ** of the node stored on pRoot into the new child page.
44107 ** The page that pCur currently points to has just been modified in
44131 /* The root page of the b-tree is overfull. In this case call the
44132 ** balance_deeper() function to create a new child for the root-page
44133 ** and copy the current contents of the root-page to it. The
44134 ** next iteration of the do-loop will balance the child page.
44184 ** will balance the parent page to correct this.
44186 ** If the parent page becomes overfull, the overflow cell or cells
44191 ** different page). Once this subsequent call to balance_nonroot()
44194 ** copied either into the body of a database page or into the new
44216 /* The next iteration of the do-loop balances the parent page. */
44315 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
44357 ** back to the root page as balance() used to invalidate the contents
44397 MemPage *pPage; /* Page to delete cell from */
44426 /* If the page containing the entry to delete is not a leaf page, move
44431 ** sub-tree headed by the child page of the cell being deleted. This makes
44440 ** making any modifications. Make the page containing the entry to be
44442 ** entry and finally remove the cell itself from within the page.
44452 /* If the cell deleted was not located on a leaf page, then the cursor
44454 ** by the child-page of the cell that was just deleted from an internal
44476 /* Balance the tree. If the entry deleted was located on a leaf page,
44477 ** then the cursor still points to that page. In this case the first
44481 ** Otherwise, if the entry deleted was on an internal node page, then
44482 ** pCur is pointing to the leaf page from which a cell was removed to
44506 ** Create a new BTree table. Write into *piTable the page
44507 ** number for the root page of the new table.
44533 Pgno pgnoMove; /* Move a page here to make room for the root-page */
44534 MemPage *pPageMove; /* The page to move to. */
44537 ** to make room for the new tables root page. In case this page turns
44538 ** out to be an overflow page, delete all overflow page-map caches
44544 ** root page of the new table should go. meta[3] is the largest root-page
44545 ** created so far, so the new root-page is (meta[3]+1).
44550 /* The new root-page may not be allocated on a pointer-map page, or the
44551 ** PENDING_BYTE page.
44559 /* Allocate a page. The page that currently resides at pgnoRoot will
44560 ** be moved to the allocated page (unless the allocated page happens
44569 /* pgnoRoot is the page that will be used for the root-page of
44572 ** by extending the file), the current page at position pgnoMove
44580 /* Move the page currently at pgnoRoot to pgnoMove. */
44598 /* Obtain the page at pgnoRoot */
44615 /* Update the pointer-map and meta-data with the new root-page number. */
44647 ** Erase the given database page and all its children. Return
44648 ** the page to the freelist.
44652 Pgno pgno, /* Page number to clear */
44653 int freePageFlag, /* Deallocate page if true */
44697 ** the page number of the root of the table. After this routine returns,
44698 ** the root page is empty, but still exists.
44730 ** page 1) is never added to the freelist.
44735 ** If AUTOVACUUM is enabled and the page at iTable is not the last
44736 ** root page in the database file, then the last root page
44738 ** iTable and that last slot formerly occupied by the last root page
44742 ** page number that used to be the last root page in the file before
44743 ** the move. If no page gets moved, *piMoved is set to 0.
44744 ** The last root page is recorded in meta[3] and the value of
44757 ** need to move another root-page to fill a gap left by the deleted
44758 ** root page. If an open cursor was using this page a problem would
44788 /* If the table being dropped is the table with the largest root-page
44789 ** number in the database, put the root page on the free list.
44797 /* The table being dropped does not have the largest root-page
44798 ** number in the database. So move the page that does into the
44799 ** gap left by the deleted root-page.
44822 /* Set the new 'max-root-page' value in the database header. This
44824 ** be a root-page number, less one again if that is the
44841 /* If sqlite3BtreeDropTable was called on page 1.
44935 ** page in the B-Tree structure (not including overflow pages).
44939 MemPage *pPage; /* Current page of the b-tree */
44941 /* If this is a leaf page or the tree is not an int-key tree, then
44942 ** this page contains countable entries. Increment the entry counter
44952 ** the next page in the tree that has not yet been visited. The
44954 ** of the page, or to the number of cells in the page if the next page
45029 ** Add 1 to the reference count for page iPage. If this is the second
45030 ** reference to the page, add an error message to pCheck->zErrMsg.
45031 ** Return 1 if there are 2 ore more references to the page and 0 if
45032 ** if this is the first reference to the page.
45034 ** Also check that the page number is in bounds.
45039 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
45043 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
45051 ** Check that the entry in the pointer-map for page iChild maps to
45052 ** page iParent, pointer type ptrType. If not, append an error message
45057 Pgno iChild, /* Child page number */
45059 Pgno iParent, /* Expected pointer map parent page number */
45082 ** Check the integrity of the freelist or of an overflow page list.
45087 int isFreeList, /* True for a freelist. False for overflow page list */
45088 int iPage, /* Page number for first page in the list */
45106 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
45119 "freelist leaf count too big on page %d", iPage);
45137 ** page in this overflow list, check that the pointer-map entry for
45138 ** the following page matches iPage.
45154 ** Do various sanity checks on a single page of a tree. Return
45161 ** but combine to completely cover the page.
45168 ** 8. Make sure this page is at least 33% full or else it is
45173 int iPage, /* Page number of the page to check */
45190 sqlite3_snprintf(sizeof(zContext), zContext, "Page
45192 /* Check that the page exists
45200 "unable to get the page. error code=%d", rc);
45226 "On tree page %d cell %d: ", iPage, i);
45255 /* Check sanity of left child page.
45266 checkAppendMsg(pCheck, zContext, "Child page depth differs");
45275 "On page %d at right child: ", iPage);
45288 /* if we are a left child page */
45290 /* if we are the left most child page */
45310 /* else if we're a right child page */
45320 /* Check for complete coverage of the page
45343 "Corruption detected in cell %d on page %d",i,iPage);
45365 "Multiple uses for byte %d of page %d", i, iPage);
45371 "Fragmentation of %d bytes reported as %d on page %d",
45384 ** an array of pages numbers were each page number is the root page of
45452 /* Make sure every page in the file is referenced
45457 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
45465 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
45469 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
45480 "Outstanding page count goes from %d to %d during this analysis",
45596 ** Obtain a lock on the table whose root page is iTab. The
45668 ** This function sets a flag only. The actual page location cache
45714 Pgno iNext; /* Page number of the next source page to copy */
45743 ** the page cache associated with the source database. The mutex
45880 ** page iSrcPg from the source database. Copy this data into the
45899 ** page sizes of the source and destination differ.
45905 /* This loop runs once for each destination page spanned by the source
45906 ** page. For each iteration, variable iOff is set to the byte offset
45907 ** of the destination page.
45920 /* Copy the data from the source page into the destination page.
45923 ** of the page 'extra' space to invalidate the Btree layers
45924 ** cached parse of the page). MemPage.isInit is marked
46019 const Pgno iSrcPg = p->iNext; /* Source page number */
46021 DbPage *pSrcPg; /* Source page object */
46057 ** database. The complication here is that the destination page
46058 ** size may be different to the source page size.
46060 ** If the source page size is smaller than the destination page size,
46064 ** destination file that lie beyond the nDestTruncate page mark are
46080 /* If the source page-size is smaller than the destination page-size,
46086 ** pending-byte page in the source database may need to be
46217 ** This function is called after the contents of page iPage of the
46218 ** source database have been modified. If page iPage has already been
46233 /* The backup process p has already copied page iPage. But now it
47192 page */
54647 ** record header if the record header does not fit on a single page
55364 ** Open a read-only cursor for the database table whose root page is
55372 ** If P5!=0 then use the content of register P2 as the root page, not
55395 ** page is P2. Or if P5!=0 use the content of register P2 to find the
55396 ** root page.
55478 ** rooted at page 1 of a zero-byte database. */
55486 ** SQLite used to check if the root-page flags were sane at this point
55536 ** automatically created table with root-page 1 (an INTKEY table).
56885 ** Delete an entire database table or index whose root page in the database
56892 ** If AUTOVACUUM is enabled then it is possible that another root page
56893 ** might be moved into the newly deleted root page in order to keep all
56895 ** value of the root page that moved - its value before the move occurred -
56896 ** is stored in register P2. If no page
56943 ** Delete all contents of the database table or index whose root page
56982 ** P1>1. Write the root page number of the new table into
56995 ** P1>1. Write the root page number of the new table into
57161 ** The root page numbers of all tables in the database are integer
57731 ** P2 contains the root-page of the table to lock.
58136 ** page count has already been successfully read and cached. So the
66515 int iTab; /* The root page of the table to be locked */
66523 ** The table to be locked has root page iTab and is found in database iDb.
66533 int iTab, /* Root page number of the table to be locked */
67353 ** The root page number of the new table is left in reg pParse->regRoot.
67354 ** The rowid and root page number values are needed by the code that
67957 page number
67958 ** for the table from the db->init.newTnum field. (The page number
67999 ** statement to populate the new table. The root-page number for the
68310 ** used by SQLite when the btree layer moves a table root page. The
68311 ** root-page of a table or index in database iDb has changed from iFrom
68348 ** Write code to erase the table with root-page iTable from database iDb.
68350 ** if a root-page of another table is moved by the btree-layer whilst
68360 ** is non-zero, then it is the root page number of a table moved to
68378 ** in case a root-page belonging to another table is moved by the btree layer
68393 ** largest root-page number. This guarantees that none of the root-pages
68401 ** and root page 5 happened to be the largest root-page number in the
68402 ** database, then root page 5 would be moved to page 4 by the
68404 ** a free-list page.
68752 ** root page number of the index. If memRootPage is negative, then
68754 ** the root page number of the index is taken from pIndex->tnum.
68761 int tnum; /* Root page of index */
77263 ** page cache size. The value returned is the maximum number of
77264 ** pages in the page cache. The second form sets both the current
77265 ** page cache size value and the persistent page cache size value
77268 ** The default cache size is stored in meta-value 2 of page 1 of the
77316 ** database page size in bytes. The second form sets the
77317 ** database page size value. The value can only be set if
77327 /* Malloc may fail when setting the page-size, as there is an internal
77593 ** page cache size. The local setting can be different from
77596 ** pages in the page cache. The second form sets the local
77597 ** page cache size value. It does not change the persistent
78419 ** argv[1] = root page number for table or index. 0 for trigger or view.
78476 ** to do here is record the root page number for that index.
78604 ** meta[2] Size of the page cache.
83285 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
83294 ** In this case set iRoot to the root page number of the index b-tree
85744 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
85755 ** we already have page 1 loaded into cache and marked dirty. */
95768 /* Designate a buffer for page cache memory space */
95776 /* Specify an alternative page cache implementation */
96678 int nCache, /* How many pages in the page cache */
99585 int isFirstTerm = 1; /* True when processing first term on page */
105335 int nReq; /* Number of bytes required on leaf page */
107708 ** child page.
108061 ** an internal node, then the 64-bit integer is a child page number.
108241 ** Cursor pCursor currently points to a cell in a non-leaf page.
108824 RtreeNode **ppLeaf /* OUT: Selected leaf page */
110123 ** This routine queries database handle db for the page-size used by
110124 ** database zDb. If successful, the page-size in bytes is written to
110211 ** the database page-size. This ensures that each node is stored on
110212 ** a single database page.
110214 ** If the databasd page-size is so large that more than RTREE_MAXCELLS