Search in sources :

Example 16 with PropertyColumn

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

the class BaseSqlDialect method flushElementGlobalUniqueIndexPropertyCache.

private <T extends SqlgElement> void flushElementGlobalUniqueIndexPropertyCache(SqlgGraph sqlgGraph, boolean forVertices, Map<SchemaTable, Pair<SortedSet<String>, Map<T, Map<String, Object>>>> schemaVertexPropertyCache) {
    Connection conn = sqlgGraph.tx().getConnection();
    for (SchemaTable schemaTable : schemaVertexPropertyCache.keySet()) {
        Pair<SortedSet<String>, Map<T, Map<String, Object>>> vertexPropertyCache = schemaVertexPropertyCache.get(schemaTable);
        SortedSet<String> propertyNames = vertexPropertyCache.getKey();
        Map<String, PropertyColumn> globalUniqueIndexPropertyMap = sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(schemaTable.withPrefix(VERTEX_PREFIX));
        for (Map.Entry<String, PropertyColumn> propertyColumnEntry : globalUniqueIndexPropertyMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumnEntry.getValue();
            if (propertyNames.contains(propertyColumn.getName())) {
                for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                    StringBuilder sql = new StringBuilder();
                    sql.append("UPDATE ");
                    sql.append(maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
                    sql.append(".");
                    sql.append(maybeWrapInQoutes((forVertices ? VERTEX_PREFIX : EDGE_PREFIX) + globalUniqueIndex.getName()));
                    sql.append(" \nSET\n\t");
                    sql.append(maybeWrapInQoutes("value"));
                    sql.append(" = ?\nWHERE\n\t");
                    sql.append(maybeWrapInQoutes("recordId"));
                    sql.append(" = ? AND ");
                    sql.append(maybeWrapInQoutes("property"));
                    sql.append(" = ?");
                    if (needsSemicolon()) {
                        sql.append(";");
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug(sql.toString());
                    }
                    try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
                        boolean foundSomething = false;
                        for (T t : vertexPropertyCache.getRight().keySet()) {
                            Object value = vertexPropertyCache.getRight().get(t).get(propertyColumn.getName());
                            if (value != null) {
                                foundSomething = true;
                                SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, 1, preparedStatement, Collections.singleton(Pair.of(propertyColumn.getPropertyType(), value)));
                                preparedStatement.setString(2, t.id().toString());
                                preparedStatement.setString(3, propertyColumn.getName());
                                preparedStatement.addBatch();
                            }
                        }
                        if (foundSomething) {
                            preparedStatement.executeBatch();
                        }
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex)

Example 17 with PropertyColumn

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

the class TestLoadSchema method testLoadGlobalUniqueIndexes.

@Test
public void testLoadGlobalUniqueIndexes() throws Exception {
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("name1", PropertyType.STRING);
    properties.put("name2", PropertyType.STRING);
    VertexLabel aVertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", properties);
    Assert.assertTrue(aVertexLabel.isUncommitted());
    properties.clear();
    properties.put("name3", PropertyType.STRING);
    properties.put("name4", PropertyType.STRING);
    VertexLabel bVertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("B", properties);
    Assert.assertTrue(bVertexLabel.isUncommitted());
    properties.clear();
    properties.put("name5", PropertyType.STRING);
    properties.put("name6", PropertyType.STRING);
    EdgeLabel edgeLabel = aVertexLabel.ensureEdgeLabelExist("ab", bVertexLabel, properties);
    Assert.assertTrue(edgeLabel.isUncommitted());
    Set<PropertyColumn> globalUniqueIndexPropertyColumns = new HashSet<>();
    globalUniqueIndexPropertyColumns.addAll(new HashSet<>(aVertexLabel.getProperties().values()));
    globalUniqueIndexPropertyColumns.addAll(new HashSet<>(bVertexLabel.getProperties().values()));
    globalUniqueIndexPropertyColumns.addAll(new HashSet<>(edgeLabel.getProperties().values()));
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(globalUniqueIndexPropertyColumns);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph = SqlgGraph.open(configuration)) {
        assertEquals(1, sqlgGraph.getTopology().getGlobalUniqueIndexes().size());
        GlobalUniqueIndex globalUniqueIndex = sqlgGraph.getTopology().getGlobalUniqueIndexes().iterator().next();
        Assert.assertTrue(globalUniqueIndex.getProperties().containsAll(globalUniqueIndexPropertyColumns));
        for (PropertyColumn globalUniqueIndexPropertyColumn : globalUniqueIndexPropertyColumns) {
            assertEquals(1, globalUniqueIndexPropertyColumn.getGlobalUniqueIndices().size());
        }
    }
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 18 with PropertyColumn

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

the class TestSchemaEagerCreation method testVertexLabelPropertiesViaVertexLabel.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testVertexLabelPropertiesViaVertexLabel() {
    Schema schema = this.sqlgGraph.getTopology().getPublicSchema();
    this.sqlgGraph.addVertex(T.label, "Person");
    Optional<VertexLabel> vertexLabel = schema.getVertexLabel("Person");
    assertTrue(vertexLabel.isPresent());
    this.sqlgGraph.tx().rollback();
    vertexLabel = schema.getVertexLabel("Person");
    assertFalse(vertexLabel.isPresent());
    this.sqlgGraph.addVertex(T.label, "Person");
    vertexLabel = schema.getVertexLabel("Person");
    assertTrue(vertexLabel.isPresent());
    this.sqlgGraph.tx().commit();
    vertexLabel = schema.getVertexLabel("Person");
    assertTrue(vertexLabel.isPresent());
    vertexLabel = schema.getVertexLabel("Person");
    assertTrue(vertexLabel.isPresent());
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("name", PropertyType.STRING);
    properties.put("age", PropertyType.INTEGER);
    vertexLabel.get().ensurePropertiesExist(properties);
    assertEquals(2, vertexLabel.get().getProperties().size());
    this.sqlgGraph.tx().rollback();
    assertEquals(0, vertexLabel.get().getProperties().size());
    vertexLabel.get().ensurePropertiesExist(properties);
    this.sqlgGraph.tx().commit();
    assertEquals(2, vertexLabel.get().getProperties().size());
    PropertyColumn propertyColumnName = vertexLabel.get().getProperties().get("name");
    PropertyColumn propertyColumnAge = vertexLabel.get().getProperties().get("age");
    assertNotNull(propertyColumnName);
    assertNotNull(propertyColumnAge);
    assertEquals(PropertyType.STRING, propertyColumnName.getPropertyType());
    assertEquals(PropertyType.INTEGER, propertyColumnAge.getPropertyType());
}
Also used : HashMap(java.util.HashMap) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Schema(org.umlg.sqlg.structure.topology.Schema) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 19 with PropertyColumn

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

the class TestBatchGlobalUniqueIndexes method testGlobalUniqueIndexOnEdgeNormalBatchMode.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testGlobalUniqueIndexOnEdgeNormalBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("name", PropertyType.STRING);
    VertexLabel vertexLabelA = this.sqlgGraph.getTopology().ensureVertexLabelExist("A", properties);
    VertexLabel vertexLabelB = this.sqlgGraph.getTopology().ensureVertexLabelExist("B", properties);
    properties.clear();
    properties.put("namea", PropertyType.STRING);
    properties.put("nameb", PropertyType.STRING);
    properties.put("namec", PropertyType.STRING);
    vertexLabelA.ensureEdgeLabelExist("ab", vertexLabelB, properties);
    @SuppressWarnings("OptionalGetWithoutIsPresent") Collection<PropertyColumn> propertyColumns = this.sqlgGraph.getTopology().getPublicSchema().getEdgeLabel("ab").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("ab_namea_ab_nameb_ab_namec");
    Assert.assertTrue(globalUniqueIndexOptional.isPresent());
    Optional<PropertyColumn> nameaPropertyColumnOptional = this.sqlgGraph.getTopology().getPublicSchema().getEdgeLabel("ab").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("ab_namea_ab_nameb_ab_namec", globalUniqueIndex.getName());
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
    Edge edge = a1.addEdge("ab", b1, "namea", "a", "nameb", "b", "namec", "c");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    try {
        a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
        b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
        a1.addEdge("ab", b1, "namea", "a", "nameb", "b", "namec", "c");
        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();
    testGlobalUniqueIndexOnEdgeNormalBatchMode_assert(this.sqlgGraph, globalUniqueIndex, edge);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testGlobalUniqueIndexOnEdgeNormalBatchMode_assert(this.sqlgGraph1, globalUniqueIndex, edge);
    }
}
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) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 20 with PropertyColumn

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

the class TestBatchGlobalUniqueIndexes method testVertexUniqueConstraintDeleteBatchMode.

@Test
public void testVertexUniqueConstraintDeleteBatchMode() 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();
    for (int i = 1; i < 1001; i++) {
        Vertex a = this.sqlgGraph.addVertex(T.label, "A", "namea", "a" + i);
    }
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(3000, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());
    try {
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
        Assert.fail("GlobalUniqueIndex should prevent this form happening");
    } catch (Exception e) {
        // swallow
        this.sqlgGraph.tx().rollback();
    }
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.traversal().V().hasLabel("A").has("namea", "a1").next();
    a1.remove();
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(2997, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());
    this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
    // this time it passes.
    this.sqlgGraph.tx().commit();
    testVertexUniqueConstraintDeleteBatchMode_assert(this.sqlgGraph);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testVertexUniqueConstraintDeleteBatchMode_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)

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