Author: manik.surtani(a)jboss.com
Date: 2008-06-25 11:09:36 -0400 (Wed, 25 Jun 2008)
New Revision: 6036
Modified:
core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java
core/trunk/src/main/java/org/jboss/cache/buddyreplication/Fqn2BuddyFqnVisitor.java
core/trunk/src/main/java/org/jboss/cache/commands/remote/ReplicateCommand.java
core/trunk/src/main/java/org/jboss/cache/commands/tx/OptimisticPrepareCommand.java
core/trunk/src/main/java/org/jboss/cache/commands/tx/PrepareCommand.java
core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java
core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticReplicationInterceptor.java
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticTxInterceptor.java
core/trunk/src/main/java/org/jboss/cache/interceptors/ReplicationInterceptor.java
core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java
core/trunk/src/main/java/org/jboss/cache/mvcc/InternalNode.java
Log:
War against cloning
Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2008-06-25 14:15:51 UTC
(rev 6035)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2008-06-25 15:09:36 UTC
(rev 6036)
@@ -731,17 +731,16 @@
setFlag(LOCK_FOR_CHILD_INSERT_REMOVE, lockForChildInsertRemove);
}
- public InternalNode clone()
+ public InternalNode copy()
{
- try
- {
- return (InternalNode) super.clone();
- }
- catch (CloneNotSupportedException e)
- {
- // do nothing
- return null;
- }
+ UnversionedNode n = new UnversionedNode(fqn.getLastElement(), fqn, data, cache);
+ n.children = children;
+ n.commandsFactory = commandsFactory;
+ n.delegate = delegate;
+ n.flags.clear();
+ n.flags.addAll(flags);
+ n.lockStrategyFactory = lockStrategyFactory;
+ return n;
}
public void setInternalState(Map state)
Modified: core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java 2008-06-25
14:15:51 UTC (rev 6035)
+++ core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -698,7 +698,7 @@
public ReplicateCommand transformReplicateCommand(ReplicateCommand rc)
{
- ReplicateCommand clone = rc.clone();
+ ReplicateCommand clone = rc.copy();
if (rc.isSingleCommand())
{
clone.setSingleModification(transformFqns((VisitableCommand)
rc.getSingleModification()));
Modified:
core/trunk/src/main/java/org/jboss/cache/buddyreplication/Fqn2BuddyFqnVisitor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/buddyreplication/Fqn2BuddyFqnVisitor.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/buddyreplication/Fqn2BuddyFqnVisitor.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -184,7 +184,7 @@
public Object visitOptimisticPrepareCommand(InvocationContext ctx,
OptimisticPrepareCommand command) throws Throwable
{
List<ReversibleCommand> transformed =
transformBatch(command.getModifications());
- return factory.buildOptimisticPrepareCommand(command.getGlobalTransaction(),
transformed, command.getData(), command.getLocalAddress(), command.isOnePhaseCommit());
+ return factory.buildOptimisticPrepareCommand(command.getGlobalTransaction(),
transformed, command.getLocalAddress(), command.isOnePhaseCommit());
}
@Override
Modified: core/trunk/src/main/java/org/jboss/cache/commands/remote/ReplicateCommand.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/commands/remote/ReplicateCommand.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/commands/remote/ReplicateCommand.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -9,6 +9,7 @@
import org.jboss.cache.commands.write.PutForExternalReadCommand;
import org.jboss.cache.interceptors.InterceptorChain;
+import java.util.ArrayList;
import java.util.List;
/**
@@ -224,24 +225,17 @@
}
/**
- * Clones this replicate command, but with an empty modification list.
+ * Creates a copy of this command, amking a deep copy of any collections but
everything else copied shallow.
*
- * @return a clone
+ * @return a copy
*/
- @Override
- public ReplicateCommand clone()
+ public ReplicateCommand copy()
{
- ReplicateCommand clone = null;
- try
- {
- clone = (ReplicateCommand) super.clone();
- clone.initialize(invoker);
- }
- catch (CloneNotSupportedException e)
- {
- // should never get here
- }
-
+ ReplicateCommand clone;
+ clone = new ReplicateCommand();
+ clone.invoker = invoker;
+ clone.modifications = modifications == null ? null : new
ArrayList<ReplicableCommand>(modifications);
+ clone.singleModification = singleModification;
return clone;
}
Modified:
core/trunk/src/main/java/org/jboss/cache/commands/tx/OptimisticPrepareCommand.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/commands/tx/OptimisticPrepareCommand.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/commands/tx/OptimisticPrepareCommand.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -6,8 +6,8 @@
import org.jboss.cache.transaction.GlobalTransaction;
import org.jgroups.Address;
+import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
/**
* An optimistic version of {@link PrepareCommand}.
@@ -18,12 +18,10 @@
public class OptimisticPrepareCommand extends PrepareCommand
{
public static final int METHOD_ID = 18;
- private Map data;
- public OptimisticPrepareCommand(GlobalTransaction gtx, List<ReversibleCommand>
modifications, Map data, Address address, boolean onePhaseCommit)
+ public OptimisticPrepareCommand(GlobalTransaction gtx, List<ReversibleCommand>
modifications, Address address, boolean onePhaseCommit)
{
super(gtx, modifications, address, onePhaseCommit);
- this.data = data;
}
public OptimisticPrepareCommand()
@@ -36,11 +34,6 @@
return visitor.visitOptimisticPrepareCommand(ctx, this);
}
- public Map getData()
- {
- return data;
- }
-
@Override
public int getCommandId()
{
@@ -50,13 +43,24 @@
@Override
public Object[] getParameters()
{
- return new Object[]{globalTransaction, modifications, data, localAddress,
onePhaseCommit};
+ // the null is needed for wire-level compat with pre-command versions
+ return new Object[]{globalTransaction, modifications, null, localAddress,
onePhaseCommit};
}
+ /**
+ * A shallow copy of all fields except collections.
+ *
+ * @return a copy of this command
+ */
@Override
- public OptimisticPrepareCommand clone() throws CloneNotSupportedException
+ public OptimisticPrepareCommand copy()
{
- return (OptimisticPrepareCommand) super.clone();
+ OptimisticPrepareCommand copy = new OptimisticPrepareCommand();
+ copy.globalTransaction = globalTransaction;
+ copy.localAddress = localAddress;
+ copy.modifications = modifications == null ? null : new
ArrayList<ReversibleCommand>(modifications);
+ copy.onePhaseCommit = onePhaseCommit;
+ return copy;
}
@Override
@@ -65,38 +69,15 @@
{
globalTransaction = (GlobalTransaction) args[0];
modifications = (List<ReversibleCommand>) args[1];
- data = (Map) args[2];
+ //args[2] is probably null.
localAddress = (Address) args[3];
onePhaseCommit = (Boolean) args[4];
}
@Override
- public boolean equals(Object o)
- {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- if (!super.equals(o)) return false;
-
- OptimisticPrepareCommand that = (OptimisticPrepareCommand) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
-
- return true;
- }
-
- @Override
- public int hashCode()
- {
- int result = super.hashCode();
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- @Override
public String toString()
{
return "OptimisticPrepareCommand{" +
- "data=" + data +
"modifications=" + modifications +
", localAddress=" + localAddress +
", onePhaseCommit=" + onePhaseCommit +
Modified: core/trunk/src/main/java/org/jboss/cache/commands/tx/PrepareCommand.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/tx/PrepareCommand.java 2008-06-25
14:15:51 UTC (rev 6035)
+++ core/trunk/src/main/java/org/jboss/cache/commands/tx/PrepareCommand.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -1,12 +1,13 @@
package org.jboss.cache.commands.tx;
import org.jboss.cache.InvocationContext;
+import org.jboss.cache.commands.ReplicableCommand;
import org.jboss.cache.commands.ReversibleCommand;
import org.jboss.cache.commands.Visitor;
-import org.jboss.cache.commands.ReplicableCommand;
import org.jboss.cache.transaction.GlobalTransaction;
import org.jgroups.Address;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -118,10 +119,14 @@
return result;
}
- @Override
- public PrepareCommand clone() throws CloneNotSupportedException
+ public PrepareCommand copy()
{
- return (PrepareCommand) super.clone();
+ PrepareCommand copy = new PrepareCommand();
+ copy.globalTransaction = globalTransaction;
+ copy.localAddress = localAddress;
+ copy.modifications = modifications == null ? null : new
ArrayList<ReversibleCommand>(modifications);
+ copy.onePhaseCommit = onePhaseCommit;
+ return copy;
}
@Override
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java 2008-06-25
14:15:51 UTC (rev 6035)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/EvictedEventNode.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -15,9 +15,9 @@
* @author Daniel Huang (dhuang(a)jboss.org)
* @see org.jboss.cache.Region
*/
-public class EvictedEventNode implements Cloneable
+public class EvictedEventNode
{
- private Fqn fqn_;
+ private Fqn fqn;
private NodeEventType type;
private int elementDifference;
@@ -64,12 +64,12 @@
public Fqn getFqn()
{
- return fqn_;
+ return fqn;
}
public void setFqn(Fqn fqn)
{
- this.fqn_ = fqn;
+ this.fqn = fqn;
}
public void setEventType(NodeEventType event)
@@ -85,21 +85,18 @@
@Override
public String toString()
{
- return "EvictedEN[fqn=" + fqn_ + " event=" + type + "
diff=" + elementDifference + "]";
+ return "EvictedEventNode[fqn=" + fqn + " event=" + type +
" diff=" + elementDifference + "]";
}
- public EvictedEventNode clone(Fqn cloneFqn)
+ /**
+ * Copies this evicted event node to create a new one with the same values, except
with a new Fqn root.
+ *
+ * @param newRoot new Fqn root to use
+ * @return a new EvictedEventNode instance
+ * @see org.jboss.cache.Region#copy(org.jboss.cache.Fqn)
+ */
+ public EvictedEventNode copy(Fqn newRoot)
{
- EvictedEventNode clone = null;
- try
- {
- clone = (EvictedEventNode) super.clone();
- clone.setFqn(Fqn.fromRelativeFqn(cloneFqn, fqn_));
- }
- catch (CloneNotSupportedException e)
- {
- // should never get here
- }
- return clone;
+ return new EvictedEventNode(Fqn.fromRelativeFqn(newRoot, fqn), type,
elementDifference);
}
}
Modified: core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java 2008-06-25
14:15:51 UTC (rev 6035)
+++ core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -253,9 +253,9 @@
return new RollbackCommand(gtx);
}
- public OptimisticPrepareCommand buildOptimisticPrepareCommand(GlobalTransaction gtx,
List<ReversibleCommand> modifications, Map data, Address address, boolean
onePhaseCommit)
+ public OptimisticPrepareCommand buildOptimisticPrepareCommand(GlobalTransaction gtx,
List<ReversibleCommand> modifications, Address address, boolean onePhaseCommit)
{
- return new OptimisticPrepareCommand(gtx, modifications, data, address,
onePhaseCommit);
+ return new OptimisticPrepareCommand(gtx, modifications, address, onePhaseCommit);
}
public AnnounceBuddyPoolNameCommand buildAnnounceBuddyPoolNameCommand(Address address,
String buddyPoolName)
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticReplicationInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticReplicationInterceptor.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticReplicationInterceptor.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -18,12 +18,12 @@
import org.jboss.cache.commands.tx.CommitCommand;
import org.jboss.cache.commands.tx.OptimisticPrepareCommand;
import org.jboss.cache.commands.tx.RollbackCommand;
+import org.jboss.cache.commands.write.ClearDataCommand;
import org.jboss.cache.commands.write.CreateNodeCommand;
import org.jboss.cache.commands.write.MoveCommand;
import org.jboss.cache.commands.write.PutDataMapCommand;
import org.jboss.cache.commands.write.PutForExternalReadCommand;
import org.jboss.cache.commands.write.PutKeyValueCommand;
-import org.jboss.cache.commands.write.ClearDataCommand;
import org.jboss.cache.commands.write.RemoveKeyCommand;
import org.jboss.cache.commands.write.RemoveNodeCommand;
import org.jboss.cache.factories.CommandsFactory;
@@ -78,7 +78,7 @@
TransactionEntry te = ctx.getTransactionEntry();
if (te.hasLocalModifications())
{
- OptimisticPrepareCommand replicablePrepareCommand = command.clone(); // makre
sure we remove any "local" transactions
+ OptimisticPrepareCommand replicablePrepareCommand = command.copy(); // makre
sure we remove any "local" transactions
replicablePrepareCommand.removeModifications(te.getLocalModifications());
command = replicablePrepareCommand;
}
@@ -182,7 +182,7 @@
// visit all elements in the collection and apply the DataVersionPopulator to
ensure all commands have data versions set.
populator.visitCollection(null, command.getModifications());
- ReplicableCommand toBroadcast =
commandsFactory.buildOptimisticPrepareCommand(gtx, populator.versionedCommands,
command.getData(), command.getLocalAddress(), command.isOnePhaseCommit());
+ ReplicableCommand toBroadcast =
commandsFactory.buildOptimisticPrepareCommand(gtx, populator.versionedCommands,
command.getLocalAddress(), command.isOnePhaseCommit());
//record the things we have possibly sent
broadcastTxs.add(gtx);
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticTxInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticTxInterceptor.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticTxInterceptor.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -114,7 +114,7 @@
protected PrepareCommand buildPrepareCommand(GlobalTransaction gtx, List
modifications, boolean onePhaseCommit)
{
// optimistic locking NEVER does one-phase prepares.
- return commandsFactory.buildOptimisticPrepareCommand(gtx, modifications, null,
rpcManager.getLocalAddress(), false);
+ return commandsFactory.buildOptimisticPrepareCommand(gtx, modifications,
rpcManager.getLocalAddress(), false);
}
/**
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/ReplicationInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/ReplicationInterceptor.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/ReplicationInterceptor.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -6,11 +6,11 @@
import org.jboss.cache.commands.tx.CommitCommand;
import org.jboss.cache.commands.tx.PrepareCommand;
import org.jboss.cache.commands.tx.RollbackCommand;
+import org.jboss.cache.commands.write.ClearDataCommand;
import org.jboss.cache.commands.write.MoveCommand;
import org.jboss.cache.commands.write.PutDataMapCommand;
import org.jboss.cache.commands.write.PutForExternalReadCommand;
import org.jboss.cache.commands.write.PutKeyValueCommand;
-import org.jboss.cache.commands.write.ClearDataCommand;
import org.jboss.cache.commands.write.RemoveKeyCommand;
import org.jboss.cache.commands.write.RemoveNodeCommand;
import org.jboss.cache.config.Configuration;
@@ -43,7 +43,7 @@
TransactionEntry te = ctx.getTransactionEntry();
if (te.hasLocalModifications())
{
- PrepareCommand replicablePrepareCommand = command.clone(); // makre sure we
remove any "local" transactions
+ PrepareCommand replicablePrepareCommand = command.copy(); // makre sure we
remove any "local" transactions
replicablePrepareCommand.removeModifications(te.getLocalModifications());
command = replicablePrepareCommand;
}
Modified: core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java 2008-06-25
14:15:51 UTC (rev 6035)
+++
core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java 2008-06-25
15:09:36 UTC (rev 6036)
@@ -6,9 +6,9 @@
import org.jboss.cache.Node;
import org.jboss.cache.NodeNotValidException;
import org.jboss.cache.NodeSPI;
-import org.jboss.cache.UnversionedNode;
import org.jboss.cache.config.Option;
import org.jboss.cache.lock.NodeLock;
+import org.jboss.cache.mvcc.InternalNode;
import org.jboss.cache.optimistic.DataVersion;
import org.jboss.cache.transaction.GlobalTransaction;
@@ -27,10 +27,10 @@
@SuppressWarnings("unchecked")
public class NodeInvocationDelegate<K, V> extends AbstractInvocationDelegate
implements NodeSPI<K, V>
{
- private final UnversionedNode node;
+ protected InternalNode node;
private CacheSPI<K, V> spi;
- public NodeInvocationDelegate(UnversionedNode node)
+ public NodeInvocationDelegate(InternalNode node)
{
this.node = node;
}
@@ -278,16 +278,7 @@
// TODO: Revisit. Is this really threadsafe? See comment in putIfAbsent() - same
solution should be applied here too.
assertValid();
Fqn nf = Fqn.fromRelativeFqn(getFqn(), f);
- Option o1;
- try
- {
- o1 = spi.getInvocationContext().getOptionOverrides().clone();
- }
- catch (CloneNotSupportedException e)
- {
- // should never happen
- throw new RuntimeException(e);
- }
+ Option o1 = spi.getInvocationContext().getOptionOverrides().copy();
Node<K, V> child = getChild(f);
if (child == null)
Modified: core/trunk/src/main/java/org/jboss/cache/mvcc/InternalNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/mvcc/InternalNode.java 2008-06-25 14:15:51
UTC (rev 6035)
+++ core/trunk/src/main/java/org/jboss/cache/mvcc/InternalNode.java 2008-06-25 15:09:36
UTC (rev 6036)
@@ -109,7 +109,7 @@
boolean isResident();
- InternalNode clone();
+ InternalNode copy();
NodeLock getLock();