Package com.compomics.util.db.object
Class ObjectsCache
java.lang.Object
com.compomics.util.db.object.ObjectsCache
An object cache can be combined to an ObjectDB to improve its performance. A
single cache can be used by different databases. This ought 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, Dominik Kopczynski, Harald Barsnes
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an object to the cache.voidAdds an object to the cache.voidaddObjects(HashMap<Long, Object> objects) Adds objects to the cache.voidaddObjects(HashMap<Long, Object> objects, boolean inDB, boolean edited) Adds objects to the cache.voidClears the cache.intReturns the cache size in number of objects.getClassInCache(Class className) Returns the class type of the objects in cache.doubleReturns the share of heap size which can be used before emptying the cache.getObject(long objectKey) Returns the object if present in the cache.booleaninCache(long longKey) Checks if a given key is in the cache.booleanisEmpty()Indicates whether the cache is empty.voidremoveObject(long objectKey) Removes an object from the cache.voidsaveCache(WaitingHandler waitingHandler, boolean emptyCache) Saves the cache content to the database.voidsaveObjects(int numLastEntries) Saves entries in the database if modified and clears it from the cache.voidsaveObjects(int numLastEntries, WaitingHandler waitingHandler, boolean clearEntries) Saves entries in the database if modified.voidsetMemoryShare(double memoryShare) Sets the share of heap size which can be used before emptying the cache.voidsetReadOnly(boolean readOnly) Sets the cache in read only mode.
-
Constructor Details
-
ObjectsCache
Constructor.- Parameters:
objectsDB- the object database
-
-
Method Details
-
getCacheSize
public int getCacheSize()Returns the cache size in number of objects.- Returns:
- the cache size in number of objects
-
getObject
Returns the object if present in the cache. Null if not.- Parameters:
objectKey- the key of the object- Returns:
- the object of interest, null if not present in the cache
-
removeObject
public void removeObject(long objectKey) Removes an object from the cache.- Parameters:
objectKey- the key of the object
-
addObject
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 overwritten.- Parameters:
objectKey- the key of the objectobject- the object to store in the cache
-
addObject
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 overwritten.- Parameters:
objectKey- the key of the objectobject- the object to store in the cacheinDB- the database stateedited- the edited state
-
addObjects
Adds objects to the cache. The objects must not necessarily be in the database. If an object is already present with the same identifiers, it will be silently overwritten.- Parameters:
objects- the key / objects to store in the cache
-
addObjects
Adds objects to the cache. The objects must not necessarily be in the database. If an object is already present with the same identifiers, it will be silently overwritten.- Parameters:
objects- the key / objects to store in the cacheinDB- their database stateedited- their editing state
-
saveObjects
public void saveObjects(int numLastEntries) Saves entries in the database if modified and clears it from the cache.- Parameters:
numLastEntries- number of keys of the entries
-
saveObjects
Saves entries in the database if modified.- Parameters:
numLastEntries- number of keys of the entrieswaitingHandler- 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
-
inCache
public boolean inCache(long longKey) Checks if a given key is in the cache.- Parameters:
longKey- key of the entry- Returns:
- if key in cache
-
saveCache
Saves the cache content to the database.- Parameters:
waitingHandler- a waiting handler on which the progress will be displayedemptyCache- boolean indicating whether the cache content shall be cleared while saving displayed as secondary progress. Can be null.
-
isEmpty
public boolean isEmpty()Indicates whether the cache is empty.- Returns:
- a boolean indicating whether the cache is empty
-
clearCache
public void clearCache()Clears the cache. -
setReadOnly
public void setReadOnly(boolean readOnly) Sets the cache in read only mode.- Parameters:
readOnly- boolean indicating whether the cache should be in read only mode
-
getClassInCache
Returns the class type of the objects in cache.- Parameters:
className- the class name- Returns:
- the class type of the objects in cache
-