Search in sources :

Example 6 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 7 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 8 with IndexNameConflictException

use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.

the class EquiJoinIntegrationTest method testSingleFilterWithSingleEquijoinMultipleFiltersOnSameRegionOnSameIteratorMapping.

@Test
public void testSingleFilterWithSingleEquijoinMultipleFiltersOnSameRegionOnSameIteratorMapping() throws Exception {
    createRegions();
    String[] queries = new String[] { "select * from /region1 c, /region2 s where c.pkid=1 and c.pkid = s.pkid and c.id = 1", "select * from /region1 c, /region2 s where c.id = 1 and c.pkid=1 and s.pkid = c.pkid" };
    for (int i = 0; i < 1000; i++) {
        region1.put(i, new Customer(i, i % 10));
        region2.put(i, new Customer(i, i));
    }
    executeQueriesWithIndexCombinations(queries, new DefaultIndexCreatorCallback(qs) {

        Index secondaryIndex;

        @Override
        public void createIndexForRegion1(int indexTypeId) throws RegionNotFoundException, IndexExistsException, IndexNameConflictException {
            secondaryIndex = qs.createIndex("region1 id", "p.id", "/region1 p");
            super.createIndexForRegion1(indexTypeId);
        }

        @Override
        public void destroyIndexForRegion1(int indexTypeId) {
            qs.removeIndex(secondaryIndex);
            super.destroyIndexForRegion1(indexTypeId);
        }
    }, false);
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with IndexNameConflictException

use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.

the class EquiJoinIntegrationTest method testSingleFilterWithSingleEquijoinLimit.

@Test
public void testSingleFilterWithSingleEquijoinLimit() throws Exception {
    // In this test we are hoping the index being used will properly use the limit while taking into
    // consideration the filters of c.id and c.pkid
    // This test is set up so that if the pkid index is used and limit applied, if id is not taken
    // into consideration until later stages, it will lead to incorrect results (0)
    createRegions();
    String[] queries = new String[] { "select * from /region1 c, /region2 s where c.id = 3 and c.pkid > 2  and c.pkid = s.pkid limit 1" };
    for (int i = 0; i < 1000; i++) {
        region1.put(i, new Customer(i, i % 10));
        region2.put(i, new Customer(i, i));
    }
    executeQueriesWithIndexCombinations(queries, new DefaultIndexCreatorCallback(qs) {

        Index secondaryIndex;

        @Override
        public void createIndexForRegion1(int indexTypeId) throws RegionNotFoundException, IndexExistsException, IndexNameConflictException {
            secondaryIndex = qs.createIndex("region1 id", "p.id", "/region1 p");
            super.createIndexForRegion1(indexTypeId);
        }

        @Override
        public void destroyIndexForRegion1(int indexTypeId) {
            qs.removeIndex(secondaryIndex);
            super.destroyIndexForRegion1(indexTypeId);
        }
    }, true);
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with IndexNameConflictException

use of org.apache.geode.cache.query.IndexNameConflictException in project geode by apache.

the class IndexManager method createIndex.

// @todo need more specific list of exceptions
/**
   * Create an index that can be used when executing queries.
   * 
   * @param indexName the name of this index, used for statistics collection
   * @param indexType the type of index
   * @param origIndexedExpression the expression to index on, a function dependent on region entries
   *        individually.
   * @param origFromClause expression that evaluates to the collection(s) that will be queried over,
   *        must contain one and only one region path.
   * @return the newly created Index
   */
public Index createIndex(String indexName, IndexType indexType, String origIndexedExpression, String origFromClause, String imports, ExecutionContext externalContext, PartitionedIndex prIndex, boolean loadEntries) throws IndexNameConflictException, IndexExistsException, IndexInvalidException {
    if (QueryMonitor.isLowMemory()) {
        throw new IndexInvalidException(LocalizedStrings.IndexCreationMsg_CANCELED_DUE_TO_LOW_MEMORY.toLocalizedString());
    }
    boolean oldReadSerialized = DefaultQuery.getPdxReadSerialized();
    DefaultQuery.setPdxReadSerialized(this.region.getCache(), true);
    TXStateProxy tx = null;
    if (!((InternalCache) this.region.getCache()).isClient()) {
        tx = ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalSuspend();
    }
    try {
        // for now this is the only option
        String projectionAttributes = "*";
        if (getIndex(indexName) != null) {
            throw new IndexNameConflictException(LocalizedStrings.IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
        }
        IndexCreationHelper helper = null;
        boolean isCompactOrHash = false;
        // to recalculate the index key for the entry for comparisons during query.
        if (indexType == IndexType.HASH && isOverFlowRegion()) {
            indexType = IndexType.FUNCTIONAL;
        }
        if (indexType != IndexType.PRIMARY_KEY) {
            helper = new FunctionalIndexCreationHelper(origFromClause, origIndexedExpression, projectionAttributes, imports, (InternalCache) region.getCache(), externalContext, this);
            // Asif: For now support Map index as non compact .expand later
            // The limitation for compact range index also apply to hash index for now
            isCompactOrHash = shouldCreateCompactIndex((FunctionalIndexCreationHelper) helper);
        } else if (indexType == IndexType.PRIMARY_KEY) {
            helper = new PrimaryKeyIndexCreationHelper(origFromClause, origIndexedExpression, projectionAttributes, (InternalCache) region.getCache(), externalContext, this);
        } else {
            throw new AssertionError("Don't know how to set helper for " + indexType);
        }
        if (!isCompactOrHash && indexType != IndexType.PRIMARY_KEY) {
            if (indexType == IndexType.HASH) {
                if (!isIndexMaintenanceTypeSynchronous()) {
                    throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE.toLocalizedString());
                }
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS.toLocalizedString());
            }
            // Overflow is not supported with range index.
            if (isOverFlowRegion()) {
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_REGIONS_WHICH_OVERFLOW_TO_DISK_THE_REGION_INVOLVED_IS_0.toLocalizedString(region.getFullPath()));
            }
            // OffHeap is not supported with range index.
            if (isOffHeap()) {
                if (!isIndexMaintenanceTypeSynchronous()) {
                    throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
                }
                throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Started creating index with indexName: {} On region: {}", indexName, region.getFullPath());
        }
        if (IndexManager.testHook != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("IndexManager TestHook is set.");
            }
            if (((LocalRegion) this.region).isInitialized()) {
                testHook.hook(1);
            } else {
                testHook.hook(0);
            }
        }
        IndexTask indexTask = new IndexTask(indexName, indexType, origFromClause, origIndexedExpression, helper, isCompactOrHash, prIndex, loadEntries);
        FutureTask<Index> indexFutureTask = new FutureTask<Index>(indexTask);
        Object oldIndex = this.indexes.putIfAbsent(indexTask, indexFutureTask);
        Index index = null;
        boolean interrupted = false;
        try {
            if (oldIndex == null) {
                // Initialize index.
                indexFutureTask.run();
                // Set the index.
                index = (Index) indexFutureTask.get();
            } else {
                // Check if index creation is complete.
                if (!(oldIndex instanceof Index)) {
                    // Some other thread is creating the same index.
                    // Wait for index to be initialized from other thread.
                    ((Future) oldIndex).get();
                }
                // from this thread.
                if (getIndex(indexName) != null) {
                    throw new IndexNameConflictException(LocalizedStrings.IndexManager_INDEX_NAMED_0_ALREADY_EXISTS.toLocalizedString(indexName));
                } else {
                    throw new IndexExistsException(LocalizedStrings.IndexManager_SIMILAR_INDEX_EXISTS.toLocalizedString());
                }
            }
        } catch (InterruptedException ignored) {
            interrupted = true;
        } catch (ExecutionException ee) {
            Throwable c = ee.getCause();
            if (c instanceof IndexNameConflictException) {
                throw (IndexNameConflictException) c;
            } else if (c instanceof IndexExistsException) {
                throw (IndexExistsException) c;
            } else if (c instanceof IMQException) {
                throw new IndexInvalidException(c.getMessage());
            }
            throw new IndexInvalidException(ee);
        } finally {
            // the map.
            if (oldIndex == null && index == null) {
                Object ind = this.indexes.get(indexTask);
                if (ind != null && !(ind instanceof Index)) {
                    this.indexes.remove(indexTask);
                }
            }
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
        assert (index != null);
        if (logger.isDebugEnabled()) {
            logger.debug("Completed creating index with indexName: {} On region: {}", indexName, region.getFullPath());
        }
        return index;
    } finally {
        DefaultQuery.setPdxReadSerialized(this.region.getCache(), oldReadSerialized);
        if (tx != null) {
            ((TXManagerImpl) this.region.getCache().getCacheTransactionManager()).internalResume(tx);
        }
    }
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) InternalCache(org.apache.geode.internal.cache.InternalCache) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) FutureTask(java.util.concurrent.FutureTask) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) Future(java.util.concurrent.Future) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) ExecutionException(java.util.concurrent.ExecutionException)

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