Author: manik.surtani(a)jboss.com
Date: 2008-11-28 10:54:45 -0500 (Fri, 28 Nov 2008)
New Revision: 7229
Added:
core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/
core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/NodeReplicatedMoveTest.java
Removed:
core/branches/flat/src/test/java/org/jboss/starobrno/tree/
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/tree/NodeImpl.java
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java
Log:
Fixed tree related stuff
Modified: core/branches/flat/src/main/java/org/jboss/starobrno/tree/NodeImpl.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/tree/NodeImpl.java 2008-11-28
04:54:27 UTC (rev 7228)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/tree/NodeImpl.java 2008-11-28
15:54:45 UTC (rev 7229)
@@ -22,6 +22,7 @@
package org.jboss.starobrno.tree;
import org.jboss.starobrno.Cache;
+import org.jboss.starobrno.atomic.AtomicMap;
import org.jboss.starobrno.util.Immutables;
import org.jboss.starobrno.util.Util;
@@ -107,9 +108,9 @@
{
Fqn absoluteChildFqn = Fqn.fromRelativeFqn(fqn, f);
NodeImpl child = new NodeImpl(absoluteChildFqn, cache);
- Map<Object, Fqn> s = getStructure();
+ AtomicMap<Object, Fqn> s = getStructure();
s.put(f.getLastElement(), absoluteChildFqn);
- updateStructure(s);
+// updateStructure(s);
createNodeInCache(absoluteChildFqn);
return child;
}
@@ -129,7 +130,7 @@
startAtomic();
try
{
- Map<Object, Fqn> s = getStructure();
+ AtomicMap<Object, Fqn> s = getStructure();
Fqn childFqn = s.remove(childName);
if (childFqn != null)
{
@@ -137,7 +138,7 @@
child.removeChildren();
cache.remove(new NodeKey(childFqn, NodeKey.Type.DATA));
cache.remove(new NodeKey(childFqn, NodeKey.Type.STRUCTURE));
- updateStructure(s);
+// updateStructure(s);
return true;
}
@@ -186,10 +187,7 @@
startAtomic();
try
{
- Map data = getDataInternal();
- Object result = data.put(key, value);
- putAll(data);
- return (V) result;
+ return getDataInternal().put(key, value);
}
finally
{
@@ -202,15 +200,10 @@
startAtomic();
try
{
- Map data = getData();
- Object result = null;
- if (!data.containsKey(key))
- {
- result = data.put(key, value);
- putAll(data);
- }
+ AtomicMap<K, V> data = getDataInternal();
+ if (!data.containsKey(key)) return data.put(key, value);
- return (V) result;
+ return null;
}
finally
{
@@ -223,8 +216,11 @@
startAtomic();
try
{
- Map<K, V> data = getDataInternal();
- return data.put(key, value);
+ AtomicMap<K, V> map = getDataInternal();
+ if (map.containsKey(key))
+ return map.put(key, value);
+ else
+ return null;
}
finally
{
@@ -237,8 +233,8 @@
startAtomic();
try
{
- Map data = getData();
- V old = (V) data.get(key);
+ AtomicMap<K, V> data = getDataInternal();
+ V old = data.get(key);
if (Util.safeEquals(oldValue, old))
{
data.put(key, newValue);
@@ -257,8 +253,7 @@
startAtomic();
try
{
- Map data = getDataInternal();
- data.putAll(map);
+ getDataInternal().putAll(map);
}
finally
{
@@ -271,7 +266,7 @@
startAtomic();
try
{
- Map data = getDataInternal();
+ AtomicMap<K, V> data = getDataInternal();
data.clear();
data.putAll(map);
}
@@ -291,8 +286,7 @@
startAtomic();
try
{
- Map<K, V> data = getDataInternal();
- return data.remove(key);
+ return getDataInternal().remove(key);
}
finally
{
@@ -362,19 +356,19 @@
}
}
- Map<K, V> getDataInternal()
+ AtomicMap<K, V> getDataInternal()
{
- return (Map<K, V>) atomicMapCache.getAtomicMap(dataKey);
+ return (AtomicMap<K, V>) atomicMapCache.getAtomicMap(dataKey);
}
- void updateStructure(Map<Object, Fqn> s)
- {
- cache.put(structureKey, s);
- }
+// void updateStructure(FastCopyHashMap<Object, Fqn> s)
+// {
+// cache.put(structureKey, s.clone());
+// }
- Map<Object, Fqn> getStructure()
+ AtomicMap<Object, Fqn> getStructure()
{
- return (Map<Object, Fqn>) cache.get(structureKey);
+ return atomicMapCache.getAtomicMap(structureKey, Object.class, Fqn.class);
}
public boolean equals(Object o)
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java 2008-11-28
04:54:27 UTC (rev 7228)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java 2008-11-28
15:54:45 UTC (rev 7229)
@@ -23,12 +23,10 @@
import org.jboss.starobrno.Cache;
import org.jboss.starobrno.CacheSPI;
+import org.jboss.starobrno.atomic.AtomicMap;
import org.jboss.starobrno.atomic.AtomicMapCache;
import org.jboss.starobrno.batch.AutoBatchSupport;
-import java.util.HashMap;
-import java.util.Map;
-
public class TreeStructureSupport extends AutoBatchSupport
{
AtomicMapCache atomicMapCache;
@@ -61,15 +59,16 @@
if (!fqn.isRoot())
{
if (!exists(parent)) createNodeInCache(parent);
- Map<Object, Fqn> parentStructure = getStructure(parent);
+ AtomicMap<Object, Fqn> parentStructure = getStructure(parent);
parentStructure.put(fqn.getLastElement(), fqn);
- updateStructure(parent, parentStructure);
+// updateStructure(parent, parentStructure);
}
NodeKey dataKey = new NodeKey(fqn, NodeKey.Type.DATA);
NodeKey structureKey = new NodeKey(fqn, NodeKey.Type.STRUCTURE);
if (cache.containsKey(dataKey) && cache.containsKey(structureKey))
return;
- Map<Object, Fqn> structure = new HashMap<Object, Fqn>();
- cache.put(structureKey, structure);
+// FastCopyHashMap<Object, Fqn> structure = new FastCopyHashMap<Object,
Fqn>();
+// cache.put(structureKey, structure);
+ atomicMapCache.getAtomicMap(structureKey);
atomicMapCache.getAtomicMap(dataKey);
}
finally
@@ -78,15 +77,15 @@
}
}
- Map<Object, Fqn> getStructure(Fqn fqn)
+ AtomicMap<Object, Fqn> getStructure(Fqn fqn)
{
- return (Map<Object, Fqn>) cache.get(new NodeKey(fqn,
NodeKey.Type.STRUCTURE));
+ return atomicMapCache.getAtomicMap(new NodeKey(fqn, NodeKey.Type.STRUCTURE),
Object.class, Fqn.class);
}
- void updateStructure(Fqn fqn, Map<Object, Fqn> structure)
- {
- cache.put(new NodeKey(fqn, NodeKey.Type.STRUCTURE), structure);
- }
+// void updateStructure(Fqn fqn, FastCopyHashMap<Object, Fqn> structure)
+// {
+// cache.put(new NodeKey(fqn, NodeKey.Type.STRUCTURE), structure.clone());
+// }
public static boolean isLocked(Cache c, Fqn fqn)
{
@@ -97,6 +96,7 @@
/**
* Visual representation of a tree
+ *
* @param cache cache to dump
* @return String rep
*/
@@ -108,7 +108,7 @@
// walk tree
sb.append("+ ").append(Fqn.SEPARATOR);
- if (details) sb.append(" ").append(cache.getRoot().getData());
+ if (details) sb.append(" ").append(cache.getRoot().getData());
sb.append("\n");
addChildren(cache.getRoot(), 1, sb, details);
return sb.toString();
@@ -116,14 +116,14 @@
private static void addChildren(Node<?, ?> node, int depth, StringBuilder sb,
boolean details)
{
- for (Node<?, ?> child: node.getChildren())
+ for (Node<?, ?> child : node.getChildren())
{
- for (int i=0; i<depth; i++) sb.append(" "); // indentations
+ for (int i = 0; i < depth; i++) sb.append(" "); // indentations
sb.append("+ ");
sb.append(child.getFqn().getLastElementAsString()).append(Fqn.SEPARATOR);
if (details) sb.append(" ").append(child.getData());
sb.append("\n");
addChildren(child, depth + 1, sb, details);
- }
+ }
}
}
Copied:
core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/NodeReplicatedMoveTest.java
(from rev 7228,
core/branches/flat/src/test/java/org/jboss/starobrno/tree/api/NodeReplicatedMoveTest.java)
===================================================================
---
core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/NodeReplicatedMoveTest.java
(rev 0)
+++
core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/NodeReplicatedMoveTest.java 2008-11-28
15:54:45 UTC (rev 7229)
@@ -0,0 +1,160 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at
gnu.org.
+ */
+package org.jboss.starobrno.api.tree;
+
+import org.jboss.cache.transaction.DummyTransactionManagerLookup;
+import org.jboss.starobrno.UnitTestCacheFactory;
+import org.jboss.starobrno.config.Configuration;
+import org.jboss.starobrno.tree.Fqn;
+import org.jboss.starobrno.tree.Node;
+import org.jboss.starobrno.tree.TreeCache;
+import org.jboss.starobrno.tree.TreeCacheImpl;
+import org.jboss.starobrno.util.TestingUtil;
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertNull;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+@Test(groups = {"functional", "jgroups", "pessimistic"},
testName = "api.NodeReplicatedMoveTest")
+public class NodeReplicatedMoveTest
+{
+ private class NodeReplicatedMoveTestTL
+ {
+ protected TreeCache<Object, Object> cache1;
+ protected TreeCache<Object, Object> cache2;
+ protected TransactionManager tm;
+ }
+
+ protected ThreadLocal<NodeReplicatedMoveTestTL> threadLocal = new
ThreadLocal<NodeReplicatedMoveTestTL>();
+
+ protected static final Fqn A = Fqn.fromString("/a"), B =
Fqn.fromString("/b"), C = Fqn.fromString("/c"), D =
Fqn.fromString("/d"), E = Fqn.fromString("/e");
+ protected static final Object k = "key", vA = "valueA", vB =
"valueB", vC = "valueC", vD = "valueD", vE =
"valueE";
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception
+ {
+ NodeReplicatedMoveTestTL tl = new NodeReplicatedMoveTestTL();
+ threadLocal.set(tl);
+ Configuration c = new Configuration();
+ c.setInvocationBatchingEnabled(true);
+ c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
+ c.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+ c.setSyncCommitPhase(true);
+ c.setSyncRollbackPhase(true);
+ // start a single cache instance
+ tl.cache1 = new TreeCacheImpl<Object, Object>(new
UnitTestCacheFactory<Object, Object>().createCache(c.clone()));
+ tl.cache2 = new TreeCacheImpl<Object, Object>(new
UnitTestCacheFactory<Object, Object>().createCache(c.clone()));
+ tl.tm = tl.cache1.getConfiguration().getRuntimeConfig().getTransactionManager();
+
+ TestingUtil.blockUntilViewsReceived(10000, tl.cache1.getCache(),
tl.cache2.getCache());
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown()
+ {
+ NodeReplicatedMoveTestTL tl = threadLocal.get();
+ if (tl != null)
+ {
+ TestingUtil.killTreeCaches(tl.cache1, tl.cache2);
+ threadLocal.set(null);
+ }
+ }
+
+
+ public void testReplicatability()
+ {
+ NodeReplicatedMoveTestTL tl = threadLocal.get();
+ Node<Object, Object> rootNode = tl.cache1.getRoot();
+
+ Node<Object, Object> nodeA = rootNode.addChild(A);
+ Node<Object, Object> nodeB = nodeA.addChild(B);
+
+ nodeA.put(k, vA);
+ nodeB.put(k, vB);
+
+ assertEquals(vA, tl.cache1.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache1.getRoot().getChild(A).getChild(B).get(k));
+
+ assertEquals(vA, tl.cache2.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache2.getRoot().getChild(A).getChild(B).get(k));
+
+ // now move...
+ tl.cache1.move(nodeB.getFqn(), Fqn.ROOT);
+
+ assertEquals(vA, tl.cache1.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache1.getRoot().getChild(B).get(k));
+
+ assertEquals(vA, tl.cache2.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache2.getRoot().getChild(B).get(k));
+ }
+
+ public void testReplTxCommit() throws Exception
+ {
+ NodeReplicatedMoveTestTL tl = threadLocal.get();
+ Node<Object, Object> rootNode = tl.cache1.getRoot();
+ Fqn A_B = Fqn.fromRelativeFqn(A, B);
+ Node<Object, Object> nodeA = rootNode.addChild(A);
+ Node<Object, Object> nodeB = nodeA.addChild(B);
+
+ nodeA.put(k, vA);
+ nodeB.put(k, vB);
+
+ assertEquals(vA, tl.cache1.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache1.getRoot().getChild(A).getChild(B).get(k));
+
+ assertEquals(vA, tl.cache2.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache2.getRoot().getChild(A).getChild(B).get(k));
+
+ // now move...
+ tl.tm.begin();
+ tl.cache1.move(nodeB.getFqn(), Fqn.ROOT);
+
+ assertEquals(vA, tl.cache1.get(A, k));
+ assertNull(tl.cache1.get(A_B, k));
+ assertEquals(vB, tl.cache1.get(B, k));
+ tl.tm.commit();
+
+ assertEquals(vA, tl.cache1.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache1.getRoot().getChild(B).get(k));
+ assertEquals(vA, tl.cache2.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache2.getRoot().getChild(B).get(k));
+
+ }
+
+ public void testReplTxRollback() throws Exception
+ {
+ NodeReplicatedMoveTestTL tl = threadLocal.get();
+ Node<Object, Object> rootNode = tl.cache1.getRoot();
+ Node<Object, Object> nodeA = rootNode.addChild(A);
+ Node<Object, Object> nodeB = nodeA.addChild(B);
+
+ nodeA.put(k, vA);
+ nodeB.put(k, vB);
+
+ assertEquals(vA, tl.cache1.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache1.getRoot().getChild(A).getChild(B).get(k));
+ assertEquals(vA, tl.cache2.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache2.getRoot().getChild(A).getChild(B).get(k));
+
+ // now move...
+ tl.tm.begin();
+ tl.cache1.move(nodeB.getFqn(), Fqn.ROOT);
+
+ assertEquals(vA, tl.cache1.get(A, k));
+ assertEquals(vB, tl.cache1.get(B, k));
+
+ tl.tm.rollback();
+
+ assertEquals(vA, tl.cache1.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache1.getRoot().getChild(A).getChild(B).get(k));
+ assertEquals(vA, tl.cache2.getRoot().getChild(A).get(k));
+ assertEquals(vB, tl.cache2.getRoot().getChild(A).getChild(B).get(k));
+ }
+}