org.infinispan.loaders
Class LockSupportCacheStore

java.lang.Object
  extended by org.infinispan.loaders.AbstractCacheLoader
      extended by org.infinispan.loaders.AbstractCacheStore
          extended by org.infinispan.loaders.LockSupportCacheStore
All Implemented Interfaces:
CacheLoader, CacheStore
Direct Known Subclasses:
BucketBasedCacheStore, JdbcStringBasedCacheStore

public abstract class LockSupportCacheStore
extends AbstractCacheStore

This class extends 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.

Author:
Mircea.Markus@jboss.com

Field Summary
 
Fields inherited from class org.infinispan.loaders.AbstractCacheStore
multiThreadedPurge, purgerService
 
Fields inherited from class org.infinispan.loaders.AbstractCacheLoader
cache, marshaller
 
Constructor Summary
LockSupportCacheStore()
           
 
Method Summary
protected  void 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()
           
 void fromStream(ObjectInput objectInput)
          Writes contents of the stream to the store.
protected abstract  void fromStreamLockSafe(ObjectInput ois)
           
protected abstract  String getLockFromKey(Object key)
           
 int getTotalLockCount()
           
protected  boolean immediateLockForWriting(String key)
          Same as lockForWriting(String), 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, String lockingKey)
           
protected  void lockForReading(String key)
          Acquires read lock on the given key.
protected  void lockForWriting(String 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, String lockingKey)
           
 void start()
           
 void store(InternalCacheEntry ed)
          Stores an entry
