com.compomics.util.db
Class ObjectsCache

java.lang.Object
  extended by com.compomics.util.db.ObjectsCache

public class ObjectsCache
extends Object

An object cache can be combined to an ObjectDB to improve its performance. A single cache can be used by different databases. This ough not to be serialized. The length of lists/maps in the cache shall stay independent from the number of objects in cache.

Author:
Marc Vaudel

Constructor Summary
ObjectsCache()
          Constructor.
 
Method Summary
 void addDb(ObjectsDB objectsDB)
          Adds a database in the list of the databases handled by the cache.
 void addObject(String dbName, String tableName, String objectKey, Object object, boolean modifiedOrNew)
          Adds an object to the cache.
 int getBatchSize()
          Returns the batch size in number of objects.
 int getCacheSize()
          Returns the cache size in number of objects.
 double getMemoryShare()
          Returns the share of heap size which can be used before emptying the cache.
 Object getObject(String dbName, String tableName, String objectKey)
          Returns the objects if present in the cache.
 boolean inCache(String dbName, String tableName, String objectKey)
          Indicates whether an object is loaded in the cache
 boolean isAutomatedMemoryManagement()
          Returns whether the cache is in automated memory management mode.
 boolean memoryCheck()
          Indicates whether the memory used by the application is lower than 99% of the heap size.
 void reduceMemoryConsumption(double share, WaitingHandler waitingHandler)
          Reduces the memory consumption by saving the given share of hits.
 void removeObject(String dbName, String tableName, String objectKey)
          Removes an object from the cache mappings.
 void saveCache(WaitingHandler waitingHandler, boolean emptyCache)
          Saves the cache content in the database.
 void saveObject(String entryKey)
          Saves an entry in the database if modified and clears it from the cache.
 void saveObject(String entryKey, boolean clearEntry)
          Saves an entry in the database if modified.
 void saveObjects(ArrayList<String> entryKeys)
          Saves an entry in the database if modified and clears it from the cache.
 void saveObjects(ArrayList<String> entryKeys, WaitingHandler waitingHandler)
          Saves an entry in the database if modified and clears it from the cache.
 void saveObjects(ArrayList<String> entryKeys, WaitingHandler waitingHandler, boolean clearEntries)
          Saves an entry in the database if modified.
 void setAutomatedMemoryManagement(boolean automatedMemoryManagement)
          Sets whether the cache is in automated memory management mode.
 void setBatchSize(int batchSize)
          Sets the batch size in number of objects.
 void setCacheSize(int cacheSize)
          Sets the cache size in number of objects.
 void setMemoryShare(double memoryShare)
          Sets the share of heap size which can be used before emptying the cache.
 void updateCache()
          Updates the cache according to the memory settings.
 boolean updateObject(String dbName, String tableName, String objectKey, Object object)
          Sets that a match has been modified and returns true in case of success.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectsCache

public ObjectsCache()
Constructor.

Method Detail

isAutomatedMemoryManagement

public boolean isAutomatedMemoryManagement()
Returns whether the cache is in automated memory management mode.

Returns:
a boolean indicating whether the cache is in automated memory management mode

setAutomatedMemoryManagement

public void setAutomatedMemoryManagement(boolean automatedMemoryManagement)
Sets whether the cache is in automated memory management mode.

Parameters:
automatedMemoryManagement - a boolean indicating whether the cache is in automated memory management mode

getCacheSize

public int getCacheSize()
Returns the cache size in number of objects.

Returns:
the cache size in number of objects

setCacheSize

public void setCacheSize(int cacheSize)
Sets the cache size in number of objects.

Parameters:
cacheSize - the cache size in number of objects

getBatchSize

public int getBatchSize()
Returns the batch size in number of objects.

Returns:
the batch size in number of objects

setBatchSize

public void setBatchSize(int batchSize)
Sets the batch size in number of objects.

Parameters:
batchSize - the batch size in number of objects

getMemoryShare

public double getMemoryShare()
Returns the share of heap size which can be used before emptying the cache. 0.99 (default) means that objects will be removed from the cache as long as more than 99% of the heap size is used.

Returns:
the share of heap size which can be used before emptying the cache

setMemoryShare

public void setMemoryShare(double memoryShare)
Sets the share of heap size which can be used before emptying the cache.

Parameters:
memoryShare - the share of heap size which can be used before emptying the cache

addDb

public void addDb(ObjectsDB objectsDB)
Adds a database in the list of the databases handled by the cache. If a database with the same name is already present it will be silently replaced.

Parameters:
objectsDB - the objects database

