Skip to content

Commit

Permalink
minor improve for source + target label builder
Browse files Browse the repository at this point in the history
  • Loading branch information
VGalaxies committed Sep 29, 2024
1 parent 19485e5 commit 87e76ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class EdgeLabel extends SchemaLabel {

private Set<Pair<Id, Id>> links = new HashSet<>();
private Id sourceLabel = NONE_ID;
private Id targetLabel = NONE_ID;
private Frequency frequency;
private List<Id> sortKeys;

Expand Down Expand Up @@ -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;

Check warning on line 142 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java#L141-L142

Added lines #L141 - L142 were not covered by tests
} else {
this.sourceLabel = id;
}
}

public String targetLabelName() {
Expand All @@ -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;

Check warning on line 171 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java#L171

Added line #L171 was not covered by tests
}
}

public boolean linkWithLabel(Id id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> properties;
private List<String> sortKeys;
Expand Down Expand Up @@ -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;

Check warning on line 430 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java#L429-L430

Added lines #L429 - L430 were not covered by tests
} else {
this.sourceLabel = label;
}
return this;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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(

Check warning on line 652 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/builder/EdgeLabelBuilder.java#L652

Added line #L652 was not covered by tests
"Not allowed to update source/target label " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, () -> {
Expand Down

0 comments on commit 87e76ff

Please sign in to comment.