Package org.apache.openjpa.datacache
Class DelegatingDataCache
- java.lang.Object
-
- org.apache.openjpa.datacache.DelegatingDataCache
-
public class DelegatingDataCache extends java.lang.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
-
-
Field Summary
-
Fields inherited from interface org.apache.openjpa.datacache.DataCache
NAME_DEFAULT
-
-
Constructor Summary
Constructors Constructor Description DelegatingDataCache(DataCache cache)
Constructor.DelegatingDataCache(DataCache cache, RuntimeExceptionTranslator trans)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addExpirationListener(ExpirationListener listen)
Add a new expiration event listener to this cache.void
clear()
Remove all data from this cache.void
close()
Free the resources used by this cache.void
commit(java.util.Collection<DataCachePCData> additions, java.util.Collection<DataCachePCData> newUpdates, java.util.Collection<DataCachePCData> existingUpdates, java.util.Collection<java.lang.Object> deletes)
Perform a batch update of the cache.boolean
contains(java.lang.Object oid)
Returnstrue
if this cache contains data corresponding tooid
; otherwise returnsfalse
.java.util.BitSet
containsAll(java.util.Collection<java.lang.Object> oids)
Returns the indexes of the oids in this cache.boolean
equals(java.lang.Object other)
DataCachePCData
get(java.lang.Object oid)
Return the cached object for the given oid.java.util.Map<java.lang.Object,DataCachePCData>
getAll(java.util.List<java.lang.Object> keys)
Gets objects from the caches for a given list of keys.DataCache
getDelegate()
Return the direct delegate.boolean
getEvictOnBulkUpdate()
Returns whether the the cache needs to be updated when bulk updates as executed.DataCache
getInnermostDelegate()
Return the native delegate.java.lang.String
getName()
Returns a string name that can be used by end-user-visible code to identify this cache.DataCache
getPartition(java.lang.String name, boolean create)
Gets the named partition.java.util.Set<java.lang.String>
getPartitionNames()
Gets the name of the known partitions.CacheStatistics
getStatistics()
Returns number of read/write request and cache hit ratio data.int
hashCode()
void
initialize(DataCacheManager manager)
Initialize any resources associated with the givenDataCacheManager
.boolean
isPartitioned()
Affirms if this cache maintains partitions.boolean
pin(java.lang.Object oid)
Pin the value stored underoid
into the cache.void
pinAll(java.lang.Class<?> cls, boolean subs)
Pin all oids for the given type.java.util.BitSet
pinAll(java.util.Collection<java.lang.Object> oids)
Pin all oids to the cache.DataCachePCData
put(DataCachePCData value)
Set the cached value for the given instance.DataCachePCData
remove(java.lang.Object oid)
Remove the value stored under the given oid.void
removeAll(java.lang.Class<?> cls, boolean subclasses)
Evict all values of a specified type.java.util.BitSet
removeAll(java.util.Collection<java.lang.Object> oids)
Remove the values stored under the given oids.boolean
removeExpirationListener(ExpirationListener listen)
Remove an expiration event listener from this cache.void
setName(java.lang.String name)
Sets a string name to be used to identify this cache to end-user needs.protected java.lang.RuntimeException
translate(java.lang.RuntimeException re)
Translate the OpenJPA exception.boolean
unpin(java.lang.Object oid)
Unpin the value stored underoid
from the cache.void
unpinAll(java.lang.Class<?> cls, boolean subs)
Unpin all oids associaed with the given type from the cache.java.util.BitSet
unpinAll(java.util.Collection<java.lang.Object> oids)
Unpin all oids from the cache.void
update(DataCachePCData value)
Update the cached value for the given instance.void
writeLock()
Obtain a write lock on the cache.void
writeUnlock()
Release the write lock on the cache.
-
-
-
Constructor Detail
-
DelegatingDataCache
public DelegatingDataCache(DataCache cache)
Constructor. Supply delegate.
-
DelegatingDataCache
public DelegatingDataCache(DataCache cache, RuntimeExceptionTranslator trans)
-
-
Method Detail
-
getDelegate
public DataCache getDelegate()
Return the direct delegate.
-
getInnermostDelegate
public DataCache getInnermostDelegate()
Return the native delegate.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
translate
protected java.lang.RuntimeException translate(java.lang.RuntimeException re)
Translate the OpenJPA exception.
-
getName
public java.lang.String getName()
Description copied from interface:DataCache
Returns a string name that can be used by end-user-visible code to identify this cache.
-
setName
public void setName(java.lang.String name)
Description copied from interface:DataCache
Sets a string name to be used to identify this cache to end-user needs.
-
initialize
public void initialize(DataCacheManager manager)
Description copied from interface:DataCache
Initialize any resources associated with the givenDataCacheManager
.- Specified by:
initialize
in interfaceDataCache
-
commit
public void commit(java.util.Collection<DataCachePCData> additions, java.util.Collection<DataCachePCData> newUpdates, java.util.Collection<DataCachePCData> existingUpdates, java.util.Collection<java.lang.Object> deletes)
Description copied from interface:DataCache
Perform a batch update of the cache. Add allDataCachePCData
objects inadditions
and innewUpdates
, make the appropriate modifications to all DataCachePCDatas inexistingUpdates
, and delete all OIDs indeletes
. 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 thedeletes
collection may contain oids that are also in theadditions
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 interfaceDataCache
- Parameters:
additions
- A collection ofDataCachePCData
objects. These represent data that have been newly created, and thus must be added to the cache.newUpdates
- A collection ofDataCachePCData
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 ofDataCachePCData
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 fromDataCache.get(java.lang.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(java.lang.Object oid)
Description copied from interface:DataCache
Returnstrue
if this cache contains data corresponding tooid
; otherwise returnsfalse
.
-
containsAll
public java.util.BitSet containsAll(java.util.Collection<java.lang.Object> oids)
Description copied from interface:DataCache
Returns the indexes of the oids in this cache.- Specified by:
containsAll
in interfaceDataCache
-
get
public DataCachePCData get(java.lang.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; theDataCache.update(org.apache.openjpa.datacache.DataCachePCData)
method should be used to re-cache any changed objects.
-
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 theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)
method.
-
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 theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)
method. A cache implementation may or may not return a live object fromDataCache.get(java.lang.Object)
invocations. If an object retrieved from aDataCache.get(java.lang.Object)
operation needs to be updated, this method can be invoked instead of invokingDataCache.put(org.apache.openjpa.datacache.DataCachePCData)
. The DataCache implementation can then make optimizations based on how itsDataCache.get(java.lang.Object)
method works.
-
remove
public DataCachePCData remove(java.lang.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 theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)
method.
-
removeAll
public java.util.BitSet removeAll(java.util.Collection<java.lang.Object> oids)
Description copied from interface:DataCache
Remove the values stored under the given oids.- Specified by:
removeAll
in interfaceDataCache
- Returns:
- the indexes of the removed oids
- See Also:
DataCache.remove(java.lang.Object)
-
removeAll
public void removeAll(java.lang.Class<?> cls, boolean subclasses)
Description copied from interface:DataCache
Evict all values of a specified type.
-
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 theDataCache.commit(java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<org.apache.openjpa.datacache.DataCachePCData>, java.util.Collection<java.lang.Object>)
method.
-
pin
public boolean pin(java.lang.Object oid)
Description copied from interface:DataCache
Pin the value stored underoid
into the cache. This method guarantees thatoid
's value will not be dropped by the caching algorithm. This method does not affect the behavior ofDataCache.remove(java.lang.Object)
.
-
pinAll
public java.util.BitSet pinAll(java.util.Collection<java.lang.Object> oids)
Description copied from interface:DataCache
Pin all oids to the cache.- Specified by:
pinAll
in interfaceDataCache
- Returns:
- the indexes of the pinned oids
- See Also:
DataCache.pin(java.lang.Object)
-
pinAll
public void pinAll(java.lang.Class<?> cls, boolean subs)
Description copied from interface:DataCache
Pin all oids for the given type.
-
unpin
public boolean unpin(java.lang.Object oid)
Description copied from interface:DataCache
Unpin the value stored underoid
from the cache. This method reverses a previous invocation ofDataCache.pin(java.lang.Object)
. This method does not remove anything from the cache; it merely makesoid
's value a candidate for flushing from the cache.
-
unpinAll
public java.util.BitSet unpinAll(java.util.Collection<java.lang.Object> oids)
Description copied from interface:DataCache
Unpin all oids from the cache.- Specified by:
unpinAll
in interfaceDataCache
- Returns:
- the indexes of the unpinned oids
- See Also:
DataCache.unpin(java.lang.Object)
-
unpinAll
public void unpinAll(java.lang.Class<?> cls, boolean subs)
Description copied from interface:DataCache
Unpin all oids associaed with the given type from the cache.
-
writeLock
public void writeLock()
Description copied from interface:DataCache
Obtain a write lock on the cache.
-
writeUnlock
public void writeUnlock()
Description copied from interface:DataCache
Release the write lock on the cache.- Specified by:
writeUnlock
in interfaceDataCache
-
addExpirationListener
public void addExpirationListener(ExpirationListener listen)
Description copied from interface:DataCache
Add a new expiration event listener to this cache.- Specified by:
addExpirationListener
in interfaceDataCache
-
removeExpirationListener
public boolean removeExpirationListener(ExpirationListener listen)
Description copied from interface:DataCache
Remove an expiration event listener from this cache.- Specified by:
removeExpirationListener
in interfaceDataCache
-
close
public void close()
Description copied from interface:DataCache
Free the resources used by this cache.
-
getAll
public java.util.Map<java.lang.Object,DataCachePCData> getAll(java.util.List<java.lang.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.
-
getStatistics
public CacheStatistics getStatistics()
Description copied from interface:DataCache
Returns number of read/write request and cache hit ratio data.- Specified by:
getStatistics
in interfaceDataCache
-
getPartition
public DataCache getPartition(java.lang.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 interfaceDataCache
- 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 java.util.Set<java.lang.String> getPartitionNames()
Description copied from interface:DataCache
Gets the name of the known partitions.- Specified by:
getPartitionNames
in interfaceDataCache
- 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 interfaceDataCache
-
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 interfaceDataCache
-
-