Search in sources :

Example 1 with InternalCache

use of org.apache.geode.internal.cache.InternalCache in project geode by apache.

the class LuceneIndexCommands method createIndex.

@CliCommand(value = LuceneCliStrings.LUCENE_CREATE_INDEX, help = LuceneCliStrings.LUCENE_CREATE_INDEX__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public // TODO : Add optionContext for indexName
Result createIndex(@CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, mandatory = true, help = LuceneCliStrings.LUCENE_CREATE_INDEX__NAME__HELP) final String indexName, @CliOption(key = LuceneCliStrings.LUCENE__REGION_PATH, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = LuceneCliStrings.LUCENE_CREATE_INDEX__REGION_HELP) final String regionPath, @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, mandatory = true, help = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD_HELP) final String[] fields, @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, help = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER_HELP) final String[] analyzers) {
    Result result;
    XmlEntity xmlEntity = null;
    this.securityService.authorizeRegionManage(regionPath);
    try {
        final InternalCache cache = getCache();
        // trim fields for any leading trailing spaces.
        String[] trimmedFields = Arrays.stream(fields).map(field -> field.trim()).toArray(size -> new String[size]);
        LuceneIndexInfo indexInfo = new LuceneIndexInfo(indexName, regionPath, trimmedFields, analyzers);
        final ResultCollector<?, ?> rc = this.executeFunctionOnAllMembers(createIndexFunction, indexInfo);
        final List<CliFunctionResult> funcResults = (List<CliFunctionResult>) rc.getResult();
        final TabularResultData tabularResult = ResultBuilder.createTabularResultData();
        for (final CliFunctionResult cliFunctionResult : funcResults) {
            tabularResult.accumulate("Member", cliFunctionResult.getMemberIdOrName());
            if (cliFunctionResult.isSuccessful()) {
                tabularResult.accumulate("Status", "Successfully created lucene index");
            // if (xmlEntity == null) {
            // xmlEntity = cliFunctionResult.getXmlEntity();
            // }
            } else {
                tabularResult.accumulate("Status", "Failed: " + cliFunctionResult.getMessage());
            }
        }
        result = ResultBuilder.buildResult(tabularResult);
    } catch (IllegalArgumentException iae) {
        LogWrapper.getInstance().info(iae.getMessage());
        result = ResultBuilder.createUserErrorResult(iae.getMessage());
    } catch (CommandResultException crex) {
        result = crex.getResult();
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    return result;
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) StringUtils(org.apache.commons.lang.StringUtils) CliAvailabilityIndicator(org.springframework.shell.core.annotation.CliAvailabilityIndicator) Arrays(java.util.Arrays) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Execution(org.apache.geode.cache.execute.Execution) CliMetaData(org.apache.geode.management.cli.CliMetaData) Function(org.apache.geode.cache.execute.Function) CliOption(org.springframework.shell.core.annotation.CliOption) ArrayList(java.util.ArrayList) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) LuceneDescribeIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction) CliStrings(org.apache.geode.management.internal.cli.i18n.CliStrings) Region(org.apache.geode.cache.Region) LuceneSearchIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneSearchIndexFunction) SystemFailure(org.apache.geode.SystemFailure) IntegratedSecurityService(org.apache.geode.internal.security.IntegratedSecurityService) Result(org.apache.geode.management.cli.Result) DistributedMember(org.apache.geode.distributed.DistributedMember) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LinkedHashSet(java.util.LinkedHashSet) CliUtil(org.apache.geode.management.internal.cli.CliUtil) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) CliCommand(org.springframework.shell.core.annotation.CliCommand) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) GfshCommand(org.apache.geode.management.internal.cli.commands.GfshCommand) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) SecurityService(org.apache.geode.internal.security.SecurityService) Set(java.util.Set) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Collectors(java.util.stream.Collectors) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) List(java.util.List) LogWrapper(org.apache.geode.management.internal.cli.LogWrapper) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation) LuceneCreateIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneCreateIndexFunction) LuceneDestroyIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction) Resource(org.apache.geode.security.ResourcePermission.Resource) ConverterHint(org.apache.geode.management.cli.ConverterHint) Operation(org.apache.geode.security.ResourcePermission.Operation) ResultBuilder(org.apache.geode.management.internal.cli.result.ResultBuilder) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ArrayList(java.util.ArrayList) List(java.util.List) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 2 with InternalCache

