Author: chetanm
Date: Thu Sep 15 07:14:55 2016
New Revision: 1760835

URL: http://svn.apache.org/viewvc?rev=1760835&view=rev
Log:
OAK-4412 - Lucene hybrid index

Use a read only builder for sync indexing case to ensure that in no case 
anything gets written to NodeStore. This would cause issue with Facets which 
needs to tackled separately

Added:
    
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProviderTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditor.java
    
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProvider.java

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditor.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditor.java?rev=1760835&r1=1760834&r2=1760835&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditor.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditor.java
 Thu Sep 15 07:14:55 2016
@@ -275,6 +275,10 @@ public class LuceneIndexEditor implement
         return null; // no need to recurse down the removed subtree
     }
 
+    LuceneIndexEditorContext getContext() {
+        return context;
+    }
+
     private boolean addOrUpdate(String path, NodeState state, boolean isUpdate)
             throws CommitFailedException {
         try {

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProvider.java?rev=1760835&r1=1760834&r2=1760835&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProvider.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProvider.java
 Thu Sep 15 07:14:55 2016
@@ -33,6 +33,7 @@ import org.apache.jackrabbit.oak.spi.mou
 import org.apache.jackrabbit.oak.spi.mount.Mounts;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -103,7 +104,7 @@ public class LuceneIndexEditorProvider i
                 if (indexingContext.isReindexing()){
                     return null;
                 }
-                //TODO [hybrid] switch the builder to readonly one
+
                 writerFactory = new LocalIndexWriterFactory(indexingContext);
 
                 //IndexDefinition from tracker might differ from one passed 
here for reindexing
@@ -112,6 +113,13 @@ public class LuceneIndexEditorProvider i
                 if (indexTracker != null){
                     indexDefinition = 
indexTracker.getIndexDefinition(indexingContext.getIndexPath());
                 }
+
+                //Pass on a read only builder to ensure that nothing gets 
written
+                //at all to NodeStore for local indexing.
+                //TODO [hybrid] This would cause issue with Facets as for 
faceted fields
+                //some stuff gets written to NodeBuilder. That logic should be 
refactored
+                //to be moved to LuceneIndexWriter
+                definition = new ReadOnlyBuilder(definition.getNodeState());
             }
 
             LuceneIndexEditorContext context = new 
LuceneIndexEditorContext(root, definition, indexDefinition, callback,

Added: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProviderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProviderTest.java?rev=1760835&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProviderTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProviderTest.java
 Thu Sep 15 07:14:55 2016
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index.lucene;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.core.SimpleCommitContext;
+import org.apache.jackrabbit.oak.plugins.index.ContextAwareCallback;
+import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.IndexUpdateCallback;
+import org.apache.jackrabbit.oak.plugins.index.IndexingContext;
+import org.apache.jackrabbit.oak.spi.commit.CommitContext;
+import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
+import org.apache.jackrabbit.oak.spi.commit.Editor;
+import org.apache.jackrabbit.oak.spi.mount.Mounts;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Test;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
+import static 
org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexConstants.TYPE_LUCENE;
+import static 
org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition;
+import static 
org.apache.jackrabbit.oak.plugins.memory.PropertyStates.createProperty;
+import static 
org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent.INITIAL_CONTENT;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class LuceneIndexEditorProviderTest {
+    private NodeState root = INITIAL_CONTENT;
+    private NodeBuilder builder = root.builder();
+
+    @Test
+    public void readOnlyBuilderUsedForSync() throws Exception {
+        LuceneIndexEditorProvider editorProvider = new 
LuceneIndexEditorProvider(null,
+                null,
+                null,
+                null,
+                Mounts.defaultMountInfoProvider());
+
+        IndexUpdateCallback callback = new TestCallback("/oak:index/fooIndex", 
newCommitInfo(), false, false);
+        NodeBuilder defnBuilder = createIndexDefinition("fooIndex").builder();
+        Editor editor = editorProvider.getIndexEditor(TYPE_LUCENE, 
defnBuilder, root, callback);
+        LuceneIndexEditor luceneEditor = (LuceneIndexEditor) editor;
+
+        NodeBuilder builderFromContext =
+                (NodeBuilder) FieldUtils.readField(luceneEditor.getContext(), 
"definitionBuilder", true);
+
+        try {
+            builderFromContext.setProperty("foo", "bar");
+            fail("Should have been read only builder");
+        } catch (UnsupportedOperationException ignore) {
+
+        }
+    }
+
+    @Test
+    public void reuseOldIndexDefinition() throws Exception{
+        IndexTracker tracker = mock(IndexTracker.class);
+        LuceneIndexEditorProvider editorProvider = new 
LuceneIndexEditorProvider(null,
+                tracker,
+                null,
+                null,
+                Mounts.defaultMountInfoProvider());
+
+        //Set up a different IndexDefinition which needs to be returned
+        //from tracker with a marker property
+        NodeBuilder testBuilder = createIndexDefinition("fooIndex").builder();
+        testBuilder.setProperty("foo", "bar");
+        IndexDefinition defn = new IndexDefinition(root, 
testBuilder.getNodeState());
+        
when(tracker.getIndexDefinition("/oak:index/fooIndex")).thenReturn(defn);
+
+        IndexUpdateCallback callback = new TestCallback("/oak:index/fooIndex", 
newCommitInfo(), false, false);
+        NodeBuilder defnBuilder = createIndexDefinition("fooIndex").builder();
+        Editor editor = editorProvider.getIndexEditor(TYPE_LUCENE, 
defnBuilder, root, callback);
+        LuceneIndexEditor luceneEditor = (LuceneIndexEditor) editor;
+        LuceneIndexEditorContext context = luceneEditor.getContext();
+
+        //Definition should reflect the marker property
+        assertEquals("bar", 
context.getDefinition().getDefinitionNodeState().getString("foo"));
+    }
+
+    @Test
+    public void editorNullInCaseOfReindex() throws Exception{
+        LuceneIndexEditorProvider editorProvider = new 
LuceneIndexEditorProvider(null,
+                null,
+                null,
+                null,
+                Mounts.defaultMountInfoProvider());
+
+        IndexUpdateCallback callback = new TestCallback("/oak:index/fooIndex", 
newCommitInfo(), true, false);
+        NodeBuilder defnBuilder = createIndexDefinition("fooIndex").builder();
+        Editor editor = editorProvider.getIndexEditor(TYPE_LUCENE, 
defnBuilder, root, callback);
+        assertNull(editor);
+    }
+
+    private NodeState createIndexDefinition(String idxName) {
+        NodeBuilder idx = 
newLucenePropertyIndexDefinition(builder.child("oak:index"),
+                idxName, ImmutableSet.of("foo"), "async");
+        idx.setProperty(createProperty(IndexConstants.ASYNC_PROPERTY_NAME, 
of("sync", "async"), STRINGS));
+        LuceneIndexEditorContext.configureUniqueId(idx);
+        IndexDefinition.updateDefinition(idx);
+        return idx.getNodeState();
+    }
+
+    private CommitInfo newCommitInfo() {
+        CommitInfo info = new CommitInfo("admin", "s1",
+                ImmutableMap.<String, Object>of(CommitContext.NAME, new 
SimpleCommitContext()));
+        return info;
+    }
+
+    private static class TestCallback implements IndexUpdateCallback, 
IndexingContext, ContextAwareCallback {
+        private final String indexPath;
+        private final CommitInfo commitInfo;
+        private final boolean reindexing;
+        private final boolean async;
+
+        private TestCallback(String indexPath, CommitInfo commitInfo, boolean 
reindexing, boolean async) {
+            this.indexPath = indexPath;
+            this.commitInfo = commitInfo;
+            this.reindexing = reindexing;
+            this.async = async;
+        }
+
+        @Override
+        public String getIndexPath() {
+            return indexPath;
+        }
+
+        @Override
+        public CommitInfo getCommitInfo() {
+            return commitInfo;
+        }
+
+        @Override
+        public boolean isReindexing() {
+            return reindexing;
+        }
+
+        @Override
+        public boolean isAsync() {
+            return async;
+        }
+
+        @Override
+        public void indexUpdate() throws CommitFailedException {
+
+        }
+
+        @Override
+        public IndexingContext getIndexingContext() {
+            return this;
+        }
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to