Search in sources :

Example 26 with HugeKeys

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

the class PostgresqlTable method orderByKeys.

@Override
protected String orderByKeys() {
    // Set order-by to keep results order consistence for PostgreSQL result
    if (this.orderByKeysTemplate != null) {
        return this.orderByKeysTemplate;
    }
    int i = 0;
    int size = this.tableDefine().keys().size();
    StringBuilder select = new StringBuilder(" ORDER BY ");
    for (HugeKeys hugeKey : this.tableDefine().keys()) {
        String key = formatKey(hugeKey);
        select.append(key).append(" ");
        select.append("ASC ");
        if (++i != size) {
            select.append(", ");
        }
    }
    this.orderByKeysTemplate = select.toString();
    return this.orderByKeysTemplate;
}
Also used : HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 27 with HugeKeys

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

the class PostgresqlTable method buildInsertTemplateForce.

@Override
protected String buildInsertTemplateForce(MysqlBackendEntry.Row entry) {
    StringBuilder insert = new StringBuilder();
    insert.append("INSERT INTO ").append(this.table()).append(" (");
    int i = 0;
    int size = entry.columns().size();
    for (HugeKeys key : entry.columns().keySet()) {
        insert.append(formatKey(key));
        if (++i != size) {
            insert.append(", ");
        }
    }
    insert.append(") VALUES (");
    for (i = 0; i < size; i++) {
        insert.append("?");
        if (i != size - 1) {
            insert.append(", ");
        }
    }
    insert.append(")");
    i = 0;
    size = this.tableDefine().keys().size();
    insert.append(" ON CONFLICT (");
    for (HugeKeys key : this.tableDefine().keys()) {
        insert.append(formatKey(key));
        if (++i != size) {
            insert.append(", ");
        }
    }
    insert.append(")");
    i = 0;
    size = entry.columns().keySet().size();
    insert.append(" DO UPDATE SET ");
    for (HugeKeys key : entry.columns().keySet()) {
        insert.append(formatKey(key)).append(" = ?");
        if (++i != size) {
            insert.append(", ");
        }
    }
    return insert.toString();
}
Also used : HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 28 with HugeKeys

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

the class MysqlTable method createTable.

protected void createTable(Session session, TableDefine tableDefine) {
    StringBuilder sql = new StringBuilder();
    sql.append("CREATE TABLE IF NOT EXISTS ");
    sql.append(this.table()).append(" (");
    // Add columns
    for (Map.Entry<HugeKeys, String> entry : tableDefine.columns().entrySet()) {
        sql.append(formatKey(entry.getKey()));
        sql.append(" ");
        sql.append(entry.getValue());
        sql.append(", ");
    }
    // Specified primary keys
    sql.append(" PRIMARY KEY (");
    int i = 0;
    int size = tableDefine.keys().size();
    for (HugeKeys key : tableDefine.keys()) {
        sql.append(formatKey(key));
        if (++i != size) {
            sql.append(", ");
        }
    }
    sql.append("))");
    sql.append(this.engine(session));
    sql.append(";");
    LOG.debug("Create table: {}", sql);
    try {
        session.execute(sql.toString());
    } catch (SQLException e) {
        throw new BackendException("Failed to create table with '%s'", e, sql);
    }
}
Also used : SQLException(java.sql.SQLException) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Map(java.util.Map) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 29 with HugeKeys

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

the class MysqlTable method wrapPage.

protected void wrapPage(StringBuilder select, Query query, boolean scan) {
    String page = query.page();
    // It's the first time if page is empty
    if (!page.isEmpty()) {
        byte[] position = PageState.fromString(page).position();
        Map<HugeKeys, Object> columns = PagePosition.fromBytes(position).columns();
        List<HugeKeys> idColumnNames = this.idColumnName();
        List<Object> values = new ArrayList<>(idColumnNames.size());
        for (HugeKeys key : idColumnNames) {
            values.add(columns.get(key));
        }
        // Need add `where` to `select` when query is IdQuery
        boolean expectWhere = scan || query.conditionsSize() == 0;
        WhereBuilder where = this.newWhereBuilder(expectWhere);
        if (!expectWhere) {
            where.and();
        }
        where.gte(formatKeys(idColumnNames), values);
        select.append(where.build());
    }
}
Also used : ArrayList(java.util.ArrayList) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 30 with HugeKeys

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

the class MysqlTable method queryByRange.

protected StringBuilder queryByRange(ConditionQuery query, StringBuilder select) {
    E.checkArgument(query.relations().size() == 1, "Invalid scan with multi conditions: %s", query);
    Condition.Relation scan = query.relations().iterator().next();
    Shard shard = (Shard) scan.value();
    String page = query.page();
    if (MysqlShardSplitter.START.equals(shard.start()) && MysqlShardSplitter.END.equals(shard.end()) && (page == null || page.isEmpty())) {
        this.wrapLimit(select, query);
        return select;
    }
    HugeKeys partitionKey = this.idColumnName().get(0);
    if (page != null && !page.isEmpty()) {
        // >= page
        this.wrapPage(select, query, true);
        // < end
        WhereBuilder where = this.newWhereBuilder(false);
        if (!MysqlShardSplitter.END.equals(shard.end())) {
            where.and();
            where.lt(formatKey(partitionKey), shard.end());
        }
        select.append(where.build());
    } else {
        // >= start
        WhereBuilder where = this.newWhereBuilder();
        boolean hasStart = false;
        if (!MysqlShardSplitter.START.equals(shard.start())) {
            where.gte(formatKey(partitionKey), shard.start());
            hasStart = true;
        }
        // < end
        if (!MysqlShardSplitter.END.equals(shard.end())) {
            if (hasStart) {
                where.and();
            }
            where.lt(formatKey(partitionKey), shard.end());
        }
        select.append(where.build());
    }
    this.wrapLimit(select, query);
    return select;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) Shard(com.baidu.hugegraph.backend.store.Shard) 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