Author: asanso Date: Tue Jul 21 14:28:19 2015 New Revision: 1692128 URL: http://svn.apache.org/r1692128 Log: SLING-4891 - Improve MapEntries to cache searched vanity paths
Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java?rev=1692128&r1=1692127&r2=1692128&view=diff ============================================================================== --- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java (original) +++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java Tue Jul 21 14:28:19 2015 @@ -282,8 +282,7 @@ public class MapEntries implements Event timer.schedule(new BloomFilterTask(), 60 * 1000); final Map<String, List<String>> vanityTargets = this - .loadVanityPaths(resolver, resolveMapsMap, - createVanityBloomFilter); + .loadVanityPaths(createVanityBloomFilter); this.vanityTargets = vanityTargets; } } finally { @@ -1116,22 +1115,22 @@ public class MapEntries implements Event * Load vanity paths Search for all nodes inheriting the sling:VanityPath * mixin */ - private Map <String, List<String>> loadVanityPaths(final ResourceResolver resolver, final Map<String, List<MapEntry>> entryMap, boolean createVanityBloomFilter) { + private Map <String, List<String>> loadVanityPaths(boolean createVanityBloomFilter) { // sling:VanityPath (uppercase V) is the mixin name // sling:vanityPath (lowercase) is the property name final Map <String, List<String>> targetPaths = new ConcurrentHashMap <String, List<String>>(); final String queryString = "SELECT sling:vanityPath, sling:redirect, sling:redirectStatus FROM sling:VanityPath WHERE sling:vanityPath IS NOT NULL"; final Iterator<Resource> i = resolver.findResources(queryString, "sql"); - while (i.hasNext() && (createVanityBloomFilter || maxCachedVanityPathEntries < vanityCounter.longValue())) { + while (i.hasNext() && (createVanityBloomFilter ||maxCachedVanityPathEntries == -1 || vanityCounter.longValue() < maxCachedVanityPathEntries)) { final Resource resource = i.next(); if (maxCachedVanityPathEntries == -1 || vanityCounter.longValue() < maxCachedVanityPathEntries) { // fill up the cache and the bloom filter - loadVanityPath(resource, entryMap, targetPaths, true, + loadVanityPath(resource, resolveMapsMap, targetPaths, true, createVanityBloomFilter); } else { // fill up the bloom filter - loadVanityPath(resource, entryMap, targetPaths, false, + loadVanityPath(resource, resolveMapsMap, targetPaths, false, createVanityBloomFilter); } Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java?rev=1692128&r1=1692127&r2=1692128&view=diff ============================================================================== --- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java (original) +++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java Tue Jul 21 14:28:19 2015 @@ -1597,8 +1597,7 @@ public class MapEntriesTest { when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); when(justVanityPath.getPath()).thenReturn("/justVanityPath"); when(justVanityPath.getName()).thenReturn("justVanityPath"); - when(justVanityPath.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); - + when(justVanityPath.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() { @@ -1629,4 +1628,70 @@ public class MapEntriesTest { assertEquals(2, counter.longValue()); } + @Test + //SLING-4891 + public void test_loadVanityPaths() throws Exception { + Field field1 = MapEntries.class.getDeclaredField("maxCachedVanityPathEntries"); + field1.setAccessible(true); + field1.set(mapEntries, 2); + + final Resource justVanityPath = mock(Resource.class, "justVanityPath"); + when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); + when(justVanityPath.getPath()).thenReturn("/justVanityPath"); + when(justVanityPath.getName()).thenReturn("justVanityPath"); + when(justVanityPath.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); + + when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() { + + public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable { + if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) { + return Collections.singleton(justVanityPath).iterator(); + } else { + return Collections.<Resource> emptySet().iterator(); + } + } + }); + + Method method = MapEntries.class.getDeclaredMethod("loadVanityPaths", boolean.class); + method.setAccessible(true); + method.invoke(mapEntries, false); + + Field vanityCounter = MapEntries.class.getDeclaredField("vanityCounter"); + vanityCounter.setAccessible(true); + AtomicLong counter = (AtomicLong) vanityCounter.get(mapEntries); + assertEquals(2, counter.longValue()); + } + + @Test + //SLING-4891 + public void test_loadVanityPaths_1() throws Exception { + + final Resource justVanityPath = mock(Resource.class, "justVanityPath"); + when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); + when(justVanityPath.getPath()).thenReturn("/justVanityPath"); + when(justVanityPath.getName()).thenReturn("justVanityPath"); + when(justVanityPath.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); + + when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() { + + public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable { + if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) { + return Collections.singleton(justVanityPath).iterator(); + } else { + return Collections.<Resource> emptySet().iterator(); + } + } + }); + + Method method = MapEntries.class.getDeclaredMethod("loadVanityPaths", boolean.class); + method.setAccessible(true); + method.invoke(mapEntries, false); + + Field vanityCounter = MapEntries.class.getDeclaredField("vanityCounter"); + vanityCounter.setAccessible(true); + AtomicLong counter = (AtomicLong) vanityCounter.get(mapEntries); + assertEquals(2, counter.longValue()); + } + + }