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