Search in sources :

Example 16 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionContainsValue.

@Test
public void testConditionContainsValue() {
    Condition c1 = Condition.containsValue(HugeKeys.ID, "abc");
    Assert.assertTrue(c1.test(ImmutableMap.of("k1", "abc")));
    Assert.assertTrue(c1.test(ImmutableMap.of("k1", "abc", "k2", "123")));
    Assert.assertFalse(c1.test(ImmutableMap.of("k1", "ab")));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test((Object) null);
    }, e -> {
        Assert.assertEquals("Can't execute `containsv` on type null, " + "expect Map", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test("v1");
    }, e -> {
        Assert.assertEquals("Can't execute `containsv` on type String, " + "expect Map", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test(123d);
    }, e -> {
        Assert.assertEquals("Can't execute `containsv` on type Double, " + "expect Map", e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 17 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class CassandraTable method queryCondition2Select.

protected Collection<Select> queryCondition2Select(Query query, Select select) {
    // Query by conditions
    Collection<Condition> conditions = query.conditions();
    for (Condition condition : conditions) {
        Clause clause = condition2Cql(condition);
        select.where(clause);
        if (Clauses.needAllowFiltering(clause)) {
            select.allowFiltering();
        }
    }
    return ImmutableList.of(select);
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) Clause(com.datastax.driver.core.querybuilder.Clause)

Example 18 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class TableSerializer method writeQueryCondition.

@Override
protected Query writeQueryCondition(Query query) {
    ConditionQuery result = (ConditionQuery) query;
    // No user-prop when serialize
    assert result.allSysprop();
    for (Condition.Relation r : result.relations()) {
        if (!r.value().equals(r.serialValue())) {
            // Has been serialized before (maybe share a query multi times)
            continue;
        }
        HugeKeys key = (HugeKeys) r.key();
        if (r.relation() == Condition.RelationType.IN) {
            E.checkArgument(r.value() instanceof List, "Expect list value for IN condition: %s", r);
            List<?> values = (List<?>) r.value();
            List<Object> serializedValues = new ArrayList<>(values.size());
            for (Object v : values) {
                serializedValues.add(this.serializeValue(key, v));
            }
            r.serialValue(serializedValues);
        } else if (r.relation() == Condition.RelationType.CONTAINS_VALUE && query.resultType().isGraph()) {
            r.serialValue(this.writeProperty(null, r.value()));
        } else {
            r.serialValue(this.serializeValue(key, r.value()));
        }
    }
    return result;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 19 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class InMemoryDBTable method queryByFilter.

protected Map<Id, BackendEntry> queryByFilter(Collection<Condition> conditions, Map<Id, BackendEntry> entries) {
    assert conditions.size() > 0;
    Map<Id, BackendEntry> rs = new HashMap<>();
    LOG.trace("queryByFilter {} size = {}", this.table(), entries.size());
    for (BackendEntry entry : entries.values()) {
        // Query by conditions
        boolean matched = true;
        for (Condition c : conditions) {
            if (!matchCondition(entry, c)) {
                // TODO: deal with others Condition like: and, or...
                matched = false;
                break;
            }
        }
        if (matched) {
            rs.put(entry.id(), entry);
        }
    }
    return rs;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) Condition(com.baidu.hugegraph.backend.query.Condition) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Id(com.baidu.hugegraph.backend.id.Id)

Example 20 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class InMemoryDBTable method matchCondition.

private static boolean matchCondition(BackendEntry item, Condition c) {
    // TODO: Compatible with BackendEntry
    TextBackendEntry entry = (TextBackendEntry) item;
    // Not supported by memory
    if (!(c instanceof Condition.Relation)) {
        throw new BackendException("Unsupported condition: " + c);
    }
    Condition.Relation r = (Condition.Relation) c;
    String key = r.serialKey().toString();
    // TODO: deal with others Relation like: <, >=, ...
    if (r.relation() == Condition.RelationType.CONTAINS_KEY) {
        return entry.contains(r.serialValue().toString());
    } else if (r.relation() == Condition.RelationType.CONTAINS_VALUE) {
        return entry.containsValue(r.serialValue().toString());
    } else if (r.relation() == Condition.RelationType.EQ) {
        return entry.contains(key, r.serialValue().toString());
    } else if (entry.contains(key)) {
        return r.test(entry.column(key));
    }
    return false;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

Condition (com.baidu.hugegraph.backend.query.Condition)49 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)29 Test (org.junit.Test)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)16 ArrayList (java.util.ArrayList)13 Id (com.baidu.hugegraph.backend.id.Id)12 Collection (java.util.Collection)8 Date (java.util.Date)8 SyspropRelation (com.baidu.hugegraph.backend.query.Condition.SyspropRelation)6 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)4 RangeConditions (com.baidu.hugegraph.backend.query.Condition.RangeConditions)4 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)4 HugeException (com.baidu.hugegraph.HugeException)3 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3 HugeType (com.baidu.hugegraph.type.HugeType)3 Directions (com.baidu.hugegraph.type.define.Directions)3 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)3 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)3 OrP (org.apache.tinkerpop.gremlin.process.traversal.util.OrP)3