This is an automated email from the ASF dual-hosted git repository. ladyvader pushed a commit to branch feature/GEODE-3930 in repository https://gitbox.apache.org/repos/asf/geode.git
commit bbcfdc8bd0e09153119ea16570033fd5443dde95 Author: Dan Smith <[email protected]> AuthorDate: Wed Nov 1 17:09:57 2017 -0700 Start of internal method to create a lucene index on an existing region Adding a parameter to LuceneIndexFactoryImpl.create to allow creation on an existing region. Started implementing LuceneService.createIndexOnExistingRegion to do what we need it to do. This needs to do a lot of what LuceneRegionListener is doing, but some parts are different because it is modifying an existing region. --- .../lucene/internal/LuceneIndexFactoryImpl.java | 19 ++++--- .../cache/lucene/internal/LuceneIndexImpl.java | 2 + .../lucene/internal/LuceneRegionListener.java | 18 +----- .../cache/lucene/internal/LuceneServiceImpl.java | 65 +++++++++++++++++++--- .../lucene/internal/xml/LuceneIndexCreation.java | 3 +- .../lucene/LuceneIndexCreationIntegrationTest.java | 10 ++++ .../internal/LuceneIndexFactoryImplJUnitTest.java | 3 +- .../internal/LuceneServiceImplJUnitTest.java | 4 +- 8 files changed, 87 insertions(+), 37 deletions(-) diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImpl.java b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImpl.java index 429da38..6353ce7 100644 --- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImpl.java +++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImpl.java @@ -23,7 +23,7 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer; import java.util.LinkedHashMap; import java.util.Map; -public class LuceneIndexFactoryImpl implements org.apache.geode.cache.lucene.LuceneIndexFactory { +public class LuceneIndexFactoryImpl implements LuceneIndexFactory { private final LuceneServiceImpl service; private final Map<String, Analyzer> fields = new LinkedHashMap<String, Analyzer>(); private LuceneSerializer serializer; @@ -34,12 +34,12 @@ public class LuceneIndexFactoryImpl implements org.apache.geode.cache.lucene.Luc } @Override - public LuceneIndexFactory addField(final String name) { + public LuceneIndexFactoryImpl addField(final String name) { return addField(name, new StandardAnalyzer()); } @Override - public LuceneIndexFactory setFields(final String... fields) { + public LuceneIndexFactoryImpl setFields(final String... fields) { this.fields.clear(); for (String field : fields) { addField(field); @@ -48,13 +48,13 @@ public class LuceneIndexFactoryImpl implements org.apache.geode.cache.lucene.Luc } @Override - public LuceneIndexFactory addField(final String name, final Analyzer analyzer) { + public LuceneIndexFactoryImpl addField(final String name, final Analyzer analyzer) { fields.put(name, analyzer); return this; } @Override - public LuceneIndexFactory setFields(final Map<String, Analyzer> fieldMap) { + public LuceneIndexFactoryImpl setFields(final Map<String, Analyzer> fieldMap) { this.fields.clear(); this.fields.putAll(fieldMap); return this; @@ -62,11 +62,16 @@ public class LuceneIndexFactoryImpl implements org.apache.geode.cache.lucene.Luc @Override public void create(final String indexName, final String regionPath) { - service.createIndex(indexName, regionPath, fields, serializer); + this.create(indexName, regionPath, false); + } + + public void create(final String indexName, final String regionPath, + boolean allowOnExistingRegion) { + service.createIndex(indexName, regionPath, fields, serializer, false); } @Override - public LuceneIndexFactory setLuceneSerializer(LuceneSerializer luceneSerializer) { + public LuceneIndexFactoryImpl setLuceneSerializer(LuceneSerializer luceneSerializer) { this.serializer = luceneSerializer; return this; } diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexImpl.java b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexImpl.java index 2110ca5..fe4ca19 100644 --- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexImpl.java +++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneIndexImpl.java @@ -24,6 +24,8 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.geode.InternalGemFireError; import org.apache.geode.cache.Cache; import org.apache.geode.cache.DataPolicy; +import org.apache.geode.cache.EvictionAlgorithm; +import org.apache.geode.cache.EvictionAttributes; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; import org.apache.geode.cache.asyncqueue.AsyncEventQueue; diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneRegionListener.java b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneRegionListener.java index df9dca0..fa44f63 100644 --- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneRegionListener.java +++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneRegionListener.java @@ -20,8 +20,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.lucene.analysis.Analyzer; import org.apache.geode.cache.AttributesFactory; -import org.apache.geode.cache.EvictionAlgorithm; -import org.apache.geode.cache.EvictionAttributes; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; import org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl; @@ -83,21 +81,7 @@ public class LuceneRegionListener implements RegionListener { if (path.equals(this.regionPath) && this.beforeCreateInvoked.compareAndSet(false, true)) { - if (!attrs.getDataPolicy().withPartitioning()) { - // replicated region - throw new UnsupportedOperationException( - "Lucene indexes on replicated regions are not supported"); - } - - // For now we cannot support eviction with local destroy. - // Eviction with overflow to disk still needs to be supported - EvictionAttributes evictionAttributes = attrs.getEvictionAttributes(); - EvictionAlgorithm evictionAlgorithm = evictionAttributes.getAlgorithm(); - if (evictionAlgorithm != EvictionAlgorithm.NONE - && evictionAttributes.getAction().isLocalDestroy()) { - throw new UnsupportedOperationException( - "Lucene indexes on regions with eviction and action local destroy are not supported"); - } + LuceneServiceImpl.validateRegionAttributes(attrs); String aeqId = LuceneServiceImpl.getUniqueIndexName(this.indexName, this.regionPath); if (!attrs.getAsyncEventQueueIds().contains(aeqId)) { diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java index 985e3ad..e0ab851 100644 --- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java +++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java @@ -32,7 +32,10 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper; import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.geode.cache.AttributesFactory; import org.apache.geode.cache.Cache; +import org.apache.geode.cache.EvictionAlgorithm; +import org.apache.geode.cache.EvictionAttributes; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; import org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl; @@ -202,20 +205,21 @@ public class LuceneServiceImpl implements InternalLuceneService { } public void createIndex(String indexName, String regionPath, Map<String, Analyzer> fieldAnalyzers, - LuceneSerializer serializer) { + LuceneSerializer serializer, boolean allowOnExistingRegion) { if (fieldAnalyzers == null || fieldAnalyzers.isEmpty()) { throw new IllegalArgumentException("At least one field must be indexed"); } Analyzer analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(), fieldAnalyzers); Set<String> fieldsSet = fieldAnalyzers.keySet(); - String[] fields = (String[]) fieldsSet.toArray(new String[fieldsSet.size()]); + String[] fields = fieldsSet.toArray(new String[fieldsSet.size()]); - createIndex(indexName, regionPath, analyzer, fieldAnalyzers, serializer, fields); + createIndex(indexName, regionPath, analyzer, fieldAnalyzers, serializer, allowOnExistingRegion, + fields); } public void createIndex(final String indexName, String regionPath, final Analyzer analyzer, final Map<String, Analyzer> fieldAnalyzers, final LuceneSerializer serializer, - final String... fields) { + boolean allowOnExistingRegion, final String... fields) { if (!regionPath.startsWith("/")) { regionPath = "/" + regionPath; @@ -225,13 +229,60 @@ public class LuceneServiceImpl implements InternalLuceneService { regionPath, fields, analyzer, fieldAnalyzers, serializer)); Region region = cache.getRegion(regionPath); - if (region != null) { + + LuceneRegionListener regionListener = new LuceneRegionListener(this, cache, indexName, + regionPath, fields, analyzer, fieldAnalyzers, serializer); + if (region == null) { + cache.addRegionListener(regionListener); + return; + } + + if (!allowOnExistingRegion) { definedIndexMap.remove(LuceneServiceImpl.getUniqueIndexName(indexName, regionPath)); throw new IllegalStateException("The lucene index must be created before region"); } - cache.addRegionListener(new LuceneRegionListener(this, cache, indexName, regionPath, fields, - analyzer, fieldAnalyzers, serializer)); + + createIndexOnExistingRegion(region, indexName, regionPath, fields, analyzer, fieldAnalyzers, + serializer); + + } + + private void createIndexOnExistingRegion(Region region, String indexName, String regionPath, + String[] fields, Analyzer analyzer, Map<String, Analyzer> fieldAnalyzers, + LuceneSerializer serializer) { + validateRegionAttributes(region.getAttributes()); + + String aeqId = LuceneServiceImpl.getUniqueIndexName(indexName, regionPath); + region.getAttributesMutator().addAsyncEventQueueId(aeqId); + + // Add index creation profile + internalRegionArgs.addCacheServiceProfile(new LuceneIndexCreationProfile(indexName, regionPath, + fields, analyzer, fieldAnalyzers, serializer)); + + LuceneIndexImpl luceneIndex = beforeDataRegionCreated(indexName, regionPath, + region.getAttributes(), analyzer, fieldAnalyzers, aeqId, serializer, fields); + + // Add internal async event id + internalRegionArgs.addInternalAsyncEventQueueId(aeqId); + } + + static void validateRegionAttributes(RegionAttributes attrs) { + if (!attrs.getDataPolicy().withPartitioning()) { + // replicated region + throw new UnsupportedOperationException( + "Lucene indexes on replicated regions are not supported"); + } + + // For now we cannot support eviction with local destroy. + // Eviction with overflow to disk still needs to be supported + EvictionAttributes evictionAttributes = attrs.getEvictionAttributes(); + EvictionAlgorithm evictionAlgorithm = evictionAttributes.getAlgorithm(); + if (evictionAlgorithm != EvictionAlgorithm.NONE + && evictionAttributes.getAction().isLocalDestroy()) { + throw new UnsupportedOperationException( + "Lucene indexes on regions with eviction and action local destroy are not supported"); + } } /** diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/xml/LuceneIndexCreation.java b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/xml/LuceneIndexCreation.java index 7af43fb..3ed1bba 100644 --- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/xml/LuceneIndexCreation.java +++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/xml/LuceneIndexCreation.java @@ -17,7 +17,6 @@ package org.apache.geode.cache.lucene.internal.xml; import java.util.*; -import org.apache.geode.cache.Declarable; import org.apache.geode.cache.lucene.LuceneIndexExistsException; import org.apache.geode.cache.lucene.LuceneSerializer; import org.apache.geode.internal.i18n.LocalizedStrings; @@ -100,7 +99,7 @@ public class LuceneIndexCreation implements LuceneIndex, Extension<Region<?, ?>> : new PerFieldAnalyzerWrapper(new StandardAnalyzer(), this.fieldAnalyzers); try { service.createIndex(getName(), getRegionPath(), analyzer, this.fieldAnalyzers, - getLuceneSerializer(), getFieldNames()); + getLuceneSerializer(), false, getFieldNames()); } catch (LuceneIndexExistsException e) { logger .info(LocalizedStrings.LuceneIndexCreation_IGNORING_DUPLICATE_INDEX_CREATION_0_ON_REGION_1 diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationIntegrationTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationIntegrationTest.java index 1d688fb..d0a1da8 100644 --- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationIntegrationTest.java +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationIntegrationTest.java @@ -39,6 +39,7 @@ import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionFactory; import org.apache.geode.cache.RegionShortcut; import org.apache.geode.cache.lucene.internal.LuceneIndexCreationProfile; +import org.apache.geode.cache.lucene.internal.LuceneIndexFactoryImpl; import org.apache.geode.cache.lucene.internal.LuceneIndexImplFactory; import org.apache.geode.cache.lucene.internal.LuceneRawIndex; import org.apache.geode.cache.lucene.internal.LuceneRawIndexFactory; @@ -184,6 +185,15 @@ public class LuceneIndexCreationIntegrationTest extends LuceneIntegrationTest { createIndex("field1", "field2", "field3"); } + @Test() + public void canCreateLuceneIndexAfterRegionCreatedIfAllowFlagIsSet() + throws IOException, ParseException { + createRegion(); + final LuceneIndexFactoryImpl indexFactory = + (LuceneIndexFactoryImpl) LuceneServiceProvider.get(cache).createIndexFactory(); + indexFactory.setFields("field1", "field2").create(INDEX_NAME, REGION_NAME, true); + } + @Test public void cannotCreateLuceneIndexForReplicateRegion() throws IOException, ParseException { try { diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImplJUnitTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImplJUnitTest.java index 67759db..cd4afa1 100644 --- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImplJUnitTest.java +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneIndexFactoryImplJUnitTest.java @@ -14,7 +14,6 @@ */ package org.apache.geode.cache.lucene.internal; -import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; @@ -37,7 +36,7 @@ public class LuceneIndexFactoryImplJUnitTest { LuceneIndexFactory factory = new LuceneIndexFactoryImpl(service); factory.setLuceneSerializer(serializer); factory.create("index", "region"); - Mockito.verify(service).createIndex(eq("index"), eq("region"), any(), eq(serializer)); + Mockito.verify(service).createIndex(eq("index"), eq("region"), any(), eq(serializer), false); } } diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneServiceImplJUnitTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneServiceImplJUnitTest.java index c5ca8f8..c24495e 100644 --- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneServiceImplJUnitTest.java +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/LuceneServiceImplJUnitTest.java @@ -62,7 +62,7 @@ public class LuceneServiceImplJUnitTest { factory.setLuceneSerializer(serializer); factory.setFields("field1", "field2"); factory.create("index", "region"); - Mockito.verify(service).createIndex(eq("index"), eq("region"), any(), eq(serializer)); + Mockito.verify(service).createIndex(eq("index"), eq("region"), any(), eq(serializer), false); } @Test @@ -74,7 +74,7 @@ public class LuceneServiceImplJUnitTest { @Test public void shouldThrowIllegalArgumentExceptionIfFieldsMapIsMissing() { thrown.expect(IllegalArgumentException.class); - service.createIndex("index", "region", Collections.emptyMap(), null); + service.createIndex("index", "region", Collections.emptyMap(), null, false); } @Test -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
