Search in sources :

Example 1 with VertexiumCypherNotImplemented

use of org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented in project vertexium by visallo.

the class MatchClauseExecutor method executeFirstMatchConstraint.

private static IterableWithTotalHits<? extends Element> executeFirstMatchConstraint(VertexiumCypherQueryContext ctx, MatchConstraint<?, ?> matchConstraint, ExpressionScope scope, Long limit) {
    try {
        List<String> labelNames = getLabelNamesFromMatchConstraint(matchConstraint);
        ListMultimap<String, CypherAstBase> propertiesMap = getPropertiesMapFromElementPatterns(ctx, matchConstraint.getPatterns());
        QueryResultsIterable<? extends Element> elements;
        Query query = ctx.getGraph().query(ctx.getAuthorizations()).limit(limit);
        if (labelNames.size() == 0 && propertiesMap.size() == 0) {
            elements = executeQuery(ctx, query, matchConstraint);
        } else {
            if (labelNames.size() > 0) {
                Stream<String> labelNamesStream = labelNames.stream().map(ctx::normalizeLabelName);
                if (matchConstraint instanceof NodeMatchConstraint) {
                    query = labelNamesStream.reduce(query, (q, labelName) -> q.has(ctx.getLabelPropertyName(), labelName), (q, q2) -> q);
                } else if (matchConstraint instanceof RelationshipMatchConstraint) {
                    List<String> normalizedLabelNames = labelNamesStream.collect(Collectors.toList());
                    query = query.hasEdgeLabel(normalizedLabelNames);
                } else {
                    throw new VertexiumCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName());
                }
            }
            for (Map.Entry<String, CypherAstBase> propertyMatch : propertiesMap.entries()) {
                Object value = ctx.getExpressionExecutor().executeExpression(ctx, propertyMatch.getValue(), scope);
                if (value instanceof CypherAstBase) {
                    throw new VertexiumException("unexpected value: " + value.getClass().getName() + ": " + value);
                }
                if (value instanceof Stream) {
                    value = ((Stream<?>) value).collect(Collectors.toList());
                }
                if (value instanceof Collection) {
                    query.has(propertyMatch.getKey(), Contains.IN, value);
                } else {
                    query.has(propertyMatch.getKey(), value);
                }
            }
            elements = executeQuery(ctx, query, matchConstraint);
        }
        return elements;
    } catch (VertexiumPropertyNotDefinedException e) {
        LOGGER.error(e.getMessage());
        return new EmptyResultsQueryResultsIterable<>();
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) java.util(java.util) VertexiumCypherException(org.vertexium.cypher.exceptions.VertexiumCypherException) ListMultimap(com.google.common.collect.ListMultimap) VertexiumLogger(org.vertexium.util.VertexiumLogger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) VertexiumLoggerFactory(org.vertexium.util.VertexiumLoggerFactory) org.vertexium(org.vertexium) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) org.vertexium.cypher.ast.model(org.vertexium.cypher.ast.model) Lists(com.google.common.collect.Lists) Stream(java.util.stream.Stream) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope) org.vertexium.query(org.vertexium.query) StreamUtils.stream(org.vertexium.util.StreamUtils.stream) ObjectUtils(org.vertexium.cypher.utils.ObjectUtils) org.vertexium.cypher.executor.models.match(org.vertexium.cypher.executor.models.match) StreamUtils(org.vertexium.util.StreamUtils) StreamSupport(java.util.stream.StreamSupport) MatchConstraintBuilder(org.vertexium.cypher.executor.utils.MatchConstraintBuilder) VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) Stream(java.util.stream.Stream)

Example 2 with VertexiumCypherNotImplemented

use of org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented in project vertexium by visallo.

the class SetClauseExecutor method executeSetProperty.

private void executeSetProperty(VertexiumCypherQueryContext ctx, CypherSetProperty setItem, VertexiumCypherScope.Item item) {
    Object left = ctx.getExpressionExecutor().executeExpression(ctx, setItem.getLeft().getAtom(), item);
    VertexiumCypherTypeErrorException.assertType(left, Element.class, null);
    if (left == null) {
        return;
    }
    String propertyName = setItem.getLeft().getProperty();
    Object value = ctx.getExpressionExecutor().executeExpression(ctx, setItem.getRight(), item);
    Element element = (Element) left;
    ExistingElementMutation<Element> m = element.prepareMutation();
    switch(setItem.getOp()) {
        case EQUAL:
            if (value == null) {
                ctx.removeProperty(m, propertyName);
            } else {
                ctx.setProperty(m, propertyName, value);
            }
            break;
        default:
            throw new VertexiumCypherNotImplemented("" + setItem);
    }
    ctx.saveElement(m);
}
Also used : VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) Element(org.vertexium.Element)

Example 3 with VertexiumCypherNotImplemented

use of org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented in project vertexium by visallo.

the class ExpressionExecutor method executeExpression.

