jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1477630 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/PropertyState.java plugins/memory/ModifiedNodeState.java plugins/memory/MutableNodeState.java
Date Tue, 30 Apr 2013 13:45:05 GMT
Author: jukka
Date: Tue Apr 30 13:45:05 2013
New Revision: 1477630

URL: http://svn.apache.org/r1477630
Log:
OAK-781: Clarify / fix effects of MISSING_NODE as base state of NodeBuilder

Reduce the number of temporary ModifiedNodeState instances and PropertyState maps we need

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/PropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MutableNodeState.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/PropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/PropertyState.java?rev=1477630&r1=1477629&r2=1477630&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/PropertyState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/PropertyState.java
Tue Apr 30 13:45:05 2013
@@ -17,6 +17,9 @@
 package org.apache.jackrabbit.oak.api;
 
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.base.Function;
 
 /**
  * Immutable property state. A property consists of a name and a value.
@@ -116,4 +119,19 @@ public interface PropertyState {
      */
     int count();
 
+    /**
+     * Mapping from a PropertyState instance to its name.
+     */
+    Function<PropertyState, String> GET_NAME =
+            new Function<PropertyState, String>() {
+                @Override @Nullable
+                public String apply(@Nullable PropertyState input) {
+                    if (input != null) {
+                        return input.getName();
+                    } else {
+                        return null;
+                    }
+                }
+            };
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java?rev=1477630&r1=1477629&r2=1477630&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
Tue Apr 30 13:45:05 2013
@@ -17,16 +17,20 @@
 package org.apache.jackrabbit.oak.plugins.memory;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Predicates.in;
+import static com.google.common.base.Predicates.not;
 import static com.google.common.base.Predicates.notNull;
 import static com.google.common.collect.Collections2.filter;
 import static com.google.common.collect.Iterables.concat;
 import static com.google.common.collect.Iterables.filter;
 import static com.google.common.collect.Maps.filterValues;
+import static com.google.common.collect.Maps.newHashMap;
 import static java.util.Collections.emptyList;
 import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.MISSING_NODE;
 import static org.apache.jackrabbit.oak.plugins.memory.MemoryChildNodeEntry.iterable;
 
 import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -39,6 +43,7 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
 
 import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
 
