com.cometway.om
Class FileObjectBlock

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

public class FileObjectBlock
extends java.lang.Object

This class is used to store Objects into a file prepared by FileObjectStore. The store returns a reference to a block in the file. Objects can be written and read from this block. A FileObjectBlock can be given a header description (an Object) as well as the Object stored in the body of the block. If the Object stored in the body of the block exceeds the length of the block, a new block will be allocated and the remaining bytes stored there. If the next block is used up, it will automatically allocate a new one from the store, and so on. When these extra blocks in chain of blocks are no longer needed, they are automatically disposed of.

See Also:
FileObjectStore

Field Summary
protected static int BODY_DATA_OFFSET
           
protected static int BODY_EXTENDED_DATA_OFFSET
           
protected static int BODY_TAG_OFFSET
           
 boolean conservativeWithErrors
          When this flag is set to FALSE this FileObjectBlock will be slightly more inventive when encountering unexpected errors rather than just give up.
protected static int HEADER_DATA_OFFSET
           
protected static int HEADER_NEXT_INDEX_OFFSET
           
protected static int HEADER_PARENT_INDEX_OFFSET
           
protected static int HEADER_TAG_OFFSET
           
protected  int headerLength
           
protected  boolean isEmpty
           
protected  FileObjectBlock nextBlock
           
protected  int parentBlockIndex
           
 
Constructor Summary
protected FileObjectBlock(FileObjectStore store, int index)
          Creates an instance of FileObjectBlock.
 
Method Summary
protected  void debug(java.lang.String s)
           
 void dispose()
          This method is called when the Block is no longer needed and should be considered a FREE BLOCK, to be reused.
 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)
           
protected  void finalize()
          Overrides Object.finalize().
protected  void format()
          This method writes the physical space of the store file so that Objects can be written and read from this Block.
protected  long getOffset()
          This returns the dynamic offset of the beginning of this block in the store file
 int hashCode()
          Overrides Object.hashCode().
 boolean isFreeBlock()
          This method reads the dirty flag in the store file for this Block and returns true if it reports itself to be FREE.
protected  void print(java.lang.String s)
           
 java.lang.Object readHeader()
          This method returns the Object stored in the header of this block.
 java.lang.Object readObject()
          This method reads the Object stored in the body of this Block.
 void updateFromFile()
          This method updates all the local instance fields from information stored in the file.
 boolean writeHeader(java.lang.Object data)
          This method writes an Object in the header of this Block.
 boolean writeObject(java.lang.Object data)
          This method stores an Object in the body of this Block.
 
Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADER_TAG_OFFSET

protected static final int HEADER_TAG_OFFSET
See Also:
Constant Field Values

HEADER_PARENT_INDEX_OFFSET

protected static final int HEADER_PARENT_INDEX_OFFSET
See Also:
Constant Field Values

HEADER_NEXT_INDEX_OFFSET

protected static final int HEADER_NEXT_INDEX_OFFSET
See Also:
Constant Field Values

HEADER_DATA_OFFSET

protected static final int HEADER_DATA_OFFSET
See Also:
Constant Field Values

BODY_TAG_OFFSET

protected static final int BODY_TAG_OFFSET
See Also:
Constant Field Values

BODY_DATA_OFFSET

protected static final int BODY_DATA_OFFSET
See Also:
Constant Field Values

BODY_EXTENDED_DATA_OFFSET

protected static final int BODY_EXTENDED_DATA_OFFSET
See Also:
Constant Field Values

headerLength

protected int headerLength

isEmpty

protected boolean isEmpty

nextBlock

protected FileObjectBlock nextBlock

parentBlockIndex

protected int parentBlockIndex

conservativeWithErrors

public boolean conservativeWithErrors
When this flag is set to FALSE this FileObjectBlock will be slightly more inventive when encountering unexpected errors rather than just give up.

Constructor Detail

FileObjectBlock

protected FileObjectBlock(FileObjectStore store,
                          int index)
Creates an instance of FileObjectBlock.

Parameters:
store - THis is the FileObjectStore which this block will read and write to.
index - This is the index into the store file which this block will read and write to.
Method Detail

updateFromFile

public void updateFromFile()
This method updates all the local instance fields from information stored in the file.


readHeader

public java.lang.Object readHeader()
This method returns the Object stored in the header of this block. If the Object's size exceeds the fixed size of the header, it may not be unserializable. If no object was found or it could not be unserialized, null is returned.

Returns:
Returns the Object stored in this Block's header, or null if nothing was found or the data was unserializable.

writeHeader

public boolean writeHeader(java.lang.Object data)
This method writes an Object in the header of this Block. If the object was successfully written, TRUE will be returned.

Parameters:
data - This Object will be stored in the header of this Block
Returns:
Returns true IFF the Object was written successfully.

readObject

public java.lang.Object readObject()
This method reads the Object stored in the body of this Block. If the Object's data exceeds the size of this Block, the next block(s) in the chain will be read from until the entire Object has been reconstructed. If the Object could not be read, null will be returned.

Returns:
Returns the Object stored in the Body of this Block, null if an error occurred or nothing was stored.

writeObject

public boolean writeObject(java.lang.Object data)
This method stores an Object in the body of this Block. If the Object's size exceeds the length of this Block, a new block will be allocated to store the remaining bytes. If the Object was successfully written, true will be returned.

Parameters:
data - The Object to write in the body of this Block.
Returns:
Returns true IFF the object was written successfully.

dispose

public void dispose()
This method is called when the Block is no longer needed and should be considered a FREE BLOCK, to be reused. This method will not reduce the size of the store file. This method will dispose the next block if one is allocated.


format

protected void format()
               throws java.io.IOException
This method writes the physical space of the store file so that Objects can be written and read from this Block. Doing this after writing information to this Block will destroy the previous data. This method also writes the dirty flag to the HEADER_TAG of this block's header, allowing it to be reused.

Throws:
java.io.IOException - Throws the IOException caused when writing to the store file.

isFreeBlock

public boolean isFreeBlock()
This method reads the dirty flag in the store file for this Block and returns true if it reports itself to be FREE.


getOffset

protected long getOffset()
This returns the dynamic offset of the beginning of this block in the store file

Returns:
Returns the offset starting from the beginning of the store file for the beginning of this block.

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides Object.finalize(). This method makes sure that the store is unreferenced.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

equals

public boolean equals(java.lang.Object o)
Overrides Object.equals(Object). This makes the FileObjectStore and the local index an equivalence relation.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides Object.hashCode(). Uses the hashcode of the String constructed using the FileObjectStore and the local index.

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)