Skip to content

Commit c672e35

Browse files
ThespicaimbajinVGalaxies
authored
chore(client): patch for father sub edge (#654)
--------- Co-authored-by: imbajin <[email protected]> Co-authored-by: VGalaxies <[email protected]>
1 parent a798704 commit c672e35

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

hugegraph-client/src/main/java/org/apache/hugegraph/structure/constant/EdgeLabelType.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ public enum EdgeLabelType {
2323

2424
PARENT(1, "PARENT"),
2525

26-
SUB(2, "SUB");
26+
SUB(2, "SUB"),
2727

28-
private final byte code;
29-
private final String name;
28+
GENERAL(3, "GENERAL");
29+
30+
private byte code = 0;
31+
private String name = null;
3032

3133
EdgeLabelType(int code, String name) {
3234
assert code < 256;
@@ -46,6 +48,10 @@ public boolean normal() {
4648
return this == NORMAL;
4749
}
4850

51+
public boolean general() {
52+
return this == GENERAL;
53+
}
54+
4955
public byte code() {
5056
return this.code;
5157
}

hugegraph-client/src/main/java/org/apache/hugegraph/structure/schema/EdgeLabel.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public boolean sub() {
8181
return this.edgeLabelType.sub();
8282
}
8383

84+
public boolean normal() {
85+
return this.edgeLabelType.normal();
86+
}
87+
88+
public boolean general() {
89+
return this.edgeLabelType.general();
90+
}
91+
8492
public String parentLabel() {
8593
return this.parentLabel;
8694
}
@@ -170,6 +178,8 @@ public interface Builder extends SchemaBuilder<EdgeLabel> {
170178

171179
Builder withBase(String fatherLabel);
172180

181+
Builder asGeneral();
182+
173183
/**
174184
* Set the source label of the edge label
175185
*/
@@ -277,6 +287,12 @@ public Builder withBase(String parentLabel) {
277287
return this;
278288
}
279289

290+
@Override
291+
public Builder asGeneral() {
292+
this.edgeLabel.edgeLabelType = EdgeLabelType.GENERAL;
293+
return this;
294+
}
295+
280296
/**
281297
* Set the source label of the edge label
282298
*/

hugegraph-client/src/main/java/org/apache/hugegraph/structure/schema/VertexLabel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ public IdStrategy idStrategy() {
5959
return this.idStrategy;
6060
}
6161

62+
public void idStrategy(IdStrategy idStrategy) {
63+
this.idStrategy = idStrategy;
64+
}
65+
6266
public List<String> primaryKeys() {
6367
return this.primaryKeys;
6468
}
6569

70+
public void primaryKeys(List<String> primaryKeys) {
71+
this.primaryKeys = primaryKeys;
72+
}
73+
6674
public long ttl() {
6775
return this.ttl;
6876
}

hugegraph-client/src/test/java/org/apache/hugegraph/api/EdgeLabelApiTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,39 @@ public void testAddEdgeLabelWithUserData() {
543543
createTime = DateUtil.parse(time);
544544
Utils.assertBeforeNow(createTime);
545545
}
546+
547+
@Test
548+
public void testEdgeLabelType() {
549+
EdgeLabel normalEdgeLabel = schema().edgeLabel("created")
550+
.sourceLabel("person")
551+
.targetLabel("software")
552+
.singleTime()
553+
.properties("date", "city")
554+
.build();
555+
Assert.assertTrue(normalEdgeLabel.normal());
556+
557+
EdgeLabel parentEdgeLabelType = schema().edgeLabel("write")
558+
.link("person", "book")
559+
.properties("date", "weight")
560+
.singleTime()
561+
.asBase()
562+
.build();
563+
Assert.assertTrue(parentEdgeLabelType.parent());
564+
565+
EdgeLabel subEdgeLabelType = schema().edgeLabel("knows")
566+
.sourceLabel("person")
567+
.targetLabel("person")
568+
.singleTime()
569+
.properties("date")
570+
.withBase("write")
571+
.build();
572+
Assert.assertTrue(subEdgeLabelType.sub());
573+
574+
EdgeLabel generalEdgeLabelType = schema().edgeLabel("father")
575+
.link("person", "person")
576+
.properties("weight")
577+
.asGeneral()
578+
.build();
579+
Assert.assertTrue(generalEdgeLabelType.general());
580+
}
546581
}

hugegraph-loader/src/main/java/org/apache/hugegraph/loader/builder/EdgeBuilder.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.hugegraph.loader.executor.LoadContext;
2929
import org.apache.hugegraph.loader.mapping.EdgeMapping;
3030
import org.apache.hugegraph.loader.mapping.InputStruct;
31+
import org.apache.hugegraph.structure.constant.IdStrategy;
3132
import org.apache.hugegraph.structure.graph.Edge;
3233
import org.apache.hugegraph.structure.graph.Vertex;
3334
import org.apache.hugegraph.structure.schema.EdgeLabel;
@@ -55,10 +56,19 @@ public EdgeBuilder(LoadContext context, InputStruct struct,
5556
super(context, struct);
5657
this.mapping = mapping;
5758
this.edgeLabel = this.getEdgeLabel(this.mapping.label());
58-
this.sourceLabel = this.getVertexLabel(this.edgeLabel.sourceLabel());
59-
this.targetLabel = this.getVertexLabel(this.edgeLabel.targetLabel());
6059
this.nonNullKeys = this.nonNullableKeys(this.edgeLabel);
61-
// Ensure that the source/target id fields are matched with id strategy
60+
if (this.edgeLabel.edgeLabelType().general()) {
61+
// If create a general type edge, the loader can't obtain the vertexlabel info of both ends
62+
// Therefore, the IdStrategy of both ends is uniformly set to CUSTOMIZE_STRING
63+
this.sourceLabel = new VertexLabel("~general");
64+
this.targetLabel = new VertexLabel("~general");
65+
this.sourceLabel.idStrategy(IdStrategy.CUSTOMIZE_STRING);
66+
this.targetLabel.idStrategy(IdStrategy.CUSTOMIZE_STRING);
67+
} else {
68+
this.sourceLabel = this.getVertexLabel(this.edgeLabel.sourceLabel());
69+
this.targetLabel = this.getVertexLabel(this.edgeLabel.targetLabel());
70+
}
71+
// Ensure that the source/target id fileds are matched with id strategy
6272
this.checkIdFields(this.sourceLabel, this.mapping.sourceFields());
6373
this.checkIdFields(this.targetLabel, this.mapping.targetFields());
6474

0 commit comments

Comments
 (0)