## org.h2.store
Class PageStore

    java.lang.Object
      org.h2.store.PageStore

All Implemented Interfaces:
  CacheWriter

    public class PageStore

extends java.lang.Object
implements CacheWriter

This class represents a file that is organized as a number of pages. Page 0 contains a static file header, and pages 1 and 2 both contain the variable file header (page 2 is a copy of page 1 and is only read if the checksum of page 1 is invalid). The format of page 0 is:

- 0-47: file header (3 time "-- H2 0.5/B -- \n")
- 48-51: page size in bytes (512 - 32768, must be a power of 2)
- 52: write version (if not 0 the file is opened in read-only mode)
- 53: read version (if not 0 opening the file fails)

The format of page 1 and 2 is:

- CRC32 of the remaining data: int (0-3)
- write counter (incremented on each write): long (4-11)
- log trunk key: int (12-15)
- log trunk page (0 for none): int (16-19)
- log data page (0 for none): int (20-23)

Page 3 contains the first free list page. Page 4 contains the meta table root page.

Field Summary
`static int` `PAGE_SIZE_DEFAULT` The default page size.
`static int` `PAGE_SIZE_MAX` The biggest possible page size.
`static int` `PAGE_SIZE_MIN` The smallest possible page size.

Constructor Summary
`PageStore(Database database, java.lang.String fileName, java.lang.String accessMode, int cacheSizeDefault)` Create a new page store object.

Method Summary
`void` `addIndex(PageIndex index)` Add an index to the in-memory index map.
`void` `addMeta(PageIndex index, Session session)` Add the meta data of an index.
`int` `allocatePage()` Allocate a page.
`void` `checkpoint()` Flush all pending changes to disk, and re-open the log file.
`static boolean` `checksumTest(byte[] d, int pageId, int pageSize)` Check if the stored checksum is correct
`void` `close()` Close the file without further writing.
`void` `commit(Session session)` Mark a committed transaction.
`void` `compact(boolean fully)` Shrink the file so there are no empty pages at the end.
`int` `copyDirect(int pageId, java.io.OutputStream out)` Copy the next page to the output stream.
`Data` `createData()` Create a data object.
`void` `flushLog()` Flush the log file, so that entries can be removed from the cache.
`void` `free(int pageId, boolean undo)` Add a page to the free list.
`Cache` `getCache()`
`ObjectArray<InDoubtTransaction>` `getInDoubtTransactions()` Get the list of in-doubt transaction.
`Page` `getPage(int pageId)` Read a page from the store.
`int` `getPageCount()` Get the number of pages (including free pages).
`int` `getPageSize()` Get the page size.
`long` `getReadCount()` Get the file read count since the database was opened.
`Record` `getRecord(int pos)` Get the record if it is stored in the file, or null if not.
`int` `getRootPageId(int indexId)` Get the root page of an index.
`int` `getSystemTableHeadPos()` Get the position of the system table head.
`Trace` `getTrace()` Get the trace writer.
`long` `getWriteCount()` Get the file write count since the database was opened.
`long` `getWriteCountTotal()` Get the file write count since the database was created.
`boolean` `isRecoveryRunning()` Check whether the recovery process is currently running.
`void` `logAddOrRemoveRow(Session session, int tableId, Row row, boolean add)` A record is added to a table, or removed from a table.
`void` `logTruncate(Session session, int tableId)` A table is truncated.
`void` `logUndo(Record record, Data old)` Write an undo log entry if required.
`static SearchRow[]` `newSearchRows(int entryCount)` Create an array of SearchRow with the given size.
`void` `open()` Open the file and read the header.
`void` `prepareCommit(Session session, java.lang.String transaction)` Prepare a transaction.
`Data` `readPage(int pos)` Read a page.
`void` `removeMeta(Index index, Session session)` Remove the meta data of an index.
`void` `removeRecord(int pageId)` Remove a page from the cache.
`void` `setInDoubtTransactionState(int sessionId, int pageId, boolean commit)` Commit or rollback a prepared transaction after opening a database with in-doubt transactions.
`void` `setMaxLogSize(long maxSize)` Set the maximum log file size in megabytes.
`void` `update(Page page)` Update a page.
`void` `writeBack(CacheObject obj)` Persist a record.
`void` `writePage(int pageId, Data data)` Write a page.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### PAGE_SIZE_MIN

    public static final int PAGE_SIZE_MIN

  The smallest possible page size.

  See Also:
    Constant Field Values

