Search in sources :

Example 41 with CliFunctionResult

use of org.apache.geode.management.internal.cli.functions.CliFunctionResult in project geode by apache.

the class LuceneDestroyIndexFunction method execute.

public void execute(final FunctionContext context) {
    CliFunctionResult result = null;
    String memberId = getCache().getDistributedSystem().getDistributedMember().getId();
    try {
        LuceneDestroyIndexInfo indexInfo = (LuceneDestroyIndexInfo) context.getArguments();
        String indexName = indexInfo.getIndexName();
        String regionPath = indexInfo.getRegionPath();
        LuceneService service = LuceneServiceProvider.get(getCache());
        if (indexName == null) {
            if (indexInfo.isDefinedDestroyOnly()) {
                ((LuceneServiceImpl) service).destroyDefinedIndexes(regionPath);
                result = new CliFunctionResult(memberId);
            } else {
                service.destroyIndexes(regionPath);
                result = new CliFunctionResult(memberId, getXmlEntity(indexName, regionPath));
            }
        } else {
            if (indexInfo.isDefinedDestroyOnly()) {
                ((LuceneServiceImpl) service).destroyDefinedIndex(indexName, regionPath);
                result = new CliFunctionResult(memberId);
            } else {
                service.destroyIndex(indexName, regionPath);
                result = new CliFunctionResult(memberId, getXmlEntity(indexName, regionPath));
            }
        }
    } catch (Exception e) {
        result = new CliFunctionResult(memberId, e, e.getMessage());
    }
    context.getResultSender().lastResult(result);
}
Also used : CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LuceneServiceImpl(org.apache.geode.cache.lucene.internal.LuceneServiceImpl) LuceneDestroyIndexInfo(org.apache.geode.cache.lucene.internal.cli.LuceneDestroyIndexInfo) LuceneService(org.apache.geode.cache.lucene.LuceneService)

Example 42 with CliFunctionResult

use of org.apache.geode.management.internal.cli.functions.CliFunctionResult in project geode by apache.

the class LuceneCreateIndexFunction method execute.

public void execute(final FunctionContext context) {
    String memberId = null;
    try {
        final LuceneIndexInfo indexInfo = (LuceneIndexInfo) context.getArguments();
        final Cache cache = getCache();
        memberId = cache.getDistributedSystem().getDistributedMember().getId();
        LuceneService service = LuceneServiceProvider.get(cache);
        INDEX_NAME.validateName(indexInfo.getIndexName());
        String[] fields = indexInfo.getSearchableFieldNames();
        String[] analyzerName = indexInfo.getFieldAnalyzers();
        final LuceneIndexFactory indexFactory = service.createIndexFactory();
        if (analyzerName == null || analyzerName.length == 0) {
            for (String field : fields) {
                indexFactory.addField(field);
            }
        } else {
            if (analyzerName.length != fields.length)
                throw new Exception("Mismatch in lengths of fields and analyzers");
            for (int i = 0; i < fields.length; i++) {
                Analyzer analyzer = toAnalyzer(analyzerName[i]);
                indexFactory.addField(fields[i], analyzer);
            }
        }
        REGION_PATH.validateName(indexInfo.getRegionPath());
        indexFactory.create(indexInfo.getIndexName(), indexInfo.getRegionPath());
        // TODO - update cluster configuration by returning a valid XmlEntity
        XmlEntity xmlEntity = null;
        context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
    } catch (Exception e) {
        String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    }
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LuceneIndexInfo(org.apache.geode.cache.lucene.internal.cli.LuceneIndexInfo) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) LuceneIndexFactory(org.apache.geode.cache.lucene.LuceneIndexFactory) Cache(org.apache.geode.cache.Cache) LuceneService(org.apache.geode.cache.lucene.LuceneService)

Example 43 with CliFunctionResult

use of org.apache.geode.management.internal.cli.functions.CliFunctionResult in project geode by apache.

the class LuceneIndexCommandsJUnitTest method createTestLuceneIndexCommandsForDestroyIndex.

private LuceneIndexCommands createTestLuceneIndexCommandsForDestroyIndex() {
    final InternalCache mockCache = mock(InternalCache.class);
    final ResultCollector mockResultCollector = mock(ResultCollector.class);
    final LuceneIndexCommands commands = spy(createIndexCommands(mockCache, null));
    final List<CliFunctionResult> cliFunctionResults = new ArrayList<>();
    cliFunctionResults.add(new CliFunctionResult("member", true, "Index Destroyed"));
    doReturn(mockResultCollector).when(commands).executeFunctionOnRegion(isA(LuceneDestroyIndexFunction.class), any(LuceneIndexInfo.class), eq(false));
    doReturn(cliFunctionResults).when(mockResultCollector).getResult();
    return commands;
}
Also used : CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LuceneDestroyIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Example 44 with CliFunctionResult