use of org.apache.geode.internal.cache.InternalCache in project geode by apache.

the class LuceneIndexCommands method executeDestroyIndexFunction.

private XmlEntity executeDestroyIndexFunction(List<CliFunctionResult> accumulatedResults, String indexName, String regionPath) {
    // Destroy has three cases:
    //
    // - no members define the region
    // In this case, send the request to all members to handle the case where the index has been
    // created, but not the region
    //
    // - all members define the region
    // In this case, send the request to one of the region members to destroy the index on all
    // member
    //
    // - some members define the region; some don't
    // In this case, send the request to one of the region members to destroy the index in all the
    // region members. Then send the function to the remaining members to handle the case where
    // the index has been created, but not the region
    XmlEntity xmlEntity = null;
    InternalCache cache = getCache();
    Set<DistributedMember> regionMembers = getRegionMembers(cache, regionPath);
    Set<DistributedMember> normalMembers = getNormalMembers(cache);
    LuceneDestroyIndexInfo indexInfo = new LuceneDestroyIndexInfo(indexName, regionPath);
    ResultCollector<?, ?> rc;
    if (regionMembers.isEmpty()) {
        // Attempt to destroy the proxy index on all members
        indexInfo.setDefinedDestroyOnly(true);
        rc = executeFunction(destroyIndexFunction, indexInfo, normalMembers);
        accumulatedResults.addAll((List<CliFunctionResult>) rc.getResult());
    } else {
        // Attempt to destroy the index on a region member
        indexInfo.setDefinedDestroyOnly(false);
        Set<DistributedMember> singleMember = new HashSet<>();
        singleMember.add(regionMembers.iterator().next());
        rc = executeFunction(destroyIndexFunction, indexInfo, singleMember);
        List<CliFunctionResult> cliFunctionResults = (List<CliFunctionResult>) rc.getResult();
        CliFunctionResult cliFunctionResult = cliFunctionResults.get(0);
        xmlEntity = cliFunctionResult.getXmlEntity();
        for (DistributedMember regionMember : regionMembers) {
            accumulatedResults.add(new CliFunctionResult(regionMember.getId(), cliFunctionResult.isSuccessful(), cliFunctionResult.getMessage()));
        }
        // If that succeeds, destroy the proxy index(es) on all other members if necessary
        if (cliFunctionResult.isSuccessful()) {
            normalMembers.removeAll(regionMembers);
            if (!normalMembers.isEmpty()) {
                indexInfo.setDefinedDestroyOnly(true);
                rc = executeFunction(destroyIndexFunction, indexInfo, normalMembers);
                accumulatedResults.addAll((List<CliFunctionResult>) rc.getResult());
            }
        } else {
        // @todo Should dummy results be added to the accumulatedResults for the non-region
        // members in the failed case
        }
    }
    return xmlEntity;
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with InternalCache

use of org.apache.geode.internal.cache.InternalCache in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testSearchIndexWithPaging.

@Ignore
public void testSearchIndexWithPaging() throws Exception {
    final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
    final Gfsh mockGfsh = mock(Gfsh.class);
    final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
    final LuceneIndexCommands commands = spy(createIndexCommands(mockCache, null));
    ArgumentCaptor<String> resultCaptor = ArgumentCaptor.forClass(String.class);
    LuceneSearchResults result1 = createQueryResults("A", "Result1", Float.valueOf("1.7"));
    LuceneSearchResults result2 = createQueryResults("B", "Result1", Float.valueOf("1.6"));
    LuceneSearchResults result3 = createQueryResults("C", "Result1", Float.valueOf("1.5"));
    LuceneSearchResults result4 = createQueryResults("D", "Result1", Float.valueOf("1.4"));
    LuceneSearchResults result5 = createQueryResults("E", "Result1", Float.valueOf("1.3"));
    LuceneSearchResults result6 = createQueryResults("F", "Result1", Float.valueOf("1.2"));
    LuceneSearchResults result7 = createQueryResults("G", "Result1", Float.valueOf("1.1"));
    final List<Set<LuceneSearchResults>> queryResultsList = getSearchResults(result1, result2, result3, result4, result5, result6, result7);
    doReturn(mockResultCollector).when(commands).executeSearch(any(LuceneQueryInfo.class));
    doReturn(queryResultsList).when(mockResultCollector).getResult();
    doReturn(mockGfsh).when(commands).initGfsh();
    when(mockGfsh.interact(anyString())).thenReturn("n").thenReturn("n").thenReturn("n").thenReturn("n").thenReturn("p").thenReturn("p").thenReturn("p").thenReturn("p").thenReturn("p").thenReturn("n").thenReturn("q");
    LuceneSearchResults[] expectedResults = new LuceneSearchResults[] { result7, result6, result5, result4, result3, result2, result1 };
    String expectedPage1 = getPage(expectedResults, new int[] { 0, 1 });
    String expectedPage2 = getPage(expectedResults, new int[] { 2, 3 });
    String expectedPage3 = getPage(expectedResults, new int[] { 4, 5 });
    String expectedPage4 = getPage(expectedResults, new int[] { 6 });
    commands.searchIndex("index", "region", "Result1", "field1", -1, false);
    verify(mockGfsh, times(20)).printAsInfo(resultCaptor.capture());
    List<String> actualPageResults = resultCaptor.getAllValues();
    assertEquals(expectedPage1, actualPageResults.get(0));
    assertEquals("\t\tPage 1 of 4", actualPageResults.get(1));
    assertEquals(expectedPage2, actualPageResults.get(2));
    assertEquals("\t\tPage 2 of 4", actualPageResults.get(3));
    assertEquals(expectedPage3, actualPageResults.get(4));
    assertEquals("\t\tPage 3 of 4", actualPageResults.get(5));
    assertEquals(expectedPage4, actualPageResults.get(6));
    assertEquals("\t\tPage 4 of 4", actualPageResults.get(7));
    assertEquals("No more results to display.", actualPageResults.get(8));
    assertEquals(expectedPage4, actualPageResults.get(9));
    assertEquals("\t\tPage 4 of 4", actualPageResults.get(10));
    assertEquals(expectedPage3, actualPageResults.get(11));
    assertEquals("\t\tPage 3 of 4", actualPageResults.get(12));
    assertEquals(expectedPage2, actualPageResults.get(13));
    assertEquals("\t\tPage 2 of 4", actualPageResults.get(14));
    assertEquals(expectedPage1, actualPageResults.get(15));
    assertEquals("\t\tPage 1 of 4", actualPageResults.get(16));
    assertEquals("At the top of the search results.", actualPageResults.get(17));
    assertEquals(expectedPage1, actualPageResults.get(18));
    assertEquals("\t\tPage 1 of 4", actualPageResults.get(19));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) InternalCache(org.apache.geode.internal.cache.InternalCache) Mockito.anyString(org.mockito.Mockito.anyString) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Ignore(org.junit.Ignore)

Example 4 with InternalCache

use of org.apache.geode.internal.cache.InternalCache in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testSearchIndex.

@Test
public void testSearchIndex() throws Exception {
    final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
    final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
    final LuceneIndexCommands commands = spy(createIndexCommands(mockCache, null));
    final List<Set<LuceneSearchResults>> queryResultsList = new ArrayList<>();
    HashSet<LuceneSearchResults> queryResults = new HashSet<>();
    queryResults.add(createQueryResults("A", "Result1", Float.valueOf("1.3")));
    queryResults.add(createQueryResults("B", "Result1", Float.valueOf("1.2")));
    queryResults.add(createQueryResults("C", "Result1", Float.valueOf("1.1")));
    queryResultsList.add(queryResults);
    doReturn(mockResultCollector).when(commands).executeSearch(isA(LuceneQueryInfo.class));
    doReturn(queryResultsList).when(mockResultCollector).getResult();
    CommandResult result = (CommandResult) commands.searchIndex("index", "region", "Result1", "field1", -1, false);
    TabularResultData data = (TabularResultData) result.getResultData();
    assertEquals(Arrays.asList("C", "B", "A"), data.retrieveAllValues("key"));
    assertEquals(Arrays.asList("Result1", "Result1", "Result1"), data.retrieveAllValues("value"));
    assertEquals(Arrays.asList("1.1", "1.2", "1.3"), data.retrieveAllValues("score"));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 5 with InternalCache

use of org.apache.geode.internal.cache.InternalCache in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testListIndexWithoutStats.

@Test
public void testListIndexWithoutStats() {
    final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
    final String serverName = "mockServer";
    final AbstractExecution mockFunctionExecutor = mock(AbstractExecution.class, "Function Executor");
    final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
    String[] searchableFields = { "field1", "field2", "field3" };
    Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
    fieldAnalyzers.put("field1", new StandardAnalyzer());
    fieldAnalyzers.put("field2", new KeywordAnalyzer());
    fieldAnalyzers.put("field3", null);
    final LuceneIndexDetails indexDetails1 = createIndexDetails("memberFive", "/Employees", searchableFields, fieldAnalyzers, true, serverName);
    final LuceneIndexDetails indexDetails2 = createIndexDetails("memberSix", "/Employees", searchableFields, fieldAnalyzers, false, serverName);
    final LuceneIndexDetails indexDetails3 = createIndexDetails("memberTen", "/Employees", searchableFields, fieldAnalyzers, true, serverName);
    final List<Set<LuceneIndexDetails>> results = new ArrayList<>();
    results.add(CollectionUtils.asSet(indexDetails2, indexDetails1, indexDetails3));
    when(mockFunctionExecutor.execute(isA(LuceneListIndexFunction.class))).thenReturn(mockResultCollector);
    when(mockResultCollector.getResult()).thenReturn(results);
    final LuceneIndexCommands commands = createIndexCommands(mockCache, mockFunctionExecutor);
    CommandResult result = (CommandResult) commands.listIndex(false);
    TabularResultData data = (TabularResultData) result.getResultData();
    assertEquals(Arrays.asList("memberFive", "memberSix", "memberTen"), data.retrieveAllValues("Index Name"));
    assertEquals(Arrays.asList("/Employees", "/Employees", "/Employees"), data.retrieveAllValues("Region Path"));
    assertEquals(Arrays.asList("[field1, field2, field3]", "[field1, field2, field3]", "[field1, field2, field3]"), data.retrieveAllValues("Indexed Fields"));
    assertEquals(Arrays.asList("{field1=StandardAnalyzer, field2=KeywordAnalyzer}", "{field1=StandardAnalyzer, field2=KeywordAnalyzer}", "{field1=StandardAnalyzer, field2=KeywordAnalyzer}"), data.retrieveAllValues("Field Analyzer"));
    assertEquals(Arrays.asList("Initialized", "Defined", "Initialized"), data.retrieveAllValues("Status"));
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) Set(java.util.Set) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) HashMap(java.util.HashMap) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) Mockito.anyString(org.mockito.Mockito.anyString) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultCollector(org.apache.geode.cache.execute.ResultCollector) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

InternalCache (org.apache.geode.internal.cache.InternalCache)267 DistributedMember (org.apache.geode.distributed.DistributedMember)78 Test (org.junit.Test)64 UnitTest (org.apache.geode.test.junit.categories.UnitTest)52 IOException (java.io.IOException)48 ArrayList (java.util.ArrayList)35 HashSet (java.util.HashSet)35 CliMetaData (org.apache.geode.management.cli.CliMetaData)34 CliCommand (org.springframework.shell.core.annotation.CliCommand)34 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)32 Region (org.apache.geode.cache.Region)31 Result (org.apache.geode.management.cli.Result)30 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)30 Expectations (org.jmock.Expectations)30 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)26 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)25 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)24 Set (java.util.Set)23 ResultCollector (org.apache.geode.cache.execute.ResultCollector)22 CommandResultException (org.apache.geode.management.internal.cli.result.CommandResultException)20