Search in sources :

Example 1 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushRemovedGlobalUniqueIndexVertices.

@Override
public void flushRemovedGlobalUniqueIndexVertices(SqlgGraph sqlgGraph, Map<SchemaTable, List<SqlgVertex>> removeVertexCache) {
    if (!removeVertexCache.isEmpty()) {
        // split the list of vertices, postgres existVertexLabel a 2 byte limit in the in clause
        for (Map.Entry<SchemaTable, List<SqlgVertex>> schemaVertices : removeVertexCache.entrySet()) {
            SchemaTable schemaTable = schemaVertices.getKey();
            Map<String, PropertyColumn> propertyColumns = sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(schemaTable.withPrefix(VERTEX_PREFIX));
            for (PropertyColumn propertyColumn : propertyColumns.values()) {
                for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                    List<SqlgVertex> vertices = schemaVertices.getValue();
                    SecureRandom random = new SecureRandom();
                    byte[] bytes = new byte[6];
                    random.nextBytes(bytes);
                    String tmpTableIdentified = Base64.getEncoder().encodeToString(bytes);
                    StringBuilder createTmpTableStatement = new StringBuilder(createTemporaryTableStatement());
                    createTmpTableStatement.append(maybeWrapInQoutes(tmpTableIdentified));
                    createTmpTableStatement.append("(");
                    createTmpTableStatement.append(maybeWrapInQoutes("recordId"));
                    createTmpTableStatement.append(" ");
                    createTmpTableStatement.append(propertyTypeToSqlDefinition(PropertyType.STRING)[0]);
                    createTmpTableStatement.append(", ");
                    createTmpTableStatement.append(maybeWrapInQoutes("property"));
                    createTmpTableStatement.append(" ");
                    createTmpTableStatement.append(propertyTypeToSqlDefinition(PropertyType.STRING)[0]);
                    createTmpTableStatement.append(")");
                    if (needsSemicolon()) {
                        createTmpTableStatement.append(";");
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug(createTmpTableStatement.toString());
                    }
                    Connection conn = sqlgGraph.tx().getConnection();
                    try (Statement statement = conn.createStatement()) {
                        statement.execute(createTmpTableStatement.toString());
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    StringBuilder sql = new StringBuilder("INSERT INTO ");
                    if (needsTemporaryTableSchema()) {
                        sql.append(maybeWrapInQoutes(getPublicSchema()));
                        sql.append(".");
                    }
                    sql.append(maybeWrapInQoutes(tmpTableIdentified));
                    sql.append(" (");
                    sql.append(maybeWrapInQoutes("recordId"));
                    sql.append(", ");
                    sql.append(maybeWrapInQoutes("property"));
                    sql.append(") VALUES (?, ?)");
                    if (needsSemicolon()) {
                        sql.append(";");
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug(sql.toString());
                    }
                    try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
                        for (SqlgVertex vertex : vertices) {
                            preparedStatement.setString(1, vertex.id().toString());
                            preparedStatement.setString(2, propertyColumn.getName());
                            preparedStatement.addBatch();
                        }
                        preparedStatement.executeBatch();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    sql = new StringBuilder("DELETE FROM\n\t");
                    sql.append(maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
                    sql.append(".");
                    sql.append(maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
                    sql.append("\nWHERE\n\t");
                    sql.append("CONCAT(");
                    sql.append(maybeWrapInQoutes("recordId"));
                    sql.append(", ");
                    sql.append(maybeWrapInQoutes("property"));
                    sql.append(") IN (\n");
                    sql.append("SELECT\n\tCONCAT(");
                    sql.append(maybeWrapInQoutes("recordId"));
                    sql.append(", ");
                    sql.append(maybeWrapInQoutes("property"));
                    sql.append(")\nFROM\n\t");
                    if (needsTemporaryTableSchema()) {
                        sql.append(maybeWrapInQoutes(sqlgGraph.getSqlDialect().getPublicSchema()));
                        sql.append(".");
                    }
                    sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(tmpTableIdentified));
                    sql.append(")");
                    if (sqlgGraph.getSqlDialect().needsSemicolon()) {
                        sql.append(";");
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug(sql.toString());
                    }
                    conn = sqlgGraph.tx().getConnection();
                    try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
                        preparedStatement.executeUpdate();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) SecureRandom(java.security.SecureRandom) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex)

Example 2 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushVertexPropertyCache.

@Override
public void flushVertexPropertyCache(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> vertexPropertyCache) {
    for (Map.Entry<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> entry : vertexPropertyCache.entrySet()) {
        SchemaTable schemaTable = entry.getKey();
        Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices = entry.getValue();
        SortedSet<String> columns = vertices.getLeft();
        Map<SqlgVertex, Map<String, Object>> rows = vertices.getRight();
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE ");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(schemaTable.getSchema()));
        sql.append(".");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + schemaTable.getTable()));
        sql.append(" SET ");
        Map<String, PropertyColumn> propertyColumns = sqlgGraph.getTopology().getSchema(schemaTable.getSchema()).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", schemaTable.getSchema()))).getVertexLabel(schemaTable.getTable()).orElseThrow(() -> new IllegalStateException(String.format("VertexLabel %s not found", schemaTable.getTable()))).getProperties();
        if (!columns.isEmpty()) {
            Map<String, PropertyType> propertyTypeMap = new HashMap<>();
            for (String column : columns) {
                PropertyColumn propertyColumn = propertyColumns.get(column);
                propertyTypeMap.put(column, propertyColumn.getPropertyType());
            }
            sql.append(" ");
            int i = 1;
            // noinspection Duplicates
            for (String column : columns) {
                PropertyType propertyType = propertyTypeMap.get(column);
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count > 1) {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column + propertyType.getPostFixes()[count - 2]));
                        sql.append(" = ?");
                    } else {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column));
                        sql.append(" = ?");
                    }
                    if (count++ < sqlDefinitions.length) {
                        sql.append(",");
                    }
                }
                if (i++ < columns.size()) {
                    sql.append(", ");
                }
            }
        }
        sql.append(" WHERE ");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Topology.ID));
        sql.append(" = ?");
        if (sqlgGraph.getSqlDialect().needsSemicolon()) {
            sql.append(";");
        }
        if (logger.isDebugEnabled()) {
            logger.debug(sql.toString());
        }
        Connection conn = sqlgGraph.tx().getConnection();
        try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
            for (Map.Entry<SqlgVertex, Map<String, Object>> rowEntry : rows.entrySet()) {
                int i = 1;
                SqlgVertex sqlgVertex = rowEntry.getKey();
                if (!columns.isEmpty()) {
                    Map<String, Object> parameterValueMap = rowEntry.getValue();
                    List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                    for (String column : columns) {
                        PropertyColumn propertyColumn = propertyColumns.get(column);
                        Object value = parameterValueMap.get(column);
                        if (value == null) {
                            // if the value is not present update it to what is currently is.
                            if (sqlgVertex.property(column).isPresent()) {
                                value = sqlgVertex.value(column);
                            } else {
                                value = null;
                            }
                        }
                        typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
                    }
                    i = SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, i, preparedStatement, typeAndValues);
                    preparedStatement.setLong(i, ((RecordId) sqlgVertex.id()).getId());
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushEdgeGlobalUniqueIndexes.

@Override
public void flushEdgeGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<MetaEdge, Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>>> edgeCache) {
    for (MetaEdge metaEdge : edgeCache.keySet()) {
        Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>> triples = edgeCache.get(metaEdge);
        Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> edgeMap = triples.getRight();
        Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(metaEdge.getSchemaTable().withPrefix(EDGE_PREFIX));
        Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rows = triples.getRight();
        for (Map.Entry<String, PropertyColumn> propertyColumnEntry : propertyColumnMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumnEntry.getValue();
            for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                StringBuilder sql = new StringBuilder();
                sql.append("INSERT INTO ");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
                sql.append(".");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
                sql.append(" (");
                PropertyType propertyType = propertyColumn.getPropertyType();
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count++ > 1) {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE + propertyType.getPostFixes()[count - 2]));
                    } else {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE));
                    }
                    sql.append(",");
                }
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID));
                sql.append(",");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME));
                sql.append(") VALUES ( ");
                count = 1;
                // noinspection Duplicates
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count++ > 1) {
                        sql.append("?");
                    } else {
                        sql.append("?");
                    }
                    sql.append(", ");
                }
                sql.append("?, ?)");
                if (sqlgGraph.getSqlDialect().needsSemicolon()) {
                    sql.append(";");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug(sql.toString());
                }
                Connection conn = sqlgGraph.tx().getConnection();
                try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
                    for (Map.Entry<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rowEntry : rows.entrySet()) {
                        SqlgEdge sqlgEdge = rowEntry.getKey();
                        Map<String, Object> parameterValueMap = rowEntry.getValue().getRight();
                        Object value = parameterValueMap.get(propertyColumn.getName());
                        List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                        typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
                        typeAndValues.add(Pair.of(PropertyType.STRING, sqlgEdge.id().toString()));
                        typeAndValues.add(Pair.of(PropertyType.STRING, propertyColumn.getName()));
                        SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, 1, preparedStatement, typeAndValues);
                        preparedStatement.addBatch();
                    }
                    preparedStatement.executeBatch();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair) Triple(org.apache.commons.lang3.tuple.Triple) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex)

