@Override public int hashCode() { return this.getName().hashCode(); }
@Override public int hashCode() { return this.getName().hashCode(); }
private static void updateGlobalUniqueIndex(SqlgGraph sqlgGraph, GlobalUniqueIndex globalUniqueIndex, RecordId recordId, Pair<PropertyColumn, Object> propertyColumnObjectPair) { List<Vertex> globalUniqueIndexVertexes = sqlgGraph.globalUniqueIndexes() .V().hasLabel(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName()) .has(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, recordId.toString()) .has(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName()) .toList(); Preconditions.checkState(globalUniqueIndexVertexes.size() <= 1, "More than one GlobalUniqueIndex for %s and recordId %s found", Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), recordId.toString()); if (!globalUniqueIndexVertexes.isEmpty()) { Vertex globalUniqueIndexVertex = globalUniqueIndexVertexes.get(0); globalUniqueIndexVertex.property(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, propertyColumnObjectPair.getValue()); } else { //This happens if the property is not present at all in which case the entry in the GlobalUniqueIndex table has not been created yet. sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, propertyColumnObjectPair.getValue(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } }
private static void updateGlobalUniqueIndex(SqlgGraph sqlgGraph, GlobalUniqueIndex globalUniqueIndex, RecordId recordId, Pair<PropertyColumn, Object> propertyColumnObjectPair) { List<Vertex> globalUniqueIndexVertexes = sqlgGraph.globalUniqueIndexes() .V().hasLabel(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName()) .has(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, recordId.toString()) .has(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName()) .toList(); Preconditions.checkState(globalUniqueIndexVertexes.size() <= 1, "More than one GlobalUniqueIndex for %s and recordId %s found", Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), recordId.toString()); if (!globalUniqueIndexVertexes.isEmpty()) { Vertex globalUniqueIndexVertex = globalUniqueIndexVertexes.get(0); globalUniqueIndexVertex.property(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, propertyColumnObjectPair.getValue()); } else { //This happens if the property is not present at all in which case the entry in the GlobalUniqueIndex table has not been created yet. sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, propertyColumnObjectPair.getValue(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } }
/** * remove the given global unique index * * @param index the index to remove * @param preserveData should we keep the sql data? */ void removeGlobalUniqueIndex(GlobalUniqueIndex index, boolean preserveData) { getTopology().lock(); String fn = index.getName(); if (!uncommittedRemovedGlobalUniqueIndexes.contains(fn)) { uncommittedRemovedGlobalUniqueIndexes.add(fn); TopologyManager.removeGlobalUniqueIndex(sqlgGraph, fn); if (!preserveData) { getVertexLabel(index.getName()).ifPresent( (VertexLabel vl) -> vl.remove(false)); } getTopology().fire(index, "", TopologyChangeAction.DELETE); } }
/** * remove the given global unique index * * @param index the index to remove * @param preserveData should we keep the sql data? */ void removeGlobalUniqueIndex(GlobalUniqueIndex index, boolean preserveData) { getTopology().lock(); String fn = index.getName(); if (!uncommittedRemovedGlobalUniqueIndexes.contains(fn)) { uncommittedRemovedGlobalUniqueIndexes.add(fn); TopologyManager.removeGlobalUniqueIndex(sqlgGraph, fn); if (!preserveData) { getVertexLabel(index.getName()).ifPresent( (VertexLabel vl) -> vl.remove(false)); } getTopology().fire(index, "", TopologyChangeAction.DELETE); } }
@SuppressWarnings("OptionalGetWithoutIsPresent") static GlobalUniqueIndex createGlobalUniqueIndex(SqlgGraph sqlgGraph, Topology topology, String globalUniqueIndexName, Set<PropertyColumn> properties) { //all PropertyColumns must be for the same PropertyType PropertyType propertyType = properties.iterator().next().getPropertyType(); Map<String, PropertyType> valueColumn = new HashMap<>(); valueColumn.put(GLOBAL_UNIQUE_INDEX_RECORD_ID, PropertyType.STRING); valueColumn.put(GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, PropertyType.STRING); valueColumn.put(GLOBAL_UNIQUE_INDEX_VALUE, propertyType); VertexLabel vertexLabel = topology.getGlobalUniqueIndexSchema().ensureVertexLabelExist(globalUniqueIndexName, valueColumn); PropertyColumn valuePropertyColumn = vertexLabel.getProperty(GLOBAL_UNIQUE_INDEX_VALUE).get(); PropertyColumn recordIdColumn = vertexLabel.getProperty(GLOBAL_UNIQUE_INDEX_RECORD_ID).get(); PropertyColumn propertyColumn = vertexLabel.getProperty(GLOBAL_UNIQUE_INDEX_PROPERTY_NAME).get(); vertexLabel.ensureIndexExists(IndexType.UNIQUE, Collections.singletonList(valuePropertyColumn)); vertexLabel.ensureIndexExists(IndexType.UNIQUE, Arrays.asList(recordIdColumn, propertyColumn)); GlobalUniqueIndex globalUniqueIndex = new GlobalUniqueIndex(topology, globalUniqueIndexName, properties); topology.getGlobalUniqueIndexSchema().globalUniqueIndexes.put(globalUniqueIndex.getName(),globalUniqueIndex); TopologyManager.addGlobalUniqueIndex(sqlgGraph, globalUniqueIndexName, properties); globalUniqueIndex.committed = false; return globalUniqueIndex; }
@SuppressWarnings("OptionalGetWithoutIsPresent") static GlobalUniqueIndex createGlobalUniqueIndex(SqlgGraph sqlgGraph, Topology topology, String globalUniqueIndexName, Set<PropertyColumn> properties) { //all PropertyColumns must be for the same PropertyType PropertyType propertyType = properties.iterator().next().getPropertyType(); Map<String, PropertyType> valueColumn = new HashMap<>(); valueColumn.put(GLOBAL_UNIQUE_INDEX_RECORD_ID, PropertyType.STRING); valueColumn.put(GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, PropertyType.STRING); valueColumn.put(GLOBAL_UNIQUE_INDEX_VALUE, propertyType); VertexLabel vertexLabel = topology.getGlobalUniqueIndexSchema().ensureVertexLabelExist(globalUniqueIndexName, valueColumn); PropertyColumn valuePropertyColumn = vertexLabel.getProperty(GLOBAL_UNIQUE_INDEX_VALUE).get(); PropertyColumn recordIdColumn = vertexLabel.getProperty(GLOBAL_UNIQUE_INDEX_RECORD_ID).get(); PropertyColumn propertyColumn = vertexLabel.getProperty(GLOBAL_UNIQUE_INDEX_PROPERTY_NAME).get(); vertexLabel.ensureIndexExists(IndexType.UNIQUE, Collections.singletonList(valuePropertyColumn)); vertexLabel.ensureIndexExists(IndexType.UNIQUE, Arrays.asList(recordIdColumn, propertyColumn)); GlobalUniqueIndex globalUniqueIndex = new GlobalUniqueIndex(topology, globalUniqueIndexName, properties); topology.getGlobalUniqueIndexSchema().globalUniqueIndexes.put(globalUniqueIndex.getName(),globalUniqueIndex); TopologyManager.addGlobalUniqueIndex(sqlgGraph, globalUniqueIndexName, properties); globalUniqueIndex.committed = false; return globalUniqueIndex; }
private void insertGlobalUniqueIndex(SqlgGraph sqlgGraph, GlobalUniqueIndex globalUniqueIndex, Pair<PropertyColumn, Object> propertyColumnObjectPair) { if (propertyColumnObjectPair.getRight() != null) { sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, propertyColumnObjectPair.getValue(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, this.recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } else { //Need to insert null values else the update the code will not work. if (sqlgGraph.getSqlDialect().uniqueIndexConsidersNullValuesEqual()) { sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, "dummy_" + UUID.randomUUID().toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, this.recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } else { sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, this.recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } } }
private void insertGlobalUniqueIndex(SqlgGraph sqlgGraph, GlobalUniqueIndex globalUniqueIndex, Pair<PropertyColumn, Object> propertyColumnObjectPair) { if (propertyColumnObjectPair.getRight() != null) { sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, propertyColumnObjectPair.getValue(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, this.recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } else { //Need to insert null values else the update the code will not work. if (sqlgGraph.getSqlDialect().uniqueIndexConsidersNullValuesEqual()) { sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_VALUE, "dummy_" + UUID.randomUUID().toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, this.recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } else { sqlgGraph.addVertex( T.label, Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + globalUniqueIndex.getName(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_RECORD_ID, this.recordId.toString(), GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, propertyColumnObjectPair.getKey().getName() ); } } }
Optional<JsonNode> toNotifyJson() { Preconditions.checkState(this.topology.isSqlWriteLockHeldByCurrentThread(), "GlobalUniqueIndex toNotifyJson() may only be called is the lock is held."); ObjectNode result = new ObjectNode(Topology.OBJECT_MAPPER.getNodeFactory()); ArrayNode propertyArrayNode = new ArrayNode(Topology.OBJECT_MAPPER.getNodeFactory()); for (PropertyColumn property : this.uncommittedProperties) { ObjectNode objectNode = property.toNotifyJson(); objectNode.put("schemaName", property.getParentLabel().getSchema().getName()); objectNode.put("abstractLabelLabel", property.getParentLabel().getLabel()); propertyArrayNode.add(objectNode); } result.put("name", getName()); result.set("uncommittedProperties", propertyArrayNode); return Optional.of(result); }
Optional<JsonNode> toNotifyJson() { Preconditions.checkState(this.topology.isSqlWriteLockHeldByCurrentThread(), "GlobalUniqueIndex toNotifyJson() may only be called is the lock is held."); ObjectNode result = new ObjectNode(Topology.OBJECT_MAPPER.getNodeFactory()); ArrayNode propertyArrayNode = new ArrayNode(Topology.OBJECT_MAPPER.getNodeFactory()); for (PropertyColumn property : this.uncommittedProperties) { ObjectNode objectNode = property.toNotifyJson(); objectNode.put("schemaName", property.getParentLabel().getSchema().getName()); objectNode.put("abstractLabelLabel", property.getParentLabel().getLabel()); propertyArrayNode.add(objectNode); } result.put("name", getName()); result.set("uncommittedProperties", propertyArrayNode); return Optional.of(result); }
put(GlobalUniqueIndex.GLOBAL_UNIQUE_INDEX_PROPERTY_NAME, PropertyType.STRING); }}; result.put(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA + "." + Topology.VERTEX_PREFIX + globalUniqueIndex.getName(), properties);
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"));
private void removeGlobalUniqueIndex() { Map<String, PropertyColumn> properties = this.sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(this.getSchemaTablePrefixed()); for (PropertyColumn propertyColumn : properties.values()) { for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) { StringBuilder sql = new StringBuilder("DELETE FROM "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA)); sql.append("."); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName())); sql.append(" WHERE "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("recordId")); sql.append(" = ? AND "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("property")); sql.append(" = ?"); if (this.sqlgGraph.getSqlDialect().needsSemicolon()) { sql.append(";"); } if (logger.isDebugEnabled()) { logger.debug(sql.toString()); } Connection conn = this.sqlgGraph.tx().getConnection(); try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) { preparedStatement.setString(1, this.id().toString()); preparedStatement.setString(2, propertyColumn.getName()); preparedStatement.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
private void removeGlobalUniqueIndex() { Map<String, PropertyColumn> properties = this.sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(this.getSchemaTablePrefixed()); for (PropertyColumn propertyColumn : properties.values()) { for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) { StringBuilder sql = new StringBuilder("DELETE FROM "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA)); sql.append("."); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName())); sql.append(" WHERE "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("recordId")); sql.append(" = ? AND "); sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("property")); sql.append(" = ?"); if (this.sqlgGraph.getSqlDialect().needsSemicolon()) { sql.append(";"); } if (logger.isDebugEnabled()) { logger.debug(sql.toString()); } Connection conn = this.sqlgGraph.tx().getConnection(); try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) { preparedStatement.setString(1, this.id().toString()); preparedStatement.setString(2, propertyColumn.getName()); preparedStatement.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
/** * JSON representation of committed state * @return */ private JsonNode toJson(){ ObjectNode result = new ObjectNode(Topology.OBJECT_MAPPER.getNodeFactory()); ArrayNode propertyArrayNode = new ArrayNode(Topology.OBJECT_MAPPER.getNodeFactory()); for (PropertyColumn property : this.properties) { ObjectNode objectNode = property.toNotifyJson(); objectNode.put("schemaName", property.getParentLabel().getSchema().getName()); objectNode.put("abstractLabelLabel", property.getParentLabel().getLabel()); propertyArrayNode.add(objectNode); } result.put("name", getName()); result.set("properties", propertyArrayNode); return result; }
/** * JSON representation of committed state * @return */ private JsonNode toJson(){ ObjectNode result = new ObjectNode(Topology.OBJECT_MAPPER.getNodeFactory()); ArrayNode propertyArrayNode = new ArrayNode(Topology.OBJECT_MAPPER.getNodeFactory()); for (PropertyColumn property : this.properties) { ObjectNode objectNode = property.toNotifyJson(); objectNode.put("schemaName", property.getParentLabel().getSchema().getName()); objectNode.put("abstractLabelLabel", property.getParentLabel().getLabel()); propertyArrayNode.add(objectNode); } result.put("name", getName()); result.set("properties", propertyArrayNode); return result; }
@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); } } } } }
@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)); 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 SQLServerEdgeGlobalUniqueIndexBulkRecord(bulkCopy, sqlgGraph, edgeMap, propertyColumn)); } } catch (SQLException e) { throw new RuntimeException(e); } } } } }