use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class HugeVertex method constructEdge.
public HugeEdge constructEdge(String label, HugeVertex vertex, Object... keyValues) {
ElementKeys elemKeys = HugeElement.classifyKeys(keyValues);
// Check id (must be null)
if (elemKeys.id() != null) {
throw Edge.Exceptions.userSuppliedIdsNotSupported();
}
Id id = null;
// Check target vertex
E.checkArgumentNotNull(vertex, "Target vertex can't be null");
// Check label
E.checkArgument(label != null && !label.isEmpty(), "Edge label can't be null or empty");
EdgeLabel edgeLabel = this.graph().edgeLabel(label);
// Check link
E.checkArgument(edgeLabel.checkLinkEqual(this.schemaLabel().id(), vertex.schemaLabel().id()), "Undefined link of edge label '%s': '%s' -> '%s'", label, this.label(), vertex.label());
// Check sortKeys
List<Id> keys = this.graph().mapPkName2Id(elemKeys.keys());
E.checkArgument(keys.containsAll(edgeLabel.sortKeys()), "The sort key(s) must be set for the edge " + "with label: '%s'", edgeLabel.name());
// Check whether passed all non-null props
@SuppressWarnings("unchecked") Collection<Id> nonNullKeys = CollectionUtils.subtract(edgeLabel.properties(), edgeLabel.nullableKeys());
if (!keys.containsAll(nonNullKeys)) {
@SuppressWarnings("unchecked") Collection<Id> missed = CollectionUtils.subtract(nonNullKeys, keys);
E.checkArgument(false, "All non-null property keys: %s " + "of edge label '%s' must be set, " + "but missed keys: %s", this.graph().mapPkId2Name(nonNullKeys), edgeLabel.name(), this.graph().mapPkId2Name(missed));
}
HugeEdge edge = new HugeEdge(this, id, edgeLabel, vertex);
// Set properties
ElementHelper.attachProperties(edge, keyValues);
edge.assignId();
return edge;
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class VertexLabelRemoveJob method removeVertexLabel.
private static void removeVertexLabel(HugeGraphParams graph, Id id) {
GraphTransaction graphTx = graph.graphTransaction();
SchemaTransaction schemaTx = graph.schemaTransaction();
VertexLabel vertexLabel = schemaTx.getVertexLabel(id);
// If the vertex label does not exist, return directly
if (vertexLabel == null) {
return;
}
if (vertexLabel.status().deleting()) {
LOG.info("The vertex label '{}' has been in {} status, " + "please check if it's expected to delete it again", vertexLabel, vertexLabel.status());
}
// Check no edge label use the vertex label
List<EdgeLabel> edgeLabels = schemaTx.getEdgeLabels();
for (EdgeLabel edgeLabel : edgeLabels) {
if (edgeLabel.linkWithLabel(id)) {
throw new HugeException("Not allowed to remove vertex label '%s' " + "because the edge label '%s' still link with it", vertexLabel.name(), edgeLabel.name());
}
}
/*
* Copy index label ids because removeIndexLabel will mutate
* vertexLabel.indexLabels()
*/
Set<Id> indexLabelIds = ImmutableSet.copyOf(vertexLabel.indexLabels());
LockUtil.Locks locks = new LockUtil.Locks(graph.name());
try {
locks.lockWrites(LockUtil.VERTEX_LABEL_DELETE, id);
schemaTx.updateSchemaStatus(vertexLabel, SchemaStatus.DELETING);
try {
for (Id ilId : indexLabelIds) {
IndexLabelRemoveJob.removeIndexLabel(graph, ilId);
}
// TODO: use event to replace direct call
// Deleting a vertex will automatically deletes the held edge
graphTx.removeVertices(vertexLabel);
/*
* Should commit changes to backend store before release
* delete lock
*/
graph.graph().tx().commit();
// Remove vertex label
removeSchema(schemaTx, vertexLabel);
} catch (Throwable e) {
schemaTx.updateSchemaStatus(vertexLabel, SchemaStatus.UNDELETED);
throw e;
}
} finally {
locks.unlock();
}
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class EdgeLabelRemoveJob method removeEdgeLabel.
private static void removeEdgeLabel(HugeGraphParams graph, Id id) {
GraphTransaction graphTx = graph.graphTransaction();
SchemaTransaction schemaTx = graph.schemaTransaction();
EdgeLabel edgeLabel = schemaTx.getEdgeLabel(id);
// If the edge label does not exist, return directly
if (edgeLabel == null) {
return;
}
if (edgeLabel.status().deleting()) {
LOG.info("The edge label '{}' has been in {} status, " + "please check if it's expected to delete it again", edgeLabel, edgeLabel.status());
}
// Remove index related data(include schema) of this edge label
Set<Id> indexIds = ImmutableSet.copyOf(edgeLabel.indexLabels());
LockUtil.Locks locks = new LockUtil.Locks(graph.name());
try {
locks.lockWrites(LockUtil.EDGE_LABEL_DELETE, id);
schemaTx.updateSchemaStatus(edgeLabel, SchemaStatus.DELETING);
try {
for (Id indexId : indexIds) {
IndexLabelRemoveJob.removeIndexLabel(graph, indexId);
}
// Remove all edges which has matched label
// TODO: use event to replace direct call
graphTx.removeEdges(edgeLabel);
/*
* Should commit changes to backend store before release
* delete lock
*/
graph.graph().tx().commit();
// Remove edge label
removeSchema(schemaTx, edgeLabel);
} catch (Throwable e) {
schemaTx.updateSchemaStatus(edgeLabel, SchemaStatus.UNDELETED);
throw e;
}
} finally {
locks.unlock();
}
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class EdgeLabelBuilder method append.
@Override
public EdgeLabel append() {
EdgeLabel edgeLabel = this.edgeLabelOrNull(this.name);
if (edgeLabel == null) {
throw new NotFoundException("Can't update edge label '%s' " + "since it doesn't exist", this.name);
}
// These methods will check params and fill to member variables
this.checkStableVars();
this.checkProperties(Action.APPEND);
this.checkNullableKeys(Action.APPEND);
Userdata.check(this.userdata, Action.APPEND);
for (String key : this.properties) {
PropertyKey propertyKey = this.graph().propertyKey(key);
edgeLabel.property(propertyKey.id());
}
for (String key : this.nullableKeys) {
PropertyKey propertyKey = this.graph().propertyKey(key);
edgeLabel.nullableKey(propertyKey.id());
}
edgeLabel.userdata(this.userdata);
this.graph().addEdgeLabel(edgeLabel);
return edgeLabel;
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class EdgeLabelBuilder method checkNullableKeys.
@SuppressWarnings("unchecked")
private void checkNullableKeys(Action action) {
// Not using switch-case to avoid indent too much
if (action == Action.ELIMINATE) {
if (!this.nullableKeys.isEmpty()) {
throw new NotAllowException("Not support to eliminate nullableKeys " + "for edge label currently");
}
return;
}
EdgeLabel edgeLabel = this.edgeLabelOrNull(this.name);
// The originProps is empty when firstly create edge label
List<String> originProps = edgeLabel == null ? ImmutableList.of() : this.graph().mapPkId2Name(edgeLabel.properties());
Set<String> appendProps = this.properties;
E.checkArgument(CollectionUtil.union(originProps, appendProps).containsAll(this.nullableKeys), "The nullableKeys: %s to be created or appended " + "must belong to the origin/new properties: %s/%s ", this.nullableKeys, originProps, appendProps);
List<String> sortKeys = edgeLabel == null ? this.sortKeys : this.graph().mapPkId2Name(edgeLabel.sortKeys());
E.checkArgument(!CollectionUtil.hasIntersection(sortKeys, this.nullableKeys), "The nullableKeys: %s are not allowed to " + "belong to sortKeys: %s of edge label '%s'", this.nullableKeys, sortKeys, this.name);
if (action == Action.APPEND) {
Collection<String> newAddedProps = CollectionUtils.subtract(appendProps, originProps);
E.checkArgument(this.nullableKeys.containsAll(newAddedProps), "The new added properties: %s must be nullable", newAddedProps);
}
}
Aggregations