[jbosscache-commits] JBoss Cache SVN: r6331 - in core/trunk/src/main/java/org/jboss/cache: mvcc and 2 other directories.
jbosscache-commits at lists.jboss.org
jbosscache-commits at lists.jboss.org
Fri Jul 18 10:56:50 EDT 2008
Author: manik.surtani at jboss.com
Date: 2008-07-18 10:56:49 -0400 (Fri, 18 Jul 2008)
New Revision: 6331
Modified:
core/trunk/src/main/java/org/jboss/cache/AbstractNodeFactory.java
core/trunk/src/main/java/org/jboss/cache/NodeFactory.java
core/trunk/src/main/java/org/jboss/cache/PessimisticNodeFactory.java
core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/mvcc/MVCCNodeFactory.java
core/trunk/src/main/java/org/jboss/cache/optimistic/OptimisticNodeFactory.java
core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java
Log:
* Performance tweaks around now data in nodes are populated
* Better NodeFactory interface
* Safer node constructors
Modified: core/trunk/src/main/java/org/jboss/cache/AbstractNodeFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/AbstractNodeFactory.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/AbstractNodeFactory.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -56,10 +56,8 @@
this.lockStrategyFactory = lockStrategyFactory;
}
- public NodeSPI<K, V> createNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data, boolean mapSafe)
+ private NodeSPI<K, V> initializeNodeInvocationDelegate(UnversionedNode<K, V> internal)
{
- UnversionedNode<K, V> internal = createInternalNode(childName, fqn, parent, data, mapSafe);
-
// always assume that new nodes do not have data loaded
internal.setDataLoaded(false);
NodeSPI<K, V> nid = createNodeInvocationDelegate(internal, false);
@@ -69,8 +67,32 @@
return nid;
}
- protected UnversionedNode<K, V> createInternalNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data, boolean mapSafe)
+ public NodeSPI<K, V> createNode(Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data)
{
+ UnversionedNode<K, V> internal = createInternalNode(fqn.getLastElement(), fqn, parent, data);
+ return initializeNodeInvocationDelegate(internal);
+ }
+
+ public NodeSPI<K, V> createNode(Object childName, NodeSPI<K, V> parent, Map<K, V> data)
+ {
+ UnversionedNode<K, V> internal = createInternalNode(childName, Fqn.fromRelativeElements(parent.getFqn(), childName), parent, data);
+ return initializeNodeInvocationDelegate(internal);
+ }
+
+ public NodeSPI<K, V> createNode(Fqn fqn, NodeSPI<K, V> parent)
+ {
+ UnversionedNode<K, V> internal = createInternalNode(fqn.getLastElement(), fqn, parent, null);
+ return initializeNodeInvocationDelegate(internal);
+ }
+
+ public NodeSPI<K, V> createNode(Object childName, NodeSPI<K, V> parent)
+ {
+ UnversionedNode<K, V> internal = createInternalNode(childName, Fqn.fromRelativeElements(parent.getFqn(), childName), parent, null);
+ return initializeNodeInvocationDelegate(internal);
+ }
+
+ protected UnversionedNode<K, V> createInternalNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data)
+ {
throw new UnsupportedOperationException("Unsupported in this implementation (" + getClass().getSimpleName() + ")!");
}
@@ -86,7 +108,7 @@
public NodeSPI<K, V> createRootNode()
{
- return createNode(null, Fqn.ROOT, null, null, false);
+ return createNode(Fqn.ROOT, null);
}
public NodeSPI<K, V> createNodeInvocationDelegate(InternalNode<K, V> internalNode, boolean wrapWithNodeReference)
Modified: core/trunk/src/main/java/org/jboss/cache/NodeFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/NodeFactory.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/NodeFactory.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -19,8 +19,50 @@
WorkspaceNode<K, V> createWrappedNode(NodeSPI<K, V> dataNode, TransactionWorkspace workspace);
- NodeSPI<K, V> createNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data, boolean mapSafe);
+ /**
+ * Creates a new node and populates its attributes.
+ * <p/>
+ * Note that the data map passed in must not be null, and must not be referenced anywhere else as a defensive copy
+ * is NOT made when injecting it into the node.
+ *
+ * @param fqn
+ * @param parent
+ * @param data
+ * @return
+ */
+ NodeSPI<K, V> createNode(Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data);
+ /**
+ * Creates a new node and populates its attributes.
+ * <p/>
+ * Note that the data map passed in must not be null, and must not be referenced anywhere else as a defensive copy
+ * is NOT made when injecting it into the node.
+ *
+ * @param childName
+ * @param parent
+ * @param data
+ * @return
+ */
+ NodeSPI<K, V> createNode(Object childName, NodeSPI<K, V> parent, Map<K, V> data);
+
+ /**
+ * Creates a new, empty node.
+ *
+ * @param fqn
+ * @param parent
+ * @return
+ */
+ NodeSPI<K, V> createNode(Fqn fqn, NodeSPI<K, V> parent);
+
+ /**
+ * Creates a new, empty node.
+ *
+ * @param childName
+ * @param parent
+ * @return
+ */
+ NodeSPI<K, V> createNode(Object childName, NodeSPI<K, V> parent);
+
NodeSPI<K, V> createRootNode();
NodeSPI<K, V> createNodeInvocationDelegate(InternalNode<K, V> internalNode, boolean wrapWithNodeReference);
Modified: core/trunk/src/main/java/org/jboss/cache/PessimisticNodeFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/PessimisticNodeFactory.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/PessimisticNodeFactory.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -11,7 +11,7 @@
public class PessimisticNodeFactory<K, V> extends AbstractNodeFactory<K, V>
{
@Override
- protected UnversionedNode<K, V> createInternalNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data, boolean mapSafe)
+ protected UnversionedNode<K, V> createInternalNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data)
{
PessimisticUnversionedNode<K, V> internal = new PessimisticUnversionedNode<K, V>(childName, fqn, data, cache);
internal.injectDependencies(cache, commandsFactory, lockStrategyFactory, this);
Modified: core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -5,6 +5,7 @@
import org.jboss.cache.lock.IdentityLock;
import org.jboss.cache.transaction.GlobalTransaction;
+import java.util.HashMap;
import java.util.Map;
/**
@@ -22,9 +23,17 @@
*/
protected transient IdentityLock lock = null;
- public PessimisticUnversionedNode(Object name, Fqn fqn, Map data, CacheSPI<K, V> cache)
+ public PessimisticUnversionedNode(Object name, Fqn fqn, Map<K, V> data, CacheSPI<K, V> cache)
{
- super(name, fqn, data, cache);
+ super(fqn, cache);
+ if (!fqn.isRoot() && !name.equals(fqn.getLastElement()))
+ throw new IllegalArgumentException("Child " + name + " must be last part of " + fqn);
+
+
+ if (data != null && !data.isEmpty())
+ setInternalState(data);
+ else
+ this.data = new HashMap<K, V>();
}
// ------ lock-per-node paradigm
@@ -94,7 +103,7 @@
// construct the new child outside the synchronized block to avoid
// spending any more time than necessary in the synchronized section
Fqn childFqn = Fqn.fromRelativeElements(fqn, childName);
- NodeSPI<K, V> newChild = nodeFactory.createNode(childName, childFqn, delegate, null, true);
+ NodeSPI<K, V> newChild = nodeFactory.createNode(childFqn, delegate);
if (newChild == null)
{
throw new IllegalStateException();
Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -50,7 +50,7 @@
/**
* Map of general data keys to values.
*/
- final Map<K, V> data = new HashMap<K, V>();
+ protected HashMap<K, V> data;
protected NodeSPI<K, V> delegate;
CommandsFactory commandsFactory;
@@ -77,40 +77,23 @@
initFlags();
}
- /**
- * Constructs a new node with a name, etc.
- *
- * @param name name of current node
- * @param fqn Fqn of current node
- * @param data data to add to node
- * @param cache cache reference
- */
- @SuppressWarnings("unchecked")
- public UnversionedNode(Object name, Fqn fqn, Map data, CacheSPI<K, V> cache)
+ public UnversionedNode(Fqn fqn, CacheSPI<K, V> cache)
{
- if (cache == null)
- {
- throw new IllegalArgumentException("no cache init for " + fqn);
- }
- if (!fqn.isRoot() && !name.equals(fqn.getLastElement()))
- {
- throw new IllegalArgumentException("Child " + name + " must be last part of " + fqn);
- }
-
+ if (cache == null) throw new IllegalArgumentException("no cache init for " + fqn);
initFlags();
this.cache = cache;
this.fqn = fqn;
-
init();
- if (data != null && !data.isEmpty()) setInternalState(data);
-
// if this is a root node, create the child map.
- if (fqn.isRoot())
- {
- children = new ConcurrentHashMap<Object, Node<K, V>>(64, .5f, 16);
- }
+ if (fqn.isRoot()) children = new ConcurrentHashMap<Object, Node<K, V>>(64, .5f, 16);
}
+ public UnversionedNode(Fqn fqn, CacheSPI<K, V> cache, Map<K, V> data)
+ {
+ this(fqn, cache);
+ if (data != null) this.data = new HashMap<K, V>(data);
+ }
+
/**
* This method initialises flags on the node, by setting DATA_LOADED to true and VALID to true and all other flags to false.
* The flags are defined in the {@link NodeFlags} enum.
@@ -176,6 +159,12 @@
}
}
+ // does not need to be synchronized since this will only be accessed by a single thread in MVCC thanks to the write lock.
+ private void initDataMap()
+ {
+ if (data == null) data = new HashMap<K, V>();
+ }
+
public CacheSPI<K, V> getCache()
{
return cache;
@@ -205,7 +194,7 @@
public V getDirect(K key)
{
- return data.get(key);
+ return data == null ? null : data.get(key);
}
@SuppressWarnings("deprecation")
@@ -228,6 +217,7 @@
public V putDirect(K key, V value)
{
+ if (data == null) initDataMap();
return data.put(key, value);
}
@@ -249,7 +239,7 @@
if (createIfNotExists && child == null)
{
Fqn childFqn = Fqn.fromRelativeElements(fqn, childName);
- NodeSPI<K, V> newChild = nodeFactory.createNode(childName, childFqn, delegate, null, true);
+ NodeSPI<K, V> newChild = nodeFactory.createNode(childFqn, delegate);
child = (NodeSPI<K, V>) children().putIfAbsent(childName, newChild);
@@ -511,7 +501,7 @@
public void putAllDirect(Map<K, V> data)
{
- if (data == null) return;
+ if (this.data == null) initDataMap();
this.data.putAll(data);
}
@@ -709,9 +699,11 @@
setFlag(LOCK_FOR_CHILD_INSERT_REMOVE, lockForChildInsertRemove);
}
+ @SuppressWarnings("unchecked")
public InternalNode<K, V> copy()
{
- UnversionedNode<K, V> n = new UnversionedNode<K, V>(fqn.getLastElement(), fqn, data, cache);
+ UnversionedNode<K, V> n = new UnversionedNode<K, V>(fqn, cache);
+ if (data != null) n.data = (HashMap<K, V>) data.clone();
copyInternals(n);
return n;
}
@@ -729,8 +721,15 @@
public void setInternalState(Map<K, V> state)
{
- // don't bother doing anything here
- putAllDirect(state);
+ if (data == null)
+ {
+ data = state == null ? new HashMap<K, V>() : new HashMap<K, V>(state);
+ }
+ else
+ {
+ // don't bother doing anything here
+ putAllDirect(state);
+ }
}
public Map<K, V> getInternalState(boolean onlyInternalState)
Modified: core/trunk/src/main/java/org/jboss/cache/mvcc/MVCCNodeFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/mvcc/MVCCNodeFactory.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/mvcc/MVCCNodeFactory.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -49,10 +49,8 @@
return rcn;
}
- @Override
- public NodeSPI<K, V> createNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data, boolean mapSafe)
+ private NodeSPI<K, V> initializeNodeInvocationDelegate(UnversionedNode<K, V> internal)
{
- UnversionedNode<K, V> internal = new UnversionedNode<K, V>(childName, fqn, data, cache);
internal.injectDependencies(cache, commandsFactory, lockStrategyFactory, this);
// always assume that new nodes do not have data loaded
@@ -65,6 +63,32 @@
}
@Override
+ public NodeSPI<K, V> createNode(Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data)
+ {
+ UnversionedNode<K, V> internal = new UnversionedNode<K, V>(fqn, cache, data);
+ return initializeNodeInvocationDelegate(internal);
+ }
+
+ @Override
+ public NodeSPI<K, V> createNode(Fqn fqn, NodeSPI<K, V> parent)
+ {
+ UnversionedNode<K, V> internal = new UnversionedNode<K, V>(fqn, cache);
+ return initializeNodeInvocationDelegate(internal);
+ }
+
+ @Override
+ public NodeSPI<K, V> createNode(Object childName, NodeSPI<K, V> parent, Map<K, V> data)
+ {
+ return createNode(Fqn.fromRelativeElements(parent.getFqn(), childName), parent, data);
+ }
+
+ @Override
+ public NodeSPI<K, V> createNode(Object childName, NodeSPI<K, V> parent)
+ {
+ return createNode(Fqn.fromRelativeElements(parent.getFqn(), childName), parent);
+ }
+
+ @Override
public NodeSPI<K, V> createNodeInvocationDelegate(InternalNode<K, V> internalNode, boolean wrapWithNodeReference)
{
if (wrapWithNodeReference && internalNode instanceof NodeReference)
Modified: core/trunk/src/main/java/org/jboss/cache/optimistic/OptimisticNodeFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/optimistic/OptimisticNodeFactory.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/optimistic/OptimisticNodeFactory.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -17,7 +17,7 @@
public class OptimisticNodeFactory<K, V> extends AbstractNodeFactory<K, V>
{
@Override
- protected UnversionedNode<K, V> createInternalNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data, boolean mapSafe)
+ protected UnversionedNode<K, V> createInternalNode(Object childName, Fqn fqn, NodeSPI<K, V> parent, Map<K, V> data)
{
VersionedNode<K, V> internal = new VersionedNode<K, V>(fqn, parent, data, cache);
internal.injectDependencies(cache, commandsFactory, lockStrategyFactory, this);
Modified: core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -238,7 +238,7 @@
return null;
}
- NodeSPI<K, V> child = nodeFactory.createNode(childName, Fqn.fromRelativeElements(parent.getFqn(), childName), parent, null, true);
+ NodeSPI<K, V> child = nodeFactory.createNode(childName, parent);
getChildrenAddedSet().add(child.getFqn());
if (childrenRemoved != null) childrenRemoved.remove(child.getFqn());
setFlag(CHILDREN_MODIFIED_IN_WORKSPACE);
Modified: core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java 2008-07-18 14:22:57 UTC (rev 6330)
+++ core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java 2008-07-18 14:56:49 UTC (rev 6331)
@@ -283,7 +283,6 @@
int target_level = parent_level + 1;
Fqn fqn;
int size;
- Object name;
NodeData nd = nodeDataIterator.hasNext() ? nodeDataIterator.next() : null;
while (nd != null && !nd.isMarker())
{
@@ -304,14 +303,12 @@
throw new IllegalStateException("NodeData " + fqn + " is not a direct child of " + parent.getFqn());
}
- name = fqn.get(size - 1);
-
Map attrs = nd.getAttributes();
// We handle this NodeData. Create a TreeNode and
// integrate its data
- NodeSPI target = factory.createNode(name, fqn, parent, attrs, false);
- parent.addChild(name, target);
+ NodeSPI target = factory.createNode(fqn, parent, attrs);
+ parent.addChild(fqn.getLastElement(), target);
// JBCACHE-913
Region region = cache.getRegion(fqn, false);
@@ -406,7 +403,7 @@
// Missing level -- have to create empty node
// This shouldn't really happen -- internal fqns should
// be immediately under the root
- child = factory.createNode(name, Fqn.fromRelativeElements(ancFqn, name), ancestor, null, true);
+ child = factory.createNode(name, ancestor);
ancestor.addChild(name, child);
}
More information about the jbosscache-commits
mailing list