/** * Ensures that the vertex's table has the required columns. * If a columns needs to be created a lock will be obtained. * The vertex's schema and table must already exists. * The default "public" schema will be used. {@link SqlDialect#getPublicSchema()} * * @param label The vertex's label. * @param properties The properties to create if they do not exist. */ public void ensureVertexLabelPropertiesExist(String label, Map<String, PropertyType> properties) { ensureVertexLabelPropertiesExist(this.sqlgGraph.getSqlDialect().getPublicSchema(), label, properties); }
private void afterCommit() { if (this.isSqlWriteLockHeldByCurrentThread()) { z_internalTopologyMapWriteLock(); try { getPublicSchema().removeTemporaryTables(); for (Iterator<Map.Entry<String, Schema>> it = this.uncommittedSchemas.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String, Schema> entry = it.next(); removeSchemaFromCaches(sch); it.remove(); Map<String, AbstractLabel> uncommittedAllTables = getUncommittedAllTables(); for (Map.Entry<String, AbstractLabel> stringMapEntry : uncommittedAllTables.entrySet()) { String uncommittedSchemaTable = stringMapEntry.getKey(); Map<SchemaTable, Pair<Set<SchemaTable>, Set<SchemaTable>>> uncommittedSchemaTableForeignKeys = getUncommittedSchemaTableForeignKeys(); for (Map.Entry<SchemaTable, Pair<Set<SchemaTable>, Set<SchemaTable>>> schemaTablePairEntry : uncommittedSchemaTableForeignKeys.entrySet()) { Pair<Set<SchemaTable>, Set<SchemaTable>> foreignKeys = this.schemaTableForeignKeyCache.get(schemaTablePairEntry.getKey()); Map<String, Set<ForeignKey>> uncommittedEdgeForeignKeys = getUncommittedEdgeForeignKeys(); for (Map.Entry<String, Set<ForeignKey>> entry : uncommittedEdgeForeignKeys.entrySet()) { Set<ForeignKey> foreignKeys = this.edgeForeignKeyCache.get(entry.getKey()); z_internalInternalTopologyMapWriteUnLock(); z_internalSqlWriteUnlock();
/** * Ensures that the edge table with out and in {@link VertexLabel}s and property columns exists. * The edge table will reside in the out vertex's schema. * If a table, a foreign key or a column needs to be created a lock is first obtained. * * @param edgeLabelName The label of the edge for which a table will be created. * @param outVertexLabel The edge's out {@link VertexLabel} * @param inVertexLabel The edge's in {@link VertexLabel} * @param properties The edge's properties with their type. * @return The {@link EdgeLabel} */ public EdgeLabel ensureEdgeLabelExist(final String edgeLabelName, final VertexLabel outVertexLabel, final VertexLabel inVertexLabel, Map<String, PropertyType> properties) { return ensureEdgeLabelExist(edgeLabelName, outVertexLabel, inVertexLabel, properties, new ListOrderedSet<>()); }
private void afterRollback() { if (this.isSqlWriteLockHeldByCurrentThread()) { getPublicSchema().removeTemporaryTables(); for (Iterator<Map.Entry<String, Schema>> it = this.uncommittedSchemas.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String, Schema> entry = it.next(); entry.getValue().afterRollback(); it.remove(); } this.uncommittedRemovedSchemas.clear(); for (Schema schema : this.schemas.values()) { schema.afterRollback(); } z_internalSqlWriteUnlock(); } }
private void process(SchemaTableTree rootSchemaTableTree) { SchemaTable table = rootSchemaTableTree.getSchemaTable(); Optional<? extends AbstractLabel> label; if (table.isVertexTable()) { label = sqlgGraph.getTopology().getVertexLabel(table.getSchema(), table.withOutPrefix().getTable()); } else if (table.isEdgeTable()) { label = sqlgGraph.getTopology().getEdgeLabel(table.getSchema(), table.withOutPrefix().getTable()); } else { label = Optional.empty(); } process(sqlgGraph, rootSchemaTableTree, label.orElseThrow(illegalState("unknown table: %s", table))); }
EdgeLabel edgeLabel = sqlgGraph.getTopology().getEdgeLabel(metaEdge.getSchemaTable().getSchema(), metaEdge.getSchemaTable().getTable()).orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel not found for %s.%s", metaEdge.getSchemaTable().getSchema(), metaEdge.getSchemaTable().getTable()))); VertexLabel outVertexLabel = sqlgGraph.getTopology().getVertexLabel(outSchemaTable.getSchema(), outSchemaTable.getTable()).orElseThrow(() -> new IllegalStateException(String.format("VertexLabel not found for %s.%s", outSchemaTable.getSchema(), outSchemaTable.getTable()))); VertexLabel inVertexLabel = sqlgGraph.getTopology().getVertexLabel(inSchemaTable.getSchema(), inSchemaTable.getTable()).orElseThrow(() -> new IllegalStateException(String.format("VertexLabel not found for %s.%s", inSchemaTable.getSchema(), inSchemaTable.getTable()))); Map<String, PropertyType> propertyTypeMap = sqlgGraph.getTopology().getTableFor(metaEdge.getSchemaTable().withPrefix(EDGE_PREFIX)); SortedSet<String> columns = triples.getLeft(); Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rows = triples.getRight(); .getSchema(metaEdge.getSchemaTable().getSchema()).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", metaEdge.getSchemaTable().getSchema()))) .getEdgeLabel(metaEdge.getSchemaTable().getTable()).orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel %s not found", metaEdge.getSchemaTable().getTable()))) .getProperties();
Map<String, PropertyType> outProperties = sqlgGraph.getTopology().getTableFor(out.withPrefix(VERTEX_PREFIX)); Map<String, PropertyType> inProperties = sqlgGraph.getTopology().getTableFor(in.withPrefix(VERTEX_PREFIX)); PropertyType outPropertyType; if (idFields.getLeft().equals(Topology.ID)) { String tmpTableIdentified = Base64.getEncoder().encodeToString(bytes); tmpTableIdentified = Topology.BULK_TEMP_EDGE + tmpTableIdentified; sqlgGraph.getTopology().getPublicSchema().createTempTable(tmpTableIdentified, columns); this._copyInBulkTempEdges(sqlgGraph, SchemaTable.of(out.getSchema(), tmpTableIdentified), uids, outPropertyType, inPropertyType); Optional<VertexLabel> outVertexLabelOptional = sqlgGraph.getTopology().getVertexLabel(out.getSchema(), out.getTable()); Optional<VertexLabel> inVertexLabelOptional = sqlgGraph.getTopology().getVertexLabel(in.getSchema(), in.getTable()); Preconditions.checkState(outVertexLabelOptional.isPresent(), "Out VertexLabel must be present. Not found for %s", out.toString()); Preconditions.checkState(inVertexLabelOptional.isPresent(), "In VertexLabel must be present. Not found for %s", in.toString()); sqlgGraph.getTopology().ensureEdgeLabelExist(edgeLabel, outVertexLabelOptional.get(), inVertexLabelOptional.get(), edgeColumns);
SQLServerVertexCacheBulkRecord(SQLServerBulkCopy bulkCopy, SqlgGraph sqlgGraph, SchemaTable schemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices) throws SQLServerException { this.rowIter = vertices.getRight().entrySet().iterator(); if (!schemaTable.isTemporary()) { this.propertyColumns = sqlgGraph.getTopology() .getSchema(schemaTable.getSchema()).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", schemaTable.getSchema()))) .getVertexLabel(schemaTable.getTable()).orElseThrow(() -> new IllegalStateException(String.format("VertexLabel %s not found", schemaTable.getTable()))) .getProperties(); } else { this.properties = sqlgGraph.getTopology().getPublicSchema().getTemporaryTable(VERTEX_PREFIX + schemaTable.getTable()); } int i = 1; this.columns = vertices.getLeft(); this.dummy = this.columns.isEmpty(); if (this.dummy) { bulkCopy.addColumnMapping(i, "dummy"); this.columnMetadata.put(i, new ColumnMetadata( "dummy", sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.INTEGER)[0], 0, 0, null, PropertyType.INTEGER )); } else { addMetaData(bulkCopy, sqlgGraph); } }
/** * Ensures that the edge table with out and in foreign keys and property columns exists. * The edge table will reside in the out vertex's schema. * If a table, a foreign key or a column needs to be created a lock is first obtained. * * @param edgeLabelName The label for the edge. * @param foreignKeyOut The {@link SchemaTable} that represents the out vertex. * @param foreignKeyIn The {@link SchemaTable} that represents the in vertex. * @param properties The edge's properties with their type. * @return The {@link SchemaTable} that represents the edge. */ public void ensureEdgeLabelExist(final String edgeLabelName, final SchemaTable foreignKeyOut, final SchemaTable foreignKeyIn, Map<String, PropertyType> properties) { Objects.requireNonNull(edgeLabelName, "Given edgeLabelName must not be null"); Objects.requireNonNull(foreignKeyOut, "Given outTable must not be null"); Objects.requireNonNull(foreignKeyIn, "Given inTable must not be null"); Preconditions.checkState(getVertexLabel(foreignKeyOut.getSchema(), foreignKeyOut.getTable()).isPresent(), "The out vertex must already exist before invoking 'ensureEdgeLabelExist'. \"%s\" does not exist", foreignKeyIn.toString()); Preconditions.checkState(getVertexLabel(foreignKeyIn.getSchema(), foreignKeyIn.getTable()).isPresent(), "The in vertex must already exist before invoking 'ensureEdgeLabelExist'. \"%s\" does not exist", foreignKeyIn.toString()); //outVertexSchema will be there as the Precondition checked it. @SuppressWarnings("OptionalGetWithoutIsPresent") Schema outVertexSchema = this.getSchema(foreignKeyOut.getSchema()).get(); Schema inVertexSchema = this.getSchema(foreignKeyIn.getSchema()).get(); Optional<VertexLabel> outVertexLabel = outVertexSchema.getVertexLabel(foreignKeyOut.getTable()); Optional<VertexLabel> inVertexLabel = inVertexSchema.getVertexLabel(foreignKeyIn.getTable()); Preconditions.checkState(outVertexLabel.isPresent(), "out VertexLabel must be present"); Preconditions.checkState(inVertexLabel.isPresent(), "in VertexLabel must be present"); @SuppressWarnings("OptionalGetWithoutIsPresent") EdgeLabel edgeLabel = outVertexSchema.ensureEdgeLabelExist(edgeLabelName, outVertexLabel.get(), inVertexLabel.get(), properties); SchemaTable.of(foreignKeyOut.getSchema(), edgeLabel.getLabel()); }
VertexLabel vertexLabel = null; if (!schemaTable.isTemporary()) { vertexLabel = sqlgGraph.getTopology().getVertexLabel(schemaTable.getSchema(), schemaTable.getTable()).orElseThrow( () -> new IllegalStateException(String.format("VertexLabel %s not found.", schemaTable.toString()))); Map<String, PropertyType> columns = new HashMap<>(); columns.put("dummy", PropertyType.from(0)); sqlgGraph.getTopology().ensureVertexLabelPropertiesExist( schemaTable.getSchema(), schemaTable.getTable(),
Optional<VertexLabel> outVertexLabelOptional = this.sqlgGraph.getTopology().getVertexLabel(this.schema, this.table); Optional<VertexLabel> inVertexLabelOptional = this.sqlgGraph.getTopology().getVertexLabel(((SqlgVertex) inVertex).schema, ((SqlgVertex) inVertex).table); Preconditions.checkState(outVertexLabelOptional.isPresent(), "Out VertexLabel must be present. Not found for %s", this.schema + "." + this.table); Preconditions.checkState(inVertexLabelOptional.isPresent(), "In VertexLabel must be present. Not found for %s", ((SqlgVertex) inVertex).schema + "." + ((SqlgVertex) inVertex).table); EdgeLabel edgeLabel = this.sqlgGraph.getTopology().ensureEdgeLabelExist(label, outVertexLabelOptional.get(), inVertexLabelOptional.get(), columns); if (!edgeLabel.hasIDPrimaryKey()) { Preconditions.checkArgument(columns.keySet().containsAll(edgeLabel.getIdentifiers()), "identifiers must be present %s", edgeLabel.getIdentifiers());
@Override public void flushRemovedEdges(SqlgGraph sqlgGraph, Map<SchemaTable, List<SqlgEdge>> removeEdgeCache) { if (!removeEdgeCache.isEmpty()) { //split the list of edges, postgres existVertexLabel a 2 byte limit in the in clause for (Map.Entry<SchemaTable, List<SqlgEdge>> schemaEdges : removeEdgeCache.entrySet()) { SchemaTable schemaTable = schemaEdges.getKey(); EdgeLabel edgeLabel = sqlgGraph.getTopology().getEdgeLabel(schemaTable.getSchema(), schemaTable.getTable()) .orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel not found for %s.%s", schemaTable.getSchema(), schemaTable.getTable()))); //TODO refacor to remove looping. List<RecordId.ID> ids = new ArrayList<>(); for (SqlgEdge edge : schemaEdges.getValue()) { ids.add(((RecordId) edge.id()).getID()); } String sql = drop(edgeLabel, ids); if (logger.isDebugEnabled()) { logger.debug(sql); } Connection conn = sqlgGraph.tx().getConnection(); try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) { preparedStatement.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
VertexLabel fileLabel = graph().getTopology().ensureVertexLabelExist(file.getSchema(), file.withOutPrefix().getTable()); graph().getTopology().ensureEdgeLabelExist(parent.withOutPrefix().getTable(), fileLabel, fileLabel, ImmutableMap.of()); graph().getTopology().ensureVertexLabelPropertiesExist(file.getSchema(), file.withOutPrefix().getTable(), ImmutableMap.of( Bdio.DataProperty.fileSystemType.name(), PropertyType.STRING, Bdio.DataProperty.byteCount.name(), PropertyType.LONG,
/** * Ensures that the schema, vertex table exist in the db. * If any element does not exist the a lock is first obtained. After the lock is obtained the maps are rechecked to * see if the element has not been added in the mean time. * * @param schemaName The schema the vertex is in. * @param label The vertex's label. Translates to a table prepended with 'V_' and the table's name being the label. */ public VertexLabel ensureVertexLabelExist(final String schemaName, final String label) { return ensureVertexLabelExist(schemaName, label, Collections.emptyMap()); }
Map<String, PropertyColumn> properties = new LinkedHashMap<>(graph().getTopology().getPropertiesFor(table)); properties.remove(options().identifierKey().get()); if (properties.isEmpty()) { graph().getTopology().ensureVertexLabelPropertiesExist(label, Collections.singletonMap("_flatten_dummy", PropertyType.from(0))); properties.putAll(graph().getTopology().getPropertiesFor(table)); properties.remove(options().identifierKey().get());
public Optional<EdgeLabel> getEdgeLabel(String schemaName, String edgeLabelName) { Preconditions.checkArgument(!edgeLabelName.startsWith(EDGE_PREFIX), "edge label name may not start with %s", EDGE_PREFIX); Optional<Schema> schemaOptional = getSchema(schemaName); if (schemaOptional.isPresent()) { Schema schema = schemaOptional.get(); return schema.getEdgeLabel(edgeLabelName); } else { return Optional.empty(); } }
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();
vertexLabel = sqlgGraph.getTopology().getVertexLabel(schemaTable.getSchema(), schemaTable.getTable()).orElseThrow( () -> new IllegalStateException(String.format("VertexLabel %s not found.", schemaTable.toString())));
private Schema(Topology topology, String name) { this.topology = topology; this.name = name; this.sqlgGraph = this.topology.getSqlgGraph(); }
Map<String, PropertyType> columns = new HashMap<>(); columns.put(key, PropertyType.from(value)); this.sqlgGraph.getTopology().ensureVertexLabelPropertiesExist( this.schema, this.table, Map<String, PropertyType> columns = new HashMap<>(); columns.put(key, PropertyType.from(value)); this.sqlgGraph.getTopology().ensureEdgePropertiesExist( this.schema, this.table,