Package org.apache.openjpa.jdbc.kernel
Class PreparedQueryCacheImpl
- java.lang.Object
-
- org.apache.openjpa.jdbc.kernel.PreparedQueryCacheImpl
-
- All Implemented Interfaces:
PreparedQueryCache
,Configurable
public class PreparedQueryCacheImpl extends java.lang.Object implements PreparedQueryCache
An implementation of the cache ofprepared queries
.- Since:
- 2.0.0
- Author:
- Pinaki Poddar
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PreparedQueryCacheImpl.StrongExclusion
Strong exclusion.static class
PreparedQueryCacheImpl.WeakExclusion
Weak exclusion.-
Nested classes/interfaces inherited from interface org.apache.openjpa.kernel.PreparedQueryCache
PreparedQueryCache.Exclusion
-
-
Constructor Summary
Constructors Constructor Description PreparedQueryCacheImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addExclusionPattern(java.lang.String pattern)
Adds a pattern for exclusion.boolean
cache(PreparedQuery q)
Cache the given query keyed by its identifier.void
clear()
Clears all cached queries.void
endConfiguration()
Invoked upon completion of bean property configuration for this object.PreparedQuery
get(java.lang.String id)
Get the PreparedQuery with the given identifier if it exists. null otherwise.int
getCacheSize()
boolean
getEnableStatistics()
Affirm if statistics is gathered.java.util.List<PreparedQueryCache.Exclusion>
getExcludes()
Gets the exclusion patterns.java.util.Map<java.lang.String,java.lang.String>
getMapView()
Get a map view of the cached queries indexed by identifier.QueryStatistics<java.lang.String>
getStatistics()
Gets the simple statistics for executed queries.PreparedQuery
initialize(java.lang.String key, java.lang.Object result)
Initialize the cached Prepared Query registered with the given key earlier by the given execution result.boolean
invalidate(java.lang.String id)
Remove the PreparedQuery with the given identifier from this cache.java.lang.Boolean
isCachable(java.lang.String id)
Affirms if a PreparedQuery can be cached against the given key.PreparedQueryCache.Exclusion
isExcluded(java.lang.String id)
Returns the exclusion status of if the given query key.protected void
lock(boolean readOnly)
Note: Care needs to be taken so that a read lock is never held while requesting a write lock.PreparedQuery
markUncachable(java.lang.String id, PreparedQueryCache.Exclusion exclusion)
Marks the given key as not amenable to caching.java.lang.Boolean
register(java.lang.String id, Query query, FetchConfiguration hints)
Register the given query for caching against the given key if it has not already been cached.void
removeExclusionPattern(java.lang.String pattern)
Removes a pattern for exclusion.void
setConfiguration(Configuration conf)
Invoked prior to setting bean properties.void
setEnableStatistics(boolean enable)
Enable/disable gathering of statistics.void
setExcludes(java.lang.String excludes)
Sets one or more exclusion regular expression patterns separated by semicolon.void
setMaxCacheSize(int size)
void
startConfiguration()
Invoked before bean property configuration is begun on this object.protected void
unlock(boolean readOnly)
-
-
-
Method Detail
-
register
public java.lang.Boolean register(java.lang.String id, Query query, FetchConfiguration hints)
Description copied from interface:PreparedQueryCache
Register the given query for caching against the given key if it has not already been cached. If the query can not be cached, then mark it as such to avoid computing for the same key again.- Specified by:
register
in interfacePreparedQueryCache
- Returns:
- TRUE the query is registered in the cache by this call null if the query is already registered in the cache FALSE if can not be registered in the cache, either because it is known not to be cacheable from a previous attempt or a hint is given to ignore the cached version.
-
getMapView
public java.util.Map<java.lang.String,java.lang.String> getMapView()
Description copied from interface:PreparedQueryCache
Get a map view of the cached queries indexed by identifier.- Specified by:
getMapView
in interfacePreparedQueryCache
-
cache
public boolean cache(PreparedQuery q)
Cache the given query keyed by its identifier. Does not cache if the identifier matches any exclusion pattern or has been marked as non-cachable. Also register the identifier as not cachable against the matched exclusion pattern.- Specified by:
cache
in interfacePreparedQueryCache
- Returns:
- true if the given query is cached. false if it can not be cached due to exclusion.
- See Also:
#markUncachable(String)
,PreparedQueryCache.setExcludes(String)
,PreparedQueryCache.addExclusionPattern(String)
-
initialize
public PreparedQuery initialize(java.lang.String key, java.lang.Object result)
Description copied from interface:PreparedQueryCache
Initialize the cached Prepared Query registered with the given key earlier by the given execution result. If it is not possible to initialize the Prepared Query from the given execution result, then the corresponding key will be marked as invalid for caching.- Specified by:
initialize
in interfacePreparedQueryCache
- Parameters:
key
- the key used during registrationresult
- an opaque instance carrying the execution result of the original query.- Returns:
- the initialized Prepared Query. If it is not possible to initialize the cached, possibly hollow Prepared Query from the given result, return null.
-
invalidate
public boolean invalidate(java.lang.String id)
Description copied from interface:PreparedQueryCache
Remove the PreparedQuery with the given identifier from this cache.- Specified by:
invalidate
in interfacePreparedQueryCache
-
get
public PreparedQuery get(java.lang.String id)
Description copied from interface:PreparedQueryCache
Get the PreparedQuery with the given identifier if it exists. null otherwise.- Specified by:
get
in interfacePreparedQueryCache
-
isCachable
public java.lang.Boolean isCachable(java.lang.String id)
Description copied from interface:PreparedQueryCache
Affirms if a PreparedQuery can be cached against the given key.- Specified by:
isCachable
in interfacePreparedQueryCache
- Returns:
- Boolean.FALSE if the given key is explicitly marked before as not be cached or matches any of the exclusion patterns. Boolean.TRUE if the given key currently exists in the cache. Otherwise, return null implying this receiver can not determine whether this key can be cached on not.
-
markUncachable
public PreparedQuery markUncachable(java.lang.String id, PreparedQueryCache.Exclusion exclusion)
Description copied from interface:PreparedQueryCache
Marks the given key as not amenable to caching. Explicit marking helps to avoid repeated computational cost of determining whether a query can be cached or not.- Specified by:
markUncachable
in interfacePreparedQueryCache
- Parameters:
id
- is the key to be excludedexclusion
- directs whether exclusion is irreversible or not.- Returns:
- The value for the given key if it had been cached before. null otherwise.
-
isExcluded
public PreparedQueryCache.Exclusion isExcluded(java.lang.String id)
Description copied from interface:PreparedQueryCache
Returns the exclusion status of if the given query key.- Specified by:
isExcluded
in interfacePreparedQueryCache
- Returns:
- null implies that the key is not excluded.
-
setExcludes
public void setExcludes(java.lang.String excludes)
Description copied from interface:PreparedQueryCache
Sets one or more exclusion regular expression patterns separated by semicolon. Any existing cache entry whose key matches any of the given pattern will be marked non-cachable in a reversible manner.- Specified by:
setExcludes
in interfacePreparedQueryCache
-
getExcludes
public java.util.List<PreparedQueryCache.Exclusion> getExcludes()
Description copied from interface:PreparedQueryCache
Gets the exclusion patterns.- Specified by:
getExcludes
in interfacePreparedQueryCache
-
addExclusionPattern
public void addExclusionPattern(java.lang.String pattern)
Adds a pattern for exclusion. Any query cached currently whose identifier matches the given pattern will be marked invalidated as a side-effect.- Specified by:
addExclusionPattern
in interfacePreparedQueryCache
-
removeExclusionPattern
public void removeExclusionPattern(java.lang.String pattern)
Removes a pattern for exclusion. Any query identifier marked as not cachable due to the given pattern will now be removed from the list of uncachables as a side-effect.- Specified by:
removeExclusionPattern
in interfacePreparedQueryCache
- See Also:
PreparedQueryCache.markUncachable(String, Exclusion)
-
getStatistics
public QueryStatistics<java.lang.String> getStatistics()
Description copied from interface:PreparedQueryCache
Gets the simple statistics for executed queries. If the statistics gathering is disabled, an empty statistics is returned.- Specified by:
getStatistics
in interfacePreparedQueryCache
-
lock
protected void lock(boolean readOnly)
Note: Care needs to be taken so that a read lock is never held while requesting a write lock. This will result in a deadlock.- Parameters:
readOnly
- - If true, a read lock will be acquired. Else a write lock will be acquired.
-
unlock
protected void unlock(boolean readOnly)
- Parameters:
readOnly
- - If true, the read lock will be released. Else a write lock will be released.
-
clear
public void clear()
Description copied from interface:PreparedQueryCache
Clears all cached queries.- Specified by:
clear
in interfacePreparedQueryCache
-
setEnableStatistics
public void setEnableStatistics(boolean enable)
Description copied from interface:PreparedQueryCache
Enable/disable gathering of statistics.- Specified by:
setEnableStatistics
in interfacePreparedQueryCache
-
getEnableStatistics
public boolean getEnableStatistics()
Description copied from interface:PreparedQueryCache
Affirm if statistics is gathered.- Specified by:
getEnableStatistics
in interfacePreparedQueryCache
-
setMaxCacheSize
public void setMaxCacheSize(int size)
-
getCacheSize
public int getCacheSize()
-
setConfiguration
public void setConfiguration(Configuration conf)
Description copied from interface:Configurable
Invoked prior to setting bean properties.- Specified by:
setConfiguration
in interfaceConfigurable
-
startConfiguration
public void startConfiguration()
Description copied from interface:Configurable
Invoked before bean property configuration is begun on this object.- Specified by:
startConfiguration
in interfaceConfigurable
-
endConfiguration
public void endConfiguration()
Description copied from interface:Configurable
Invoked upon completion of bean property configuration for this object.- Specified by:
endConfiguration
in interfaceConfigurable
-
-