org.w3c.www.protocol.http.cache
Class CacheStore

java.lang.Object
  extended by org.w3c.www.protocol.http.cache.CacheStore
All Implemented Interfaces:
PropertyMonitoring

public class CacheStore
extends java.lang.Object
implements PropertyMonitoring


Field Summary
static java.lang.String CACHE_DIRECTORY_P
          Name of the property giving the cache's directory.
static java.lang.String FILE_SIZE_RATIO_P
          Name of the property indicating the max size for files to be cached.
static java.lang.String GARBAGE_COLLECTION_ENABLED_P
          Name of the property enabling garbage collection of the cache.
static java.lang.String GARBAGE_COLLECTION_THRESHOLD_P
          Name of the property used to knkow the percentage of bytes to be kept after a garbage collection It defaults to 0.80 (80% of the cache size)
static java.lang.String GENERATION_COMPACT_DELAY_P
          Name of the property indicating the amount of time in second between two attempts to compact generations.
static java.lang.String GENERATION_FILENAME
           
static java.lang.String MAX_CACHED_RESOURCES_P
          Name of the property indicating the maximal number of resources the cache can load in memory (not the content of the resources)
static java.lang.String MAX_GENERATIONS_P
          Name of the property indicating the maximal number of generations in this cache store
protected  ObservableProperties props
          The properties we initialized ourself from.
static java.lang.String STORE_SIZE_P
          The name of the properties indicating the storesize of the cache (in bytes).
static java.lang.String SYNCHRONIZATION_DELAY_P
          Name of the property indicating the amount of time in second between two synchronization of the database (aka dump on disk) Milliseconds
 
Constructor Summary
CacheStore()
           
 
Method Summary
protected  CacheGeneration addNewGeneration()
          Create and add a new Generation.
protected  void checkDirs()
          Check the subdirectories.
protected  void checkState()
          used for debugging, display some internal information about the state of the cache
protected  int cleanCacheDir()
          Clean the Cache directory, remove unused files.
protected  void compactGenerations()
          Compact our generations The algorithm is the following, If the number of generation is the maximum number allowed, then a check is done from the the generation after the MRU one and if the sum of two generation can fit into one, it is done, and the generation is removed from the list
 long getCachedByteCount()
          get the cached size of this cache
 long getCachedByteFree()
          get the number of bytes available for the cache memory
 CachedResource getCachedResource(CachedResource cr)
          extract a cached resource from the store
 CachedResource getCachedResource(java.lang.String url)
          Get a cached resource relative to the given URL.
 CachedResource getCachedResourceReference(java.lang.String url)
          Get a cached resource relative to the given URL.
 long getCompactGenerationDelay()
          Get the delay between two attempts to compact the generations.
 CacheGeneration getLRUGeneration()
          Get the LRU generation, ie the Least recently used generation
 CacheGeneration getLRULoadedGeneration()
          Get the oldest loaded generation
 CacheGeneration getMRUGeneration()
          Get the last generation, ie the Most recently used generation
 float getMRUGenerationRatio()
          Get the fill ratio of the last generation (the most recently used)
protected  java.io.File getNewEntryFile()
          allocate a new name for the next cached resource.
 CacheGeneration getNextGeneration(CacheGeneration gen)
          Get the next generation (in LRU order).
 CacheGeneration getPrevGeneration(CacheGeneration gen)
          Get the previous generation (in LRU order).
 long getRequiredByteNumber()
          get the number of bytes the garbage collector needs to collect to keep the cache in good state, it will only move the resource to the delete list, another check has to be done to save physical space
 CacheSerializer getSerializer()
          return the serializer used by the cache
 StoreState getState()
           
 long getStoredByteCount()
          get the number of bytes used phisycally by this cache
 CacheSweeper getSweeper()
          return the cache sweeper used by the cache
 long getSyncDelay()
          Get the synchronization delay between to sync, in milliseconds
 CacheValidator getValidator()
          return the cache validator used by the cache
 void initialize(CacheFilter filter)
          initialize this CacheStore, and get some infos from the parent, aka the cache filter
