Author: chetanm
Date: Fri Apr 21 10:35:46 2017
New Revision: 1792178

URL: http://svn.apache.org/viewvc?rev=1792178&view=rev
Log:
OAK-6082 - Service to determine index paths

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathService.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImpl.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImplTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathService.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathService.java?rev=1792178&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathService.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathService.java
 Fri Apr 21 10:35:46 2017
@@ -0,0 +1,31 @@
+/*
+ * 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;
+
+import java.util.Iterator;
+
+public interface IndexPathService {
+
+    /**
+     * Returns all index definitions paths present in the repository
+     */
+    Iterator<String> getIndexPaths();
+
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImpl.java?rev=1792178&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImpl.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImpl.java
 Fri Apr 21 10:35:46 2017
@@ -0,0 +1,110 @@
+/*
+ * 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;
+
+import java.util.Iterator;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Iterators;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.jackrabbit.oak.plugins.index.nodetype.NodeTypeIndexProvider;
+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.mount.MountInfoProvider;
+import org.apache.jackrabbit.oak.spi.mount.Mounts;
+import org.apache.jackrabbit.oak.spi.query.IndexRow;
+import org.apache.jackrabbit.oak.spi.query.QueryIndex;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+
+import static com.google.common.base.Preconditions.checkState;
+import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.DECLARING_NODE_TYPES;
+import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NODE_TYPE;
+
+@Component
+@Service
+public class IndexPathServiceImpl implements IndexPathService {
+
+    private final QueryEngineSettings settings = new QueryEngineSettings();
+
+    @Reference
+    private NodeStore nodeStore;
+
+    @Reference
+    private MountInfoProvider mountInfoProvider;
+
+    public IndexPathServiceImpl() {
+        //Required for SCR
+        settings.setFailTraversal(true);
+        settings.setLimitReads(Long.MAX_VALUE);
+    }
+
+    public IndexPathServiceImpl(NodeStore nodeStore) {
+        this(nodeStore, Mounts.defaultMountInfoProvider());
+    }
+
+    public IndexPathServiceImpl(NodeStore nodeStore, MountInfoProvider 
mountInfoProvider) {
+        this.nodeStore = nodeStore;
+        this.mountInfoProvider = mountInfoProvider;
+    }
+
+    @Override
+    public Iterator<String> getIndexPaths() {
+        NodeState nodeType = NodeStateUtils.getNode(nodeStore.getRoot(), 
"/oak:index/nodetype");
+
+        checkState("property".equals(nodeType.getString("type")), "nodetype 
index at " +
+                "/oak:index/nodetype is found to be disabled. Cannot determine 
the paths of all indexes");
+
+        //Check if oak:QueryIndexDefinition is indexed as part of nodetype 
index
+        boolean indxDefnTypeIndexed = 
Iterables.contains(nodeType.getNames(DECLARING_NODE_TYPES), 
INDEX_DEFINITIONS_NODE_TYPE);
+        checkState(indxDefnTypeIndexed, INDEX_DEFINITIONS_NODE_TYPE + " is not 
found to be indexed as part of " +
+                "nodetype index. Cannot determine the paths of all indexes");
+
+        Iterator<IndexRow> itr = 
getIndex().query(createFilter(INDEX_DEFINITIONS_NODE_TYPE), 
nodeStore.getRoot());
+        return Iterators.transform(itr, new Function<IndexRow, String>() {
+            @Override
+            public String apply(IndexRow input) {
+                return input.getPath();
+            }
+        });
+
+    }
+
+    private FilterImpl createFilter(String nodeTypeName) {
+        NodeTypeInfoProvider nodeTypes = new 
NodeStateNodeTypeInfoProvider(nodeStore.getRoot());
+        NodeTypeInfo type = nodeTypes.getNodeTypeInfo(nodeTypeName);
+        SelectorImpl selector = new SelectorImpl(type, nodeTypeName);
+        return new FilterImpl(selector, "SELECT * FROM [" + nodeTypeName + 
"]", settings);
+    }
+
+    private QueryIndex getIndex() {
+        NodeTypeIndexProvider idxProvider = new NodeTypeIndexProvider();
+        idxProvider.with(mountInfoProvider);
+        return idxProvider.getQueryIndexes(nodeStore.getRoot()).get(0);
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImplTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImplTest.java?rev=1792178&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImplTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexPathServiceImplTest.java
 Fri Apr 21 10:35:46 2017
@@ -0,0 +1,112 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.Set;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.InitialContent;
+import org.apache.jackrabbit.oak.Oak;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.plugins.index.nodetype.NodeTypeIndexProvider;
+import 
org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.junit.Test;
+
+import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.DECLARING_NODE_TYPES;
+import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NODE_TYPE;
+import static org.hamcrest.Matchers.hasItem;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class IndexPathServiceImplTest extends AbstractQueryTest {
+
+    private NodeStore nodeStore = new MemoryNodeStore();
+    private IndexPathService indexPathService = new 
IndexPathServiceImpl(nodeStore);
+
+    @Override
+    protected ContentRepository createRepository() {
+        return new Oak(nodeStore).with(new InitialContent())
+                .with(new OpenSecurityProvider())
+                .with(new NodeTypeIndexProvider())
+                .with(new PropertyIndexEditorProvider())
+                .createContentRepository();
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void errorIfQueryDefinitionsNotIndexed() throws Exception{
+        indexPathService.getIndexPaths();
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void errorIfNodetypeIndexDisabled() throws Exception{
+        Tree tree = root.getTree("/oak:index/nodetype");
+        tree.setProperty("type", "disabled");
+        root.commit();
+        indexPathService.getIndexPaths();
+    }
+
+    @Test
+    public void nodeTypeIndexed() throws Exception{
+        enableIndexDefinitionIndex();
+        Set<String> paths = Sets.newHashSet(indexPathService.getIndexPaths());
+        assertThat(paths, hasItem("/oak:index/uuid"));
+        assertThat(paths, hasItem("/oak:index/nodetype"));
+        assertThat(paths, hasItem("/oak:index/reference"));
+    }
+
+    @Test
+    public void indexInSubTree() throws Exception{
+        enableIndexDefinitionIndex();
+        Tree tree = root.getTree("/").addChild("a").addChild("b");
+        Tree fooIndex = tree.addChild("oak:index").addChild("fooIndex");
+        fooIndex.setProperty(JcrConstants.JCR_PRIMARYTYPE, 
INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        fooIndex.setProperty("type", "disabled");
+        root.commit();
+
+        Set<String> paths = Sets.newHashSet(indexPathService.getIndexPaths());
+        assertThat(paths, hasItem("/a/b/oak:index/fooIndex"));
+    }
+
+    private void enableIndexDefinitionIndex() throws CommitFailedException {
+        Tree nodetype = root.getTree("/oak:index/nodetype");
+        assertTrue(nodetype.exists());
+
+        List<String> nodetypes = Lists.newArrayList();
+        if (nodetype.hasProperty(DECLARING_NODE_TYPES)){
+            nodetypes = 
Lists.newArrayList(nodetype.getProperty(DECLARING_NODE_TYPES).getValue(Type.STRINGS));
+        }
+
+        nodetypes.add(INDEX_DEFINITIONS_NODE_TYPE);
+        nodetype.setProperty(DECLARING_NODE_TYPES, nodetypes, Type.NAMES);
+        nodetype.setProperty(IndexConstants.REINDEX_PROPERTY_NAME, true);
+        root.commit();
+    }
+
+}
\ No newline at end of file

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


Reply via email to