From jbosscache-commits at lists.jboss.org Thu Dec 11 09:19:18 2008 Content-Type: multipart/mixed; boundary="===============3895393585768400557==" MIME-Version: 1.0 From: jbosscache-commits at lists.jboss.org To: jbosscache-commits at lists.jboss.org Subject: [jbosscache-commits] JBoss Cache SVN: r7280 - in core/branches/flat/src/main/java/org/jboss/starobrno: util and 1 other directory. Date: Thu, 11 Dec 2008 09:19:18 -0500 Message-ID: --===============3895393585768400557== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: manik.surtani(a)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/ObjectDuplicat= or.java Modified: core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/Lockin= gInterceptor.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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/Locki= ngInterceptor.java 2008-12-11 03:12:21 UTC (rev 7279) +++ core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/Locki= ngInterceptor.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.PrePostProcessingCommandInter= ceptor; +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 MVCC designs= * @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 c= ommand) throws Throwable { -// if (ctx.getOptionOverrides().isSuppressLocking()) -// { -// if (log.isWarnEnabled()) log.warn("Lock suppression not support= ed 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, RollbackComm= and command) throws Throwable + public Object visitRollbackCommand(InvocationContext ctx, RollbackComma= nd command) throws Throwable { try { @@ -110,7 +98,7 @@ } = @Override - public Object handlePrepareCommand(InvocationContext ctx, PrepareComman= d 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, GetKeyVal= ueCommand command) throws Throwable + public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValu= eCommand 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 comm= and) throws Throwable + public Object visitSizeCommand(InvocationContext ctx, SizeCommand comma= nd) throws Throwable { - if (log.isDebugEnabled()) log.debug("No locking performed for SizeCo= mmands"); - return invokeNextInterceptor(ctx, command); + try + { + if (log.isDebugEnabled()) log.debug("No locking performed for Siz= eCommands"); + return invokeNextInterceptor(ctx, command); + } + finally + { + doAfterCall(ctx); + } } = // write commands = @Override - public Object handleClearCommand(InvocationContext ctx, ClearCommand co= mmand) throws Throwable + public Object visitClearCommand(InvocationContext ctx, ClearCommand com= mand) throws Throwable { - // get a snapshot of all keys in the data container - for (Object key : dataContainer.keySet()) entryWrapper.wrapEntryForW= riting(ctx, key, false, false); + try + { + // get a snapshot of all keys in the data container + for (Object key : dataContainer.keySet()) entryWrapper.wrapEntryF= orWriting(ctx, key, false, false); = - return invokeNextInterceptor(ctx, command); + return invokeNextInterceptor(ctx, command); + } + finally + { + doAfterCall(ctx); + } } = @Override - public Object handleEvictCommand(InvocationContext ctx, EvictCommand co= mmand) throws Throwable + public Object visitEvictCommand(InvocationContext ctx, EvictCommand com= mand) throws Throwable { - entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true); - return invokeNextInterceptor(ctx, command); + try + { + entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, tr= ue); + return invokeNextInterceptor(ctx, command); + } + finally + { + doAfterCall(ctx); + } } = @Override - public Object handlePutKeyValueCommand(InvocationContext ctx, PutKeyVal= ueCommand command) throws Throwable + public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValu= eCommand command) throws Throwable { - entryWrapper.wrapEntryForWriting(ctx, command.getKey(), true, false); - Object o =3D invokeNextInterceptor(ctx, command); - return o; + try + { + entryWrapper.wrapEntryForWriting(ctx, command.getKey(), true, fal= se); + Object o =3D invokeNextInterceptor(ctx, command); + return o; + } + finally + { + doAfterCall(ctx); + } } = @Override - public Object handlePutMapCommand(InvocationContext ctx, PutMapCommand = command) throws Throwable + public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand c= ommand) 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 c= ommand) throws Throwable { - entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, true); - return invokeNextInterceptor(ctx, command); + try + { + entryWrapper.wrapEntryForWriting(ctx, command.getKey(), false, tr= ue); + return invokeNextInterceptor(ctx, command); + } + finally + { + doAfterCall(ctx); + } } = @Override - public Object handleReplaceCommand(InvocationContext ctx, ReplaceComman= d 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, tr= ue); + return invokeNextInterceptor(ctx, command); + } + finally + { + doAfterCall(ctx); + } } = @Override - public Object handleGravitateDataCommand(InvocationContext ctx, Gravita= teDataCommand command) throws Throwable + public Object visitGravitateDataCommand(InvocationContext ctx, Gravitat= eDataCommand 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 comm= and) + private void doAfterCall(InvocationContext ctx) { // for non-transactional stuff. if (ctx.getTransactionContext() =3D=3D null) @@ -214,12 +265,12 @@ } else { - if (useReadCommitted && !(command instanceof TransactionBoundaryC= ommand)) + if (useReadCommitted) { Map original =3D ctx.getLookedUpEntries(); if (!original.isEmpty()) { - Map defensiveCopy =3D new HashMap(original); + Map defensiveCopy =3D ObjectDuplicator.d= uplicateMap(original); for (Map.Entry e : defensiveCopy.entrySe= t()) { if (!e.getValue().isChanged()) ctx.removeLookedUpEntry(e= .getKey()); Modified: core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutab= les.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutables.ja= va 2008-12-11 03:12:21 UTC (rev 7279) +++ core/branches/flat/src/main/java/org/jboss/starobrno/util/Immutables.ja= va 2008-12-11 14:19:18 UTC (rev 7280) @@ -120,10 +120,8 @@ */ public static Set immutableSetCopy(Set set) { - Set copy =3D attemptKnownSetCopy(set); + Set copy =3D ObjectDuplicator.duplicateSet(set); if (copy =3D=3D null) - attemptClone(set); - if (copy =3D=3D null) // Set uses Collection copy-ctor copy =3D attemptCopyConstructor(set, Collection.class); if (copy =3D=3D null) @@ -152,11 +150,9 @@ */ public static Map immutableMapCopy(Map map) { - Map copy =3D attemptKnownMapCopy(map); + Map copy =3D ObjectDuplicator.duplicateMap= (map); = if (copy =3D=3D null) - attemptClone(map); - if (copy =3D=3D null) copy =3D attemptCopyConstructor(map, Map.class); if (copy =3D=3D null) copy =3D new HashMap(map); @@ -172,10 +168,8 @@ */ public static Collection immutableCollectionCopy(Collection collection) { - Collection copy =3D attemptKnownSetCopy(collection); + Collection copy =3D ObjectDuplicator.duplicateCollectio= n(collection); if (copy =3D=3D null) - copy =3D attemptClone(collection); - if (copy =3D=3D null) copy =3D attemptCopyConstructor(collection, Collection.class); if (copy =3D=3D null) copy =3D new ArrayList(collection); @@ -184,51 +178,6 @@ } = @SuppressWarnings("unchecked") - private static 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 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 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 attemptCopyConstructor(T source, Class = clazz) { try Added: core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDupl= icator.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDuplica= tor.java (rev 0) +++ core/branches/flat/src/main/java/org/jboss/starobrno/util/ObjectDuplica= tor.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 Map duplicateMap(Map original) + { + if (original instanceof FastCopyHashMap) + return (Map) ((FastCopyHashMap) original).clone(); + if (original instanceof HashMap) + return (Map) ((HashMap) original).clone(); + if (original instanceof TreeMap) + return (Map) ((TreeMap) original).clone(); + return attemptClone(original); + } + + @SuppressWarnings("unchecked") + public static Set duplicateSet(Set original) + { + if (original instanceof HashSet) + return (Set) ((HashSet) original).clone(); + if (original instanceof TreeSet) + return (Set) ((TreeSet) original).clone(); + + return attemptClone(original); + } + + @SuppressWarnings("unchecked") + public static Collection duplicateCollection(Collection origi= nal) + { + if (original instanceof HashSet) + return (Set) ((HashSet) original).clone(); + if (original instanceof TreeSet) + return (Set) ((TreeSet) original).clone(); + + return attemptClone(original); + } + + @SuppressWarnings("unchecked") + private static T attemptClone(T source) + { + if (source instanceof Cloneable) + { + try + { + return (T) source.getClass().getMethod("clone").invoke(source); + } + catch (Exception e) + { + } + } + + return null; + } +} --===============3895393585768400557==--