Author: cziegeler
Date: Fri Oct 14 06:12:00 2016
New Revision: 1764815

URL: http://svn.apache.org/viewvc?rev=1764815&view=rev
Log:
SLING-6070 : Reduce temporary storage required in JcrResourceListener, call 
reportChanges earlier

Modified:
    sling/trunk/bundles/jcr/resource/pom.xml
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceChange.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
    
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/AbstractListenerTest.java
    
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java

Modified: sling/trunk/bundles/jcr/resource/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/pom.xml?rev=1764815&r1=1764814&r2=1764815&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/pom.xml (original)
+++ sling/trunk/bundles/jcr/resource/pom.xml Fri Oct 14 06:12:00 2016
@@ -167,7 +167,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.11.0</version>
+            <version>2.14.3-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceChange.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceChange.java?rev=1764815&r1=1764814&r2=1764815&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceChange.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceChange.java
 Fri Oct 14 06:12:00 2016
@@ -18,9 +18,6 @@
  */
 package org.apache.sling.jcr.resource.internal;
 
-import java.util.LinkedHashSet;
-import java.util.Set;
-
 import org.apache.sling.api.resource.observation.ResourceChange;
 
 public class JcrResourceChange extends ResourceChange {
@@ -28,7 +25,7 @@ public class JcrResourceChange extends R
     private final String userId;
 
     private JcrResourceChange(Builder builder) {
-        super(builder.changeType, builder.path, builder.isExternal, 
builder.addedAttributeNames, builder.changedAttributeNames, 
builder.removedAttributeNames);
+        super(builder.changeType, builder.path, builder.isExternal);
         this.userId = builder.userId;
     }
 
@@ -40,17 +37,11 @@ public class JcrResourceChange extends R
     @Override
     public String toString() {
         StringBuilder b = new StringBuilder();
-        b.append("ResourceChange[type=").append(this.getType()).append(", 
path=").append(this.getPath());
-        if (getAddedPropertyNames() != null && 
!getAddedPropertyNames().isEmpty()) {
-            b.append(", added=").append(getAddedPropertyNames());
-        }
-        if (getChangedPropertyNames() != null && 
!getChangedPropertyNames().isEmpty()) {
-            b.append(", changed=").append(getChangedPropertyNames());
-        }
-        if (getRemovedPropertyNames() != null && 
!getRemovedPropertyNames().isEmpty()) {
-            b.append(", removed=").append(getRemovedPropertyNames());
-        }
-        b.append("]");
+        b.append("ResourceChange[type=")
+          .append(this.getType())
+          .append(", path=")
+          .append(this.getPath())
+          .append("]");
         return b.toString();
     }
 
@@ -64,12 +55,6 @@ public class JcrResourceChange extends R
 
         private String userId;
 
-        private Set<String> changedAttributeNames;
-
-        private Set<String> addedAttributeNames;
-
-        private Set<String> removedAttributeNames;
-
         public String getPath() {
             return path;
         }
@@ -102,45 +87,6 @@ public class JcrResourceChange extends R
             this.userId = userId;
         }
 
-        public Set<String> getChangedAttributeNames() {
-            return changedAttributeNames;
-        }
-
-        public void addChangedAttributeName(String propName) {
-            if (changedAttributeNames == null) {
-                changedAttributeNames = new LinkedHashSet<String>();
-            }
-            if (!changedAttributeNames.contains(propName)) {
-                changedAttributeNames.add(propName);
-            }
-        }
-
-        public Set<String> getAddedAttributeNames() {
-            return addedAttributeNames;
-        }
-
-        public void addAddedAttributeName(String propName) {
-            if (addedAttributeNames == null) {
-                addedAttributeNames = new LinkedHashSet<String>();
-            }
-            if (!addedAttributeNames.contains(propName)) {
-                addedAttributeNames.add(propName);
-            }
-        }
-
-        public Set<String> getRemovedAttributeNames() {
-            return removedAttributeNames;
-        }
-
-        public void addRemovedAttributeName(String propName) {
-            if (removedAttributeNames == null) {
-                removedAttributeNames = new LinkedHashSet<String>();
-            }
-            if (!removedAttributeNames.contains(propName)) {
-                removedAttributeNames.add(propName);
-            }
-        }
-
         public ResourceChange build() {
             return new JcrResourceChange(this);
         }

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java?rev=1764815&r1=1764814&r2=1764815&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
 Fri Oct 14 06:12:00 2016
@@ -152,18 +152,13 @@ public class JcrResourceListener impleme
                      || type == PROPERTY_CHANGED ) {
                     final int lastSlash = eventPath.lastIndexOf('/');
                     final String nodePath = eventPath.substring(0, lastSlash);
-                    final String propName = eventPath.substring(lastSlash + 1);
                     Builder builder = changedEvents.get(nodePath);
                     if (builder == null) {
-                        changedEvents.put(nodePath, builder = 
createResourceChange(event, nodePath, ChangeType.CHANGED));
+                        changedEvents.put(nodePath, 
createResourceChange(event, nodePath, ChangeType.CHANGED));
                     }
-                    this.updateResourceChanged(builder, event.getType(), 
propName);
                 } else if ( type == NODE_ADDED ) {
                     addedEvents.put(eventPath, createResourceChange(event, 
ChangeType.ADDED));
                 } else if ( type == NODE_REMOVED) {
-                    // remove is the strongest operation, therefore remove all 
removed
-                    // paths from added
-                    addedEvents.remove(eventPath);
                     removedEvents.put(eventPath, createResourceChange(event, 
ChangeType.REMOVED));
                 }
             } catch (final RepositoryException e) {
@@ -171,6 +166,17 @@ public class JcrResourceListener impleme
             }
         }
 
