visad.data
Class DataCacheManager

java.lang.Object
  extended by visad.data.DataCacheManager
All Implemented Interfaces:
Runnable

public class DataCacheManager
extends Object
implements Runnable

This provides a global cache for primitive array data You access it via the singelton:

     DataCacheManager.getCacheManager()
Client objects can store their data with:
  Object cacheId = DataCacheManager.getCacheManager().addToCache(theData);
Where data can be 1D or 2D byte/float/int/double arrays If the data changes then update the cache with:
        DataCacheManager.getCacheManager().updateData(cacheId, newData);
When the client object is finalized or is finished with the data call:
        DataCacheManager.getCacheManager().removeFromCache(cacheId);
When you want to access the data use one of:
      DataCacheManager.getCacheManager().getByteArray1D(cacheId); 
      DataCacheManager.getCacheManager().getByteArray2D(cacheId); 
      DataCacheManager.getCacheManager().getFloatArray1D(cacheId); 
      DataCacheManager.getCacheManager().getFloatArray2D(cacheId); 
      DataCacheManager.getCacheManager().getShortArray1D(cacheId); 
      DataCacheManager.getCacheManager().getShortArray2D(cacheId); 
      DataCacheManager.getCacheManager().getIntArray1D(cacheId); 
      DataCacheManager.getCacheManager().getIntArray2D(cacheId); 
      DataCacheManager.getCacheManager().getDoubleArray1D(cacheId); 
      DataCacheManager.getCacheManager().getDoubleArray2D(cacheId); 
The cachemanager will keep the data arrays in memory until the total size is greater than getMaxSize(). Then it will serialize the data arrays in a least recently used manner until the totalSize less than the max size.


