geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zho...@apache.org
Subject [geode] branch feature/GEODE-3239 updated: GEODE-3273: If serializer returns empty, should update index. Add junit tests for IndexRepositoryImpl.update()
Date Thu, 28 Sep 2017 17:24:44 GMT
This is an automated email from the ASF dual-hosted git repository.

zhouxj pushed a commit to branch feature/GEODE-3239
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-3239 by this push:
     new 3cc1ccc  GEODE-3273: If serializer returns empty, should update index.          
  Add junit tests for IndexRepositoryImpl.update()
3cc1ccc is described below

commit 3cc1cccc65425aa64d76358401461fe062cdfaa5
Author: zhouxh <gzhou@pivotal.io>
AuthorDate: Wed Sep 27 17:29:43 2017 -0700

    GEODE-3273: If serializer returns empty, should update index.
                Add junit tests for IndexRepositoryImpl.update()
---
 .../internal/repository/IndexRepositoryImpl.java   | 14 +++++--
 .../repository/IndexRepositoryImplJUnitTest.java   | 44 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImpl.java
b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImpl.java
index 0032fe9..06bf653 100644
--- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImpl.java
+++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImpl.java
@@ -66,7 +66,7 @@ public class IndexRepositoryImpl implements IndexRepository {
     this.region = region;
     this.userRegion = userRegion;
     this.writer = writer;
-    searcherManager = new SearcherManager(writer, APPLY_ALL_DELETES, true, null);
+    searcherManager = createSearchManager();
     this.serializer = serializer;
     this.stats = stats;
     documentCountSupplier = new DocumentCountSupplier();
@@ -76,18 +76,24 @@ public class IndexRepositoryImpl implements IndexRepository {
     this.index = index;
   }
 
+  protected SearcherManager createSearchManager() throws IOException {
+    return new SearcherManager(writer, APPLY_ALL_DELETES, true, null);
+  }
+
   @Override
   public void create(Object key, Object value) throws IOException {
     long start = stats.startUpdate();
     Collection<Document> docs = Collections.emptyList();
+    boolean exceptionHappened = false;
     try {
       try {
         docs = serializer.toDocuments(index, value);
       } catch (Exception e) {
+        exceptionHappened = true;
         stats.incFailedEntries();
         logger.info("Failed to add index for " + value + " due to " + e.getMessage());
       }
-      if (!docs.isEmpty()) {
+      if (!exceptionHappened) {
         docs.forEach(doc -> SerializerUtil.addKey(key, doc));
         writer.addDocuments(docs);
       }
@@ -100,14 +106,16 @@ public class IndexRepositoryImpl implements IndexRepository {
   public void update(Object key, Object value) throws IOException {
     long start = stats.startUpdate();
     Collection<Document> docs = Collections.emptyList();
+    boolean exceptionHappened = false;
     try {
       try {
         docs = serializer.toDocuments(index, value);
       } catch (Exception e) {
+        exceptionHappened = true;
         stats.incFailedEntries();
         logger.info("Failed to update index for " + value + " due to " + e.getMessage());
       }
-      if (!docs.isEmpty()) {
+      if (!exceptionHappened) {
         docs.forEach(doc -> SerializerUtil.addKey(key, doc));
         Term keyTerm = SerializerUtil.toKeyTerm(key);
         writer.updateDocuments(keyTerm, docs);
diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
index bac4c12..c564ff3 100644
--- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
+++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/repository/IndexRepositoryImplJUnitTest.java
@@ -21,6 +21,7 @@ import static org.mockito.Mockito.*;
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Random;
 import java.util.Set;
@@ -35,6 +36,7 @@ import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.queryparser.classic.ParseException;
 import org.apache.lucene.queryparser.classic.QueryParser;
+import org.apache.lucene.search.SearcherManager;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -43,7 +45,11 @@ import org.mockito.Mockito;
 
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.lucene.LuceneIndex;
+import org.apache.geode.cache.lucene.LuceneSerializer;
+import org.apache.geode.cache.lucene.LuceneService;
+import org.apache.geode.cache.lucene.internal.LuceneIndexFactoryImpl;
 import org.apache.geode.cache.lucene.internal.LuceneIndexStats;
+import org.apache.geode.cache.lucene.internal.LuceneServiceImpl;
 import org.apache.geode.cache.lucene.internal.directory.RegionDirectory;
 import org.apache.geode.cache.lucene.internal.filesystem.FileSystemStats;
 import org.apache.geode.cache.lucene.internal.repository.serializer.HeterogeneousLuceneSerializer;
@@ -141,6 +147,32 @@ public class IndexRepositoryImplJUnitTest {
   }
 
   @Test
+  public void updateShouldHandleException() throws IOException {
+    LuceneIndex index = Mockito.mock(LuceneIndex.class);
+    LuceneSerializer serializer = Mockito.mock(LuceneSerializer.class);
+    IndexWriter writerSpy = spy(writer);
+    repo = new DummyIndexRepositoryImpl(region, writerSpy, serializer, stats, userRegion,
+        mock(DistributedLockService.class), "lockName", index);
+    Mockito.when(serializer.toDocuments(any(), any()))
+        .thenThrow(new RuntimeException("SerializerException"));
+    repo.update("key1", new Type2("bacon maple bar", 1, 2L, 3.0, 4.0f, "Grape Ape doughnut"));
+    verify(writerSpy, never()).updateDocuments(any(), any());
+  }
+
+  @Test
+  public void emptyDocsShouldUpdateDocuments() throws IOException {
+    LuceneIndex index = Mockito.mock(LuceneIndex.class);
+    LuceneSerializer serializer = Mockito.mock(LuceneSerializer.class);
+    IndexWriter writerSpy = spy(writer);
+    repo = new DummyIndexRepositoryImpl(region, writerSpy, serializer, stats, userRegion,
+        mock(DistributedLockService.class), "lockName", index);
+    Mockito.when(serializer.toDocuments(any(), any())).thenReturn(Collections.emptyList());
+    Mockito.doReturn(0L).when(writerSpy).updateDocuments(any(), any());
+    repo.update("key1", new Type2("bacon maple bar", 1, 2L, 3.0, 4.0f, "Grape Ape doughnut"));
+    verify(writerSpy).updateDocuments(any(), any());
+  }
+
+  @Test
   public void deleteShouldUpdateStats() throws IOException {
     repo.delete("key1");
     verify(stats, times(1)).startUpdate();
@@ -284,4 +316,16 @@ public class IndexRepositoryImplJUnitTest {
     }
   }
 
+  private class DummyIndexRepositoryImpl extends IndexRepositoryImpl {
+    public DummyIndexRepositoryImpl(Region<?, ?> region, IndexWriter writer,
+        LuceneSerializer serializer, LuceneIndexStats stats, Region<?, ?> userRegion,
+        DistributedLockService lockService, String lockName, LuceneIndex index) throws IOException
{
+      super(region, writer, serializer, stats, userRegion, lockService, lockName, index);
+    }
+
+    protected SearcherManager createSearchManager() throws IOException {
+      return new SearcherManager(writer, true, true, null);
+    }
+  }
+
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <commits@geode.apache.org>'].

Mime
View raw message