Author: catholicon
Date: Sun Sep 24 14:33:06 2017
New Revision: 1809519

URL: http://svn.apache.org/viewvc?rev=1809519&view=rev
Log:
OAK-6123: Filter documents not matching path restriction in LucenePropertyIndex 
even when evaluatePathRestriction isn't set

Only doing the improvement in LucenePropertyIndex - Aggregate(LuceneIndex) 
isn't being in use anymore anyway.

Added:
    
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/ResultCountingIndexProvider.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
    
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java?rev=1809519&r1=1809518&r2=1809519&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
 Sun Sep 24 14:33:06 2017
@@ -348,8 +348,10 @@ public class LucenePropertyIndex impleme
                         seenPaths.add(path);
                     }
 
-                    LOG.trace("Matched path {}", path);
-                    return new LuceneResultRow(path, doc.score, excerpt, 
facets, explanation);
+                    boolean shouldIncludeForHierarchy = shouldInclude(path, 
plan);
+                    LOG.trace("Matched path {}; shouldIncludeForHierarchy: 
{}", path, shouldIncludeForHierarchy);
+                    return shouldIncludeForHierarchy? new 
LuceneResultRow(path, doc.score, excerpt, facets, explanation)
+                            : null;
                 }
                 return null;
             }
@@ -603,6 +605,27 @@ public class LucenePropertyIndex impleme
         return query;
     }
 
+    private static boolean shouldInclude(String docPath, IndexPlan plan) {
+        String path = getPathRestriction(plan);
+
+        boolean include = true;
+
+        Filter filter = plan.getFilter();
+        switch (filter.getPathRestriction()) {
+            case EXACT:
+                include = path.equals(docPath);
+                break;
+            case DIRECT_CHILDREN:
+                include = PathUtils.getParentPath(docPath).equals(path);
+                break;
+            case ALL_CHILDREN:
+                include = PathUtils.isAncestor(path, docPath);
+                break;
+        }
+
+        return include;
+    }
+
     private String getExcerpt(Query query, Analyzer analyzer, IndexSearcher 
searcher, ScoreDoc doc,
                               FieldInfos fieldInfos) throws IOException {
         StringBuilder excerpt = new StringBuilder();

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java?rev=1809519&r1=1809518&r2=1809519&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java
 Sun Sep 24 14:33:06 2017
@@ -127,7 +127,6 @@ import org.apache.jackrabbit.oak.query.A
 import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
 import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
 import org.apache.jackrabbit.oak.spi.commit.Observer;
-import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
 import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
@@ -166,6 +165,8 @@ public class LucenePropertyIndexTest ext
 
     private LuceneIndexProvider provider;
 
+    private ResultCountingIndexProvider queryIndexProvider;
+
     @After
     public void after() {
         new ExecutorCloser(executorService).close();
@@ -182,11 +183,12 @@ public class LucenePropertyIndexTest ext
         IndexCopier copier = createIndexCopier();
         editorProvider = new LuceneIndexEditorProvider(copier, new 
ExtractedTextCache(10* FileUtils.ONE_MB, 100));
         provider = new LuceneIndexProvider(copier);
+        queryIndexProvider = new ResultCountingIndexProvider(provider);
         nodeStore = new MemoryNodeStore();
         return new Oak(nodeStore)
                 .with(new InitialContent())
                 .with(new OpenSecurityProvider())
-                .with((QueryIndexProvider) provider)
+                .with(queryIndexProvider)
                 .with((Observer) provider)
                 .with(editorProvider)
                 .with(optionalEditorProvider)
@@ -1152,6 +1154,54 @@ public class LucenePropertyIndexTest ext
     }
 
     @Test
+    public void queryPathRescrictionWithoutEvaluatePathRestriction() throws 
Exception {
+        Tree parent = root.getTree("/");
+        Tree idx = createIndex(parent, "test1", of());
+        idx.addChild(PROP_NODE).addChild("propa");
+
+        Tree test = parent.addChild("test");
+        test.addChild("a").addChild("c").addChild("d").setProperty("propa", 
"a");
+        test.addChild("b").addChild("c").addChild("d").setProperty("propa", 
"a");
+
+        parent = root.getTree("/").addChild("subRoot");
+        idx = createIndex(parent, "test1", of());
+        idx.addChild(PROP_NODE).addChild("propa");
+
+        test = parent.addChild("test");
+        test.addChild("a").addChild("c").addChild("d").setProperty("propa", 
"a");
+        test.addChild("b").addChild("c").addChild("d").setProperty("propa", 
"a");
+        root.commit();
+
+        queryIndexProvider.setShouldCount(true);
+
+        // Top level index
+        assertQuery("/jcr:root/test/a/c/d[@propa='a']", XPATH, 
asList("/test/a/c/d"));
+        assertEquals("Unexpected number of docs passed back to query engine", 
1, queryIndexProvider.getCount());
+        queryIndexProvider.reset();
+
+        assertQuery("/jcr:root/test/a/c/*[@propa='a']", XPATH, 
asList("/test/a/c/d"));
+        assertEquals("Unexpected number of docs passed back to query engine", 
1, queryIndexProvider.getCount());
+        queryIndexProvider.reset();
+
+        assertQuery("/jcr:root/test/a//*[@propa='a']", XPATH, 
asList("/test/a/c/d"));
+        assertEquals("Unexpected number of docs passed back to query engine", 
1, queryIndexProvider.getCount());
+        queryIndexProvider.reset();
+
+        // Sub-root index
+        assertQuery("/jcr:root/subRoot/test/a/c/d[@propa='a']", XPATH, 
asList("/subRoot/test/a/c/d"));
+        assertEquals("Unexpected number of docs passed back to query engine", 
1, queryIndexProvider.getCount());
+        queryIndexProvider.reset();
+
+        assertQuery("/jcr:root/subRoot/test/a/c/*[@propa='a']", XPATH, 
asList("/subRoot/test/a/c/d"));
+        assertEquals("Unexpected number of docs passed back to query engine", 
1, queryIndexProvider.getCount());
+        queryIndexProvider.reset();
+
+        assertQuery("/jcr:root/subRoot/test/a//*[@propa='a']", XPATH, 
asList("/subRoot/test/a/c/d"));
+        assertEquals("Unexpected number of docs passed back to query engine", 
1, queryIndexProvider.getCount());
+        queryIndexProvider.reset();
+    }
+
+    @Test
     public void sortQueriesWithLong() throws Exception {
         Tree idx = createIndex("test1", of("foo", "bar"));
         Tree propIdx = idx.addChild(PROP_NODE).addChild("foo");
@@ -2898,5 +2948,4 @@ public class LucenePropertyIndexTest ext
             accessCount = 0;
         }
     }