### PAGE_SIZE_MAX

    public static final int PAGE_SIZE_MAX

  The biggest possible page size.

  See Also:
    Constant Field Values

### PAGE_SIZE_DEFAULT

    public static final int PAGE_SIZE_DEFAULT

  The default page size.

  See Also:
    Constant Field Values

Constructor Detail

### PageStore

    public PageStore(Database database,
                     java.lang.String fileName,
                     java.lang.String accessMode,
                     int cacheSizeDefault)
              throws java.sql.SQLException

  Create a new page store object.

  Parameters:
    `database` - the database
    `fileName` - the file name
    `accessMode` - the access mode
    `cacheSizeDefault` - the default cache size
  Throws:
    `java.sql.SQLException`

Method Detail

### copyDirect

    public int copyDirect(int pageId,
                          java.io.OutputStream out)
                   throws java.sql.SQLException

  Copy the next page to the output stream.

  Parameters:
    `pageId` - the page to copy
    `out` - the output stream
  Returns:
    the new position, or -1 if there is no more data to copy
  Throws:
    `java.sql.SQLException`

### open

    public void open()
              throws java.sql.SQLException

  Open the file and read the header.

  Throws:
    `java.sql.SQLException`

### checkpoint

    public void checkpoint()
                    throws java.sql.SQLException

  Flush all pending changes to disk, and re-open the log file.

  Throws:
    `java.sql.SQLException`

### compact

    public void compact(boolean fully)
                 throws java.sql.SQLException

  Shrink the file so there are no empty pages at the end.

  Parameters:
    `fully` - if the database should be fully compressed
  Throws:
    `java.sql.SQLException`

### getPage

    public Page getPage(int pageId)
                 throws java.sql.SQLException

  Read a page from the store.

  Parameters:
    `pageId` - the page id
  Returns:
    the page
  Throws:
    `java.sql.SQLException`

### close

    public void close()
               throws java.sql.SQLException

  Close the file without further writing.

  Throws:
    `java.sql.SQLException`

### flushLog

    public void flushLog()
                  throws java.sql.SQLException

  Description copied from interface: `CacheWriter`
  Flush the log file, so that entries can be removed from the cache. This is only required if the cache is full and contains data that is not yet written to the log file. It is required to write the log entries to the log file first, because the log file is 'write ahead'.

  Specified by:
    `flushLog` in interface `CacheWriter`
  Throws:
    `java.sql.SQLException`

### getTrace

    public Trace getTrace()

  Description copied from interface: `CacheWriter`
  Get the trace writer.

  Specified by:
    `getTrace` in interface `CacheWriter`
  Returns:
    the trace writer

### writeBack

    public void writeBack(CacheObject obj)
                   throws java.sql.SQLException

  Description copied from interface: `CacheWriter`
  Persist a record.

  Specified by:
    `writeBack` in interface `CacheWriter`
  Parameters:
    `obj` - the cache entry
  Throws:
    `java.sql.SQLException`

### logUndo

    public void logUndo(Record record,
                        Data old)
                 throws java.sql.SQLException

  Write an undo log entry if required.

  Parameters:
    `record` - the page
    `old` - the old data (if known) or null
  Throws:
    `java.sql.SQLException`

### update

    public void update(Page page)
                throws java.sql.SQLException

  Update a page.

  Parameters:
    `page` - the page
  Throws:
    `java.sql.SQLException`

### allocatePage

    public int allocatePage()
                     throws java.sql.SQLException

  Allocate a page.

  Returns:
    the page id
  Throws:
    `java.sql.SQLException`

### free

    public void free(int pageId,
                     boolean undo)
              throws java.sql.SQLException

  Add a page to the free list.

  Parameters:
    `pageId` - the page id
    `undo` - if the undo record must have been written
  Throws:
    `java.sql.SQLException`

### createData

    public Data createData()

  Create a data object.

  Returns:
    the data page.

### getRecord

    public Record getRecord(int pos)

  Get the record if it is stored in the file, or null if not.

  Parameters:
    `pos` - the page id
  Returns:
    the record or null

### readPage

    public Data readPage(int pos)
                  throws java.sql.SQLException

  Read a page.

  Parameters:
    `pos` - the page id
  Returns:
    the page
  Throws:
    `java.sql.SQLException`

