ehcache

net.sf.ehcache.store
Class DiskStore

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

public class DiskStore
extends java.lang.Object
implements Store

A disk cache implementation.

Version:
$Id: DiskStore.java,v 1.25 2004/12/02 07:35:38 gregluck Exp $
Author:
Adam Murdoch, Greg Luck

Field Summary
 
Fields inherited from interface net.sf.ehcache.store.Store
CACHE_HUB, DISK_CACHE, STATUS_ALIVE, STATUS_DISPOSED, STATUS_ERROR, STATUS_UNINITIALISED
 
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.
 void dispose()
          Shuts down the disk store in preparation for cache shutdown

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

 Element get(java.io.Serializable key)
          Gets an Element from the Disk Store.
 int getCacheType()
          Returns the store type.
 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.
 java.lang.String getName()
          Returns the cache name this disk cache is spooling for
 Element getQuiet(java.io.Serializable key)
          Gets an Element from the Disk Store, without updating statistics
 int getSize()
          Returns the current store size.
 int getStatus()
          Returns the cache 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 entry)
          Puts an item into the cache.
 boolean remove(java.io.Serializable key)
          Removes an item from the cache.
 void removeAll()
          Removes all cached items from the cache.
 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
 

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

getCacheType

public int getCacheType()
Returns the store type.

Specified by:
getCacheType in interface Store
Returns:
The cacheType value

getName

public java.lang.String getName()
Returns the cache name this disk cache is spooling for

Specified by:
getName in interface Store

getStatus

public int getStatus()
Returns the cache status.

Specified by:
getStatus in interface Store

get

public Element get(java.io.Serializable key)
            throws java.io.IOException
Gets an Element from the Disk Store.

Specified by:
get in interface Store
Returns:
The element
Throws:
java.io.IOException

getQuiet

public Element getQuiet(java.io.Serializable key)
                 throws java.io.IOException
Gets an Element from the Disk Store, without updating statistics

Returns:
The element
Throws:
java.io.IOException

getKeyArray

public 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 int getSize()
Returns the current store size.

Specified by:
getSize in interface Store

put

public void put(Element entry)
         throws java.io.IOException
Puts an item into the cache.

Specified by:
put in interface Store
Throws:
java.io.IOException

remove

public boolean remove(java.io.Serializable key)
               throws java.io.IOException
Removes an item from the cache.

Specified by:
remove in interface Store
Throws:
java.io.IOException

removeAll

public void removeAll()
               throws java.io.IOException
Removes all cached items from the cache.

Specified by:
removeAll in interface Store
Throws:
java.io.IOException

dispose

public 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

isSpoolEmpty

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

Returns:
false if there are elements waiting, otherwise true

toString

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


getTotalFileSize

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

getDataFileSize

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

calculateDataFileSparseness

public 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 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 long getIndexFileSize()
Returns:
the size of the index file, in bytes.

getDataFileName

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

getDataFilePath

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

getIndexFileName

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