|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmessif.buckets.storage.impl.DiskStorage<T>
T
- the class of objects stored in this storagepublic class DiskStorage<T>
Disk based storage.
The objects in this storage are stored in a file in the order
of insertion. The address is the position within the file.
Objects are serialized using the provided BinarySerializator
.
Field Summary | |
---|---|
protected boolean |
bufferDirect
Allocate the buffers for read/write operations as direct |
protected int |
bufferSize
Buffer sizes for read/write operations |
protected int |
deletedFragments
The number of deleted objects - the file space fragmentation is the ratio between this and objectCount |
protected java.io.File |
file
The file with data |
protected java.nio.channels.FileChannel |
fileChannel
The channel on the file with data |
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 long |
fileOccupation
The number of bytes currently stored in the file (excluding headers) |
protected static int |
FLAG_CLOSED
Header flag constant for indication whether the file was correctly closed |
protected java.lang.ref.WeakReference<BufferInputStream> |
inputStream
Stream for reading data |
protected long |
maximalLength
The maximal length of the file |
protected boolean |
modified
Flag whether the file is modified |
protected java.lang.Thread |
modifiedThread
Finalizer thread that writes a modified header |
protected int |
objectCount
The number of objects currently stored in the file |
protected FileChannelOutputStream |
outputStream
Stream for writing data |
protected BinarySerializator |
serializator
Serializator responsible for storing (and restoring) binary objects in the file |
protected long |
startPosition
The position in the file where this storage starts (the real data starts at startPosition + headerSize) |
protected java.lang.Class<? extends T> |
storedObjectsClass
Class of objects that the this storage works with |
Constructor Summary | |
---|---|
DiskStorage(java.lang.Class<? extends T> storedObjectsClass,
java.io.File file,
boolean readonly,
int bufferSize,
boolean bufferDirect,
boolean memoryMap,
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. |
Method Summary | ||
---|---|---|
boolean |
add(T object)
Adds the specified object to this instance. |
|
static
|
castToDiskStorage(java.lang.Class<E> storageObjectsClass,
java.lang.Object object)
Cast the provided object to DiskStorage with generics typing. |
|
protected boolean |
closeFileChannel()
Close the associated file channel if this storage is no longer references from any index. |
|
static
|
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. |
|
java.io.File |
getFile()
Returns the file where the data of this storage are stored. |
|
protected BufferInputStream |
getInputStream(long position)
Retrieves an input stream for this storage's file. |
|
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. |
|
Lock |
lock(boolean blocking)
Acquires a lock on this object. |
|
protected java.nio.channels.FileChannel |
openFileChannel(java.io.File file,
boolean readonly)
Opens the file channel on file and reads the header. |
|
protected BufferInputStream |
openInputStream(java.nio.channels.FileChannel fileChannel)
Create input stream on the specified channel. |
|
protected FileChannelOutputStream |
openOutputStream()
Create output stream over the current file channel. |
|
T |
read(long position)
Reads the object stored at the specified address in this storage. |
|
protected void |
readHeader(java.nio.channels.FileChannel fileChannel,
long position)
Read header information from the file. |
|
protected void |
reconstructHeader(java.nio.channels.FileChannel 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 . |
|
LongStorageSearch<T> |
search()
Returns a search for all objects in this index. |
|
|
search(IndexComparator<? super C,? super T> comparator,
C key)
Returns a search for objects in this index that have any of the specified keys. |
|
|
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. |
|
|
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 void |
writeHeader(java.nio.channels.FileChannel fileChannel,
long position,
int flags)
Write header information to the file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String FILENAME_PREFIX
public static final java.lang.String FILENAME_SUFFIX
protected static final int FLAG_CLOSED
protected final int bufferSize
protected final boolean bufferDirect
direct
protected transient int objectCount
protected transient long fileOccupation
protected transient int deletedFragments
protected final java.io.File file
protected final transient java.nio.channels.FileChannel fileChannel
protected final long startPosition
protected final long maximalLength
protected final BinarySerializator serializator
protected final java.lang.Class<? extends T> storedObjectsClass
protected transient java.lang.ref.WeakReference<BufferInputStream> inputStream
protected final transient FileChannelOutputStream outputStream
protected transient boolean modified
protected transient java.lang.Thread modifiedThread
Constructor Detail |
---|
public DiskStorage(java.lang.Class<? extends T> storedObjectsClass, java.io.File file, boolean readonly, int bufferSize, boolean bufferDirect, boolean memoryMap, 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)memoryMap
- flag whether to use memory-mapped I/OstartPosition
- the position in the file where this storage startsmaximalLength
- the maximal length of the fileserializator
- the object responsible for storing (and restoring) binary objects
java.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 bucket
java.io.IOException
- if there was an error opening the bucket fileMethod Detail |
---|
protected boolean closeFileChannel() throws java.io.IOException
java.io.IOException
- if there was a problem closing the file channelpublic void finalize() throws java.lang.Throwable
ModifiableIndex
finalize
in interface ModifiableIndex<T>
finalize
in interface Storage<T>
finalize
in class java.lang.Object
java.lang.Throwable
- if there was an error while cleaningpublic void destroy() throws java.lang.Throwable
ModifiableIndex
destroy
in interface ModifiableIndex<T>
destroy
in interface Storage<T>
java.lang.Throwable
- if there was an error while cleaningpublic 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 filesystem
java.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 generics 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 instance
DiskStorage
object
java.lang.ClassCastException
- if passed object
is not a DiskStorage
or the storage objects are incompatibleprotected void writeHeader(java.nio.channels.FileChannel 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 header
java.io.IOException
- if something goes wrong when working with the filesystemprotected void readHeader(java.nio.channels.FileChannel 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 from
java.io.IOException
- if something goes wrong when working with the filesystemprotected void reconstructHeader(java.nio.channels.FileChannel 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 from
java.io.IOException
- if something goes wrong when working with the filesystemprotected BufferInputStream openInputStream(java.nio.channels.FileChannel fileChannel) throws java.io.IOException
fileChannel
- the file channel
java.io.IOException
- if something goes wrong when working with the filesystemprotected BufferInputStream getInputStream(long position) throws java.io.IOException
position
- the position on which to set the input stream
java.io.IOException
- if something goes wrong when working with the filesystemprotected FileChannelOutputStream openOutputStream() throws java.io.IOException
java.io.IOException
- if something goes wrong when working with the filesystemprotected java.nio.channels.FileChannel openFileChannel(java.io.File file, boolean readonly) throws java.io.IOException
file
and reads the header.
file
- the file to open the channel onreadonly
- if true, the channel will be opened in read-only mode
java.io.IOException
- if something goes wrong when working with the filesystempublic Lock lock(boolean blocking) throws java.lang.IllegalStateException
Lockable
Lock
object is obtained and
the lock holds until the Lock.unlock()
method is called.
Otherwise, this method blocks until the lock can be acquired or
null is returned if a non-blocking call was required.
lock
in interface Lockable
blocking
- if true, this method will block until the lock is obtained
java.lang.IllegalStateException
- if the lock cannot be obtainedpublic java.lang.Class<? extends T> getStoredObjectsClass()
public java.io.File getFile()
public int size()
size
in interface Index<T>
public boolean isModified()
public 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 disk
java.io.IOException
- if there was an I/O errorpublic LongAddress<T> store(T object) throws BucketStorageException
Storage
store
in interface LongStorage<T>
store
in interface Storage<T>
object
- the object to store
BucketStorageException
- if there was an error writing the datapublic void remove(long position) throws BucketStorageException
LongStorage
remove
in interface LongStorage<T>
position
- the address of the object to remove
BucketStorageException
- 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 remove
BucketStorageException
- 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 read
BucketStorageException
- if there was an error reading the datapublic boolean add(T object) throws BucketStorageException
Addible
add
in interface Addible<T>
object
- the object to be added
BucketStorageException
- 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 for
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 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 returned
java.lang.IllegalStateException
- if there was an error initializing the search on this index
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |