Search in sources :

Example 6 with Relation

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

the class CassandraTable method relation2Cql.

protected Clause relation2Cql(Relation relation) {
    String key = relation.serialKey().toString();
    Object value = relation.serialValue();
    switch(relation.relation()) {
        case EQ:
            return QueryBuilder.eq(key, value);
        case GT:
            return QueryBuilder.gt(key, value);
        case GTE:
            return QueryBuilder.gte(key, value);
        case LT:
            return QueryBuilder.lt(key, value);
        case LTE:
            return QueryBuilder.lte(key, value);
        case IN:
            return Clauses.in(key, (List<?>) value);
        case CONTAINS_VALUE:
            return QueryBuilder.contains(key, value);
        case CONTAINS_KEY:
            return QueryBuilder.containsKey(key, value);
        case SCAN:
            String[] col = pkColumnName().stream().map(pk -> formatKey(pk)).toArray(String[]::new);
            Shard shard = (Shard) value;
            Object start = QueryBuilder.raw(shard.start());
            Object end = QueryBuilder.raw(shard.end());
            return Clauses.and(QueryBuilder.gte(QueryBuilder.token(col), start), QueryBuilder.lt(QueryBuilder.token(col), end));
        // return QueryBuilder.like(key, value);
        case NEQ:
        default:
            throw new NotSupportException("relation '%s'", relation);
    }
}
Also used : Selection(com.datastax.driver.core.querybuilder.Select.Selection) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) BiFunction(java.util.function.BiFunction) BackendException(com.baidu.hugegraph.backend.BackendException) Clause(com.datastax.driver.core.querybuilder.Clause) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) PagingStateException(com.datastax.driver.core.exceptions.PagingStateException) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) Create(com.datastax.driver.core.schemabuilder.Create) Delete(com.datastax.driver.core.querybuilder.Delete) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) PageState(com.baidu.hugegraph.backend.page.PageState) List(java.util.List) Log(com.baidu.hugegraph.util.Log) Id(com.baidu.hugegraph.backend.id.Id) Select(com.datastax.driver.core.querybuilder.Select) Statement(com.datastax.driver.core.Statement) CopyUtil(com.baidu.hugegraph.util.CopyUtil) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) Order(com.baidu.hugegraph.backend.query.Query.Order) Row(com.datastax.driver.core.Row) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) Function(java.util.function.Function) BackendTable(com.baidu.hugegraph.backend.store.BackendTable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) ResultSet(com.datastax.driver.core.ResultSet) ImmutableList(com.google.common.collect.ImmutableList) DriverException(com.datastax.driver.core.exceptions.DriverException) Shard(com.baidu.hugegraph.backend.store.Shard) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) E(com.baidu.hugegraph.util.E) SchemaBuilder(com.datastax.driver.core.schemabuilder.SchemaBuilder) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) PagingState(com.datastax.driver.core.PagingState) Condition(com.baidu.hugegraph.backend.query.Condition) Update(com.datastax.driver.core.querybuilder.Update) Insert(com.datastax.driver.core.querybuilder.Insert) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Clauses(com.datastax.driver.core.querybuilder.Clauses) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) DataType(com.datastax.driver.core.DataType) HugeType(com.baidu.hugegraph.type.HugeType) Definition(com.datastax.driver.core.ColumnDefinitions.Definition) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Shard(com.baidu.hugegraph.backend.store.Shard)

Example 7 with Relation

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

the class ConditionQuery method query.

public ConditionQuery query(Condition condition) {
    // Query by id (HugeGraph-259)
    if (condition instanceof Relation) {
        Relation relation = (Relation) condition;
        if (relation.key().equals(HugeKeys.ID) && relation.relation() == RelationType.EQ) {
            E.checkArgument(relation.value() instanceof Id, "Invalid id value '%s'", relation.value());
            super.query((Id) relation.value());
            return this;
        }
    }
    if (this.conditions == EMPTY_CONDITIONS) {
        this.conditions = InsertionOrderUtil.newList();
    }
    this.conditions.add(condition);
    return this;
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) Id(com.baidu.hugegraph.backend.id.Id)

Example 8 with Relation

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

the class ConditionQuery method userpropKeys.

public Set<Id> userpropKeys() {
    Set<Id> keys = new LinkedHashSet<>();
    for (Relation r : this.relations()) {
        if (!r.isSysprop()) {
            Condition.UserpropRelation ur = (Condition.UserpropRelation) r;
            keys.add(ur.key());
        }
    }
    return keys;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) Id(com.baidu.hugegraph.backend.id.Id)

Example 9 with Relation

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

the class ConditionQuery method userpropValuesString.

/**
 * This method is only used for secondary index scenario,
 * its relation must be EQ
 * @param fields the user property fields
 * @return the corresponding user property serial values of fields
 */
public String userpropValuesString(List<Id> fields) {
    List<Object> values = new ArrayList<>(fields.size());
    for (Id field : fields) {
        boolean got = false;
        for (Relation r : this.userpropRelations()) {
            if (r.key().equals(field) && !r.isSysprop()) {
                E.checkState(r.relation == RelationType.EQ || r.relation == RelationType.CONTAINS, "Method userpropValues(List<String>) only " + "used for secondary index, " + "relation must be EQ or CONTAINS, but got %s", r.relation());
                values.add(r.serialValue());
                got = true;
            }
        }
        if (!got) {
            throw new BackendException("No such userprop named '%s' in the query '%s'", field, this);
        }
    }
    return concatValues(values);
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 10 with Relation

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

the class HbaseTable method queryByCond.

protected <R> R queryByCond(HbaseSession<R> session, ConditionQuery query) {
    if (query.containsScanRelation()) {
        E.checkArgument(query.relations().size() == 1, "Invalid scan with multi conditions: %s", query);
        Relation scan = query.relations().iterator().next();
        Shard shard = (Shard) scan.value();
        return this.queryByRange(session, shard, query.page());
    }
    throw new NotSupportException("query: %s", query);
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) Shard(com.baidu.hugegraph.backend.store.Shard)

Aggregations

Relation (com.baidu.hugegraph.backend.query.Condition.Relation)18 SyspropRelation (com.baidu.hugegraph.backend.query.Condition.SyspropRelation)7 Id (com.baidu.hugegraph.backend.id.Id)5 Condition (com.baidu.hugegraph.backend.query.Condition)5 ArrayList (java.util.ArrayList)4 BackendException (com.baidu.hugegraph.backend.BackendException)3 Shard (com.baidu.hugegraph.backend.store.Shard)3 NotSupportException (com.baidu.hugegraph.exception.NotSupportException)3 HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)2 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)2 Clause (com.datastax.driver.core.querybuilder.Clause)2 Date (java.util.Date)2 Test (org.junit.Test)2 PageState (com.baidu.hugegraph.backend.page.PageState)1 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)1 Query (com.baidu.hugegraph.backend.query.Query)1 Order (com.baidu.hugegraph.backend.query.Query.Order)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 BackendTable (com.baidu.hugegraph.backend.store.BackendTable)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1