public void testDefensiveCopyOfChildren() { Node<Object, Object> rootNode = cache.getRoot(); Fqn childFqn = Fqn.fromString("/child"); rootNode.addChild(childFqn).put("k", "v"); Set<Node<Object, Object>> children = rootNode.getChildren(); Set<Object> childrenNames = rootNode.getChildrenNames(); assert childrenNames.size() == 1; assert childrenNames.contains(childFqn.getLastElement()); assert children.size() == 1; assert children.iterator().next().getFqn().equals(childFqn); // now change stuff. rootNode.addChild(Fqn.fromString("/child2")); // assert that the collections we initially got have not changed. assert childrenNames.size() == 1; assert childrenNames.contains(childFqn.getLastElement()); assert children.size() == 1; assert children.iterator().next().getFqn().equals(childFqn); }
public void testDuplicatePersistence() throws CacheLoaderException { cache.put(Fqn.fromElements("a", "b"), "k", "v"); assert "v".equals(cache.get(Fqn.fromElements("a", "b"), "k")); cache.stop(); cache.start(); cache.put(Fqn.fromElements("a", "b"), "k", "v"); assert "v".equals(cache.get(Fqn.fromElements("a", "b"), "k")); }
private boolean hasChild(AdvancedCache<?, ?> cache, Fqn f) { if (f.size() > 1) { // indirect child. Fqn absoluteFqn = Fqn.fromRelativeFqn(fqn, f); return exists(cache, absoluteFqn); } else { return hasChild(f.getLastElement()); } }
@Override public boolean hasChild(Fqn f) { if (f.size() > 1) { // indirect child. Fqn absoluteFqn = Fqn.fromRelativeFqn(fqn, f); return exists(absoluteFqn); } else { return hasChild(f.getLastElement()); } }
private Node<K, V> addChild(AdvancedCache<?, ?> cache, Fqn f) { startAtomic(); try { Fqn absoluteChildFqn = Fqn.fromRelativeFqn(fqn, f); // 1) first register it with the parent AtomicMap<Object, Fqn> structureMap = getStructure(cache); structureMap.put(f.getLastElement(), absoluteChildFqn); // 2) then create the structure and data maps createNodeInCache(cache, absoluteChildFqn); return new NodeImpl<K, V>(absoluteChildFqn, cache, batchContainer); } finally { endAtomic(); } }
private Node<K, V> getChild(AdvancedCache cache, Fqn f) { startAtomic(); try { if (hasChild(f)) return new NodeImpl<K, V>(Fqn.fromRelativeFqn(fqn, f), cache, batchContainer); else return null; } finally { endAtomic(); } }
private Node<K, V> getChild(AdvancedCache cache, Object name) { startAtomic(); try { if (hasChild(name)) return new NodeImpl<K, V>(Fqn.fromRelativeElements(fqn, name), cache, batchContainer); else return null; } finally { endAtomic(); } }
public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeImpl<?, ?> node = (NodeImpl<?, ?>) o; if (fqn != null ? !fqn.equals(node.fqn) : node.fqn != null) return false; return true; }
@Override public Node<K, V> getChild(Object name) { startAtomic(); try { if (hasChild(name)) return new NodeImpl<K, V>(Fqn.fromRelativeElements(fqn, name), cache, batchContainer); else return null; } finally { endAtomic(); } }
@Override public Node<K, V> getChild(Fqn f) { startAtomic(); try { if (hasChild(f)) return new NodeImpl<K, V>(Fqn.fromRelativeFqn(fqn, f), cache, batchContainer); else return null; } finally { endAtomic(); } }
public void testGetChildrenNames() throws Exception { Node<Object, Object> rootNode = cache.getRoot(); rootNode.addChild(A).put("k", "v"); rootNode.addChild(B).put("k", "v"); Set<Object> childrenNames = new HashSet<Object>(); childrenNames.add(A.getLastElement()); childrenNames.add(B.getLastElement()); assertEquals(childrenNames, rootNode.getChildrenNames()); // now delete a child, within a tx tm.begin(); rootNode.removeChild(B); assertFalse(rootNode.hasChild(B)); childrenNames.remove(B.getLastElement()); assertEquals(childrenNames, rootNode.getChildrenNames()); tm.commit(); assertEquals(childrenNames, rootNode.getChildrenNames()); }
@Test public void testCacheManager() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.transaction().invocationBatching().enable(); EmbeddedCacheManager cm = new DefaultCacheManager(builder.build()); Cache<String, String> cache = cm.getCache(); TreeCacheFactory tcf = new TreeCacheFactory(); TreeCache<String, String> tree = tcf.createTreeCache(cache); Fqn leafFqn = Fqn.fromElements("branch", "leaf"); Node<String, String> leaf = tree.getRoot().addChild(leafFqn); leaf.put("fruit", "orange"); cm.stop(); }
public void testGetChildAPI() { Node<Object, Object> rootNode = cache.getRoot(); // creates a Node<Object, Object> with fqn /a/b/c Node childA = rootNode.addChild(A); childA.addChild(B).addChild(C); rootNode.getChild(A).put("key", "value"); rootNode.getChild(A).getChild(B).put("key", "value"); rootNode.getChild(A).getChild(B).getChild(C).put("key", "value"); assertEquals("value", rootNode.getChild(A).get("key")); assertEquals("value", rootNode.getChild(A).getChild(B).get("key")); assertEquals("value", rootNode.getChild(A).getChild(B).getChild(C).get("key")); assertNull(rootNode.getChild(Fqn.fromElements("nonexistent"))); }
@SuppressWarnings("unchecked") private Node<String, Object> getNode(final String node) { LOGGER.debug("Fetching node for store {} from Infinispan", node); final StringBuilder merchantPath = new StringBuilder(); merchantPath.append(getRootName()).append(node); Fqn contentFilesFqn = Fqn.fromString(merchantPath.toString()); Node<String, Object> nd = cacheManager.getTreeCache().getRoot().getChild(contentFilesFqn); if (nd == null) { cacheManager.getTreeCache().getRoot().addChild(contentFilesFqn); nd = cacheManager.getTreeCache().getRoot().getChild(contentFilesFqn); } return nd; }
/** Remember, Fqns are relative!! */ public void testParentsAndChildren() { Node<Object, Object> rootNode = cache.getRoot(); Node<Object, Object> nodeA = rootNode.addChild(A); Node<Object, Object> nodeB = nodeA.addChild(B); Node<Object, Object> nodeC = nodeA.addChild(C); Node<Object, Object> nodeD = rootNode.addChild(D); assertEquals(rootNode, nodeA.getParent()); assertEquals(nodeA, nodeB.getParent()); assertEquals(nodeA, nodeC.getParent()); assertEquals(rootNode, nodeD.getParent()); assertTrue(rootNode.hasChild(A)); assertFalse(rootNode.hasChild(B)); assertFalse(rootNode.hasChild(C)); assertTrue(rootNode.hasChild(D)); assertTrue(nodeA.hasChild(B)); assertTrue(nodeA.hasChild(C)); assertEquals(nodeA, rootNode.getChild(A)); assertEquals(nodeD, rootNode.getChild(D)); assertEquals(nodeB, nodeA.getChild(B)); assertEquals(nodeC, nodeA.getChild(C)); assertTrue(nodeA.getChildren().contains(nodeB)); assertTrue(nodeA.getChildren().contains(nodeC)); assertEquals(2, nodeA.getChildren().size()); assertTrue(rootNode.getChildren().contains(nodeA)); assertTrue(rootNode.getChildren().contains(nodeD)); assertEquals(2, rootNode.getChildren().size()); assertEquals(true, rootNode.removeChild(A)); assertFalse(rootNode.getChildren().contains(nodeA)); assertTrue(rootNode.getChildren().contains(nodeD)); assertEquals(1, rootNode.getChildren().size()); assertEquals("double remove", false, rootNode.removeChild(A)); assertEquals("double remove", false, rootNode.removeChild(A.getLastElement())); }
public void testPersistence() throws CacheLoaderException { cache.put("/a/b/c", "key", "value"); assert "value".equals(cache.get("/a/b/c", "key")); assert store.containsKey(new NodeKey(Fqn.fromString("/a/b/c"), DATA)); assert "value".equals(nodeContentsInCacheStore(store, Fqn.fromString("/a/b/c")).get("key")); assert store.containsKey(new NodeKey(Fqn.fromString("/a/b/c"), STRUCTURE)); cache.stop(); cache.start(); assert "value".equals(cache.get("/a/b/c", "key")); assert store.containsKey(new NodeKey(Fqn.fromString("/a/b/c"), DATA)); assert "value".equals(nodeContentsInCacheStore(store, Fqn.fromString("/a/b/c")).get("key")); assert store.containsKey(new NodeKey(Fqn.fromString("/a/b/c"), STRUCTURE)); }
public void testBasicOperation() throws SystemException, NotSupportedException, HeuristicMixedException, HeuristicRollbackException, RollbackException { assertClusterSize("Should only be 2 caches in the cluster!!!", 2); Fqn f = Fqn.fromString("/test/data"); String k = "key", v = "value"; assertNull("Should be null", cache1.getRoot().getChild(f)); assertNull("Should be null", cache2.getRoot().getChild(f)); Node<Object, Object> node = cache1.getRoot().addChild(f); assertNotNull("Should not be null", node); TransactionManager tm = beginTransaction(cache1.getCache()); node.put(k, v); tm.commit(); assertEquals(v, node.get(k)); assertEquals(v, cache1.get(f, k)); assertEquals("Should have replicated", v, cache2.get(f, k)); }
/** * Creates a new Fqn whose ancestor has been replaced with the new ancestor passed in. * * @param oldAncestor old ancestor to replace * @param newAncestor nw ancestor to replace with * @return a new Fqn with ancestors replaced. */ public Fqn replaceAncestor(Fqn oldAncestor, Fqn newAncestor) { if (!isChildOf(oldAncestor)) throw new IllegalArgumentException("Old ancestor must be an ancestor of the current Fqn!"); Fqn subFqn = this.getSubFqn(oldAncestor.size(), size()); return Fqn.fromRelativeFqn(newAncestor, subFqn); }
public void testHashCodesAppendedCount() { List<Fqn> fqns = new ArrayList<Fqn>(); fqns.add(Fqn.ROOT); for (int i = 0; i < 256; i++) fqns.add(Fqn.fromString("/fqn" + i)); doTest(fqns); }
public int hashCode() { return (fqn != null ? fqn.hashCode() : 0); }
private Node<K, V> getParent(AdvancedCache<?, ?> cache) { if (fqn.isRoot()) return this; return new NodeImpl<K, V>(fqn.getParent(), cache, batchContainer); }
@Override public boolean removeChild(Fqn f) { return removeChild(f.getLastElement()); }
@Override public Node<K, V> getParent() { if (fqn.isRoot()) return this; return new NodeImpl<K, V>(fqn.getParent(), cache, batchContainer); }
/** * Tests {@link Node}-centric operations * * @author <a href="mailto:manik AT jboss DOT org">Manik Surtani</a> */ @Test(groups = "functional", testName = "api.tree.NodeAPITest") public class NodeAPITest extends SingleCacheManagerTest { static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn.fromString("/d"); Fqn A_B = Fqn.fromRelativeFqn(A, B); Fqn A_C = Fqn.fromRelativeFqn(A, C); TransactionManager tm; TreeCache<Object, Object> cache; @Override protected EmbeddedCacheManager createCacheManager() throws Exception { // start a single cache instance ConfigurationBuilder cb = getDefaultStandaloneCacheConfig(true); cb.invocationBatching().enable(); EmbeddedCacheManager cm = TestCacheManagerFactory.createCacheManager(cb); cache = new TreeCacheImpl<Object, Object>(cm.getCache()); tm = cache.getCache().getAdvancedCache().getTransactionManager(); return cm; } /** Test method behaves according to javadoc. */ public void testGetParent() { Node<Object, Object> rootNode = cache.getRoot(); assertEquals(rootNode, rootNode.getParent()); Node<Object, Object> nodeA = rootNode.addChild(A); assertEquals(rootNode, nodeA.getParent()); } public void testAddingData() { Node<Object, Object> rootNode = cache.getRoot(); Node<Object, Object> nodeA = rootNode.addChild(A); nodeA.put("key", "value"); assertEquals("value", nodeA.get("key")); } public void testAddingDataPutMap() { cache.put(A_B, Collections.singletonMap("key", "value")); assertEquals("value", cache.get(A_B, "key")); } public void testAddingDataPutKey() { cache.put(A_B, "key", "value"); assertEquals("value", cache.get(A_B, "key")); } public void testAddingDataTx() throws Exception { Node<Object, Object> rootNode = cache.getRoot(); tm.begin(); Node<Object, Object> nodeA = rootNode.addChild(A); nodeA.put("key", "value"); assertEquals("value", nodeA.get("key")); tm.commit(); } public void testOverwritingDataTx() throws Exception { Node<Object, Object> rootNode = cache.getRoot(); Node<Object, Object> nodeA = rootNode.addChild(A); nodeA.put("key", "value"); assertEquals("value", nodeA.get("key")); tm.begin(); rootNode.removeChild(A); cache.put(A, "k2", "v2"); tm.commit(); assertNull(nodeA.get("key")); assertEquals("v2", nodeA.get("k2")); } /** Remember, Fqns are relative!! */ public void testParentsAndChildren() { Node<Object, Object> rootNode = cache.getRoot(); Node<Object, Object> nodeA = rootNode.addChild(A); Node<Object, Object> nodeB = nodeA.addChild(B); Node<Object, Object> nodeC = nodeA.addChild(C); Node<Object, Object> nodeD = rootNode.addChild(D); assertEquals(rootNode, nodeA.getParent()); assertEquals(nodeA, nodeB.getParent()); assertEquals(nodeA, nodeC.getParent()); assertEquals(rootNode, nodeD.getParent()); assertTrue(rootNode.hasChild(A)); assertFalse(rootNode.hasChild(B)); assertFalse(rootNode.hasChild(C)); assertTrue(rootNode.hasChild(D)); assertTrue(nodeA.hasChild(B)); assertTrue(nodeA.hasChild(C)); assertEquals(nodeA, rootNode.getChild(A)); assertEquals(nodeD, rootNode.getChild(D)); assertEquals(nodeB, nodeA.getChild(B)); assertEquals(nodeC, nodeA.getChild(C)); assertTrue(nodeA.getChildren().contains(nodeB)); assertTrue(nodeA.getChildren().contains(nodeC)); assertEquals(2, nodeA.getChildren().size()); assertTrue(rootNode.getChildren().contains(nodeA)); assertTrue(rootNode.getChildren().contains(nodeD)); assertEquals(2, rootNode.getChildren().size()); assertEquals(true, rootNode.removeChild(A)); assertFalse(rootNode.getChildren().contains(nodeA)); assertTrue(rootNode.getChildren().contains(nodeD)); assertEquals(1, rootNode.getChildren().size()); assertEquals("double remove", false, rootNode.removeChild(A)); assertEquals("double remove", false, rootNode.removeChild(A.getLastElement())); } public void testImmutabilityOfData() { Node<Object, Object> rootNode = cache.getRoot(); rootNode.put("key", "value"); Map<Object, Object> m = rootNode.getData(); try { m.put("x", "y"); fail("Map should be immutable!!"); } catch (Exception e) { // expected } try { rootNode.getKeys().add(new Object()); fail("Key set should be immutable"); } catch (Exception e) { // expected } } public void testDefensiveCopyOfData() { Node<Object, Object> rootNode = cache.getRoot(); rootNode.put("key", "value"); Map<Object, Object> data = rootNode.getData(); Set<Object> keys = rootNode.getKeys(); assert keys.size() == 1; assert keys.contains("key"); assert data.size() == 1; assert data.containsKey("key"); // now change stuff. rootNode.put("key2", "value2"); // assert that the collections we initially got have not changed. assert keys.size() == 1; assert keys.contains("key"); assert data.size() == 1; assert data.containsKey("key"); } public void testDefensiveCopyOfChildren() { Node<Object, Object> rootNode = cache.getRoot(); Fqn childFqn = Fqn.fromString("/child"); rootNode.addChild(childFqn).put("k", "v"); Set<Node<Object, Object>> children = rootNode.getChildren(); Set<Object> childrenNames = rootNode.getChildrenNames(); assert childrenNames.size() == 1; assert childrenNames.contains(childFqn.getLastElement()); assert children.size() == 1; assert children.iterator().next().getFqn().equals(childFqn); // now change stuff. rootNode.addChild(Fqn.fromString("/child2")); // assert that the collections we initially got have not changed. assert childrenNames.size() == 1; assert childrenNames.contains(childFqn.getLastElement()); assert children.size() == 1; assert children.iterator().next().getFqn().equals(childFqn); } public void testImmutabilityOfChildren() { Node<Object, Object> rootNode = cache.getRoot(); rootNode.addChild(A); try { rootNode.getChildren().clear(); fail("Collection of child nodes returned in getChildren() should be immutable"); } catch (Exception e) { // expected } } public void testGetChildAPI() { Node<Object, Object> rootNode = cache.getRoot(); // creates a Node<Object, Object> with fqn /a/b/c Node childA = rootNode.addChild(A); childA.addChild(B).addChild(C); rootNode.getChild(A).put("key", "value"); rootNode.getChild(A).getChild(B).put("key", "value"); rootNode.getChild(A).getChild(B).getChild(C).put("key", "value"); assertEquals("value", rootNode.getChild(A).get("key")); assertEquals("value", rootNode.getChild(A).getChild(B).get("key")); assertEquals("value", rootNode.getChild(A).getChild(B).getChild(C).get("key")); assertNull(rootNode.getChild(Fqn.fromElements("nonexistent"))); } public void testClearingData() { Node<Object, Object> rootNode = cache.getRoot(); rootNode.put("k", "v"); rootNode.put("k2", "v2"); assertEquals(2, rootNode.getKeys().size()); rootNode.clearData(); assertEquals(0, rootNode.getKeys().size()); assertTrue(rootNode.getData().isEmpty()); } public void testClearingDataTx() throws Exception { Node<Object, Object> rootNode = cache.getRoot(); tm.begin(); rootNode.put("k", "v"); rootNode.put("k2", "v2"); assertEquals(2, rootNode.getKeys().size()); rootNode.clearData(); assertEquals(0, rootNode.getKeys().size()); assertTrue(rootNode.getData().isEmpty()); tm.commit(); assertTrue(rootNode.getData().isEmpty()); } public void testPutData() { Node<Object, Object> rootNode = cache.getRoot(); assertTrue(rootNode.getData().isEmpty()); Map<Object, Object> map = new HashMap<Object, Object>(); map.put("k1", "v1"); map.put("k2", "v2"); rootNode.putAll(map); assertEquals(2, rootNode.getData().size()); assertEquals("v1", rootNode.get("k1")); assertEquals("v2", rootNode.get("k2")); map.clear(); map.put("k3", "v3"); rootNode.putAll(map); assertEquals(3, rootNode.getData().size()); assertEquals("v1", rootNode.get("k1")); assertEquals("v2", rootNode.get("k2")); assertEquals("v3", rootNode.get("k3")); map.clear(); map.put("k4", "v4"); map.put("k5", "v5"); rootNode.replaceAll(map); assertEquals(2, rootNode.getData().size()); assertEquals("v4", rootNode.get("k4")); assertEquals("v5", rootNode.get("k5")); } public void testGetChildrenNames() throws Exception { Node<Object, Object> rootNode = cache.getRoot(); rootNode.addChild(A).put("k", "v"); rootNode.addChild(B).put("k", "v"); Set<Object> childrenNames = new HashSet<Object>(); childrenNames.add(A.getLastElement()); childrenNames.add(B.getLastElement()); assertEquals(childrenNames, rootNode.getChildrenNames()); // now delete a child, within a tx tm.begin(); rootNode.removeChild(B); assertFalse(rootNode.hasChild(B)); childrenNames.remove(B.getLastElement()); assertEquals(childrenNames, rootNode.getChildrenNames()); tm.commit(); assertEquals(childrenNames, rootNode.getChildrenNames()); } public void testDoubleRemovalOfData() throws Exception { assert tm.getTransaction() == null; cache.put("/foo/1/2/3", "item", 1); assert tm.getTransaction() == null; assert 1 == (Integer) cache.get("/foo/1/2/3", "item"); tm.begin(); assert 1 == (Integer) cache.get("/foo/1/2/3", "item"); cache.removeNode("/foo/1"); assertNull(cache.getNode("/foo/1")); assertNull(cache.get("/foo/1", "item")); cache.removeNode("/foo/1/2/3"); System.out.println("Cache: " + cache); assertNull(cache.get("/foo/1/2/3", "item")); assertNull(cache.get("/foo/1", "item")); tm.commit(); assertFalse(cache.exists("/foo/1")); assertNull(cache.get("/foo/1/2/3", "item")); assertNull(cache.get("/foo/1", "item")); } public void testDoubleRemovalOfData2() throws Exception { cache.put("/foo/1/2", "item", 1); tm.begin(); assertEquals(cache.get("/foo/1", "item"), null); cache.removeNode("/foo/1"); assertNull(cache.get("/foo/1", "item")); cache.removeNode("/foo/1/2"); assertNull(cache.get("/foo/1", "item")); tm.commit(); assertFalse(cache.exists("/foo/1")); assertNull(cache.get("/foo/1/2", "item")); assertNull(cache.get("/foo/1", "item")); } }
public void testHashCodesAlpha() { List<Fqn> fqns = new ArrayList<Fqn>(); fqns.add(Fqn.ROOT); for (int i = 0; i < 256; i++) fqns.add(Fqn.fromString("/" + Integer.toString(i, 36))); doTest(fqns); }
public boolean removeChild(AdvancedCache<?, ?> cache, Fqn f) { return removeChild(cache, f.getLastElement()); }