You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by re...@apache.org on 2025/01/18 11:41:15 UTC

(sling-org-apache-sling-resourceresolver) branch master updated: Resource Resolver: refactor MapEntries - improve MapIterator test coverage

This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/master by this push:
     new b7ce082  Resource Resolver: refactor MapEntries - improve MapIterator test coverage
b7ce082 is described below

commit b7ce082eb5ae734b9cac0cc32ed46d60bc5b4648
Author: Julian Reschke <re...@apache.org>
AuthorDate: Sat Jan 18 12:41:08 2025 +0100

    Resource Resolver: refactor MapEntries - improve MapIterator test coverage
---
 .../resourceresolver/impl/mapping/MapEntries.java  | 43 +++++++++++++---------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index 3522adb..2442c54 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -73,6 +73,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Function;
 import java.util.stream.Stream;
 
 public class MapEntries implements
@@ -720,7 +721,8 @@ public class MapEntries implements
             key = requestPath.substring(secondIndex);
         }
 
-        return new MapEntryIterator(key, resolveMapsMap, this.factory.hasVanityPathPrecedence());
+        return new MapEntryIterator(key, resolveMapsMap.get(GLOBAL_LIST_KEY).iterator(),
+                this::getCurrentMapEntryForVanityPath, this.factory.hasVanityPathPrecedence());
     }
 
     @Override
@@ -731,7 +733,7 @@ public class MapEntries implements
     public boolean isOptimizeAliasResolutionEnabled() {
         return this.useOptimizeAliasResolution;
     }
-    
+
     @Override
     public @NotNull Map<String, Collection<String>> getAliasMap(final String parentPath) {
         Map<String, Collection<String>> aliasMapForParent = aliasMapsMap.get(parentPath);
@@ -1686,13 +1688,22 @@ public class MapEntries implements
                 log.error("A problem occured during initialization of optimize alias resolution. Optimize alias resolution is disabled. Check the logs for the reported problem.", e);
             }
         }
+    }
 
+    // return vanity path entry iterator from cache when complete and ready, otherwise from
+    // regular lockup
+    public @NotNull Iterator<MapEntry> getCurrentMapEntryForVanityPath(final String key) {
+        List<MapEntry> l;
+        if (this.isAllVanityPathEntriesCached() && this.vanityPathsProcessed.get()) {
+            l = this.resolveMapsMap.get(key);
+        } else {
+            l = this.getMapEntryList(key);
+        }
+        return l == null ? Collections.emptyIterator() : l.iterator();
     }
 
     private final class MapEntryIterator implements Iterator<MapEntry> {
 
-        private final Map<String, List<MapEntry>> resolveMapsMap;
-
         private String key;
 
         private MapEntry next;
@@ -1704,12 +1715,15 @@ public class MapEntries implements
         private MapEntry nextSpecial;
 
         private boolean vanityPathPrecedence;
+        private final Function<String, Iterator<MapEntry>> getCurrentMapEntryIteratorForVanityPath;
 
-        public MapEntryIterator(final String startKey, final Map<String, List<MapEntry>> resolveMapsMap, final boolean vanityPathPrecedence) {
+        public MapEntryIterator(final String startKey, @NotNull final Iterator<MapEntry> globalListIterator,
+                                final Function<String, Iterator<MapEntry>> getCurrentMapEntryIteratorForVanityPath,
+                                final boolean vanityPathPrecedence) {
             this.key = startKey;
-            this.resolveMapsMap = resolveMapsMap;
-            this.globalListIterator = this.resolveMapsMap.get(GLOBAL_LIST_KEY).iterator();
+            this.globalListIterator = globalListIterator;
             this.vanityPathPrecedence = vanityPathPrecedence;
+            this.getCurrentMapEntryIteratorForVanityPath = getCurrentMapEntryIteratorForVanityPath;
             this.seek();
         }
 
@@ -1758,15 +1772,8 @@ public class MapEntries implements
                         key = key.substring(0, lastDotPos);
                     }
 
-                    final List<MapEntry> special;
-                    if (MapEntries.this.isAllVanityPathEntriesCached() && MapEntries.this.vanityPathsProcessed.get()) {
-                        special = this.resolveMapsMap.get(key);
-                    } else {
-                        special = MapEntries.this.getMapEntryList(key); 
-                    }
-                    if (special != null) {
-                        specialIterator = special.iterator();
-                    }
+                    this.specialIterator = this.getCurrentMapEntryIteratorForVanityPath.apply(this.key);
+
                     // recurse to the parent
                     if (key.length() > 1) {
                         final int lastSlash = key.lastIndexOf("/");
@@ -1786,7 +1793,7 @@ public class MapEntries implements
             if (this.nextSpecial == null) {
                 this.next = this.nextGlobal;
                 this.nextGlobal = null;
-            } else if (!this.vanityPathPrecedence){
+            } else if (!vanityPathPrecedence) {
                 if (this.nextGlobal == null) {
                     this.next = this.nextSpecial;
                     this.nextSpecial = null;
@@ -1794,7 +1801,7 @@ public class MapEntries implements
                     this.next = this.nextGlobal;
                     this.nextGlobal = null;
 
-                }else {
+                } else {
                     this.next = this.nextSpecial;
                     this.nextSpecial = null;
                 }