protected  CacheGeneration loadGeneration(CacheGeneration cg)
          Load the given generation and unload the LRU loaded generation if necessary.
protected  void loadGenerations()
          Load the generations.
protected  void loadState()
          Load the store state
 boolean propertyChanged(java.lang.String name)
          Property monitoring for the CacheStore.
protected  void removeResource(CacheGeneration cg, CachedResource cr)
          Remove the given CachedResource from the given CacheGeneration.
protected  void resizeGeneration(CacheGeneration cg, CachedResource cr)
          Resize the generation in order to be able to store the given Resource.
protected  void saveGeneration(CacheGeneration cg)
          Save a generation.
protected  void saveState()
          Save the current state
 boolean storeCachedResource(CachedResource cr)
          Store a newly created (or updated) CachedResource.
 boolean storeCachedResource(CachedResource cr, long oldsize)
          Store a newly created (or updated) CachedResource.
 void sync()
          synchronize the internal database with the storage
 java.lang.String toString()
           
protected  void unloadGeneration(CacheGeneration cg)
          UnLoad a "loaded" generation.
 CachedResource updateResourceGeneration(CachedResource cr)
          update this cached resource from generation x ot the latest
protected  void updateSweeperPriority()
          update the mode of the sweeper according to the state of the cache store,
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CACHE_DIRECTORY_P

public static final java.lang.String CACHE_DIRECTORY_P
Name of the property giving the cache's directory. This property defaults to the current directory.

See Also:
Constant Field Values

STORE_SIZE_P

public static final java.lang.String STORE_SIZE_P
The name of the properties indicating the storesize of the cache (in bytes). This property will give the value of the disk-based cache size. This value only takes into account the size of the entities saved, not the size of the associated headers, and not the physical size on the disc.

This property defaults to 20 Mbytes.

See Also:
Constant Field Values

GARBAGE_COLLECTION_THRESHOLD_P

public static final java.lang.String GARBAGE_COLLECTION_THRESHOLD_P
Name of the property used to knkow the percentage of bytes to be kept after a garbage collection It defaults to 0.80 (80% of the cache size)

See Also:
Constant Field Values

FILE_SIZE_RATIO_P

public static final java.lang.String FILE_SIZE_RATIO_P
Name of the property indicating the max size for files to be cached. This property gives the ratio (relative to the cache size) of the number of bytes a single entry is able to occupy.

The ratio should be given as a floating point value between 0 and 1. If set to 0.1 and the cache size is 5000000, files larger then 500000 will not be cached (except if garbage collection is disbabled).

This property defaults to 0.1. Note that the generation size will be taken from this threshold

See Also:
Constant Field Values

GARBAGE_COLLECTION_ENABLED_P

public static final java.lang.String GARBAGE_COLLECTION_ENABLED_P
Name of the property enabling garbage collection of the cache. This property defaults to true.

See Also:
Constant Field Values

SYNCHRONIZATION_DELAY_P

public static final java.lang.String SYNCHRONIZATION_DELAY_P
Name of the property indicating the amount of time in second between two synchronization of the database (aka dump on disk) Milliseconds

See Also:
Constant Field Values

GENERATION_COMPACT_DELAY_P

public static final java.lang.String GENERATION_COMPACT_DELAY_P
Name of the property indicating the amount of time in second between two attempts to compact generations. In milliseconds

See Also:
Constant Field Values

MAX_CACHED_RESOURCES_P

public static final java.lang.String MAX_CACHED_RESOURCES_P
Name of the property indicating the maximal number of resources the cache can load in memory (not the content of the resources)

See Also:
Constant Field Values

MAX_GENERATIONS_P

public static final java.lang.String MAX_GENERATIONS_P
Name of the property indicating the maximal number of generations in this cache store

See Also:
Constant Field Values

GENERATION_FILENAME

public static final java.lang.String GENERATION_FILENAME
See Also:
Constant Field Values

props

protected ObservableProperties props
The properties we initialized ourself from.

