org.h2.store
Class PageStore

java.lang.Object
  extended by 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:

The format of page 1 and 2 is: Page 3 contains the first free list page. Page 4 contains the meta table root page.


Field Summary
static int LOG_MODE_OFF
          This log mode means the transaction log is not used.
static int LOG_MODE_SYNC
          This log mode means the transaction log is used and FileDescriptor.sync() is called for each checkpoint.
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 switch the new transaction log.
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(int compactMode)
          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 transaction log, so that entries can be removed from the cache.
 void free(int pageId)
          Add a page to the free list.
 Cache getCache()
           
 int getChangeCount()
          Get the current change count.
 java.util.ArrayList<InDoubtTransaction> getInDoubtTransactions()
          Get the list of in-doubt transaction.
 int getLogMode()
           
 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.
 int getRootPageId(int indexId)
          Get the root page of an index.
 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.
 void incrementChangeCount()
          Increment the change count.
 boolean isNew()
          Check whether this is a new database.
 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(Page page, Data old)
          Write an undo log entry if required.
 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 setLockFile(boolean lockFile)
           
 void setLogMode(int logMode)
           
 void setMaxLogSize(long maxSize)
          Set the maximum transaction log size in megabytes.
 void setPageSize(int size)
          Set the page size.
 java.util.HashMap<java.lang.String,java.lang.Integer> statisticsEnd()
          Stop collecting statistics.
 void statisticsStart()
          Start collecting statistics.
 void sync()
          Flush the transaction log and sync the file.
 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

LOG_MODE_OFF

public static final int LOG_MODE_OFF
This log mode means the transaction log is not used.

See Also:
Constant Field Values

LOG_MODE_SYNC

public static final int LOG_MODE_SYNC
This log mode means the transaction log is used and FileDescriptor.sync() is called for each checkpoint. This is the default level.

See Also:
Constant Field Values
Constructor Detail

PageStore

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

Parameters:
database - the database
fileName - the file name
accessMode - the access mode
cacheSizeDefault - the default cache size
Method Detail

statisticsStart

public void statisticsStart()
Start collecting statistics.


statisticsEnd

public java.util.HashMap<java.lang.String,java.lang.Integer> statisticsEnd()
Stop collecting statistics.

Returns:
the statistics

copyDirect

public int copyDirect(int pageId,
                      java.io.OutputStream out)
               throws java.io.IOException
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.io.IOException

open

public void open()
Open the file and read the header.


checkpoint

public void checkpoint()
Flush all pending changes to disk, and switch the new transaction log.


compact

public void compact(int compactMode)
Shrink the file so there are no empty pages at the end.

Parameters:
compactMode - 0 if no compacting should happen, otherwise TransactionCommand.SHUTDOWN_COMPACT or TransactionCommand.SHUTDOWN_DEFRAG

getPage

public Page getPage(int pageId)
Read a page from the store.

Parameters:
pageId - the page id
Returns:
the page

setPageSize

public void setPageSize(int size)
Set the page size. The size must be a power of two. This method must be called before opening.

Parameters:
size - the page size

close

public void close()
Close the file without further writing.


flushLog

public void flushLog()
Description copied from interface: CacheWriter
Flush the transaction log, 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. It is required to write the log entries to the log first, because the log is 'write ahead'.

Specified by:
flushLog in interface CacheWriter

sync

public void sync()
Flush the transaction log and sync the file.


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)
Description copied from interface: CacheWriter
Persist a record.

Specified by:
writeBack in interface CacheWriter
Parameters:
obj - the cache entry

logUndo

public void logUndo(Page page,
                    Data old)
Write an undo log entry if required.

Parameters:
page - the page
old - the old data (if known) or null

update

public void update(Page page)
Update a page.

Parameters:
page - the page

allocatePage

public int allocatePage()
Allocate a page.

Returns:
the page id

free

public void free(int pageId)
Add a page to the free list. The undo log entry must have been written.

Parameters:
pageId - the page id

createData

public Data createData()
Create a data object.

Returns:
the data page.

readPage

public Data readPage(int pos)
Read a page.

Parameters:
pos - the page id
Returns:
the page

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)
Write a page.

Parameters:
pageId - the page id
data - the data

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)
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

commit

public void commit(Session session)
Mark a committed transaction.

Parameters:
session - the session

prepareCommit

public void prepareCommit(Session session,
                          java.lang.String transaction)
Prepare a transaction.

Parameters:
session - the session
transaction - the name of the transaction

isNew

public boolean isNew()
Check whether this is a new database.

Returns:
true if it is

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)
Add the meta data of an index.

Parameters:
index - the index to add
session - the session

removeMeta

public void removeMeta(Index index,
                       Session session)
Remove the meta data of an index.

Parameters:
index - the index to remove
session - the session

setMaxLogSize

public void setMaxLogSize(long maxSize)
Set the maximum transaction log size in megabytes.

Parameters:
maxSize - the new maximum log size

setInDoubtTransactionState

public void setInDoubtTransactionState(int sessionId,
                                       int pageId,
                                       boolean commit)
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

getInDoubtTransactions

public java.util.ArrayList<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

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)
A table is truncated.

Parameters:
session - the session
tableId - the table id

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

incrementChangeCount

public void incrementChangeCount()
Increment the change count. To be done after the operation has finished.


getChangeCount

public int getChangeCount()
Get the current change count. The first value is 1

Returns:
the change count

setLogMode

public void setLogMode(int logMode)

getLogMode

public int getLogMode()

setLockFile

public void setLockFile(boolean lockFile)