-
 }

Added: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/ResultCountingIndexProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/ResultCountingIndexProvider.java?rev=1809519&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/ResultCountingIndexProvider.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/ResultCountingIndexProvider.java
 Sun Sep 24 14:33:06 2017
@@ -0,0 +1,162 @@
+/*
+ * 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.jackrabbit.oak.plugins.index.lucene;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import org.apache.jackrabbit.oak.api.Result;
+import org.apache.jackrabbit.oak.spi.query.Cursor;
+import org.apache.jackrabbit.oak.spi.query.Filter;
+import org.apache.jackrabbit.oak.spi.query.IndexRow;
+import org.apache.jackrabbit.oak.spi.query.QueryIndex;
+import 
org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvanceFulltextQueryIndex;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+
+import javax.annotation.Nonnull;
+import java.util.List;
+
+class ResultCountingIndexProvider implements QueryIndexProvider {
+    private final QueryIndexProvider delegate;
+    private final CountingCursorFactory cursorFactory;
+
+    private boolean shouldCount = false;
+
+    private int count = 0;
+
+    ResultCountingIndexProvider(QueryIndexProvider delegate) {
+        this.delegate = delegate;
+        cursorFactory = new CountingCursorFactory(this);
+    }
+
+    void setShouldCount(boolean shouldCount) {
+        this.shouldCount = shouldCount;
+    }
+
+    int getCount() {
+        return count;
+    }
+
+    void reset() {
+        count = 0;
+    }
+
+    void incrementCount() {
+        count++;
+    }
+
+    @Nonnull
+    @Override
+    public List<? extends QueryIndex> getQueryIndexes(NodeState nodeState) {
+        if (shouldCount) {
+            return Lists.transform(delegate.getQueryIndexes(nodeState), new 
Function<QueryIndex, QueryIndex>() {
+                @Nonnull
+                @Override
+                public QueryIndex apply(@Nonnull  QueryIndex input) {
+                    if (input instanceof AdvanceFulltextQueryIndex) {
+                        return new 
CountingIndex((AdvanceFulltextQueryIndex)input, cursorFactory);
+                    } else {
+                        return input;
+                    }
+                }
+            });
+        } else {
+            return delegate.getQueryIndexes(nodeState);
+        }
+    }
+
+    private static class CountingIndex implements QueryIndex, 
QueryIndex.AdvancedQueryIndex {
+        final AdvanceFulltextQueryIndex delegate;
+        final CountingCursorFactory cursorFactory;
+
+        CountingIndex(AdvanceFulltextQueryIndex delegate, 
CountingCursorFactory cursorFactory) {
+            this.delegate = delegate;
+            this.cursorFactory = cursorFactory;
+        }
+
+        @Override
+        public double getMinimumCost() {
+            return delegate.getMinimumCost();
+        }
+
+        @Override
+        public double getCost(Filter filter, NodeState rootState) {
+            return delegate.getCost(filter, rootState);
+        }
+
+        @Override
+        public Cursor query(Filter filter, NodeState rootState) {
+            return delegate.query(filter, rootState);
+        }
+
+        @Override
+        public String getPlan(Filter filter, NodeState rootState) {
+            return delegate.getPlan(filter, rootState);
+        }
+
+        @Override
+        public String getIndexName() {
+            return delegate.getIndexName();
+        }
+
+        @Override
+        public List<IndexPlan> getPlans(Filter filter, List<OrderEntry> 
sortOrder, NodeState rootState) {
+            return delegate.getPlans(filter, sortOrder, rootState);
+        }
+
+        @Override
+        public String getPlanDescription(IndexPlan plan, NodeState root) {
+            return delegate.getPlanDescription(plan, root);
+        }
+
+        @Override
+        public Cursor query(IndexPlan plan, NodeState rootState) {
+            return cursorFactory.wrap(delegate.query(plan, rootState));
+        }
+    }
+
+    private static class CountingCursorFactory {
+        final ResultCountingIndexProvider provider;
+
+        CountingCursorFactory(ResultCountingIndexProvider provider) {
+            this.provider = provider;
+        }
+
+        Cursor wrap(final Cursor c) {
+            return new Cursor() {
+                @Override
+                public IndexRow next() {
+                    provider.incrementCount();
+                    return c.next();
+                }
+
+                @Override
+                public long getSize(Result.SizePrecision precision, long max) {
+                    return c.getSize(precision, max);
+                }
+
+                @Override
+                public boolean hasNext() {
+                    return c.hasNext();
+                }
+            };
+        }
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/ResultCountingIndexProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to