org.magnos.data.store
Class FileStore

java.lang.Object
  extended by org.magnos.data.store.AbstractStore
      extended by org.magnos.data.store.FileStore
All Implemented Interfaces:
Store

public class FileStore
extends AbstractStore

A store which has a file as its persisted medium. All reads and writes are made to the file immediately.

Author:
Philip Diffenderfer

Constructor Summary
FileStore(java.io.File file)
          Instantiantes a new FileStore.
FileStore(java.io.File file, StoreAccess access, int capacity)
          Instantiates a new FileStore while opening it with the given access and setting it to the given capacity.
FileStore(java.lang.String filename)
          Instantiantes a new FileStore.
FileStore(java.lang.String filename, StoreAccess access, int capacity)
          Instantiates a new FileStore while opening it with the given access and setting it to the given capacity.
 
Method Summary
 java.io.File getFile()
          Returns the file the store is persisted to.
 java.io.RandomAccessFile getStream()
          The stream used to perform operations on the file.
protected  void storeClose()
          Should close the store.
protected  void storeDelete()
          Deletes the medium to which the store has been persisting to.
protected  boolean storeExists()
          Returns whether the store exists in its probable medium.
protected  void storeFlush()
          Should flush the contents of the store from memory to a persisted medium if the implementation supports it.
protected  void storeGet(int location, byte[] bytes, int offset, int length)
          Gets data at the given location and stores it in the given byte array.
protected  void storeGet(int location, java.nio.ByteBuffer buffer)
          Gets data at the given location and stores it in the given ByteBuffer.
protected  void storeLoad()
          Should load contents of the store into memory from a persisted medium if the implementation supports it.
protected  int storeOpen(StoreAccess access)
          Should open the store and return the capacity.
protected  void storePut(int location, byte[] bytes, int offset, int length)
          Puts data at the given location from the given byte array.
protected  void storePut(int location, java.nio.ByteBuffer buffer)
          Puts data at the given location from the given ByteBuffer.
protected  int storeResize(int capacity)
          Resizes the store to the given capacity.
 
Methods inherited from class org.magnos.data.store.AbstractStore
capacity, capacity, close, create, delete, equals, exists, flush, get, get, get, get, getAccess, getName, hashCode, isAutoFlush, isAutoLoad, isAutoOpen, isClosed, isOpen, load, open, put, put, put, setAccess, setAutoFlush, setAutoLoad, setAutoOpen, toString, validate
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileStore

public FileStore(java.io.File file,
                 StoreAccess access,
                 int capacity)
Instantiates a new FileStore while opening it with the given access and setting it to the given capacity.

Parameters:
file - The file to persist data to. If this file doesn't exist it will be created when the store is opened, sized, or written to.
access - The requested access to the store.
capacity - The requested capacity of the store.

FileStore

public FileStore(java.lang.String filename,
                 StoreAccess access,
                 int capacity)
Instantiates a new FileStore while opening it with the given access and setting it to the given capacity.

Parameters:
filename - The file to persist data to. If this file doesn't exist it will be created when the store is opened, sized, or written to.
access - The requested access to the store.
capacity - The requested capacity of the store.

FileStore

public FileStore(java.io.File file)
Instantiantes a new FileStore.

Parameters:
file - The file to persist data to. If this file doesn't exist it will be created when the store is opened, sized, or written to.

FileStore

public FileStore(java.lang.String filename)
Instantiantes a new FileStore.

Parameters:
filename - The file to persist data to. If this file doesn't exist it will be created when the store is opened, sized, or written to.
Method Detail

getFile

public java.io.File getFile()
Returns the file the store is persisted to.

Returns:
The stores file.

getStream

public java.io.RandomAccessFile getStream()
The stream used to perform operations on the file.

Returns:
The stores data stream.

storeOpen

protected int storeOpen(StoreAccess access)
                 throws java.io.IOException
