Change default Index type from HASH to FUNCTIONAL (Range).

Resolves gh-531.
This commit is contained in:
John Blum
2021-08-20 11:04:17 -07:00
parent c431c4394e
commit acaa449688
5 changed files with 82 additions and 65 deletions

View File

@@ -13,11 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.gemfire; package org.springframework.data.gemfire;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/** /**
* The IndexType class is an enumerated type of GemFire Index Types. * {@link IndexType} is an enumerated type of Apache Geode {@link org.apache.geode.cache.query.IndexType Index Types}.
*
* NOTE: The Apache Geode {@link org.apache.geode.cache.query.IndexType} enum has been deprecated, therefore the SDG
* {@link IndexType} exists to replace it.
* *
* @author John Blum * @author John Blum
* @see org.apache.geode.cache.query.IndexType * @see org.apache.geode.cache.query.IndexType
@@ -31,58 +37,68 @@ public enum IndexType {
PRIMARY_KEY(org.apache.geode.cache.query.IndexType.PRIMARY_KEY), PRIMARY_KEY(org.apache.geode.cache.query.IndexType.PRIMARY_KEY),
KEY(org.apache.geode.cache.query.IndexType.PRIMARY_KEY); KEY(org.apache.geode.cache.query.IndexType.PRIMARY_KEY);
public static final IndexType DEFAULT = IndexType.FUNCTIONAL;
private final org.apache.geode.cache.query.IndexType gemfireIndexType; private final org.apache.geode.cache.query.IndexType gemfireIndexType;
/** /**
* Constructs an instance of the IndexType enum initialized with the given GemFire IndexType. * Constructs a new instance of the {@link IndexType} enum initialized with the given Apache Geode
* {@link org.apache.geode.cache.query.IndexType}.
* *
* @param gemfireIndexType the corresponding GemFire IndexType * @param gemfireIndexType an Apache Geode {@link org.apache.geode.cache.query.IndexType}.
* @throws IllegalArgumentException if the Apache Geode {@link org.apache.geode.cache.query.IndexType}
* is {@literal null}.
* @see org.apache.geode.cache.query.IndexType * @see org.apache.geode.cache.query.IndexType
*/ */
IndexType(final org.apache.geode.cache.query.IndexType gemfireIndexType) { IndexType(@NonNull org.apache.geode.cache.query.IndexType gemfireIndexType) {
Assert.notNull(gemfireIndexType, "The Apache Geode IndexType must not be null");
this.gemfireIndexType = gemfireIndexType; this.gemfireIndexType = gemfireIndexType;
} }
/** /**
* Null-safe operation to determine if the IndexType is a "FUNCTIONAL" Index. * Null-safe operation to determine if the given {@link IndexType} is a {@literal FUNCTIONAL} Index.
* *
* @param indexType the IndexType to evaluate. * @param indexType {@link IndexType} to evaluate.
* @return a boolean value indicating whether the IndexType is a "FUNCTIONAL" Index. * @return a boolean value indicating whether the given {@link IndexType} is a {@literal FUNCTIONAL} Index.
* @see #isFunctional() * @see #isFunctional()
*/ */
public static boolean isFunctional(IndexType indexType) { public static boolean isFunctional(@Nullable IndexType indexType) {
return (indexType != null && indexType.isFunctional()); return indexType != null && indexType.isFunctional();
} }
/** /**
* Null-safe operation to determine if the IndexType is a "HASH" Index. * Null-safe operation to determine if the given {@link IndexType} is a {@literal HASH} Index.
* *
* @param indexType the IndexType to evaluate. * @param indexType {@link IndexType} to evaluate.
* @return a boolean value indicating whether the IndexType is a "HASH" Index. * @return a boolean value indicating whether the given {@link IndexType} is a {@literal HASH} Index.
* @see #isHash() * @see #isHash()
*/ */
public static boolean isHash(IndexType indexType) { public static boolean isHash(IndexType indexType) {
return (indexType != null && indexType.isHash()); return indexType != null && indexType.isHash();
} }
/** /**
* Null-safe operation to determine if the IndexType is a "KEY" Index. * Null-safe operation to determine if the given {@link IndexType} is a {@literal KEY} Index.
* *
* @param indexType the IndexType to evaluate. * @param indexType {@link IndexType} to evaluate.
* @return a boolean value indicating whether the IndexType is a "KEY" Index. * @return a boolean value indicating whether the given {@link IndexType} is a {@literal KEY} Index.
* @see #isFunctional() * @see #isKey()
*/ */
public static boolean isKey(IndexType indexType) { public static boolean isKey(IndexType indexType) {
return (indexType != null && indexType.isKey()); return indexType != null && indexType.isKey();
} }
/** /**
* Returns an IndexType given the corresponding GemFire IndexType or null if no SDG IndexType * Returns an {@link IndexType} given the corresponding Apache Geode {@link org.apache.geode.cache.query.IndexType}
* corresponds to the GemFire IndexType. * or {@literal null} if no SDG {@link IndexType} corresponds to the given Apache Geode
* {@link org.apache.geode.cache.query.IndexType}.
* *
* @param gemfireIndexType the GemFire IndexType. * @param gemfireIndexType Apache Geode {@link org.apache.geode.cache.query.IndexType}.
* @return a IndexType matching the GemFire IndexType or null if the GemFire IndexType does not match * @return an {@link IndexType} matching the Apache Geode {@link org.apache.geode.cache.query.IndexType}
* any IndexType in this enumeration. * or {@literal null} if the Apache Geode {@link org.apache.geode.cache.query.IndexType} does not match
* any {@literal IndexType} in this enumeration.
* @see org.apache.geode.cache.query.IndexType * @see org.apache.geode.cache.query.IndexType
*/ */
public static IndexType valueOf(org.apache.geode.cache.query.IndexType gemfireIndexType) { public static IndexType valueOf(org.apache.geode.cache.query.IndexType gemfireIndexType) {
@@ -97,10 +113,10 @@ public enum IndexType {
} }
/** /**
* Returns an IndexType matching the given String. * Return an {@link IndexType} matching the given {@link String}.
* *
* @param value the String value describing the matching IndexType. * @param value {@link String} value describing {@link IndexType} to match.
* @return an IndexType matching the given String. * @return an {@link IndexType} matching the given {@link String}.
* @see java.lang.String#equalsIgnoreCase(String) * @see java.lang.String#equalsIgnoreCase(String)
*/ */
public static IndexType valueOfIgnoreCase(String value) { public static IndexType valueOfIgnoreCase(String value) {
@@ -115,39 +131,40 @@ public enum IndexType {
} }
/** /**
* Gets the matching GemFire IndexType for this IndexType enumerated value. * Gets the matching Apache Geode {@link org.apache.geode.cache.query.IndexType} for this {@link IndexType}
* enumerated value.
* *
* @return the matching GemFire IndexType. * @return the matching Apache Geode {@link org.apache.geode.cache.query.IndexType}.
* @see org.apache.geode.cache.query.IndexType * @see org.apache.geode.cache.query.IndexType
*/ */
public org.apache.geode.cache.query.IndexType getGemfireIndexType() { public @NonNull org.apache.geode.cache.query.IndexType getGemfireIndexType() {
return gemfireIndexType; return this.gemfireIndexType;
} }
/** /**
* Determines whether this IndexType is "FUNCTIONAL". * Determines whether this {@link IndexType} is a {@literal FUNCTIONAL} Index.
* *
* @return a boolean value indicating whether this IndexType is "FUNCTIONAL". * @return a boolean value indicating whether this {@link IndexType} is a {@literal FUNCTIONAL} Index.
*/ */
public boolean isFunctional() { public boolean isFunctional() {
return this.equals(FUNCTIONAL); return this.equals(FUNCTIONAL);
} }
/** /**
* Determines whether this IndexType is a "HASH" Index. * Determines whether this {@link IndexType} is a {@literal HASH} Index.
* *
* @return a boolean value indicating whether this IndexType is a "HASH" Index. * @return a boolean value indicating whether this {@literal IndexType} is a {@literal HASH} Index.
*/ */
public boolean isHash() { public boolean isHash() {
return this.equals(HASH); return this.equals(HASH);
} }
/** /**
* Determines whether this IndexType is a "KEY" Index. * Determines whether this {@literal IndexType} is a {@literal KEY} Index.
* *
* @return a boolean value indicating whether this IndexType is a "KEY" Index. * @return a boolean value indicating whether this {@link IndexType} is a {@literal KEY} Index.
*/ */
public boolean isKey() { public boolean isKey() {
return (this.equals(KEY) || this.equals(PRIMARY_KEY)); return this.equals(KEY) || this.equals(PRIMARY_KEY);
} }
} }

