Search in sources :

Example 1 with IndexInvalidException

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

the class PrimaryKeyIndexCreationHelper method prepareIndexExpression.

private void prepareIndexExpression(String indexedExpression) throws IndexInvalidException {
    List indexedExprs = this.compiler.compileProjectionAttributes(indexedExpression);
    if (indexedExprs == null || indexedExprs.size() != 1) {
        throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0.toLocalizedString(indexedExpression));
    }
    CompiledValue expr = (CompiledValue) ((Object[]) indexedExprs.get(0))[1];
    if (expr.getType() == CompiledValue.LITERAL)
        throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0.toLocalizedString(indexedExpression));
    try {
        StringBuilder sb = new StringBuilder();
        expr.generateCanonicalizedExpression(sb, context);
        this.indexedExpression = sb.toString();
    } catch (Exception e) {
        throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0_N_1.toLocalizedString(new Object[] { indexedExpression, e.getMessage() }));
    }
}
Also used : CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) List(java.util.List) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException)

Example 2 with IndexInvalidException

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

the class FunctionalIndexCreationHelper method prepareIndexExpression.

/**
   * This function is modified so that if the indexed expression has any dependency on the 0th
   * iterator, then it needs to modified by using the missing link so that it is derivable from the
   * 0th iterator.
   * <p>
   * TODO: refactor large method prepareIndexExpression
   */
private void prepareIndexExpression(String indexedExpression) throws IndexInvalidException {
    CompiledValue expr = this.compiler.compileQuery(indexedExpression);
    if (expr == null) {
        throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression));
    }
    if (expr instanceof CompiledUndefined || expr instanceof CompiledLiteral || expr instanceof CompiledComparison || expr instanceof CompiledBindArgument || expr instanceof CompiledNegation) {
        throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression));
    }
    try {
        StringBuilder sb = new StringBuilder();
        if (expr instanceof MapIndexable) {
            MapIndexable mi = (MapIndexable) expr;
            List<CompiledValue> indexingKeys = mi.getIndexingKeys();
            if (indexingKeys.size() == 1 && indexingKeys.get(0) == CompiledValue.MAP_INDEX_ALL_KEYS) {
                this.isMapTypeIndex = true;
                this.isAllKeys = true;
                // Strip the index operator
                expr = mi.getRecieverSansIndexArgs();
                expr.generateCanonicalizedExpression(sb, this.context);
                sb.append('[').append('*').append(']');
            } else if (indexingKeys.size() == 1) {
                expr.generateCanonicalizedExpression(sb, this.context);
            } else {
                this.isMapTypeIndex = true;
                this.multiIndexKeysPattern = new String[indexingKeys.size()];
                this.mapKeys = new Object[indexingKeys.size()];
                expr = mi.getRecieverSansIndexArgs();
                expr.generateCanonicalizedExpression(sb, this.context);
                sb.append('[');
                String prefixStr = sb.toString();
                StringBuilder sb2 = new StringBuilder();
                int size = indexingKeys.size();
                for (int j = 0; j < size; ++j) {
                    CompiledValue cv = indexingKeys.get(size - j - 1);
                    this.mapKeys[size - j - 1] = cv.evaluate(this.context);
                    StringBuilder sb3 = new StringBuilder();
                    cv.generateCanonicalizedExpression(sb3, this.context);
                    sb3.insert(0, prefixStr);
                    sb3.append(']');
                    this.multiIndexKeysPattern[j] = sb3.toString();
                    cv.generateCanonicalizedExpression(sb2, this.context);
                    sb2.insert(0, ',');
                }
                sb2.deleteCharAt(0);
                sb.append(sb2);
                sb.append(']');
            }
        } else {
            expr.generateCanonicalizedExpression(sb, this.context);
        }
        this.indexedExpression = sb.toString();
        this.modifiedIndexExpr = expr;
        if (!this.isFirstIteratorRegionEntry && this.indexedExpression.contains(this.canonicalizedIteratorNames[0])) {
            this.modifiedIndexExpr = getModifiedDependentCompiledValue(this.context, -1, expr, true);
        }
    } catch (Exception e) {
        throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression), e);
    }
    this.indexedExpr = expr;
}
Also used : CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) CompiledUndefined(org.apache.geode.cache.query.internal.CompiledUndefined) CompiledComparison(org.apache.geode.cache.query.internal.CompiledComparison) CompiledLiteral(org.apache.geode.cache.query.internal.CompiledLiteral) AmbiguousNameException(org.apache.geode.cache.query.AmbiguousNameException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) CompiledBindArgument(org.apache.geode.cache.query.internal.CompiledBindArgument) CompiledNegation(org.apache.geode.cache.query.internal.CompiledNegation) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) MapIndexable(org.apache.geode.cache.query.internal.MapIndexable)

