Index: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java =================================================================== --- oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java (revision 1601598) +++ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java (working copy) @@ -19,6 +19,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayListWithCapacity; +import static org.apache.jackrabbit.oak.api.Type.BOOLEAN; import static org.apache.jackrabbit.oak.commons.PathUtils.concat; import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.ASYNC_PROPERTY_NAME; import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.ASYNC_REINDEX_VALUE; @@ -108,7 +109,7 @@ @Override public void enter(NodeState before, NodeState after) throws CommitFailedException { - collectIndexEditors(builder.getChildNode(INDEX_DEFINITIONS_NAME)); + collectIndexEditors(builder.getChildNode(INDEX_DEFINITIONS_NAME), before); // no-op when reindex is empty CommitFailedException exception = EditorDiff.process( @@ -122,17 +123,30 @@ } } - private void collectIndexEditors(NodeBuilder definitions) - throws CommitFailedException { + private boolean shouldReindex(NodeBuilder definition, NodeState before, + String name) { + PropertyState ps = definition.getProperty(REINDEX_PROPERTY_NAME); + if (ps != null) { + return ps.getValue(BOOLEAN); + } + // reindex only in the case this is a new node, even though there is + // no other way in which the property would be missing + return !before.getChildNode(INDEX_DEFINITIONS_NAME).hasChildNode(name); + } + + private void collectIndexEditors(NodeBuilder definitions, + NodeState before) throws CommitFailedException { for (String name : definitions.getChildNodeNames()) { NodeBuilder definition = definitions.getChildNode(name); if (Objects.equal(async, definition.getString(ASYNC_PROPERTY_NAME))) { String type = definition.getString(TYPE_PROPERTY_NAME); + boolean shouldReindex = shouldReindex(definition, + before, name); Editor editor = provider.getIndexEditor(type, definition, root, updateCallback); if (editor == null) { // trigger reindexing when an indexer becomes available definition.setProperty(REINDEX_PROPERTY_NAME, true); - } else if (definition.getBoolean(REINDEX_PROPERTY_NAME)) { + } else if (shouldReindex) { if (definition.getBoolean(REINDEX_ASYNC_PROPERTY_NAME) && definition.getString(ASYNC_PROPERTY_NAME) == null) { // switch index to an async update mode Index: oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java =================================================================== --- oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java (revision 1601598) +++ oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java (working copy) @@ -172,6 +172,41 @@ assertEquals(ImmutableSet.of("testRoot"), find(lookup, "foo", "abc")); } + /** + * Auto Reindex Test + * + */ + @Test + public void testReindexAuto() throws Exception { + builder.child("testRoot").setProperty("foo", "abc"); + NodeState before = builder.getNodeState(); + + NodeBuilder index = createIndexDefinition( + builder.child(INDEX_DEFINITIONS_NAME), "rootIndex", true, + false, ImmutableSet.of("foo"), null); + index.removeProperty(REINDEX_PROPERTY_NAME); + + NodeState after = builder.getNodeState(); + + NodeState indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY); + + // first check that the index content nodes exist + NodeState ns = checkPathExists(indexed, INDEX_DEFINITIONS_NAME, + "rootIndex"); + checkPathExists(ns, INDEX_CONTENT_NODE_NAME); + PropertyState ps = ns.getProperty(REINDEX_PROPERTY_NAME); + assertNotNull(ps); + assertFalse(ps.getValue(Type.BOOLEAN)); + + // next, lookup + PropertyIndexLookup lookup = new PropertyIndexLookup(indexed); + assertEquals(ImmutableSet.of("testRoot"), find(lookup, "foo", "abc")); + } + @Test public void testIndexDefinitions() throws Exception { createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME),