Package org.apache.openjpa.datacache
Class AbstractQueryCache
- java.lang.Object
-
- org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
-
- org.apache.openjpa.datacache.AbstractQueryCache
-
- All Implemented Interfaces:
java.io.Serializable,QueryCache,TypesChangedListener,Configurable,Closeable,EventManager
- Direct Known Subclasses:
ConcurrentQueryCache
public abstract class AbstractQueryCache extends AbstractConcurrentEventManager implements QueryCache, Configurable
AbstractQueryCacheimplementation that provides various statistics, logging, and timeout functionality common across cache implementations.- Author:
- Patrick Linskey, Abe White
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractQueryCache.Default<T>A default implementation of query statistics for the Query result cache.static classAbstractQueryCache.EvictPolicy
-
Field Summary
Fields Modifier and Type Field Description protected OpenJPAConfigurationconfThe configuration set by the system.protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Long>entityTimestampMapAbstractQueryCache.EvictPolicyevictPolicyprotected LoglogThe log to use.-
Fields inherited from class org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
_listeners
-
-
Constructor Summary
Constructors Constructor Description AbstractQueryCache()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddTypesChangedListener(TypesChangedListener listen)Add a new types event listener to this cache.voidafterCommit(RemoteCommitEvent event)This method is part of theRemoteCommitListenerinterface.voidclear()Remove all data from this cache.protected abstract voidclearInternal()Clear the cache.voidclose()Free the resources used by this cache.protected voidclose(boolean clear)intcount()voidendConfiguration()Invoked upon completion of bean property configuration for this object.protected voidfireEvent(java.lang.Object event, java.lang.Object listener)Implement this method to fire the given event to the given listener.QueryResultget(QueryKey key)Return a list of oids for the given query key.java.util.List<java.lang.Long>getAllEntityTimestamp(java.util.List<java.lang.String> keyList)Returns a list of timestamps in the form of Long objects which are the last updated time stamps for the given entities in the keylist.booleangetEnableStatistics()AbstractQueryCache.EvictPolicygetEvictPolicy()Returns the evictionPolicy for QueryCacheprotected abstract QueryResultgetInternal(QueryKey qk)Return the list for the given key.java.lang.StringgetName()QueryStatistics<QueryKey>getStatistics()Gets the simple statistics for query results.voidinitialize(DataCacheManager manager)Initialize any resources associated with the givenDataCacheManager.booleanisClosed()protected abstract java.util.CollectionkeySet()Return a threadsafe view of the keys in this cache.protected java.util.CollectionnewListenerCollection()Individual query results will be registered as types changed listeners.voidonTypesChanged(TypesChangedEvent ev)Notification that instances of persistent types have changed.booleanpin(QueryKey key)Pin the value stored underqkinto the cache.protected abstract booleanpinInternal(QueryKey qk)Pin an object to the cache.QueryResultput(QueryKey qk, QueryResult oids)Set the list of OIDs for the given query key.protected abstract QueryResultputInternal(QueryKey qk, QueryResult oids)Add the given result to the cache, returning the old result under the given key.QueryResultremove(QueryKey key)Remove the value stored under the given query key.protected voidremoveAllInternal(java.util.Collection qks)Remove all results under the given keys from the cache.protected abstract QueryResultremoveInternal(QueryKey qk)Remove the result under the given key from the cache.booleanremoveTypesChangedListener(TypesChangedListener listen)Remove an types event listener from this cache.voidsetConfiguration(Configuration conf)Invoked prior to setting bean properties.voidsetEnableStatistics(boolean enable)voidsetEvictPolicy(java.lang.String evictPolicy)Sets the eviction policy for the query cachevoidsetName(java.lang.String n)voidstartConfiguration()Invoked before bean property configuration is begun on this object.booleanunpin(QueryKey key)Unpin the value stored underkeyinto the cache.protected abstract booleanunpinInternal(QueryKey qk)Unpin an object from the cache.protected voidupdateEntityTimestamp(java.util.Map<java.lang.String,java.lang.Long> timestampMap)Updates the entity timestamp map with the current time in milliseconds-
Methods inherited from class org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager
addListener, fireEvent, getListeners, hasListener, hasListeners, isFailFast, removeListener, setFailFast
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.openjpa.datacache.QueryCache
writeLock, writeUnlock
-
-
-
-
Field Detail
-
conf
protected OpenJPAConfiguration conf
The configuration set by the system.
-
log
protected Log log
The log to use.
-
entityTimestampMap
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Long> entityTimestampMap
-
evictPolicy
public AbstractQueryCache.EvictPolicy evictPolicy
-
-
Method Detail
-
setEnableStatistics
public void setEnableStatistics(boolean enable)
-
getEnableStatistics
public boolean getEnableStatistics()
-
getStatistics
public QueryStatistics<QueryKey> getStatistics()
Description copied from interface:QueryCacheGets the simple statistics for query results. If the statistics gathering is disabled, an empty statistics is returned.- Specified by:
getStatisticsin interfaceQueryCache
-
initialize
public void initialize(DataCacheManager manager)
Description copied from interface:QueryCacheInitialize any resources associated with the givenDataCacheManager.- Specified by:
initializein interfaceQueryCache
-
onTypesChanged
public void onTypesChanged(TypesChangedEvent ev)
Description copied from interface:TypesChangedListenerNotification that instances of persistent types have changed.- Specified by:
onTypesChangedin interfaceTypesChangedListener
-
get
public QueryResult get(QueryKey key)
Description copied from interface:QueryCacheReturn a list of oids for the given query key. This is an unmodifiable list.- Specified by:
getin interfaceQueryCache- Returns:
- The query results matching the given key, or null if none
-
put
public QueryResult put(QueryKey qk, QueryResult oids)
Description copied from interface:QueryCacheSet the list of OIDs for the given query key. A reference to the given list will be stored in the query cache, so the list should not be modified after invoking this method.- Specified by:
putin interfaceQueryCache- Returns:
- The previously cached value, or
nullif the key was not previously cached. SeeMap.put(K, V)for more information.
-
remove
public QueryResult remove(QueryKey key)
Description copied from interface:QueryCacheRemove the value stored under the given query key. This method is typically not invoked directly from outside theQueryCacheclass. Instead, the cache should be updated by implementingRemoteCommitListener, which will result in all queries that may be invalid being dropped.- Specified by:
removein interfaceQueryCache- Returns:
- The previously cached value, or
nullif the key was not previously cached. SeeMap.remove(java.lang.Object)for more information.
-
pin
public boolean pin(QueryKey key)
Description copied from interface:QueryCachePin the value stored underqkinto the cache. This method guarantees thatqk's value will not be expired if the cache exceeds its capacity. It causes this data to be ignored when determining whether or not the cache is full, effectively increasing the total amount of data stored in the cache. This method does not affect the behavior ofQueryCache.remove(org.apache.openjpa.datacache.QueryKey)orTypesChangedListener.onTypesChanged(org.apache.openjpa.datacache.TypesChangedEvent).- Specified by:
pinin interfaceQueryCache- Returns:
trueifkey's value was pinned into the cache;falseif the key is not in the cache.
-
unpin
public boolean unpin(QueryKey key)
Description copied from interface:QueryCacheUnpin the value stored underkeyinto the cache. This method reverses a previous invocation ofQueryCache.pin(org.apache.openjpa.datacache.QueryKey). This method does not remove anything from the cache; it merely makeskey's value a candidate for flushing from the cache.- Specified by:
unpinin interfaceQueryCache- Returns:
trueifkey's value was unpinned from the cache;falseif the key is not in the cache.
-
clear
public void clear()
Description copied from interface:QueryCacheRemove all data from this cache.- Specified by:
clearin interfaceQueryCache
-
close
public void close()
Description copied from interface:QueryCacheFree the resources used by this cache.- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceQueryCache
-
close
protected void close(boolean clear)
-
isClosed
public boolean isClosed()
-
addTypesChangedListener
public void addTypesChangedListener(TypesChangedListener listen)
Description copied from interface:QueryCacheAdd a new types event listener to this cache.- Specified by:
addTypesChangedListenerin interfaceQueryCache
-
removeTypesChangedListener
public boolean removeTypesChangedListener(TypesChangedListener listen)
Description copied from interface:QueryCacheRemove an types event listener from this cache.- Specified by:
removeTypesChangedListenerin interfaceQueryCache
-
afterCommit
public void afterCommit(RemoteCommitEvent event)
This method is part of theRemoteCommitListenerinterface. If your cache subclass relies on OpenJPA for clustering support, make it implementRemoteCommitListener. This method will take care of invalidating entries from remote commits, by delegating toonTypesChanged(org.apache.openjpa.datacache.TypesChangedEvent).
-
keySet
protected abstract java.util.Collection keySet()
Return a threadsafe view of the keys in this cache. This collection must be iterable without risk of concurrent modification exceptions. It does not have to implement contains() efficiently or use set semantics.
-
getInternal
protected abstract QueryResult getInternal(QueryKey qk)
Return the list for the given key.
-
putInternal
protected abstract QueryResult putInternal(QueryKey qk, QueryResult oids)
Add the given result to the cache, returning the old result under the given key.
-
removeInternal
protected abstract QueryResult removeInternal(QueryKey qk)
Remove the result under the given key from the cache.
-
removeAllInternal
protected void removeAllInternal(java.util.Collection qks)
Remove all results under the given keys from the cache.
-
clearInternal
protected abstract void clearInternal()
Clear the cache.
-
pinInternal
protected abstract boolean pinInternal(QueryKey qk)
Pin an object to the cache.
-
unpinInternal
protected abstract boolean unpinInternal(QueryKey qk)
Unpin an object from the cache.
-
setConfiguration
public void setConfiguration(Configuration conf)
Description copied from interface:ConfigurableInvoked prior to setting bean properties.- Specified by:
setConfigurationin interfaceConfigurable
-
startConfiguration
public void startConfiguration()
Description copied from interface:ConfigurableInvoked before bean property configuration is begun on this object.- Specified by:
startConfigurationin interfaceConfigurable
-
endConfiguration
public void endConfiguration()
Description copied from interface:ConfigurableInvoked upon completion of bean property configuration for this object.- Specified by:
endConfigurationin interfaceConfigurable
-
fireEvent
protected void fireEvent(java.lang.Object event, java.lang.Object listener)Description copied from class:AbstractConcurrentEventManagerImplement this method to fire the given event to the given listener.- Specified by:
fireEventin classAbstractConcurrentEventManager
-
newListenerCollection
protected java.util.Collection newListenerCollection()
Individual query results will be registered as types changed listeners. We want such query results to be gc'd once the only reference is held by the list of expiration listeners.- Overrides:
newListenerCollectionin classAbstractConcurrentEventManager
-
setEvictPolicy
public void setEvictPolicy(java.lang.String evictPolicy)
Sets the eviction policy for the query cache- Parameters:
evictPolicy- -- String value that specifies the eviction policy
-
getEvictPolicy
public AbstractQueryCache.EvictPolicy getEvictPolicy()
Returns the evictionPolicy for QueryCache- Returns:
- -- returns a String value of evictPolicy attribute
-
updateEntityTimestamp
protected void updateEntityTimestamp(java.util.Map<java.lang.String,java.lang.Long> timestampMap)
Updates the entity timestamp map with the current time in milliseconds- Parameters:
timestampMap- -- a map that contains entityname and its last updated timestamp
-
getAllEntityTimestamp
public java.util.List<java.lang.Long> getAllEntityTimestamp(java.util.List<java.lang.String> keyList)
Returns a list of timestamps in the form of Long objects which are the last updated time stamps for the given entities in the keylist.- Parameters:
keyList- -- List of entity names- Returns:
- -- Returns a list that has the timestamp for the given entities
-
setName
public void setName(java.lang.String n)
-
getName
public java.lang.String getName()
-
count
public int count()
-
-