Example 3 with IndexInvalidException

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

the class FunctionalIndexCreationHelper method modifyIterDefToSuiteIMQ.

private Object[] modifyIterDefToSuiteIMQ(CompiledIteratorDef iterDef) throws IndexInvalidException {
    Object[] retValues = { null, null };
    try {
        CompiledValue def = iterDef.getCollectionExpr();
        if (def instanceof CompiledRegion) {
            CompiledBindArgument bindArg = new CompiledBindArgument(1);
            CompiledIteratorDef newDef = new CompiledIteratorDef(iterDef.getName(), null, bindArg);
            retValues[0] = def.evaluate(this.context);
            retValues[1] = newDef;
            return retValues;
        }
        if (def instanceof CompiledPath || def instanceof CompiledOperation || def instanceof CompiledIndexOperation) {
            CompiledValue cv = def;
            List reconstruct = new ArrayList();
            while (!(cv instanceof CompiledRegion)) {
                CompiledValue prevCV = cv;
                if (cv instanceof CompiledOperation) {
                    reconstruct.add(0, ((CompiledOperation) cv).getArguments());
                    reconstruct.add(0, ((CompiledOperation) cv).getMethodName());
                    cv = ((CompiledOperation) cv).getReceiver(this.context);
                } else if (cv instanceof CompiledPath) {
                    reconstruct.add(0, ((CompiledPath) cv).getTailID());
                    cv = ((CompiledPath) cv).getReceiver();
                } else if (cv instanceof CompiledIndexOperation) {
                    reconstruct.add(0, ((CompiledIndexOperation) cv).getExpression());
                    cv = ((CompiledIndexOperation) cv).getReceiver();
                } else {
                    throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_FUNCTIONALINDEXCREATIONHELPERPREPAREFROMCLAUSEFROM_CLAUSE_IS_NEITHER_A_COMPILEDPATH_NOR_COMPILEDOPERATION.toLocalizedString());
                }
                reconstruct.add(0, prevCV.getType());
            }
            CompiledValue v = cv;
            cv = new CompiledBindArgument(1);
            int len = reconstruct.size();
            for (int j = 0; j < len; ++j) {
                Object obj = reconstruct.get(j);
                if (obj instanceof Integer) {
                    int tokenType = (Integer) obj;
                    if (tokenType == CompiledValue.PATH) {
                        cv = new CompiledPath(cv, (String) reconstruct.get(++j));
                    } else if (tokenType == OQLLexerTokenTypes.TOK_LBRACK) {
                        cv = new CompiledIndexOperation(cv, (CompiledValue) reconstruct.get(++j));
                    } else if (tokenType == OQLLexerTokenTypes.METHOD_INV) {
                        cv = new CompiledOperation(cv, (String) reconstruct.get(++j), (List) reconstruct.get(++j));
                    }
                }
            }
            CompiledIteratorDef newDef = new CompiledIteratorDef(iterDef.getName(), null, cv);
            retValues[0] = v.evaluate(this.context);
            retValues[1] = newDef;
            return retValues;
        }
    } catch (Exception e) {
        throw new IndexInvalidException(e);
    }
    return retValues;
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) ArrayList(java.util.ArrayList) CompiledPath(org.apache.geode.cache.query.internal.CompiledPath) CompiledIndexOperation(org.apache.geode.cache.query.internal.CompiledIndexOperation) AmbiguousNameException(org.apache.geode.cache.query.AmbiguousNameException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) CompiledBindArgument(org.apache.geode.cache.query.internal.CompiledBindArgument) CompiledOperation(org.apache.geode.cache.query.internal.CompiledOperation) ArrayList(java.util.ArrayList) List(java.util.List) CompiledRegion(org.apache.geode.cache.query.internal.CompiledRegion) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException)

Example 4 with IndexInvalidException

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

the class CreateIndexFunction method execute.

