You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ca...@apache.org on 2016/08/05 17:08:59 UTC

svn commit: r1755366 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookupTest.java

Author: catholicon
Date: Fri Aug  5 17:08:59 2016
New Revision: 1755366

URL: http://svn.apache.org/viewvc?rev=1755366&view=rev
Log:
OAK-4636: PropertyIndexLookup#getIndexNode should be more tolerant towards property types

Added:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookupTest.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java?rev=1755366&r1=1755365&r2=1755366&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java Fri Aug  5 17:08:59 2016
@@ -25,10 +25,12 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider.TYPE;
 import static org.apache.jackrabbit.oak.plugins.index.property.PropertyIndex.encode;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
 import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import org.apache.jackrabbit.oak.api.PropertyState;
@@ -45,6 +47,8 @@ import org.apache.jackrabbit.oak.spi.sta
 
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Is responsible for querying the property index content.
@@ -62,6 +66,8 @@ import com.google.common.collect.Lists;
  */
 public class PropertyIndexLookup {
 
+    static final Logger LOG = LoggerFactory.getLogger(PropertyIndexLookup.class);
+
     /**
      * The cost overhead to use the index in number of read operations.
      */
@@ -168,7 +174,7 @@ public class PropertyIndexLookup {
             if (type == null || type.isArray() || !getType().equals(type.getValue(Type.STRING))) {
                 continue;
             }
-            if (contains(index.getNames(PROPERTY_NAMES), propertyName)) {
+            if (contains(getNames(index, PROPERTY_NAMES), propertyName)) {
                 NodeState indexContent = index.getChildNode(INDEX_CONTENT_NODE_NAME);
                 if (!indexContent.exists()) {
                     continue;
@@ -176,7 +182,7 @@ public class PropertyIndexLookup {
                 Set<String> supertypes = getSuperTypes(filter);
                 if (index.hasProperty(DECLARING_NODE_TYPES)) {
                     if (supertypes != null) {
-                        for (String typeName : index.getNames(DECLARING_NODE_TYPES)) {
+                        for (String typeName : getNames(index, DECLARING_NODE_TYPES)) {
                             if (supertypes.contains(typeName)) {
                                 // TODO: prefer the most specific type restriction
                                 return index;
@@ -211,4 +217,22 @@ public class PropertyIndexLookup {
         return null;
     }
 
+    @Nonnull
+    private static Iterable<String> getNames(@Nonnull NodeState state, @Nonnull String propertyName) {
+        Iterable<String> ret = state.getNames(propertyName);
+        if (ret.iterator().hasNext()) {
+            return ret;
+        }
+
+        PropertyState property = state.getProperty(propertyName);
+        if (property != null) {
+            LOG.warn("Expected '{}' as type of property '{}' but found '{}'. Node - '{}'",
+                    Type.NAMES, propertyName, property.getType(), state);
+            ret = property.getValue(Type.STRINGS);
+        } else {
+            ret = Collections.emptyList();
+        }
+
+        return ret;
+    }
 }
\ No newline at end of file

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookupTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookupTest.java?rev=1755366&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookupTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookupTest.java Fri Aug  5 17:08:59 2016
@@ -0,0 +1,111 @@
+/*
+ * 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.property;
+
+import com.google.common.collect.Lists;
+import org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider;
+import org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
+import org.apache.jackrabbit.oak.query.NodeStateNodeTypeInfoProvider;
+import org.apache.jackrabbit.oak.query.QueryEngineSettings;
+import org.apache.jackrabbit.oak.query.ast.NodeTypeInfo;
+import org.apache.jackrabbit.oak.query.ast.NodeTypeInfoProvider;
+import org.apache.jackrabbit.oak.query.ast.SelectorImpl;
+import org.apache.jackrabbit.oak.query.index.FilterImpl;
+import org.apache.jackrabbit.oak.spi.commit.EditorHook;
+import org.apache.jackrabbit.oak.spi.query.Filter;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static org.apache.jackrabbit.oak.api.Type.NAMES;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.DECLARING_NODE_TYPES;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.PROPERTY_NAMES;
+import static org.apache.jackrabbit.oak.spi.commit.CommitInfo.EMPTY;
+import static org.junit.Assert.assertNotNull;
+
+public class PropertyIndexLookupTest {
+
+    private static final List<String> PROP_NAMES = Lists.newArrayList("jcr:primaryType", "jcr:mixinTypes");
+    private static final List<String> DECL_NODE_TYPES = Lists.newArrayList("nt:unstructured", "mix:versionable");
+
+    private NodeState root;
+    private NodeBuilder rootBuilder;
+    private static final EditorHook HOOK = new EditorHook(
+            new IndexUpdateProvider(new PropertyIndexEditorProvider()));
+
+    @Before
+    public void setup() throws Exception {
+        root = EmptyNodeState.EMPTY_NODE;
+        rootBuilder = InitialContent.INITIAL_CONTENT.builder();
+        commit();
+    }
+
+    @Test
+    public void getIndexNodeForNamedDeclaringNodeTypes() throws Exception {
+        rootBuilder.child(INDEX_DEFINITIONS_NAME).child("nodetype")
+                .setProperty(PropertyStates.createProperty(DECLARING_NODE_TYPES, DECL_NODE_TYPES, NAMES));
+        commit();
+
+        Filter f = createFilter(root, "nt:unstructured");
+        assertNotNull("declaringNodeTypes with Name[] must find index node",
+                new PropertyIndexLookup(root).getIndexNode(root, JCR_PRIMARYTYPE, f));
+    }
+
+    @Test
+    public void getIndexNodeForStringDeclaringNodeTypes() throws Exception {
+        rootBuilder.child(INDEX_DEFINITIONS_NAME).child("nodetype")
+                .setProperty(PropertyStates.createProperty(DECLARING_NODE_TYPES, DECL_NODE_TYPES, STRINGS));
+        commit();
+
+        Filter f = createFilter(root, "nt:unstructured");
+        assertNotNull("declaringNodeTypes with String[] should also find index node",
+                new PropertyIndexLookup(root).getIndexNode(root, JCR_PRIMARYTYPE, f));
+    }
+
+    @Test
+    public void getIndexNodeForStringPropertyNames() throws Exception {
+        rootBuilder.child(INDEX_DEFINITIONS_NAME).child("nodetype")
+                .removeProperty(PROPERTY_NAMES)
+                .setProperty(PropertyStates.createProperty(PROPERTY_NAMES, PROP_NAMES, STRINGS));
+        commit();
+
+        Filter f = createFilter(root, "nt:unstructured");
+        assertNotNull("propertyNames with String[] should also find index node",
+                new PropertyIndexLookup(root).getIndexNode(root, JCR_PRIMARYTYPE, f));
+    }
+
+    private void commit() throws Exception {
+        root = HOOK.processCommit(rootBuilder.getBaseState(), rootBuilder.getNodeState(), EMPTY);
+        rootBuilder = root.builder();
+    }
+
+    private static FilterImpl createFilter(NodeState root, String nodeTypeName) {
+        NodeTypeInfoProvider nodeTypes = new NodeStateNodeTypeInfoProvider(root);
+        NodeTypeInfo type = nodeTypes.getNodeTypeInfo(nodeTypeName);
+        SelectorImpl selector = new SelectorImpl(type, nodeTypeName);
+        return new FilterImpl(selector, "SELECT * FROM [" + nodeTypeName + "]",
+                new QueryEngineSettings());
+    }
+}

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