void appendProperties(AbstractLabel edgeLabel, StringBuilder sql) { for (PropertyColumn propertyColumn : edgeLabel.getProperties().values()) { sql.append(", "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(propertyColumn.getName())); // additional columns for time zone, etc. String[] ps = propertyColumn.getPropertyType().getPostFixes(); if (ps != null) { for (String p : propertyColumn.getPropertyType().getPostFixes()) { sql.append(", "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(propertyColumn.getName() + p)); } } } }
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); } } }
/** * Only called for a new vertex/edge label being added. * * @param label The vertex or edge's label. * @param properties The vertex's properties. * @param identifiers The vertex or edge's identifiers */ AbstractLabel(SqlgGraph sqlgGraph, String label, Map<String, PropertyType> properties, ListOrderedSet<String> identifiers) { this.sqlgGraph = sqlgGraph; this.label = label; for (Map.Entry<String, PropertyType> propertyEntry : properties.entrySet()) { PropertyColumn property = new PropertyColumn(this, propertyEntry.getKey(), propertyEntry.getValue()); property.setCommitted(false); this.uncommittedProperties.put(propertyEntry.getKey(), property); } this.uncommittedIdentifiers.addAll(identifiers); }
@SuppressWarnings("OptionalGetWithoutIsPresent") static String globalUniqueIndexName(Topology topology, Set<PropertyColumn> properties) { List<PropertyColumn> propertyColumns = new ArrayList<>(properties); propertyColumns.sort(Comparator.comparing(PropertyColumn::getName)); String name = propertyColumns.stream().map(p -> p.getParentLabel().getLabel() + "_" + p.getName()).reduce((a, b) -> a + "_" + b).get(); if (("gui_schema_V_A" + name).length() > 50) { name = "globalUniqueIndex_" + topology.getGlobalUniqueIndexes().size(); } return name; }
if (propertyNames.contains(propertyColumn.getName())) { for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) { StringBuilder sql = new StringBuilder(); sql.append("UPDATE "); 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();
Map<String, PropertyType> getPropertyTypeMap() { Map<String, PropertyType> result = new HashMap<>(); for (Map.Entry<String, PropertyColumn> propertyEntry : this.properties.entrySet()) { result.put(propertyEntry.getKey(), propertyEntry.getValue().getPropertyType()); } if (getSchema().getTopology().isSqlWriteLockHeldByCurrentThread()) { for (Map.Entry<String, PropertyColumn> uncommittedPropertyEntry : this.uncommittedProperties.entrySet()) { result.put(uncommittedPropertyEntry.getKey(), uncommittedPropertyEntry.getValue().getPropertyType()); } for (String s : this.uncommittedRemovedProperties) { result.remove(s); } } return result; }
void addGlobalUniqueIndexToProperties(PropertyColumn propertyColumn) { this.globalUniqueIndexProperties.put(propertyColumn.getName(), propertyColumn); }
public void ensureDistributed(int shardCount, PropertyColumn distributionPropertyColumn, AbstractLabel colocate) { Preconditions.checkState(getProperty(distributionPropertyColumn.getName()).isPresent(), "distributionPropertyColumn \"%s\" not found.", distributionPropertyColumn.getName()); Preconditions.checkState(getProperty(distributionPropertyColumn.getName()).get().equals(distributionPropertyColumn), "distributionPropertyColumn \"%s\" must be a property of \"%s\"", distributionPropertyColumn.getName(), this.getFullName()); Preconditions.checkArgument(getIdentifiers().contains(distributionPropertyColumn.getName()), "The distribution column must be part of the primary key"); if (!this.isDistributed()) { this.getSchema().getTopology().lock(); if (!this.isDistributed()) { TopologyManager.distributeAbstractLabel(this.sqlgGraph, this, shardCount, distributionPropertyColumn, colocate); distribute(shardCount, distributionPropertyColumn, colocate); this.uncommittedDistributionPropertyColumn = distributionPropertyColumn; this.uncommittedDistributionColocateAbstractLabel = colocate; this.uncommittedShardCount = shardCount; } } }
private EdgeLabel( boolean forSqlgSchema, String edgeLabelName, VertexLabel outVertexLabel, VertexLabel inVertexLabel, Map<String, PropertyType> properties, ListOrderedSet<String> identifiers) { super(outVertexLabel.getSchema().getSqlgGraph(), edgeLabelName, properties, identifiers); if (forSqlgSchema) { this.outVertexLabels.add(outVertexLabel); this.inVertexLabels.add(inVertexLabel); } else { this.uncommittedOutVertexLabels.add(outVertexLabel); this.uncommittedInVertexLabels.add(inVertexLabel); } // this is a topology edge label, the columns exist if (forSqlgSchema) { for (PropertyColumn pc : this.uncommittedProperties.values()) { pc.setCommitted(true); this.properties.put(pc.getName(), pc); } this.uncommittedProperties.clear(); } this.topology = outVertexLabel.getSchema().getTopology(); }
@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()) { try { Connection connection = sqlgGraph.tx().getConnection(); SQLServerConnection sqlServerConnection = connection.unwrap(SQLServerConnection.class); try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(sqlServerConnection)) { bulkCopy.setDestinationTableName(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA) + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()) ); bulkCopy.writeToServer(new SQLServerVertexGlobalUniqueIndexBulkRecord(bulkCopy, sqlgGraph, vertices, propertyColumn)); } } catch (SQLException e) { throw new RuntimeException(e); } } } } }
static PropertyColumn fromNotifyJson(AbstractLabel abstractLabel, JsonNode jsonNode) { String pt = jsonNode.get("propertyType").asText(); if (pt.equals("VARCHAR")) { //This is not ideal, however Sqlg only uses VARCHAR when creating the column. //For the rest is is considered the same as STRING return new PropertyColumn( abstractLabel, jsonNode.get("name").asText(), PropertyType.STRING); } else { return new PropertyColumn( abstractLabel, jsonNode.get("name").asText(), PropertyType.valueOf(pt)); } }
void afterCommit() { this.indexType = this.uncommittedIndexType; Iterator<PropertyColumn> propertyColumnIterator = this.uncommittedProperties.iterator(); while (propertyColumnIterator.hasNext()) { PropertyColumn propertyColumn = propertyColumnIterator.next(); this.properties.add(propertyColumn); propertyColumn.afterCommit(); propertyColumnIterator.remove(); } this.uncommittedIndexType = null; this.committed = true; }
private GlobalUniqueIndex(Topology topology, String name, Set<PropertyColumn> uncommittedProperties) { this.topology = topology; this.name = name; this.uncommittedProperties = uncommittedProperties; for (PropertyColumn uncommittedProperty : uncommittedProperties) { uncommittedProperty.addGlobalUniqueIndex(this); } }
/** * Called from {@link Topology#fromNotifyJson(int, LocalDateTime)} * * @param properties The properties. */ void addGlobalUniqueProperties(Set<PropertyColumn> properties) { for (PropertyColumn property : properties) { property.addToGlobalUniqueIndexes(this); } this.properties = properties; }
for (Iterator<Map.Entry<String, PropertyColumn>> it = this.uncommittedProperties.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String, PropertyColumn> entry = it.next(); entry.getValue().afterRollback(); it.remove(); if (this.propertyTypeMap != null) { for (Iterator<Map.Entry<String, PropertyColumn>> it = this.properties.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String, PropertyColumn> entry = it.next(); entry.getValue().afterRollback();
if (propertyNames.contains(propertyColumn.getName())) { for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) { StringBuilder sql = new StringBuilder(); sql.append("UPDATE "); 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();
Map<String, PropertyType> getPropertyTypeMap() { Map<String, PropertyType> result = new HashMap<>(); for (Map.Entry<String, PropertyColumn> propertyEntry : this.properties.entrySet()) { result.put(propertyEntry.getKey(), propertyEntry.getValue().getPropertyType()); } if (getSchema().getTopology().isSqlWriteLockHeldByCurrentThread()) { for (Map.Entry<String, PropertyColumn> uncommittedPropertyEntry : this.uncommittedProperties.entrySet()) { result.put(uncommittedPropertyEntry.getKey(), uncommittedPropertyEntry.getValue().getPropertyType()); } for (String s : this.uncommittedRemovedProperties) { result.remove(s); } } return result; }
void addGlobalUniqueIndexToUncommittedProperties(PropertyColumn propertyColumn) { this.uncommittedGlobalUniqueIndexProperties.put(propertyColumn.getName(), propertyColumn); }
@SuppressWarnings("OptionalGetWithoutIsPresent") static String globalUniqueIndexName(Topology topology, Set<PropertyColumn> properties) { List<PropertyColumn> propertyColumns = new ArrayList<>(properties); propertyColumns.sort(Comparator.comparing(PropertyColumn::getName)); String name = propertyColumns.stream().map(p -> p.getParentLabel().getLabel() + "_" + p.getName()).reduce((a, b) -> a + "_" + b).get(); if (("gui_schema_V_A" + name).length() > 50) { name = "globalUniqueIndex_" + topology.getGlobalUniqueIndexes().size(); } return name; }
public void ensureDistributed(int shardCount, PropertyColumn distributionPropertyColumn, AbstractLabel colocate) { Preconditions.checkState(getProperty(distributionPropertyColumn.getName()).isPresent(), "distributionPropertyColumn \"%s\" not found.", distributionPropertyColumn.getName()); Preconditions.checkState(getProperty(distributionPropertyColumn.getName()).get().equals(distributionPropertyColumn), "distributionPropertyColumn \"%s\" must be a property of \"%s\"", distributionPropertyColumn.getName(), this.getFullName()); Preconditions.checkArgument(getIdentifiers().contains(distributionPropertyColumn.getName()), "The distribution column must be part of the primary key"); if (!this.isDistributed()) { this.getSchema().getTopology().lock(); if (!this.isDistributed()) { TopologyManager.distributeAbstractLabel(this.sqlgGraph, this, shardCount, distributionPropertyColumn, colocate); distribute(shardCount, distributionPropertyColumn, colocate); this.uncommittedDistributionPropertyColumn = distributionPropertyColumn; this.uncommittedDistributionColocateAbstractLabel = colocate; this.uncommittedShardCount = shardCount; } } }