Class SqlJetBtreeShared


  • public class SqlJetBtreeShared
    extends java.lang.Object
    An instance of this object represents a single database file. A single database file can be in use as the same time by two or more database connections. When two or more connections are sharing the same database file, each connection has it own private Btree object for the file and each of those Btrees points to this one BtShared object. BtShared.nRef is the number of connections currently sharing this database file. Fields in this structure are accessed under the BtShared.mutex mutex, except for nRef and pNext which are accessed under the global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field may not be modified once it is initially set as long as nRef>0. The pSchema field may be set once under BtShared.mutex and thereafter is unchanged as long as nRef>0.
    Author:
    TMate Software Ltd., Sergey Scherbina (sergey.scherbina@gmail.com)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      SqlJetMemPage allocatePage​(int[] pPgno, int nearby, boolean exact)
      Allocate a new page from the database file.
      void allocateTempSpace()
      Make sure pBt->pTmpSpace points to an allocation of MX_CELL_SIZE(pBt) bytes.
      void autoVacuumCommit()
      This routine is called prior to sqlite3PagerCommit when a transaction is commited for an auto-vacuum database.
      void clearDatabasePage​(int pgno, boolean freePageFlag, int[] pnChange)
      Erase the given database page and all its children.
      int countWriteCursors()
      Return the number of write-cursors open on this handle.
      void getOverflowPage​(int ovfl, SqlJetMemPage[] ppPage, int[] pPgnoNext)
      Given the page number of an overflow page in the database (parameter ovfl), this function finds the page number of the next page in the linked list of overflow pages.
      SqlJetMemPage getPage​(int pgno, boolean noContent)
      Get a page from the pager.
      void incrVacuumStep​(int nFin, int iLastPg)
      Perform a single step of an incremental-vacuum.
      void invalidateAllOverflowCache()
      Invalidate the overflow page-list cache for all cursors opened on the shared btree structure pBt.
      int MX_CELL()
      The maximum number of cells on a single page of the database.
      int MX_CELL_SIZE()
      The following value is the maximum cell size assuming a maximum page size give above.
      int PENDING_BYTE_PAGE()
      The database page the PENDING_BYTE occupies.
      int PTRMAP_PAGENO​(int pgno)
      These macros define the location of the pointer-map entry for a database page.
      void ptrmapGet​(int key, short[] pEType, int[] pPgno)
      Read an entry from the pointer map.
      void ptrmapPut​(int key, short eType, int parent)
      Write an entry into the pointer map.
      void ptrmapPutOvflPtr​(SqlJetMemPage pPage, ISqlJetMemoryPointer pCell)
      If the cell pCell, part of page pPage contains a pointer to an overflow page, insert an entry into the pointer-map for the overflow page.
      void relocatePage​(SqlJetMemPage pDbPage, short s, int iPtrPage, int iFreePage, boolean isCommit)
      Move the open database page pDbPage to location iFreePage in the database.
      boolean saveAllCursors​(int iRoot, SqlJetBtreeCursor pExcept)
      Save the positions of all cursors except pExcept open on the table with root-page iRoot.
      void unlockBtreeIfUnused()
      If there are no outstanding cursors and we are not in the middle of a transaction but there is a read lock on the database, then this routine unrefs the first page of the database file which has the effect of releasing the read lock.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SqlJetBtreeShared

        public SqlJetBtreeShared()
    • Method Detail

      • PENDING_BYTE_PAGE

        public int PENDING_BYTE_PAGE()
        The database page the PENDING_BYTE occupies. This page is never used. TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They should possibly be consolidated (presumably in pager.h). If disk I/O is omitted (meaning that the database is stored purely in memory) then there is no pending byte.
      • MX_CELL_SIZE

        public int MX_CELL_SIZE()
        The following value is the maximum cell size assuming a maximum page size give above.
      • MX_CELL

        public int MX_CELL()
        The maximum number of cells on a single page of the database. This assumes a minimum cell size of 6 bytes (4 bytes for the cell itself plus 2 bytes for the index to the cell in the page header). Such small cells will be rare, but they are possible.
      • PTRMAP_PAGENO

        public int PTRMAP_PAGENO​(int pgno)
        These macros define the location of the pointer-map entry for a database page. The first argument to each is the number of usable bytes on each page of the database (often 1024). The second is the page number to look up in the pointer map. PTRMAP_PAGENO returns the database page number of the pointer-map page that stores the required pointer. PTRMAP_PTROFFSET returns the offset of the requested map entry. If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements this test.
      • invalidateAllOverflowCache

        public void invalidateAllOverflowCache()
        Invalidate the overflow page-list cache for all cursors opened on the shared btree structure pBt.
      • ptrmapPut

        public void ptrmapPut​(int key,
                              short eType,
                              int parent)
                       throws SqlJetException
        Write an entry into the pointer map. This routine updates the pointer map entry for page number 'key' so that it maps to type 'eType' and parent page number 'pgno'. An error code is returned if something goes wrong, otherwise SQLITE_OK.
        Throws:
        SqlJetException
      • ptrmapGet

        public void ptrmapGet​(int key,
                              short[] pEType,
                              int[] pPgno)
                       throws SqlJetException
        Read an entry from the pointer map. This routine retrieves the pointer map entry for page 'key', writing the type and parent page number to *pEType and *pPgno respectively. An error code is returned if something goes wrong, otherwise SQLITE_OK.
        Throws:
        SqlJetException
      • getPage

        public SqlJetMemPage getPage​(int pgno,
                                     boolean noContent)
                              throws SqlJetException
        Get a page from the pager. Initialize the MemPage.pBt and MemPage.aData elements if needed. If the noContent flag is set, it means that we do not care about the content of the page at this time. So do not go to the disk to fetch the content. Just fill in the content with zeros for now. If in the future we call sqlite3PagerWrite() on this page, that means we have started to be concerned about content and the disk read should occur at that point.
        Parameters:
        pgno - Number of the page to fetch
        noContent - Do not load page content if true
        Returns:
        Throws:
        SqlJetException
      • allocatePage

        public SqlJetMemPage allocatePage​(int[] pPgno,
                                          int nearby,
                                          boolean exact)
                                   throws SqlJetException
        Allocate a new page from the database file. The new page is marked as dirty. (In other words, sqlite3PagerWrite() has already been called on the new page.) The new page has also been referenced and the calling routine is responsible for calling sqlite3PagerUnref() on the new page when it is done. SQLITE_OK is returned on success. Any other return value indicates an error. *ppPage and *pPgno are undefined in the event of an error. Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. If the "nearby" parameter is not 0, then a (feeble) effort is made to locate a page close to the page number "nearby". This can be used in an attempt to keep related pages close to each other in the database file, which in turn can make database access faster. If the "exact" parameter is not 0, and the page-number nearby exists anywhere on the free-list, then it is guarenteed to be returned. This is only used by auto-vacuum databases when allocating a new table.
        Throws:
        SqlJetException
      • relocatePage

        public void relocatePage​(SqlJetMemPage pDbPage,
                                 short s,
                                 int iPtrPage,
                                 int iFreePage,
                                 boolean isCommit)
                          throws SqlJetException
        Move the open database page pDbPage to location iFreePage in the database. The pDbPage reference remains valid.
        Parameters:
        pDbPage - Open page to move
        s - Pointer map 'type' entry for pDbPage
        iPtrPage - Pointer map 'page-no' entry for pDbPage
        iFreePage - The location to move pDbPage to
        isCommit -
        Throws:
        SqlJetException
      • incrVacuumStep

        public void incrVacuumStep​(int nFin,
                                   int iLastPg)
                            throws SqlJetException
        Perform a single step of an incremental-vacuum. If successful, return SQLITE_OK. If there is no work to do (and therefore no point in calling this function again), return SQLITE_DONE. More specificly, this function attempts to re-organize the database so that the last page of the file currently in use is no longer in use. If the nFin parameter is non-zero, the implementation assumes that the caller will keep calling incrVacuumStep() until it returns SQLITE_DONE or an error, and that nFin is the number of pages the database file will contain after this process is complete.
        Throws:
        SqlJetException
      • autoVacuumCommit

        public void autoVacuumCommit()
                              throws SqlJetException
        This routine is called prior to sqlite3PagerCommit when a transaction is commited for an auto-vacuum database. If SQLITE_OK is returned, then *pnTrunc is set to the number of pages the database file should be truncated to during the commit process. i.e. the database has been reorganized so that only the first *pnTrunc pages are in use.
        Throws:
        SqlJetException
      • unlockBtreeIfUnused

        public void unlockBtreeIfUnused()
                                 throws SqlJetException
        If there are no outstanding cursors and we are not in the middle of a transaction but there is a read lock on the database, then this routine unrefs the first page of the database file which has the effect of releasing the read lock. If there are any outstanding cursors, this routine is a no-op. If there is a transaction in progress, this routine is a no-op.
        Throws:
        SqlJetException
      • saveAllCursors

        public boolean saveAllCursors​(int iRoot,
                                      SqlJetBtreeCursor pExcept)
                               throws SqlJetException
        Save the positions of all cursors except pExcept open on the table with root-page iRoot. Usually, this is called just before cursor pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
        Parameters:
        i -
        j -
        Throws:
        SqlJetException
      • countWriteCursors

        public int countWriteCursors()
        Return the number of write-cursors open on this handle. This is for use in assert() expressions, so it is only compiled if NDEBUG is not defined. For the purposes of this routine, a write-cursor is any cursor that is capable of writing to the databse. That means the cursor was originally opened for writing and the cursor has not be disabled by having its state changed to CURSOR_FAULT.
        Returns:
      • clearDatabasePage

        public void clearDatabasePage​(int pgno,
                                      boolean freePageFlag,
                                      int[] pnChange)
                               throws SqlJetException
        Erase the given database page and all its children. Return the page to the freelist.
        Parameters:
        pgno -
        freePageFlag - Page number to clear
        pnChange - Deallocate page if true
        Throws:
        SqlJetException
      • getOverflowPage

        public void getOverflowPage​(int ovfl,
                                    SqlJetMemPage[] ppPage,
                                    int[] pPgnoNext)
                             throws SqlJetException
        Given the page number of an overflow page in the database (parameter ovfl), this function finds the page number of the next page in the linked list of overflow pages. If possible, it uses the auto-vacuum pointer-map data instead of reading the content of page ovfl to do so. If an error occurs an SQLite error code is returned. Otherwise: Unless pPgnoNext is NULL, the page number of the next overflow page in the linked list is written to *pPgnoNext. If page ovfl is the last page in its linked list, *pPgnoNext is set to zero. If ppPage is not NULL, *ppPage is set to the MemPage* handle for page ovfl. The underlying pager page may have been requested with the noContent flag set, so the page data accessable via this handle may not be trusted.
        Parameters:
        ovfl - Overflow page
        ppPage - OUT: MemPage handle
        pPgnoNext - OUT: Next overflow page number
        Throws:
        SqlJetException
      • allocateTempSpace

        public void allocateTempSpace()
        Make sure pBt->pTmpSpace points to an allocation of MX_CELL_SIZE(pBt) bytes.