## org.h2.store
Class Storage

    java.lang.Object
      org.h2.store.Storage

    public class Storage

extends java.lang.Object

This class represents an persistent container that stores data of a table or an index. An object contains a list of records, see `Record`. For each storage there is a `RecordReader` object that knows how to convert records into a byte array and vice versa. The data is stored in a `DiskFile`. A storage occupies a number of pages in a file. File format:

     int block size
     int storage id
     record data
     byte checksum
     [bytes * fillerLength]

Field Summary
`static int` `ALLOCATE_POS` This value is used to indicate that the position is not yet known, and space needs to be allocated.

Constructor Summary
`Storage(Database database, DiskFile file, RecordReader reader, int id)` Create a new storage object for this file.

Method Summary
`void` `addRecord(Session session, Record record, int pos)` Add or update a record in the file.
`void` `flushRecord(Record rec)` Write this record now.
`DiskFile` `getDiskFile()`
`int` `getId()` Get the unique storage id.
`int` `getNext(Record record)` Gets the position of the next record.
`Record` `getRecord(Session session, int pos)` Read a record from the file or cache.
`int` `getRecordCount()` Get the number of records in this storage.
`Record` `getRecordIfStored(Session session, int pos)` Read a record if it is stored at that location.
`int` `getRecordOverhead()` Get the overhead to store a record (header data) in number of bytes.
`RecordReader` `getRecordReader()` Get the record parser for this storage.
`void` `removeRecord(Session session, int pos)` Remove a record.
`void` `setReader(RecordReader reader)` Set the record parser for this storage.
`void` `setRecordCount(int recordCount)` Update the record count.
`void` `truncate(Session session)` Delete all records from this storage.
`void` `updateRecord(Session session, Record record)` Update an existing record.

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

Field Detail

### ALLOCATE_POS

    public static final int ALLOCATE_POS

  This value is used to indicate that the position is not yet known, and space needs to be allocated.

  See Also:
    Constant Field Values

Constructor Detail

### Storage

    public Storage(Database database,
                   DiskFile file,
                   RecordReader reader,
                   int id)

  Create a new storage object for this file.

  Parameters:
    `database` - the database
    `file` - the file
    `reader` - the reader that can parse records
    `id` - the storage id

Method Detail

### getRecordReader

    public RecordReader getRecordReader()

  Get the record parser for this storage.

  Returns:
    the record parser

### getRecord

    public Record getRecord(Session session,
                            int pos)
                     throws java.sql.SQLException

  Read a record from the file or cache.

  Parameters:
    `session` - the session
    `pos` - the position of the record
  Returns:
    the record
  Throws:
    `java.sql.SQLException`

### getRecordIfStored

    public Record getRecordIfStored(Session session,
                                    int pos)
                             throws java.sql.SQLException

  Read a record if it is stored at that location.

  Parameters:
    `session` - the session
    `pos` - the position where it is stored
  Returns:
    the record or null
  Throws:
    `java.sql.SQLException`

### getNext

    public int getNext(Record record)

  Gets the position of the next record.

  Parameters:
    `record` - the last record (null to get the first record)
  Returns:
    -1 if no record is found, otherwise the position

### updateRecord

    public void updateRecord(Session session,
                             Record record)
                      throws java.sql.SQLException

  Update an existing record.

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

### addRecord

    public void addRecord(Session session,
                          Record record,
                          int pos)
                   throws java.sql.SQLException

  Add or update a record in the file.

  Parameters:
    `session` - the session
    `record` - the record
    `pos` - the position (use ALLOCATE_POS to add a new record)
  Throws:
    `java.sql.SQLException`

### removeRecord

    public void removeRecord(Session session,
                             int pos)
                      throws java.sql.SQLException

  Remove a record.

  Parameters:
    `session` - the session
    `pos` - where the record is stored
  Throws:
    `java.sql.SQLException`

### getId

    public int getId()

  Get the unique storage id.

  Returns:
    the id

### getRecordCount

    public int getRecordCount()

  Get the number of records in this storage.

  Returns:
    the number of records

### truncate

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

  Delete all records from this storage.

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

### setReader

    public void setReader(RecordReader reader)

  Set the record parser for this storage.

  Parameters:
    `reader` - the record parser

### flushRecord

    public void flushRecord(Record rec)
                     throws java.sql.SQLException

  Write this record now.

  Parameters:
    `rec` - the record to write
  Throws:
    `java.sql.SQLException`

### getRecordOverhead

    public int getRecordOverhead()

  Get the overhead to store a record (header data) in number of bytes.

  Returns:
    the overhead

### getDiskFile

    public DiskFile getDiskFile()

### setRecordCount

    public void setRecordCount(int recordCount)

  Update the record count.

  Parameters:
    `recordCount` - the new record count