use of org.umlg.sqlg.structure.topology.Schema in project sqlg by pietermartin.
the class SchemaTableTree method hasNoEdgeLabels.
private boolean hasNoEdgeLabels(SchemaTable schemaTable) {
Optional<Schema> schemaOptional = sqlgGraph.getTopology().getSchema(schemaTable.getSchema());
Preconditions.checkState(schemaOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.getSchema());
Schema schema = schemaOptional.get();
boolean result = true;
if (schemaTable.isVertexTable()) {
// Need to delete any in/out edges.
Optional<VertexLabel> vertexLabelOptional = schema.getVertexLabel(schemaTable.withOutPrefix().getTable());
Preconditions.checkState(vertexLabelOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.withOutPrefix().getTable());
VertexLabel vertexLabel = vertexLabelOptional.get();
Collection<EdgeLabel> outEdgeLabels = vertexLabel.getOutEdgeLabels().values();
Collection<EdgeLabel> inEdgeLabels = vertexLabel.getInEdgeLabels().values();
result = outEdgeLabels.isEmpty() && inEdgeLabels.isEmpty();
}
return result;
}
use of org.umlg.sqlg.structure.topology.Schema 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.Schema 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.Schema in project sqlg by pietermartin.
the class TestTopologyDeleteSpecific method testRemoveSchemaWithCrossEdges.
@Test
public void testRemoveSchemaWithCrossEdges() throws Exception {
Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsDistribution());
Configuration c = getConfigurationClone();
c.setProperty(SqlgGraph.DISTRIBUTED, true);
sqlgGraph = SqlgGraph.open(c);
String schema1 = "willDelete1";
Vertex v1 = sqlgGraph.addVertex(T.label, schema1 + ".t1", "name", "n1", "hello", "world");
String schema2 = "willDelete2";
Vertex v2 = sqlgGraph.addVertex(T.label, schema2 + ".t2", "name", "n2", "hello", "world");
Vertex v3 = sqlgGraph.addVertex(T.label, schema2 + ".t3", "name", "n3", "hello", "world");
Vertex v4 = sqlgGraph.addVertex(T.label, schema2 + ".t4", "name", "n4", "hello", "world");
v1.addEdge("e1", v3, "me", "again");
v2.addEdge("e1", v3, "me", "again");
v1.addEdge("e1", v4, "me", "again");
v2.addEdge("e1", v4, "me", "again");
sqlgGraph.tx().commit();
assertTrue(sqlgGraph.getTopology().getSchema(schema1).isPresent());
assertTrue(sqlgGraph.getTopology().getSchema(schema2).isPresent());
sqlgGraph.getTopology().getSchema(schema1).ifPresent((Schema s) -> s.remove(false));
sqlgGraph.tx().commit();
assertFalse(sqlgGraph.getTopology().getSchema(schema1).isPresent());
// this used to fail
sqlgGraph.getTopology().getSchema(schema2).ifPresent((Schema s) -> s.remove(false));
sqlgGraph.tx().commit();
assertFalse(sqlgGraph.getTopology().getSchema(schema2).isPresent());
}
use of org.umlg.sqlg.structure.topology.Schema 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);
}
}
Aggregations