Search in sources :

Example 6 with PropertyColumn

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

the class SqlgEdge method internalAddEdge.

private void internalAddEdge(Map<String, Object> keyValueMap) throws SQLException {
    StringBuilder sql = new StringBuilder("INSERT INTO ");
    sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.schema));
    sql.append(".");
    sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(EDGE_PREFIX + this.table));
    sql.append(" (");
    Map<String, Pair<PropertyType, Object>> propertyTypeValueMap = new HashMap<>();
    Map<String, PropertyColumn> propertyColumns = null;
    if (!keyValueMap.isEmpty()) {
        propertyColumns = this.sqlgGraph.getTopology().getSchema(this.schema).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", this.schema))).getEdgeLabel(this.table).orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel %s not found in schema %s", this.table, this.schema))).getProperties();
        // sync up the keyValueMap with its PropertyColumn
        for (Map.Entry<String, Object> keyValueEntry : keyValueMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumns.get(keyValueEntry.getKey());
            Pair<PropertyType, Object> propertyColumnObjectPair = Pair.of(propertyColumn.getPropertyType(), keyValueEntry.getValue());
            propertyTypeValueMap.put(keyValueEntry.getKey(), propertyColumnObjectPair);
        }
    }
    writeColumnNames(propertyTypeValueMap, sql);
    if (keyValueMap.size() > 0) {
        sql.append(", ");
    }
    sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.inVertex.schema + "." + this.inVertex.table + Topology.IN_VERTEX_COLUMN_END));
    sql.append(", ");
    sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.outVertex.schema + "." + this.outVertex.table + Topology.OUT_VERTEX_COLUMN_END));
    sql.append(") VALUES (");
    writeColumnParameters(propertyTypeValueMap, sql);
    if (keyValueMap.size() > 0) {
        sql.append(", ");
    }
    sql.append("?, ?");
    sql.append(")");
    if (this.sqlgGraph.getSqlDialect().needsSemicolon()) {
        sql.append(";");
    }
    if (logger.isDebugEnabled()) {
        logger.debug(sql.toString());
    }
    int i = 1;
    Connection conn = this.sqlgGraph.tx().getConnection();
    try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS)) {
        i = SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(this.sqlgGraph, i, preparedStatement, propertyTypeValueMap);
        preparedStatement.setLong(i++, this.inVertex.recordId.getId());
        preparedStatement.setLong(i, this.outVertex.recordId.getId());
        preparedStatement.executeUpdate();
        ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
        if (generatedKeys.next()) {
            this.recordId = RecordId.from(SchemaTable.of(this.schema, this.table), generatedKeys.getLong(1));
        } else {
            throw new RuntimeException("Could not retrieve the id after an insert into " + Topology.VERTICES);
        }
        if (!keyValueMap.isEmpty()) {
            insertGlobalUniqueIndex(keyValueMap, propertyColumns);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair)

Example 7 with PropertyColumn

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

the class SqlgElement method insertGlobalUniqueIndex.

protected void insertGlobalUniqueIndex(Map<String, Object> keyValueMap, Map<String, PropertyColumn> propertyColumns) {
    for (PropertyColumn propertyColumn : propertyColumns.values()) {
        for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
            Object value = keyValueMap.get(propertyColumn.getName());
            Pair<PropertyColumn, Object> propertyColumnObjectPair = Pair.of(propertyColumn, value);
            this.insertGlobalUniqueIndex(this.sqlgGraph, globalUniqueIndex, propertyColumnObjectPair);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex)

Example 8 with PropertyColumn

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

the class TestBatchGlobalUniqueIndexes method testVertexUniqueConstraintUpdateNormalBatchMode.

@Test
public void testVertexUniqueConstraintUpdateNormalBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    VertexLabel vertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", new HashMap<String, PropertyType>() {

        {
            put("namea", PropertyType.STRING);
            put("nameb", PropertyType.STRING);
            put("namec", PropertyType.STRING);
        }
    });
    Set<PropertyColumn> properties = new HashSet<>(vertexLabel.getProperties().values());
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(properties);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    a1.property("namea", "aa");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    try {
        // this should pass
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
        this.sqlgGraph.tx().commit();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("GlobalUniqueIndex should not fire");
    }
    testVertexUniqueConstraintUpdateNormalBatchMode_assert(this.sqlgGraph);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testVertexUniqueConstraintUpdateNormalBatchMode_assert(this.sqlgGraph1);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 9 with PropertyColumn

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

