Author: catholicon Date: Thu Mar 14 13:48:56 2019 New Revision: 1855522 URL: http://svn.apache.org/viewvc?rev=1855522&view=rev Log: OAK-8114: IndexDefinitionBuilder should be smarter when to reindex while updating a definition
Applying work done by Nitin Gupta at https://github.com/apache/jackrabbit-oak/pull/122. Thanks Nitin for the contribution. Did a few more changes on top: * check refresh only when reindex isn't required * we had forgotten that index tags also don't require reindexing 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=1855522&r1=1855521&r2=1855522&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 Thu Mar 14 13:48:56 2019 @@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.plugins.index.lucene.util; +import java.util.List; import java.util.Map; import java.util.Set; @@ -43,6 +44,7 @@ import org.apache.jackrabbit.oak.spi.sta import org.apache.jackrabbit.oak.spi.state.NodeState; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.ImmutableList.of; import static java.util.Arrays.asList; import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; import static org.apache.jackrabbit.JcrConstants.NT_UNSTRUCTURED; @@ -146,7 +148,10 @@ public final class IndexDefinitionBuilde } public NodeState build(){ + // make sure to check for reindex required before checking for refresh as refresh optimizes a bit using + // that assumption (we have tests that fail if the order is swapped btw) setReindexFlagIfRequired(); + setRefreshFlagIfRequired(); return builder.getNodeState(); } @@ -174,6 +179,16 @@ public final class IndexDefinitionBuilde } } + private void setRefreshFlagIfRequired() { + // Utilize the fact that reindex requirement is checked before checking for refresh + // It serves 2 purposes: + // * avoids setting up refresh if reindex is already set + // * avoid calling SelectiveEqualDiff#equal again + if (!isReindexRequired() && !initial.equals(builder.getNodeState())) { + tree.setProperty(FulltextIndexConstants.PROP_REFRESH_DEFN, true); + } + } + private void setType() { PropertyState type = tree.getProperty(IndexConstants.TYPE_PROPERTY_NAME); if (type == null || !"disabled".equals(type.getValue(Type.STRING))) { @@ -567,6 +582,20 @@ public final class IndexDefinitionBuilde } static class SelectiveEqualsDiff extends EqualsDiff { + // Properties for which changes shouldn't auto set the reindex flag + static final List<String> ignorablePropertiesList = of( + FulltextIndexConstants.PROP_WEIGHT, + FIELD_BOOST, + IndexConstants.QUERY_PATHS, + IndexConstants.INDEX_TAGS, + FulltextIndexConstants.BLOB_SIZE, + FulltextIndexConstants.COST_PER_ENTRY, + FulltextIndexConstants.COST_PER_EXECUTION); + static final List<String> ignorableFacetConfigProps = of( + FulltextIndexConstants.PROP_SECURE_FACETS, + FulltextIndexConstants.PROP_STATISTICAL_FACET_SAMPLE_SIZE, + FulltextIndexConstants.PROP_FACETS_TOP_CHILDREN); + public static boolean equals(NodeState before, NodeState after) { return before.exists() == after.exists() && after.compareAgainstBaseState(before, new SelectiveEqualsDiff()); @@ -578,10 +607,58 @@ public final class IndexDefinitionBuilde Set<String> asyncBefore = getAsyncValuesWithoutNRT(before); Set<String> asyncAfter = getAsyncValuesWithoutNRT(after); return asyncBefore.equals(asyncAfter); + } else if (ignorablePropertiesList.contains(before.getName()) || ignorableFacetConfigProps.contains(before.getName())) { + return true; } return false; } + @Override + public boolean propertyAdded(PropertyState after) { + if(ignorablePropertiesList.contains(after.getName()) || ignorableFacetConfigProps.contains(after.getName())) { + return true; + } + return super.propertyAdded(after); + } + + @Override + public boolean propertyDeleted(PropertyState before) { + if(ignorablePropertiesList.contains(before.getName()) || ignorableFacetConfigProps.contains(before.getName())) { + return true; + } + return super.propertyDeleted(before); + } + + @Override + public boolean childNodeAdded(String name, NodeState after) { + if(name.equals(FulltextIndexConstants.PROP_FACETS)) { + // This here makes sure any new property under FACETS that might be added in future and if so might require + // reindexing - then addition of facet node (/facet/foo) with such property lead to auto set of reindex flag + for(PropertyState property : after.getProperties()) { + if(!ignorableFacetConfigProps.contains(property.getName())) { + return super.childNodeAdded(name, after); + } + } + return true; + } + return super.childNodeAdded(name, after); + } + + @Override + public boolean childNodeDeleted(String name, NodeState before) { + if (name.equals(FulltextIndexConstants.PROP_FACETS)) { + // This here makes sure any new property under FACETS that might be added in future and if so might require + // reindexing - then deletion of facet node with such property lead to auto set of reindex flag + for(PropertyState property : before.getProperties()) { + if(!ignorableFacetConfigProps.contains(property.getName())) { + return super.childNodeAdded(name, before); + } + } + return true; + } + return super.childNodeDeleted(name, before); + } + private Set<String> getAsyncValuesWithoutNRT(PropertyState state){ Set<String> async = Sets.newHashSet(state.getValue(Type.STRINGS)); async.remove(IndexConstants.INDEXING_MODE_NRT); 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=1855522&r1=1855521&r2=1855522&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 Thu Mar 14 13:48:56 2019 @@ -36,13 +36,20 @@ import org.apache.jackrabbit.oak.spi.sta 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.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; import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_FACETS; +import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_REFRESH_DEFN; +import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.COST_PER_ENTRY; +import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.COST_PER_EXECUTION; +import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.BLOB_SIZE; +import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_WEIGHT; import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE; +import static org.apache.jackrabbit.oak.plugins.memory.MultiStringPropertyState.stringProperty; import static org.junit.Assert.*; public class IndexDefinitionBuilderTest { @@ -216,6 +223,7 @@ public class IndexDefinitionBuilderTest assertFalse(builder.isReindexRequired()); NodeState state = builder.build(); assertFalse(state.getBoolean(REINDEX_PROPERTY_NAME)); + assertFalse(state.getBoolean(PROP_REFRESH_DEFN)); NodeState baseState = builder.build(); @@ -254,6 +262,507 @@ public class IndexDefinitionBuilderTest } @Test + public void noReindexWhenIfQueryPathsAddedOrChanged() { + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.queryPaths("/a","/b"); + + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.queryPaths("/a","/c"); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().removeProperty(IndexConstants.QUERY_PATHS); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void noReindexWhenIfIndexTagsAddedOrChanged() { + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().setProperty(stringProperty(IndexConstants.INDEX_TAGS, of("foo1", "foo2"))); + + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().setProperty(stringProperty(IndexConstants.INDEX_TAGS, of("foo2", "foo3"))); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(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(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void noReindexWhenIfBlobSizeAddedOrChanged() { + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().setProperty(BLOB_SIZE,32768); + + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().setProperty(BLOB_SIZE,35768); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().removeProperty(BLOB_SIZE); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + // This property is used in cost estimation - no reindexing required + // on property change + @Test + public void noReindexIfWeightPropertyAddedOrChanged() throws Exception { + + builder.indexRule("nt:base").property("fooProp"); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + // Add the property weight to fooProp - this shouldn't cause reindex flag to set + builder.indexRule("nt:base").property("fooProp").weight(10); + + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + + // Now change the value for weight on fooProp - this also shouldn't lead to setting of reindex flag + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.indexRule("nt:base").property("fooProp").weight(20); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.indexRule("nt:base").property("fooProp").getBuilderTree().removeProperty(PROP_WEIGHT); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + } + // modifying boost value shouldn't require reindexing because we use + // QueryTime Boosts and not index time boosts. Refer OAK-3367 for details + @Test + public void noReindexIfBoostPropAddedOrChanged() throws Exception { + builder.indexRule("nt:base").property("fooProp"); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + // Add the property boost - this shouldn't cause reindex flag to set + builder.indexRule("nt:base").property("fooProp").boost(1.0f); + + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + + // Now change the value for boost - this also shouldn't lead to setting of reindex flag + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.indexRule("nt:base").property("fooProp").boost(2.0f); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.indexRule("nt:base").property("fooProp").getBuilderTree().removeProperty(FIELD_BOOST); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + } + + // This is a node for configuration on how faceted search works + // Everything impacts querty time evauation - so no need of reindexing in case of changes + @Test + public void noReindexWhenFacetNodeAddedOrRemoved() throws Exception { + builder.indexRule("nt:base") + .property("foo1").facets(); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + //Add the facets child node now + builder.getBuilderTree().addChild(PROP_FACETS); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + // Now test deleting the facets node should also not set the reindexing flag + nodeBuilder = currentNodeState.builder(); + + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().getChild(PROP_FACETS).remove(); + + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void noReindexWhenFacetConfigChanged_topChildren() throws Exception { + builder.indexRule("nt:base") + .property("foo1").facets(); + + builder.getBuilderTree().addChild(PROP_FACETS); + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + //Add top Children prop on facets node + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_FACETS_TOP_CHILDREN,100); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + nodeBuilder = currentNodeState.builder(); + + //Now test with changing the value - this too shouldn't set the reindexing flag + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_FACETS_TOP_CHILDREN,200); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().getChild(PROP_FACETS).removeProperty(FulltextIndexConstants.PROP_FACETS_TOP_CHILDREN); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void noReindexWhenFacetConfigChanged_secure() throws Exception { + builder.indexRule("nt:base") + .property("foo1").facets(); + + builder.getBuilderTree().addChild(PROP_FACETS); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + //Add top secure prop on facets node + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_SECURE_FACETS,FulltextIndexConstants.PROP_SECURE_FACETS_VALUE_SECURE); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + + //Now test with changing the value - this too shouldn't set the reindexing flag + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_SECURE_FACETS,FulltextIndexConstants.PROP_SECURE_FACETS_VALUE_INSECURE); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().getChild(PROP_FACETS).removeProperty(FulltextIndexConstants.PROP_SECURE_FACETS); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void noReindexWhenFacetConfigChanged_sampleSize() throws Exception { + builder.indexRule("nt:base") + .property("foo1").facets(); + + builder.getBuilderTree().addChild(PROP_FACETS); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + //Add top sample size prop on facets node + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_STATISTICAL_FACET_SAMPLE_SIZE,1000); + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + + //Now test with changing the value - this too shouldn't set the reindexing flag + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_STATISTICAL_FACET_SAMPLE_SIZE,2000); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().getChild(PROP_FACETS).removeProperty(FulltextIndexConstants.PROP_STATISTICAL_FACET_SAMPLE_SIZE); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void noReindexWhenIfCostPerExecAddedOrChanged() { + builder.indexRule("nt:base"); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild("indexRules").getChild("nt:base").setProperty(COST_PER_EXECUTION, 2.0); + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + + //Now test with changing the value - this too shouldn't set the reindexing flag + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild("indexRules").getChild("nt:base").setProperty(COST_PER_EXECUTION, 3.0); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().getChild("indexRules").getChild("nt:base").removeProperty(COST_PER_EXECUTION); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + } + + @Test + public void noReindexWhenIfCostPerEntryAddedOrChanged() { + builder.indexRule("nt:base"); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild("indexRules").getChild("nt:base").setProperty(COST_PER_ENTRY, 2.0); + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + + //Now test with changing the value - this too shouldn't set the reindexing flag + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild("indexRules").getChild("nt:base").setProperty(COST_PER_ENTRY, 3.0); + currentNodeState = builder.build(); + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + //Now check for property delete use case + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().getChild("indexRules").getChild("nt:base").removeProperty(COST_PER_ENTRY); + currentNodeState = builder.build(); + + assertFalse(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertTrue(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + } + + @Test + public void reindexFlagSetWhenRequired() { + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.includedPaths("/a", "/b"); + + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.includedPaths("/a", "/c"); + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + builder.getBuilderTree().removeProperty(PathFilter.PROP_INCLUDED_PATHS); + currentNodeState = builder.build(); + + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + } + + @Test + public void renidexIfFacetsNodeAddedwithSomeNewPropThatReqIndexing() throws Exception { + builder.indexRule("nt:base") + .property("foo1").facets(); + + NodeState currentNodeState = builder.build(); + nodeBuilder = currentNodeState.builder(); + + //Unset the reindex flag first because first build would have set it . + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().addChild(PROP_FACETS); + + //Add foo prop on facets node + builder.getBuilderTree().getChild(PROP_FACETS).setProperty("foo","bar"); + currentNodeState = builder.build(); + + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertFalse(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + nodeBuilder = currentNodeState.builder(); + + //Now test with changing the value - this too should set the reindexing flag + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild(PROP_FACETS).setProperty("foo","bar2"); + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertFalse(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + //now deleting the node + nodeBuilder = currentNodeState.builder(); + nodeBuilder.removeProperty(PROP_REFRESH_DEFN); + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().getChild(PROP_FACETS).remove(); + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertFalse(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + nodeBuilder = currentNodeState.builder(); + nodeBuilder.setProperty(REINDEX_PROPERTY_NAME, false); + builder = new IndexDefinitionBuilder(nodeBuilder); + + builder.getBuilderTree().addChild(PROP_FACETS); + + //Add foo prop on facets node + builder.getBuilderTree().getChild(PROP_FACETS).setProperty("foo","bar"); + builder.getBuilderTree().getChild(PROP_FACETS).setProperty(FulltextIndexConstants.PROP_STATISTICAL_FACET_SAMPLE_SIZE,200); + + currentNodeState = builder.build(); + assertTrue(currentNodeState.getBoolean(REINDEX_PROPERTY_NAME)); + assertFalse(currentNodeState.getBoolean(PROP_REFRESH_DEFN)); + + } + + @Test public void propRuleCustomName() throws Exception{ builder.indexRule("nt:base").property("foo").property("bar"); builder.indexRule("nt:base").property("fooProp", "foo2");
