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 al...@apache.org on 2012/10/19 11:48:12 UTC

svn commit: r1400014 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/index/property/ main/java/org/apache/jackrabbit/oak/plugins/nodetype/ test/resources/org/apache/jackrabbit/oak/query/

Author: alexparvulescu
Date: Fri Oct 19 09:48:11 2012
New Revision: 1400014

URL: http://svn.apache.org/viewvc?rev=1400014&view=rev
Log:
OAK-388 Add NodeType Index

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexDiff.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexHook.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexUpdate.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/InitialContent.java
    jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2.txt

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java?rev=1400014&r1=1400013&r2=1400014&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java Fri Oct 19 09:48:11 2012
@@ -38,6 +38,39 @@ import org.apache.jackrabbit.oak.spi.sta
 import com.google.common.base.Charsets;
 import com.google.common.collect.Sets;
 
+/**
+ * Provides a QueryIndex that does lookups against a property index
+ * 
+ * <p>
+ * How to define a property index on a subtree you have to add an
+ * <code>oak:index<code> node. Under it follows the index definition node that must be of type <code>oak:queryIndexDefinition</code>
+ * and contain the
+ * <code>pnames<code> property that indicates what property will be stored in the index.
+ * </p>
+ * 
+ * <p>
+ * <code>pnames<code> can be a list of properties, and it is optional.in case it is missing, the node name will be used as a property name reference value
+ * </p>
+ * 
+ * <p>
+ * Optionally you can specify the uniqueness constraint on a property index by
+ * setting the <code>unique</code> flag to true.
+ * </p>
+ * 
+ * <pre>
+ * <code>
+ * {
+ *     NodeBuilder index = root.child("oak:index");
+ *     index.child("uuid")
+ *         .setProperty("jcr:primaryType", "oak:queryIndexDefinition", Type.NAME)
+ *         .setProperty("pnames", "jcr:uuid")
+ *         .setProperty("unique", true);
+ * }
+ * </code>
+ * </pre>
+ * 
+ * @see PropertyIndexLookup
+ */
 public class PropertyIndex implements QueryIndex {
 
     private static final int MAX_STRING_LENGTH = 100; // TODO: configurable

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexDiff.java?rev=1400014&r1=1400013&r2=1400014&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexDiff.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexDiff.java Fri Oct 19 09:48:11 2012
@@ -16,18 +16,29 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.property;
 
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
+import static org.apache.jackrabbit.oak.commons.PathUtils.concat;
+
 import java.util.List;
 import java.util.Map;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
 import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
 
+/**
+ * {@link NodeStateDiff} implementation that extracts changes for the
+ * {@link PropertyIndexHook} to be applied on the {@link PropertyIndex}
+ * 
+ * @see PropertyIndexHook
+ * 
+ */
 class PropertyIndexDiff implements NodeStateDiff {
 
     private final PropertyIndexDiff parent;
@@ -50,16 +61,21 @@ class PropertyIndexDiff implements NodeS
         this.path = path;
         this.updates = updates;
 
-        if (node != null && node.hasChildNode("oak:index")) {
-            NodeBuilder index = node.child("oak:index");
+        if (node != null && node.hasChildNode(INDEX_DEFINITIONS_NAME)) {
+            NodeBuilder index = node.child(INDEX_DEFINITIONS_NAME);
             for (String indexName : index.getChildNodeNames()) {
-                List<PropertyIndexUpdate> list = updates.get(indexName);
-                if (list == null) {
-                    list = Lists.newArrayList();
-                    updates.put(indexName, list);
+                NodeBuilder indexChild = index.child(indexName);
+                PropertyState ps = indexChild.getProperty("pnames");
+                Iterable<String> propertyNames = ps != null ? ps
+                        .getValue(Type.STRINGS) : ImmutableList.of(indexName);
+                for (String pname : propertyNames) {
+                    List<PropertyIndexUpdate> list = this.updates.get(pname);
+                    if (list == null) {
+                        list = Lists.newArrayList();
+                        this.updates.put(pname, list);
+                    }
+                    list.add(new PropertyIndexUpdate(getPath(), indexChild));
                 }
-                list.add(new PropertyIndexUpdate(
-                        getPath(), index.child(indexName)));
             }
         }
     }
@@ -84,12 +100,7 @@ class PropertyIndexDiff implements NodeS
 
     private String getPath() {
         if (path == null) { // => parent != null
-            String parentPath = parent.getPath();
-            if ("/".equals(parentPath)) {
-                path = parentPath + name;
-            } else {
-                path = parentPath + "/" + name;
-            }
+            path = concat(parent.getPath(), name);
         }
         return path;
     }
@@ -136,8 +147,7 @@ class PropertyIndexDiff implements NodeS
     public void childNodeChanged(
             String name, NodeState before, NodeState after) {
         if (!NodeStateUtils.isHidden(name)) {
-            PropertyIndexDiff child = new PropertyIndexDiff(this, name);
-            after.compareAgainstBaseState(before, child);
+            after.compareAgainstBaseState(before, new PropertyIndexDiff(this, name));
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexHook.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexHook.java?rev=1400014&r1=1400013&r2=1400014&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexHook.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexHook.java Fri Oct 19 09:48:11 2012
@@ -28,6 +28,14 @@ import org.apache.jackrabbit.oak.spi.sta
 
 import com.google.common.collect.Maps;
 
+/**
+ * {@link CommitHook} implementation that is responsible for keeping the
+ * {@link PropertyIndex} up to date
+ * 
+ * @see PropertyIndex
+ * @see PropertyIndexLookup
+ * 
+ */
 public class PropertyIndexHook implements CommitHook {
 
     @Override @Nonnull

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=1400014&r1=1400013&r2=1400014&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 Oct 19 09:48:11 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.property;
 
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
+
 import java.util.Set;
 
 import org.apache.jackrabbit.oak.api.PropertyState;
@@ -27,6 +29,24 @@ import org.apache.jackrabbit.oak.spi.sta
 
 import com.google.common.collect.Sets;
 
+/**
+ * Is responsible for querying the property index content.
+ * 
+ * <p>
+ * This class can be used directly on a subtree where there is an index defined
+ * by supplying a {@link NodeState} root.
+ * </p>
+ * 
+ * <pre>
+ * <code>
+ * {
+ *     NodeState state = ... // get a node state
+ *     PropertyIndexLookup lookup = new PropertyIndexLookup(state);
+ *     Set<String> hits = lookup.find("foo", "xyz");
+ * }
+ * </code>
+ * </pre>
+ */
 public class PropertyIndexLookup {
 
     private final NodeState root;
@@ -43,7 +63,7 @@ public class PropertyIndexLookup {
      * @param path lookup path
      */
     public boolean isIndexed(String name, String path) {
-        NodeState state = root.getChildNode("oak:index");
+        NodeState state = root.getChildNode(INDEX_DEFINITIONS_NAME);
         if (state != null) {
             state = state.getChildNode(name);
             if (state != null) {
@@ -72,7 +92,7 @@ public class PropertyIndexLookup {
         Set<String> paths = Sets.newHashSet();
 
         PropertyState property = null;
-        NodeState state = root.getChildNode("oak:index");
+        NodeState state = root.getChildNode(INDEX_DEFINITIONS_NAME);
         if (state != null) {
             state = state.getChildNode(name);
             if (state != null) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexUpdate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexUpdate.java?rev=1400014&r1=1400013&r2=1400014&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexUpdate.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexUpdate.java Fri Oct 19 09:48:11 2012
@@ -33,6 +33,10 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.spi.query.PropertyValues;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 
+/**
+ * Takes care of applying the updates to the index content.
+ * 
+ */
 class PropertyIndexUpdate {
 
     private final String path;

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/InitialContent.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/InitialContent.java?rev=1400014&r1=1400013&r2=1400014&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/InitialContent.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/InitialContent.java Fri Oct 19 09:48:11 2012
@@ -90,16 +90,22 @@ public class InitialContent extends Defa
 
         if (!root.hasChildNode("oak:index")) {
             NodeBuilder index = root.child("oak:index");
-            index.child("jcr:uuid")
+            index.child("uuid")
                 .setProperty("jcr:primaryType", "oak:queryIndexDefinition", Type.NAME)
+                .setProperty("pnames", "jcr:uuid")
                 .setProperty("unique", true);
+            index.child("primaryType")
+                .setProperty("jcr:primaryType", "oak:queryIndexDefinition", Type.NAME)
+                .setProperty("pnames", "jcr:primaryType");
             // FIXME: user-mgt related unique properties (rep:authorizableId, rep:principalName) are implementation detail and not generic for repo
             // FIXME: rep:principalName only needs to be unique if defined with user/group nodes -> add defining nt-info to uniqueness constraint otherwise ac-editing will fail.
-            index.child("rep:authorizableId")
+            index.child("authorizableId")
                 .setProperty("jcr:primaryType", "oak:queryIndexDefinition", Type.NAME)
+                .setProperty("pnames", "rep:authorizableId")
                 .setProperty("unique", true);
-            index.child("rep:principalName")
+            index.child("principalName")
                 .setProperty("jcr:primaryType", "oak:queryIndexDefinition", Type.NAME)
+                .setProperty("pnames", "rep:principalName")
                 .setProperty("unique", true);
         }
         try {

Modified: jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2.txt
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2.txt?rev=1400014&r1=1400013&r2=1400014&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2.txt (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2.txt Fri Oct 19 09:48:11 2012
@@ -120,10 +120,11 @@ select * from [nt:base] as b where local
 select * from [nt:base] as x where isdescendantnode(x, '/') and not isdescendantnode(x, '/jcr:system')
 /jcr:system
 /oak:index
-/oak:index/jcr:uuid
-/oak:index/rep:authorizableId
-/oak:index/rep:principalName
+/oak:index/authorizableId
+/oak:index/primaryType
+/oak:index/principalName
 /oak:index/test-index
+/oak:index/uuid
 /rep:security
 /rep:security/rep:authorizables
 /rep:security/rep:authorizables/rep:users
@@ -212,10 +213,11 @@ select * from [nt:base] where not isdesc
 /
 /jcr:system
 /oak:index
-/oak:index/jcr:uuid
-/oak:index/rep:authorizableId
-/oak:index/rep:principalName
+/oak:index/authorizableId
+/oak:index/primaryType
+/oak:index/principalName
 /oak:index/test-index
+/oak:index/uuid
 /rep:security
 /rep:security/rep:authorizables
 /rep:security/rep:authorizables/rep:users
@@ -243,10 +245,11 @@ select * from [nt:base] where not (id = 
 /
 /jcr:system
 /oak:index
-/oak:index/jcr:uuid
-/oak:index/rep:authorizableId
-/oak:index/rep:principalName
+/oak:index/authorizableId
+/oak:index/primaryType
+/oak:index/principalName
 /oak:index/test-index
+/oak:index/uuid
 /rep:security
 /rep:security/rep:authorizables
 /rep:security/rep:authorizables/rep:users
@@ -262,10 +265,11 @@ select * from [nt:base] where x is null 
 /
 /jcr:system
 /oak:index
-/oak:index/jcr:uuid
-/oak:index/rep:authorizableId
-/oak:index/rep:principalName
+/oak:index/authorizableId
+/oak:index/primaryType
+/oak:index/principalName
 /oak:index/test-index
+/oak:index/uuid
 /rep:security
 /rep:security/rep:authorizables
 /rep:security/rep:authorizables/rep:users