use of org.umlg.sqlg.structure.topology.GlobalUniqueIndex in project sqlg by pietermartin.
the class BaseSqlDialect method flushRemovedGlobalUniqueIndexVertices.
@Override
public void flushRemovedGlobalUniqueIndexVertices(SqlgGraph sqlgGraph, Map<SchemaTable, List<SqlgVertex>> removeVertexCache) {
if (!removeVertexCache.isEmpty()) {
// split the list of vertices, postgres existVertexLabel a 2 byte limit in the in clause
for (Map.Entry<SchemaTable, List<SqlgVertex>> schemaVertices : removeVertexCache.entrySet()) {
SchemaTable schemaTable = schemaVertices.getKey();
Map<String, PropertyColumn> propertyColumns = sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(schemaTable.withPrefix(VERTEX_PREFIX));
for (PropertyColumn propertyColumn : propertyColumns.values()) {
for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
List<SqlgVertex> vertices = schemaVertices.getValue();
SecureRandom random = new SecureRandom();
byte[] bytes = new byte[6];
random.nextBytes(bytes);
String tmpTableIdentified = Base64.getEncoder().encodeToString(bytes);
StringBuilder createTmpTableStatement = new StringBuilder(createTemporaryTableStatement());
createTmpTableStatement.append(maybeWrapInQoutes(tmpTableIdentified));
createTmpTableStatement.append("(");
createTmpTableStatement.append(maybeWrapInQoutes("recordId"));
createTmpTableStatement.append(" ");
createTmpTableStatement.append(propertyTypeToSqlDefinition(PropertyType.STRING)[0]);
createTmpTableStatement.append(", ");
createTmpTableStatement.append(maybeWrapInQoutes("property"));
createTmpTableStatement.append(" ");
createTmpTableStatement.append(propertyTypeToSqlDefinition(PropertyType.STRING)[0]);
createTmpTableStatement.append(")");
if (needsSemicolon()) {
createTmpTableStatement.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(createTmpTableStatement.toString());
}
Connection conn = sqlgGraph.tx().getConnection();
try (Statement statement = conn.createStatement()) {
statement.execute(createTmpTableStatement.toString());
} catch (SQLException e) {
throw new RuntimeException(e);
}
StringBuilder sql = new StringBuilder("INSERT INTO ");
if (needsTemporaryTableSchema()) {
sql.append(maybeWrapInQoutes(getPublicSchema()));
sql.append(".");
}
sql.append(maybeWrapInQoutes(tmpTableIdentified));
sql.append(" (");
sql.append(maybeWrapInQoutes("recordId"));
sql.append(", ");
sql.append(maybeWrapInQoutes("property"));
sql.append(") VALUES (?, ?)");
if (needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
for (SqlgVertex vertex : vertices) {
preparedStatement.setString(1, vertex.id().toString());
preparedStatement.setString(2, propertyColumn.getName());
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} catch (SQLException e) {
throw new RuntimeException(e);
}
sql = new StringBuilder("DELETE FROM\n\t");
sql.append(maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
sql.append(".");
sql.append(maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
sql.append("\nWHERE\n\t");
sql.append("CONCAT(");
sql.append(maybeWrapInQoutes("recordId"));
sql.append(", ");
sql.append(maybeWrapInQoutes("property"));
sql.append(") IN (\n");
sql.append("SELECT\n\tCONCAT(");
sql.append(maybeWrapInQoutes("recordId"));
sql.append(", ");
sql.append(maybeWrapInQoutes("property"));
sql.append(")\nFROM\n\t");
if (needsTemporaryTableSchema()) {
sql.append(maybeWrapInQoutes(sqlgGraph.getSqlDialect().getPublicSchema()));
sql.append(".");
}
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(tmpTableIdentified));
sql.append(")");
if (sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
conn = sqlgGraph.tx().getConnection();
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
use of org.umlg.sqlg.structure.topology.GlobalUniqueIndex in project sqlg by pietermartin.
the class BaseSqlDialect method flushEdgeGlobalUniqueIndexes.
@Override
public void flushEdgeGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<MetaEdge, Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>>> edgeCache) {
for (MetaEdge metaEdge : edgeCache.keySet()) {
Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>> triples = edgeCache.get(metaEdge);
Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> edgeMap = triples.getRight();
Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(metaEdge.getSchemaTable().withPrefix(EDGE_PREFIX));
Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rows = triples.getRight();
for (Map.Entry<String, PropertyColumn> propertyColumnEntry : propertyColumnMap.entrySet()) {
PropertyColumn propertyColumn = propertyColumnEntry.getValue();
for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO ");
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
sql.append(".");
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
sql.append(" (");
PropertyType propertyType = propertyColumn.getPropertyType();
String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
int count = 1;
for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
if (count++ > 1) {
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE + propertyType.getPostFixes()[count - 2]));
} else {
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE));
}
sql.append(",");
}
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID));
sql.append(",");
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME));
sql.append(") VALUES ( ");
count = 1;
// noinspection Duplicates
for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
if (count++ > 1) {
sql.append("?");
} else {
sql.append("?");
}
sql.append(", ");
}
sql.append("?, ?)");
if (sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
Connection conn = sqlgGraph.tx().getConnection();
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
for (Map.Entry<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rowEntry : rows.entrySet()) {
SqlgEdge sqlgEdge = rowEntry.getKey();
Map<String, Object> parameterValueMap = rowEntry.getValue().getRight();
Object value = parameterValueMap.get(propertyColumn.getName());
List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
typeAndValues.add(Pair.of(PropertyType.STRING, sqlgEdge.id().toString()));
typeAndValues.add(Pair.of(PropertyType.STRING, propertyColumn.getName()));
SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, 1, preparedStatement, typeAndValues);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
use of org.umlg.sqlg.structure.topology.GlobalUniqueIndex in project sqlg by pietermartin.
the class BaseSqlDialect method flushVertexGlobalUniqueIndexes.
@Override
public void flushVertexGlobalUniqueIndexes(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> vertexCache) {
for (SchemaTable schemaTable : vertexCache.keySet()) {
Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices = vertexCache.get(schemaTable);
Map<String, PropertyColumn> propertyColumnMap = sqlgGraph.getTopology().getPropertiesFor(schemaTable.withPrefix(VERTEX_PREFIX));
for (Map.Entry<String, PropertyColumn> propertyColumnEntry : propertyColumnMap.entrySet()) {
PropertyColumn propertyColumn = propertyColumnEntry.getValue();
for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
Map<SqlgVertex, Map<String, Object>> rows = vertices.getRight();
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO ");
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
sql.append(".");
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
sql.append(" (");
PropertyType propertyType = propertyColumn.getPropertyType();
String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
int count = 1;
for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
if (count++ > 1) {
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE + propertyType.getPostFixes()[count - 2]));
} else {
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE));
}
sql.append(",");
}
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID));
sql.append(",");
sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME));
sql.append(") VALUES ( ");
count = 1;
// noinspection Duplicates
for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
if (count++ > 1) {
sql.append("?");
} else {
sql.append("?");
}
sql.append(", ");
}
sql.append("?, ?)");
if (sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
Connection conn = sqlgGraph.tx().getConnection();
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
for (Map.Entry<SqlgVertex, Map<String, Object>> rowEntry : rows.entrySet()) {
SqlgVertex sqlgVertex = rowEntry.getKey();
Map<String, Object> parameterValueMap = rowEntry.getValue();
Object value = parameterValueMap.get(propertyColumn.getName());
List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
typeAndValues.add(Pair.of(PropertyType.STRING, sqlgVertex.id().toString()));
typeAndValues.add(Pair.of(PropertyType.STRING, propertyColumn.getName()));
SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, 1, preparedStatement, typeAndValues);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
use of org.umlg.sqlg.structure.topology.GlobalUniqueIndex in project sqlg by pietermartin.
the class SqlgElement method insertGlobalUniqueIndex.
protected void insertGlobalUniqueIndex(Map<String, Object> keyValueMap, Map<String, PropertyColumn> propertyColumns) {
for (PropertyColumn propertyColumn : propertyColumns.values()) {
for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
Object value = keyValueMap.get(propertyColumn.getName());
Pair<PropertyColumn, Object> propertyColumnObjectPair = Pair.of(propertyColumn, value);
this.insertGlobalUniqueIndex(this.sqlgGraph, globalUniqueIndex, propertyColumnObjectPair);
}
}
}
use of org.umlg.sqlg.structure.topology.GlobalUniqueIndex in project sqlg by pietermartin.
the class TestBatchGlobalUniqueIndexes method testGlobalUniqueIndexOnVertexNormalBatchMode.
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testGlobalUniqueIndexOnVertexNormalBatchMode() throws InterruptedException {
Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
Map<String, PropertyType> properties = new HashMap<>();
properties.put("namec", PropertyType.STRING);
properties.put("namea", PropertyType.STRING);
properties.put("nameb", PropertyType.STRING);
this.sqlgGraph.getTopology().ensureVertexLabelExist("A", properties);
@SuppressWarnings("OptionalGetWithoutIsPresent") Collection<PropertyColumn> propertyColumns = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").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("A_namea_A_nameb_A_namec");
Assert.assertTrue(globalUniqueIndexOptional.isPresent());
Optional<PropertyColumn> nameaPropertyColumnOptional = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").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("A_namea_A_nameb_A_namec", globalUniqueIndex.getName());
this.sqlgGraph.tx().normalBatchModeOn();
Vertex a = this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
this.sqlgGraph.tx().commit();
try {
this.sqlgGraph.tx().normalBatchModeOn();
this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
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();
testGlobalUniqueIndexOnVertexNormalBatchMode_assert(this.sqlgGraph, globalUniqueIndex, a);
if (this.sqlgGraph1 != null) {
Thread.sleep(1000);
testGlobalUniqueIndexOnVertexNormalBatchMode_assert(this.sqlgGraph1, globalUniqueIndex, a);
}
}
Aggregations