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 th...@apache.org on 2013/09/24 17:33:27 UTC

svn commit: r1525929 - in /jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark: DescendantSearchTest.java IndexManager.java util/ util/OakIndexUtils.java

Author: thomasm
Date: Tue Sep 24 15:33:26 2013
New Revision: 1525929

URL: http://svn.apache.org/r1525929
Log:
OAK-641: Improved benchmark tooling - fix the index declaration

Added:
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/util/
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/util/OakIndexUtils.java
Removed:
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/IndexManager.java
Modified:
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/DescendantSearchTest.java

Modified: jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/DescendantSearchTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/DescendantSearchTest.java?rev=1525929&r1=1525928&r2=1525929&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/DescendantSearchTest.java (original)
+++ jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/DescendantSearchTest.java Tue Sep 24 15:33:26 2013
@@ -23,6 +23,8 @@ import javax.jcr.Session;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 
+import org.apache.jackrabbit.oak.benchmark.util.OakIndexUtils;
+
 /**
  * Performance test to check performance of queries on sub-trees.
  */
@@ -62,8 +64,9 @@ public class DescendantSearchTest extend
             session.setNamespacePrefix("oak", "http://jackrabbit.apache.org/oak/ns/1.0");
         }
 
-        IndexManager.createPropertyIndex(session, "testcount");
-
+        new OakIndexUtils.PropertyIndex().
+            property("testcount").
+            create(session);
     }
 
     @Override

Added: jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/util/OakIndexUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/util/OakIndexUtils.java?rev=1525929&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/util/OakIndexUtils.java (added)
+++ jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/benchmark/util/OakIndexUtils.java Tue Sep 24 15:33:26 2013
@@ -0,0 +1,176 @@
+package org.apache.jackrabbit.oak.benchmark.util;
+
+import java.util.Arrays;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
+
+/**
+ * A simple utility class for Oak indexes.
+ */
+public class OakIndexUtils {
+    
+    /**
+     * A property index
+     */
+    public static class PropertyIndex {
+        
+        private String indexName;
+        
+        private String propertyName;
+        
+        private String[] nodeTypeNames;
+
+        /**
+         * Set the index name. If not set, the index name is the property name.
+         * 
+         * @param indexName the index name
+         * @return this
+         */
+        public PropertyIndex name(String indexName) {
+            this.indexName = indexName;
+            return this;
+        }
+
+        /**
+         * Set the property name. This field is mandatory.
+         * 
+         * @param propertyName the property name
+         * @return this
+         */
+        public PropertyIndex property(String propertyName) {
+            this.propertyName = propertyName;
+            return this;
+        }
+
+        /**
+         * Restrict the node types.
+         * 
+         * @param nodeTypeNames the list of declaring node types
+         * @return this
+         */
+        public PropertyIndex nodeTypes(String... nodeTypeNames) {
+            this.nodeTypeNames = nodeTypeNames;
+            return this;
+        }
+
+        /**
+         * Create the index.
+         * <p>
+         * If this is not a Oak repository, this method does nothing.
+         * <p>
+         * If a matching index already exists, this method verifies that the
+         * definition matches. If no such index exists, a new one is created.
+         * 
+         * @param session the session to use for creating the index
+         * @throws RepositoryException if writing to the repository failed, the
+         *             index definition is incorrect, or if such an index exists
+         *             but is not compatible with this definition (for example,
+         *             a different property is indexed)
+         */
+        public void create(Session session) throws RepositoryException {
+            if (!session.getWorkspace().getNodeTypeManager().hasNodeType(
+                        "oak:queryIndexDefinition")) {
+                // not an Oak repository
+                return;
+            }
+            if (session.hasPendingChanges()) {
+                throw new RepositoryException("The session has pending changes");
+            }
+            if (indexName == null) {
+                indexName = propertyName;
+            }
+            if (propertyName == null) {
+                throw new RepositoryException("Index property name not set");
+            }
+            if (nodeTypeNames != null) {
+                if (nodeTypeNames.length == 0) {
+                    // setting the node types to an empty array means "all node types"
+                    // (same as not setting it)
+                    nodeTypeNames = null;
+                } else {
+                    Arrays.sort(nodeTypeNames);
+                }
+            }
+            Node root = session.getRootNode();
+            Node indexDef;
+            if (!root.hasNode(IndexConstants.INDEX_DEFINITIONS_NAME)) {
+                indexDef = root.addNode(IndexConstants.INDEX_DEFINITIONS_NAME, 
+                        JcrConstants.NT_UNSTRUCTURED);
+                session.save();
+            } else {
+                indexDef = root.getNode(IndexConstants.INDEX_DEFINITIONS_NAME);
+            }
+            Node index;
+            if (indexDef.hasNode(indexName)) {
+                // verify the index matches
+                index = indexDef.getNode(indexName);
+                if (index.hasProperty(IndexConstants.UNIQUE_PROPERTY_NAME)) {
+                    Property p = index.getProperty(IndexConstants.UNIQUE_PROPERTY_NAME);
+                    if (p.getBoolean()) {
+                        throw new RepositoryException(
+                                "Index already exists, but is unique");
+                    }
+                }
+                String type = index.getProperty(
+                        IndexConstants.TYPE_PROPERTY_NAME).getString();
+                if (!type.equals(PropertyIndexEditorProvider.TYPE)) {
+                    throw new RepositoryException(
+                            "Index already exists, but is of type " + type);
+                }
+                Value[] v = index.getProperty(IndexConstants.PROPERTY_NAMES).getValues();
+                if (v.length != 1) {
+                    String[] list = new String[v.length];
+                    for (int i = 0; i < v.length; i++) {
+                        list[i] = v[i].getString();
+                    }
+                    throw new RepositoryException(
+                            "Index already exists, but is not just one property, but " + Arrays.toString(list));
+                }
+                if (!propertyName.equals(v[0].getString())) {
+                    throw new RepositoryException(
+                            "Index already exists, but is for property " + v[0].getString());
+                }
+                if (index.hasProperty(IndexConstants.DECLARING_NODE_TYPES)) {
+                    v = index.getProperty(IndexConstants.DECLARING_NODE_TYPES).getValues();
+                    String[] list = new String[v.length];
+                    for (int i = 0; i < v.length; i++) {
+                        list[i] = v[i].getString();
+                    }
+                    Arrays.sort(list);
+                    if (Arrays.equals(list,  nodeTypeNames)) {
+                        throw new RepositoryException(
+                                "Index already exists, but with different node types: " + Arrays.toString(list));
+                    }
+                } else if (nodeTypeNames != null) {
+                    throw new RepositoryException(
+                            "Index already exists, but without node type restriction");
+                }
+                // matches
+                return;
+            }
+            index = indexDef.addNode(indexName, IndexConstants.INDEX_DEFINITIONS_NODE_TYPE);
+            index.setProperty(IndexConstants.TYPE_PROPERTY_NAME, 
+                    PropertyIndexEditorProvider.TYPE);
+            index.setProperty(IndexConstants.REINDEX_PROPERTY_NAME, 
+                    true);
+            index.setProperty(IndexConstants.PROPERTY_NAMES, 
+                    new String[] { propertyName }, PropertyType.NAME);
+            if (nodeTypeNames != null) {
+                index.setProperty(IndexConstants.DECLARING_NODE_TYPES,
+                        nodeTypeNames);
+            }
+            session.save();
+        }
+        
+    }
+
+}