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;
}
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());
}
}
}
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());
}
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());
}
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);
}
Aggregations