You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2023/07/23 10:16:36 UTC
[sling-org-apache-sling-servlets-resolver] branch SLING-11982-avoid-repo-access created (now 2d99034)
This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a change to branch SLING-11982-avoid-repo-access
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
at 2d99034 added unit test coverage
This branch includes the following new commits:
new 1f9372f SLING-11982 use a cache avoid repo access
new 2d99034 added unit test coverage
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[sling-org-apache-sling-servlets-resolver] 02/02: added unit test coverage
Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a commit to branch SLING-11982-avoid-repo-access
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
commit 2d99034e4497c83a8705d4f1200d664039fe3ef2
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Sun Jul 23 12:16:24 2023 +0200
added unit test coverage
---
.../helper/NamedScriptResourceCollector.java | 1 -
.../internal/helper/ResourceCollector.java | 1 -
.../helper/AbstractResourceCollectorTest.java | 83 ++++++++++++++++++++++
3 files changed, 83 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
index 2df58e5..2bf2668 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
@@ -18,7 +18,6 @@
*/
package org.apache.sling.servlets.resolver.internal.helper;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
index 0d436de..d159256 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
@@ -19,7 +19,6 @@
package org.apache.sling.servlets.resolver.internal.helper;
import java.util.Arrays;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollectorTest.java b/src/test/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollectorTest.java
new file mode 100644
index 0000000..9e3cdd3
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollectorTest.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.servlets.resolver.internal.helper;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class AbstractResourceCollectorTest {
+
+
+ @Rule
+ public SlingContext context = new SlingContext();
+
+
+ @Before
+ public void setup() {
+ context.create().resource("/parent");
+ context.create().resource("/parent/child1");
+ context.create().resource("/parent/child2");
+ context.create().resource("/parent/child3");
+ }
+
+ @Test
+ public void testWithCachingEnabled() {
+
+ Resource spy = Mockito.spy(context.resourceResolver().getResource("/parent"));
+
+ List<Resource> children = AbstractResourceCollector.getChildrenList(spy, true);
+ assertEquals(3,children.size());
+ children = AbstractResourceCollector.getChildrenList(spy, true);
+ Mockito.verify(spy,Mockito.times(1)).listChildren();
+ }
+
+
+ @Test
+ public void testWithCachingDisabled() {
+
+ Resource spy = Mockito.spy(context.resourceResolver().getResource("/parent"));
+
+ List<Resource> children = AbstractResourceCollector.getChildrenList(spy, false);
+ assertEquals(3,children.size());
+ children = AbstractResourceCollector.getChildrenList(spy, false);
+ Mockito.verify(spy,Mockito.times(2)).listChildren();
+ }
+
+ @Test
+ public void testWithCachingWithAlreadyUsedCacheKey() {
+
+ Resource spy = Mockito.spy(context.resourceResolver().getResource("/parent"));
+ String payload = "some payload";
+ context.resourceResolver().getPropertyMap().put(AbstractResourceCollector.CACHE_KEY_CHILDREN_LIST, payload);
+
+ List<Resource> children = AbstractResourceCollector.getChildrenList(spy, true);
+ assertEquals(3,children.size());
+ children = AbstractResourceCollector.getChildrenList(spy, true);
+ Mockito.verify(spy,Mockito.times(2)).listChildren();
+ assertEquals(payload, context.resourceResolver().getPropertyMap().get(AbstractResourceCollector.CACHE_KEY_CHILDREN_LIST));
+ }
+}
[sling-org-apache-sling-servlets-resolver] 01/02: SLING-11982 use a cache avoid repo access
Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a commit to branch SLING-11982-avoid-repo-access
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
commit 1f9372f37a28ea1e7d04d009c22bb5f626f36558
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Sun Jul 23 11:54:56 2023 +0200
SLING-11982 use a cache avoid repo access
---
.../internal/helper/AbstractResourceCollector.java | 59 ++++++++++++++++++++++
.../helper/NamedScriptResourceCollector.java | 6 +--
.../internal/helper/ResourceCollector.java | 6 +--
3 files changed, 65 insertions(+), 6 deletions(-)
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
index 4f90c97..77ac48b 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
@@ -20,7 +20,10 @@ package org.apache.sling.servlets.resolver.internal.helper;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -30,6 +33,8 @@ import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.SyntheticResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* The <code>ResourceCollector</code> class provides a single public method -
@@ -38,6 +43,13 @@ import org.apache.sling.api.resource.SyntheticResource;
* script to handle a request to the given resource.
*/
public abstract class AbstractResourceCollector {
+
+
+
+ private static final Logger LOG = LoggerFactory.getLogger(AbstractResourceCollector.class);
+
+
+ protected final static String CACHE_KEY_CHILDREN_LIST = AbstractResourceCollector.class.getName() + ".childrenList";
// the most generic resource type to use. This may be null in which
// case the default servlet name will be used as the base name
@@ -197,5 +209,52 @@ public abstract class AbstractResourceCollector {
}
return null;
}
+
+ /**
+ * Retrieves the list of children for a resource
+ * @param parent the resource for which the children should be retrieved
+ * @param useCaching if true try to read the list from the cache
+ * @return the children (or an empty list of no children are present)
+ */
+ static List<Resource> getChildrenList(Resource parent, boolean useCaching) {
+
+ List<Resource> childList = new ArrayList<Resource>();
+ if (useCaching) {
+
+ // init the caching structure
+ Map<String,List<Resource>> childrenListMap = new HashMap<String,List<Resource>>();
+ Map<String,Object> cache = parent.getResourceResolver().getPropertyMap();
+ if (!cache.containsKey(CACHE_KEY_CHILDREN_LIST)) {
+ childrenListMap = new HashMap<String,List<Resource>>();
+ cache.put(CACHE_KEY_CHILDREN_LIST, childrenListMap);
+
+ } else {
+ Object entry = cache.get(CACHE_KEY_CHILDREN_LIST);
+ if (entry instanceof HashMap) {
+ childrenListMap = (Map<String,List<Resource>>) cache.get(CACHE_KEY_CHILDREN_LIST);
+ } else {
+ // unexpected type
+ LOG.debug("Found key '{}' used with the unexpected type '{}', not caching the resource children list",
+ CACHE_KEY_CHILDREN_LIST, entry.getClass().getName());
+ }
+ }
+
+ // lookup
+ if (childrenListMap.containsKey(parent.getPath())) {
+ // this is a cache hit
+ List<Resource> result = childrenListMap.get(parent.getPath());
+ LOG.trace("getChildrenList cache-hit for {} with {} child resources", parent.getPath(), result.size());
+ return result;
+ }
+ // it's a cache miss
+ childrenListMap.put(parent.getPath(),childList);
+ }
+ Iterator<Resource> childrenIterator = parent.listChildren();
+ while (childrenIterator.hasNext()) {
+ childList.add(childrenIterator.next());
+ }
+ LOG.trace("getChildrenList cache-miss for {} with {} child resources", parent.getPath(),childList.size());
+ return childList;
+ }
}
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
index 07c32b4..2df58e5 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
@@ -19,6 +19,7 @@
package org.apache.sling.servlets.resolver.internal.helper;
import java.util.Iterator;
+import java.util.List;
import java.util.Set;
import org.apache.sling.api.resource.Resource;
@@ -113,9 +114,8 @@ public class NamedScriptResourceCollector extends AbstractResourceCollector {
current = getResource(resolver, location.getPath() + '/' + this.scriptName.substring(0, pos));
name = this.scriptName.substring(pos + 1);
}
- final Iterator<Resource> children = resolver.listChildren(current);
- while (children.hasNext()) {
- final Resource child = children.next();
+ final List<Resource> children = getChildrenList(current, useResourceCaching);
+ for (Resource child: children) {
if ( SlingServletResolver.isPathAllowed(child.getPath(), this.executionPaths) ) {
final String currentScriptName = child.getName();
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
index df70b15..0d436de 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
@@ -20,6 +20,7 @@ package org.apache.sling.servlets.resolver.internal.helper;
import java.util.Arrays;
import java.util.Iterator;
+import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
@@ -228,9 +229,8 @@ public class ResourceCollector extends AbstractResourceCollector {
? requestSelectors[selIdx]
: null;
- Iterator<Resource> children = resolver.listChildren(current);
- while (children.hasNext()) { // NOSONAR
- Resource child = children.next();
+ List<Resource> children = getChildrenList(current, isDefaultExtension);
+ for (Resource child: children) {
if (!SlingServletResolver.isPathAllowed(child.getPath(), this.executionPaths)) {
continue;