+        // remove is the strongest operation, therefore remove all removed
+        // paths from added and changed
+        for(final String path : removedEvents.keySet()) {
+            addedEvents.remove(path);
+            changedEvents.remove(path);
+        }
+        // add is stronger than update
+        for(final String path : addedEvents.keySet()) {
+            changedEvents.remove(path);
+        }
+
         final List<ResourceChange> changes = new ArrayList<ResourceChange>();
         for (Entry<String, Builder> e : addedEvents.entrySet()) {
             String path = e.getKey();
@@ -194,20 +200,6 @@ public class JcrResourceListener impleme
         }
     }
 
-    private void updateResourceChanged(Builder builder, int eventType, final 
String propName) {
-        switch (eventType) {
-        case Event.PROPERTY_ADDED:
-            builder.addAddedAttributeName(propName);
-            break;
-        case Event.PROPERTY_CHANGED:
-            builder.addChangedAttributeName(propName);
-            break;
-        case Event.PROPERTY_REMOVED:
-            builder.addRemovedAttributeName(propName);
-            break;
-        }
-    }
-
     private Builder createResourceChange(final Event event, final String path, 
final ChangeType changeType) throws RepositoryException {
         Builder builder = new Builder();
         String strippedPath;

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java?rev=1764815&r1=1764814&r2=1764815&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
 Fri Oct 14 06:12:00 2016
@@ -191,10 +191,12 @@ public class JcrResourceProvider extends
     }
 
     private void registerListener(final ProviderContext ctx) {
-        try {
-            this.listener = new JcrResourceListener(ctx, root, pathMapper, 
repository);
-       } catch (RepositoryException e) {
-            throw new SlingException("Can't create the listener", e);
+        if ( this.repository != null ) {
+            try {
+                this.listener = new JcrResourceListener(ctx, root, pathMapper, 
repository);
+           } catch (RepositoryException e) {
+                throw new SlingException("Can't create the listener", e);
+            }
         }
     }
 

Modified: 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/AbstractListenerTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/AbstractListenerTest.java?rev=1764815&r1=1764814&r2=1764815&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/AbstractListenerTest.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/AbstractListenerTest.java
 Fri Oct 14 06:12:00 2016
@@ -67,7 +67,6 @@ public abstract class AbstractListenerTe
                 addPaths.add(event.getPath());
             } else if (event.getType() == ChangeType.CHANGED) {
                 modifyPaths.add(event.getPath());
-                assertEquals(Collections.singleton("foo"), 
event.getAddedPropertyNames());
             } else if (event.getType() == ChangeType.REMOVED) {
                 removePaths.add(event.getPath());
             } else {
@@ -168,6 +167,11 @@ public abstract class AbstractListenerTe
             };
             return Collections.singletonList(config);
         }
+
+        @Override
+        public void reportChanges(ObserverConfiguration config, 
Iterable<ResourceChange> changes, boolean distribute) {
+            this.reportChanges(changes, distribute);
+        }
     }
 
 }

Modified: 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java?rev=1764815&r1=1764814&r2=1764815&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
 Fri Oct 14 06:12:00 2016
@@ -102,6 +102,11 @@ public class JcrResourceListenerScalabil
                 }
 
                 @Override
+                public void reportChanges(ObserverConfiguration config, 
Iterable<ResourceChange> changes,
+                        boolean distribute) {
+                }
+
+                @Override
                 public List<ObserverConfiguration> getObserverConfigurations() 
{
                     ObserverConfiguration config = new ObserverConfiguration() 
{
 


Reply via email to