Search in sources :

Example 1 with CompiledValue

use of org.apache.geode.cache.query.internal.CompiledValue 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 CompiledValue

use of org.apache.geode.cache.query.internal.CompiledValue 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 CompiledValue

use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.

the class FunctionalIndexCreationHelper method getModifiedDependentCompiledValue.

/**
   * This function is used to correct the complied value's dependency , in case the compiledValue is
   * dependent on the 0th RuntimeIterator in some way. Thus the dependent compiled value is prefixed
   * with the missing link so that it is derivable from the 0th iterator.
   */
private CompiledValue getModifiedDependentCompiledValue(ExecutionContext context, int currItrID, CompiledValue cv, boolean isDependent) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    if (cv instanceof CompiledIteratorDef) {
        CompiledIteratorDef iterDef = (CompiledIteratorDef) cv;
        RuntimeIterator rItr = (RuntimeIterator) context.getCurrentIterators().get(currItrID);
        String canonFrmClause = rItr.getDefinition();
        // TODO: original value of isDependent is always ignored
        isDependent = canonFrmClause.startsWith(this.canonicalizedIteratorNames[0]);
        return new CompiledIteratorDef(iterDef.getName(), rItr.getElementType(), getModifiedDependentCompiledValue(context, currItrID, iterDef.getCollectionExpr(), isDependent));
    } else if (cv instanceof CompiledPath) {
        CompiledPath path = (CompiledPath) cv;
        return new CompiledPath(getModifiedDependentCompiledValue(context, currItrID, path.getReceiver(), isDependent), path.getTailID());
    } else if (cv instanceof CompiledOperation) {
        CompiledOperation oper = (CompiledOperation) cv;
        List list = oper.getArguments();
        List newList = new ArrayList();
        for (Object aList : list) {
            CompiledValue cv1 = (CompiledValue) aList;
            StringBuilder sb = new StringBuilder();
            cv1.generateCanonicalizedExpression(sb, context);
            if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
                newList.add(getModifiedDependentCompiledValue(context, currItrID, cv1, true));
            } else {
                newList.add(getModifiedDependentCompiledValue(context, currItrID, cv1, false));
            }
        }
        // What if the receiver is null?
        CompiledValue rec = oper.getReceiver(context);
        if (rec == null) {
            if (isDependent) {
                rec = this.missingLink;
            }
            return new CompiledOperation(rec, oper.getMethodName(), newList);
        } else {
            return new CompiledOperation(getModifiedDependentCompiledValue(context, currItrID, rec, isDependent), oper.getMethodName(), newList);
        }
    } else if (cv instanceof CompiledFunction) {
        CompiledFunction cf = (CompiledFunction) cv;
        CompiledValue[] cvArray = cf.getArguments();
        int function = cf.getFunction();
        int len = cvArray.length;
        CompiledValue[] newCvArray = new CompiledValue[len];
        for (int i = 0; i < len; ++i) {
            CompiledValue cv1 = cvArray[i];
            StringBuilder sb = new StringBuilder();
            cv1.generateCanonicalizedExpression(sb, context);
            if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
                newCvArray[i] = getModifiedDependentCompiledValue(context, currItrID, cv1, true);
            } else {
                newCvArray[i] = getModifiedDependentCompiledValue(context, currItrID, cv1, false);
            }
        }
        return new CompiledFunction(newCvArray, function);
    } else if (cv instanceof CompiledID) {
        CompiledID id = (CompiledID) cv;
        RuntimeIterator rItr0 = (RuntimeIterator) context.getCurrentIterators().get(0);
        if (isDependent) {
            String name;
            if ((name = rItr0.getName()) != null && name.equals(id.getId())) {
                // The CompiledID is a RuneTimeIterator & so it needs to be replaced by the missing link
                return this.missingLink;
            } else {
                // The compiledID is a compiledPath
                return new CompiledPath(this.missingLink, id.getId());
            }
        } else {
            return cv;
        }
    } else if (cv instanceof CompiledIndexOperation) {
        CompiledIndexOperation co = (CompiledIndexOperation) cv;
        CompiledValue cv1 = co.getExpression();
        StringBuilder sb = new StringBuilder();
        cv1.generateCanonicalizedExpression(sb, context);
        if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
            cv1 = getModifiedDependentCompiledValue(context, currItrID, cv1, true);
        } else {
            cv1 = getModifiedDependentCompiledValue(context, currItrID, cv1, false);
        }
        return new CompiledIndexOperation(getModifiedDependentCompiledValue(context, currItrID, co.getReceiver(), isDependent), cv1);
    } else {
        return cv;
    }
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) CompiledID(org.apache.geode.cache.query.internal.CompiledID) CompiledFunction(org.apache.geode.cache.query.internal.CompiledFunction) 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) CompiledOperation(org.apache.geode.cache.query.internal.CompiledOperation) ArrayList(java.util.ArrayList) List(java.util.List) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator)

Example 4 with CompiledValue

use of org.apache.geode.cache.query.internal.CompiledValue 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 5 with CompiledValue

use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.

the class AbstractMapIndex method isMatchingWithIndexExpression.

@Override
public boolean isMatchingWithIndexExpression(CompiledValue condnExpr, String conditionExprStr, ExecutionContext context) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    if (this.isAllKeys) {
        // stripped of the index arg & see if it matches.
        if (condnExpr instanceof MapIndexable) {
            MapIndexable mi = (MapIndexable) condnExpr;
            CompiledValue recvr = mi.getRecieverSansIndexArgs();
            StringBuilder sb = new StringBuilder();
            recvr.generateCanonicalizedExpression(sb, context);
            sb.append('[').append(']');
            return sb.toString().equals(this.patternStr[0]);
        } else {
            return false;
        }
    } else {
        for (String expr : this.patternStr) {
            if (expr.equals(conditionExprStr)) {
                return true;
            }
        }
        return false;
    }
}
Also used : CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) MapIndexable(org.apache.geode.cache.query.internal.MapIndexable)

Aggregations

CompiledValue (org.apache.geode.cache.query.internal.CompiledValue)21 HashSet (java.util.HashSet)7 List (java.util.List)6 Set (java.util.Set)6 Region (org.apache.geode.cache.Region)5 SelectResults (org.apache.geode.cache.query.SelectResults)5 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)5 RuntimeIterator (org.apache.geode.cache.query.internal.RuntimeIterator)5 LocalRegion (org.apache.geode.internal.cache.LocalRegion)5 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)4 Query (org.apache.geode.cache.query.Query)4 Portfolio (org.apache.geode.cache.query.data.Portfolio)4 CompiledIteratorDef (org.apache.geode.cache.query.internal.CompiledIteratorDef)4 CompiledPath (org.apache.geode.cache.query.internal.CompiledPath)4 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)4 AmbiguousNameException (org.apache.geode.cache.query.AmbiguousNameException)3 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)3