com.cometway.om
Class FileObjectStore

java.lang.Object
  extended by com.cometway.om.FileObjectStore

public class FileObjectStore
extends java.lang.Object

This class takes a File and partitions it into FileObjectBlocks. This class will keep track of free blocks and allocate new blocks if needed.

See Also:
FileObjectBlock

Field Summary
protected  long BLOCK_SIZE
           
protected  java.lang.String headerInfo
           
protected  long headerLength
           
 
Constructor Summary
FileObjectStore(java.io.File storeFile)
          Creates an instance of FileObjectStore using an empty description and the default block size.
FileObjectStore(java.io.File storeFile, java.lang.String fileInfo)
          Creates an instance of FileObjectStore using the description parameter and the default block size.
FileObjectStore(java.io.File storeFile, java.lang.String fileInfo, long blockSize)
          Creates an instance of FileObjectStore using the description parameter and the given block size.
 
Method Summary
 boolean allocateNewBlocks(int number)
          This method allocates new blocks at the end of the store file.
 boolean changeFileInfo(java.lang.String newInfo)
          This method changes the String info stored in the header of this Store to the String parameter.
protected  void debug(java.lang.String s)
           
 void dispose()
          This method removes this file from the filesystem and frees resources that it was using.
 boolean equals(java.lang.Object o)
          Overrides Object.equals(Object).
protected  void error(java.lang.String s)
           
protected  void error(java.lang.String s, java.lang.Exception e)
           
 FileObjectBlock getBlock(int index)
          This method retrieves the previously allocated FileObjectBlock with the index given.
 java.lang.String getFileInfo()
          This method reads the String info stored in the header of this Store and returns it.
 FileObjectBlock getNextFreeBlock()
          This method returns the next Free Block.
 int hashCode()
          Overrides Object.hashCode().
protected  void initBlocks()
          This method attempts to initialize all the blocks stored in the file.
protected  void print(java.lang.String s)
           
protected  byte[] readBytes(long start, long stop)
          This method reads a byte array from the file at the positions given as the parameters.
protected  void readHeader()
          This method reads the store file header information from the physical file.
protected  int readInt(long start)
          This method reads an int at the position in the file given as the parameter.
protected  long readLong(long start)
          This method reads a long at the position in the file given as the parameter.
protected  void writeBytes(byte[] data, long start)
          This method writes a byte array to the file starting at the position given by the parameter.
protected  void writeHeader()
          This method writes the store file header information to the physical file.
protected  void writeInt(int data, long start)
          This method writes an int at the position in the file given as the parameter.
protected  void writeLong(long data, long start)
          This method writes a long at the position in the file given as the parameter.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

protected long BLOCK_SIZE

headerLength

protected long headerLength

headerInfo

protected java.lang.String headerInfo
Constructor Detail

FileObjectStore

public FileObjectStore(java.io.File storeFile)
                throws java.io.IOException
Creates an instance of FileObjectStore using an empty description and the default block size. The parameter passed in will be used to initialize any previous information in this file (NOTE that the BLOCK sizes must be the same) and for reading and writing.

Parameters:
storeFile - This is the File which will be used for initialization and all reads and writes.
Throws:
java.io.IOException - This is thrown if the FileObjectStore could not be created from the given parameters.

FileObjectStore

public FileObjectStore(java.io.File storeFile,
                       java.lang.String fileInfo)
                throws java.io.IOException
Creates an instance of FileObjectStore using the description parameter and the default block size. The File parameter passed in will be used to initialize any previous information in this file (NOTE that the BLOCK sizes must be the same) and for reading and writing.

Parameters:
storeFile - This is the File which will be used for initialization and all reads and writes.
fileInfo - This will be stored in the header of this store file (NOT block headers) as a description for this file. If this is an empty String, the original fileInfo will attempt to be read in.
Throws:
java.io.IOException - This is thrown if the FileObjectStore could not be created from the given parameters.

FileObjectStore

public FileObjectStore(java.io.File storeFile,
                       java.lang.String fileInfo,
                       long blockSize)
                throws java.io.IOException
