Search in sources :

Example 31 with HugeKeys

use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.

the class MysqlTable method queryId2Select.

protected List<StringBuilder> queryId2Select(Query query, StringBuilder select) {
    // Query by id(s)
    if (query.idsSize() == 0) {
        return ImmutableList.of(select);
    }
    List<HugeKeys> nameParts = this.idColumnName();
    List<List<Object>> ids = new ArrayList<>(query.idsSize());
    for (Id id : query.ids()) {
        List<Object> idParts = this.idColumnValue(id);
        if (nameParts.size() != idParts.size()) {
            throw new NotFoundException("Unsupported ID format: '%s' (should contain %s)", id, nameParts);
        }
        ids.add(idParts);
    }
    // Query only by partition-key
    if (nameParts.size() == 1) {
        List<Object> values = new ArrayList<>(ids.size());
        for (List<Object> objects : ids) {
            assert objects.size() == 1;
            values.add(objects.get(0));
        }
        WhereBuilder where = this.newWhereBuilder();
        where.in(formatKey(nameParts.get(0)), values);
        select.append(where.build());
        return ImmutableList.of(select);
    }
    /*
         * Query by partition-key + clustering-key
         * NOTE: Error if multi-column IN clause include partition key:
         * error: multi-column relations can only be applied to clustering
         * columns when using: select.where(QueryBuilder.in(names, idList));
         * So we use multi-query instead of IN
         */
    List<StringBuilder> selections = new ArrayList<>(ids.size());
    for (List<Object> objects : ids) {
        assert nameParts.size() == objects.size();
        StringBuilder idSelection = new StringBuilder(select);
        /*
             * NOTE: concat with AND relation, like:
             * "pk = id and ck1 = v1 and ck2 = v2"
             */
        WhereBuilder where = this.newWhereBuilder();
        where.and(formatKeys(nameParts), objects);
        idSelection.append(where.build());
        selections.add(idSelection);
    }
    return selections;
}
Also used : ArrayList(java.util.ArrayList) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Id(com.baidu.hugegraph.backend.id.Id) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 32 with HugeKeys

use of com.baidu.hugegraph.type.define.HugeKeys in project incubator-hugegraph by apache.

the class MysqlTable method buildInsertTemplateForce.

protected String buildInsertTemplateForce(MysqlBackendEntry.Row entry) {
    StringBuilder insert = new StringBuilder();
    insert.append("REPLACE INTO ").append(this.table()).append(" (");
    int i = 0;
    int n = entry.columns().size();
    for (HugeKeys key : entry.columns().keySet()) {
        insert.append(formatKey(key));
        if (++i != n) {
            insert.append(", ");
        }
    }
    insert.append(") VALUES (");
    // Fill with '?'
    for (i = 0; i < n; i++) {
        insert.append("?");
        if (i != n - 1) {
            insert.append(", ");
        }
    }
    insert.append(")");
    return insert.toString();
}
Also used : HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Aggregations

HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)32 ArrayList (java.util.ArrayList)9 Map (java.util.Map)7 LinkedHashMap (java.util.LinkedHashMap)5 List (java.util.List)5 HugeGraph (com.baidu.hugegraph.HugeGraph)4 Id (com.baidu.hugegraph.backend.id.Id)4 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 BackendException (com.baidu.hugegraph.backend.BackendException)3 Condition (com.baidu.hugegraph.backend.query.Condition)3 SQLException (java.sql.SQLException)3 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)2 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)2 Select (com.datastax.driver.core.querybuilder.Select)2 Update (com.datastax.driver.core.querybuilder.Update)2 HugeException (com.baidu.hugegraph.HugeException)1 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)1 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)1