removeObject

public void removeObject(String dbName,
                         String tableName,
                         String objectKey)
Removes an object from the cache mappings.

Parameters:
dbName - the name of the database
tableName - the name of the table
objectKey - the key of the object

getObject

public Object getObject(String dbName,
                        String tableName,
                        String objectKey)
Returns the objects if present in the cache. Null if not.

Parameters:
dbName - the name of the database
tableName - the name of the table
objectKey - the key of the object
Returns:
the object of interest, null if not present in the cache

updateObject

public boolean updateObject(String dbName,
                            String tableName,
                            String objectKey,
                            Object object)
Sets that a match has been modified and returns true in case of success.

Parameters:
dbName - the name of the database
tableName - the name of the table
objectKey - the key of the object
object -
Returns:
returns a boolean indicating that the entry was in cache and has been updated. False otherwise.

addObject

public void addObject(String dbName,
                      String tableName,
                      String objectKey,
                      Object object,
                      boolean modifiedOrNew)
               throws IOException,
                      SQLException
Adds an object to the cache. The object must not necessarily be in the database. If an object is already present with the same identifiers, it will be silently erased.

Parameters:
dbName - the name of the database
tableName - the name of the table
objectKey - the key of the object
object - the object to store in the cache
modifiedOrNew - true if the object is modified or new
Throws:
IOException
SQLException

memoryCheck

public boolean memoryCheck()
Indicates whether the memory used by the application is lower than 99% of the heap size.

Returns:
a boolean indicating whether the memory used by the application is lower than 99% of the heap

saveObjects

public void saveObjects(ArrayList<String> entryKeys)
                 throws IOException,
                        SQLException
Saves an entry in the database if modified and clears it from the cache.

Parameters:
entryKeys - the keys of the entries
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

saveObjects

public void saveObjects(ArrayList<String> entryKeys,
                        WaitingHandler waitingHandler)
                 throws IOException,
                        SQLException
Saves an entry in the database if modified and clears it from the cache.

Parameters:
entryKeys - the keys of the entries
waitingHandler - a waiting handler displaying progress to the user. Can be null. Progress will be displayed as secondary.
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

saveObjects

public void saveObjects(ArrayList<String> entryKeys,
                        WaitingHandler waitingHandler,
                        boolean clearEntries)
                 throws IOException,
                        SQLException
Saves an entry in the database if modified.

Parameters:
entryKeys - the keys of the entries
waitingHandler - a waiting handler displaying progress to the user. Can be null. Progress will be displayed as secondary.
clearEntries - a boolean indicating whether the entry shall be cleared from the cache
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

saveObject

public void saveObject(String entryKey)
                throws IOException,
                       SQLException
Saves an entry in the database if modified and clears it from the cache. Batch saving should be used instead when possible in order to limit the interactions with the database. See method saveObjects

Parameters:
entryKey - the key of the entry
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

saveObject

public void saveObject(String entryKey,
                       boolean clearEntry)
                throws IOException,
                       SQLException
Saves an entry in the database if modified. Batch saving should be used instead in order to limit the interactions with the database. See method saveObjects

Parameters:
entryKey - the key of the entry
clearEntry - a boolean indicating whether the entry shall be cleared from the cache
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

updateCache

public void updateCache()
                 throws IOException,
                        SQLException
Updates the cache according to the memory settings.

Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

reduceMemoryConsumption

public void reduceMemoryConsumption(double share,
                                    WaitingHandler waitingHandler)
                             throws IOException,
                                    SQLException
Reduces the memory consumption by saving the given share of hits.

Parameters:
share - the share to be saved, 0.25 means that 25% of the hits will be saved
waitingHandler - a waiting handler on which the progress will be displayed as secondary progress. can be null
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object

inCache

public boolean inCache(String dbName,
                       String tableName,
                       String objectKey)
Indicates whether an object is loaded in the cache

Parameters:
dbName - the database name
tableName - the table name
objectKey - the object key
Returns:
a boolean indicating whether an object is loaded in the cache

saveCache

public void saveCache(WaitingHandler waitingHandler,
                      boolean emptyCache)
               throws IOException,
                      SQLException
Saves the cache content in the database.

Parameters:
waitingHandler - a waiting handler on which the progress will be
emptyCache - boolean indicating whether the cache content shall be cleared while saving displayed as secondary progress. can be null
Throws:
SQLException - exception thrown whenever an error occurred while adding the object in the database
IOException - exception thrown whenever an error occurred while writing the object


Copyright © 2012. All Rights Reserved.