Author: mircea.markus
Date: 2007-09-26 19:42:55 -0400 (Wed, 26 Sep 2007)
New Revision: 4513
Removed:
core/trunk/src/main/java/org/jboss/cache/MetadataNodeWrapper.java
Modified:
core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java
core/trunk/src/test/java/org/jboss/cache/api/ResidentNodesTest.java
Log:
JBCACHE-1154 - this would fix some tests roken indirectly. Also added some unit tests.
Modified: core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheImpl.java 2007-09-26 19:56:51 UTC (rev
4512)
+++ core/trunk/src/main/java/org/jboss/cache/CacheImpl.java 2007-09-26 23:42:55 UTC (rev
4513)
@@ -281,7 +281,7 @@
*/
public NodeSPI<K, V> getRoot()
{
- return MetadataNodeWrapper.wrapNodeData(root);
+ return root;
}
/**
Deleted: core/trunk/src/main/java/org/jboss/cache/MetadataNodeWrapper.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/MetadataNodeWrapper.java 2007-09-26 19:56:51
UTC (rev 4512)
+++ core/trunk/src/main/java/org/jboss/cache/MetadataNodeWrapper.java 2007-09-26 23:42:55
UTC (rev 4513)
@@ -1,420 +0,0 @@
-package org.jboss.cache;
-
-import org.jboss.cache.Node;
-import org.jboss.cache.Fqn;
-import org.jboss.cache.optimistic.DataVersion;
-import org.jboss.cache.lock.NodeLock;
-import org.jboss.cache.transaction.GlobalTransaction;
-
-import java.util.*;
-
-/**
- * Metadata information from class <tt>Node</tt> (metadata = all
configuration information except attribute map,
- * e.g. Node.isResident) is internally also held in the internal attribute map. The role
of this class is not the
- * expose those internal map information to the end user - so it wraps such a node and
expose it to the the user
- * removing wrapped info.
- *
- * @author <a href="mailto:mircea.markus@jboss.com">Mircea
Markus</a>
- * @since 2.1.0
- */
-public class MetadataNodeWrapper implements NodeSPI
-{
-
- public static final String JBOSSCACHE_INTERNAL_RESIDENT =
"_jbosscache.internal.resident";
-
- private Node wrappedNode;
-
- public MetadataNodeWrapper(Node wrappedNode)
- {
- this.wrappedNode = wrappedNode;
- }
-
- public NodeSPI getParent()
- {
- return wrapNodeData(wrappedNode);
- }
-
- public Set getChildren()
- {
- Iterator it = wrappedNode.getChildren().iterator();
- Set result = new HashSet();
- while (it.hasNext())
- {
- result.add(wrapNodeData((Node) it.next()));
- }
- return Collections.unmodifiableSet(result);
- }
-
- public Set getChildrenNames()
- {
- return wrappedNode.getChildrenNames();
- }
-
- public Map getData()
- {
- Map realData = wrappedNode.getData();
- if (!realData.containsKey(JBOSSCACHE_INTERNAL_RESIDENT))
- {
- return realData;
- } else
- {
- Map clearedData = new HashMap(realData);
- clearedData.remove(JBOSSCACHE_INTERNAL_RESIDENT);
- return Collections.unmodifiableMap(clearedData);
- }
- }
-
- public Set getKeys()
- {
- Set realSet = wrappedNode.getKeys();
- if (!realSet.contains(JBOSSCACHE_INTERNAL_RESIDENT))
- {
- return realSet;
- } else
- {
- Set clearedData = new HashSet(realSet);
- clearedData.remove(JBOSSCACHE_INTERNAL_RESIDENT);
- return Collections.unmodifiableSet(clearedData);
- }
- }
-
- public Fqn getFqn()
- {
- return wrappedNode.getFqn();
- }
-
- public Node addChild(Fqn f)
- {
- return wrapNodeData(wrappedNode.addChild(f));
- }
-
- public boolean removeChild(Fqn f)
- {
- return wrappedNode.removeChild(f);
- }
-
- public boolean removeChild(Object childName)
- {
- return wrappedNode.removeChild(childName);
- }
-
- public Node getChild(Fqn f)
- {
- return wrapNodeData(wrappedNode.getChild(f));
- }
-
- public Node getChild(Object name)
- {
- return wrapNodeData(wrappedNode.getChild(name));
- }
-
- public Object put(Object key, Object value)
- {
- return wrappedNode.put(key, value);
- }
-
- public Object putIfAbsent(Object key, Object value)
- {
- return wrappedNode.putIfAbsent(key, value);
- }
-
- public Object replace(Object key, Object value)
- {
- return wrappedNode.replace(key, value);
- }
-
- public boolean replace(Object key, Object oldValue, Object newValue)
- {
- return wrappedNode.replace(key, oldValue, newValue);
- }
-
- public void putAll(Map map)
- {
- wrappedNode.putAll(map);
- }
-
- public void replaceAll(Map map)
- {
- wrappedNode.replaceAll(map);
- }
-
- public Object get(Object key)
- {
- return wrappedNode.get(key);
- }
-
- public Object remove(Object key)
- {
- if (JBOSSCACHE_INTERNAL_RESIDENT.equals(key))
- {
- throw new IllegalArgumentException("The '" +
JBOSSCACHE_INTERNAL_RESIDENT + "' is an reserved key, please " +
- "refrain using it!");
- }
- return wrappedNode.remove(key);
- }
-
- public void clearData()
- {
- if (wrappedNode.get(JBOSSCACHE_INTERNAL_RESIDENT) == null)
- {
- wrappedNode.clearData();
- } else
- {
- Iterator it = wrappedNode.getKeys().iterator();
- while (it.hasNext())
- {
- Object key = it.next();
- if (!JBOSSCACHE_INTERNAL_RESIDENT.equals(key))
- {
- wrappedNode.remove(key);
- }
- }
- }
- }
-
- public int dataSize()
- {
- if (wrappedNode.getKeys().contains(JBOSSCACHE_INTERNAL_RESIDENT))
- {
- return wrappedNode.dataSize() - 1;
- }
- return wrappedNode.dataSize();
- }
-
- public boolean hasChild(Fqn f)
- {
- return wrappedNode.hasChild(f);
- }
-
- public boolean hasChild(Object o)
- {
- return wrappedNode.hasChild(o);
- }
-
- public boolean isValid()
- {
- return wrappedNode.isValid();
- }
-
- public boolean isResident()
- {
- return wrappedNode.isResident();
- }
-
- public void setResident(boolean resident)
- {
- wrappedNode.setResident(resident);
- }
-
- public static MetadataNodeWrapper wrapNodeData(Node node)
- {
- if (node == null)
- {
- return null;
- }
- if (node instanceof MetadataNodeWrapper)
- {
- return (MetadataNodeWrapper) node;
- } else
- {
- return new MetadataNodeWrapper(node);
- }
- }
-
-
- public boolean isChildrenLoaded()
- {
- return ((NodeSPI) wrappedNode).isChildrenLoaded();
- }
-
- public void setChildrenLoaded(boolean loaded)
- {
- ((NodeSPI) wrappedNode).setChildrenLoaded(loaded);
- }
-
- public boolean isDataLoaded()
- {
- return ((NodeSPI) wrappedNode).isDataLoaded();
- }
-
- public void setDataLoaded(boolean dataLoaded)
- {
- ((NodeSPI) wrappedNode).setDataLoaded(dataLoaded);
- }
-
- public Map getChildrenMapDirect()
- {
- return ((NodeSPI) wrappedNode).getChildrenMapDirect();
- }
-
- public void setChildrenMapDirect(Map children)
- {
- ((NodeSPI) wrappedNode).setChildrenMapDirect(children);
- }
-
- public NodeSPI getOrCreateChild(Object name, GlobalTransaction tx)
- {
- return ((NodeSPI) wrappedNode).getOrCreateChild(name, tx);
- }
-
- public NodeLock getLock()
- {
- return ((NodeSPI) wrappedNode).getLock();
- }
-
- public void setFqn(Fqn f)
- {
- ((NodeSPI) wrappedNode).setFqn(f);
- }
-
- public boolean isDeleted()
- {
- return ((NodeSPI) wrappedNode).isDeleted();
- }
-
- public void markAsDeleted(boolean marker)
- {
- ((NodeSPI) wrappedNode).markAsDeleted(marker);
- }
-
- public void markAsDeleted(boolean marker, boolean recursive)
- {
- ((NodeSPI) wrappedNode).markAsDeleted(marker, recursive);
- }
-
- public void addChild(Object nodeName, Node nodeToAdd)
- {
- ((NodeSPI) wrappedNode).addChild(nodeName, nodeToAdd);
- }
-
- public void printDetails(StringBuffer sb, int indent)
- {
- ((NodeSPI) wrappedNode).printDetails(sb, indent);
- }
-
- public void print(StringBuffer sb, int indent)
- {
- ((NodeSPI) wrappedNode).print(sb, indent);
- }
-
- public void setVersion(DataVersion version)
- {
- ((NodeSPI) wrappedNode).setVersion(version);
- }
-
- public DataVersion getVersion()
- {
- return ((NodeSPI) wrappedNode).getVersion();
- }
-
- public Set getChildrenDirect()
- {
- return ((NodeSPI) wrappedNode).getChildrenDirect();
- }
-
- public void removeChildrenDirect()
- {
- ((NodeSPI) wrappedNode).removeChildrenDirect();
- }
-
- public Set getChildrenDirect(boolean includeMarkedAsDeleted)
- {
- return ((NodeSPI) wrappedNode).getChildrenDirect(includeMarkedAsDeleted);
- }
-
- public NodeSPI getChildDirect(Object childName)
- {
- return ((NodeSPI) wrappedNode).getChildDirect(childName);
- }
-
- public NodeSPI addChildDirect(Fqn childName)
- {
- return ((NodeSPI) wrappedNode).addChildDirect(childName);
- }
-
- public void addChildDirect(NodeSPI child)
- {
- ((NodeSPI) wrappedNode).addChildDirect(child);
- }
-
- public NodeSPI getChildDirect(Fqn childName)
- {
- return ((NodeSPI) wrappedNode).getChildDirect(childName);
- }
-
- public boolean removeChildDirect(Fqn fqn)
- {
- return ((NodeSPI) wrappedNode).removeChildDirect(fqn);
- }
-
- public boolean removeChildDirect(Object childName)
- {
- return ((NodeSPI) wrappedNode).removeChildDirect(childName);
- }
-
- public Object removeDirect(Object key)
- {
- return ((NodeSPI) wrappedNode).removeDirect(key);
- }
-
- public Object putDirect(Object key, Object value)
- {
- return ((NodeSPI) wrappedNode).putDirect(key, value);
- }
-
- public void putAllDirect(Map data)
- {
- ((NodeSPI) wrappedNode).putAllDirect(data);
- }
-
- public Map getDataDirect()
- {
- return ((NodeSPI) wrappedNode).getDataDirect();
- }
-
- public Object getDirect(Object key)
- {
- return ((NodeSPI) wrappedNode).getDirect(key);
- }
-
- public void clearDataDirect()
- {
- ((NodeSPI) wrappedNode).clearDataDirect();
- }
-
- public Set getKeysDirect()
- {
- return ((NodeSPI) wrappedNode).getKeysDirect();
- }
-
- public Set getChildrenNamesDirect()
- {
- return ((NodeSPI) wrappedNode).getChildrenNamesDirect();
- }
-
- public CacheSPI getCache()
- {
- return ((NodeSPI) wrappedNode).getCache();
- }
-
- public boolean hasChildrenDirect()
- {
- return ((NodeSPI) wrappedNode).hasChildrenDirect();
- }
-
- public boolean isResidentDirect()
- {
- return ((NodeSPI) wrappedNode).isResidentDirect();
- }
-
-
- public boolean isLockForChildInsertRemove()
- {
- return wrappedNode.isLockForChildInsertRemove();
- }
-
- public void setLockForChildInsertRemove(boolean lockForChildInsertRemove)
- {
- wrappedNode.setLockForChildInsertRemove(lockForChildInsertRemove);
- }
-}
Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2007-09-26 19:56:51 UTC
(rev 4512)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2007-09-26 23:42:55 UTC
(rev 4513)
@@ -15,11 +15,7 @@
import org.jboss.cache.optimistic.DataVersion;
import org.jboss.cache.transaction.GlobalTransaction;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -38,6 +34,11 @@
private static final int INDENT = 4;
/**
+ * Metadata-entry needed for refering the node's metadata.
+ */
+ public static final String JBOSSCACHE_INTERNAL_RESIDENT =
"_jbosscache.internal.resident";
+
+ /**
* Debug log.
*/
private static Log log = LogFactory.getLog(UnversionedNode.class);
@@ -69,6 +70,7 @@
private final Map<K, V> data = new HashMap<K, V>();
private boolean lockForChildInsertRemove;
+ private int realSize;
/**
* Constructs a new node with an FQN of Root.
@@ -194,8 +196,17 @@
public Map<K, V> getData()
{
if (cache == null) return Collections.emptyMap();
- return cache.getData(getFqn());
-
+ Map<K, V> realData = cache.getData(getFqn());
+ if (!realData.containsKey(JBOSSCACHE_INTERNAL_RESIDENT))
+ {
+ return realData;
+ }
+ else
+ {
+ Map clearedData = new HashMap(realData);
+ clearedData.remove(JBOSSCACHE_INTERNAL_RESIDENT);
+ return Collections.unmodifiableMap(clearedData);
+ }
/*
Map<K, V> dMap = new HashMap<K, V>();
for (K k : cache.getKeys(getFqn()))
@@ -378,7 +389,13 @@
public void clearData()
{
+ Map realData = getDataDirect();
+ boolean isResident = isResidentDirect();
cache.removeData(getFqn());
+ if (isResident)
+ {
+ setResident(true);
+ }
}
public void clearDataDirect()
@@ -423,8 +440,21 @@
public Set<K> getKeys()
{
- Set<K> keys = cache.getKeys(getFqn());
- return keys == null ? Collections.<K>emptySet() :
Collections.<K>unmodifiableSet(keys);
+ Set<K> realSet = cache.getKeys(getFqn());
+ if (realSet == null)
+ {
+ Collections.<K>emptySet();
+ }
+ if (!realSet.contains(JBOSSCACHE_INTERNAL_RESIDENT))
+ {
+ return realSet;
+ }
+ else
+ {
+ Set clearedData = new HashSet(realSet);
+ clearedData.remove(JBOSSCACHE_INTERNAL_RESIDENT);
+ return Collections.unmodifiableSet(clearedData);
+ }
}
public Set<K> getKeysDirect()
@@ -494,7 +524,11 @@
public int dataSize()
{
- return cache.getKeys(getFqn()).size();
+ realSize = cache.getKeys(getFqn()).size();
+ if (getDataDirect().containsKey(JBOSSCACHE_INTERNAL_RESIDENT)) {
+ realSize--;
+ }
+ return realSize;
}
public boolean removeChild(Object childName)
Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java 2007-09-26
19:56:51 UTC (rev 4512)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/EvictionInterceptor.java 2007-09-26
23:42:55 UTC (rev 4513)
@@ -199,7 +199,7 @@
Fqn fqn = (Fqn) args[0];
Object key = args[1];
/*see hack comment inside PutKeyEvictionMethodHandler.extractEvictedEventNode
*/
- if (MetadataNodeWrapper.JBOSSCACHE_INTERNAL_RESIDENT.equals(key)) {
+ if (UnversionedNode.JBOSSCACHE_INTERNAL_RESIDENT.equals(key)) {
return null;
}
if (fqn != null && key != null
@@ -341,7 +341,7 @@
//by the eviction code, which would result in events being produced -> that
when consumed would produce
// other events (due to call to Node.isResident). The isResidentDirect call
cannot be used here
//the resident information is stored in the node attributes map directly
- if (MetadataNodeWrapper.JBOSSCACHE_INTERNAL_RESIDENT.equals(key))
+ if (UnversionedNode.JBOSSCACHE_INTERNAL_RESIDENT.equals(key))
{
return null;
}
Modified: core/trunk/src/test/java/org/jboss/cache/api/ResidentNodesTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/api/ResidentNodesTest.java 2007-09-26
19:56:51 UTC (rev 4512)
+++ core/trunk/src/test/java/org/jboss/cache/api/ResidentNodesTest.java 2007-09-26
23:42:55 UTC (rev 4513)
@@ -88,12 +88,12 @@
}
/**
- * The 'resident' attribute of the node is kept within node's attribute
map.
+ * The 'resident' attribute of the Node.getData() is not exposed to the user.
* This information won't be visible to the ouside clients if they are not using
the
* resident attribute. This would ensure a 100% backward comatibility. New clients
would need to be aware of this
* extra attribute, though.
*/
- public void testInternalStateNotVisibleOutside()
+ public void testInternalStateNotVisibleOutsideOnGet()
{
cache.put(getSubFqn("/a"), "k_a", "v_a");
cache.get(getSubFqn("/a")).setResident(true);
@@ -116,7 +116,50 @@
assertTrue(cache.getRoot().getChild(TEST_NODES_ROOT).getChild("a").isResident());
}
+
/**
+ * When Node.clearData is called the node does not lose its residency metadata.
+ */
+ public void testInternalStateNotVisibleOutsideOnClearData()
+ {
+ Fqn theNode = Fqn.fromString("/a_a");
+ cache.put(theNode, "key", "value");
+ cache.getRoot().getChild(theNode).setResident(true);
+ cache.getRoot().getChild(theNode).clearData();
+
assertFalse(cache.getRoot().getChild(theNode).getKeys().contains("key"));
+ assertTrue(cache.getRoot().getChild(theNode).isResident());
+ }
+
+ /**
+ * When Node.getKeys is called, the given key set does not contain the
'resident' metadata.
+ */
+ public void testInternalStateNotVisibleOutsideOnGetKeys()
+ {
+ Fqn theNode = Fqn.fromString("/a_a");
+ cache.put(theNode, "key", "value");
+
+ assertEquals(cache.getRoot().getChild(theNode).getKeys().size(), 1);
+
+ cache.getRoot().getChild(theNode).setResident(true);
+ assertEquals(cache.getRoot().getChild(theNode).getKeys().size(), 1);
+ }
+
+ /**
+ * When Node.dataSize is called the returned value does not reflect metadata
information.
+ */
+ public void testInternalStateNotVisibleOutsideOnDataSize()
+ {
+ Fqn theNode = Fqn.fromString("/a_a");
+ cache.put(theNode, "key", "value");
+
+ assertEquals(cache.getRoot().getChild(theNode).dataSize(), 1);
+
+ cache.getRoot().getChild(theNode).setResident(true);
+ assertEquals(cache.getRoot().getChild(theNode).dataSize(), 1);
+ }
+
+
+ /**
* When replication is on, we want to make sure that the operation will subscribe to
the global
* replication strategy.
*/