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;
}
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;
}
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));
}
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"));
}
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"));
}
Aggregations