Author: catholicon
Date: Sun Aug  4 04:05:54 2019
New Revision: 1864349

URL: http://svn.apache.org/viewvc?rev=1864349&view=rev
Log:
OAK-8526: IndexDefinitionBuilder should support setting up index tags

Modified:
    
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilder.java
    
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilderTest.java

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilder.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilder.java?rev=1864349&r1=1864348&r2=1864349&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilder.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilder.java
 Sun Aug  4 04:05:54 2019
@@ -22,11 +22,13 @@ package org.apache.jackrabbit.oak.plugin
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.StreamSupport;
 
 import javax.jcr.Node;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import org.apache.jackrabbit.oak.api.PropertyState;
@@ -42,6 +44,7 @@ import org.apache.jackrabbit.oak.spi.fil
 import org.apache.jackrabbit.oak.spi.state.EqualsDiff;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.jetbrains.annotations.NotNull;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.ImmutableList.of;
@@ -51,6 +54,7 @@ import static org.apache.jackrabbit.JcrC
 import static org.apache.jackrabbit.oak.api.Type.NAME;
 import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEPRECATED;
+import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_TAGS;
 import static 
org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexConstants.TYPE_LUCENE;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.FIELD_BOOST;
 import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
@@ -138,6 +142,23 @@ public final class IndexDefinitionBuilde
         return this;
     }
 
+    public IndexDefinitionBuilder tags(String ... tagVals) {
+        tree.removeProperty(INDEX_TAGS);
+        tree.setProperty(INDEX_TAGS, asList(tagVals), STRINGS);
+        return this;
+    }
+
+    public IndexDefinitionBuilder addTags(String ... additionalTagVals) {
+        Set<String> currTags = 
Sets.newHashSet(tree.getProperty(INDEX_TAGS).getValue(STRINGS));
+        Set<String> tagVals = Sets.newHashSet(Iterables.concat(currTags, 
asList(additionalTagVals)));
+        boolean noAdditionalTags = currTags.containsAll(tagVals);
+        if (!noAdditionalTags) {
+            tree.removeProperty(INDEX_TAGS);
+            tree.setProperty(INDEX_TAGS, asList(Iterables.toArray(tagVals, 
String.class)), STRINGS);
+        }
+        return this;
+    }
+
     public IndexDefinitionBuilder nodeTypeIndex() {
         tree.setProperty(FulltextIndexConstants.PROP_INDEX_NODE_TYPE, true);
         return this;

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilderTest.java?rev=1864349&r1=1864348&r2=1864349&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilderTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/IndexDefinitionBuilderTest.java
 Sun Aug  4 04:05:54 2019
@@ -21,6 +21,7 @@ package org.apache.jackrabbit.oak.plugin
 
 import java.util.Iterator;
 
+import com.google.common.collect.Iterables;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
@@ -33,12 +34,14 @@ import org.apache.jackrabbit.oak.spi.fil
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
+import org.hamcrest.Matchers;
 import org.junit.After;
 import org.junit.Test;
 
 import static com.google.common.collect.ImmutableList.of;
 import static java.util.Arrays.asList;
 import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEPRECATED;
+import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_TAGS;
 import static 
org.apache.jackrabbit.oak.plugins.index.IndexConstants.REINDEX_PROPERTY_NAME;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.AGGREGATES;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.FIELD_BOOST;
@@ -302,7 +305,7 @@ public class IndexDefinitionBuilderTest
 
         nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false);
         builder = new IndexDefinitionBuilder(nodeBuilder);
-        
builder.getBuilderTree().setProperty(stringProperty(IndexConstants.INDEX_TAGS, 
of("foo1", "foo2")));
+        builder.tags("foo1", "foo2");
 
         currentNodeState = builder.build();
         assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME));
@@ -312,7 +315,7 @@ public class IndexDefinitionBuilderTest
         nodeBuilder.removeProperty(PROP_REFRESH_DEFN);
         builder = new IndexDefinitionBuilder(nodeBuilder);
 
