Search in sources :

Example 1 with HugeKeys

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

the class MysqlTable method buildInsertObjects.

protected List<Object> buildInsertObjects(MysqlBackendEntry.Row entry) {
    List<Object> objects = new ArrayList<>();
    for (Map.Entry<HugeKeys, Object> e : entry.columns().entrySet()) {
        Object value = e.getValue();
        String type = this.tableDefine().columns().get(e.getKey());
        if (type.startsWith(DECIMAL)) {
            value = new BigDecimal(value.toString());
        }
        objects.add(value);
    }
    return objects;
}
Also used : ArrayList(java.util.ArrayList) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 2 with HugeKeys

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

the class MysqlTable method delete.

@Override
public void delete(Session session, MysqlBackendEntry.Row entry) {
    List<HugeKeys> idNames = this.idColumnName();
    String template = this.buildDeleteTemplate(idNames);
    PreparedStatement deleteStmt;
    try {
        deleteStmt = session.prepareStatement(template);
        if (entry.columns().isEmpty()) {
            // Delete just by id
            List<Long> idValues = this.idColumnValue(entry);
            assert idNames.size() == idValues.size();
            for (int i = 0, n = idNames.size(); i < n; i++) {
                deleteStmt.setObject(i + 1, idValues.get(i));
            }
        } else {
            // Delete just by column keys(must be id columns)
            for (int i = 0, n = idNames.size(); i < n; i++) {
                HugeKeys key = idNames.get(i);
                Object value = entry.column(key);
                deleteStmt.setObject(i + 1, value);
            }
        }
    } catch (SQLException e) {
        throw new BackendException("Failed to prepare statement '%s'" + "with entry columns %s", template, entry.columns().values());
    }
    session.add(deleteStmt);
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 3 with HugeKeys

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

the class PaloTable method insert.

@Override
public void insert(MysqlSessions.Session session, MysqlBackendEntry.Row entry) {
    assert session instanceof PaloSessions.Session;
    PaloSessions.Session paloSession = (PaloSessions.Session) session;
    Set<HugeKeys> columnNames = this.tableDefine().columnNames();
    // Ensure column order match with table define
    List<Object> columnValues = new ArrayList<>(columnNames.size());
    for (HugeKeys key : columnNames) {
        columnValues.add(entry.column(key));
    }
    String insert = StringUtils.join(columnValues, "\t");
    paloSession.add(this.table(), insert);
}
Also used : ArrayList(java.util.ArrayList) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 4 with HugeKeys

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

the class PaloTable method createTable.

@Override
public void createTable(MysqlSessions.Session session, TableDefine tableDefine) {
    StringBuilder sql = new StringBuilder();
    sql.append("CREATE TABLE IF NOT EXISTS ");
    sql.append(this.table()).append(" (");
    // Add columns
    int i = 0;
    for (Map.Entry<HugeKeys, String> entry : tableDefine.columns().entrySet()) {
        sql.append(formatKey(entry.getKey()));
        sql.append(" ");
        sql.append(entry.getValue());
        if (++i != tableDefine.columns().size()) {
            sql.append(", ");
        }
    }
    sql.append(")");
    // Unique keys
    sql.append(" UNIQUE KEY(");
    i = 0;
    for (HugeKeys key : tableDefine.keys()) {
        sql.append(formatKey(key));
        if (++i != tableDefine.keys().size()) {
            sql.append(", ");
        }
    }
    sql.append(")");
    // Hash keys
    sql.append(" DISTRIBUTED BY HASH(");
    i = 0;
    for (HugeKeys key : tableDefine.keys()) {
        sql.append(formatKey(key));
        if (++i != tableDefine.keys().size()) {
            sql.append(", ");
        }
    }
    sql.append(");");
    // TODO: 'replication_num(default=3)’ can be a configuration
    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 5 with HugeKeys

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

the class CassandraTable method buildEliminate.

protected Update buildEliminate(CassandraBackendEntry.Row entry) {
    List<HugeKeys> idNames = this.idColumnName();
    List<HugeKeys> colNames = this.modifiableColumnName();
    Map<HugeKeys, Object> columns = entry.columns();
    // Update by id
    Update update = QueryBuilder.update(table());
    for (HugeKeys key : colNames) {
        /*
             * NOTE: eliminate from map<text, text> should just pass key,
             * if use the following statement:
             * UPDATE vertices SET PROPERTIES=PROPERTIES-{'city':'"Wuhan"'}
             * WHERE LABEL='person' AND PRIMARY_VALUES='josh';
             * it will throw a cassandra exception:
             * Invalid map literal for properties of typefrozen<set<text>>
             */
        if (!columns.containsKey(key)) {
            continue;
        }
        String name = formatKey(key);
        Object value = columns.get(key);
        if (value instanceof Map) {
            @SuppressWarnings("rawtypes") Set<?> keySet = ((Map) value).keySet();
            update.with(QueryBuilder.removeAll(name, keySet));
        } else if (value instanceof Set) {
            update.with(QueryBuilder.removeAll(name, (Set<?>) value));
        } else if (value instanceof List) {
            Set<?> keySet = new HashSet<>((List<?>) value);
            update.with(QueryBuilder.removeAll(name, keySet));
        } else {
            update.with(QueryBuilder.remove(name, value));
        }
    }
    for (HugeKeys idName : idNames) {
        assert columns.containsKey(idName);
        update.where(formatEQ(idName, columns.get(idName)));
    }
    return update;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(com.datastax.driver.core.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Update(com.datastax.driver.core.querybuilder.Update) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

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