ehcache

net.sf.ehcache.store.compound.factories
Class DiskPersistentStorageFactory

java.lang.Object
  extended by net.sf.ehcache.store.compound.factories.DiskPersistentStorageFactory
All Implemented Interfaces:
ElementSubstituteFactory<ElementSubstitute>

public class DiskPersistentStorageFactory
extends Object

This will be the disk-persistent element substitute factory

Author:
Chris Dennis

Field Summary
protected static String AUTO_DISK_PATH_DIRECTORY_PREFIX
          Path stub used to create unique ehcache directories.
protected  CompoundStore store
          The store bound to this factory.
 
Constructor Summary
DiskPersistentStorageFactory(Ehcache cache, String diskPath)
          Constructs an disk persistent factory for the given cache and disk path.
 
Method Summary
 void bind(CompoundStore store)
          Bind a store instance to this factory.
 boolean bufferFull()
          Return true if the disk write queue is full.
 ElementSubstitute create(Object key, Element element)
          Creates a substitute for the supplied Element
 boolean created(Object object)
          Returns true if this factory created the given object.
protected  net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker createMarker(long position, int size, Element element)
          Create a disk marker representing the given element, and area on disk.
protected  void delete()
          Deletes the data file for this factory.
 void expireElements()
          Remove elements created by this factory if they have expired.
 Future<Void> flush()
          Schedule a flush (index write) for this factory.
protected  void free(net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker marker)
          Free the given marker to be used by a subsequent write.
 void free(Lock exclusion, ElementSubstitute object)
          Free any manually managed resources used by this ElementSubstitute.
 File getDataFile()
          Return a reference to the data file backing this factory.
 File getIndexFile()
          Return the index file for this store.
 Policy getInMemoryEvictionPolicy()
          Return the in-memory eviction policy used by this store.
 int getInMemorySize()
          Return the number of in-memory elements
 long getInMemorySizeInBytes()
          Return the approximate serialized size of the in-memory elements
 int getOnDiskSize()
          Return the number of on-disk elements
 long getOnDiskSizeInBytes()
          Return this size in bytes of this factory
 boolean isInMemory(Object object)
          Return true if the given element is in memory
 boolean isOnDisk(Object object)
          Return true if the given element is on disk
protected  void markUsed(net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker marker)
          Mark this on-disk marker as used (hooks into the file space allocation structure).
protected  Element read(net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker marker)
          Read the data at the given marker, and return the associated deserialized Element.
 Element retrieve(Object key, ElementSubstitute object)
          Decodes the supplied ElementSubstitute.
protected
<U> Future<U>
schedule(Callable<U> call)
          Schedule to given task on the disk writer executor service.
 void setInMemoryCapacity(int capacity)
          Set the maximum in-memory capacity for this factory.
 void setInMemoryEvictionPolicy(Policy policy)
          Set the in-memory eviction policy to be used by this store.
 void setOnDiskCapacity(int capacity)
          Set the maximum on-disk capacity for this factory.
protected  void shrinkDataFile()
          Shrink this store's data file down to a minimal size for its contents.
protected  void shutdown()
          Shuts down this disk factory.
 void unbind(CompoundStore store)
          Unbinds a store instance from this factory
protected  net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker write(Element element)
          Write the given element to disk, and return the associated marker.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTO_DISK_PATH_DIRECTORY_PREFIX

protected static final String AUTO_DISK_PATH_DIRECTORY_PREFIX
Path stub used to create unique ehcache directories.

See Also:
Constant Field Values

store

protected volatile CompoundStore store
The store bound to this factory.

Constructor Detail

DiskPersistentStorageFactory

public DiskPersistentStorageFactory(Ehcache cache,
                                    String diskPath)
Constructs an disk persistent factory for the given cache and disk path.

Parameters:
cache - cache that fronts this factory
diskPath - path to store data in
Method Detail

create

public ElementSubstitute create(Object key,
                                Element element)
                         throws IllegalArgumentException
Creates a substitute for the supplied Element

In the case that this element is no longer mapped to a key - if for example the element is being decoded following a removal - then the supplied key will be null.

