Added: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/LocalIndexWriterFactoryTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/LocalIndexWriterFactoryTest.java?rev=1760831&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/LocalIndexWriterFactoryTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/LocalIndexWriterFactoryTest.java
 Thu Sep 15 07:14:06 2016
@@ -0,0 +1,182 @@
+/*
+ * 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.hybrid;
+
+import java.io.IOException;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.core.SimpleCommitContext;
+import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.IndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.index.IndexUpdateProvider;
+import 
org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorProvider;
+import org.apache.jackrabbit.oak.spi.commit.CommitContext;
+import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
+import org.apache.jackrabbit.oak.spi.commit.EditorHook;
+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.After;
+import org.junit.Before;
+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.util.LuceneIndexHelper.newLucenePropertyIndexDefinition;
+import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+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.*;
+
+public class LocalIndexWriterFactoryTest {
+    private NodeState root = INITIAL_CONTENT;
+    private NodeBuilder builder = root.builder();
+
+    private EditorHook syncHook;
+    private EditorHook asyncHook;
+    private CommitInfo info;
+
+    @Before
+    public void setUp() throws IOException {
+        IndexEditorProvider editorProvider = new LuceneIndexEditorProvider(
+                null,
+                null,
+                null,
+                Mounts.defaultMountInfoProvider()
+        );
+
+        syncHook = new EditorHook(new IndexUpdateProvider(editorProvider));
+        asyncHook = new EditorHook(new IndexUpdateProvider(editorProvider, 
"async", false));
+    }
+
+    @After
+    public void cleanup() throws IOException {
+    }
+
+    @Test
+    public void ignoreReindexCase() throws Exception{
+        createIndexDefinition("fooIndex");
+
+        builder.child("a").setProperty("foo", "bar");
+        NodeState after = builder.getNodeState();
+        syncHook.processCommit(EMPTY_NODE, after, newCommitInfo());
+
+        //This is reindex case so nothing would be indexed
+        //So now holder should be present in context
+        assertNull(getHolder());
+        
assertNull(getCommitAttribute(LocalIndexWriterFactory.COMMIT_PROCESSED_BY_LOCAL_LUCENE_EDITOR));
+    }
+
+    @Test
+    public void holderNotInitializedUnlessIndexed() throws Exception{
+        NodeState indexed = createAndPopulateAsyncIndex();
+        builder = indexed.builder();
+        builder.child("b");
+        NodeState after = builder.getNodeState();
+        syncHook.processCommit(indexed, after, newCommitInfo());
+
+        //This is incremental index case but no entry for fooIndex
+        //so holder should be null
+        assertNull(getHolder());
+        
assertNotNull(getCommitAttribute(LocalIndexWriterFactory.COMMIT_PROCESSED_BY_LOCAL_LUCENE_EDITOR));
+    }
+
+    @Test
+    public void localIndexWriter() throws Exception{
+        NodeState indexed = createAndPopulateAsyncIndex();
+        builder = indexed.builder();
+        builder.child("b").setProperty("foo", "bar");
+        builder.child("c").setProperty("foo", "bar");
+        builder.child("a").remove();
+        NodeState after = builder.getNodeState();
+        syncHook.processCommit(indexed, after, newCommitInfo());
+
+        LuceneDocumentHolder holder = getHolder();
+        assertNotNull(holder);
+
+        //2 add and 1 delete
+        assertEquals(3, 
holder.getAsyncIndexedDocList("/oak:index/fooIndex").size());
+    }
+
+    @Test
+    public void mutlipleIndex() throws Exception{
+        NodeState indexed = createAndPopulateTwoAsyncIndex();
+        builder = indexed.builder();
+        builder.child("b").setProperty("foo", "bar");
+        builder.child("c").setProperty("bar", "foo");
+        builder.child("a").remove();
+        NodeState after = builder.getNodeState();
+        syncHook.processCommit(indexed, after, newCommitInfo());
+
+        LuceneDocumentHolder holder = getHolder();
+        assertNotNull(holder);
+
+        //1 add and 1 delete - bar
+        assertEquals(2, 
holder.getAsyncIndexedDocList("/oak:index/fooIndex").size());
+
+        //1 add and 1 delete - bar
+        assertEquals(2, 
holder.getAsyncIndexedDocList("/oak:index/barIndex").size());
+
+    }
+
+    private NodeState createAndPopulateAsyncIndex() throws 
CommitFailedException {
+        createIndexDefinition("fooIndex");
+
+        //Have some stuff to be indexed
+        builder.child("a").setProperty("foo", "bar");
+        NodeState after = builder.getNodeState();
+        return asyncHook.processCommit(EMPTY_NODE, after, newCommitInfo());
+    }
+
+    private NodeState createAndPopulateTwoAsyncIndex() throws 
CommitFailedException {
+        createIndexDefinition("fooIndex");
+        createIndexDefinition("barIndex");
+
+        //Have some stuff to be indexed
+        builder.child("a").setProperty("foo", "bar");
+        builder.child("a").setProperty("bar", "foo");
+        NodeState after = builder.getNodeState();
+        return asyncHook.processCommit(EMPTY_NODE, after, newCommitInfo());
+    }
+
+    private LuceneDocumentHolder getHolder(){
+        return (LuceneDocumentHolder) 
getCommitAttribute(LuceneDocumentHolder.NAME);
+    }
+
+    private Object getCommitAttribute(String name){
+        CommitContext cc = (CommitContext) 
info.getInfo().get(CommitContext.NAME);
+        return cc.get(name);
+    }
+
+    private CommitInfo newCommitInfo(){
+        info = new CommitInfo("admin", "s1",
+                ImmutableMap.<String, Object>of(CommitContext.NAME, new 
SimpleCommitContext()));
+        return info;
+    }
+
+    private void 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));
+    }
+
+}
\ No newline at end of file

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

Added: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexFactoryTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexFactoryTest.java?rev=1760831&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexFactoryTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexFactoryTest.java
 Thu Sep 15 07:14:06 2016
@@ -0,0 +1,112 @@
+/*
+ * 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.hybrid;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopier;
+import org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static 
com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
+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.*;
+
+public class NRTIndexFactoryTest {
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder(new 
File("target"));
+
+    private NodeState root = INITIAL_CONTENT;
+    private NodeBuilder builder = root.builder();
+
+    private IndexCopier indexCopier;
+    private NRTIndexFactory indexFactory;
+
+    @Before
+    public void setUp() throws IOException {
+        indexCopier = new IndexCopier(sameThreadExecutor(), 
temporaryFolder.getRoot());
+        indexFactory = new NRTIndexFactory(indexCopier);
+    }
+
+    @Test
+    public void noIndexForAsync() throws Exception{
+        IndexDefinition idxDefn = new IndexDefinition(root, 
builder.getNodeState());
+        assertNull(indexFactory.createIndex(idxDefn));
+    }
+
+    @Test
+    public void indexCreation() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+
+        NRTIndex idx1 = indexFactory.createIndex(idxDefn);
+        assertNotNull(idx1);
+        assertEquals(1, indexFactory.getIndexes("/foo").size());
+    }
+
+    @Test
+    public void indexCreationAndCloser() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+
+        NRTIndex idx1 = indexFactory.createIndex(idxDefn);
+        assertNotNull(idx1);
+        assertEquals(1, indexFactory.getIndexes("/foo").size());
+
+        NRTIndex idx2 = indexFactory.createIndex(idxDefn);
+        assertEquals(2, indexFactory.getIndexes("/foo").size());
+        assertFalse(idx1.isClosed());
+
+        NRTIndex idx3 = indexFactory.createIndex(idxDefn);
+        assertEquals(2, indexFactory.getIndexes("/foo").size());
+
+        //With 2 generation open the first one should be closed
+        assertTrue(idx1.isClosed());
+    }
+
+    @Test
+    public void closeIndexOnClose() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+
+        NRTIndex idx1 = indexFactory.createIndex(idxDefn);
+        NRTIndex idx2 = indexFactory.createIndex(idxDefn);
+        assertEquals(2, indexFactory.getIndexes("/foo").size());
+
+        indexFactory.close();
+        assertEquals(0, indexFactory.getIndexes("/foo").size());
+        assertTrue(idx1.isClosed());
+        assertTrue(idx2.isClosed());
+    }
+
+    private IndexDefinition getSyncIndexDefinition(String indexPath) {
+        builder.setProperty(IndexConstants.INDEX_PATH, indexPath);
+        builder.setProperty(createProperty(IndexConstants.ASYNC_PROPERTY_NAME, 
of("sync" , "async"), STRINGS));
+
+        return new IndexDefinition(root, builder.getNodeState());
+    }
+}
\ No newline at end of file

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

Added: 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexTest.java?rev=1760831&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/hybrid/NRTIndexTest.java
 Thu Sep 15 07:14:06 2016
@@ -0,0 +1,177 @@
+/*
+ * 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.hybrid;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopier;
+import org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition;
+import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorContext;
+import org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader;
+import org.apache.jackrabbit.oak.plugins.index.lucene.writer.LuceneIndexWriter;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.lucene.document.Document;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static com.google.common.collect.ImmutableSet.of;
+import static 
com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
+import static 
org.apache.jackrabbit.oak.plugins.index.lucene.FieldFactory.newPathField;
+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.*;
+
+public class NRTIndexTest {
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder(new 
File("target"));
+
+    private NodeState root = INITIAL_CONTENT;
+    private NodeBuilder builder = root.builder();
+
+    private IndexCopier indexCopier;
+    private NRTIndexFactory indexFactory;
+
+    @Before
+    public void setUp() throws IOException {
+        indexCopier = new IndexCopier(sameThreadExecutor(), 
temporaryFolder.getRoot());
+        indexFactory = new NRTIndexFactory(indexCopier);
+        LuceneIndexEditorContext.configureUniqueId(builder);
+    }
+
+    @After
+    public void cleanup() throws IOException {
+        indexFactory.close();
+        indexCopier.close();
+    }
+
+    @Test
+    public void getReaderWithoutWriter() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+
+        NRTIndex idx1 = indexFactory.createIndex(idxDefn);
+        List<LuceneIndexReader> readers = idx1.getReaders();
+        assertNotNull(readers);
+        assertTrue(readers.isEmpty());
+
+        idx1.close();
+        assertTrue(idx1.isClosed());
+
+        //Closing multiple times should not raise exception
+        idx1.close();
+    }
+
+    @Test
+    public void writerCreation() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+        NRTIndex idx = indexFactory.createIndex(idxDefn);
+        LuceneIndexWriter writer = idx.getWriter();
+
+        assertNotNull(writer);
+        assertNotNull(idx.getIndexDir());
+        List<LuceneIndexReader> readers = idx.getReaders();
+        assertEquals(1, readers.size());
+
+        LuceneIndexWriter writer2 = idx.getWriter();
+        assertSame(writer, writer2);
+    }
+
+    @Test
+    public void dirDeletedUponClose() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+        NRTIndex idx = indexFactory.createIndex(idxDefn);
+        LuceneIndexWriter writer = idx.getWriter();
+        File indexDir = idx.getIndexDir();
+
+        assertTrue(indexDir.exists());
+
+        idx.close();
+        assertFalse(indexDir.exists());
+
+        try{
+            idx.getReaders();
+            fail();
+        } catch (IllegalStateException ignore){
+
+        }
+
+        try{
+            idx.getWriter();
+            fail();
+        } catch (IllegalStateException ignore){
+
+        }
+    }
+
+    @Test
+    public void multipleUpdateForSamePath() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+        NRTIndex idx = indexFactory.createIndex(idxDefn);
+        LuceneIndexWriter writer = idx.getWriter();
+
+        Document document = new Document();
+        document.add(newPathField("/a/b"));
+
+        writer.updateDocument("/a/b", document);
+        assertEquals(1, idx.getPrimaryReader().getReader().numDocs());
+
+        writer.updateDocument("/a/b", document);
+
+        //Update for same path should not lead to deletion
+        assertEquals(2, idx.getPrimaryReader().getReader().numDocs());
+        assertEquals(0, idx.getPrimaryReader().getReader().numDeletedDocs());
+    }
+
+    @Test
+    public void previousIndexInitialized() throws Exception{
+        IndexDefinition idxDefn = getSyncIndexDefinition("/foo");
+        NRTIndex idx1 = indexFactory.createIndex(idxDefn);
+        LuceneIndexWriter w1 = idx1.getWriter();
+
+        Document d1 = new Document();
+        d1.add(newPathField("/a/b"));
+        w1.updateDocument("/a/b", d1);
+
+        NRTIndex idx2 = indexFactory.createIndex(idxDefn);
+        assertEquals(1, idx2.getReaders().size());
+
+        LuceneIndexWriter w2 = idx2.getWriter();
+        assertEquals(2, idx2.getReaders().size());
+
+        assertNotEquals(idx1.getIndexDir(), idx2.getIndexDir());
+    }
+
+    private IndexDefinition getSyncIndexDefinition(String indexPath) {
+        builder.setProperty(IndexConstants.INDEX_PATH, indexPath);
+        builder.setProperty(createProperty(IndexConstants.ASYNC_PROPERTY_NAME, 
of("sync" , "async"), STRINGS));
+
+        return new IndexDefinition(root, builder.getNodeState());
+    }
+
+
+
+}
\ No newline at end of file

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


Reply via email to