### getPageSize

    public int getPageSize()

  Get the page size.

  Returns:
    the page size

### getPageCount

    public int getPageCount()

  Get the number of pages (including free pages).

  Returns:
    the page count

### writePage

    public void writePage(int pageId,
                          Data data)
                   throws java.sql.SQLException

  Write a page.

  Parameters:
    `pageId` - the page id
    `data` - the data
  Throws:
    `java.sql.SQLException`

### removeRecord

    public void removeRecord(int pageId)

  Remove a page from the cache.

  Parameters:
    `pageId` - the page id

### logAddOrRemoveRow

    public void logAddOrRemoveRow(Session session,
                                  int tableId,
                                  Row row,
                                  boolean add)
                           throws java.sql.SQLException

  A record is added to a table, or removed from a table.

  Parameters:
    `session` - the session
    `tableId` - the table id
    `row` - the row to add
    `add` - true if the row is added, false if it is removed
  Throws:
    `java.sql.SQLException`

### commit

    public void commit(Session session)
                throws java.sql.SQLException

  Mark a committed transaction.

  Parameters:
    `session` - the session
  Throws:
    `java.sql.SQLException`

### prepareCommit

    public void prepareCommit(Session session,
                              java.lang.String transaction)
                       throws java.sql.SQLException

  Prepare a transaction.

  Parameters:
    `session` - the session
    `transaction` - the name of the transaction
  Throws:
    `java.sql.SQLException`

### getSystemTableHeadPos

    public int getSystemTableHeadPos()

  Get the position of the system table head.

  Returns:
    the system table head

### addIndex

    public void addIndex(PageIndex index)

  Add an index to the in-memory index map.

  Parameters:
    `index` - the index

### addMeta

    public void addMeta(PageIndex index,
                        Session session)
                 throws java.sql.SQLException

  Add the meta data of an index.

  Parameters:
    `index` - the index to add
    `session` - the session
  Throws:
    `java.sql.SQLException`

### removeMeta

    public void removeMeta(Index index,
                           Session session)
                    throws java.sql.SQLException

  Remove the meta data of an index.

  Parameters:
    `index` - the index to remove
    `session` - the session
  Throws:
    `java.sql.SQLException`

### setMaxLogSize

    public void setMaxLogSize(long maxSize)

  Set the maximum log file size in megabytes.

  Parameters:
    `maxSize` - the new maximum log file size

### setInDoubtTransactionState

    public void setInDoubtTransactionState(int sessionId,
                                           int pageId,
                                           boolean commit)
                                    throws java.sql.SQLException

  Commit or rollback a prepared transaction after opening a database with in-doubt transactions.

  Parameters:
    `sessionId` - the session id
    `pageId` - the page where the transaction was prepared
    `commit` - if the transaction should be committed
  Throws:
    `java.sql.SQLException`

### getInDoubtTransactions

    public ObjectArray<InDoubtTransaction> getInDoubtTransactions()

  Get the list of in-doubt transaction.

  Returns:
    the list

### isRecoveryRunning

    public boolean isRecoveryRunning()

  Check whether the recovery process is currently running.

  Returns:
    true if it is

### newSearchRows

    public static SearchRow[] newSearchRows(int entryCount)

  Create an array of SearchRow with the given size.

  Parameters:
    `entryCount` - the number of elements
  Returns:
    the array

### getWriteCountTotal

    public long getWriteCountTotal()

  Get the file write count since the database was created.

  Returns:
    the write count

### getWriteCount

    public long getWriteCount()

  Get the file write count since the database was opened.

  Returns:
    the write count

### getReadCount

    public long getReadCount()

  Get the file read count since the database was opened.

  Returns:
    the read count

### logTruncate

    public void logTruncate(Session session,
                            int tableId)
                     throws java.sql.SQLException

  A table is truncated.

  Parameters:
    `session` - the session
    `tableId` - the table id
  Throws:
    `java.sql.SQLException`

### getRootPageId

    public int getRootPageId(int indexId)

  Get the root page of an index.

  Parameters:
    `indexId` - the index id
  Returns:
    the root page

### getCache

    public Cache getCache()

### checksumTest

    public static boolean checksumTest(byte[] d,
                                       int pageId,
                                       int pageSize)

  Check if the stored checksum is correct

  Parameters:
    `d` - the data
    `pageId` - the page id
    `pageSize` - the page size
  Returns:
    true if it is correct