Package com.compomics.util.db.object
Class ObjectsCache
java.lang.Object
com.compomics.util.db.object.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 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 Modifier and Type Class Description class
ObjectsCache.ObjectsCacheElement
Objects cache element. -
Constructor Summary
Constructors Constructor Description ObjectsCache(ObjectsDB objectsDB)
Constructor. -
Method Summary
Modifier and Type Method Description void
addObject(long objectKey, Object object)
Adds an object to the cache.void
addObject(long objectKey, Object object, boolean inDB, boolean edited)
Adds an object to the cache.void
addObjects(HashMap<Long,Object> objects)
Adds an object to the cache.void
addObjects(HashMap<Long,Object> objects, boolean inDB, boolean edited)
Adds an object to the cache.void
clearCache()
Clears the cache.int
getCacheSize()
Returns the cache size in number of objects.HashSet<Long>
getClassInCache(Class className)
Returns the class type of the objects in cache.double
getMemoryShare()
Returns the share of heap size which can be used before emptying the cache.Object
getObject(long objectKey)
Returns the objects if present in the cache.boolean
inCache(long longKey)
Check if key in cache.boolean
isEmpty()
Indicates whether the cache is empty.void
removeObject(long objectKey)
Removes an object from the cache.void
saveCache(WaitingHandler waitingHandler, boolean emptyCache)
Saves the cache content in the database.void
saveObjects(int numLastEntries)
Saves an entry in the database if modified and clears it from the cache.void
saveObjects(int numLastEntries, WaitingHandler waitingHandler, boolean clearEntries)
Saves an entry in the database if modified.void
setMemoryShare(double memoryShare)
Sets the share of heap size which can be used before emptying the cache.void
setReadOnly(boolean readOnly)
Sets the cache in read only.
-
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
-
getMemoryShare
public double getMemoryShare()Returns the share of heap size which can be used before emptying the cache. 0.75 (default) means that objects will be removed from the cache as long as more than 75% 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
-
getObject
Returns the objects 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 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:
objects
- the key / objects to store in the cache
-
addObjects
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:
objects
- the key / objects to store in the cacheinDB
- their database stateedited
- their editing state
-
saveObjects
public void saveObjects(int numLastEntries)Saves an entry in the database if modified and clears it from the cache.- Parameters:
numLastEntries
- number of keys of the entries
-
saveObjects
Saves an entry 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)Check if key in cache.- Parameters:
longKey
- key of the entry- Returns:
- if key in cache
-
saveCache
Saves the cache content in 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.- Parameters:
readOnly
- boolean indicating whether the cache should be in read only
-
getClassInCache
Returns the class type of the objects in cache.- Parameters:
className
- the class name- Returns:
- the class type of the objects in cache
-