public abstract class ObjectStorageFile
extends java.lang.Object
This class can also restore an old table, as defined in the constructor.
Implementation:
Since the table is on disk, any new or updated Objects must be appended to the file. This causes the file to grow rather large, seeing that an old or outdated Object cannot be removed from the table. A "removed" or "outdated" Object is simply marked for later removal, during the "compression" stage. The "compression" takes place when a pre-defined number of "outdated" Objects have been reached. This number must be defined in the constructor, and is known as the compression threshold. Keep in mind that a lower compression threshold causing compression to happen more often, but results in the disk table being smaller. Likewise, a larger compression threshold causing a less frequent compression, thereby improving performance, but allowing a larger disk table.
This class must be extended to handle the specifics of the Objects it is storing.
Through use of the "setupWalker" and "walkFile" methods, a user may step through the file by valid Object. This means that all "outdated" Objects will be skipped. Note that the walker supports only one active walk at a time, and the user must use "setupWalker" to reset the walker. It will not reset on its own.
Modifier and Type | Field and Description |
---|---|
protected ObjectStorageAllocator |
allocator |
protected int |
currentStorageSize |
protected byte[] |
headers |
protected byte[] |
headers_no_flag |
protected static byte[] |
invalid_flags |
protected static int |
INVALID_OBJ |
protected static long |
MAX_PARTITION_SIZE |
protected long |
maxPartitionSize |
static int |
OBJ_HEADER |
protected java.io.RandomAccessFile |
objectFile
RandomAccessFile that actually stores the Objects.
|
protected boolean |
partitioned |
protected long |
time_used |
protected static byte[] |
valid_flags |
protected static int |
VALID_OBJ |
Constructor and Description |
---|
ObjectStorageFile(int count,
java.io.File fileName,
boolean newFile)
Constructor for the storage manager.
|
Modifier and Type | Method and Description |
---|---|
long |
add(java.lang.Object obj)
Adds a new Object to the table.
|
protected long |
addBytes(byte[] newBytes,
ObjectStorageInfo info)
Adds a set of bytes to the file, and returns the offset
where they are stored.
|
java.lang.String |
allocatorReport() |
void |
close()
Closes a storage file.
|
void |
drop() |
java.lang.Object |
get(long offset)
Get the Object at the given offset.
|
protected byte[] |
getBytes(long offset)
Returns the bytes at the given offset.
|
int |
getNumObjects()
Returns the number of Objects in the storage file.
|
java.io.File |
getObjectFile()
Returns the File object associated with this ObjectStorageFile.
|
java.lang.Object[] |
getSomeObjects(int numObjects)
Returns a given set of Objects that exist in the file.
|
protected long[] |
getValidOffsets()
Returns a list of offsets for all the valid Objects in the
file.
|
boolean |
isEmpty() |
boolean |
isNewFile()
Returns whether this is a new storage file.
|
protected void |
markDeletedBytes(long offset) |
protected abstract java.lang.Object |
objectFromBytes(byte[] bytes)
This method should return an Object, as created by the given
bytes.
|
protected abstract byte[] |
objectToBytes(java.lang.Object obj)
This method should return the Object in its byte[] format.
|
void |
open()
Opens a storage file.
|
java.lang.Object |
remove(long offset,
boolean bRetValue)
Remove the Object at the given offset.
|
protected byte[] |
removeBytes(long offset)
Removes the bytes at the given offset, and returns them.
|
byte[] |
removeDelayedParsing(long offset,
boolean bRetValue)
Same as remove(), except that objectFromBytes() is not called.
|
long[] |
restoreFile()
Rebuilds the storage file offsets from a existing table
storage file.
|
void |
setPartition(long partitionSize) |
protected void |
setupWalker()
Sets up the file walker to start at the beginning of the file.
|
protected void |
setupWalker(long offset)
Sets up the file walker to start at the given offset.
|
java.lang.String |
toString()
Converts the storage file to String representation.
|
void |
toString(java.io.OutputStream os)
Writes the String representation of this storage file to the
given OutputStream.
|
long |
update(java.lang.Object obj,
long offset)
Update a prior Object with a new Object.
|
protected long |
walkFile()
Walks the file to the next valid Object, and returns its offset.
|
protected java.io.RandomAccessFile objectFile
public static final int OBJ_HEADER
protected ObjectStorageAllocator allocator
protected int currentStorageSize
protected static final int VALID_OBJ
protected static final int INVALID_OBJ
protected long time_used
protected static final long MAX_PARTITION_SIZE
protected boolean partitioned
protected byte[] headers
protected static final byte[] invalid_flags
protected static final byte[] valid_flags
protected byte[] headers_no_flag
protected long maxPartitionSize
public ObjectStorageFile(int count, java.io.File fileName, boolean newFile)
count
- Compression threshold. (see class description)fileName
- File name to use for the table.newFile
- Is this file name for a new table, or should
this restore an old table?public void setPartition(long partitionSize)
public java.io.File getObjectFile()
public long add(java.lang.Object obj) throws java.io.IOException
obj
- Add this Object to the table.java.io.IOException
public java.lang.Object get(long offset) throws java.io.IOException
offset
- to retrieve the Object.java.io.IOException
public java.lang.Object remove(long offset, boolean bRetValue) throws java.io.IOException
offset
- Offset of the Object.bRetValue
- Whether it will return the removed object or notjava.io.IOException
public byte[] removeDelayedParsing(long offset, boolean bRetValue) throws java.io.IOException
offset
- bRetValue
- java.io.IOException
public long update(java.lang.Object obj, long offset) throws java.io.IOException
obj
- New Object.offset
- Offset of the old Object.java.io.IOException
public long[] restoreFile() throws java.io.IOException
java.io.IOException
public void open() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
java.io.IOException
public void drop()
public boolean isNewFile()
public int getNumObjects() throws java.io.IOException
java.io.IOException
protected long[] getValidOffsets() throws java.io.IOException
java.io.IOException
protected void setupWalker() throws java.io.IOException
java.io.IOException
protected void setupWalker(long offset) throws java.io.IOException
java.io.IOException
protected long walkFile() throws java.io.IOException
java.io.IOException
public java.lang.Object[] getSomeObjects(int numObjects) throws java.io.IOException
numObjects
- Number of Objects to return.java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
public void toString(java.io.OutputStream os) throws java.io.IOException
os
- OutputStream to write to.java.io.IOException
public java.lang.String allocatorReport()
protected long addBytes(byte[] newBytes, ObjectStorageInfo info) throws java.io.IOException
newBytes
- Bytes to storejava.io.IOException
protected byte[] getBytes(long offset) throws java.io.IOException
offset
- offset to retrieve the bytesjava.io.IOException
protected void markDeletedBytes(long offset) throws java.io.IOException
java.io.IOException
protected byte[] removeBytes(long offset) throws java.io.IOException
offset
- offset of bytes to removejava.io.IOException
public boolean isEmpty()
protected abstract java.lang.Object objectFromBytes(byte[] bytes)
bytes
- byte[] used to create the Objectprotected abstract byte[] objectToBytes(java.lang.Object obj)
obj
- Object to convert to bytes.