[jbosscache-commits] JBoss Cache SVN: r4979 - core/trunk/src/main/java/org/jboss/cache/optimistic.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Jan 4 08:02:24 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-01-04 08:02:24 -0500 (Fri, 04 Jan 2008)
New Revision: 4979

Modified:
   core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java
Log:
Lazy construction of internal collections

Modified: core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java	2008-01-04 13:01:55 UTC (rev 4978)
+++ core/trunk/src/main/java/org/jboss/cache/optimistic/WorkspaceNodeImpl.java	2008-01-04 13:02:24 UTC (rev 4979)
@@ -44,8 +44,8 @@
    private boolean created;
    private boolean childrenModified;
    private Map<Object, NodeSPI<K, V>> optimisticChildNodeMap;
-   private Set<Fqn> childrenAdded = new HashSet<Fqn>();
-   private Set<Fqn> childrenRemoved = new HashSet<Fqn>();
+   private Set<Fqn> childrenAdded;// = new HashSet<Fqn>();
+   private Set<Fqn> childrenRemoved;// = new HashSet<Fqn>();
    private Map<K, V> optimisticDataMap;
    private boolean versioningImplicit = true; // default
 
@@ -61,7 +61,8 @@
       }
       this.node = node;
       this.workspace = workspace;
-      optimisticDataMap = new HashMap<K, V>(node.getDataDirect());
+      Map<K, V> nodeData = node.getDataDirect();
+      if (!nodeData.isEmpty()) optimisticDataMap = new HashMap<K, V>(nodeData);
       this.version = node.getVersion();
       if (version == null)
       {
@@ -69,6 +70,18 @@
       }
    }
 
+   protected Set<Fqn> getChildrenAddedSet()
+   {
+      if (childrenAdded == null) childrenAdded = new HashSet<Fqn>();
+      return childrenAdded;
+   }
+
+   protected Set<Fqn> getChildrenRemovedSet()
+   {
+      if (childrenRemoved == null) childrenRemoved = new HashSet<Fqn>();
+      return childrenRemoved;
+   }
+
    public boolean isChildrenModified()
    {
       return childrenModified;
@@ -117,6 +130,7 @@
    public V put(K key, V value)
    {
       modified = true;
+      if (optimisticDataMap == null) optimisticDataMap = new HashMap<K, V>();
       return optimisticDataMap.put(key, value);
 
    }
@@ -124,17 +138,19 @@
    public V remove(K key)
    {
       modified = true;
+      if (optimisticDataMap == null) return null;
       return optimisticDataMap.remove(key);
 
    }
 
    public V get(K key)
    {
-      return optimisticDataMap.get(key);
+      return optimisticDataMap == null ? null : optimisticDataMap.get(key);
    }
 
    public Set<K> getKeys()
    {
+      if (optimisticDataMap == null) return Collections.emptySet();
       return optimisticDataMap.keySet();
    }
 
@@ -149,8 +165,8 @@
       Set<Object> names = new HashSet<Object>(optimisticChildNodeMap.keySet());
 
       // process deltas
-      for (Fqn child : childrenAdded) names.add(child.getLastElement());
-      for (Fqn child : childrenRemoved) names.remove(child.getLastElement());
+      if (childrenAdded != null) for (Fqn child : childrenAdded) names.add(child.getLastElement());
+      if (childrenRemoved != null) for (Fqn child : childrenRemoved) names.remove(child.getLastElement());
 
       return names;
    }
@@ -170,11 +186,15 @@
    private void realPut(Map<K, V> data, boolean eraseData, boolean forceDirtyFlag)
    {
       if (forceDirtyFlag) modified = true;
-      if (eraseData)
+      if (eraseData && optimisticDataMap != null)
       {
          optimisticDataMap.clear();
       }
-      if (data != null) optimisticDataMap.putAll(data);
+      if (data != null)
+      {
+         if (optimisticDataMap == null) optimisticDataMap = new HashMap<K, V>();
+         optimisticDataMap.putAll(data);
+      }
    }
 
    public Node<K, V> getParent()
@@ -190,18 +210,10 @@
          return null;
       }
 
-      //see if we already have it
-//      NodeSPI<K, V> child = optimisticChildNodeMap.get(child_name);
-
-      // if not we need to create it
-//      if (child == null)
-//      {
       NodeFactory<K, V> factory = cache.getConfiguration().getRuntimeConfig().getNodeFactory();
       NodeSPI<K, V> child = (NodeSPI<K, V>) factory.createNodeOfType(parent, child_name, parent, null);
-//         optimisticChildNodeMap.put(child_name, child);
-      childrenAdded.add(child.getFqn());
-      childrenRemoved.remove(child.getFqn());
-//      }
+      getChildrenAddedSet().add(child.getFqn());
+      if (childrenRemoved != null) childrenRemoved.remove(child.getFqn());
       childrenModified = true;
       return child;
    }
@@ -238,17 +250,27 @@
       this.version = version;
    }
 
+   @SuppressWarnings("unchecked")
    public List<Set<Fqn>> getMergedChildren()
    {
-      //return optimisticChildNodeMap;
-      List<Set<Fqn>> l = new ArrayList<Set<Fqn>>(2);
-      l.add(childrenAdded);
-      l.add(childrenRemoved);
+      List l = new ArrayList(2);
+
+      if (childrenAdded != null)
+         l.add(childrenAdded);
+      else
+         l.add(Collections.emptySet());
+
+      if (childrenRemoved != null)
+         l.add(childrenRemoved);
+      else
+         l.add(Collections.emptySet());
+
       return l;
    }
 
    public Map<K, V> getMergedData()
    {
+      if (optimisticDataMap == null) return Collections.emptyMap();
       return optimisticDataMap;
    }
 
@@ -266,12 +288,18 @@
    {
       created = true;
       // created != modified!!!
-//      modified = true;
    }
 
    public Map<K, V> getData()
    {
-      return Collections.unmodifiableMap(optimisticDataMap);
+      if (optimisticDataMap == null)
+      {
+         return Collections.emptyMap();
+      }
+      else
+      {
+         return Collections.unmodifiableMap(optimisticDataMap);
+      }
    }
 
    public String toString()
@@ -322,21 +350,23 @@
 
    public void addChild(WorkspaceNode<K, V> child)
    {
-//      optimisticChildNodeMap.put(child.getFqn().getLastElement(), child.getNode());
-      childrenAdded.add(child.getFqn());
-      childrenRemoved.remove(child.getFqn());
+      getChildrenAddedSet().add(child.getFqn());
+      if (childrenRemoved != null) childrenRemoved.remove(child.getFqn());
       if (log.isTraceEnabled()) log.trace("Adding child " + child.getFqn());
    }
 
    public void clearData()
    {
-      modified = true;
-      optimisticDataMap.clear();
+      if (optimisticDataMap != null)
+      {
+         optimisticDataMap.clear();
+         modified = true;
+      }
    }
 
    public int dataSize()
    {
-      return optimisticDataMap.size();
+      return optimisticDataMap == null ? 0 : optimisticDataMap.size();
    }
 
    public boolean hasChild(Object o)
@@ -411,8 +441,8 @@
       Fqn childFqn = new Fqn(getFqn(), childName);
       /*if (n != null)
       {*/
-      childrenRemoved.add(childFqn);
-      childrenAdded.remove(childFqn);
+      getChildrenRemovedSet().add(childFqn);
+      if (childrenAdded != null) childrenAdded.remove(childFqn);
       childrenModified = true;
       return node.getChildDirect(childName) != null;
       /*}




More information about the jbosscache-commits mailing list