org.h2.store
Class DiskFile

java.lang.Object
  extended by org.h2.store.DiskFile
All Implemented Interfaces:
CacheWriter

public class DiskFile
extends java.lang.Object
implements CacheWriter

This class represents a file that is usually written to disk. The two main files are .data.db and .index.db. For each such file, a number of Storage objects exists. The disk file is responsible for caching; each object contains a Cache object. Changes in the file are logged in a LogSystem object. Reading and writing to the file is delegated to the FileStore class.

There are 'blocks' of 128 bytes (DiskFile.BLOCK_SIZE). Each objects own one or more pages; each page size is 64 blocks (DiskFile.BLOCKS_PER_PAGE). That is 8 KB page size. However pages are not read or written as one unit; only individual objects (multiple blocks at a time) are read or written.

Currently there are no in-place updates. Each row occupies one or multiple blocks. Rows can occupy multiple pages. Rows are always contiguous (except LOBs, they are stored in their own files).


Field Summary
static int BLOCK_PAGE_PAGE_SHIFT
          The number of bits to shift to divide a position to get the page number.
static int BLOCK_SIZE
          The size of a block in bytes.
static int BLOCKS_PER_PAGE
          The size of a page in blocks.
 
Constructor Summary
DiskFile(Database database, java.lang.String fileName, java.lang.String mode, boolean dataFile, boolean logChanges, int cacheSize)
          Create a new disk file.
 
Method Summary
 void addRedoLog(Storage storage, int recordId, int blockCount, DataPage rec)
          Add a redo-log entry to the redo buffer.
 void close()
          Flush all pending changes and close the file.
 int copyDirect(int pos, java.io.OutputStream out)
          Copy a number of bytes at the specified location to the output stream.
 void delete()
          Close the file and delete it.
 void flush()
          Flush all pending changes to disk.
 void flushLog()
          Flush the log file, so that entries can be removed from the cache.
 void flushRedoLog()
          Write all buffered redo log data to the file.
 Cache getCache()
          Get the cache.
 int getReadCount()
           
 byte[] getSummary()
          Get the 'storage allocation table' of this file.
 Trace getTrace()
          Get the trace writer.
 int getWriteCount()
           
 void init()
          Read the 'storage allocation table' from the file if required.
 void initFromSummary(byte[] summary)
          Initialize the the 'storage allocation table' of this file from a given byte array.
 boolean isDataFile()
          Check if this is the data file.
 void setLogChanges(boolean logChanges)
          Set whether changes should be written to the transaction log before they are applied in the file.
 void setPageOwner(int page, int storageId)
          Set the owner of a page.
 void sync()
          Flush pending writes of the underlying file.
 java.lang.String toString()
           
 void writeBack(CacheObject obj)
          Write a record to the file immediately.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BLOCK_PAGE_PAGE_SHIFT

public static final int BLOCK_PAGE_PAGE_SHIFT
The number of bits to shift to divide a position to get the page number.

See Also:
Constant Field Values

BLOCKS_PER_PAGE

public static final int BLOCKS_PER_PAGE
The size of a page in blocks. Each page contains blocks from the same storage.

See Also:
Constant Field Values

BLOCK_SIZE

public static final int BLOCK_SIZE
The size of a block in bytes. A block is the minimum row size.

See Also:
Constant Field Values
Constructor Detail

DiskFile

public DiskFile(Database database,
                java.lang.String fileName,
                java.lang.String mode,
                boolean dataFile,
                boolean logChanges,
                int cacheSize)
         throws java.sql.SQLException
Create a new disk file.

Parameters:
database - the database
fileName - the file name
mode - the file opening mode ("r", "rw", "rws", "rwd")
dataFile - if this is the data file
logChanges - if changes should be logged
cacheSize - the number of cache entries
Throws:
java.sql.SQLException
Method Detail

getSummary

public byte[] getSummary()
                  throws java.sql.SQLException
Get the 'storage allocation table' of this file.

Returns:
the table
Throws:
java.sql.SQLException

initFromSummary

public void initFromSummary(byte[] summary)
Initialize the the 'storage allocation table' of this file from a given byte array.

Parameters:
summary - the storage allocation table

init

public void init()
          throws java.sql.SQLException
Read the 'storage allocation table' from the file if required.

Throws:
java.sql.SQLException

flush

public void flush()
           throws java.sql.SQLException
Flush all pending changes to disk.

Throws:
java.sql.SQLException

close

public void close()
           throws java.sql.SQLException
Flush all pending changes and close the file.

Throws:
java.sql.SQLException

setPageOwner

public void setPageOwner(int page,
                         int storageId)
                  throws java.sql.SQLException
Set the owner of a page.

Parameters:
page - the page id
storageId - the storage id of this page
Throws:
java.sql.SQLException

delete

public void delete()
            throws java.sql.SQLException
Close the file and delete it.

Throws:
java.sql.SQLException

writeBack

public void writeBack(CacheObject obj)
               throws java.sql.SQLException
Write a record to the file immediately. This method is called by the cache, and when flushing pending changes.

Specified by:
writeBack in interface CacheWriter
Parameters:
obj - the record to write
Throws:
java.sql.SQLException

copyDirect

public int copyDirect(int pos,
                      java.io.OutputStream out)
               throws java.sql.SQLException
Copy a number of bytes at the specified location to the output stream.

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

getCache

public Cache getCache()
Get the cache. The cache must be synchronized externally.

Returns:
the cache

sync

public void sync()
Flush pending writes of the underlying file.


isDataFile

public boolean isDataFile()
Check if this is the data file.

Returns:
true if this is the data file

setLogChanges

public void setLogChanges(boolean logChanges)
Set whether changes should be written to the transaction log before they are applied in the file.

Parameters:
logChanges - the new value

addRedoLog

public void addRedoLog(Storage storage,
                       int recordId,
                       int blockCount,
                       DataPage rec)
                throws java.sql.SQLException
Add a redo-log entry to the redo buffer.

Parameters:
storage - the storage
recordId - the record id of the entry
blockCount - the number of blocks
rec - the record
Throws:
java.sql.SQLException

flushRedoLog

public void flushRedoLog()
                  throws java.sql.SQLException
Write all buffered redo log data to the file. Redo log data is buffered to improve recovery performance.

Throws:
java.sql.SQLException

getWriteCount

public int getWriteCount()

getReadCount

public int getReadCount()

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getTrace

public Trace getTrace()
Description copied from interface: CacheWriter
Get the trace writer.

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