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
  • Constructor Details

    • ObjectsCache

      public ObjectsCache​(ObjectsDB objectsDB)
      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

      public Object getObject​(long objectKey)
      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

      public void addObject​(long objectKey, Object object)
      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 object
      object - the object to store in the cache
    • addObject

      public void addObject​(long objectKey, Object object, boolean inDB, boolean edited)
      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 object
      object - the object to store in the cache
      inDB - the database state
      edited - the edited state
    • addObjects

      public void addObjects​(HashMap<Long,​Object> objects)
      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

      public void addObjects​(HashMap<Long,​Object> objects, boolean inDB, boolean edited)
      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
      inDB - their database state
      edited - 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

      public void saveObjects​(int numLastEntries, WaitingHandler waitingHandler, boolean clearEntries)
      Saves an entry in the database if modified.
      Parameters:
      numLastEntries - number of 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
    • inCache

      public boolean inCache​(long longKey)
      Check if key in cache.
      Parameters:
      longKey - key of the entry
      Returns:
      if key in cache
    • saveCache

      public void saveCache​(WaitingHandler waitingHandler, boolean emptyCache)
      Saves the cache content in the database.
      Parameters:
      waitingHandler - a waiting handler on which the progress will be displayed
      emptyCache - 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

      public HashSet<Long> getClassInCache​(Class className)
      Returns the class type of the objects in cache.
      Parameters:
      className - the class name
      Returns:
      the class type of the objects in cache