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());
});
}
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);
}
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;
}
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;
}
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;
}
Aggregations