Example 4 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushVertexGlobalUniqueIndexes.

@Override
public void flushVertexGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> vertexCache) {
    for (SchemaTable schemaTable : vertexCache.keySet()) {
        Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices = vertexCache.get(schemaTable);
        Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(schemaTable.withPrefix(VERTEX_PREFIX));
        for (Map.Entry<String, PropertyColumn> propertyColumnEntry : propertyColumnMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumnEntry.getValue();
            for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                Map<SqlgVertex, Map<String, Object>> rows = vertices.getRight();
                StringBuilder sql = new StringBuilder();
                sql.append("INSERT INTO ");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
                sql.append(".");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
                sql.append(" (");
                PropertyType propertyType = propertyColumn.getPropertyType();
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count++ > 1) {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE + propertyType.getPostFixes()[count - 2]));
                    } else {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE));
                    }
                    sql.append(",");
                }
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID));
                sql.append(",");
                sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME));
                sql.append(") VALUES ( ");
                count = 1;
                // noinspection Duplicates
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count++ > 1) {
                        sql.append("?");
                    } else {
                        sql.append("?");
                    }
                    sql.append(", ");
                }
                sql.append("?, ?)");
                if (sqlgGraph.getSqlDialect().needsSemicolon()) {
                    sql.append(";");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug(sql.toString());
                }
                Connection conn = sqlgGraph.tx().getConnection();
                try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
                    for (Map.Entry<SqlgVertex, Map<String, Object>> rowEntry : rows.entrySet()) {
                        SqlgVertex sqlgVertex = rowEntry.getKey();
                        Map<String, Object> parameterValueMap = rowEntry.getValue();
                        Object value = parameterValueMap.get(propertyColumn.getName());
                        List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                        typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
                        typeAndValues.add(Pair.of(PropertyType.STRING, sqlgVertex.id().toString()));
                        typeAndValues.add(Pair.of(PropertyType.STRING, propertyColumn.getName()));
                        SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, 1, preparedStatement, typeAndValues);
                        preparedStatement.addBatch();
                    }
                    preparedStatement.executeBatch();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex)

