T
- the class of objects stored in this storagepublic class DiskStorage<T> extends java.lang.Object implements LongStorageIndexed<T>, java.io.Serializable, TemporaryCloseable
BinarySerializator
.Modifier and Type | Field and Description |
---|---|
protected static float |
COMPACTING_FRAGMENTATION_RATIO
|
protected static int |
DEFAULT_ASYNC_THREADS
Default number of asynchronous threads
|
protected static int |
DEFAULT_BUFFER_SIZE
Default size of the reading buffer
|
static java.lang.String |
FILENAME_PREFIX
The prefix for auto-generated filenames
|
static java.lang.String |
FILENAME_SUFFIX
The suffix for auto-generated filenames
|
protected static int |
FLAG_CLOSED
Header flag constant for indication whether the file was correctly closed
|
protected static int |
headerSize
The size of the header - must match the
writeHeader(java.nio.channels.AsynchronousFileChannel, long, int) and readHeader(java.nio.channels.AsynchronousFileChannel, long) methods |
protected static java.util.logging.Logger |
log
Logger for the disk storage class
|
protected long |
startPosition
The position in the file where this storage starts (the real data starts at startPosition + headerSize)
|
Constructor and Description |
---|
DiskStorage(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 DiskStreamStorage instance.
|
DiskStorage(DiskStorage<? extends T> copyAttributesDiskStorage,
java.io.File file)
Creates a new DiskStreamStorage instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T object)
Adds the specified object to this instance.
|
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
deletedFragments field. |
static <E> DiskStorage<E> |
castToDiskStorage(java.lang.Class<E> storageObjectsClass,
java.lang.Object object)
Cast the provided object to
DiskStorage with generic typing. |
boolean |
closeTemporarilyIfIdle(boolean resetAccessCounter)
Release the resources associated with this instance.
|
protected void |
compactData()
Compacts the deleted fragments of the disk storage.
|
static <T> DiskStorage<T> |
create(java.lang.Class<T> storedObjectsClass,
java.util.Map<java.lang.String,java.lang.Object> parameters)
Creates a new disk storage.
|
void |
destroy()
Destroy this index.
|
void |
finalize()
Finalize this index.
|
void |
flush(boolean syncPhysical)
Flushes this storage and forces any buffered data to be written out.
|
protected int |
getDeletedFragments()
Returns the number of deleted fragments (gaps) in this storage as known by
deletedFragments . |
java.io.File |
getFile()
Returns the file where the data of this storage are stored.
|
protected java.nio.channels.AsynchronousFileChannel |
getFileChannel()
Returns the currently opened file channel.
|
protected long |
getFileOccupation(boolean excludeHeaderSize)
Returns the number of bytes currently stored in the file.
|
float |
getFragmentation()
Returns the file space fragmentation, i.e. the ratio between the free and occupied disk space.
|
protected AsynchronousFileChannelOutputStream |
getOutputStream()
Returns the current output stream.
|
protected BinarySerializator |
getSerializator()
Returns the serializator of the objects stored in this storage.
|
java.lang.Class<? extends T> |
getStoredObjectsClass()
Returns the class of objects that the this storage works with.
|
boolean |
isModified()
Returns true if the storage was modified since last open/flush.
|
protected AsynchronousFileChannelInputStream |
openInputStream()
Create input stream on the specified channel.
|
java.util.Iterator<T> |
read(long... positions)
Read multiple objects asynchronously stored at the specified addresses in this storage.
|
T |
read(long position)
Reads the object stored at the specified address in this storage.
|
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.
|
void |
remove(long position)
Removes the object stored at the specified address in this storage.
|
protected void |
remove(long position,
int objectSize)
Removes object with size
objectSize at position position . |
protected void |
removeDeletedFragment(long position)
Removes a deleted fragment on given position.
|
protected void |
returnInputStream(AsynchronousFileChannelInputStream stream)
Return the taken stream back into the waiting queue.
|
void |
rewrite(T object,
long position)
Stores given object to a given address where an object was already stored (typically the
same object was stored there and now it's being updated).
|
LongStorageSearch<T> |
search()
Returns a search for all objects in this index.
|
<C> LongStorageSearch<T> |
search(IndexComparator<? super C,? super T> comparator,
C key)
Returns a search for objects in this index that have any of the specified keys.
|
<C> LongStorageSearch<T> |
search(IndexComparator<? super C,? super T> comparator,
C from,
C to)
Returns a search for objects in this index that are within the specified key-range.
|
<C> LongStorageSearch<T> |
search(IndexComparator<? super C,? super T> comparator,
java.util.Collection<? extends C> keys)
Returns a search for objects in this index that have any of the specified keys.
|
int |
size()
Returns the number of objects stored in this storage.
|
LongAddress<T> |
store(T object)
Stores an object in this storage.
|
protected AsynchronousFileChannelInputStream |
takeInputStream(long position)
Retrieves an input stream for this storage's file.
|
protected void |
writeHeader(java.nio.channels.AsynchronousFileChannel fileChannel,
long position,
int flags)
Write header information to the file.
|
protected static final java.util.logging.Logger log
public static final java.lang.String FILENAME_PREFIX
public static final java.lang.String FILENAME_SUFFIX
protected static final float COMPACTING_FRAGMENTATION_RATIO
protected static final int FLAG_CLOSED
protected static final int DEFAULT_ASYNC_THREADS
protected static final int DEFAULT_BUFFER_SIZE
protected final long startPosition
protected static final int headerSize
writeHeader(java.nio.channels.AsynchronousFileChannel, long, int)
and readHeader(java.nio.channels.AsynchronousFileChannel, long)
methodspublic DiskStorage(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
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 DiskStorage(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 void finalize() throws java.lang.Throwable
ModifiableIndex
public void destroy() throws java.lang.Throwable
ModifiableIndex
public static <T> DiskStorage<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.)public static <E> DiskStorage<E> castToDiskStorage(java.lang.Class<E> storageObjectsClass, java.lang.Object object) throws java.lang.ClassCastException
DiskStorage
with generic typing.
The objects stored in the storage must be of the same type as the storageObjectsClass
.E
- the class of objects stored in the storagestorageObjectsClass
- the class of objects stored in the storageobject
- the storage instanceDiskStorage
objectjava.lang.ClassCastException
- if passed object
is not a DiskStorage
or the storage objects are incompatibleprotected void writeHeader(java.nio.channels.AsynchronousFileChannel fileChannel, long position, int flags) throws java.io.IOException
objectCount
, fileOccupation
and deletedFragments
are stored. The write is two-phase, with synchronized flag for opened/closed file.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 readHeader(java.nio.channels.AsynchronousFileChannel fileChannel, long position) throws java.io.IOException
objectCount
, fileOccupation
and deletedFragments
variables are replaced by the values from the header.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 reconstructHeader(java.nio.channels.AsynchronousFileChannel fileChannel, long position) throws java.io.IOException
objectCount
, fileOccupation
and deletedFragments
variables are replaced by the reconstructed values.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 compactData() throws java.io.IOException
java.io.IOException
- if there was a problem with reading or writing the datapublic float getFragmentation()
protected AsynchronousFileChannelInputStream openInputStream() throws java.io.IOException
java.io.IOException
- if something goes wrong when working with the filesystemprotected AsynchronousFileChannelInputStream takeInputStream(long position) throws java.io.IOException
returnInputStream(messif.objects.nio.AsynchronousFileChannelInputStream)
.position
- the position on which to set the input streamjava.io.IOException
- if something goes wrong when working with the filesystemprotected void returnInputStream(AsynchronousFileChannelInputStream stream)
stream
- the stream to returntakeInputStream(long)
protected final AsynchronousFileChannelOutputStream getOutputStream() throws java.io.IOException
java.io.IOException
- if there was an I/O error opening the output streamprotected final java.nio.channels.AsynchronousFileChannel getFileChannel() throws java.io.IOException
file
is opened and the header read.java.io.IOException
- if something goes wrong when working with the filesystempublic void flush(boolean syncPhysical) throws java.io.IOException
syncPhysical
- if true then also the file is flushed
to be sure the data are really written to diskjava.io.IOException
- if there was an I/O errorpublic boolean closeTemporarilyIfIdle(boolean resetAccessCounter) throws java.io.IOException
TemporaryCloseable
closeTemporarilyIfIdle
in interface TemporaryCloseable
resetAccessCounter
- flag whether to reset the access counterjava.io.IOException
- if there was a problem closing the instance resourcespublic java.lang.Class<? extends T> getStoredObjectsClass()
public java.io.File getFile()
protected BinarySerializator getSerializator()
protected long getFileOccupation(boolean excludeHeaderSize)
excludeHeaderSize
- flag whether the returned number of bytes includes the size of the header (false) or not (true)protected int getDeletedFragments()
deletedFragments
.protected void addDeletedFragment(long position, int size)
deletedFragments
field.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)
position
- position in the file of the deleted fragment (gap)public int size()
public boolean isModified()
public LongAddress<T> store(T object) throws BucketStorageException
Storage
store
in interface LongStorage<T>
store
in interface Storage<T>
object
- the object to storeBucketStorageException
- if there was an error writing the datapublic void rewrite(T object, long position) throws BucketStorageException, java.lang.IllegalArgumentException
LongStorage
rewrite
in interface LongStorage<T>
object
- the object to storeposition
- the address where to store the objectBucketStorageException
- if there was an error reading the datajava.lang.IllegalArgumentException
- if the binary size of the passed object differs from the space in the storagepublic void remove(long position) throws BucketStorageException
LongStorage
remove
in interface LongStorage<T>
position
- the address of the object to removeBucketStorageException
- if there was an error deleting an objectprotected void remove(long position, int objectSize) throws BucketStorageException
objectSize
at position position
.position
- the absolute position in the fileobjectSize
- the number of bytes to removeBucketStorageException
- if there was an error writing to the filepublic T read(long position) throws BucketStorageException
LongStorage
read
in interface LongStorage<T>
position
- the address of the object to readBucketStorageException
- if there was an error reading the datapublic java.util.Iterator<T> read(long... positions)
IllegalStateException
exception with the encapsulated error.positions
- the addresses of the objects to readpublic boolean add(T object) throws BucketStorageException
Addible
add
in interface Addible<T>
object
- the object to be addedBucketStorageException
- if there was an error adding the objectpublic LongStorageSearch<T> search() throws java.lang.IllegalStateException
Index
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface LongStorageIndexed<T>
search
in interface StorageIndexed<T>
java.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic <C> LongStorageSearch<T> search(IndexComparator<? super C,? super T> comparator, java.util.Collection<? extends C> keys) throws java.lang.IllegalStateException
Index
key
need not necessarily be of the same class as the objects stored
in this index and also consistency with equals
is not required.
Note that objects are not necessarily returned in the order defined by the comparator
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface LongStorageIndexed<T>
search
in interface StorageIndexed<T>
C
- the type of the keys used by the searchcomparator
- compares the keys
with the stored objectskeys
- the keys to search for (at least one key must be given)java.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic <C> LongStorageSearch<T> search(IndexComparator<? super C,? super T> comparator, C key) throws java.lang.IllegalStateException
Index
key
need not necessarily be of the same class as the objects stored
in this index and also consistency with equals
is not required.
Note that objects are not necessarily returned in the order defined by the comparator
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface LongStorageIndexed<T>
search
in interface StorageIndexed<T>
C
- the type of the key used by the searchcomparator
- compares the key
with the stored objectskey
- the key to search forjava.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic <C> LongStorageSearch<T> search(IndexComparator<? super C,? super T> comparator, C from, C to) throws java.lang.IllegalStateException
Index
[from, to]
need not necessarily be of the same
class as the objects stored in this index, however, the comparator must be
able to compare the boundaries and the internal objects.
Note that objects are not returned in the order defined by the comparator
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface LongStorageIndexed<T>
search
in interface StorageIndexed<T>
C
- the type the boundaries used by the searchcomparator
- compares the boundaries [from, to]
with the stored objectsfrom
- the lower bound on the searched objects, i.e. objects greater or equal are returnedto
- the upper bound on the searched objects, i.e. objects smaller or equal are returnedjava.lang.IllegalStateException
- if there was an error initializing the search on this index