[jbosscache-commits] JBoss Cache SVN: r7280 - in core/branches/flat/src/main/java/org/jboss/starobrno: util and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu Dec 11 09:19:18 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-12-11 09:19:18 -0500 (Thu, 11 Dec 2008)
New Revision: 7280

Added:
   core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDuplicator.java
Modified:
   core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/LockingInterceptor.java
   core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutables.java
Log:
Improved locking interceptor

Modified: core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/LockingInterceptor.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/LockingInterceptor.java	2008-12-11 03:12:21 UTC (rev 7279)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/LockingInterceptor.java	2008-12-11 14:19:18 UTC (rev 7280)
@@ -22,8 +22,6 @@
 package org.jboss.starobrno.interceptors;
 
 import org.jboss.cache.lock.IsolationLevel;
-import org.jboss.starobrno.commands.TransactionBoundaryCommand;
-import org.jboss.starobrno.commands.VisitableCommand;
 import org.jboss.starobrno.commands.read.GetKeyValueCommand;
 import org.jboss.starobrno.commands.read.GravitateDataCommand;
 import org.jboss.starobrno.commands.read.SizeCommand;
@@ -37,10 +35,10 @@
 import org.jboss.starobrno.context.InvocationContext;
 import org.jboss.starobrno.factories.annotations.Inject;
 import org.jboss.starobrno.factories.annotations.Start;
-import org.jboss.starobrno.interceptors.base.PrePostProcessingCommandInterceptor;
+import org.jboss.starobrno.interceptors.base.CommandInterceptor;
 import org.jboss.starobrno.lock.LockManager;
+import org.jboss.starobrno.util.ObjectDuplicator;
 
-import java.util.HashMap;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
@@ -52,7 +50,7 @@
  * @see <a href="http://wiki.jboss.org/wiki/JBossCacheMVCC">MVCC designs</a>
  * @since 3.0
  */
-public class LockingInterceptor extends PrePostProcessingCommandInterceptor
+public class LockingInterceptor extends CommandInterceptor
 {
    LockManager lockManager;
    DataContainer dataContainer;
@@ -74,18 +72,8 @@
    }
 
    @Override
