org.apache.jcs.auxiliary.disk.indexed
Class IndexedDiskCache

java.lang.Object
  extended by org.apache.jcs.auxiliary.AbstractAuxiliaryCache
      extended by org.apache.jcs.auxiliary.AbstractAuxiliaryCacheEventLogging
          extended by org.apache.jcs.auxiliary.disk.AbstractDiskCache
              extended by org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache
All Implemented Interfaces:
java.io.Serializable, AuxiliaryCache, ICache, ICacheType

public class IndexedDiskCache
extends AbstractDiskCache

Disk cache that uses a RandomAccessFile with keys stored in memory. The maximum number of keys stored in memory is configurable. The disk cache tries to recycle spots on disk to limit file expansion.

See Also:
Serialized Form

Nested Class Summary
 class IndexedDiskCache.LRUMap
          Class for recylcing and lru.
 
Field Summary
protected  EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock storageLock
          Use this lock to synchronize reads and writes to the underlying storage mechansism.
 
Fields inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
alive, cacheEventQueue, cacheName, purgatory, purgHits
 
Fields inherited from class org.apache.jcs.auxiliary.AbstractAuxiliaryCache
cacheEventLogger, elementSerializer, keyMatcher
 
Fields inherited from interface org.apache.jcs.engine.behavior.ICacheType
CACHE_HUB, DISK_CACHE, LATERAL_CACHE, REMOTE_CACHE
 
Constructor Summary
IndexedDiskCache(IndexedDiskCacheAttributes cattr)
          Constructor for the DiskCache object.
 
Method Summary
protected  boolean checkForDedOverlaps(IndexedDiskElementDescriptor[] sortedDescriptors)
          Detects any overlapping elements.
 void dump()
          For debugging.
 void dump(boolean dumpValues)
          For debugging.
 AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
          This returns the generic attributes for an auxiliary cache.
protected  long getBytesFree()
          Returns the number of bytes that are free.
protected  long getDataFileSize()
          This is for debugging and testing.
protected  java.lang.String getDiskLocation()
          This is used by the event logging.
 java.util.Set getGroupKeys(java.lang.String groupName)
          Gets the group keys from the disk.
protected  int getRecyleBinSize()
          Returns the size of the recyclebin in number of elements.
protected  int getRecyleCount()
          Returns the number of times we have used spots from the recycle bin.
 int getSize()
          Returns the current cache size.
 IStats getStatistics()
          Returns info about the disk cache.
 java.lang.String getStats()
          Gets basic stats for the disk cache.
protected  int getTimesOptimized()
          This is exposed for testing.
protected  void loadKeys()
          Loads the keys from the .key file.
protected  void optimizeFile()
          File optimization is handled by this method.
 void processDispose()
          Dispose of the disk cache in a background thread.
protected  ICacheElement processGet(java.io.Serializable key)
          Gets the key, then goes to disk to get the object.
 java.util.Map processGetMatching(java.lang.String pattern)
          Gets matching items from the cache.
protected  boolean processRemove(java.io.Serializable key)
          Returns true if the removal was successful; or false if there is nothing to remove.
 void processRemoveAll()
          Remove all the items from the disk cache by reseting everything.
protected  void processUpdate(ICacheElement ce)
          Update the disk cache.
protected  void saveKeys()
          Saves key file to disk.
 
Methods inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
dispose, doDispose, doGet, doGetMatching, doRemove, doRemoveAll, doUpdate, get, getCacheName, getCacheType, getEventLoggingExtraInfo, getMatching, getStatus, processGetMultiple, remove, removeAll, update
 
Methods inherited from class org.apache.jcs.auxiliary.AbstractAuxiliaryCacheEventLogging
disposeWithEventLogging, getMatchingWithEventLogging, getMultiple, getMultipleWithEventLogging, getWithEventLogging, removeAllWithEventLogging, removeWithEventLogging, updateWithEventLogging
 
Methods inherited from class org.apache.jcs.auxiliary.AbstractAuxiliaryCache
createICacheEvent, createICacheEvent, getCacheEventLogger, getElementSerializer, getKeyMatcher, logApplicationEvent, logError, logICacheEvent, setCacheEventLogger, setElementSerializer, setKeyMatcher
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.jcs.auxiliary.AuxiliaryCache
setCacheEventLogger, setElementSerializer
 
Methods inherited from interface org.apache.jcs.engine.behavior.ICache
getMultiple, setKeyMatcher
 

Field Detail

storageLock

protected EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock storageLock
Use this lock to synchronize reads and writes to the underlying storage mechansism.

Constructor Detail

IndexedDiskCache

public IndexedDiskCache(IndexedDiskCacheAttributes cattr)
Constructor for the DiskCache object.

Parameters:
cattr -
Method Detail

loadKeys

protected void loadKeys()
                 throws java.lang.InterruptedException
Loads the keys from the .key file. The keys are stored in a HashMap on disk. This is converted into a LRUMap.

Throws:
java.lang.InterruptedException

checkForDedOverlaps

protected boolean checkForDedOverlaps(IndexedDiskElementDescriptor[] sortedDescriptors)
Detects any overlapping elements. This expects a sorted list.

The total length of an item is IndexedDisk.RECORD_HEADER + ded.len.