View File

@@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
* *
*/ */
package org.springframework.data.gemfire.mapping.annotation; package org.springframework.data.gemfire.mapping.annotation;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
@@ -67,7 +66,7 @@ public @interface Indexed {
String expression() default ""; String expression() default "";
/** /**
* The GemFire/Geode {@link org.apache.geode.cache.Region} on which the Index is created. * The Apache Geode {@link org.apache.geode.cache.Region} on which the {@link Index} is created.
*/ */
String from() default ""; String from() default "";
@@ -76,6 +75,6 @@ public @interface Indexed {
* *
* Defaults to {@link IndexType#HASH}. * Defaults to {@link IndexType#HASH}.
*/ */
IndexType type() default IndexType.HASH; IndexType type() default IndexType.FUNCTIONAL;
} }

View File

@@ -68,6 +68,8 @@ import org.springframework.data.gemfire.config.annotation.test.entities.GenericR
import org.springframework.data.gemfire.config.annotation.test.entities.LocalRegionEntity; import org.springframework.data.gemfire.config.annotation.test.entities.LocalRegionEntity;
import org.springframework.data.gemfire.config.annotation.test.entities.NonEntity; import org.springframework.data.gemfire.config.annotation.test.entities.NonEntity;
import org.springframework.data.gemfire.config.annotation.test.entities.ReplicateRegionEntity; import org.springframework.data.gemfire.config.annotation.test.entities.ReplicateRegionEntity;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
/** /**
* Unit Tests for the {@link EnableIndexing} and {@link IndexConfiguration} class. * Unit Tests for the {@link EnableIndexing} and {@link IndexConfiguration} class.
@@ -95,19 +97,11 @@ public class EnableIndexingConfigurationUnitTests {
private static final Set<Index> indexes = Collections.synchronizedSet(new HashSet<>()); private static final Set<Index> indexes = Collections.synchronizedSet(new HashSet<>());
private ConfigurableApplicationContext applicationContext; private static @NonNull String[] asArray(@NonNull List<String> list) {
@After
public void tearDown() {
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
indexes.clear();
}
private static String[] asArray(List<String> list) {
return list.toArray(new String[0]); return list.toArray(new String[0]);
} }
private static String[] toStringArray(Object[] array) { private static @NonNull String[] toStringArray(@NonNull Object[] array) {
String[] stringArray = new String[array.length]; String[] stringArray = new String[array.length];
@@ -120,8 +114,7 @@ public class EnableIndexingConfigurationUnitTests {
return stringArray; return stringArray;
} }
/* (non-Javadoc) */ private static @Nullable Index findIndexByName(@Nullable String indexName) {
private static Index findIndexByName(String indexName) {
for (Index index : indexes) { for (Index index : indexes) {
if (index.getName().equalsIgnoreCase(indexName)) { if (index.getName().equalsIgnoreCase(indexName)) {
@@ -132,7 +125,17 @@ public class EnableIndexingConfigurationUnitTests {
return null; return null;
} }
/* (non-Javadoc) */ private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
Optional.ofNullable(this.applicationContext)
.ifPresent(ConfigurableApplicationContext::close);
indexes.clear();
}
private void assertLuceneIndex(LuceneIndex index, String name, String regionPath, String... fields) { private void assertLuceneIndex(LuceneIndex index, String name, String regionPath, String... fields) {
assertThat(index).isNotNull(); assertThat(index).isNotNull();
@@ -142,7 +145,6 @@ public class EnableIndexingConfigurationUnitTests {
assertThat(index.getFieldNames()).contains(fields); assertThat(index.getFieldNames()).contains(fields);
} }
/* (non-Javadoc) */
private void assertOqlIndex(Index index, String name, String expression, String from, IndexType indexType) { private void assertOqlIndex(Index index, String name, String expression, String from, IndexType indexType) {
assertThat(index).isNotNull(); assertThat(index).isNotNull();
@@ -152,7 +154,6 @@ public class EnableIndexingConfigurationUnitTests {
assertThat(index.getType()).isEqualTo(indexType.getGemfireIndexType()); assertThat(index.getType()).isEqualTo(indexType.getGemfireIndexType());
} }
/* (non-Javadoc) */
private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) { private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(annotatedClasses); ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(annotatedClasses);
@@ -240,6 +241,7 @@ public class EnableIndexingConfigurationUnitTests {
return mockQueryService(mockRegionFactory(mock(Cache.class, "MockGemFireCache"))); return mockQueryService(mockRegionFactory(mock(Cache.class, "MockGemFireCache")));
} }
@SuppressWarnings("deprecation")
Cache mockQueryService(Cache mockCache) throws Exception { Cache mockQueryService(Cache mockCache) throws Exception {
QueryService mockQueryService = mock(QueryService.class); QueryService mockQueryService = mock(QueryService.class);

View File

@@ -13,21 +13,20 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.gemfire.config.annotation; package org.springframework.data.gemfire.config.annotation;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.apache.geode.cache.DataPolicy; import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.Region; import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.cache.query.Index; import org.apache.geode.cache.query.Index;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
import org.springframework.data.gemfire.GemfireUtils; import org.springframework.data.gemfire.GemfireUtils;
@@ -63,7 +62,7 @@ public class EnableOqlIndexingConfigurationIntegrationTests {
@Resource(name = "PeopleIdKeyIdx") @Resource(name = "PeopleIdKeyIdx")
private Index personIdKeyIndex; private Index personIdKeyIndex;
@Resource(name = "PeopleLastNameHashIdx") @Resource(name = "PeopleLastNameFunctionalIdx")
private Index personLastNameHashIndex; private Index personLastNameHashIndex;
@Test @Test
@@ -92,8 +91,8 @@ public class EnableOqlIndexingConfigurationIntegrationTests {
public void idKeyIndexAndLastNameHashIndexAreSetupCorrectly() { public void idKeyIndexAndLastNameHashIndexAreSetupCorrectly() {
assertIndex(this.personIdKeyIndex, "PeopleIdKeyIdx", "id", "/People", IndexType.KEY); assertIndex(this.personIdKeyIndex, "PeopleIdKeyIdx", "id", "/People", IndexType.KEY);
assertIndex(this.personLastNameHashIndex, "PeopleLastNameHashIdx", assertIndex(this.personLastNameHashIndex, "PeopleLastNameFunctionalIdx",
"lastName", "/People", IndexType.HASH); "lastName", "/People", IndexType.FUNCTIONAL);
} }
@ClientCacheApplication(logLevel = "none") @ClientCacheApplication(logLevel = "none")

View File

@@ -42,10 +42,10 @@ public class PartitionRegionEntity {
@Id @Id
private Long id; private Long id;
@Indexed(expression = "first_name", from = "/LoyalCustomers", type = IndexType.FUNCTIONAL) @Indexed(expression = "first_name", from = "/LoyalCustomers")
private String firstName; private String firstName;
@Indexed(name = "LastNameIdx", expression = "surname") @Indexed(name = "LastNameIdx", expression = "surname", type = IndexType.HASH)
private String lastName; private String lastName;
@LuceneIndexed("TitleLuceneIdx") @LuceneIndexed("TitleLuceneIdx")