-   protected boolean doBeforeCall(InvocationContext ctx, VisitableCommand command)
+   public Object visitCommitCommand(InvocationContext ctx, CommitCommand command) throws Throwable
    {
-//      if (ctx.getOptionOverrides().isSuppressLocking())
-//      {
-//         if (log.isWarnEnabled()) log.warn("Lock suppression not supported with MVCC!");
-//      }
-      return true;
-   }
-
-   @Override
-   public Object handleCommitCommand(InvocationContext ctx, CommitCommand command) throws Throwable
-   {
       try
       {
          return invokeNextInterceptor(ctx, command);
@@ -97,7 +85,7 @@
    }
 
    @Override
-   public Object handleRollbackCommand(InvocationContext ctx, RollbackCommand command) throws Throwable
+   public Object visitRollbackCommand(InvocationContext ctx, RollbackCommand command) throws Throwable
    {
       try
       {
@@ -110,7 +98,7 @@
    }
 
    @Override
-   public Object handlePrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable
+   public Object visitPrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable
    {
       try
       {
@@ -125,79 +113,142 @@
    // read commands
 
    @Override
-   public Object handleGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable
+   public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable
    {
-      entryWrapper.wrapEntryForReading(ctx, command.getKey(), true);
-      return invokeNextInterceptor(ctx, command);
+      try
+      {
+         entryWrapper.wrapEntryForReading(ctx, command.getKey(), true);
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handleSizeCommand(InvocationContext ctx, SizeCommand command) throws Throwable
+   public Object visitSizeCommand(InvocationContext ctx, SizeCommand command) throws Throwable
    {
-      if (log.isDebugEnabled()) log.debug("No locking performed for SizeCommands");
-      return invokeNextInterceptor(ctx, command);
+      try
+      {
+         if (log.isDebugEnabled()) log.debug("No locking performed for SizeCommands");
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    // write commands
 
    @Override
-   public Object handleClearCommand(InvocationContext ctx, ClearCommand command) throws Throwable
+   public Object visitClearCommand(InvocationContext ctx, ClearCommand command) throws Throwable
    {
-      // get a snapshot of all keys in the data container
-      for (Object key : dataContainer.keySet()) entryWrapper.wrapEntryForWriting(ctx, key, false, false);
+      try
+      {
+         // get a snapshot of all keys in the data container
+         for (Object key : dataContainer.keySet()) entryWrapper.wrapEntryForWriting(ctx, key, false, false);
 
-      return invokeNextInterceptor(ctx, command);
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handleEvictCommand(InvocationContext ctx, EvictCommand command) throws Throwable
+   public Object visitEvictCommand(InvocationContext ctx, EvictCommand command) throws Throwable
    {
-      entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true);
-      return invokeNextInterceptor(ctx, command);
+      try
+      {
+         entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true);
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handlePutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
+   public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
    {
-      entryWrapper.wrapEntryForWriting(ctx, command.getKey(), true, false);
-      Object o = invokeNextInterceptor(ctx, command);
-      return o;
+      try
+      {
+         entryWrapper.wrapEntryForWriting(ctx, command.getKey(), true, false);
+         Object o = invokeNextInterceptor(ctx, command);
+         return o;
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handlePutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable
+   public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) throws Throwable
    {
-      for (Object key : command.getMap().keySet())
+      try
       {
-         entryWrapper.wrapEntryForWriting(ctx, key, true, false);
+         for (Object key : command.getMap().keySet())
+         {
+            entryWrapper.wrapEntryForWriting(ctx, key, true, false);
+         }
+         return invokeNextInterceptor(ctx, command);
       }
-      return invokeNextInterceptor(ctx, command);
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handleRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable
+   public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable
    {
-      entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true);
-      return invokeNextInterceptor(ctx, command);
+      try
+      {
+         entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true);
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handleReplaceCommand(InvocationContext ctx, ReplaceCommand command) throws Throwable
+   public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) throws Throwable
    {
-      entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true);
-      return invokeNextInterceptor(ctx, command);
+      try
+      {
+         entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true);
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
    @Override
-   public Object handleGravitateDataCommand(InvocationContext ctx, GravitateDataCommand command) throws Throwable
+   public Object visitGravitateDataCommand(InvocationContext ctx, GravitateDataCommand command) throws Throwable
    {
-      entryWrapper.wrapEntryForReading(ctx, command.getKey(), true);
-      return invokeNextInterceptor(ctx, command);
+      try
+      {
+         entryWrapper.wrapEntryForReading(ctx, command.getKey(), true);
+         return invokeNextInterceptor(ctx, command);
+      }
+      finally
+      {
+         doAfterCall(ctx);
+      }
    }
 
 
    @SuppressWarnings("unchecked")
-   protected void doAfterCall(InvocationContext ctx, VisitableCommand command)
+   private void doAfterCall(InvocationContext ctx)
    {
       // for non-transactional stuff.
       if (ctx.getTransactionContext() == null)
@@ -214,12 +265,12 @@
       }
       else
       {
-         if (useReadCommitted && !(command instanceof TransactionBoundaryCommand))
+         if (useReadCommitted)
          {
             Map<Object, MVCCEntry> original = ctx.getLookedUpEntries();
             if (!original.isEmpty())
             {
-               Map<Object, MVCCEntry> defensiveCopy = new HashMap<Object, MVCCEntry>(original);
+               Map<Object, MVCCEntry> defensiveCopy = ObjectDuplicator.duplicateMap(original);
                for (Map.Entry<Object, MVCCEntry> e : defensiveCopy.entrySet())
                {
                   if (!e.getValue().isChanged()) ctx.removeLookedUpEntry(e.getKey());

Modified: core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutables.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutables.java	2008-12-11 03:12:21 UTC (rev 7279)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutables.java	2008-12-11 14:19:18 UTC (rev 7280)
@@ -120,10 +120,8 @@
     */
    public static <T> Set<T> immutableSetCopy(Set<? extends T> set)
    {
-      Set<? extends T> copy = attemptKnownSetCopy(set);
+      Set<? extends T> copy = ObjectDuplicator.duplicateSet(set);
       if (copy == null)
-         attemptClone(set);
-      if (copy == null)
          // Set uses Collection copy-ctor
          copy = attemptCopyConstructor(set, Collection.class);
       if (copy == null)
@@ -152,11 +150,9 @@
     */
    public static <K, V> Map<K, V> immutableMapCopy(Map<? extends K, ? extends V> map)
    {
-      Map<? extends K, ? extends V> copy = attemptKnownMapCopy(map);
+      Map<? extends K, ? extends V> copy = ObjectDuplicator.duplicateMap(map);
 
       if (copy == null)
-         attemptClone(map);
-      if (copy == null)
          copy = attemptCopyConstructor(map, Map.class);
       if (copy == null)
          copy = new HashMap<K, V>(map);
@@ -172,10 +168,8 @@
     */
    public static <T> Collection<T> immutableCollectionCopy(Collection<? extends T> collection)
    {
-      Collection<? extends T> copy = attemptKnownSetCopy(collection);
+      Collection<? extends T> copy = ObjectDuplicator.duplicateCollection(collection);
       if (copy == null)
-         copy = attemptClone(collection);
-      if (copy == null)
          copy = attemptCopyConstructor(collection, Collection.class);
       if (copy == null)
          copy = new ArrayList<T>(collection);
@@ -184,51 +178,6 @@
    }
 
    @SuppressWarnings("unchecked")
-   private static <T extends Map> T attemptKnownMapCopy(T map)
-   {
-      if (map instanceof FastCopyHashMap)
-         return (T) ((FastCopyHashMap) map).clone();
-      if (map instanceof HashMap)
-         return (T) ((HashMap) map).clone();
-      if (map instanceof LinkedHashMap)
-         return (T) ((LinkedHashMap) map).clone();
-      if (map instanceof TreeMap)
-         return (T) ((TreeMap) map).clone();
-
-      return null;
-   }
-
-   @SuppressWarnings("unchecked")
-   private static <T extends Collection> T attemptKnownSetCopy(T set)
-   {
-      if (set instanceof HashSet)
-         return (T) ((HashSet) set).clone();
-      if (set instanceof LinkedHashSet)
-         return (T) ((LinkedHashSet) set).clone();
-      if (set instanceof TreeSet)
-         return (T) ((TreeSet) set).clone();
-
-      return null;
-   }
-
-   @SuppressWarnings("unchecked")
-   private static <T> T attemptClone(T source)
-   {
-      if (source instanceof Cloneable)
-      {
-         try
-         {
-            return (T) source.getClass().getMethod("clone").invoke(source);
-         }
-         catch (Exception e)
-         {
-         }
-      }
-
-      return null;
-   }
-
-   @SuppressWarnings("unchecked")
    private static <T> T attemptCopyConstructor(T source, Class<? super T> clazz)
    {
       try

Added: core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDuplicator.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDuplicator.java	                        (rev 0)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDuplicator.java	2008-12-11 14:19:18 UTC (rev 7280)
@@ -0,0 +1,60 @@
+package org.jboss.starobrno.util;
+
+import java.util.*;
+
+/**
+ * A helper that efficiently duplicates known object types.
+ */
+public class ObjectDuplicator
+{
+   @SuppressWarnings("unchecked")
+   public static <K, V> Map<K, V> duplicateMap(Map<K, V> original)
+   {
+      if (original instanceof FastCopyHashMap)
+         return (Map<K, V>) ((FastCopyHashMap) original).clone();
+      if (original instanceof HashMap)
+         return (Map<K, V>) ((HashMap) original).clone();
+      if (original instanceof TreeMap)
+         return (Map<K, V>) ((TreeMap) original).clone();
+      return attemptClone(original);
+   }
+
+   @SuppressWarnings("unchecked")
+   public static <E> Set<E> duplicateSet(Set<E> original)
+   {
+      if (original instanceof HashSet)
+         return (Set<E>) ((HashSet) original).clone();
+      if (original instanceof TreeSet)
+         return (Set<E>) ((TreeSet) original).clone();
+
+      return attemptClone(original);
+   }
+
+   @SuppressWarnings("unchecked")
+   public static <E> Collection<E> duplicateCollection(Collection<E> original)
+   {
+      if (original instanceof HashSet)
+         return (Set<E>) ((HashSet) original).clone();
+      if (original instanceof TreeSet)
+         return (Set<E>) ((TreeSet) original).clone();
+
+      return attemptClone(original);
+   }
+
+   @SuppressWarnings("unchecked")
+   private static <T> T attemptClone(T source)
+   {
+      if (source instanceof Cloneable)
+      {
+         try
+         {
+            return (T) source.getClass().getMethod("clone").invoke(source);
+         }
+         catch (Exception e)
+         {
+         }
+      }
+
+      return null;
+   }
+}




More information about the jbosscache-commits mailing list