diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java index 0de8d85e3b..e04e23fd44 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java @@ -47,6 +47,7 @@ public class EdgeLabel extends SchemaLabel { private Set> links = new HashSet<>(); private Id sourceLabel = NONE_ID; + private Id targetLabel = NONE_ID; private Frequency frequency; private List sortKeys; @@ -136,7 +137,12 @@ public void sourceLabel(Id id) { E.checkArgument(this.links.isEmpty(), "Not allowed add source label to an edge label which " + "already has links"); - this.sourceLabel = id; + if (this.targetLabel != NONE_ID) { + this.links.add(Pair.of(id, this.targetLabel)); + this.targetLabel = NONE_ID; + } else { + this.sourceLabel = id; + } } public String targetLabelName() { @@ -158,11 +164,12 @@ public void targetLabel(Id id) { E.checkArgument(this.links.isEmpty(), "Not allowed add source label to an edge label which " + "already has links"); - E.checkArgument(this.sourceLabel != NONE_ID, - "Not allowed add target label to an edge label which " + - "not has source label yet"); - this.links.add(Pair.of(this.sourceLabel, id)); - this.sourceLabel = NONE_ID; + if (this.sourceLabel != NONE_ID) { + this.links.add(Pair.of(this.sourceLabel, id)); + this.sourceLabel = NONE_ID; + } else { + this.targetLabel = id; + } } public boolean linkWithLabel(Id id) { diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java index df24010a1d..410b094fb9 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java @@ -31,7 +31,6 @@ import org.apache.hugegraph.backend.id.Id; import org.apache.hugegraph.backend.id.IdGenerator; import org.apache.hugegraph.backend.tx.ISchemaTransaction; -import org.apache.hugegraph.backend.tx.SchemaTransaction; import org.apache.hugegraph.exception.ExistedException; import org.apache.hugegraph.exception.NotAllowException; import org.apache.hugegraph.exception.NotFoundException; @@ -57,6 +56,7 @@ public class EdgeLabelBuilder extends AbstractBuilder private EdgeLabelType edgeLabelType; private String fatherLabel; private String sourceLabel; + private String targetLabel; private Frequency frequency; private Set properties; private List sortKeys; @@ -425,7 +425,12 @@ public EdgeLabelBuilder sourceLabel(String label) { E.checkArgument(this.links.isEmpty(), "Not allowed add source label to an edge label which " + "already has links"); - this.sourceLabel = label; + if (this.targetLabel != null) { + this.links.add(Pair.of(label, this.targetLabel)); + this.targetLabel = null; + } else { + this.sourceLabel = label; + } return this; } @@ -434,11 +439,12 @@ public EdgeLabelBuilder targetLabel(String label) { E.checkArgument(this.links.isEmpty(), "Not allowed add source label to an edge label which " + "already has links"); - E.checkArgument(this.sourceLabel != null, - "Not allowed add target label to an edge label which " + - "not has source label yet"); - this.links.add(Pair.of(this.sourceLabel, label)); - this.sourceLabel = null; + if (this.sourceLabel != null) { + this.links.add(Pair.of(this.sourceLabel, label)); + this.sourceLabel = null; + } else { + this.targetLabel = label; + } return this; } @@ -637,6 +643,11 @@ private void checkStableVars() { "Not allowed to update source label " + "for edge label '%s', it must be null", this.name); } + if (this.targetLabel != null) { + throw new NotAllowException( + "Not allowed to update target label " + + "for edge label '%s', it must be null", this.name); + } if (this.links != null && !this.links.isEmpty()) { throw new NotAllowException( "Not allowed to update source/target label " + diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeLabelCoreTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeLabelCoreTest.java index d66f78de3a..3b81c2f16e 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeLabelCoreTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeLabelCoreTest.java @@ -1184,7 +1184,7 @@ public void testEliminateVertexLabelWithoutUserdata() { }); Assert.assertThrows(HugeException.class, () -> { - schema.edgeLabel("write").sourceLabel("person2").targetLabel("person2").eliminate(); + schema.edgeLabel("write").targetLabel("person2").eliminate(); }); Assert.assertThrows(HugeException.class, () -> {