the class TestBatchGlobalUniqueIndexes method testGlobalUniqueIndexOnVertexNormalBatchMode.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testGlobalUniqueIndexOnVertexNormalBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("namec", PropertyType.STRING);
    properties.put("namea", PropertyType.STRING);
    properties.put("nameb", PropertyType.STRING);
    this.sqlgGraph.getTopology().ensureVertexLabelExist("A", properties);
    @SuppressWarnings("OptionalGetWithoutIsPresent") Collection<PropertyColumn> propertyColumns = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").get().getProperties().values();
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(new HashSet<>(propertyColumns));
    this.sqlgGraph.tx().commit();
    Schema globalUniqueIndexSchema = this.sqlgGraph.getTopology().getGlobalUniqueIndexSchema();
    Optional<GlobalUniqueIndex> globalUniqueIndexOptional = globalUniqueIndexSchema.getGlobalUniqueIndex("A_namea_A_nameb_A_namec");
    Assert.assertTrue(globalUniqueIndexOptional.isPresent());
    Optional<PropertyColumn> nameaPropertyColumnOptional = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").get().getProperty("namea");
    Assert.assertTrue(nameaPropertyColumnOptional.isPresent());
    @SuppressWarnings("OptionalGetWithoutIsPresent") Set<GlobalUniqueIndex> globalUniqueIndices = nameaPropertyColumnOptional.get().getGlobalUniqueIndices();
    Assert.assertEquals(1, globalUniqueIndices.size());
    GlobalUniqueIndex globalUniqueIndex = globalUniqueIndices.iterator().next();
    Assert.assertEquals("A_namea_A_nameb_A_namec", globalUniqueIndex.getName());
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a = this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
    this.sqlgGraph.tx().commit();
    try {
        this.sqlgGraph.tx().normalBatchModeOn();
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
        this.sqlgGraph.tx().commit();
        Assert.fail("GlobalUniqueIndex should prevent this from executing");
    } catch (Exception e) {
    // swallow
    }
    this.sqlgGraph.tx().rollback();
    this.sqlgGraph.tx().normalBatchModeOn();
    this.sqlgGraph.addVertex(T.label, "A", "namea", "aa");
    this.sqlgGraph.tx().commit();
    testGlobalUniqueIndexOnVertexNormalBatchMode_assert(this.sqlgGraph, globalUniqueIndex, a);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testGlobalUniqueIndexOnVertexNormalBatchMode_assert(this.sqlgGraph1, globalUniqueIndex, a);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Schema(org.umlg.sqlg.structure.topology.Schema) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 10 with PropertyColumn

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

the class SQLServerBaseCacheBulkRecord method addMetaData.

protected int addMetaData(SQLServerBulkCopy bulkCopy, SqlgGraph sqlgGraph) throws SQLServerException {
    int i = 1;
    for (String column : this.columns) {
        PropertyType propertyType;
        if (this.propertyColumns != null) {
            PropertyColumn propertyColumn = propertyColumns.get(column);
            propertyType = propertyColumn.getPropertyType();
        } else {
            propertyType = this.properties.get(column);
        }
        switch(propertyType) {
            case BOOLEAN:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case BYTE:
            case SHORT:
            case INTEGER:
            case LONG:
            case FLOAT:
            case DOUBLE:
            case JSON:
            case STRING:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case LOCALDATE:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case LOCALDATETIME:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case LOCALTIME:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case ZONEDDATETIME:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.ZONEDDATETIME)[0], 0, 0, null, PropertyType.LOCALDATETIME));
                bulkCopy.addColumnMapping(i, column + PropertyType.ZONEDDATETIME.getPostFixes()[0]);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.ZONEDDATETIME)[0], 0, 0, null, PropertyType.STRING));
                break;
            case PERIOD:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.PERIOD)[0], 0, 0, null, PropertyType.INTEGER));
                bulkCopy.addColumnMapping(i, column + PropertyType.PERIOD.getPostFixes()[0]);
                this.columnMetadata.put(i++, new ColumnMetadata(column + PropertyType.PERIOD.getPostFixes()[0], sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.PERIOD)[1], 0, 0, null, PropertyType.INTEGER));
                bulkCopy.addColumnMapping(i, column + PropertyType.PERIOD.getPostFixes()[1]);
                this.columnMetadata.put(i++, new ColumnMetadata(column + PropertyType.PERIOD.getPostFixes()[1], sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.PERIOD)[2], 0, 0, null, PropertyType.INTEGER));
                break;
            case DURATION:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.DURATION)[0], 0, 0, null, PropertyType.LONG));
                bulkCopy.addColumnMapping(i, column + PropertyType.DURATION.getPostFixes()[0]);
                this.columnMetadata.put(i++, new ColumnMetadata(column + PropertyType.DURATION.getPostFixes()[0], sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.DURATION)[1], 0, 0, null, PropertyType.INTEGER));
                break;
            case byte_ARRAY:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 8000, 0, null, propertyType));
                break;
            case BYTE_ARRAY:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 8000, 0, null, propertyType));
                break;
            default:
                throw SqlgExceptions.invalidPropertyType(propertyType);
        }
    }
    return i;
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) PropertyType(org.umlg.sqlg.structure.PropertyType)

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