Permalink
Browse files

ISPN-2824 Add support for JSR-107 Cache.invokeEntryProcessor()

* It uses an optimistic method based around reference copying and
conditional, CAS-like, cache operations to provide perceived exclusive
data access.
* It also adds per-key locking for when keys are used by the entry
processor in order to necessary delays to operations.
* To avoid affecting performance of normal CRUD cache operations,
it checks whether a key is locked before trying to acquire the lock
for it. If it's locked, it means it's in use by an entry processor.
  • Loading branch information...
1 parent cf49494 commit 95aa2b1a75492909d9c30a8927f1d2c84df11ceb @galderz galderz committed with maniksurtani Feb 20, 2013
@@ -20,7 +20,16 @@
import javax.cache.Cache.Entry;
-public class JCacheEntry<K, V> implements Entry<K, V> {
+/**
+ * Infinispan implementation of {@link javax.cache.Cache.Entry<K, V>}.
+ *
+ * @param <K> the type of key maintained by this cache entry
+ * @param <V> the type of value maintained by this cache entry
+ * @author Vladimir Blagojevic
+ * @author Galder Zamarreño
+ * @since 5.3
+ */
+public final class JCacheEntry<K, V> implements Entry<K, V> {
protected final K key;
@@ -28,35 +28,73 @@
import javax.cache.Cache;
/**
- * Mutable cache entry
+ * Infinispan implementation of {@link Cache.MutableEntry<K, V>} designed to
+ * be passed as parameter to {@link Cache.EntryProcessor#process(javax.cache.Cache.MutableEntry)}.
*
+ * @param <K> the type of key maintained by this cache entry
+ * @param <V> the type of value maintained by this cache entry
* @author Galder Zamarreño
* @since 5.3
*/
-public class MutableJCacheEntry<K, V>
- extends JCacheEntry<K, V> implements Cache.MutableEntry<K, V> {
+public final class MutableJCacheEntry<K, V> implements Cache.MutableEntry<K, V> {
private final AdvancedCache<K, V> cache;
- public MutableJCacheEntry(
- AdvancedCache<K, V> cache, K key, V value) {
- super(key, value);
+ private final K key;
+
+ private final V oldValue;
+
+ private V value; // mutable
+
+ private boolean removed;
+
+ public MutableJCacheEntry(AdvancedCache<K, V> cache, K key, V value) {
this.cache = cache;
+ this.key = key;
+ this.oldValue = value;
}
@Override
public boolean exists() {
- return cache.containsKey(key);
+ if (value != null)
+ return true;
+ else if (!removed)
+ return cache.containsKey(key);
+
+ return false;
}
@Override
public void remove() {
- cache.remove(key);
+ removed = true;
+ value = null;
}
@Override
public void setValue(V value) {
- cache.put(key, value);
+ this.value = value;
+ removed = false;
+ }
+
+ @Override
+ public K getKey() {
+ return key;
+ }
+
+ @Override
+ public V getValue() {
+ if (value != null)
+ return value;
+
+ return oldValue;
+ }
+
+ V getNewValue() {
+ return value;
+ }
+
+ boolean isRemoved() {
+ return removed;
}
}

0 comments on commit 95aa2b1

Please sign in to comment.