protected abstract  void storeLockSafe(InternalCacheEntry ed, String 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(String key)
          Release the locks (either read or write).
 
Methods inherited from class org.infinispan.loaders.AbstractCacheStore
applyModifications, commit, getCacheStoreConfig, getConcurrencyLevel, getMarshaller, prepare, purgeExpired, purgeInternal, removeAll, rollback, safeClose, safeClose, stop, supportsMultiThreadedPurge
 
Methods inherited from class org.infinispan.loaders.AbstractCacheLoader
containsKey
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.infinispan.loaders.CacheLoader
containsKey, getConfigurationClass
 

Constructor Detail

LockSupportCacheStore

public LockSupportCacheStore()
Method Detail

init

public void init(CacheLoaderConfig config,
                 Cache<?,?> cache,
                 StreamingMarshaller m)
          throws CacheLoaderException
Description copied from interface: CacheLoader
Used to initialize a cache loader. Typically invoked by the CacheLoaderManager when setting up cache loaders.

Specified by:
init in interface CacheLoader
Overrides:
init in class AbstractCacheStore
Parameters:
config - the cache loader configuration bean
cache - 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.
Throws:
CacheLoaderException

start

public void start()
           throws CacheLoaderException
Specified by:
start in interface CacheLoader
Overrides:
start in class AbstractCacheStore
Throws:
CacheLoaderException

unlock

protected final void unlock(String key)
Release the locks (either read or write).


lockForWriting

protected final void lockForWriting(String key)
                             throws CacheLoaderException
Acquires write lock on the given key.

Throws:
CacheLoaderException

lockForReading

protected final void lockForReading(String key)
                             throws CacheLoaderException
Acquires read lock on the given key.

Throws:
CacheLoaderException

immediateLockForWriting

protected final boolean immediateLockForWriting(String key)
                                         throws CacheLoaderException
Same as lockForWriting(String), but with 0 timeout.

Throws:
CacheLoaderException

acquireGlobalLock

protected final void acquireGlobalLock(boolean exclusive)
                                throws CacheLoaderException
Based on the supplied param, acquires a global read(false) or write (true) lock.

Throws:
CacheLoaderException

releaseGlobalLock

protected final void releaseGlobalLock(boolean exclusive)
Based on the supplied param, releases a global read(false) or write (true) lock.


load

public final InternalCacheEntry load(Object key)
                              throws CacheLoaderException
Description copied from interface: CacheLoader
Loads an entry mapped to by a given key. Should return null if the entry does not exist. Expired entries are not returned.

Parameters:
key - key
Returns:
an entry
Throws:
CacheLoaderException - in the event of problems reading from source

loadAll

public final Set<InternalCacheEntry> loadAll()
                                      throws CacheLoaderException
Description copied from interface: CacheLoader
Loads all entries in the loader. Expired entries are not returned.

Returns:
a set of entries, or an empty set if the loader is emptied.
Throws:
CacheLoaderException - in the event of problems reading from source

load

public final Set<InternalCacheEntry> load(int maxEntries)
                                   throws CacheLoaderException
Description copied from interface: CacheLoader
Loads up to a specific number of entries. There is no guarantee as to order of entries loaded. The set returned would contain up to a maximum of numEntries entries, and no more.

Parameters:
maxEntries - maximum number of entries to load
Returns:
a set of entries, which would contain between 0 and numEntries entries.
Throws:
CacheLoaderException

loadAllKeys

public Set<Object> loadAllKeys(Set<Object> keysToExclude)
                        throws CacheLoaderException
Description copied from interface: CacheLoader
Loads a set of all keys, excluding a filter set.

Parameters:
keysToExclude - a set of keys to exclude. An empty set or null will indicate that all keys should be returned.
Returns:
A set containing keys of entries stored. An empty set is returned if the loader is empty.
Throws:
CacheLoaderException

store

public final void store(InternalCacheEntry ed)
                 throws CacheLoaderException
Description copied from interface: CacheStore
Stores an entry

Parameters:
ed - entry to store
Throws:
CacheLoaderException - in the event of problems writing to the store

remove

public final boolean remove(Object key)
                     throws CacheLoaderException
Description copied from interface: CacheStore
Removes an entry in the store.

Parameters:
key - key to remove
Returns:
true if the entry was removed; false if the entry wasn't found.
Throws:
CacheLoaderException - in the event of problems writing to the store

fromStream

public final void fromStream(ObjectInput objectInput)
                      throws CacheLoaderException
Description copied from interface: CacheStore
Writes contents of the stream to the store. Implementations should expect that the stream contains data in an implementation-specific format, typically generated using 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.

Parameters:
objectInput - stream to read from
Throws:
CacheLoaderException - in the event of problems writing to the store

toStream

public void toStream(ObjectOutput objectOutput)
              throws CacheLoaderException
Description copied from interface: CacheStore
Loads the entire state into a stream, using whichever format is most efficient for the cache loader implementation. Typically read and parsed by 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.

Parameters:
objectOutput - stream to write to
Throws:
CacheLoaderException - in the event of problems reading from the store

clear

public final void clear()
                 throws CacheLoaderException
Description copied from interface: CacheStore
Clears all entries in the store

Throws:
CacheLoaderException - in the event of problems writing to the store

getTotalLockCount

public int getTotalLockCount()

clearLockSafe

protected abstract void clearLockSafe()
                               throws CacheLoaderException
Throws:
CacheLoaderException

loadAllLockSafe

protected abstract Set<InternalCacheEntry> loadAllLockSafe()
                                                    throws CacheLoaderException
Throws:
CacheLoaderException

loadLockSafe

protected abstract Set<InternalCacheEntry> loadLockSafe(int maxEntries)
                                                 throws CacheLoaderException
Throws:
CacheLoaderException

loadAllKeysLockSafe

protected abstract Set<Object> loadAllKeysLockSafe(Set<Object> keysToExclude)
                                            throws CacheLoaderException
Throws:
CacheLoaderException

toStreamLockSafe

protected abstract void toStreamLockSafe(ObjectOutput oos)
                                  throws CacheLoaderException
Throws:
CacheLoaderException

fromStreamLockSafe

protected abstract void fromStreamLockSafe(ObjectInput ois)
                                    throws CacheLoaderException
Throws:
CacheLoaderException

removeLockSafe

protected abstract boolean removeLockSafe(Object key,
                                          String lockingKey)
                                   throws CacheLoaderException
Throws:
CacheLoaderException

storeLockSafe

protected abstract void storeLockSafe(InternalCacheEntry ed,
                                      String lockingKey)
                               throws CacheLoaderException
Throws:
CacheLoaderException

loadLockSafe

protected abstract InternalCacheEntry loadLockSafe(Object key,
                                                   String lockingKey)
                                            throws CacheLoaderException
Throws:
CacheLoaderException

getLockFromKey

protected abstract String getLockFromKey(Object key)
                                  throws CacheLoaderException
Throws:
CacheLoaderException

Google Analytics

Copyright © 2010 JBoss, a division of Red Hat. All Rights Reserved.