Search in sources :

Example 21 with VertexLabel

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

the class SqlDialect method drop.

/**
 * if the query traverses edges then the deletion logic is non trivial.
 * The edges can not be deleted upfront as then we will not be able to travers to the leaf vertices anymore
 * because the edges are no longer there to travers. In this case we need to drop foreign key constraint checking.
 * Delete the vertices and then the edges using the same query.
 * The edge query is the same as the vertex query with the last SchemaTableTree removed from the distinctQueryStack;
 *
 * @param sqlgGraph            The graph.
 * @param leafElementsToDelete The leaf elements of the query. eg. g.V().out().out() The last vertices returned by the gremlin query.
 * @param edgesToDelete
 * @param distinctQueryStack   The query's SchemaTableTree stack as constructed by parsing.
 * @return
 */
default List<Triple<SqlgSqlExecutor.DROP_QUERY, String, SchemaTable>> drop(SqlgGraph sqlgGraph, String leafElementsToDelete, Optional<String> edgesToDelete, LinkedList<SchemaTableTree> distinctQueryStack) {
    List<Triple<SqlgSqlExecutor.DROP_QUERY, String, SchemaTable>> sqls = new ArrayList<>();
    SchemaTableTree last = distinctQueryStack.getLast();
    SchemaTableTree lastEdge = null;
    // if the leaf elements are vertices then we need to delete its in and out edges.
    boolean isVertex = last.getSchemaTable().isVertexTable();
    VertexLabel lastVertexLabel = null;
    if (isVertex) {
        Optional<Schema> schemaOptional = sqlgGraph.getTopology().getSchema(last.getSchemaTable().getSchema());
        Preconditions.checkState(schemaOptional.isPresent(), "BUG: %s not found in the topology.", last.getSchemaTable().getSchema());
        Schema schema = schemaOptional.get();
        Optional<VertexLabel> vertexLabelOptional = schema.getVertexLabel(last.getSchemaTable().withOutPrefix().getTable());
        Preconditions.checkState(vertexLabelOptional.isPresent(), "BUG: %s not found in the topology.", last.getSchemaTable().withOutPrefix().getTable());
        lastVertexLabel = vertexLabelOptional.get();
    }
    boolean queryTraversesEdge = isVertex && (distinctQueryStack.size() > 1);
    EdgeLabel lastEdgeLabel = null;
    if (queryTraversesEdge) {
        lastEdge = distinctQueryStack.get(distinctQueryStack.size() - 2);
        Optional<Schema> edgeSchema = sqlgGraph.getTopology().getSchema(lastEdge.getSchemaTable().getSchema());
        Preconditions.checkState(edgeSchema.isPresent(), "BUG: %s not found in the topology.", lastEdge.getSchemaTable().getSchema());
        Optional<EdgeLabel> edgeLabelOptional = edgeSchema.get().getEdgeLabel(lastEdge.getSchemaTable().withOutPrefix().getTable());
        Preconditions.checkState(edgeLabelOptional.isPresent(), "BUG: %s not found in the topology.", lastEdge.getSchemaTable().getTable());
        lastEdgeLabel = edgeLabelOptional.get();
    }
    if (isVertex) {
        // First delete all edges except for this edge traversed to get to the vertices.
        StringBuilder sb;
        for (Map.Entry<String, EdgeLabel> edgeLabelEntry : lastVertexLabel.getOutEdgeLabels().entrySet()) {
            EdgeLabel edgeLabel = edgeLabelEntry.getValue();
            if (lastEdgeLabel == null || !edgeLabel.equals(lastEdgeLabel)) {
                // Delete
                sb = new StringBuilder();
                sb.append("DELETE FROM ");
                sb.append(maybeWrapInQoutes(edgeLabel.getSchema().getName()));
                sb.append(".");
                sb.append(maybeWrapInQoutes(Topology.EDGE_PREFIX + edgeLabel.getName()));
                sb.append("\nWHERE ");
                sb.append(maybeWrapInQoutes(lastVertexLabel.getSchema().getName() + "." + lastVertexLabel.getName() + Topology.OUT_VERTEX_COLUMN_END));
                sb.append(" IN\n\t(");
                sb.append(leafElementsToDelete);
                sb.append(")");
                sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.NORMAL, sb.toString(), SchemaTable.of(edgeLabel.getSchema().getName(), Topology.EDGE_PREFIX + edgeLabel.getName())));
            }
        }
        for (Map.Entry<String, EdgeLabel> edgeLabelEntry : lastVertexLabel.getInEdgeLabels().entrySet()) {
            EdgeLabel edgeLabel = edgeLabelEntry.getValue();
            if (lastEdgeLabel == null || !edgeLabel.equals(lastEdgeLabel)) {
                // Delete
                sb = new StringBuilder();
                sb.append("DELETE FROM ");
                sb.append(maybeWrapInQoutes(edgeLabel.getSchema().getName()));
                sb.append(".");
                sb.append(maybeWrapInQoutes(Topology.EDGE_PREFIX + edgeLabel.getName()));
                sb.append("\nWHERE ");
                sb.append(maybeWrapInQoutes(lastVertexLabel.getSchema().getName() + "." + lastVertexLabel.getName() + Topology.IN_VERTEX_COLUMN_END));
                sb.append(" IN\n\t(");
                sb.append(leafElementsToDelete);
                sb.append(")");
                sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.NORMAL, sb.toString(), SchemaTable.of(edgeLabel.getSchema().getName(), Topology.EDGE_PREFIX + edgeLabel.getName())));
            }
        }
    }
    // Need to defer foreign key constraint checks.
    if (queryTraversesEdge) {
        String edgeTableName = (maybeWrapInQoutes(lastEdge.getSchemaTable().getSchema())) + "." + maybeWrapInQoutes(lastEdge.getSchemaTable().getTable());
        sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.ALTER, this.sqlToTurnOffReferentialConstraintCheck(edgeTableName), lastEdge.getSchemaTable()));
    }
    // Delete the leaf vertices, if there are foreign keys then its been deferred.
    StringBuilder sb = new StringBuilder();
    sb.append("DELETE FROM ");
    sb.append(maybeWrapInQoutes(last.getSchemaTable().getSchema()));
    sb.append(".");
    sb.append(maybeWrapInQoutes(last.getSchemaTable().getTable()));
    sb.append("\nWHERE \"ID\" IN (\n\t");
    sb.append(leafElementsToDelete);
    sb.append(")");
    sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.NORMAL, sb.toString(), null));
    if (queryTraversesEdge) {
        sb = new StringBuilder();
        sb.append("DELETE FROM ");
        sb.append(maybeWrapInQoutes(lastEdge.getSchemaTable().getSchema()));
        sb.append(".");
        sb.append(maybeWrapInQoutes(lastEdge.getSchemaTable().getTable()));
        sb.append("\nWHERE \"ID\" IN (\n\t");
        sb.append(edgesToDelete.get());
        sb.append(")");
        sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.EDGE, sb.toString(), lastEdge.getSchemaTable()));
    }
    // Enable the foreign key constraint
    if (queryTraversesEdge) {
        String edgeTableName = (maybeWrapInQoutes(lastEdge.getSchemaTable().getSchema())) + "." + maybeWrapInQoutes(lastEdge.getSchemaTable().getTable());
        sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.ALTER, this.sqlToTurnOnReferentialConstraintCheck(edgeTableName), null));
    }
    return sqls;
}
Also used : Schema(org.umlg.sqlg.structure.topology.Schema) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) Triple(org.apache.commons.lang3.tuple.Triple) SqlgSqlExecutor(org.umlg.sqlg.strategy.SqlgSqlExecutor) SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel)

