Class VariableSizeDataStore

java.lang.Object
com.mckoi.database.VariableSizeDataStore

public class VariableSizeDataStore extends Object
Provides a mechanism for storing variable length data in a file which can quickly be indexed via a reference number. The store maintains a master index file that contains a reference to all individual data records stored in the system. The data file contains all the data that has been stored.

This file format is not intended to be a fully fledged file system. For example, we can not easily change the size of a data entry. To change the record, we must delete it then add a new.

This system uses two files. One file is the index, and the second file stores the data. The index file contains sectors as follows:

   4 (int) : index  - The sector index of the data in the data file.
   4 (int) : length - Length of the data that was stored.
   4 (int) : status - 32 status bits.
 

This employs a simple compression scheme when writing out data that would span more than one sector. It tries compressing the field. If the field can be compressed into less sectors than if left uncompressed, then the compressed field is put into the data store.

Author:
Tobias Downer
  • Constructor Summary

    Constructors
    Constructor
    Description
    VariableSizeDataStore(File name, int sector_size, DebugLogger logger)
    Constructs the variable size store.
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the data store.
    int
    Updates the record allocation table with the data in the output stream returned by 'getRecordOutputStream'.
    void
    copyTo(File path)
    Copies this data store to the given path.
    void
    Deletes the store from the file system.
    int
    delete(int record)
    Deletes the data at the given index position.
    static boolean
    delete(File path, String name)
    Convenience for deleting a VariableSizeDataStore store.
    boolean
    Returns true if the data store exists.
    static boolean
    exists(File path, String name)
    Convenience for checking if a given data store exists or not.
    void
    fix(UserTerminal terminal)
    Attempts to fix a corrupt VariableSizeDataStore object.
    Returns an InputStream that is used to read a record in this store with the given index.
    Returns an OutputStream object that can be used to write data into the store.
    void
    Hard synchronizes the data from memory into the hard copy on disk.
    boolean
    isCompressed(int record)
    Returns true if the given record is compressed.
    boolean
    Returns true if the store was openned in read only mode.
    void
    Locks the store so that not deleted elements may be overwritten.
    boolean
    Returns true if we are locked.
    boolean
    open(boolean read_only)
    Opens the data store.
    int
    Returns the total number of records that are in the store (including deleted records.
    int
    read(int record, byte[] buf, int offset, int length)
    Reads a variable length byte[] array from the given index position.
    byte[]
    readRecord(int record)
    Reads in a complete record and puts it into the returned byte[] array.
    int
    readRecordType(int record_index)
    Reads the 32-bit type_key int for the given record.
    void
    readReservedBuffer(byte[] info, int offset, int length)
    Reads reserved information from the variable data store.
    readString(int record)
     
    boolean
    recordDeleted(int record_index)
    Returns true if the given record is marked as deleted or not.
    int
    recordSectorCount(int record)
    Returns the number of sectors the given record takes up in the data store.
    int
    recordSize(int record)
    Returns the size of the given record number (compressed size if applicable).
    static boolean
    rename(File path_source, String name_source, File path_dest, String name_dest)
    Convenience for renaming a VariableSizeDataStore store to another name.
    int
    Returns the size (in bytes) of the sectors used to store information in the data file.
    void
    Synchronizes all the data in memory with the hard copy on disk.
    long
    Returns the size of the data file that keeps all the data in this store.
    void
    Unlocks the store so that deleted elements can be reclaimed again.
    int
    Returns the number of records that are being used.
    int
    write(byte[] buf, int offset, int length)
    Writes a variable length byte[] array to the first available index.
    int
    writeRecordType(int record_index, int type_key)
    Updates the 32-bit type_key int of a record.
    void
    writeReservedBuffer(byte[] info, int offset, int length)
     
    void
    writeReservedBuffer(byte[] info, int offset, int length, int res_offset)
    Writes reserved information to the variable data store.
    int
     

    Methods inherited from class java.lang.Object

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

    • VariableSizeDataStore

      public VariableSizeDataStore(File name, int sector_size, DebugLogger logger)
      Constructs the variable size store.
    • VariableSizeDataStore

      public VariableSizeDataStore(File name, DebugLogger logger)
  • Method Details

    • synch

      public void synch() throws IOException
      Synchronizes all the data in memory with the hard copy on disk.
      Throws:
      IOException
    • hardSynch

      public void hardSynch() throws IOException
      Hard synchronizes the data from memory into the hard copy on disk. This is guarenteed to ensure the image on the disk will match the image in memory.
      Throws:
      IOException
    • locked

      public boolean locked()
      Returns true if we are locked.
    • lock

      public void lock()
      Locks the store so that not deleted elements may be overwritten.
    • unlock

      public void unlock()
      Unlocks the store so that deleted elements can be reclaimed again.
    • recordDeleted

      public boolean recordDeleted(int record_index) throws IOException
      Returns true if the given record is marked as deleted or not.
      Throws:
      IOException
    • usedRecordCount

      public int usedRecordCount()
      Returns the number of records that are being used.
    • rawRecordCount

      public int rawRecordCount() throws IOException
      Returns the total number of records that are in the store (including deleted records.
      Throws:
      IOException
    • exists

      public boolean exists() throws IOException
      Returns true if the data store exists.
      Throws:
      IOException
    • isReadOnly

      public boolean isReadOnly()
      Returns true if the store was openned in read only mode.
    • open

      public boolean open(boolean read_only) throws IOException
      Opens the data store. The data store can be opened in 'read only' mode. Returns 'true' if the open procedure should repair itself (dirty open) or false if the file was cleanly closed down.

      It is not possible to open a damaged store in read only mode.

      Parameters:
      read_only - if true, then the database is opened in read only mode, otherwise it is opened in read/write mode.
      Throws:
      IOException
    • close

      public void close() throws IOException
      Closes the data store.
      Throws:
      IOException
    • delete

      public void delete()
      Deletes the store from the file system. Must be called after a 'close'.
    • fix

      public void fix(UserTerminal terminal) throws IOException
      Attempts to fix a corrupt VariableSizeDataStore object.

      The store should be open before this method is called.

      Throws:
      IOException
    • copyTo

      public void copyTo(File path) throws IOException
      Copies this data store to the given path. The store must be open when this is called.
      Throws:
      IOException
    • writeRecordType

      public int writeRecordType(int record_index, int type_key) throws IOException
      Updates the 32-bit type_key int of a record. Bit 1-8 are reserved for this data store, and are used to indicate such things as whether the record chain is compressed or not. The rest of the bits can be used for any purpose. It is recommended bits 8 through 16 are used for user-definable information.
      Throws:
      IOException
    • readRecordType

      public int readRecordType(int record_index) throws IOException
      Reads the 32-bit type_key int for the given record. The 'type_key' contains various bit flags set for the record.
      Throws:
      IOException
    • write

      public int write(byte[] buf, int offset, int length) throws IOException
      Writes a variable length byte[] array to the first available index. Returns the index reference for this element.
      Throws:
      IOException
    • read

      public int read(int record, byte[] buf, int offset, int length) throws IOException
      Reads a variable length byte[] array from the given index position. This will read the first n bytes from the element, upto the maximum that was stored. It returns the number of bytes that were read.
      Throws:
      IOException
    • readRecord

      public byte[] readRecord(int record) throws IOException
      Reads in a complete record and puts it into the returned byte[] array.
      Throws:
      IOException
    • delete

      public int delete(int record) throws IOException
      Deletes the data at the given index position.
      Throws:
      IOException
    • getRecordOutputStream

      public OutputStream getRecordOutputStream() throws IOException
      Returns an OutputStream object that can be used to write data into the store. When the 'completeWriteStream' method is called, the records in this store are updated appropriately for the data written in, and a record index is returned.

      NOTE: Only one open stream may be active at a time. While this stream is open this VariableSizeDataStore object may not be used in any other way.

      Throws:
      IOException
    • completeRecordStreamWrite

      public int completeRecordStreamWrite() throws IOException
      Updates the record allocation table with the data in the output stream returned by 'getRecordOutputStream'. Returns an index for how to reference this data later.

      After this method is called it is safe again to use this VariableSizeDataStore object.

      Throws:
      IOException
    • getRecordInputStream

      public InputStream getRecordInputStream(int record) throws IOException
      Returns an InputStream that is used to read a record in this store with the given index.

      NOTE: This can not handle compressed records.

      NOTE: This does not detect the end of stream (reading past the end of the record will return undefined data).

      Throws:
      IOException
    • sectorSize

      public int sectorSize() throws IOException
      Returns the size (in bytes) of the sectors used to store information in the data file.
      Throws:
      IOException
    • recordSize

      public int recordSize(int record) throws IOException
      Returns the size of the given record number (compressed size if applicable).
      Throws:
      IOException
    • isCompressed

      public boolean isCompressed(int record) throws IOException
      Returns true if the given record is compressed.
      Throws:
      IOException
    • recordSectorCount

      public int recordSectorCount(int record) throws IOException
      Returns the number of sectors the given record takes up in the data store.
      Throws:
      IOException
    • totalStoreSize

      public long totalStoreSize()
      Returns the size of the data file that keeps all the data in this store. This is the file size of the data store.
    • writeReservedBuffer

      public void writeReservedBuffer(byte[] info, int offset, int length, int res_offset) throws IOException
      Writes reserved information to the variable data store. You may only write upto 128 bytes to the reserved data buffer.
      Throws:
      IOException
    • writeReservedBuffer

      public void writeReservedBuffer(byte[] info, int offset, int length) throws IOException
      Throws:
      IOException
    • readReservedBuffer

      public void readReservedBuffer(byte[] info, int offset, int length) throws IOException
      Reads reserved information from the variable data store. You may only read upto 128 bytes from the reserved data buffer.
      Throws:
      IOException
    • exists

      public static boolean exists(File path, String name) throws IOException
      Convenience for checking if a given data store exists or not. Returns true if it exists.
      Throws:
      IOException
    • delete

      public static boolean delete(File path, String name) throws IOException
      Convenience for deleting a VariableSizeDataStore store.
      Throws:
      IOException
    • rename

      public static boolean rename(File path_source, String name_source, File path_dest, String name_dest) throws IOException
      Convenience for renaming a VariableSizeDataStore store to another name.
      Throws:
      IOException
    • writeString

      public int writeString(String str) throws IOException
      Throws:
      IOException
    • readString

      public String readString(int record) throws IOException
      Throws:
      IOException