Class IndexedDiskCache<K,V>

All Implemented Interfaces:
AuxiliaryCache<K,V>, ICache<K,V>, ICacheType

public class IndexedDiskCache<K,V> extends AbstractDiskCache<K,V>
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.
  • Field Details

  • Constructor Details

  • Method Details

    • loadKeys

      protected void loadKeys()
      Loads the keys from the .key file. The keys are stored in a HashMap on disk. This is converted into a LRUMap.
    • 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 deserialization.
    • processUpdate

      protected void processUpdate(ICacheElement<K,V> 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<K,V>
      Parameters:
      ce - The ICacheElement<K, V> to put to disk.
    • processGet

      protected ICacheElement<K,V> processGet(K key)
      Gets the key, then goes to disk to get the object.

      Specified by:
      processGet in class AbstractAuxiliaryCacheEventLogging<K,V>
      Parameters:
      key -
      Returns:
      ICacheElement<K, V> or null
      See Also:
    • processGetMatching

      Gets matching items from the cache.

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

      public Set<K> getKeySet() throws IOException
      Return the keys in this cache.

      Specified by:
      getKeySet in interface AuxiliaryCache<K,V>
      Specified by:
      getKeySet in class AbstractDiskCache<K,V>
      Returns:
      a set of the key type TODO This should probably be done in chunks with a range passed in. This will be a problem if someone puts a 1,000,000 or so items in a region.
      Throws:
      IOException - if access to the auxiliary cache fails
      See Also:
    • processRemove

      protected boolean processRemove(K 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<K,V>
      Parameters:
      key -
      Returns:
      true if at least one item was removed.
    • processRemoveAll

      public void processRemoveAll()
      Remove all the items from the disk cache by resetting everything.
      Specified by:
      processRemoveAll in class AbstractAuxiliaryCacheEventLogging<K,V>
    • 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<K,V>
    • disposeInternal

      protected void disposeInternal()
      Internal method that handles the disposal.
    • addToRecycleBin

      Add descriptor to recycle bin if it is not null. Adds the length of the item to the bytes free.

      This is called in three places: (1) When an item is removed. All item removals funnel down to the removeSingleItem method. (2) When an item on disk is updated with a value that will not fit in the previous slot. (3) When the max key size is reached, the freed slot will be added.

      Parameters:
      ded -
    • doOptimizeRealTime

      protected void doOptimizeRealTime()
      Performs the check for optimization, and if it is required, do it.
    • 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<K,V>
      Specified by:
      getSize in class AbstractDiskCache<K,V>
      Returns:
      The size value
      See Also:
    • getRecyleBinSize

      protected int getRecyleBinSize()
      Returns the size of the recycle bin 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 IOException
      This is for debugging and testing.

      Returns:
      the length of the data file.
      Throws:
      IOException
    • dump

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

      public void dump(boolean dumpValues)
      For debugging.

      Parameters:
      dumpValues - A boolean indicating if values should be dumped.
    • 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.
    • getStatistics

      Returns info about the disk cache.

      Specified by:
      getStatistics in interface AuxiliaryCache<K,V>
      Overrides:
      getStatistics in class AbstractDiskCache<K,V>
      Returns:
      the historical and statistical data for a region's auxiliary cache.
      See Also:
    • getTimesOptimized

      protected int getTimesOptimized()
      This is exposed for testing.

      Returns:
      Returns the timesOptimized.
    • getDiskLocation

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

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