Constructor Detail

CacheStore

public CacheStore()
Method Detail

propertyChanged

public boolean propertyChanged(java.lang.String name)
Property monitoring for the CacheStore. The CacheStore allows you to dynamically (typically through the property setter) change the class of the sweeper, the validator, the size...

Specified by:
propertyChanged in interface PropertyMonitoring
Parameters:
name - The name of the property that has changed.
Returns:
A boolean, true if the change was made, false otherwise.

getState

public StoreState getState()

getSweeper

public CacheSweeper getSweeper()
return the cache sweeper used by the cache

Returns:
an instance of CacheSweeper

getSerializer

public CacheSerializer getSerializer()
return the serializer used by the cache

Returns:
an instance of Serializer

getValidator

public CacheValidator getValidator()
return the cache validator used by the cache

Returns:
an instance of CacheValidator

getNextGeneration

public CacheGeneration getNextGeneration(CacheGeneration gen)
Get the next generation (in LRU order).

Parameters:
gen - the current generation
Returns:
a generation

getPrevGeneration

public CacheGeneration getPrevGeneration(CacheGeneration gen)
Get the previous generation (in LRU order).

Parameters:
gen - the current generation
Returns:
a generation

getMRUGeneration

public CacheGeneration getMRUGeneration()
Get the last generation, ie the Most recently used generation

Returns:
a CacheGeneration, the most recently used one

getMRUGenerationRatio

public float getMRUGenerationRatio()
Get the fill ratio of the last generation (the most recently used)


getLRUGeneration

public CacheGeneration getLRUGeneration()
Get the LRU generation, ie the Least recently used generation

Returns:
a CacheGeneration, the least recently used one

getLRULoadedGeneration

public CacheGeneration getLRULoadedGeneration()
Get the oldest loaded generation

Returns:
a generation

getSyncDelay

public long getSyncDelay()
Get the synchronization delay between to sync, in milliseconds

Returns:
a long, the number of milliseconds

getCompactGenerationDelay

public long getCompactGenerationDelay()
Get the delay between two attempts to compact the generations.

Returns:
a long, the number of milliseconds

addNewGeneration

protected CacheGeneration addNewGeneration()
                                    throws InvalidCacheException
Create and add a new Generation. WARNING, this method is not synchronized.

Returns:
the newly created generation or null if the max number of generations is reached or it the current size of the cache (in memory) is too big to accept a new generation.
Throws:
InvalidCacheException - if the cache is corrupted

loadGeneration

protected CacheGeneration loadGeneration(CacheGeneration cg)
                                  throws InvalidCacheException
Load the given generation and unload the LRU loaded generation if necessary.

Parameters:
cg - the generation to load
Throws:
InvalidCacheException - if the cache is corrupted

unloadGeneration

protected void unloadGeneration(CacheGeneration cg)
                         throws InvalidCacheException
UnLoad a "loaded" generation. WARNING: not synchronized.

Parameters:
the - loaded generation
Throws:
InvalidCacheException - if the cache is corrupted

saveGeneration

protected void saveGeneration(CacheGeneration cg)
                       throws InvalidCacheException
Save a generation.

Parameters:
the - generation to be saved
Throws:
InvalidCacheException - if the cache is corrupted

loadGenerations

protected void loadGenerations()
                        throws InvalidCacheException
Load the generations. Only a subset of the generations will actually be loaded, some generations will only have a description (URLs, size) in memory. This method load some generations (until cr_limit is reached) and the remaining generations (if any) will only be a set of CachedResourceDescription.

Throws:
InvalidCacheException

loadState

protected void loadState()
Load the store state


saveState

protected void saveState()
Save the current state


getRequiredByteNumber

public long getRequiredByteNumber()
get the number of bytes the garbage collector needs to collect to keep the cache in good state, it will only move the resource to the delete list, another check has to be done to save physical space

Returns:
a long, the number of bytes to save

getCachedByteCount

public long getCachedByteCount()
get the cached size of this cache

Returns:
a long, the number of bytes cached