Example 5 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class SqlgGraph method createVertexLabeledIndex.

/**
 * @deprecated Please use {@link Topology#ensureVertexLabelExist(String, Map)} and {@link VertexLabel#ensureIndexExists(IndexType, List)}.
 */
@Deprecated
public void createVertexLabeledIndex(String label, Object... dummykeyValues) {
    Map<String, PropertyType> columns = SqlgUtil.transformToColumnDefinitionMap(dummykeyValues);
    SchemaTable schemaTablePair = SchemaTable.from(this, label);
    VertexLabel vertexLabel = this.getTopology().ensureVertexLabelExist(schemaTablePair.getSchema(), schemaTablePair.getTable(), columns);
    List<PropertyColumn> properties = new ArrayList<>();
    List<String> keys = SqlgUtil.transformToKeyList(dummykeyValues);
    for (String key : keys) {
        properties.add(vertexLabel.getProperty(key).get());
    }
    vertexLabel.ensureIndexExists(IndexType.NON_UNIQUE, properties);
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn)

Aggregations

PropertyColumn (org.umlg.sqlg.structure.topology.PropertyColumn)23 GlobalUniqueIndex (org.umlg.sqlg.structure.topology.GlobalUniqueIndex)10 VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)8 Pair (org.apache.commons.lang3.tuple.Pair)7 Test (org.junit.Test)7 BaseTest (org.umlg.sqlg.test.BaseTest)7 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)5 PropertyVetoException (java.beans.PropertyVetoException)4 IOException (java.io.IOException)4 Schema (org.umlg.sqlg.structure.topology.Schema)3 Triple (org.apache.commons.lang3.tuple.Triple)2 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)2 SecureRandom (java.security.SecureRandom)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Edge (org.apache.tinkerpop.gremlin.structure.Edge)1 PropertyType (org.umlg.sqlg.structure.PropertyType)1