Author: chetanm
Date: Tue Oct  3 05:32:03 2017
New Revision: 1810655

URL: http://svn.apache.org/viewvc?rev=1810655&view=rev
Log:
OAK-6714 - Support non root index in ContentMirrorStoreStrategy

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java?rev=1810655&r1=1810654&r2=1810655&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
 Tue Oct  3 05:32:03 2017
@@ -82,13 +82,33 @@ public class ContentMirrorStoreStrategy
     public static final int TRAVERSING_WARN = 
Integer.getInteger("oak.traversing.warn", 10000);
 
     private final String indexName;
+    private final String pathPrefix;
+    private final boolean prependPathPrefix;
 
     public ContentMirrorStoreStrategy() {
         this(INDEX_CONTENT_NODE_NAME);
     }
 
     public ContentMirrorStoreStrategy(String indexName) {
+        this(indexName, "", true);
+    }
+
+    /**
+     * Constructs a ContentMirrorStoreStrategy
+     *
+     * @param indexName name of sub node under which paths are stored
+     * @param pathPrefix path of the index in repository. Defaults to empty 
for indexes at root nodes i.e.
+     *                   those stored directly under '/oak:index'. For non 
root index its the path excluding
+     *                   the '/oak:index' node. For e.g. for index at 
'/content/oak:index/fooIndex' the
+     *                   pathPrefix would be '/content'.
+     *                   If this is appened to the paths returned by index 
then they would become absolute
+     *                   path in repository
+     * @param prependPathPrefix Should the path prefix be added to the query 
result
+     */
+    public ContentMirrorStoreStrategy(String indexName, String pathPrefix, 
boolean prependPathPrefix) {
         this.indexName = indexName;
+        this.pathPrefix = pathPrefix;
+        this.prependPathPrefix = prependPathPrefix;
     }
 
     @Override
@@ -148,7 +168,7 @@ public class ContentMirrorStoreStrategy
         return new Iterable<String>() {
             @Override
             public Iterator<String> iterator() {
-                PathIterator it = new PathIterator(filter, indexName, "");
+                PathIterator it = new PathIterator(filter, indexName, 
pathPrefix, prependPathPrefix);
                 if (values == null) {
                     it.setPathContainsValue(true);
                     it.enqueue(getChildNodeEntries(index).iterator());
@@ -330,6 +350,7 @@ public class ContentMirrorStoreStrategy
         private String parentPath;
         private String currentPath;
         private boolean pathContainsValue;
+        private final boolean prependPathPrefix;
         
         /**
          * Keep the returned path, to avoid returning duplicate entries.
@@ -337,7 +358,7 @@ public class ContentMirrorStoreStrategy
         private final Set<String> knownPaths = Sets.newHashSet();
         private final QueryLimits settings;
 
-        PathIterator(Filter filter, String indexName, String pathPrefix) {
+        PathIterator(Filter filter, String indexName, String pathPrefix, 
boolean prependPathPrefix) {
             this.filter = filter;
             this.pathPrefix = pathPrefix;
             this.indexName = indexName;
@@ -353,6 +374,7 @@ public class ContentMirrorStoreStrategy
             parentPath = "";
             currentPath = "/";
             this.settings = filter.getQueryLimits();
+            this.prependPathPrefix = prependPathPrefix;
         }
 
         void enqueue(Iterator<? extends ChildNodeEntry> it) {
@@ -460,7 +482,7 @@ public class ContentMirrorStoreStrategy
                 fetchNext();
                 init = true;
             }
-            String result = PathUtils.concat(pathPrefix, currentPath);
+            String result = prependPathPrefix ? PathUtils.concat(pathPrefix, 
currentPath) : currentPath;
             fetchNext();
             return result;
         }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java?rev=1810655&r1=1810654&r2=1810655&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java
 Tue Oct  3 05:32:03 2017
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.oak.plugins.index.property.strategy;
 
 import static com.google.common.base.Suppliers.memoize;
+import static com.google.common.collect.ImmutableList.copyOf;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Arrays.asList;
 import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.ENTRY_COUNT_PROPERTY_NAME;
@@ -26,6 +27,8 @@ import static org.apache.jackrabbit.oak.
 import static 
org.apache.jackrabbit.oak.plugins.index.counter.NodeCounterEditor.DEFAULT_RESOLUTION;
 import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
 import static 
org.apache.jackrabbit.oak.plugins.index.counter.ApproximateCounter.COUNT_PROPERTY_PREFIX;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.junit.Assert.assertThat;
 
 import java.util.Collections;
 import java.util.Set;
@@ -284,6 +287,42 @@ public class ContentMirrorStoreStrategyT
                                 KEY, maxTraversal));
     }
 
+    @Test
+    public void nonRootStorage() throws Exception{
+        IndexStoreStrategy store = new 
ContentMirrorStoreStrategy(INDEX_CONTENT_NODE_NAME, "/content", false);
+
+        NodeState root = EMPTY_NODE;
+        NodeBuilder builder = root.builder();
+        Supplier<NodeBuilder> index = () -> builder;
+
+        for (String path : asList("a", "a/c", "b")) {
+            store.update(index, path, null, null, EMPTY, KEY);
+        }
+
+        FilterImpl filter = FilterImpl.newTestInstance();
+        filter.restrictPath("/content", Filter.PathRestriction.ALL_CHILDREN);
+
+        NodeBuilder indexMeta = EMPTY_NODE.builder();
+        indexMeta.setChildNode(INDEX_CONTENT_NODE_NAME, 
builder.getNodeState());
+
+        Iterable<String> paths = store.query(filter, null, 
indexMeta.getNodeState(), KEY);
+        assertThat(copyOf(paths), containsInAnyOrder("a", "a/c", "b"));
+
+        FilterImpl filter2 = FilterImpl.newTestInstance();
+        filter2.restrictPath("/content/a", 
Filter.PathRestriction.ALL_CHILDREN);
+
+        paths = store.query(filter2, null, indexMeta.getNodeState(), KEY);
+        assertThat(copyOf(paths), containsInAnyOrder("a", "a/c"));
+
+        store = new ContentMirrorStoreStrategy(INDEX_CONTENT_NODE_NAME, 
"/content", true);
+
+        paths = store.query(filter, null, indexMeta.getNodeState(), KEY);
+        assertThat(copyOf(paths), containsInAnyOrder("/content/a", 
"/content/a/c", "/content/b"));
+
+        paths = store.query(filter2, null, indexMeta.getNodeState(), KEY);
+        assertThat(copyOf(paths), containsInAnyOrder("/content/a", 
"/content/a/c"));
+    }
+
     private static void assertInRange(String msg, double expected, double 
actual) {
         double allowedError = 0.1;
         double diff = Math.abs(expected - actual);


Reply via email to