org.deegree.coverage.raster.cache
Class RasterCache

java.lang.Object
  extended by org.deegree.coverage.raster.cache.RasterCache

public class RasterCache
extends Object

The RasterCache holds references CacheRasterReader which wrap other RasterReaders. This Cache can have multiple directories for storing cache files. The cache is kept up-to-date because the RasterReaders allocate cached memory by using ByteBufferPool.allocate(int, boolean, boolean). This will call freeMemory(long) which in turn keeps track of reserved memory.

The RasterCache memory size and the amount of disk space can be set by adding the keys DEF_RASTER_CACHE_MEM_SIZE and DEF_RASTER_CACHE_DISK_SIZE to the JVM.

Version:
$Revision: 31644 $, $Date: 2011-08-24 22:27:43 +0200 (Wed, 24 Aug 2011) $
Author:
Rutger Bezema, last edited by: $Author: mschneider $

Field Summary
static String DEF_RASTER_CACHE_DISK_SIZE
          A key which can be given to the JVM to define the amount of memory used for caching.
static String DEF_RASTER_CACHE_MEM_SIZE
          A key which can be given to the JVM to define the amount of memory used for caching.
static File DEFAULT_CACHE_DIR
          Default cache dir if no directory was given.
static String FILE_EXTENSION
          Standard name for a deegree cache file.
 
Method Summary
 RasterReader addReader(RasterReader reader)
          Adds a raster reader to this cache, all cache files will be written to this cache directory.
static void clear(boolean deleteCacheFiles)
          Clears all Memory buffers from all known cached readers, removes the cache files (if requested) and removes all readers from the cache.
 File createCacheFile(String id)
          Creates a unique cachefile for the given id, if the id already exists in the cache directory an index will be appended. if the given id is null a uuid will be used, this file will be marked to be deleted on exit.
 SimpleRaster createFromCache(RasterReader reader, String rasterId)
          Tries to find the file with given id from the current cache directory and instantiates a cachedraster for it.
static void dispose()
          Iterates over all current cache directories and calls dispose on their cache files.
static void flush()
          Writes all current caches to their cache files, but leaves the in memory cached rasters alone.
static long freeMemory(long requiredMemory)
          Signals the cache to write as much data to cache files so that the memory occupied by rasters can be returned to running process.
 File getCacheDirectory()
           
static long getCurrentlyUsedDisk()
           
static long getCurrentlyUsedMemory()
           
static RasterCache getInstance()
          Gets an instance of a data cache which uses the default directory as the cache directory.
static RasterCache getInstance(File directory, boolean create)
          Gets an instance of a data cache which uses the given directory as the cache directory.
static RasterCache getInstance(RasterIOOptions options)
          Gets an instance of a data cache which uses the given options to instantiate a cache.
static void reset(boolean deleteCachedFile)
          Clear the cache of all readers (and optionally delete all cache files) and reevaluate the disk and memory size ( DEF_RASTER_CACHE_DISK_SIZE, DEF_RASTER_CACHE_MEM_SIZE) properties from the system.
static int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CACHE_DIR

public static final File DEFAULT_CACHE_DIR
Default cache dir if no directory was given.


DEF_RASTER_CACHE_MEM_SIZE

public static final String DEF_RASTER_CACHE_MEM_SIZE
A key which can be given to the JVM to define the amount of memory used for caching.

See Also:
Constant Field Values

DEF_RASTER_CACHE_DISK_SIZE

public static final String DEF_RASTER_CACHE_DISK_SIZE
A key which can be given to the JVM to define the amount of memory used for caching.

See Also:
Constant Field Values

FILE_EXTENSION

public static final String FILE_EXTENSION
Standard name for a deegree cache file.

See Also:
Constant Field Values
Method Detail

reset

public static void reset(boolean deleteCachedFile)
Clear the cache of all readers (and optionally delete all cache files) and reevaluate the disk and memory size ( DEF_RASTER_CACHE_DISK_SIZE, DEF_RASTER_CACHE_MEM_SIZE) properties from the system. This method is to be called with care.

Parameters:
deleteCachedFile - true if all cached files should be deleted.

getInstance

public static RasterCache getInstance(File directory,
                                      boolean create)
Gets an instance of a data cache which uses the given directory as the cache directory.

Parameters:
directory -
create - true if the directory should be created if missing.
Returns:
a raster cache for the given directory.

getInstance

public static RasterCache getInstance(RasterIOOptions options)
Gets an instance of a data cache which uses the given options to instantiate a cache. Currently following RasterIOOptions keys are evaluated. If the options are null the DEFAULT_CACHE_DIR will be used.

Parameters:
options - which can contain cache information.
Returns:
a raster cache for the given directory.

getInstance

public static RasterCache getInstance()
Gets an instance of a data cache which uses the default directory as the cache directory.

Returns:
a raster cache for the default directory.

getCurrentlyUsedMemory

public static final long getCurrentlyUsedMemory()
Returns:
the currentlyUsedMemory

getCurrentlyUsedDisk

public static final long getCurrentlyUsedDisk()
Returns:
the currentlyUsedDisk

size

public static int size()
Returns:
the number of cached readers.

clear

public static void clear(boolean deleteCacheFiles)
Clears all Memory buffers from all known cached readers, removes the cache files (if requested) and removes all readers from the cache. Note, all information on the currently rasters is lost.

Parameters:
deleteCacheFiles - true if the currently used files should be removed from cache.

flush

public static void flush()
Writes all current caches to their cache files, but leaves the in memory cached rasters alone.


dispose

public static void dispose()
Iterates over all current cache directories and calls dispose on their cache files.


freeMemory

public static long freeMemory(long requiredMemory)
Signals the cache to write as much data to cache files so that the memory occupied by rasters can be returned to running process. Note this method does not actually write the cache files, it merely signals the CacheRasterReaders to write their data to file if they have a file to write to. It may well be that the required memory can not be freed.

Parameters:
requiredMemory - some process may need.
Returns:
the amount of currently used cache memory, which is only an approximation.

addReader

public RasterReader addReader(RasterReader reader)
Adds a raster reader to this cache, all cache files will be written to this cache directory.

Parameters:
reader - to add to the cache.
Returns:
a new CachedReader which was added to the cache.

createCacheFile

public final File createCacheFile(String id)
Creates a unique cachefile for the given id, if the id already exists in the cache directory an index will be appended. if the given id is null a uuid will be used, this file will be marked to be deleted on exit.

Parameters:
id - to be used for the identification for the cache file.
Returns:
a unique file name based on the given id.

createFromCache

public SimpleRaster createFromCache(RasterReader reader,
                                    String rasterId)
Tries to find the file with given id from the current cache directory and instantiates a cachedraster for it.

Parameters:
reader - to be used for reading the original data, if parts of the cachefile are incoherent
rasterId - the id of the raster if null no raster cache file will be created.
Returns:
the raster created from the cache or null if no cache file with given id was found.

getCacheDirectory

public File getCacheDirectory()
Returns:
the directory used for caching.


Copyright © 2011. All Rights Reserved.