Creates an instance of FileObjectStore using the description parameter and the given block size. The File parameter passed in will be used to initialize any previous information in this file (NOTE that the BLOCK sizes must be the same) and for reading and writing. The Block size must exceed the required size the Block's header.

Parameters:
storeFile - This is the File which will be used for initialization and all reads and writes.
fileInfo - This will be stored in the header of this store file (NOT block headers) as a description for this file. If this is an empty String, the original fileInfo will attempt to be read in.
blockSize - This will be the size each FileObjectBlock allocated by this FileObjectStore.
Throws:
java.io.IOException - This is thrown if the FileObjectStore could not be created from the given parameters.
See Also:
FileObjectBlock
Method Detail

getFileInfo

public java.lang.String getFileInfo()
This method reads the String info stored in the header of this Store and returns it.


changeFileInfo

public boolean changeFileInfo(java.lang.String newInfo)
This method changes the String info stored in the header of this Store to the String parameter. True will be returned if the change was successful.


getBlock

public FileObjectBlock getBlock(int index)
                         throws java.lang.ArrayIndexOutOfBoundsException
This method retrieves the previously allocated FileObjectBlock with the index given.

Parameters:
index - This is the block index of the FileObjectBlock to retrieve.
Throws:
java.lang.ArrayIndexOutOfBoundsException - This is thrown if the index given not used by this Store.

getNextFreeBlock

public FileObjectBlock getNextFreeBlock()
This method returns the next Free Block. If one does not exist (all allocated blocks are being used), another one will be allocated. If another cannot be allocated, null is returned.

Returns:
Returns a FileObjectBlock available for storing Object in, null if no more are available.

allocateNewBlocks

public boolean allocateNewBlocks(int number)
This method allocates new blocks at the end of the store file. These blocks will have been formatted and are stored in the freeBlocks Queue.

Parameters:
number - The number of new blocks to allocate.
Returns:
Returns true IFF the specified number of new blocks were allocated successfully.

dispose

public void dispose()
This method removes this file from the filesystem and frees resources that it was using.


initBlocks

protected void initBlocks()
                   throws java.io.IOException
This method attempts to initialize all the blocks stored in the file. Here is where the chains are rebuilt. This method calls FileObjectBlock.updateFromFile().

Throws:
java.io.IOException

writeHeader

protected void writeHeader()
                    throws java.io.IOException
This method writes the store file header information to the physical file.

Throws:
java.io.IOException

readHeader

protected void readHeader()
                   throws java.io.IOException
This method reads the store file header information from the physical file. The local field is updated.

Throws:
java.io.IOException

readLong

protected long readLong(long start)
                 throws java.io.IOException
This method reads a long at the position in the file given as the parameter.

Throws:
java.io.IOException

writeLong

protected void writeLong(long data,
                         long start)
                  throws java.io.IOException
This method writes a long at the position in the file given as the parameter.

Throws:
java.io.IOException

readInt

protected int readInt(long start)
               throws java.io.IOException
This method reads an int at the position in the file given as the parameter.

Throws:
java.io.IOException

writeInt

protected void writeInt(int data,
                        long start)
                 throws java.io.IOException
This method writes an int at the position in the file given as the parameter.

Throws:
java.io.IOException

readBytes

protected byte[] readBytes(long start,
                           long stop)
                    throws java.io.IOException
This method reads a byte array from the file at the positions given as the parameters.

Throws:
java.io.IOException

writeBytes

protected void writeBytes(byte[] data,
                          long start)
                   throws java.io.IOException
This method writes a byte array to the file starting at the position given by the parameter.

Throws:
java.io.IOException

equals

public boolean equals(java.lang.Object o)
Overrides Object.equals(Object). Makes the Store File (java.io.File) object an equavalence relation.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides Object.hashCode(). Uses the java.io.File as the hashCode.

Overrides:
hashCode in class java.lang.Object

print

protected void print(java.lang.String s)

error

protected void error(java.lang.String s)

error

protected void error(java.lang.String s,
                     java.lang.Exception e)

debug

protected void debug(java.lang.String s)