Parameters:
sortedDescriptors -
Returns:
false if there are overlaps.

saveKeys

protected void saveKeys()
Saves key file to disk. This converts the LRUMap to a HashMap for deserialzation.


processUpdate

protected void processUpdate(ICacheElement ce)
Update the disk cache. Called from the Queue. Makes sure the Item has not been retrieved from purgatory while in queue for disk. Remove items from purgatory when they go to disk.

Specified by:
processUpdate in class AbstractAuxiliaryCacheEventLogging
Parameters:
ce - The ICacheElement to put to disk.

processGet

protected ICacheElement processGet(java.io.Serializable key)
Gets the key, then goes to disk to get the object.

Specified by:
processGet in class AbstractAuxiliaryCacheEventLogging
Parameters:
key -
Returns:
ICacheElement or null
See Also:
AbstractDiskCache.doGet(java.io.Serializable)

processGetMatching

public java.util.Map processGetMatching(java.lang.String pattern)
Gets matching items from the cache.

Specified by:
processGetMatching in class AbstractAuxiliaryCacheEventLogging
Parameters:
pattern -
Returns:
a map of Serializable key to ICacheElement element, or an empty map if there is no data in cache matching keys

getGroupKeys

public java.util.Set getGroupKeys(java.lang.String groupName)
Gets the group keys from the disk.

Specified by:
getGroupKeys in interface AuxiliaryCache
Specified by:
getGroupKeys in class AbstractDiskCache
Returns:
a set of group keys
See Also:
AuxiliaryCache.getGroupKeys(java.lang.String)

processRemove

protected boolean processRemove(java.io.Serializable key)
Returns true if the removal was successful; or false if there is nothing to remove. Current implementation always result in a disk orphan.

Specified by:
processRemove in class AbstractAuxiliaryCacheEventLogging
Parameters:
key -
Returns:
true if at least one item was removed.

processRemoveAll

public void processRemoveAll()
Remove all the items from the disk cache by reseting everything.

Specified by:
processRemoveAll in class AbstractAuxiliaryCacheEventLogging

processDispose

public void processDispose()
Dispose of the disk cache in a background thread. Joins against this thread to put a cap on the disposal time.

TODO make dispose window configurable.

Specified by:
processDispose in class AbstractAuxiliaryCacheEventLogging

optimizeFile

protected void optimizeFile()
File optimization is handled by this method. It works as follows:
  1. Shutdown recycling and turn on queuing of puts.
  2. Take a snapshot of the current descriptors. If there are any removes, ignore them, as they will be compacted during the next optimization.
  3. Optimize the snapshot. For each descriptor:
    1. Obtain the write-lock.
    2. Shift the element on the disk, in order to compact out the free space.
    3. Release the write-lock. This allows elements to still be accessible during optimization.
  4. Obtain the write-lock.
  5. All queued puts are made at the end of the file. Optimize these under a single write-lock.
  6. Truncate the file.
  7. Release the write-lock.
  8. Restore system to standard operation.


getSize

public int getSize()
Returns the current cache size.

Specified by:
getSize in interface ICache
Specified by:
getSize in class AbstractDiskCache
Returns:
The size value
See Also:
ICache.getSize()

getRecyleBinSize

protected int getRecyleBinSize()
Returns the size of the recyclebin in number of elements.

Returns:
The number of items in the bin.

getRecyleCount

protected int getRecyleCount()
Returns the number of times we have used spots from the recycle bin.

Returns:
The number of spots used.

getBytesFree

protected long getBytesFree()
Returns the number of bytes that are free. When an item is removed, its length is recorded. When a spot is used form the recycle bin, the length of the item stored is recorded.

Returns:
The number bytes free on the disk file.

getDataFileSize

protected long getDataFileSize()
                        throws java.io.IOException
This is for debugging and testing.

Returns:
the length of the data file.
Throws:
java.io.IOException

dump

public void dump()
For debugging. This dumps the values by defualt.


dump

public void dump(boolean dumpValues)
For debugging.

Parameters:
dumpValues - A boolean indicating if values should be dumped.

getAuxiliaryCacheAttributes

public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
Description copied from interface: AuxiliaryCache
This returns the generic attributes for an auxiliary cache. Most implementations will cast this to a more specific type.

Returns:
Returns the AuxiliaryCacheAttributes.

getStats

public java.lang.String getStats()
Gets basic stats for the disk cache.

Specified by:
getStats in interface ICache
Overrides:
getStats in class AbstractDiskCache
Returns:
String

getStatistics

public IStats getStatistics()
Returns info about the disk cache.

(non-Javadoc)

Specified by:
getStatistics in interface AuxiliaryCache
Overrides:
getStatistics in class AbstractDiskCache
Returns:
the historical and statistical data for a region's auxiliary cache.
See Also:
AuxiliaryCache.getStatistics()

getTimesOptimized

protected int getTimesOptimized()
This is exposed for testing.

Returns:
Returns the timesOptimized.

getDiskLocation

protected java.lang.String getDiskLocation()
This is used by the event logging.

Specified by:
getDiskLocation in class AbstractDiskCache
Returns:
the location of the disk, either path or ip.


Copyright © 2002-2010 Apache Software Foundation. All Rights Reserved.