getStoredByteCount

public long getStoredByteCount()
get the number of bytes used phisycally by this cache

Returns:
a long, the number of bytes cached

getCachedByteFree

public long getCachedByteFree()
get the number of bytes available for the cache memory

Returns:
a long, the number of bytes free

sync

public void sync()
synchronize the internal database with the storage


removeResource

protected void removeResource(CacheGeneration cg,
                              CachedResource cr)
                       throws NoSuchResourceException
Remove the given CachedResource from the given CacheGeneration. WARNING: not synchronized

Throws:
NoSuchResourceException - if this resource was not in this generation

getCachedResource

public CachedResource getCachedResource(java.lang.String url)
                                 throws InvalidCacheException
Get a cached resource relative to the given URL. WARNING: the CachedResource returned is no more in the CacheStore.

Parameters:
url - the URL of the CachedResource
Returns:
a CachedResource or null
Throws:
InvalidCacheException - if the cache is corrupted
See Also:
storeCachedResource(org.w3c.www.protocol.http.cache.CachedResource)

getCachedResource

public CachedResource getCachedResource(CachedResource cr)
                                 throws InvalidCacheException
extract a cached resource from the store

Parameters:
the - cached resource to be extracted
Returns:
the extracted cached resource
Throws:
InvalidCacheException - if the cache is corrupted

resizeGeneration

protected void resizeGeneration(CacheGeneration cg,
                                CachedResource cr)
Resize the generation in order to be able to store the given Resource.

Parameters:
cg - the generation to resize
cr - the CachedResource to store.

getCachedResourceReference

public CachedResource getCachedResourceReference(java.lang.String url)
                                          throws InvalidCacheException
Get a cached resource relative to the given URL. WARNING: the CachedResource returned is still in the cache store!

Parameters:
url - the URL of the CachedResource
Returns:
a CachedResource or null
Throws:
InvalidCacheException - if the cache is corrupted
See Also:
storeCachedResource(org.w3c.www.protocol.http.cache.CachedResource)

updateResourceGeneration

public CachedResource updateResourceGeneration(CachedResource cr)
                                        throws InvalidCacheException
update this cached resource from generation x ot the latest

Parameters:
the - cached resource to be updated
Returns:
the updated
Throws:
InvalidCacheException - if the cache is corrupted

storeCachedResource

public boolean storeCachedResource(CachedResource cr)
                            throws InvalidCacheException
Store a newly created (or updated) CachedResource.

Parameters:
cr - the CachedResource to store
Returns:
true if the resource has been cached
Throws:
InvalidCacheException - if the cache is corrupted

storeCachedResource

public boolean storeCachedResource(CachedResource cr,
                                   long oldsize)
                            throws InvalidCacheException
Store a newly created (or updated) CachedResource.

Parameters:
cr - the CachedResource to store
Returns:
true if the resource has been cached
Throws:
InvalidCacheException - if the cache is corrupted

getNewEntryFile

protected java.io.File getNewEntryFile()
allocate a new name for the next cached resource. This method create some directories if needed.

Returns:
a File, used to dump the entry

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

checkDirs

protected void checkDirs()
Check the subdirectories. Create them if necessary.


cleanCacheDir

protected int cleanCacheDir()
Clean the Cache directory, remove unused files.

Returns:
the number of files deleted.

updateSweeperPriority

protected void updateSweeperPriority()
update the mode of the sweeper according to the state of the cache store,


compactGenerations

protected void compactGenerations()
Compact our generations The algorithm is the following, If the number of generation is the maximum number allowed, then a check is done from the the generation after the MRU one and if the sum of two generation can fit into one, it is done, and the generation is removed from the list


checkState

protected void checkState()
used for debugging, display some internal information about the state of the cache


initialize

public void initialize(CacheFilter filter)
                throws InvalidCacheException
initialize this CacheStore, and get some infos from the parent, aka the cache filter

Parameters:
filter - a CacheFilter, our parent
Throws:
InvalidCacheException - if the cache not initialized