Search in sources :

Example 6 with Or

use of org.janusgraph.graphdb.query.condition.Or in project janusgraph by JanusGraph.

the class QueryUtil method addConstraint.

private static <E extends JanusGraphElement> And<E> addConstraint(final RelationType type, AndJanusPredicate predicate, List<Object> values, And<E> and, StandardJanusGraphTx tx) {
    for (int i = 0; i < values.size(); i++) {
        final JanusGraphPredicate janusGraphPredicate = predicate.get(i);
        if (janusGraphPredicate instanceof Contain) {
            // Rewrite contains conditions
            final Collection childValues = (Collection) values.get(i);
            if (janusGraphPredicate == Contain.NOT_IN) {
                // Simply ignore since trivially satisfied
                if (childValues.isEmpty())
                    continue;
                for (final Object inValue : childValues) addConstraint(type, Cmp.NOT_EQUAL, inValue, and, tx);
            } else {
                Preconditions.checkArgument(janusGraphPredicate == Contain.IN);
                if (childValues.isEmpty()) {
                    // Cannot be satisfied
                    return null;
                }
                if (childValues.size() == 1) {
                    addConstraint(type, Cmp.EQUAL, childValues.iterator().next(), and, tx);
                } else {
                    final Or<E> nested = new Or<>(childValues.size());
                    for (final Object inValue : childValues) addConstraint(type, Cmp.EQUAL, inValue, nested, tx);
                    and.add(nested);
                }
            }
        } else if (janusGraphPredicate instanceof AndJanusPredicate) {
            if (addConstraint(type, (AndJanusPredicate) (janusGraphPredicate), (List<Object>) (values.get(i)), and, tx) == null) {
                return null;
            }
        } else if (predicate.get(i) instanceof OrJanusPredicate) {
            final List<Object> childValues = (List<Object>) (values.get(i));
            final Or<E> nested = addConstraint(type, (OrJanusPredicate) (janusGraphPredicate), childValues, new Or<>(childValues.size()), tx);
            if (nested == null) {
                return null;
            }
            and.add(nested);
        } else {
            addConstraint(type, janusGraphPredicate, values.get(i), and, tx);
        }
    }
    return and;
}
Also used : Or(org.janusgraph.graphdb.query.condition.Or) AndJanusPredicate(org.janusgraph.graphdb.predicate.AndJanusPredicate) OrJanusPredicate(org.janusgraph.graphdb.predicate.OrJanusPredicate) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Contain(org.janusgraph.core.attribute.Contain)

Aggregations

Or (org.janusgraph.graphdb.query.condition.Or)6 Collection (java.util.Collection)4 And (org.janusgraph.graphdb.query.condition.And)4 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)4 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 List (java.util.List)3 UUID (java.util.UUID)3 Cmp (org.janusgraph.core.attribute.Cmp)3 Geo (org.janusgraph.core.attribute.Geo)3 Geoshape (org.janusgraph.core.attribute.Geoshape)3 Mapping (org.janusgraph.core.schema.Mapping)3 JanusGraphPredicate (org.janusgraph.graphdb.query.JanusGraphPredicate)3 Condition (org.janusgraph.graphdb.query.condition.Condition)3 Not (org.janusgraph.graphdb.query.condition.Not)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RelationType (org.janusgraph.core.RelationType)2 KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)2