@@ -47,12 +52,59 @@ import com.google.common.collect.Maps;
  */
 public class ModifiedNodeState extends AbstractNodeState {
 
-    static NodeState withProperties(
-            NodeState base, Map<String, ? extends PropertyState> properties) {
-        if (properties.isEmpty()) {
-            return base;
+    static long getPropertyCount(
+            NodeState base, Map<String, PropertyState> properties) {
+        long count = 0;
+        if (base.exists()) {
+            count = base.getPropertyCount();
+            for (Entry<String, PropertyState> entry : properties.entrySet()) {
+                if (base.hasProperty(entry.getKey())) {
+                    count--;
+                }
+                if (entry.getValue() != null) {
+                    count++;
+                }
+            }
+        }
+        return count;
+    }
+
+    static boolean hasProperty(
+            NodeState base, Map<String, PropertyState> properties,
+            String name) {
+        if (properties.containsKey(name)) {
+            return properties.get(name) != null;
         } else {
-            return new ModifiedNodeState(base, properties, ImmutableMap.<String, NodeState>of());
+            return base.hasProperty(name);
+        }
+    }
+
+    static PropertyState getProperty(
+            NodeState base, Map<String, PropertyState> properties,
+            String name) {
+        PropertyState property = properties.get(name);
+        if (property == null && !properties.containsKey(name)) {
+            property = base.getProperty(name);
+        }
+        return property;
+    }
+
+    static Iterable<? extends PropertyState> getProperties(
+            NodeState base, Map<String, PropertyState> properties,
+            boolean copy) {
+        if (!base.exists()) {
+            return emptyList();
+        } else if (properties.isEmpty()) {
+            return base.getProperties(); // shortcut
+        } else {
+            if (copy) {
+                properties = newHashMap(properties);
+            }
+            Predicate<PropertyState> predicate = Predicates.compose(
+                    not(in(properties.keySet())), PropertyState.GET_NAME);
+            return concat(
+                    filter(base.getProperties(), predicate),
+                    filter(properties.values(), notNull()));
         }
     }
 
@@ -67,7 +119,7 @@ public class ModifiedNodeState extends A
 
     static NodeState with(
             NodeState base,
-            Map<String, ? extends PropertyState> properties,
+            Map<String, PropertyState> properties,
             Map<String, ? extends NodeState> nodes) {
         if (properties.isEmpty() && nodes.isEmpty()) {
             return base;
@@ -103,7 +155,7 @@ public class ModifiedNodeState extends A
      * Set of added, modified or removed ({@code null} value)
      * property states.
      */
-    private final Map<String, ? extends PropertyState> properties;
+    private final Map<String, PropertyState> properties;
 
     /**
      * Set of added, modified or removed ({@code null} value)
@@ -111,13 +163,6 @@ public class ModifiedNodeState extends A
      */
     private final Map<String, ? extends NodeState> nodes;
 
-    private final Predicate<PropertyState> unmodifiedProperties = new Predicate<PropertyState>()
{
-        @Override
-        public boolean apply(PropertyState input) {
-            return !properties.containsKey(input.getName());
-        }
-    };
-
     private final Predicate<ChildNodeEntry> unmodifiedNodes = new Predicate<ChildNodeEntry>()
{
         @Override
         public boolean apply(ChildNodeEntry input) {
@@ -134,7 +179,7 @@ public class ModifiedNodeState extends A
 
     private ModifiedNodeState(
             @Nonnull NodeState base,
-            @Nonnull Map<String, ? extends PropertyState> properties,
+            @Nonnull Map<String, PropertyState> properties,
             @Nonnull Map<String, ? extends NodeState> nodes) {
         this.base = checkNotNull(base);
         this.properties = checkNotNull(properties);
@@ -160,47 +205,22 @@ public class ModifiedNodeState extends A
 
     @Override
     public long getPropertyCount() {
-        if (!exists()) {
-            return 0;
-        }
-        long count = base.getPropertyCount();
-
-        for (Map.Entry<String, ? extends PropertyState> entry : properties.entrySet())
{
-            if (base.hasProperty(entry.getKey())) {
-                count--;
-            }
-            if (entry.getValue() != null) {
-                count++;
-            }
-        }
+        return getPropertyCount(base, properties);
+    }
 
-        return count;
+    @Override
+    public boolean hasProperty(String name) {
+        return hasProperty(base, properties, name);
     }
 
     @Override
     public PropertyState getProperty(String name) {
-        PropertyState property = properties.get(name);
-        if (property != null) {
-            return property;
-        } else if (properties.containsKey(name)) {
-            return null; // removed
-        } else {
-            return base.getProperty(name);
-        }
+        return getProperty(base, properties, name);
     }
 
     @Override
     public Iterable<? extends PropertyState> getProperties() {
-        if (!exists()) {
-            return emptyList();
-        }
-        if (properties.isEmpty()) {
-            return base.getProperties(); // shortcut
-        } else {
-            return concat(
-                    filter(base.getProperties(), unmodifiedProperties),
-                    filter(properties.values(), notNull()));
-        }
+        return getProperties(base, properties, false);
     }
 
     @Override
@@ -378,4 +398,4 @@ public class ModifiedNodeState extends A
         }
     }
 
-}
\ No newline at end of file
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MutableNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MutableNodeState.java?rev=1477630&r1=1477629&r2=1477630&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MutableNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MutableNodeState.java
Tue Apr 30 13:45:05 2013
@@ -25,7 +25,6 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.MISSING_NODE;
 import static org.apache.jackrabbit.oak.plugins.memory.ModifiedNodeState.with;
 import static org.apache.jackrabbit.oak.plugins.memory.ModifiedNodeState.withNodes;
-import static org.apache.jackrabbit.oak.plugins.memory.ModifiedNodeState.withProperties;
 
 import java.util.Iterator;
 import java.util.Map;
@@ -376,27 +375,22 @@ class MutableNodeState extends AbstractN
 
     @Override
     public long getPropertyCount() {
-        assert base != null;
-        return withProperties(base, properties).getPropertyCount();
+        return ModifiedNodeState.getPropertyCount(base, properties);
     }
 
     @Override
     public boolean hasProperty(String name) {
-        assert base != null;
-        return withProperties(base, properties).hasProperty(name);
+        return ModifiedNodeState.hasProperty(base, properties, name);
     }
 
     @Override
     public PropertyState getProperty(String name) {
-        assert base != null;
-        return withProperties(base, properties).getProperty(name);
+        return ModifiedNodeState.getProperty(base, properties, name);
     }
 
     @Override @Nonnull
     public Iterable<? extends PropertyState> getProperties() {
-        assert base != null;
-        Map<String, PropertyState> copy = newHashMap(properties);
-        return withProperties(base, copy).getProperties();
+        return ModifiedNodeState.getProperties(base, properties, true);
     }
 
     @Override



Mime
View raw message