L
- the type of the locking key returned by
getLockFromKey(Object)
public abstract class LockSupportCacheStore<L> extends AbstractCacheStore
AbstractCacheStore
adding lock support for consistently accessing stored data.
In-memory locking is needed by aggregation operations(e.g. loadAll, toStream, fromStream) to make sure that
manipulated data won't be corrupted by concurrent access to Store. It also assures atomic data access for each stored
entry.
Locking is based on a StripedLock
. You can tune the concurrency level of the striped lock (see the Javadocs
of StripedLock for details on what this is) by using the LockSupportCacheStoreConfig.setLockConcurrencyLevel(int)
setter.
multiThreadedPurge, purgerService
cache, marshaller
Constructor and Description |
---|
LockSupportCacheStore() |
Modifier and Type | Method and Description |
---|---|
protected boolean |
acquireGlobalLock(boolean exclusive)
Based on the supplied param, acquires a global read(false) or write (true) lock.
|
void |
clear()
Clears all entries in the store
|
protected abstract void |
clearLockSafe() |
protected void |
downgradeLock(L key)
Downgrade a write lock to a read lock
|
void |
fromStream(ObjectInput objectInput)
Writes contents of the stream to the store.
|
protected abstract void |
fromStreamLockSafe(ObjectInput ois) |
protected abstract L |
getLockFromKey(Object key) |
int |
getTotalLockCount() |
protected boolean |
immediateLockForWriting(L key)
Same as
lockForWriting(Object) , but with 0 timeout. |
void |
init(CacheLoaderConfig config,
Cache<?,?> cache,
StreamingMarshaller m)
Used to initialize a cache loader.
|
Set<InternalCacheEntry> |
load(int maxEntries)
Loads up to a specific number of entries.
|
InternalCacheEntry |
load(Object key)
Loads an entry mapped to by a given key.
|
Set<InternalCacheEntry> |
loadAll()
Loads all entries in the loader.
|
Set<Object> |
loadAllKeys(Set<Object> keysToExclude)
Loads a set of all keys, excluding a filter set.
|
protected abstract Set<Object> |
loadAllKeysLockSafe(Set<Object> keysToExclude) |
protected abstract Set<InternalCacheEntry> |
loadAllLockSafe() |
protected abstract Set<InternalCacheEntry> |
loadLockSafe(int maxEntries) |
protected abstract InternalCacheEntry |
loadLockSafe(Object key,
L lockingKey) |
protected void |
lockForReading(L key)
Acquires read lock on the given key.
|
protected void |
lockForWriting(L key)
Acquires write lock on the given key.
|
protected void |
releaseGlobalLock(boolean exclusive)
Based on the supplied param, releases a global read(false) or write (true) lock.
|
boolean |
remove(Object key)
Removes an entry in the store.
|
protected abstract boolean |
removeLockSafe(Object key,
L lockingKey) |
void |
start() |
void |
store(InternalCacheEntry ed)
Stores an entry
|
protected abstract void |
storeLockSafe(InternalCacheEntry ed,
L lockingKey) |
void |
toStream(ObjectOutput objectOutput)
Loads the entire state into a stream, using whichever format is most efficient for the cache loader
implementation.
|
protected abstract void |
toStreamLockSafe(ObjectOutput oos) |
protected void |
unlock(L key)
Release the locks (either read or write).
|
protected void |
upgradeLock(L key)
Upgrades a read lock to a write lock.
|
applyModifications, commit, getCacheStoreConfig, getConcurrencyLevel, getMarshaller, prepare, purgeExpired, purgeInternal, removeAll, rollback, safeClose, safeClose, stop, supportsMultiThreadedPurge
containsKey
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
containsKey, getConfigurationClass
public void init(CacheLoaderConfig config, Cache<?,?> cache, StreamingMarshaller m) throws CacheLoaderException
CacheLoader
CacheLoaderManager
when setting up cache loaders.init
in interface CacheLoader
init
in class AbstractCacheStore
config
- the cache loader configuration beancache
- cache associated with this cache loader. Implementations may use this to determine cache name when
selecting where refer to state in storage, for example, a different database table name.m
- marshaller to use when loading state from a stream, if supported by the implementation.CacheLoaderException
public void start() throws CacheLoaderException
start
in interface CacheLoader
start
in class AbstractCacheStore
CacheLoaderException
protected final void unlock(L key)
protected final void lockForWriting(L key)
protected final void lockForReading(L key)
protected final void upgradeLock(L key)
protected final void downgradeLock(L key)
protected final boolean immediateLockForWriting(L key)
lockForWriting(Object)
, but with 0 timeout.protected final boolean acquireGlobalLock(boolean exclusive)
protected final void releaseGlobalLock(boolean exclusive)
public final InternalCacheEntry load(Object key) throws CacheLoaderException
CacheLoader
key
- keyCacheLoaderException
- in the event of problems reading from sourcepublic final Set<InternalCacheEntry> loadAll() throws CacheLoaderException
CacheLoader
CacheLoaderException
- in the event of problems reading from sourcepublic final Set<InternalCacheEntry> load(int maxEntries) throws CacheLoaderException
CacheLoader
maxEntries
- maximum number of entries to loadCacheLoaderException
public Set<Object> loadAllKeys(Set<Object> keysToExclude) throws CacheLoaderException
CacheLoader
keysToExclude
- a set of keys to exclude. An empty set or null will indicate that all keys should be returned.CacheLoaderException
public final void store(InternalCacheEntry ed) throws CacheLoaderException
CacheStore
ed
- entry to storeCacheLoaderException
- in the event of problems writing to the storepublic final boolean remove(Object key) throws CacheLoaderException
CacheStore
key
- key to removeCacheLoaderException
- in the event of problems writing to the storepublic final void fromStream(ObjectInput objectInput) throws CacheLoaderException
CacheStore
CacheStore.toStream(java.io.ObjectOutput)
. While not a
requirement, it is recommended that implementations make use of the StreamingMarshaller
when dealing with the stream to make use of efficient marshalling.
It is imperative that implementations do not close the stream after finishing with it.
It is also recommended that implementations use their own start and end markers on the stream since
other processes may write additional data to the stream after the cache store has written to it. As such, either
markers or some other mechanism to prevent the store from reading too much information should be employed when
writing to the stream in CacheStore.fromStream(java.io.ObjectInput)
to prevent data corruption.
It can be assumed that the stream passed in already performs buffering such that the cache store implementation
doesn't have to.
objectInput
- stream to read fromCacheLoaderException
- in the event of problems writing to the storepublic void toStream(ObjectOutput objectOutput) throws CacheLoaderException
CacheStore
CacheStore.fromStream(java.io.ObjectInput)
.
While not a requirement, it is recommended that implementations make use of the StreamingMarshaller
when dealing with the stream to make use of efficient marshalling.
It is imperative that implementations do not flush or close the stream after finishing with it.
It is also recommended that implementations use their own start and end markers on the stream since
other processes may write additional data to the stream after the cache store has written to it. As such, either
markers or some other mechanism to prevent the store from reading too much information in CacheStore.fromStream(java.io.ObjectInput)
should be employed, to prevent data corruption.
It can be assumed that the stream passed in already performs buffering such that the cache store implementation
doesn't have to.
objectOutput
- stream to write toCacheLoaderException
- in the event of problems reading from the storepublic final void clear() throws CacheLoaderException
CacheStore
CacheLoaderException
- in the event of problems writing to the storepublic int getTotalLockCount()
protected abstract void clearLockSafe() throws CacheLoaderException
CacheLoaderException
protected abstract Set<InternalCacheEntry> loadAllLockSafe() throws CacheLoaderException
CacheLoaderException
protected abstract Set<InternalCacheEntry> loadLockSafe(int maxEntries) throws CacheLoaderException
CacheLoaderException
protected abstract Set<Object> loadAllKeysLockSafe(Set<Object> keysToExclude) throws CacheLoaderException
CacheLoaderException
protected abstract void toStreamLockSafe(ObjectOutput oos) throws CacheLoaderException
CacheLoaderException
protected abstract void fromStreamLockSafe(ObjectInput ois) throws CacheLoaderException
CacheLoaderException
protected abstract boolean removeLockSafe(Object key, L lockingKey) throws CacheLoaderException
CacheLoaderException
protected abstract void storeLockSafe(InternalCacheEntry ed, L lockingKey) throws CacheLoaderException
CacheLoaderException
protected abstract InternalCacheEntry loadLockSafe(Object key, L lockingKey) throws CacheLoaderException
CacheLoaderException
protected abstract L getLockFromKey(Object key) throws CacheLoaderException
CacheLoaderException
Copyright © 2012 JBoss by Red Hat. All Rights Reserved.