T
- the class of objects stored in this storagepublic class DiskStorageFillGaps<T> extends DiskStorage<T>
DiskStorage
.
The only difference is that this storage assumes storing objects of the same sizes (or several
specific fixed sizes) and thus, when storing new object to storage with "gaps" (deleted objects)
the storage tries to find a gap with exactly the same size as the object to be inserted. The
process of finding the gap may take a while since the storage must iterate over all stored
objects.COMPACTING_FRAGMENTATION_RATIO, DEFAULT_ASYNC_THREADS, DEFAULT_BUFFER_SIZE, FILENAME_PREFIX, FILENAME_SUFFIX, FLAG_CLOSED, headerSize, log, startPosition
Constructor and Description |
---|
DiskStorageFillGaps(java.lang.Class<? extends T> storedObjectsClass,
java.io.File file,
boolean readonly,
int bufferSize,
boolean bufferDirect,
int asyncThreads,
long startPosition,
long maximalLength,
BinarySerializator serializator)
Creates a new
DiskStorageFillGaps instance. |
DiskStorageFillGaps(DiskStorage<? extends T> copyAttributesDiskStorage,
java.io.File file)
Creates a new DiskStreamStorage instance.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addDeletedFragment(long position,
int size)
Adds a new deleted fragment at given position of given size;
this should be the only method to increase the
DiskStorage.deletedFragments field. |
protected void |
compactData()
Compacts the deleted fragments of the disk storage.
|
static <T> DiskStorageFillGaps<T> |
create(java.lang.Class<T> storedObjectsClass,
java.util.Map<java.lang.String,java.lang.Object> parameters)
Creates a new disk storage which fills the gaps.
|
protected java.lang.Long |
getPositionToStore(T object)
This method either finds a position in the middle of the file where is space to store given
object, or it returns null.
|
protected void |
readHeader(java.nio.channels.AsynchronousFileChannel fileChannel,
long position)
Read header information from the file.
|
protected void |
reconstructHeader(java.nio.channels.AsynchronousFileChannel fileChannel,
long position)
Reconstruct header information by seeking through the whole file.
|
protected void |
removeDeletedFragment(long position)
Removes a deleted fragment on given position.
|
LongAddress<T> |
store(T object)
Stores an object in this storage.
|
protected void |
writeHeader(java.nio.channels.AsynchronousFileChannel fileChannel,
long position,
int flags)
Write header information to the file.
|
add, castToDiskStorage, closeTemporarilyIfIdle, destroy, finalize, flush, getDeletedFragments, getFile, getFileChannel, getFileOccupation, getFragmentation, getOutputStream, getSerializator, getStoredObjectsClass, isModified, openInputStream, read, read, remove, remove, returnInputStream, rewrite, search, search, search, search, size, takeInputStream
public DiskStorageFillGaps(java.lang.Class<? extends T> storedObjectsClass, java.io.File file, boolean readonly, int bufferSize, boolean bufferDirect, int asyncThreads, long startPosition, long maximalLength, BinarySerializator serializator) throws java.io.IOException
DiskStorageFillGaps
instance.storedObjectsClass
- the class of objects that the new storage will work withfile
- the file in which to create the bucketreadonly
- if true, the storage will be opened in read-only mode (e.g. the store method will throw an exception)bufferSize
- the size of the buffer used for reading/writingbufferDirect
- the bucket is either direct (true) or array-backed (false)asyncThreads
- the maximal number of threads to use (for asynchronous reading)startPosition
- the position in the file where this storage startsmaximalLength
- the maximal length of the fileserializator
- the object responsible for storing (and restoring) binary objectsjava.io.IOException
- if there was an error opening the bucket filepublic DiskStorageFillGaps(DiskStorage<? extends T> copyAttributesDiskStorage, java.io.File file) throws java.io.IOException
copyAttributesDiskStorage
except for the file name. The bucket is always opened in read-write mode.copyAttributesDiskStorage
- the disk storage from which to copy parametersfile
- the file in which to create the bucketjava.io.IOException
- if there was an error opening the bucket filepublic static <T> DiskStorageFillGaps<T> create(java.lang.Class<T> storedObjectsClass, java.util.Map<java.lang.String,java.lang.Object> parameters) throws java.io.IOException, java.lang.InstantiationException
T
- the class of objects that the new storage will work withstoredObjectsClass
- the class of objects that the new storage will work withparameters
- list of named parameters (see above)java.io.IOException
- if something goes wrong when working with the filesystemjava.lang.InstantiationException
- if the parameters specified are invalid (non existent directory, null values, etc.)protected void readHeader(java.nio.channels.AsynchronousFileChannel fileChannel, long position) throws java.io.IOException
DiskStorage
DiskStorage.objectCount
, DiskStorage.fileOccupation
and DiskStorage.deletedFragments
variables are replaced by the values from the header.readHeader
in class DiskStorage<T>
fileChannel
- the file channel to read the header fromposition
- the position in the file channel to read the header fromjava.io.IOException
- if something goes wrong when working with the filesystemprotected void writeHeader(java.nio.channels.AsynchronousFileChannel fileChannel, long position, int flags) throws java.io.IOException
DiskStorage
DiskStorage.objectCount
, DiskStorage.fileOccupation
and DiskStorage.deletedFragments
are stored. The write is two-phase, with synchronized flag for opened/closed file.writeHeader
in class DiskStorage<T>
fileChannel
- the file channel to write the header toposition
- the position in the file channel to write the header toflags
- the flags accompanied with the bucket;
currently only "opened/closed" flag is used to resolve validity of the headerjava.io.IOException
- if something goes wrong when working with the file systemprotected void reconstructHeader(java.nio.channels.AsynchronousFileChannel fileChannel, long position) throws java.io.IOException
DiskStorage
DiskStorage.objectCount
, DiskStorage.fileOccupation
and DiskStorage.deletedFragments
variables are replaced by the reconstructed values.reconstructHeader
in class DiskStorage<T>
fileChannel
- the file channel to read the header fromposition
- the position in the file channel to read the header fromjava.io.IOException
- if something goes wrong when working with the filesystemprotected void addDeletedFragment(long position, int size)
DiskStorage
DiskStorage.deletedFragments
field.addDeletedFragment
in class DiskStorage<T>
position
- position in the file of the deleted fragment (gap)size
- size of the gap (INCLUDING the 4 bytes of an integer to mark size of the fragment)protected void removeDeletedFragment(long position)
DiskStorage
removeDeletedFragment
in class DiskStorage<T>
position
- position in the file of the deleted fragment (gap)protected java.lang.Long getPositionToStore(T object)
object
- object to find a spot forpublic LongAddress<T> store(T object) throws BucketStorageException
Storage
store
in interface LongStorage<T>
store
in interface Storage<T>
store
in class DiskStorage<T>
object
- the object to storeBucketStorageException
- if there was an error writing the dataprotected void compactData() throws java.io.IOException
DiskStorage
compactData
in class DiskStorage<T>
java.io.IOException
- if there was a problem with reading or writing the data