[infinispan-commits] Infinispan SVN: r338 - trunk/core/src/main/java/org/infinispan/interceptors.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Tue May 26 04:52:06 EDT 2009


Author: vblagojevic at jboss.com
Date: 2009-05-26 04:52:06 -0400 (Tue, 26 May 2009)
New Revision: 338

Added:
   trunk/core/src/main/java/org/infinispan/interceptors/ImplicitEagerLockingInterceptor.java
Log:
[ISPN-70] - Transparent eager locking for transactions

Added: trunk/core/src/main/java/org/infinispan/interceptors/ImplicitEagerLockingInterceptor.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/interceptors/ImplicitEagerLockingInterceptor.java	                        (rev 0)
+++ trunk/core/src/main/java/org/infinispan/interceptors/ImplicitEagerLockingInterceptor.java	2009-05-26 08:52:06 UTC (rev 338)
@@ -0,0 +1,95 @@
+package org.infinispan.interceptors;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.infinispan.commands.CommandsFactory;
+import org.infinispan.commands.LockControlCommand;
+import org.infinispan.commands.write.EvictCommand;
+import org.infinispan.commands.write.InvalidateCommand;
+import org.infinispan.commands.write.PutKeyValueCommand;
+import org.infinispan.commands.write.PutMapCommand;
+import org.infinispan.commands.write.RemoveCommand;
+import org.infinispan.commands.write.ReplaceCommand;
+import org.infinispan.context.InvocationContext;
+import org.infinispan.factories.annotations.Inject;
+import org.infinispan.interceptors.base.CommandInterceptor;
+
+/**
+ * Interceptor in charge of eager, implicit locking of cache keys across cluster within
+ * transactional context
+ * 
+ * @author <a href="mailto:vblagoje at redhat.com">Vladimir Blagojevic (vblagoje at redhat.com)</a>
+ * @since 4.0
+ */
+public class ImplicitEagerLockingInterceptor extends CommandInterceptor {
+
+   private CommandsFactory cf;
+
+   @Inject
+   public void init(CommandsFactory factory) {
+      this.cf = factory;
+   }
+
+   @Override
+   public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command)
+            throws Throwable {
+      boolean localTxScope = ctx.isInTxScope() & ctx.isOriginLocal();
+      if (localTxScope) {
+         return lockEagerly(ctx, Collections.singleton(command.getKey()));
+      }
+      return invokeNextInterceptor(ctx, command);
+   }
+
+   @Override
+   public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable {
+      boolean localTxScope = ctx.isInTxScope() & ctx.isOriginLocal();
+      if (localTxScope) {
+         return lockEagerly(ctx, Collections.singleton(command.getKey()));
+      }
+      return invokeNextInterceptor(ctx, command);
+   }
+
+   @Override
+   public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command)
+            throws Throwable {
+      boolean localTxScope = ctx.isInTxScope() & ctx.isOriginLocal();
+      if (localTxScope) {
+         return lockEagerly(ctx, Collections.singleton(command.getKey()));
+      }
+      return invokeNextInterceptor(ctx, command);
+   }
+
+   @Override
+   public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable {
+      boolean localTxScope = ctx.isInTxScope() & ctx.isOriginLocal();
+      if (localTxScope) {
+         return lockEagerly(ctx, command.getMap().keySet());
+      }
+      return invokeNextInterceptor(ctx, command);
+   }
+
+   @Override
+   public Object visitEvictCommand(InvocationContext ctx, EvictCommand command) throws Throwable {
+      boolean localTxScope = ctx.isInTxScope() & ctx.isOriginLocal();
+      if (localTxScope) {
+         return lockEagerly(ctx, Collections.singleton(command.getKey()));
+      }
+      return invokeNextInterceptor(ctx, command);
+   }
+
+   @Override
+   public Object visitInvalidateCommand(InvocationContext ctx, InvalidateCommand command)
+            throws Throwable {
+      boolean localTxScope = ctx.isInTxScope() & ctx.isOriginLocal();
+      if (localTxScope) {
+         return lockEagerly(ctx, Collections.singleton(command.getKey()));
+      }
+      return invokeNextInterceptor(ctx, command);
+   }
+
+   private Object lockEagerly(InvocationContext ctx, Collection<Object> keys) throws Throwable {
+      LockControlCommand lcc = cf.buildLockControlCommand(keys, true);
+      return invokeNextInterceptor(ctx, lcc);
+   }
+}




More information about the infinispan-commits mailing list