Example usage for org.springframework.data.gemfire IndexType HASH

List of usage examples for org.springframework.data.gemfire IndexType HASH

Introduction

In this page you can find the example usage for org.springframework.data.gemfire IndexType HASH.

Prototype

IndexType HASH

To view the source code for org.springframework.data.gemfire IndexType HASH.

Click Source Link

Usage

From source file:example.app.config.gemfire.GemFireConfiguration.java

@Bean
@DependsOn("Contacts")
public IndexFactoryBean emailIndex(GemFireCache gemfireCache) {
    IndexFactoryBean lastNameIndex = new IndexFactoryBean();

    lastNameIndex.setCache(gemfireCache);
    lastNameIndex.setExpression("email");
    lastNameIndex.setFrom("/Contacts");
    lastNameIndex.setName("EmailIdx");
    lastNameIndex.setType(IndexType.HASH);

    return lastNameIndex;
}

From source file:example.app.config.gemfire.GemFireConfiguration.java

@Bean
@DependsOn("Contacts")
public IndexFactoryBean lastNameIndex(GemFireCache gemfireCache) {
    IndexFactoryBean lastNameIndex = new IndexFactoryBean();

    lastNameIndex.setCache(gemfireCache);
    lastNameIndex.setExpression("person.lastName");
    lastNameIndex.setFrom("/Contacts");
    lastNameIndex.setName("PersonLastNameIdx");
    lastNameIndex.setType(IndexType.HASH);

    return lastNameIndex;
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test(expected = GemfireIndexException.class)
public void createIndexThrowsIndexNameConflictException() throws Exception {

    Index mockIndex = mockIndexWithDefinition("TestIndex", "purchaseDate", "/Orders", IndexType.HASH);

    when(mockQueryService.getIndexes()).thenReturn(Collections.singletonList(mockIndex));

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).when(indexFactoryBean)
            .createFunctionalIndex(eq(mockQueryService), anyString(), anyString(), anyString(), any());

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setType(IndexType.FUNCTIONAL);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isFalse();
    assertThat(indexFactoryBean.isOverride()).isFalse();

    try {//from  w  w  w .  j  a v  a 2 s. c  om
        indexFactoryBean.createIndex(mockQueryService, "TestIndex");
    } catch (GemfireIndexException expected) {

        String existingIndexDefinition = String.format(IndexFactoryBean.DETAILED_INDEX_DEFINITION,
                mockIndex.getName(), mockIndex.getIndexedExpression(), mockIndex.getFromClause(), "unknown",
                IndexType.valueOf(mockIndex.getType()));

        assertThat(expected).hasMessageStartingWith(String.format(
                "An Index with the same name [TestIndex] having possibly a different definition already exists;"
                        + " you may choose to ignore this Index definition [%1$s] and use the existing Index"
                        + " definition [%2$s] by setting the 'ignoreIfExists' property to 'true'",
                indexFactoryBean.toDetailedIndexDefinition(), existingIndexDefinition));

        assertThat(expected).hasCauseInstanceOf(IndexNameConflictException.class);
        assertThat(expected.getCause()).hasMessage("TEST");
        assertThat(expected.getCause()).hasNoCause();

        throw expected;
    } finally {
        verify(indexFactoryBean, times(1)).createFunctionalIndex(eq(mockQueryService), eq("TestIndex"),
                eq("id"), eq("/Example"), eq(null));

        verifyZeroInteractions(mockLog);

        verify(mockQueryService, times(1)).getIndexes();
    }
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
public void createIndexThrowsIndexNameConflictExceptionAndOverridesExistingIndexWithWarning() throws Exception {

    Index mockIndex = mockIndexWithDefinition("TestIndex", "price", "/Orders", IndexType.FUNCTIONAL);

    Index testIndex = mockIndexWithDefinition("TestIndex", "purchaseDate", "/Orders", IndexType.HASH);

    assertThat(mockIndex).isNotSameAs(testIndex);

    when(mockLog.isWarnEnabled()).thenReturn(true);

    when(mockQueryService.getIndexes()).thenReturn(Collections.singletonList(mockIndex));

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).doReturn(testIndex).when(indexFactoryBean)
            .createHashIndex(any(QueryService.class), anyString(), anyString(), anyString(), any());

    indexFactoryBean.setExpression("purchaseDate");
    indexFactoryBean.setFrom("/Orders");
    indexFactoryBean.setIgnoreIfExists(false);
    indexFactoryBean.setName("TestIndex");
    indexFactoryBean.setOverride(true);//from w ww .  jav a2  s  .c  o m
    indexFactoryBean.setType(IndexType.HASH);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isFalse();
    assertThat(indexFactoryBean.isOverride()).isTrue();
    assertThat(indexFactoryBean.createIndex(mockQueryService, "TestIndex")).isEqualTo(testIndex);

    String existingIndexDefinition = String.format(IndexFactoryBean.BASIC_INDEX_DEFINITION,
            mockIndex.getIndexedExpression(), mockIndex.getFromClause(),
            IndexType.valueOf(mockIndex.getType()));

    verify(indexFactoryBean, times(2)).createHashIndex(eq(mockQueryService), eq("TestIndex"),
            eq("purchaseDate"), eq("/Orders"), eq(null));

    verify(mockLog, times(1)).warn(eq(String.format(
            "WARNING! Overriding existing Index [TestIndex] having a definition [%1$s] that does not match"
                    + " the Index defined [%2$s] by this IndexFactoryBean [TestIndex]",
            existingIndexDefinition, indexFactoryBean.toBasicIndexDefinition())));

    verify(mockQueryService, times(1)).getIndexes();
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test(expected = GemfireIndexException.class)
public void createIndexThrowsIndexNameConflictExceptionAndOverrideThrowsRuntimeException() throws Exception {

    Index mockIndex = mockIndexWithDefinition("MockIndex", "purchaseDate", "/Example", IndexType.HASH);

    when(mockLog.isWarnEnabled()).thenReturn(true);

    when(mockQueryService.getIndexes()).thenReturn(Collections.singletonList(mockIndex))
            .thenReturn(Collections.emptyList());

    IndexFactoryBean indexFactoryBean = newIndexFactoryBean();

    doThrow(new IndexNameConflictException("TEST")).doThrow(new RuntimeException("RETRY"))
            .when(indexFactoryBean)//from ww w.java2 s .c  om
            .createKeyIndex(any(QueryService.class), anyString(), anyString(), anyString());

    indexFactoryBean.setExpression("id");
    indexFactoryBean.setFrom("/Example");
    indexFactoryBean.setIgnoreIfExists(false);
    indexFactoryBean.setName("MockIndex");
    indexFactoryBean.setOverride(true);
    indexFactoryBean.setType(IndexType.PRIMARY_KEY);

    assertThat(indexFactoryBean.isIgnoreIfExists()).isFalse();
    assertThat(indexFactoryBean.isOverride()).isTrue();

    try {
        indexFactoryBean.createIndex(mockQueryService, "MockIndex");
    } catch (GemfireIndexException expected) {

        assertThat(expected).hasMessageStartingWith(
                "Attempt to 'override' existing Index [MockIndex] with the Index that would be created by this"
                        + " IndexFactoryBean [MockIndex] failed; you should verify the state of your system"
                        + " and make sure the previously existing Index [MockIndex] still exits");

        assertThat(expected).hasCauseInstanceOf(GemfireIndexException.class);
        assertThat(expected.getCause()).hasMessageStartingWith(
                String.format("Failed to create Index [%s]", indexFactoryBean.toDetailedIndexDefinition()));
        assertThat(expected.getCause()).hasCauseInstanceOf(RuntimeException.class);
        assertThat(expected.getCause().getCause()).hasMessageStartingWith("RETRY");
        assertThat(expected.getCause().getCause()).hasNoCause();

        throw expected;
    } finally {

        String existingIndexDefinition = String.format(IndexFactoryBean.BASIC_INDEX_DEFINITION,
                mockIndex.getIndexedExpression(), mockIndex.getFromClause(),
                IndexType.valueOf(mockIndex.getType()));

        verify(indexFactoryBean, times(2)).createKeyIndex(eq(mockQueryService), eq("MockIndex"), eq("id"),
                eq("/Example"));

        verify(mockLog, times(1)).warn(eq(String.format(
                "WARNING! Overriding existing Index [MockIndex] having a definition [%1$s] that does not match"
                        + " the Index defined [%2$s] by this IndexFactoryBean [MockIndex]",
                existingIndexDefinition, indexFactoryBean.toBasicIndexDefinition())));

        verify(mockQueryService, times(1)).getIndexes();
    }
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
public void tryToFindExistingIndexByDefinitionReturnsIndex() {

    Index mockIndexOne = mockIndexWithDefinition("PrimaryIndex", "id", "/People", IndexType.HASH);

    Index mockIndexTwo = mockIndexWithDefinition("SecondaryIndex", "id", "/People", IndexType.PRIMARY_KEY);

    Index mockIndexThree = mockIndexWithDefinition("TernaryIndex", "purchaseDate", "/Orders",
            IndexType.FUNCTIONAL);/*ww w  . j a  v  a  2s . c  o m*/

    when(mockQueryService.getIndexes()).thenReturn(Arrays.asList(mockIndexOne, mockIndexTwo, mockIndexThree));

    assertThat(indexFactoryBean
            .tryToFindExistingIndexByDefinition(mockQueryService, "id", "/People", IndexType.PRIMARY_KEY)
            .orElse(null)).isSameAs(mockIndexTwo);

    assertThat(indexFactoryBean.tryToFindExistingIndexByDefinition(mockQueryService, "purchaseDate", "/Orders",
            IndexType.FUNCTIONAL).orElse(null)).isSameAs(mockIndexThree);

    assertThat(indexFactoryBean
            .tryToFindExistingIndexByDefinition(mockQueryService, "id", "/People", IndexType.HASH).orElse(null))
                    .isSameAs(mockIndexOne);

    verify(mockQueryService, times(3)).getIndexes();
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
public void tryToFindExistingIndexByDefinitionReturnsNull() {

    Index mockIndex = mockIndexWithDefinition("PrimaryIndex", "id", "/People", IndexType.HASH);

    when(mockQueryService.getIndexes()).thenReturn(Collections.singletonList(mockIndex));

    assertThat(indexFactoryBean/*from   w  ww .j  av a2 s.co  m*/
            .tryToFindExistingIndexByDefinition(mockQueryService, "key", "/People", IndexType.HASH)
            .orElse(null)).isNull();

    assertThat(indexFactoryBean
            .tryToFindExistingIndexByDefinition(mockQueryService, "id", "/Persons", IndexType.HASH)
            .orElse(null)).isNull();

    assertThat(indexFactoryBean
            .tryToFindExistingIndexByDefinition(mockQueryService, "id", "/People", IndexType.KEY).orElse(null))
                    .isNull();

    verify(mockQueryService, times(3)).getIndexes();
}

From source file:org.springframework.data.gemfire.IndexFactoryBeanTest.java

@Test
@SuppressWarnings("deprecation")
public void indexWrapperDelegation() {

    Index mockIndex = mockIndex("testIndexWrapperDelegation.MockIndex");

    IndexStatistics mockIndexStats = mock(IndexStatistics.class, "testIndexWrapperDelegation.MockIndexStats");

    QueryService mockQueryService = mock(QueryService.class, "testIndexWrapperDelegation.MockQueryService");

    when(mockIndex.getCanonicalizedFromClause()).thenReturn("/Example");
    when(mockIndex.getCanonicalizedIndexedExpression()).thenReturn("ID");
    when(mockIndex.getCanonicalizedProjectionAttributes()).thenReturn("identifier");
    when(mockIndex.getFromClause()).thenReturn("Example");
    when(mockIndex.getIndexedExpression()).thenReturn("id");
    when(mockIndex.getName()).thenReturn("MockIndex");
    when(mockIndex.getProjectionAttributes()).thenReturn("id");
    when(mockIndex.getStatistics()).thenReturn(mockIndexStats);
    when(mockIndex.getType()).thenReturn(org.apache.geode.cache.query.IndexType.HASH);
    when(mockQueryService.getIndexes()).thenReturn(Collections.singletonList(mockIndex));

    IndexFactoryBean.IndexWrapper indexWrapper = new IndexFactoryBean.IndexWrapper(mockQueryService,
            "MockIndex");

    assertThat(indexWrapper).isNotNull();
    assertThat(indexWrapper.getIndexName()).isEqualTo("MockIndex");
    assertThat(indexWrapper.getQueryService()).isEqualTo(mockQueryService);

    Index actualIndex = indexWrapper.resolveIndex();

    assertThat(actualIndex).isSameAs(mockIndex);

    assertThat(indexWrapper.getCanonicalizedFromClause()).isEqualTo("/Example");
    assertThat(indexWrapper.getCanonicalizedIndexedExpression()).isEqualTo("ID");
    assertThat(indexWrapper.getCanonicalizedProjectionAttributes()).isEqualTo("identifier");
    assertThat(indexWrapper.getFromClause()).isEqualTo("Example");
    assertThat(indexWrapper.getIndexedExpression()).isEqualTo("id");
    assertThat(indexWrapper.getName()).isEqualTo("MockIndex");
    assertThat(indexWrapper.getProjectionAttributes()).isEqualTo("id");
    assertThat(indexWrapper.getStatistics()).isSameAs(mockIndexStats);
    assertThat(indexWrapper.getType()).isEqualTo(org.apache.geode.cache.query.IndexType.HASH);

    Index sameIndex = indexWrapper.resolveIndex();

    assertThat(sameIndex).isSameAs(actualIndex);
}