Parameters:
key - key to which this element is mapped
element - Element to encode
Returns:
The substitute element
Throws:
IllegalArgumentException - if element cannot be substituted

free

public void free(Lock exclusion,
                 ElementSubstitute object)
Free any manually managed resources used by this ElementSubstitute.

Specified by:
free in interface ElementSubstituteFactory<ElementSubstitute>
object - ElementSubstitute being free'd.

retrieve

public Element retrieve(Object key,
                        ElementSubstitute object)
Decodes the supplied ElementSubstitute.

Parameters:
key - key to which this element is mapped
object - ElementSubstitute to decode
Returns:
a decoded Element

created

public boolean created(Object object)
Returns true if this factory created the given object.

Parameters:
object - object to check
Returns:
true if object created by this factory

bind

public void bind(CompoundStore store)
Bind a store instance to this factory.

Parameters:
store - store to bind

unbind

public void unbind(CompoundStore store)
Unbinds a store instance from this factory

Parameters:
store - store to unbind

flush

public Future<Void> flush()
Schedule a flush (index write) for this factory.


createMarker

protected net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker createMarker(long position,
                                                                                             int size,
                                                                                             Element element)
Create a disk marker representing the given element, and area on disk.

This method can be overridden by subclasses to use different marker types.

Parameters:
position - starting disk offset
size - size of in disk area
element - element to be written to area
Returns:
marker representing the element.

isInMemory

public boolean isInMemory(Object object)
Return true if the given element is in memory


isOnDisk

public boolean isOnDisk(Object object)
Return true if the given element is on disk


getInMemorySize

public int getInMemorySize()
Return the number of in-memory elements


getInMemorySizeInBytes

public long getInMemorySizeInBytes()
Return the approximate serialized size of the in-memory elements


getOnDiskSize

public int getOnDiskSize()
Return the number of on-disk elements


setInMemoryCapacity

public void setInMemoryCapacity(int capacity)
Set the maximum in-memory capacity for this factory.


setOnDiskCapacity

public void setOnDiskCapacity(int capacity)
Set the maximum on-disk capacity for this factory.


setInMemoryEvictionPolicy

public void setInMemoryEvictionPolicy(Policy policy)
Set the in-memory eviction policy to be used by this store.


getInMemoryEvictionPolicy

public Policy getInMemoryEvictionPolicy()
Return the in-memory eviction policy used by this store.


getIndexFile

public File getIndexFile()
Return the index file for this store.


getOnDiskSizeInBytes

public long getOnDiskSizeInBytes()
Return this size in bytes of this factory


markUsed

protected void markUsed(net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker marker)
Mark this on-disk marker as used (hooks into the file space allocation structure).


shrinkDataFile

protected void shrinkDataFile()
Shrink this store's data file down to a minimal size for its contents.


shutdown

protected void shutdown()
                 throws IOException
Shuts down this disk factory.

This shuts down the executor and then waits for its termination, before closing the data file.

Throws:
IOException

delete

protected void delete()
Deletes the data file for this factory.


schedule

protected <U> Future<U> schedule(Callable<U> call)
Schedule to given task on the disk writer executor service.

Type Parameters:
U - return type of the callable
Parameters:
call - callable to call
Returns:
Future representing the return of this call

read

protected Element read(net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker marker)
                throws IOException,
                       ClassNotFoundException
Read the data at the given marker, and return the associated deserialized Element.

Parameters:
marker - marker to read
Returns:
deserialized Element
Throws:
IOException - on read error
ClassNotFoundException - on deserialization error

write

protected net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker write(Element element)
                                                                               throws IOException
Write the given element to disk, and return the associated marker.

Parameters:
element - to write
Returns:
marker representing the element
Throws:
IOException - on write error

free

protected void free(net.sf.ehcache.store.compound.factories.DiskStorageFactory.DiskMarker marker)
Free the given marker to be used by a subsequent write.

Parameters:
marker - marker to be free'd

bufferFull

public boolean bufferFull()
Return true if the disk write queue is full.


getDataFile

public File getDataFile()
Return a reference to the data file backing this factory.


expireElements

public void expireElements()
Remove elements created by this factory if they have expired.


ehcache

true