Skip to content

Commit d7955af

Browse files
Refactor(loader): merge duplicate label creation logic of GraphSource
1 parent 7a1b66c commit d7955af

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

hugegraph-loader/src/main/java/org/apache/hugegraph/loader/HugeGraphLoader.java

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.hugegraph.loader.task.TaskManager;
4545
import org.apache.hugegraph.loader.util.HugeClientHolder;
4646
import org.apache.hugegraph.loader.util.LoadUtil;
47+
import org.apache.hugegraph.structure.schema.SchemaLabel;
4748
import org.apache.hugegraph.util.ExecutorUtil;
4849
import org.apache.hugegraph.loader.util.Printer;
4950
import org.apache.hugegraph.structure.schema.EdgeLabel;
@@ -336,13 +337,61 @@ private void createSchema() {
336337
* @param graphSource
337338
*/
338339
private void createGraphSourceSchema(GraphSource graphSource) {
339-
340340
try (HugeClient sourceClient = graphSource.createHugeClient();
341341
HugeClient client = HugeClientHolder.create(this.options, false)) {
342-
343342
createGraphSourceVertexLabel(sourceClient, client, graphSource);
344343
createGraphSourceEdgeLabel(sourceClient, client, graphSource);
345344
createGraphSourceIndexLabel(sourceClient, client, graphSource);
345+
} catch (Exception e) {
346+
LOG.error("Failed to create graph source schema for {}: {}",
347+
graphSource.getGraph(), e.getMessage(), e);
348+
throw new LoadException("Schema creation failed", e);
349+
}
350+
}
351+
352+
// handles labels (can be used for both VertexLabel and EdgeLabel)
353+
private void createGraphSourceLabels(
354+
HugeClient sourceClient,
355+
HugeClient targetClient,
356+
List<? extends SchemaLabel> labels, // VertexLabel or EdgeLabel
357+
Map<String, GraphSource.SeletedLabelDes> selectedMap,
358+
Map<String, GraphSource.IgnoredLabelDes> ignoredMap,
359+
boolean isVertex) {
360+
361+
for (SchemaLabel label : labels) {
362+
if (ignoredMap.containsKey(label.name())) {
363+
GraphSource.IgnoredLabelDes des
364+
= ignoredMap.get(label.name());
365+
366+
if (des.getProperties() != null) {
367+
des.getProperties()
368+
.forEach((p) -> label.properties().remove(p));
369+
}
370+
}
371+
372+
Set<String> existedPKs =
373+
targetClient.schema().getPropertyKeys().stream()
374+
.map(pk -> pk.name()).collect(Collectors.toSet());
375+
376+
for (String pkName : label.properties()) {
377+
PropertyKey pk = sourceClient.schema()
378+
.getPropertyKey(pkName);
379+
if (!existedPKs.contains(pk.name())) {
380+
targetClient.schema().addPropertyKey(pk);
381+
}
382+
}
383+
384+
if (isVertex) {
385+
if (!(label instanceof VertexLabel)) {
386+
throw new IllegalArgumentException("Expected VertexLabel but got " + label.getClass());
387+
}
388+
targetClient.schema().addVertexLabel((VertexLabel) label);
389+
} else {
390+
if (!(label instanceof EdgeLabel)) {
391+
throw new IllegalArgumentException("Expected EdgeLabel but got " + label.getClass());
392+
}
393+
targetClient.schema().addEdgeLabel((EdgeLabel) label);
394+
}
346395
}
347396
}
348397

@@ -401,31 +450,8 @@ private void createGraphSourceVertexLabel(HugeClient sourceClient,
401450
}
402451
}
403452

404-
for (VertexLabel vertexLabel : vertexLabels) {
405-
if (mapIgnoredVertices.containsKey(vertexLabel.name())) {
406-
GraphSource.IgnoredLabelDes des
407-
= mapIgnoredVertices.get(vertexLabel.name());
408-
409-
if (des.getProperties() != null) {
410-
des.getProperties()
411-
.forEach((p) -> vertexLabel.properties().remove(p));
412-
}
413-
}
414-
415-
Set<String> existedPKs =
416-
targetClient.schema().getPropertyKeys().stream()
417-
.map(pk -> pk.name()).collect(Collectors.toSet());
418-
419-
for (String pkName : vertexLabel.properties()) {
420-
PropertyKey pk = sourceClient.schema()
421-
.getPropertyKey(pkName);
422-
if (!existedPKs.contains(pk.name())) {
423-
targetClient.schema().addPropertyKey(pk);
424-
}
425-
}
426-
427-
targetClient.schema().addVertexLabel(vertexLabel);
428-
}
453+
createGraphSourceLabels(sourceClient, targetClient, vertexLabels, mapSelectedVertices,
454+
mapIgnoredVertices, true);
429455
}
430456

431457
private void createGraphSourceEdgeLabel(HugeClient sourceClient,
@@ -478,31 +504,8 @@ private void createGraphSourceEdgeLabel(HugeClient sourceClient,
478504
}
479505
}
480506

481-
for (EdgeLabel edgeLabel : edgeLabels) {
482-
if (mapIgnoredEdges.containsKey(edgeLabel.name())) {
483-
GraphSource.IgnoredLabelDes des
484-
= mapIgnoredEdges.get(edgeLabel.name());
485-
486-
if (des.getProperties() != null) {
487-
des.getProperties()
488-
.forEach((p) -> edgeLabel.properties().remove(p));
489-
}
490-
}
491-
492-
Set<String> existedPKs =
493-
targetClient.schema().getPropertyKeys().stream()
494-
.map(pk -> pk.name()).collect(Collectors.toSet());
495-
496-
for (String pkName : edgeLabel.properties()) {
497-
PropertyKey pk = sourceClient.schema()
498-
.getPropertyKey(pkName);
499-
if (!existedPKs.contains(pk.name())) {
500-
targetClient.schema().addPropertyKey(pk);
501-
}
502-
}
503-
504-
targetClient.schema().addEdgeLabel(edgeLabel);
505-
}
507+
createGraphSourceLabels(sourceClient, targetClient, edgeLabels, mapSelectedEdges,
508+
mapIgnoredEdges, false);
506509
}
507510

508511
private void createGraphSourceIndexLabel(HugeClient sourceClient,

0 commit comments

Comments
 (0)