use of org.apache.geode.management.internal.cli.functions.CliFunctionResult in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testDestroyAllIndexesWithRegionMembers.

@Test
@Parameters({ "true", "false" })
public void testDestroyAllIndexesWithRegionMembers(boolean expectedToSucceed) throws Exception {
    LuceneIndexCommands commands = createTestLuceneIndexCommandsForDestroyIndex();
    String indexName = null;
    String regionPath = "regionPath";
    Set<DistributedMember> members = new HashSet<>();
    DistributedMember mockMember = mock(DistributedMember.class);
    when(mockMember.getId()).thenReturn("member0");
    members.add(mockMember);
    final ResultCollector mockResultCollector = mock(ResultCollector.class);
    final List<CliFunctionResult> cliFunctionResults = new ArrayList<>();
    String expectedStatus;
    if (expectedToSucceed) {
        expectedStatus = CliStrings.format(LuceneCliStrings.LUCENE_DESTROY_INDEX__MSG__SUCCESSFULLY_DESTROYED_INDEXES_FROM_REGION_0, new Object[] { regionPath });
        cliFunctionResults.add(new CliFunctionResult(mockMember.getId()));
    } else {
        Exception e = new IllegalStateException("failed");
        expectedStatus = e.getMessage();
        cliFunctionResults.add(new CliFunctionResult("member0", e, e.getMessage()));
    }
    doReturn(mockResultCollector).when(commands).executeFunction(isA(LuceneDestroyIndexFunction.class), any(LuceneDestroyIndexInfo.class), any());
    doReturn(cliFunctionResults).when(mockResultCollector).getResult();
    doReturn(Collections.emptySet()).when(commands).getNormalMembers(any());
    doReturn(Collections.emptySet()).when(commands).getRegionMembers(any(), any());
    CommandResult result = (CommandResult) commands.destroyIndex(indexName, regionPath);
    verifyDestroyIndexCommandResult(result, cliFunctionResults, expectedStatus);
}
Also used : LuceneDestroyIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) DistributedMember(org.apache.geode.distributed.DistributedMember) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) Parameters(junitparams.Parameters) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 45 with CliFunctionResult

use of org.apache.geode.management.internal.cli.functions.CliFunctionResult in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testDestroyAllIndexesNoRegionMembers.

@Test
@Parameters({ "true", "false" })
public void testDestroyAllIndexesNoRegionMembers(boolean expectedToSucceed) throws Exception {
    LuceneIndexCommands commands = createTestLuceneIndexCommandsForDestroyIndex();
    String indexName = null;
    String regionPath = "regionPath";
    final ResultCollector mockResultCollector = mock(ResultCollector.class);
    final List<CliFunctionResult> cliFunctionResults = new ArrayList<>();
    String expectedStatus;
    if (expectedToSucceed) {
        expectedStatus = CliStrings.format(LuceneCliStrings.LUCENE_DESTROY_INDEX__MSG__SUCCESSFULLY_DESTROYED_INDEXES_FROM_REGION_0, new Object[] { regionPath });
        cliFunctionResults.add(new CliFunctionResult("member0"));
    } else {
        Exception e = new IllegalStateException("failed");
        expectedStatus = e.getMessage();
        cliFunctionResults.add(new CliFunctionResult("member0", e, e.getMessage()));
    }
    doReturn(mockResultCollector).when(commands).executeFunction(isA(LuceneDestroyIndexFunction.class), any(LuceneDestroyIndexInfo.class), any());
    doReturn(cliFunctionResults).when(mockResultCollector).getResult();
    doReturn(Collections.emptySet()).when(commands).getNormalMembers(any());
    doReturn(Collections.emptySet()).when(commands).getRegionMembers(any(), any());
    CommandResult result = (CommandResult) commands.destroyIndex(indexName, regionPath);
    verifyDestroyIndexCommandResult(result, cliFunctionResults, expectedStatus);
}
Also used : CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LuceneDestroyIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) ResultCollector(org.apache.geode.cache.execute.ResultCollector) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) Parameters(junitparams.Parameters) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)45 DistributedMember (org.apache.geode.distributed.DistributedMember)25 XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)24 CliCommand (org.springframework.shell.core.annotation.CliCommand)22 CliMetaData (org.apache.geode.management.cli.CliMetaData)20 ArrayList (java.util.ArrayList)19 Result (org.apache.geode.management.cli.Result)19 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)17 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)17 List (java.util.List)14 InternalCache (org.apache.geode.internal.cache.InternalCache)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Cache (org.apache.geode.cache.Cache)10 ResultCollector (org.apache.geode.cache.execute.ResultCollector)10 ConverterHint (org.apache.geode.management.cli.ConverterHint)10 HashSet (java.util.HashSet)7 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)7 UnitTest (org.apache.geode.test.junit.categories.UnitTest)7 Test (org.junit.Test)7 Set (java.util.Set)6