Class DbCache<T extends DbObject>

java.lang.Object
ghidra.program.database.DbCache<T>
Type Parameters:
T - the type of the database object
Direct Known Subclasses:
CodeUnitCache

public class DbCache<T extends DbObject> extends Object
A DbObject cache that efficiently manages the use of the database lock and uses a factory to create objects in the cache when they are not present.

This version of the cache is designed to be used without first acquiring a database lock. It will first attempt to retrieve the object without acquiring the lock. If that fails, it will acquire the lock, attempt to refresh the object if necessary or possibly create a new instance using the factory and add it to the cache.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final DbCache<?>
     
    protected DbFactory<T>
     
    static final int
     
    protected Lock
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    DbCache(DbFactory<T> factory, Lock lock, int hardCacheSize)
    Constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    add(T dbObject)
    Adds the given database object to the cache.
    void
    delete(long key)
    Removes the object with the given key from the cache.
    void
    delete(List<KeyRange> keyRanges)
    Delete all objects from HashMap whose key is contained within the specified keyRanges.
    Retrieves the database object with the given record and associated key from the cache.
    Retrieves the database object with the given key from the cache.
    Returns an List of all the cached objects.
    Retrieves the object from the cache, but only if it already exists and is valid in the cache.
    int
    Get the current cache modification counter value which corresponds to the number of times the entire cache has been invalidated.
    getRaw(Long key)
    Retrieves the object with the given key from the cache with checking if it is valid or needs to be refreshed.
    void
    Marks all the cached objects as invalid.
    void
    keyChanged(long oldKey, long newKey)
    Updates the cache for an object whose key has changed.
    int
    Returns the number of objects currently in the cache.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • DbCache

      public DbCache(DbFactory<T> factory, Lock lock, int hardCacheSize)
      Constructor
      Parameters:
      factory - the factory for creating new instances of database objects
      lock - the database lock.
      hardCacheSize - the size of the hard reference cache
  • Method Details

    • size

      public int size()
      Returns the number of objects currently in the cache. A database lock is not required.
      Returns:
      the number of objects currently in the cache.
    • add

      public T add(T dbObject)
      Adds the given database object to the cache.
      Parameters:
      dbObject - the object to add to the cache.
      Returns:
      the object that was cached
    • getCachedInstance

      public T getCachedInstance(Long key)
      Retrieves the database object with the given key from the cache. This differs from the super's get() method in that it does not require that the database lock be acquired prior to calling this method and in fact performs better if the lock is not acquired. This version will first look in the cache and if it finds one that is definitely valid (there is a quick check that doesn't require the lock), it returns it immediately. Otherwise, the cache doesn't have the object or the object needs refreshing. In this case, this method will acquire the lock, attempt refresh the object if it exists, and if necessary call a factory method to create a new instance and add it to the cache.
      Parameters:
      key - the key of the object to retrieve.
      Returns:
      the cached object or null if the object with that key is not currently cached.
    • getIfValid

      public T getIfValid(Long key)
      Retrieves the object from the cache, but only if it already exists and is valid in the cache. It will not attempt to refresh the object or call the factory method to create new instances.
      Parameters:
      key - the key of the object to retrieve.
      Returns:
      the cached object or null if the object with that key is not currently cached and valid.
    • getRaw

      public T getRaw(Long key)
      Retrieves the object with the given key from the cache with checking if it is valid or needs to be refreshed. It will not use the factory to create new instances if they don't exist. This method is used in very specialized situations where the caller has already done checking and they know the status of the object and how to refresh it cheaply if needed.
      Parameters:
      key - the key of the object to retrieve
      Returns:
      the object directly from the cache if one exists in the cache.
    • getCachedInstance

      public T getCachedInstance(DBRecord dbRecord)
      Retrieves the database object with the given record and associated key from the cache. This form should be used in conjunction with record iterators to avoid unnecessary record query during a possible object refresh. To benefit from the record the cached object must implement the DbObject.refresh(DBRecord) method which by default ignores the record and simply calls DbObject.refresh(DBRecord).

      This method is similar to the get() method in that it can be called without the database lock. It will first check if the object is in the cache and definitely valid before acquiring the lock and refreshing or creating the database object.

      Parameters:
      dbRecord - the valid record corresponding to the object to be retrieved and possibly used to refresh the associated object if found in cache
      Returns:
      the cached object or null if the object with that key is not currently cached.
    • getCachedObjects

      public List<T> getCachedObjects()
      Returns an List of all the cached objects. These objects have not been checked to see if they are valid or not.
      Returns:
      an List of all the cached objects.
    • delete

      public void delete(List<KeyRange> keyRanges)
      Delete all objects from HashMap whose key is contained within the specified keyRanges.
      Parameters:
      keyRanges - key ranges to delete
    • invalidate

      public void invalidate()
      Marks all the cached objects as invalid. Invalid objects will have to refresh themselves before they are allowed to be used. If an invalidated object cannot refresh itself, then the object is removed from the cache and discarded and the application can no longer use that instance of the object.
    • delete

      public void delete(long key)
      Removes the object with the given key from the cache. A database lock is not required.
      Parameters:
      key - the key of the object to remove.
    • keyChanged

      public void keyChanged(long oldKey, long newKey)
      Updates the cache for an object whose key has changed.
      Parameters:
      oldKey - the old key
      newKey - the new key
    • getModificationCount

      public int getModificationCount()
      Get the current cache modification counter value which corresponds to the number of times the entire cache has been invalidated.
      Returns:
      the current modification counter value