Persistences - File (Abstract)

Basics

If your want to persist your values (V) to a file, you will have to provide the means to load/save values and restore the cache.

An abstract class is provided with default implementation of the generic methods AbstractFileValueManager<K,V>.

It need two parameters:

Attribute Description
CachingDirectory Where shall the file holding the Value be stored, and from where shall the map try to restore the cache after a "reboot".
You can choose to add a SubDirectoryStrategy to the caching directory. Which can create sub directories to store a file in, but only if recursive is true.
Important Note: This implementation relies on the the filename is unique by it self, not that the sub directory structure is part of the key.

PersistencesRestoreMethod How to restore the the key/values after a "reboot". See Persistences

ValueWrapper

The store file value is wrapped inside a DefaultFileValueWrapper<K,V>.

Setting expiration datetime

The expiration time is set on the file using - getFile().setLastModified(expireDateTime.getTime());

Generic considerations

  • How do we store the Value(V) in a file(F) ?
  • How do we retreive the Value(V) from a file(F) ?
  • How do map that the Key(K) is store in the file(F) containing the value(V) ?

Current abstract key handling implementations

Type Key Type Value Type Filename Super class Description
Reference File.getName() File The string key value. DefaultFilenameFileReferenceValueManager A special case for file persistences.
This only stores file references, to already existing files.
The key is the name of the file, the value is the file it self.
Not persistences of the content of the file is performed,
but the files is remove when a restore, cleanup, removeAll or remove is executed.

Filename String V The string key value. AbstractFilenameFileValueManager<V> The key is always of type String, and is used as
the filename in the caching directory.
The downside is, that this limits the key type a String.

Indexed K extends Serializable V Random UUID AbstractIndexedFileValueManager
<K extends Serializable,V>
The key and the filename is serialized to a index file in the caching directory,
which holds the map between key and file.
The downside is, that this index file will very often be written to the directory,
a give overhead to the cache.
Think hard what a good "indexSaveInterval" is, and test it.

Keyed K extends Keyed<K> V Random UUID AbstractKeyedFileValueManagerr
<K,V extends Keyed<K>>
The value implements the Keyed interface, so from the value we can get the key.
The downside is, to restore the cache an restablish a key to file map,
we have to load all values.

Current value handling implementations

Currently there is support for saving/loading the value as xml using JAXB or serializable the the file.
Type Persisted as Instance class Description
Filename Serialized DefaultFilenameSerializableFileValueManager
<V extends Serializable>
Serializes the value to a file where the filename is the key string value.
The value V must implement Serializable.

Filename XML DefaultFilenameXmlFileValueManager<V> Load and save the file as XML using JAXB where the filename is the key string value.
The value V must have the annotation "@XmlRootElement".

Indexed Serialized DefaultIndexedSerializableFileValueManager
<K extends Serializable,V extends Serializable>
Serializes the value to a file where the filename is a random UUID,
the key to file map is stored in a seperate index file which is also persisted frequently.
Both the key (K) and value (V) must extend Serializable.
Think hard what a good "indexSaveInterval" is, and test it.

Indexed XML DefaultIndexedXmlFileValueManager
<K extends Serializable,V>
Load and save the file as XML using JAXB where the filename is a random UUID,
the key to file map is stored in a seperate index file which is also persisted frequently.
The value V must have the annotation "@XmlRootElement".
The key (K) and value (V) must extend Serializable.
Think hard what a good "indexSaveInterval" is, and test it.

Keyed Serialized DefaultKeyedSerializableFileValueManager
<K extends Serializable,V extends Keyed<K> & Serializable>
Serializes the value to a file where the filename is a random UUID,
the key stored inside the value using the Keyed interface.
Both the key (K) and value (V) must extend Serializable.
The value (V) must also extend the Keyed interface.

Keyed XML DefaultKeyedXmlFileValueManager
<K extends Serializable,V extends Keyed<K>>
Load and save the file as XML using JAXB where the filename is a random UUID,
the key stored inside the value using the Keyed interface.
The value V must have the annotation "@XmlRootElement".
The key (K) must extend Serializable.