-        
builder.getBuilderTree().setProperty(stringProperty(IndexConstants.INDEX_TAGS, 
of("foo2", "foo3")));
+        builder.tags("foo2", "foo3");
         currentNodeState = builder.build();
         assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME));
         assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN));
@@ -320,6 +323,15 @@ public class IndexDefinitionBuilderTest
         nodeBuilder = currentNodeState.builder();
         nodeBuilder.removeProperty(PROP_REFRESH_DEFN);
         builder = new IndexDefinitionBuilder(nodeBuilder);
+
+        builder.addTags("foo2");
+        currentNodeState = builder.build();
+        assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME));
+        assertFalse(currentNodeState.getBoolean(PROP_REFRESH_DEFN));
+
+        nodeBuilder = currentNodeState.builder();
+        nodeBuilder.removeProperty(PROP_REFRESH_DEFN);
+        builder = new IndexDefinitionBuilder(nodeBuilder);
         builder.getBuilderTree().removeProperty(IndexConstants.INDEX_TAGS);
         currentNodeState = builder.build();
 
@@ -900,4 +912,57 @@ public class IndexDefinitionBuilderTest
         assertFalse("Incorrectly existing facets property",
                 foo2.hasProperty(PROP_FACETS));
     }
+
+    @Test
+    public void tags() {
+        NodeState state = EMPTY_NODE;
+
+        builder = new IndexDefinitionBuilder(state.builder());
+        builder.tags("foo");
+        state = builder.build();
+        Iterable<String> tags = 
state.getProperty(INDEX_TAGS).getValue(Type.STRINGS);
+        assertEquals("Unexpected number of tags", 1, Iterables.size(tags));
+        assertThat(state.getProperty(INDEX_TAGS).getValue(Type.STRINGS),
+                Matchers.containsInAnyOrder("foo"));
+
+        builder = new IndexDefinitionBuilder(state.builder());
+        builder.addTags("foo");
+        state = builder.build();
+        tags = state.getProperty(INDEX_TAGS).getValue(Type.STRINGS);
+        assertEquals("Unexpected number of tags", 1, Iterables.size(tags));
+        assertThat(state.getProperty(INDEX_TAGS).getValue(Type.STRINGS),
+                Matchers.containsInAnyOrder("foo"));
+
+        builder = new IndexDefinitionBuilder(state.builder());
+        builder.addTags("foo", "foo1");
+        state = builder.build();
+        tags = state.getProperty(INDEX_TAGS).getValue(Type.STRINGS);
+        assertEquals("Unexpected number of tags", 2, Iterables.size(tags));
+        assertThat(state.getProperty(INDEX_TAGS).getValue(Type.STRINGS),
+                Matchers.containsInAnyOrder("foo", "foo1"));
+
+        builder = new IndexDefinitionBuilder(state.builder());
+        builder.addTags("foo2");
+        state = builder.build();
+        tags = state.getProperty(INDEX_TAGS).getValue(Type.STRINGS);
+        assertEquals("Unexpected number of tags", 3, Iterables.size(tags));
+        assertThat(state.getProperty(INDEX_TAGS).getValue(Type.STRINGS),
+                Matchers.containsInAnyOrder("foo", "foo1", "foo2"));
+
+        builder = new IndexDefinitionBuilder(state.builder());
+        builder.addTags("foo2", "foo3");
+        state = builder.build();
+        tags = state.getProperty(INDEX_TAGS).getValue(Type.STRINGS);
+        assertEquals("Unexpected number of tags", 4, Iterables.size(tags));
+        assertThat(state.getProperty(INDEX_TAGS).getValue(Type.STRINGS),
+                Matchers.containsInAnyOrder("foo", "foo1", "foo2", "foo3"));
+
+        builder = new IndexDefinitionBuilder(state.builder());
+        builder.tags("foo4");
+        state = builder.build();
+        tags = state.getProperty(INDEX_TAGS).getValue(Type.STRINGS);
+        assertEquals("Unexpected number of tags", 1, Iterables.size(tags));
+        assertThat(state.getProperty(INDEX_TAGS).getValue(Type.STRINGS),
+                Matchers.containsInAnyOrder("foo4"));
+    }
 }
\ No newline at end of file


Reply via email to