public Object executeExpression(VertexiumCypherQueryContext ctx, CypherAstBase expression, ExpressionScope scope) {
    if (expression == null) {
        return null;
    }
    if (expression instanceof CypherExpression) {
        if (expression instanceof CypherBinaryExpression) {
            return executeBinaryExpression(ctx, (CypherBinaryExpression) expression, scope);
        } else if (expression instanceof CypherComparisonExpression) {
            return executeComparisonExpression(ctx, (CypherComparisonExpression) expression, scope);
        } else if (expression instanceof CypherUnaryExpression) {
            return executeUnaryExpression(ctx, (CypherUnaryExpression) expression, scope);
        } else if (expression instanceof CypherTrueExpression) {
            return true;
        } else if (expression instanceof CypherNegateExpression) {
            return executeNegateExpression(ctx, (CypherNegateExpression) expression, scope);
        }
        throw new VertexiumCypherNotImplemented("" + expression);
    }
    if (expression instanceof CypherListLiteral) {
        // noinspection unchecked
        CypherListLiteral<? extends CypherAstBase> list = (CypherListLiteral<? extends CypherAstBase>) expression;
        return executeList(ctx, list, scope);
    }
    if (expression instanceof CypherLiteral) {
        CypherLiteral literal = (CypherLiteral) expression;
        return literal.getValue();
    }
    if (expression instanceof CypherVariable) {
        CypherVariable variable = (CypherVariable) expression;
        return executeObject(ctx, executeVariable(ctx, variable, scope), scope);
    }
    if (expression instanceof CypherLookup) {
        CypherLookup lookup = (CypherLookup) expression;
        return executeLookup(ctx, lookup, scope);
    }
    if (expression instanceof CypherFunctionInvocation) {
        CypherFunctionInvocation functionInvocation = (CypherFunctionInvocation) expression;
        return executeFunctionInvocation(ctx, functionInvocation, scope);
    }
    if (expression instanceof CypherIn) {
        CypherIn in = (CypherIn) expression;
        return executeIn(ctx, in, scope);
    }
    if (expression instanceof CypherArrayAccess) {
        CypherArrayAccess arrayAccess = (CypherArrayAccess) expression;
        return executeArrayAccess(ctx, arrayAccess, scope);
    }
    if (expression instanceof CypherArraySlice) {
        CypherArraySlice arraySlice = (CypherArraySlice) expression;
        return executeArraySlice(ctx, arraySlice, scope);
    }
    if (expression instanceof CypherParameter) {
        CypherParameter parameter = (CypherParameter) expression;
        return executeParameter(ctx, parameter);
    }
    if (expression instanceof CypherIsNull) {
        CypherIsNull isNull = (CypherIsNull) expression;
        return executeIsNull(ctx, isNull, scope);
    }
    if (expression instanceof CypherIsNotNull) {
        CypherIsNotNull isNotNull = (CypherIsNotNull) expression;
        return executeIsNotNull(ctx, isNotNull, scope);
    }
    if (expression instanceof CypherListComprehension) {
        CypherListComprehension listComprehension = (CypherListComprehension) expression;
        return executeListComprehension(ctx, listComprehension, scope);
    }
    if (expression instanceof CypherStringMatch) {
        CypherStringMatch startWith = (CypherStringMatch) expression;
        return executeStringMatch(ctx, startWith, scope);
    }
    if (expression instanceof CypherPatternComprehension) {
        CypherPatternComprehension patternComprehension = (CypherPatternComprehension) expression;
        VertexiumCypherScope matchScope = scope instanceof VertexiumCypherScope ? (VertexiumCypherScope) scope : VertexiumCypherScope.newSingleItemScope((VertexiumCypherScope.Item) scope);
        VertexiumCypherScope results = ctx.getMatchClauseExecutor().execute(ctx, Lists.newArrayList(patternComprehension.getMatchClause()), matchScope);
        return results.stream().map(item -> executeExpression(ctx, patternComprehension.getExpression(), item));
    }
    throw new VertexiumException("not implemented \"" + expression.getClass().getName() + "\": " + expression);
}
Also used : VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) VertexiumException(org.vertexium.VertexiumException) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Aggregations

VertexiumCypherNotImplemented (org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented)3 VertexiumCypherScope (org.vertexium.cypher.VertexiumCypherScope)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 Lists (com.google.common.collect.Lists)1 java.util (java.util)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 org.vertexium (org.vertexium)1 Element (org.vertexium.Element)1 VertexiumException (org.vertexium.VertexiumException)1 VertexiumCypherQueryContext (org.vertexium.cypher.VertexiumCypherQueryContext)1 org.vertexium.cypher.ast.model (org.vertexium.cypher.ast.model)1 VertexiumCypherException (org.vertexium.cypher.exceptions.VertexiumCypherException)1 VertexiumCypherTypeErrorException (org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException)1 org.vertexium.cypher.executor.models.match (org.vertexium.cypher.executor.models.match)1 MatchConstraintBuilder (org.vertexium.cypher.executor.utils.MatchConstraintBuilder)1