use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class MultiIndexCreationJUnitTest method testMultiIndexCreationOnFailure.
@Test
public void testMultiIndexCreationOnFailure() throws Exception {
Region r = CacheUtils.getRegion(regionName);
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("IDIndex1", "ID", r.getFullPath());
qs.defineIndex("IDIndex2", "ID", r.getFullPath());
List<Index> indexes = null;
try {
indexes = qs.createDefinedIndexes();
fail("Exception should have been thrown");
} catch (MultiIndexCreationException me) {
assertTrue("IndexExistsException should have been thrown ", me.getExceptionsMap().values().iterator().next() instanceof IndexExistsException);
}
assertNull("Index should not have been returned", indexes);
assertEquals("1 index should have been created.", 1, qs.getIndexes().size());
Index ind = qs.getIndexes().iterator().next();
assertNotNull("Index should not be null.", ind);
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
}
use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class CreateDefinedIndexesFunction method execute.
@Override
public void execute(FunctionContext context) {
String memberId = null;
List<Index> indexes = null;
Cache cache = null;
try {
cache = CacheFactory.getAnyInstance();
memberId = cache.getDistributedSystem().getDistributedMember().getId();
QueryService queryService = cache.getQueryService();
Set<IndexInfo> indexDefinitions = (Set<IndexInfo>) context.getArguments();
for (IndexInfo indexDefinition : indexDefinitions) {
String indexName = indexDefinition.getIndexName();
String indexedExpression = indexDefinition.getIndexedExpression();
String regionPath = indexDefinition.getRegionPath();
if (indexDefinition.getIndexType() == IndexInfo.KEY_INDEX) {
queryService.defineKeyIndex(indexName, indexedExpression, regionPath);
} else if (indexDefinition.getIndexType() == IndexInfo.HASH_INDEX) {
queryService.defineHashIndex(indexName, indexedExpression, regionPath);
} else {
queryService.defineIndex(indexName, indexedExpression, regionPath);
}
}
indexes = queryService.createDefinedIndexes();
context.getResultSender().lastResult(new CliFunctionResult(memberId));
} catch (MultiIndexCreationException e) {
StringBuffer sb = new StringBuffer();
sb.append("Index creation failed for indexes: ").append("\n");
for (Map.Entry<String, Exception> failedIndex : e.getExceptionsMap().entrySet()) {
sb.append(failedIndex.getKey()).append(" : ").append(failedIndex.getValue().getMessage()).append("\n");
}
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, sb.toString()));
} catch (Exception e) {
String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, exceptionMessage));
}
}
use of org.apache.geode.cache.query.MultiIndexCreationException in project geode by apache.
the class RollingUpgrade2DUnitTest method createIndexes.
public static void createIndexes(String regionPath, GemFireCache cache) {
try {
QueryService service = cache.getQueryService();
service.defineIndex("statusIndex", "status", regionPath);
service.defineIndex("IDIndex", "ID", regionPath);
service.defineIndex("secIdIndex", "pos.secId", regionPath + " p, p.positions.values pos");
try {
service.createDefinedIndexes();
fail("Index creation should have failed");
} catch (Exception e) {
Assert.assertTrue("Only MultiIndexCreationException should have been thrown and not " + e.getClass(), e instanceof MultiIndexCreationException);
Assert.assertEquals("3 exceptions should have be present in the exceptionsMap.", 3, ((MultiIndexCreationException) e).getExceptionsMap().values().size());
for (Exception ex : ((MultiIndexCreationException) e).getExceptionsMap().values()) {
Assert.assertTrue("Index creation should have been failed with IndexCreationException ", ex instanceof IndexCreationException);
Assert.assertEquals("Incorrect exception message ", LocalizedStrings.PartitionedRegion_INDEX_CREATION_FAILED_ROLLING_UPGRADE.toLocalizedString(), ((IndexCreationException) ex).getMessage());
}
}
} catch (Exception e) {
throw new Error("Exception ", e);
}
}
Aggregations