Class ObjectStorageFile
- Direct Known Subclasses:
NARKeyStorageFile
,NARStorageFile
,TimeStampedRecordStorageFile
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ObjectStorageAllocator
protected int
protected byte[]
protected byte[]
protected static final byte[]
protected static final int
protected static final long
protected long
static final int
protected RandomAccessFile
RandomAccessFile that actually stores the Objects.protected boolean
protected long
protected static final byte[]
protected static final int
-
Constructor Summary
ConstructorsConstructorDescriptionObjectStorageFile
(int count, File fileName, boolean newFile) Constructor for the storage manager. -
Method Summary
Modifier and TypeMethodDescriptionlong
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.void
close()
Closes a storage file.void
drop()
get
(long offset) Get the Object at the given offset.protected byte[]
getBytes
(long offset) Returns the bytes at the given offset.int
Returns the number of Objects in the storage file.Returns the File object associated with this ObjectStorageFile.Object[]
getSomeObjects
(int numObjects) Returns a given set of Objects that exist in the file.protected long[]
Returns a list of offsets for all the valid Objects in the file.boolean
isEmpty()
boolean
Returns whether this is a new storage file.protected void
markDeletedBytes
(long offset) protected abstract Object
objectFromBytes
(byte[] bytes) This method should return an Object, as created by the given bytes.protected abstract byte[]
objectToBytes
(Object obj) This method should return the Object in its byte[] format.void
open()
Opens a storage file.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[]
Rebuilds the storage file offsets from a existing table storage file.void
setPartition
(long partitionSize) protected void
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.toString()
Converts the storage file to String representation.void
toString
(OutputStream os) Writes the String representation of this storage file to the given OutputStream.long
Update a prior Object with a new Object.protected long
walkFile()
Walks the file to the next valid Object, and returns its offset.
-
Field Details
-
objectFile
RandomAccessFile that actually stores the Objects. -
OBJ_HEADER
public static final int OBJ_HEADER- See Also:
-
allocator
-
currentStorageSize
protected int currentStorageSize -
VALID_OBJ
protected static final int VALID_OBJ- See Also:
-
INVALID_OBJ
protected static final int INVALID_OBJ- See Also:
-
time_used
protected long time_used -
MAX_PARTITION_SIZE
protected static final long MAX_PARTITION_SIZE- See Also:
-
partitioned
protected boolean partitioned -
headers
protected byte[] headers -
invalid_flags
protected static final byte[] invalid_flags -
valid_flags
protected static final byte[] valid_flags -
headers_no_flag
protected byte[] headers_no_flag -
maxPartitionSize
protected long maxPartitionSize
-
-
Constructor Details
-
ObjectStorageFile
Constructor for the storage manager.- Parameters:
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?
-
-
Method Details
-
setPartition
public void setPartition(long partitionSize) -
getObjectFile
Returns the File object associated with this ObjectStorageFile.- Returns:
- File, representing this ObjectStorageFile
-
add
Adds a new Object to the table.- Parameters:
obj
- Add this Object to the table.- Returns:
- Offset at which this Object exists.
- Throws:
IOException
-
get
Get the Object at the given offset.- Parameters:
offset
- to retrieve the Object.- Returns:
- The desired Object.
- Throws:
IOException
-
remove
Remove the Object at the given offset.- Parameters:
offset
- Offset of the Object.bRetValue
- Whether it will return the removed object or not- Throws:
IOException
-
removeDelayedParsing
Same as remove(), except that objectFromBytes() is not called. This is exclusively for use by multi-threaded EP and AP.- Parameters:
offset
-bRetValue
-- Returns:
- The byte[] that should have been passed to objectFromBytes() to convert to original object with.
- Throws:
IOException
-
update
Update a prior Object with a new Object.- Parameters:
obj
- New Object.offset
- Offset of the old Object.- Returns:
- Offset of the new Object.
- Throws:
IOException
-
restoreFile
Rebuilds the storage file offsets from a existing table storage file.- Returns:
- An array of longs, representing the new offsets.
- Throws:
IOException
-
open
Opens a storage file.- Throws:
IOException
-
close
Closes a storage file.- Throws:
IOException
-
drop
public void drop() -
isNewFile
public boolean isNewFile()Returns whether this is a new storage file.- Returns:
- true, if this is using a new storage file
-
getNumObjects
Returns the number of Objects in the storage file.- Returns:
- Number of Objects in the storage file.
- Throws:
IOException
-
getValidOffsets
Returns a list of offsets for all the valid Objects in the file.- Returns:
- array of valid offsets
- Throws:
IOException
-
setupWalker
Sets up the file walker to start at the beginning of the file.- Throws:
IOException
-
setupWalker
Sets up the file walker to start at the given offset.- Throws:
IOException
-
walkFile
Walks the file to the next valid Object, and returns its offset.- Returns:
- offset of new valid Object, or -1 if no valid offset was found
- Throws:
IOException
-
getSomeObjects
Returns a given set of Objects that exist in the file. Subsequent calls to this method will eventually return all the Objects in the file. Note that getting ALL the Objects at once, and storing them in memory, may cause "out of memory" issues. Therefore, it is recommended that you deal with the Objects a little bit at a time. When using this method, be sure to call it until ALL of the Objects have been retrieved. The method will only reset its pointer to the top of the file once all of the current Objects have been retrieved.- Parameters:
numObjects
- Number of Objects to return.- Returns:
- Set of Objects from the file. Null, if no more Objects exist.
- Throws:
IOException
-
toString
Converts the storage file to String representation. -
toString
Writes the String representation of this storage file to the given OutputStream.- Parameters:
os
- OutputStream to write to.- Throws:
IOException
-
allocatorReport
-
addBytes
Adds a set of bytes to the file, and returns the offset where they are stored.- Parameters:
newBytes
- Bytes to store- Returns:
- offset of bytes
- Throws:
IOException
-
getBytes
Returns the bytes at the given offset.- Parameters:
offset
- offset to retrieve the bytes- Returns:
- bytes at the offset
- Throws:
IOException
-
markDeletedBytes
- Throws:
IOException
-
removeBytes
Removes the bytes at the given offset, and returns them.- Parameters:
offset
- offset of bytes to remove- Returns:
- bytes at offset
- Throws:
IOException
-
isEmpty
public boolean isEmpty() -
objectFromBytes
This method should return an Object, as created by the given bytes. If, for whatever reason, the Object could not be created from these bytes, this method should return null.- Parameters:
bytes
- byte[] used to create the Object- Returns:
- A valid Object, created from the bytes.
-
objectToBytes
This method should return the Object in its byte[] format.- Parameters:
obj
- Object to convert to bytes.- Returns:
- bytes of the Object
-