Search in sources :

Example 1 with IndexNameConflictException

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.
        }
    }
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Cache(org.apache.geode.cache.Cache)

Example 2 with IndexNameConflictException

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.");
            }
        }
    });
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) CacheException(org.apache.geode.cache.CacheException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with IndexNameConflictException

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());
            }
        }
    });
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) IndexType(org.apache.geode.cache.query.IndexType) Cache(org.apache.geode.cache.Cache)

Example 4 with IndexNameConflictException

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");
        }
    }
}
Also used : QueryException(org.apache.geode.cache.query.QueryException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) Region(org.apache.geode.cache.Region)

Example 5 with IndexNameConflictException

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.
            }
        }
    }
}
Also used : ExecutionContext(org.apache.geode.cache.query.internal.ExecutionContext) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) BucketRegion(org.apache.geode.internal.cache.BucketRegion) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) PartitionedRegionDataStore(org.apache.geode.internal.cache.PartitionedRegionDataStore)

Aggregations

IndexNameConflictException (org.apache.geode.cache.query.IndexNameConflictException)16 IndexExistsException (org.apache.geode.cache.query.IndexExistsException)15 Cache (org.apache.geode.cache.Cache)9 Index (org.apache.geode.cache.query.Index)9 QueryService (org.apache.geode.cache.query.QueryService)8 Region (org.apache.geode.cache.Region)7 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)6 Test (org.junit.Test)6 Query (org.apache.geode.cache.query.Query)5 QueryException (org.apache.geode.cache.query.QueryException)5 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)5 CacheException (org.apache.geode.cache.CacheException)4 SelectResults (org.apache.geode.cache.query.SelectResults)4 Portfolio (org.apache.geode.cache.query.data.Portfolio)4 StructSetOrResultsSet (org.apache.geode.cache.query.functional.StructSetOrResultsSet)4 Host (org.apache.geode.test.dunit.Host)4 VM (org.apache.geode.test.dunit.VM)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3