use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.
the class QueryIndexDUnitTest method createIndexOnOverFlowRegions.
private static void createIndexOnOverFlowRegions() throws Exception {
Cache cache = basicGetCache();
String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
cache.getLogger().fine("createIndexOnOverFlowRegions()");
QueryService qs = cache.getQueryService();
Region region = null;
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
String indexName = "idIndex" + regionNames[i];
cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
try {
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i1 = qs.createIndex(indexName, IndexType.FUNCTIONAL, "pf.ID", "/" + regionNames[i] + " pf");
}
indexName = "keyIdIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i2 = qs.createIndex(indexName, IndexType.FUNCTIONAL, "key.ID", "/" + regionNames[i] + ".keys key");
}
indexName = "entryIdIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i2 = qs.createIndex(indexName, IndexType.FUNCTIONAL, "entry.value.ID", "/" + regionNames[i] + ".entries entry");
}
indexName = "entryMethodIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i2 = qs.createIndex(indexName, IndexType.FUNCTIONAL, "entry.getValue().getID()", "/" + regionNames[i] + ".entries entry");
}
indexName = "entryMethodWithArgIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i2 = qs.createIndex(indexName, IndexType.FUNCTIONAL, "entry.getValue().boolFunction('active')", "/" + regionNames[i] + ".entries entry");
}
} catch (IndexNameConflictException ice) {
// Ignore. The pr may have created the index through
// remote index create message from peer.
}
}
}
use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.
the class QueryIndexDUnitTest method createIndexOnOverflowRegionsWithMultipleWhereClauseAndValidateResults.
@Test
public void createIndexOnOverflowRegionsWithMultipleWhereClauseAndValidateResults() throws Exception {
Host host = Host.getHost(0);
VM[] vms = new VM[] { host.getVM(0), host.getVM(1) };
// Create and load regions on all vms.
for (int i = 0; i < vms.length; i++) {
int finalI = i;
vms[i].invoke(() -> QueryIndexDUnitTest.createAndLoadOverFlowRegions("testOfValid2" + "vm" + finalI, new Boolean(true), new Boolean(false)));
}
vms[0].invoke(new CacheSerializableRunnable("Execute query validate results") {
public void run2() throws CacheException {
Cache cache = basicGetCache();
String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
QueryService qs = cache.getQueryService();
Region region = null;
int numObjects = 10;
// The index should get updated accordingly.
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
for (int cnt = 1; cnt < numObjects; cnt++) {
region.put(new Portfolio(cnt), new String("XX" + cnt));
}
}
String[] qString = new String[] { "SELECT * FROM /REGION_NAME pf WHERE pf = 'XX1'", "SELECT * FROM /REGION_NAME pf WHERE pf IN SET( 'XX5', 'XX6', 'XX7')", "SELECT * FROM /REGION_NAME.values pf WHERE pf IN SET( 'XX5', 'XX6', 'XX7')", "SELECT * FROM /REGION_NAME.keys k WHERE k.ID = 1", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT ID, status FROM /REGION_NAME.keys WHERE ID = 1", "SELECT k.ID, k.status FROM /REGION_NAME.keys k WHERE k.ID = 1 and k.status = 'active'", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID > 5", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID > 5 and key.status = 'active'" };
// Execute Query without index.
SelectResults[] srWithoutIndex = new SelectResults[qString.length * regionNames.length];
String[] queryString = new String[qString.length * regionNames.length];
int r = 0;
try {
for (int q = 0; q < qString.length; q++) {
for (int i = 0; i < regionNames.length; i++) {
String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
Query query = qs.newQuery(queryStr);
queryString[r] = queryStr;
srWithoutIndex[r] = (SelectResults) query.execute();
r++;
}
}
} catch (Exception ex) {
logger.info("Failed to Execute query", ex);
fail("Failed to Execute query.");
}
// Create index.
String indexName = "";
try {
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
indexName = "idIndex" + regionNames[i];
cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
try {
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + " pf");
}
indexName = "valueIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + ".values pf");
}
indexName = "keyIdIndex" + regionNames[i];
if (qs.getIndex(region, indexName) == null) {
cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
Index i2 = qs.createIndex(indexName, "key.ID", "/" + regionNames[i] + ".keys key");
}
indexName = "keyIdIndex2" + regionNames[i];
} catch (IndexNameConflictException ice) {
// Ignore. The pr may have created the index through
// remote index create message from peer.
}
}
} catch (Exception ex) {
logger.info("Failed to create index", ex);
fail("Failed to create index." + indexName);
}
// Execute Query with index.
SelectResults[] srWithIndex = new SelectResults[qString.length * regionNames.length];
try {
r = 0;
for (int q = 0; q < qString.length; q++) {
for (int i = 0; i < regionNames.length; i++) {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
Query query = qs.newQuery(queryStr);
srWithIndex[r++] = (SelectResults) query.execute();
if (!observer.isIndexesUsed) {
fail("Index not used for query. " + queryStr);
}
}
}
} catch (Exception ex) {
logger.info("Failed to Execute query", ex);
fail("Failed to Execute query.");
}
// Compare results with and without index.
StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
SelectResults[][] sr = new SelectResults[1][2];
for (int i = 0; i < srWithIndex.length; i++) {
sr[0][0] = srWithoutIndex[i];
sr[0][1] = srWithIndex[i];
logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
}
// The index should get updated accordingly.
for (int i = 0; i < regionNames.length; i++) {
region = cache.getRegion(regionNames[i]);
for (int cnt = 1; cnt < numObjects; cnt++) {
if (cnt % 2 == 0) {
region.destroy(new Portfolio(cnt));
}
}
for (int cnt = 10; cnt < numObjects; cnt++) {
if (cnt % 2 == 0) {
region.put(new Portfolio(cnt), new String("XX" + cnt));
}
}
}
// Execute Query with index.
srWithIndex = new SelectResults[qString.length * regionNames.length];
try {
r = 0;
for (int q = 0; q < qString.length; q++) {
for (int i = 0; i < regionNames.length; i++) {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
Query query = qs.newQuery(queryStr);
srWithIndex[r++] = (SelectResults) query.execute();
if (!observer.isIndexesUsed) {
fail("Index not used for query. " + queryStr);
}
}
}
} catch (Exception ex) {
logger.info("Failed to Execute query", ex);
fail("Failed to Execute query.");
}
}
});
}
use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.
the class OrderByPartitionedDUnitTest method createIndex.
private void createIndex(VM vm, final String indexName, IndexType indexType, final String indexedExpression, final String fromClause) {
int indxTypeCode = -1;
if (indexType.equals(IndexType.FUNCTIONAL)) {
indxTypeCode = 0;
} else if (indexType.equals(IndexType.PRIMARY_KEY)) {
indxTypeCode = 1;
} else if (indexType.equals(IndexType.HASH)) {
indxTypeCode = 2;
}
final int finalIndxTypeCode = indxTypeCode;
vm.invoke(new SerializableRunnable("create index") {
public void run() {
try {
Cache cache = getCache();
IndexType indxType = null;
if (finalIndxTypeCode == 0) {
indxType = IndexType.FUNCTIONAL;
} else if (finalIndxTypeCode == 1) {
indxType = IndexType.PRIMARY_KEY;
} else if (finalIndxTypeCode == 2) {
indxType = IndexType.HASH;
}
cache.getQueryService().createIndex(indexName, indxType, indexedExpression, fromClause);
} catch (RegionNotFoundException e) {
fail(e.toString());
} catch (IndexExistsException e) {
fail(e.toString());
} catch (IndexNameConflictException e) {
fail(e.toString());
}
}
});
}
use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.
the class QueryIndexDUnitTest method createIndex.
private static void createIndex() {
Region region = basicGetCache().getRegion("portfolios");
if (region == null) {
logger.info("The region is not created properly");
} else {
if (!region.isDestroyed()) {
logger.info("Obtained the region with name :" + "portfolios");
QueryService qs = basicGetCache().getQueryService();
if (qs != null) {
try {
qs.createIndex("statusIndex", "status", "/portfolios");
logger.info("Index statusIndex Created successfully");
} catch (IndexNameConflictException e) {
Assert.fail("Caught IndexNameConflictException", e);
} catch (IndexExistsException e) {
Assert.fail("Caught IndexExistsException", e);
} catch (QueryException e) {
Assert.fail("Caught exception while trying to create index", e);
}
} else {
fail("Could not obtain QueryService for the cache ");
}
} else {
fail("Region.isDestroyed() returned true for region : " + "/portfolios");
}
}
}
use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.
the class PartitionedIndex method verifyAndCreateMissingIndex.
/**
* Verify if the index is available of the buckets. If not create index on the bucket.
*/
public void verifyAndCreateMissingIndex(List buckets) throws QueryInvocationTargetException {
PartitionedRegion pr = (PartitionedRegion) this.getRegion();
PartitionedRegionDataStore prds = pr.getDataStore();
for (Object bId : buckets) {
// create index
BucketRegion bukRegion = (BucketRegion) prds.getLocalBucketById((Integer) bId);
if (bukRegion == null) {
throw new QueryInvocationTargetException("Bucket not found for the id :" + bId);
}
IndexManager im = IndexUtils.getIndexManager(bukRegion, true);
if (im != null && im.getIndex(indexName) == null) {
try {
if (pr.getCache().getLogger().fineEnabled()) {
pr.getCache().getLogger().fine("Verifying index presence on bucket region. " + " Found index " + this.indexName + " not present on the bucket region " + bukRegion.getFullPath() + ", index will be created on this region.");
}
ExecutionContext externalContext = new ExecutionContext(null, bukRegion.getCache());
externalContext.setBucketRegion(pr, bukRegion);
im.createIndex(this.indexName, this.type, this.originalIndexedExpression, this.fromClause, this.imports, externalContext, this, true);
} catch (IndexExistsException iee) {
// Index exists.
} catch (IndexNameConflictException ince) {
// ignore.
}
}
}
}
Aggregations