Class DelegatingDataCache

java.lang.Object
org.apache.openjpa.datacache.DelegatingDataCache
All Implemented Interfaces:
DataCache, Clearable, Closeable

public class DelegatingDataCache extends Object implements DataCache
Delegating data cache that can also perform exception translation for use in facades. This cache allows its delegate to be null, in which case it returns default values for all operations.
Author:
Abe White
  • Constructor Details

  • Method Details

    • getDelegate

      public DataCache getDelegate()
      Return the direct delegate.
    • getInnermostDelegate

      public DataCache getInnermostDelegate()
      Return the native delegate.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • translate

      protected RuntimeException translate(RuntimeException re)
      Translate the OpenJPA exception.
    • getName

      public String getName()
      Description copied from interface: DataCache
      Returns a string name that can be used by end-user-visible code to identify this cache.
      Specified by:
      getName in interface DataCache
    • setName

      public void setName(String name)
      Description copied from interface: DataCache
      Sets a string name to be used to identify this cache to end-user needs.
      Specified by:
      setName in interface DataCache
    • initialize

      public void initialize(DataCacheManager manager)
      Description copied from interface: DataCache
      Initialize any resources associated with the given DataCacheManager.
      Specified by:
      initialize in interface DataCache
    • commit

      public void commit(Collection<DataCachePCData> additions, Collection<DataCachePCData> newUpdates, Collection<DataCachePCData> existingUpdates, Collection<Object> deletes)
      Description copied from interface: DataCache
      Perform a batch update of the cache. Add all DataCachePCData objects in additions and in newUpdates, make the appropriate modifications to all DataCachePCDatas in existingUpdates, and delete all OIDs in deletes. All changes made to cached data must be made via this method. It is this method that is responsible for performing any side-effects that should happen on meaningful cache changes. Implementations should bear in mind that the deletes collection may contain oids that are also in the additions map. This is possible because it is valid for a user to delete an object with a particular oid and then add that object in the same batch.
      Specified by:
      commit in interface DataCache
      Parameters:
      additions - A collection of DataCachePCData objects. These represent data that have been newly created, and thus must be added to the cache.
      newUpdates - A collection of DataCachePCData objects. These represent data that have been modified but were not originally in the cache, and thus must be added to the cache.
      existingUpdates - A collection of DataCachePCData objects. These represent data that have been modified and were originally loaded from the cache. It is up to the cache implementation to decide if these values must be re-enlisted in the cache. Some caches may return live data from DataCache.get(Object) invocations, in which case these values need not be re-enlisted.
      deletes - A collection of object IDs that have been deleted and must therefore be dropped from the cache.
    • contains

      public boolean contains(Object oid)
      Description copied from interface: DataCache
      Returns true if this cache contains data corresponding to oid; otherwise returns false.
      Specified by:
      contains in interface DataCache
    • containsAll

      public BitSet containsAll(Collection<Object> oids)
      Description copied from interface: DataCache
      Returns the indexes of the oids in this cache.
      Specified by:
      containsAll in interface DataCache
    • get

      public DataCachePCData get(Object oid)
      Description copied from interface: DataCache
      Return the cached object for the given oid. Modifying the returned object may or may not change the cached value; the DataCache.update(DataCachePCData) method should be used to re-cache any changed objects.
      Specified by:
      get in interface DataCache
      Returns:
      the object matching the given oid, or null if none
    • put

      public DataCachePCData put(DataCachePCData value)
      Description copied from interface: DataCache
      Set the cached value for the given instance. This does not result in an update of other caches. Rather, it should only be used for loading clean data into the cache. Meaningful changes to the state of the cache should be made via the DataCache.commit(Collection, Collection, Collection, Collection) method.
      Specified by:
      put in interface DataCache
      Returns:
      The previously cached value, or null if the value was not previously cached. See Map.put(K, V) for more information.
    • update

      public void update(DataCachePCData value)
      Description copied from interface: DataCache
      Update the cached value for the given instance. This does not result in an update of other caches. Rather, it should only be used for loading clean data into the cache. Meaningful changes to the state of the cache should be made via the DataCache.commit(Collection, Collection, Collection, Collection) method. A cache implementation may or may not return a live object from DataCache.get(Object) invocations. If an object retrieved from a DataCache.get(Object) operation needs to be updated, this method can be invoked instead of invoking DataCache.put(DataCachePCData). The DataCache implementation can then make optimizations based on how its DataCache.get(Object) method works.
      Specified by:
      update in interface DataCache
    • remove

      public DataCachePCData remove(Object oid)
      Description copied from interface: DataCache
      Remove the value stored under the given oid. This does not result in an update of other caches. Rather, it should only be used for removing data in the cache. Meaningful changes to the state of the cache should be made via the DataCache.commit(Collection, Collection, Collection, Collection) method.
      Specified by:
      remove in interface DataCache
      Returns:
      The previously cached value, or null if the oid was not previously cached. See Map.remove(Object) for more information.
    • removeAll

      public BitSet removeAll(Collection<Object> oids)
      Description copied from interface: DataCache
      Remove the values stored under the given oids.
      Specified by:
      removeAll in interface DataCache
      Returns:
      the indexes of the removed oids
      See Also:
    • removeAll

      public void removeAll(Class<?> cls, boolean subclasses)
      Description copied from interface: DataCache
      Evict all values of a specified type.
      Specified by:
      removeAll in interface DataCache
    • clear

      public void clear()
      Description copied from interface: DataCache
      Remove all data from this cache. This does not result in an update of other caches. Rather, it should only be used for clearing the cache. Meaningful changes to the state of the cache should be made via the DataCache.commit(Collection, Collection, Collection, Collection) method.
      Specified by:
      clear in interface Clearable
      Specified by:
      clear in interface DataCache
    • pin

      public boolean pin(Object oid)
      Description copied from interface: DataCache
      Pin the value stored under oid into the cache. This method guarantees that oid's value will not be dropped by the caching algorithm. This method does not affect the behavior of DataCache.remove(Object).
      Specified by:
      pin in interface DataCache
      Returns:
      true if oid's value was pinned into the cache; false if the oid is not in the cache.
    • pinAll

      public BitSet pinAll(Collection<Object> oids)
      Description copied from interface: DataCache
      Pin all oids to the cache.
      Specified by:
      pinAll in interface DataCache
      Returns:
      the indexes of the pinned oids
      See Also:
    • pinAll

      public void pinAll(Class<?> cls, boolean subs)
      Description copied from interface: DataCache
      Pin all oids for the given type.
      Specified by:
      pinAll in interface DataCache
      Parameters:
      subs - Whether to include subclasses.
    • unpin

      public boolean unpin(Object oid)
      Description copied from interface: DataCache
      Unpin the value stored under oid from the cache. This method reverses a previous invocation of DataCache.pin(Object). This method does not remove anything from the cache; it merely makes oid's value a candidate for flushing from the cache.
      Specified by:
      unpin in interface DataCache
      Returns:
      true if oid's value was unpinned from the cache; false if the oid is not in the cache.
    • unpinAll

      public BitSet unpinAll(Collection<Object> oids)
      Description copied from interface: DataCache
      Unpin all oids from the cache.
      Specified by:
      unpinAll in interface DataCache
      Returns:
      the indexes of the unpinned oids
      See Also:
    • unpinAll

      public void unpinAll(Class<?> cls, boolean subs)
      Description copied from interface: DataCache
      Unpin all oids associaed with the given type from the cache.
      Specified by:
      unpinAll in interface DataCache
      Parameters:
      subs - Whether to include subclasses.
    • writeLock

      public void writeLock()
      Description copied from interface: DataCache
      Obtain a write lock on the cache.
      Specified by:
      writeLock in interface DataCache
    • writeUnlock

      public void writeUnlock()
      Description copied from interface: DataCache
      Release the write lock on the cache.
      Specified by:
      writeUnlock in interface DataCache
    • addExpirationListener

      public void addExpirationListener(ExpirationListener listen)
      Description copied from interface: DataCache
      Add a new expiration event listener to this cache.
      Specified by:
      addExpirationListener in interface DataCache
    • removeExpirationListener

      public boolean removeExpirationListener(ExpirationListener listen)
      Description copied from interface: DataCache
      Remove an expiration event listener from this cache.
      Specified by:
      removeExpirationListener in interface DataCache
    • close

      public void close()
      Description copied from interface: DataCache
      Free the resources used by this cache.
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface DataCache
    • getAll

      public Map<Object, DataCachePCData> getAll(List<Object> keys)
      Description copied from interface: DataCache
      Gets objects from the caches for a given list of keys. The returned map has the same keys as the given keys. If the cache does not contain data for a specific key, the returned map still contains the key with a null value.
      Specified by:
      getAll in interface DataCache
    • getStatistics

      public CacheStatistics getStatistics()
      Description copied from interface: DataCache
      Returns number of read/write request and cache hit ratio data.
      Specified by:
      getStatistics in interface DataCache
    • getPartition

      public DataCache getPartition(String name, boolean create)
      Description copied from interface: DataCache
      Gets the named partition. Note that a partition itself is another cache.
      Specified by:
      getPartition in interface DataCache
      Parameters:
      name - name of the given partition.
      create - if true optionally create a new partition.
      Returns:
      a partition of the given name. Or null, if either no such partition exists or can not be created.
    • getPartitionNames

      public Set<String> getPartitionNames()
      Description copied from interface: DataCache
      Gets the name of the known partitions.
      Specified by:
      getPartitionNames in interface DataCache
      Returns:
      empty set if no partition exists.
    • isPartitioned

      public boolean isPartitioned()
      Description copied from interface: DataCache
      Affirms if this cache maintains partitions.
      Specified by:
      isPartitioned in interface DataCache
    • getEvictOnBulkUpdate

      public boolean getEvictOnBulkUpdate()
      Description copied from interface: DataCache
      Returns whether the the cache needs to be updated when bulk updates as executed. Defaults to true.
      Specified by:
      getEvictOnBulkUpdate in interface DataCache