Search in sources :

Example 1 with ResultCollector

use of org.apache.geode.cache.execute.ResultCollector 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 ResultCollector

use of org.apache.geode.cache.execute.ResultCollector in project geode by apache.

the class LuceneIndexCommands method getIndexListing.

@SuppressWarnings("unchecked")
protected List<LuceneIndexDetails> getIndexListing() {
    final Execution functionExecutor = getMembersFunctionExecutor(getMembers(getCache()));
    if (functionExecutor instanceof AbstractExecution) {
        ((AbstractExecution) functionExecutor).setIgnoreDepartedMembers(true);
    }
    final ResultCollector resultsCollector = functionExecutor.execute(new LuceneListIndexFunction());
    final List<Set<LuceneIndexDetails>> results = (List<Set<LuceneIndexDetails>>) resultsCollector.getResult();
    List<LuceneIndexDetails> sortedResults = results.stream().flatMap(set -> set.stream()).sorted().collect(Collectors.toList());
    LinkedHashSet<LuceneIndexDetails> uniqResults = new LinkedHashSet<>();
    uniqResults.addAll(sortedResults);
    sortedResults.clear();
    sortedResults.addAll(uniqResults);
    return sortedResults;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) Execution(org.apache.geode.cache.execute.Execution) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Example 3 with ResultCollector

use of org.apache.geode.cache.execute.ResultCollector 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 ResultCollector

use of org.apache.geode.cache.execute.ResultCollector 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 ResultCollector

use of org.apache.geode.cache.execute.ResultCollector 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

ResultCollector (org.apache.geode.cache.execute.ResultCollector)235 Execution (org.apache.geode.cache.execute.Execution)164 HashSet (java.util.HashSet)148 ArrayList (java.util.ArrayList)144 FunctionException (org.apache.geode.cache.execute.FunctionException)126 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)122 List (java.util.List)112 Function (org.apache.geode.cache.execute.Function)111 TestFunction (org.apache.geode.internal.cache.functions.TestFunction)106 Test (org.junit.Test)101 IgnoredException (org.apache.geode.test.dunit.IgnoredException)94 Iterator (java.util.Iterator)81 Region (org.apache.geode.cache.Region)77 Set (java.util.Set)69 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)65 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)63 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)56 AttributesFactory (org.apache.geode.cache.AttributesFactory)53 Host (org.apache.geode.test.dunit.Host)53 VM (org.apache.geode.test.dunit.VM)53