net.sf.ehcache.store

Class DiskStore

public class DiskStore extends 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
classDiskStore.MemoryEfficientByteArrayOutputStream
This class is designed to minimse the number of System.arraycopy(); methods required to complete.
Field Summary
static StringAUTO_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, String diskPath)
Creates a disk store.
Method Summary
floatcalculateDataFileSparseness()
The design of the layout on the data file means that there will be small gaps created when DiskElements are reused.
booleancontainsKey(Object key)
An unsynchronized and very low cost check to see if a key is in the Store.
voiddispose()
Shuts down the disk store in preparation for cache shutdown

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

voidflush()
Flush the spool if persistent, so we don't lose any data.
Elementget(Object key)
Gets an Element from the Disk Store.
StringgetDataFileName()
StringgetDataFilePath()
longgetDataFileSize()
StringgetIndexFileName()
longgetIndexFileSize()
Object[]getKeyArray()
Gets an Array of the keys for all elements in the disk store.
ElementgetQuiet(Object key)
Gets an Element from the Disk Store, without updating statistics
intgetSize()
Returns the current store size.
StatusgetStatus()
Returns the store status.
longgetTotalFileSize()
longgetUsedDataSize()
When elements are deleted, spaces are left in the file.
booleanisExpiryThreadAlive()
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.

booleanisSpoolEmpty()
Whether there are any elements waiting to be spooled to disk.
voidput(Element element)
Puts an element into the disk store.
Elementremove(Object key)
Removes an item from the disk store.
voidremoveAll()
Remove all of the elements from the store.
StringtoString()
Returns a String representation of the

Field Detail

AUTO_DISK_PATH_DIRECTORY_PREFIX

public static final 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.

Constructor Detail

DiskStore

public DiskStore(Cache cache, 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

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

containsKey

public final boolean containsKey(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.

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.

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.

flush

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

Throws: IOException

get

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

Returns: The element

getDataFileName

public final String getDataFileName()

Returns: the file name of the data file where the disk store stores data, without any path information.

getDataFilePath

public final String getDataFilePath()

Returns: the disk path, which will be dependent on the operating system

getDataFileSize

public final long getDataFileSize()

Returns: the size of the data file in bytes.

getIndexFileName

public final 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.

getIndexFileSize

public final long getIndexFileSize()

Returns: the size of the index file, in bytes.

getKeyArray

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

Returns: An Object[] of Serializable keys

UNKNOWN: SynchronizeOnNonFinalField

getQuiet

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

Returns: The element

getSize

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

UNKNOWN: SynchronizeOnNonFinalField

getStatus

public final Status getStatus()
Returns the store status.

getTotalFileSize

public final long getTotalFileSize()

Returns: the total size of the data file and the index file, in bytes.

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.

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.

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

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

remove

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

UNKNOWN: SynchronizeOnNonFinalField

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.

toString

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