Should open the store and return the capacity. If the requested access could not be granted an exception should be thrown. This will only be invoked if the store has been closed previously or has not been opened yet. If the store doesn't exist this should forcefully create it before opening it with the requested access.

Specified by:
storeOpen in class AbstractStore
Returns:
The capacity of the store.
Throws:
java.io.IOException - An error occurred opening the store with the given access.

storeLoad

protected void storeLoad()
                  throws java.io.IOException
Should load contents of the store into memory from a persisted medium if the implementation supports it.

Specified by:
storeLoad in class AbstractStore
Throws:
java.io.IOException - An error occurred loading the store.

storeFlush

protected void storeFlush()
                   throws java.io.IOException
Should flush the contents of the store from memory to a persisted medium if the implementation supports it.

Specified by:
storeFlush in class AbstractStore
Throws:
java.io.IOException - An error occurred flushing the store.

storeClose

protected void storeClose()
                   throws java.io.IOException
Should close the store. This will only be invoked if the store has been successfully opened previously. If there is an error closing the store an exception should be thrown. This exception however may be ignored due to the likeliness that the store is still closed even after an exception is thrown.

Specified by:
storeClose in class AbstractStore
Throws:
java.io.IOException - An error occurred closing the store.

storeDelete

protected void storeDelete()
Deletes the medium to which the store has been persisting to.

Specified by:
storeDelete in class AbstractStore

storeExists

protected boolean storeExists()
Returns whether the store exists in its probable medium. This could be called before or after the store has been opened or created.

Specified by:
storeExists in class AbstractStore
Returns:
True if the store already exists.

storeResize

protected int storeResize(int capacity)
                   throws java.io.IOException
Resizes the store to the given capacity. If the store could not be resized to the requested capacity an exception should be thrown. This will only be called if the store was successfully opened previously. If the resize results in a larger store the end of the store should be padded with zeros (which the system will implicitly due most likely).

Specified by:
storeResize in class AbstractStore
Parameters:
capacity - The new capacity of the store.
Throws:
java.io.IOException - An error occured resizing the store.

storeGet

protected void storeGet(int location,
                        byte[] bytes,
                        int offset,
                        int length)
                 throws java.io.IOException
Gets data at the given location and stores it in the given byte array.

Specified by:
storeGet in class AbstractStore
Parameters:
location - The offset to get the data, in bytes, from the beginning of the store.
bytes - The array of bytes to place the data in.
offset - The offset in the byte array to place the data.
length - The number of bytes to copy from the store and place in the array.
Throws:
java.io.IOException - An error occurred reading from the store.

storePut

protected void storePut(int location,
                        byte[] bytes,
                        int offset,
                        int length)
                 throws java.io.IOException
Puts data at the given location from the given byte array.

Specified by:
storePut in class AbstractStore
Parameters:
location - The offset to put the data, in bytes, from the beginning of the store.
bytes - The array of bytes to take data from.
offset - The offset in the byte array to take the data.
length - The number of bytes to copy from the array and place in the store.
Throws:
java.io.IOException - An error occurred writing to the store.

storeGet

protected void storeGet(int location,
                        java.nio.ByteBuffer buffer)
                 throws java.io.IOException
Gets data at the given location and stores it in the given ByteBuffer.

Specified by:
storeGet in class AbstractStore
Parameters:
location - The offset to get the data, in bytes, from the beginning of the store.
buffer - The buffer to place the data in. The buffer will be filled with data to its limit (so it has no remaining bytes).
Throws:
java.io.IOException - An error occurred reading from the store.

storePut

protected void storePut(int location,
                        java.nio.ByteBuffer buffer)
                 throws java.io.IOException
Puts data at the given location from the given ByteBuffer.

Specified by:
storePut in class AbstractStore
Parameters:
location - The offset to put the data, in bytes, from the beginning of the store.
buffer - The buffer to take the data from. The buffer will be emptied of data to its limit (so it has no remaining bytes).
Throws:
java.io.IOException - An error occurred writing to the store.