ehcache

net.sf.ehcache.store
Class DiskStore

java.lang.Object
  extended by net.sf.ehcache.store.DiskStore
All Implemented Interfaces:
Store

public class DiskStore
extends java.lang.Object
implements Store

A disk store implementation.

As of ehcache-1.2 (v1.41 of this file) DiskStore has been changed to a mix of finer grained locking using synchronized collections and synchronizing on the whole instance, as was the case with earlier versions.

Version:
$Id: DiskStore.java 155 2006-07-15 08:23:12Z gregluck $
Author:
Adam Murdoch, Greg Luck, patches contributed: Ben Houston

Nested Class Summary
(package private)  class DiskStore.MemoryEfficientByteArrayOutputStream
          This class is designed to minimse the number of System.arraycopy(); methods required to complete.
 
Field Summary
static java.lang.String AUTO_DISK_PATH_DIRECTORY_PREFIX
          If the CacheManager needs to resolve a conflict with the disk path, it will create a subdirectory in the given disk path with this prefix followed by a number.
 
Constructor Summary
DiskStore(Cache cache, java.lang.String diskPath)
          Creates a disk store.
 
Method Summary
 float calculateDataFileSparseness()
          The design of the layout on the data file means that there will be small gaps created when DiskElements are reused.
 boolean containsKey(java.lang.Object key)
          An unsynchronized and very low cost check to see if a key is in the Store.
 void dispose()
          Shuts down the disk store in preparation for cache shutdown

If a VM crash happens, the shutdown hook will not run.

 void flush()
          Flush the spool if persistent, so we don't lose any data.
 Element get(java.lang.Object key)
          Gets an Element from the Disk Store.
 java.lang.String getDataFileName()
           
 java.lang.String getDataFilePath()
           
 long getDataFileSize()
           
 java.lang.String getIndexFileName()
           
 long getIndexFileSize()
           
 java.lang.Object[] getKeyArray()
          Gets an Array of the keys for all elements in the disk store.
 Element getQuiet(java.lang.Object key)
          Gets an Element from the Disk Store, without updating statistics
 int getSize()
          Returns the current store size.
 Status getStatus()
          Returns the store status.
 long getTotalFileSize()
           
 long getUsedDataSize()
          When elements are deleted, spaces are left in the file.
 boolean isExpiryThreadAlive()
          The expiry thread is started provided the cache is not eternal

If started it will continue to run until the dispose() method is called, at which time it should be interrupted and then die.

 boolean isSpoolEmpty()
          Whether there are any elements waiting to be spooled to disk.
 void put(Element element)
          Puts an element into the disk store.
 Element remove(java.lang.Object key)
          Removes an item from the disk store.
 void removeAll()
          Remove all of the elements from the store.
 java.lang.String toString()
          Returns a String representation of the DiskStore
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

AUTO_DISK_PATH_DIRECTORY_PREFIX

public static final java.lang.String AUTO_DISK_PATH_DIRECTORY_PREFIX
If the CacheManager needs to resolve a conflict with the disk path, it will create a subdirectory in the given disk path with this prefix followed by a number. The presence of this name is used to determined whether it makes sense for a persistent DiskStore to be loaded. Loading persistent DiskStores will only have useful semantics where the diskStore path has not changed.

See Also:
Constant Field Values
Constructor Detail

DiskStore

public DiskStore(Cache cache,
                 java.lang.String diskPath)
Creates a disk store.

Parameters:
cache - the Cache that the store is part of
diskPath - the directory in which to create data and index files
Method Detail

get

public final Element get(java.lang.Object key)
Gets an Element from the Disk Store.

Specified by:
get in interface Store
Returns:
The element

containsKey

public final boolean containsKey(java.lang.Object key)
An unsynchronized and very low cost check to see if a key is in the Store. No check is made to see if the Element is expired.

Specified by:
containsKey in interface Store
Parameters:
key - The Element key
Returns:
true if found. If this method return false, it means that an Element with the given key is definitely not in the MemoryStore. If it returns true, there is an Element there. An attempt to get it may return null if the Element has expired.

getQuiet

public final Element getQuiet(java.lang.Object key)
Gets an Element from the Disk Store, without updating statistics

Returns:
The element

getKeyArray

public final java.lang.Object[] getKeyArray()
Gets an Array of the keys for all elements in the disk store.

Returns:
An Object[] of Serializable keys

getSize

public final int getSize()
Returns the current store size.

Specified by:
getSize in interface Store

getStatus

public final Status getStatus()
Returns the store status.

Specified by:
getStatus in interface Store

put

public final void put(Element element)
Puts an element into the disk store.

This method is not synchronized. It is however threadsafe. It uses fine-grained synchronization on the spool

Specified by:
put in interface Store

remove

public final Element remove(java.lang.Object key)
Removes an item from the disk store.

Specified by:
remove in interface Store

removeAll

public final void removeAll()
Remove all of the elements from the store.

If there are registered CacheEventListeners they are notified of the expiry or removal of the Element as each is removed.

Specified by:
removeAll in interface Store

dispose

public final void dispose()
Shuts down the disk store in preparation for cache shutdown

If a VM crash happens, the shutdown hook will not run. The data file and the index file will be out of synchronisation. At initialisation we always delete the index file after we have read the elements, so that it has a zero length. On a dirty restart, it still will have and the data file will automatically be deleted, thus preserving safety.

Specified by:
dispose in interface Store

flush

public final void flush()
                 throws java.io.IOException
Flush the spool if persistent, so we don't lose any data.

Throws:
java.io.IOException

isSpoolEmpty

public final boolean isSpoolEmpty()
Whether there are any elements waiting to be spooled to disk.

Returns:
false if there are elements waiting, otherwise true

toString

public final java.lang.String toString()
Returns a String representation of the DiskStore

Overrides:
toString in class java.lang.Object

getTotalFileSize

public final long getTotalFileSize()
Returns:
the total size of the data file and the index file, in bytes.

getDataFileSize

public final long getDataFileSize()
Returns:
the size of the data file in bytes.

calculateDataFileSparseness

public final float calculateDataFileSparseness()
The design of the layout on the data file means that there will be small gaps created when DiskElements are reused.

Returns:
the sparseness, measured as the percentage of space in the Data File not used for holding data

getUsedDataSize

public final long getUsedDataSize()
When elements are deleted, spaces are left in the file. These spaces are tracked and are reused when new elements need to be written.

This method indicates the actual size used for data, excluding holes. It can be compared with getDataFileSize() as a measure of fragmentation.


getIndexFileSize

public final long getIndexFileSize()
Returns:
the size of the index file, in bytes.

getDataFileName

public final java.lang.String getDataFileName()
Returns:
the file name of the data file where the disk store stores data, without any path information.

getDataFilePath

public final java.lang.String getDataFilePath()
Returns:
the disk path, which will be dependent on the operating system

getIndexFileName

public final java.lang.String getIndexFileName()
Returns:
the file name of the index file, which maintains a record of elements and their addresses on the data file, without any path information.

isExpiryThreadAlive

public final boolean isExpiryThreadAlive()
The expiry thread is started provided the cache is not eternal

If started it will continue to run until the dispose() method is called, at which time it should be interrupted and then die.

Returns:
true if an expiryThread was created and is still alive.

ehcache