@Override
public void execute(FunctionContext context) {
    final IndexInfo indexInfo = (IndexInfo) context.getArguments();
    String memberId = null;
    try {
        Cache cache = CacheFactory.getAnyInstance();
        memberId = cache.getDistributedSystem().getDistributedMember().getId();
        QueryService queryService = cache.getQueryService();
        String indexName = indexInfo.getIndexName();
        String indexedExpression = indexInfo.getIndexedExpression();
        String fromClause = indexInfo.getRegionPath();
        // Check to see if the region path contains an alias e.g "/region1 r1"
        // Then the first string will be the regionPath
        String[] regionPathTokens = fromClause.trim().split(" ");
        String regionPath = regionPathTokens[0];
        switch(indexInfo.getIndexType()) {
            case IndexInfo.RANGE_INDEX:
                queryService.createIndex(indexName, indexedExpression, fromClause);
                break;
            case IndexInfo.KEY_INDEX:
                queryService.createKeyIndex(indexName, indexedExpression, fromClause);
                break;
            case IndexInfo.HASH_INDEX:
                queryService.createHashIndex(indexName, indexedExpression, fromClause);
                break;
            default:
                queryService.createIndex(indexName, indexedExpression, fromClause);
        }
        regionPath = getValidRegionName(cache, regionPath);
        setResultInSender(context, indexInfo, memberId, cache, regionPath);
    } catch (IndexExistsException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getIndexName());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexNameConflictException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__NAME__CONFLICT, indexInfo.getIndexName());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (RegionNotFoundException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH, indexInfo.getRegionPath());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexInvalidException e) {
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    } catch (Exception e) {
        String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    }
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Cache(org.apache.geode.cache.Cache)

Example 5 with IndexInvalidException

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

the class RegionProvider method getOrCreateRegion0.

private Region<?, ?> getOrCreateRegion0(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context, boolean addToMeta) {
    checkDataType(key, type);
    Region<?, ?> r = this.regions.get(key);
    if (r != null && r.isDestroyed()) {
        removeKey(key, type);
        r = null;
    }
    if (r == null) {
        String stringKey = key.toString();
        Lock lock = this.locks.get(stringKey);
        if (lock == null) {
            this.locks.putIfAbsent(stringKey, new ReentrantLock());
            lock = this.locks.get(stringKey);
        }
        try {
            lock.lock();
            r = regions.get(key);
            if (r == null) {
                // Can create
                boolean hasTransaction = context != null && context.hasTransaction();
                // without context
                CacheTransactionManager txm = null;
                TransactionId transactionId = null;
                try {
                    if (hasTransaction) {
                        txm = cache.getCacheTransactionManager();
                        transactionId = txm.suspend();
                    }
                    Exception concurrentCreateDestroyException = null;
                    do {
                        concurrentCreateDestroyException = null;
                        r = createRegionGlobally(stringKey);
                        try {
                            if (type == RedisDataType.REDIS_LIST) {
                                doInitializeList(key, r);
                            } else if (type == RedisDataType.REDIS_SORTEDSET) {
                                try {
                                    doInitializeSortedSet(key, r);
                                } catch (RegionNotFoundException | IndexInvalidException e) {
                                    concurrentCreateDestroyException = e;
                                }
                            }
                        } catch (QueryInvalidException e) {
                            if (e.getCause() instanceof RegionNotFoundException) {
                                concurrentCreateDestroyException = e;
                            }
                        }
                    } while (concurrentCreateDestroyException != null);
                    this.regions.put(key, r);
                    if (addToMeta) {
                        RedisDataType existingType = metaPutIfAbsent(key, type);
                        if (existingType != null && existingType != type)
                            throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + existingType.toString());
                    }
                } finally {
                    if (hasTransaction)
                        txm.resume(transactionId);
                }
            }
        } finally {
            lock.unlock();
        }
    }
    return r;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionId(org.apache.geode.cache.TransactionId)

Aggregations

IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)11 IndexExistsException (org.apache.geode.cache.query.IndexExistsException)5 IndexNameConflictException (org.apache.geode.cache.query.IndexNameConflictException)5 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)5 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)5 List (java.util.List)4 AmbiguousNameException (org.apache.geode.cache.query.AmbiguousNameException)4 ExecutionException (java.util.concurrent.ExecutionException)3 FutureTask (java.util.concurrent.FutureTask)3 Index (org.apache.geode.cache.query.Index)3 CompiledBindArgument (org.apache.geode.cache.query.internal.CompiledBindArgument)3 CompiledIteratorDef (org.apache.geode.cache.query.internal.CompiledIteratorDef)3 CompiledValue (org.apache.geode.cache.query.internal.CompiledValue)3 ArrayList (java.util.ArrayList)2 MultiIndexCreationException (org.apache.geode.cache.query.MultiIndexCreationException)2 QueryException (org.apache.geode.cache.query.QueryException)2 QueryService (org.apache.geode.cache.query.QueryService)2 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)2 CompiledIndexOperation (org.apache.geode.cache.query.internal.CompiledIndexOperation)2 CompiledOperation (org.apache.geode.cache.query.internal.CompiledOperation)2