ehcache

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

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

public class DiskOverflowStorageFactory
extends Object

A factory that stores elements on disk in their serialized form.

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
DiskOverflowStorageFactory(Ehcache cache, String diskPath)
          Constructs an overflow 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)
          Encodes an Element as a marker to on-disk location.
 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.
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 lock, ElementSubstitute substitute)
          Free any manually managed resources used by this ElementSubstitute.
 File getDataFile()
          Return a reference to the data file backing this factory.
 long getOnDiskSizeInBytes()
          Return this size in bytes of this factory
 int getSize()
          Return the count of elements created by this factory.
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).
 void primary(CapacityLimitedInMemoryFactory memory)
          Sets the primary factory that this factory should fault to, when elements are retrieved.
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 proxy)
          Decode an ElementProxy from an on disk marker (or a pending placeholder).
protected
<U> Future<U>
schedule(Callable<U> call)
          Schedule to given task on the disk writer executor service.
 void setCapacity(int newCapacity)
          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 localStore)
          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

DiskOverflowStorageFactory

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

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

primary

public void primary(CapacityLimitedInMemoryFactory memory)
Sets the primary factory that this factory should fault to, when elements are retrieved.


create

public ElementSubstitute create(Object key,
                                Element element)
                         throws IllegalArgumentException
Encodes an Element as a marker to on-disk location.

Immediately substitutes a placeholder for the original element while the Element itself is asynchronously written to disk using the executor service.

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

retrieve

public Element retrieve(Object key,
                        ElementSubstitute proxy)
Decode an ElementProxy from an on disk marker (or a pending placeholder).

This implementation makes no attempt to fault in the decoded Element in place of the proxy.

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

free

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

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

unbind

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

Parameters:
localStore - store to unbind

getSize

public int getSize()
Return the count of elements created by this factory.


setCapacity

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


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

getOnDiskSizeInBytes

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


bind

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

Parameters:
store - store to bind

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

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.

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