Example 22 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel 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 23 with VertexLabel

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

the class TestSchemaEagerCreation method testAddEdgeProperties.

@Test
public void testAddEdgeProperties() {
    VertexLabel outVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("A");
    VertexLabel inVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("B");
    this.sqlgGraph.getTopology().ensureEdgeLabelExist("ab", outVertexLabel, inVertexLabel, Collections.emptyMap());
    this.sqlgGraph.tx().commit();
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("name", PropertyType.STRING);
    properties.put("age", PropertyType.INTEGER);
    assertTrue(this.sqlgGraph.getTopology().getAllTables().entrySet().stream().allMatch((entry) -> entry.getValue().isEmpty()));
    assertTrue(this.sqlgGraph.getTopology().getAllTables().get(this.sqlgGraph.getSqlDialect().getPublicSchema() + ".E_ab").isEmpty());
    this.sqlgGraph.getTopology().ensureEdgePropertiesExist("ab", properties);
    this.sqlgGraph.tx().rollback();
    assertTrue(this.sqlgGraph.getTopology().getAllTables().get(this.sqlgGraph.getSqlDialect().getPublicSchema() + ".E_ab").isEmpty());
    this.sqlgGraph.getTopology().ensureEdgePropertiesExist("ab", properties);
    this.sqlgGraph.tx().commit();
    assertFalse(this.sqlgGraph.getTopology().getAllTables().get(this.sqlgGraph.getSqlDialect().getPublicSchema() + ".E_ab").isEmpty());
}
Also used : org.umlg.sqlg.structure(org.umlg.sqlg.structure) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) Schema(org.umlg.sqlg.structure.topology.Schema) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Test(org.junit.Test) HashMap(java.util.HashMap) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Map(java.util.Map) Optional(java.util.Optional) Assume(org.junit.Assume) Assert(org.junit.Assert) Collections(java.util.Collections) Before(org.junit.Before) HashMap(java.util.HashMap) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 24 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel 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 25 with VertexLabel

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

the class TestSchemaEagerCreation method createModernSchema.

private void createModernSchema() {
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("name", PropertyType.STRING);
    properties.put("age", PropertyType.INTEGER);
    VertexLabel personVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("person", properties);
    properties.remove("age");
    properties.put("name", PropertyType.STRING);
    properties.put("lang", PropertyType.STRING);
    VertexLabel softwareVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("software", properties);
    properties.clear();
    properties.put("weight", PropertyType.DOUBLE);
    this.sqlgGraph.getTopology().ensureEdgeLabelExist("knows", personVertexLabel, personVertexLabel, properties);
    this.sqlgGraph.getTopology().ensureEdgeLabelExist("created", personVertexLabel, softwareVertexLabel, properties);
}
Also used : HashMap(java.util.HashMap) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel)

Aggregations

VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)31 BaseTest (org.umlg.sqlg.test.BaseTest)23 Test (org.junit.Test)22 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)17 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)11 PropertyColumn (org.umlg.sqlg.structure.topology.PropertyColumn)9 Schema (org.umlg.sqlg.structure.topology.Schema)7 PropertyVetoException (java.beans.PropertyVetoException)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 PropertyType (org.umlg.sqlg.structure.PropertyType)3 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 SqlgSqlExecutor (org.umlg.sqlg.strategy.SqlgSqlExecutor)2 org.umlg.sqlg.structure (org.umlg.sqlg.structure)2 GlobalUniqueIndex (org.umlg.sqlg.structure.topology.GlobalUniqueIndex)2 Preconditions (com.google.common.base.Preconditions)1 java.util (java.util)1 Collections (java.util.Collections)1 Map (java.util.Map)1