Method Summary
 Object addToCache(byte[] values)
          add the data to the cache
 Object addToCache(byte[][] values)
          add the data to the cache
 Object addToCache(byte[][][] values)
          add the data to the cache
 Object addToCache(double[] values)
          add the data to the cache
 Object addToCache(double[][] values)
          add the data to the cache
 Object addToCache(double[][][] values)
          add the data to the cache
 Object addToCache(float[] values)
          add the data to the cache
 Object addToCache(float[][] values)
          add the data to the cache
 Object addToCache(float[][][] values)
          add the data to the cache
 Object addToCache(int[] values)
          add the data to the cache
 Object addToCache(int[][] values)
          add the data to the cache
 Object addToCache(int[][][] values)
          add the data to the cache
 Object addToCache(short[] values)
          add the data to the cache
 Object addToCache(short[][] values)
          add the data to the cache
 Object addToCache(short[][][] values)
          add the data to the cache
 Object addToCache(String what, byte[] values)
          add the data to the cache
 Object addToCache(String what, byte[][] values)
          add the data to the cache
 Object addToCache(String what, byte[][][] values)
          add the data to the cache
 Object addToCache(String what, byte[][][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, byte[][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, byte[] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, double[] values)
          add the data to the cache
 Object addToCache(String what, double[][] values)
          add the data to the cache
 Object addToCache(String what, double[][][] values)
          add the data to the cache
 Object addToCache(String what, double[][][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, double[][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, double[] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, float[] values)
          add the data to the cache
 Object addToCache(String what, float[][] values)
          add the data to the cache
 Object addToCache(String what, float[][][] values)
          add the data to the cache
 Object addToCache(String what, float[][][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, float[][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, float[] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, int[] values)
          add the data to the cache
 Object addToCache(String what, int[][] values)
          add the data to the cache
 Object addToCache(String what, int[][][] values)
          add the data to the cache
 Object addToCache(String what, int[][][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, int[][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, int[] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, short[] values)
          add the data to the cache
 Object addToCache(String what, short[][] values)
          add the data to the cache
 Object addToCache(String what, short[][][] values)
          add the data to the cache
 Object addToCache(String what, short[][][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, short[][] values, boolean removeIfNeeded)
          add the data to the cache
 Object addToCache(String what, short[] values, boolean removeIfNeeded)
          add the data to the cache
 void checkCache()
          Check if we are above the max size.
 void flushAllCachedData()
           
 byte[] getByteArray1D(Object cacheId)
          get the value from the cache
 byte[][] getByteArray2D(Object cacheId)
          get the value from the cache
 byte[][][] getByteArray3D(Object cacheId)
          get the value from the cache
 File getCacheDir()
          get the cache dir.
 File getCacheFile()
           
static DataCacheManager getCacheManager()
          The singleton access
 double[] getDoubleArray1D(Object cacheId)
          get the value from the cache
 double[][] getDoubleArray2D(Object cacheId)
          get the value from the cache
 double[][][] getDoubleArray3D(Object cacheId)
          get the value from the cache
 float[] getFloatArray1D(Object cacheId)
          get the value from the cache
 float[][] getFloatArray2D(Object cacheId)
          get the value from the cache
 float[][][] getFloatArray3D(Object cacheId)
          get the value from the cache
 Object getId()
          Get a unique id
 int[] getIntArray1D(Object cacheId)
          get the value from the cache
 int[][] getIntArray2D(Object cacheId)
          get the value from the cache
 int[][][] getIntArray3D(Object cacheId)
          get the value from the cache
 int getMaxSize()
           
 short[] getShortArray1D(Object cacheId)
          get the value from the cache
 short[][] getShortArray2D(Object cacheId)
          get the value from the cache
 short[][][] getShortArray3D(Object cacheId)
          get the value from the cache
 String getStats()
           
 boolean inMemory(Object cacheId)
           
 void printStats()
          Print out the cache statistics
 void removeFromCache(Object cacheId)
          Remove the item from the cache
 void run()
           
 void setCacheDir(File f)
          set the directory to write files to
 void setMemoryPercent(double percentage)
           
 void updateData(Object cacheId, Object data)
          the data has changed for the given cache id
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getCacheManager

public static DataCacheManager getCacheManager()
The singleton access

Returns:
the cache manager

run

public void run()
Specified by:
run in interface Runnable

setCacheDir

public void setCacheDir(File f)
set the directory to write files to

Parameters:
f - dir

getCacheDir

public File getCacheDir()
get the cache dir. If it was not set then default to current directory

Returns:
The cache dir

getId

public Object getId()
Get a unique id

Returns:
unique id

updateData

public void updateData(Object cacheId,
                       Object data)
the data has changed for the given cache id

Parameters:
cacheId - cache id
data - the new data

inMemory

public boolean inMemory(Object cacheId)

getCacheFile

public File getCacheFile()

removeFromCache

public void removeFromCache(Object cacheId)
Remove the item from the cache

Parameters:
cacheId - the cache id

flushAllCachedData

public void flushAllCachedData()

setMemoryPercent

public void setMemoryPercent(double percentage)

getMaxSize

public int getMaxSize()

checkCache

public void checkCache()
Check if we are above the max size. If so then flush data from memory until we are below the threshold


printStats

public void printStats()
Print out the cache statistics


getStats

public String getStats()

getDoubleArray1D

public double[] getDoubleArray1D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(double[] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         double[] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         double[] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getFloatArray1D

public float[] getFloatArray1D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(float[] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         float[] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         float[] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getIntArray1D

public int[] getIntArray1D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(int[] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         int[] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         int[] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getShortArray1D

public short[] getShortArray1D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(short[] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         short[] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         short[] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getByteArray1D

public byte[] getByteArray1D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(byte[] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         byte[] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         byte[] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getDoubleArray2D

public double[][] getDoubleArray2D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(double[][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         double[][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         double[][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getFloatArray2D

public float[][] getFloatArray2D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(float[][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         float[][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         float[][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getIntArray2D

public int[][] getIntArray2D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(int[][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         int[][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         int[][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getShortArray2D

public short[][] getShortArray2D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(short[][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         short[][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         short[][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getByteArray2D

public byte[][] getByteArray2D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(byte[][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         byte[][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         byte[][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getDoubleArray3D

public double[][][] getDoubleArray3D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(double[][][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         double[][][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         double[][][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getFloatArray3D

public float[][][] getFloatArray3D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(float[][][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         float[][][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         float[][][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getIntArray3D

public int[][][] getIntArray3D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(int[][][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         int[][][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         int[][][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getShortArray3D

public short[][][] getShortArray3D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(short[][][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         short[][][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         short[][][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id

getByteArray3D

public byte[][][] getByteArray3D(Object cacheId)
get the value from the cache

Parameters:
cacheId - the cache id
Returns:
the value

addToCache

public Object addToCache(byte[][][] values)
add the data to the cache

Parameters:
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         byte[][][] values)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
Returns:
the cache id

addToCache

public Object addToCache(String what,
                         byte[][][] values,
                         boolean removeIfNeeded)
add the data to the cache

Parameters:
what - the name of the item. used for tracking cache behavior
values - the values to add
removeIfNeeded - If true then this data will not be written to disk and will be removed from the cache when the cache is exceeding memory limits
Returns:
the cache id