|
44 | 44 | import org.apache.hugegraph.loader.task.TaskManager; |
45 | 45 | import org.apache.hugegraph.loader.util.HugeClientHolder; |
46 | 46 | import org.apache.hugegraph.loader.util.LoadUtil; |
| 47 | +import org.apache.hugegraph.structure.schema.SchemaLabel; |
47 | 48 | import org.apache.hugegraph.util.ExecutorUtil; |
48 | 49 | import org.apache.hugegraph.loader.util.Printer; |
49 | 50 | import org.apache.hugegraph.structure.schema.EdgeLabel; |
@@ -336,13 +337,61 @@ private void createSchema() { |
336 | 337 | * @param graphSource |
337 | 338 | */ |
338 | 339 | private void createGraphSourceSchema(GraphSource graphSource) { |
339 | | - |
340 | 340 | try (HugeClient sourceClient = graphSource.createHugeClient(); |
341 | 341 | HugeClient client = HugeClientHolder.create(this.options, false)) { |
342 | | - |
343 | 342 | createGraphSourceVertexLabel(sourceClient, client, graphSource); |
344 | 343 | createGraphSourceEdgeLabel(sourceClient, client, graphSource); |
345 | 344 | 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 | + } |
346 | 395 | } |
347 | 396 | } |
348 | 397 |
|
@@ -401,31 +450,8 @@ private void createGraphSourceVertexLabel(HugeClient sourceClient, |
401 | 450 | } |
402 | 451 | } |
403 | 452 |
|
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); |
429 | 455 | } |
430 | 456 |
|
431 | 457 | private void createGraphSourceEdgeLabel(HugeClient sourceClient, |
@@ -478,31 +504,8 @@ private void createGraphSourceEdgeLabel(HugeClient sourceClient, |
478 | 504 | } |
479 | 505 | } |
480 | 506 |
|
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); |
506 | 509 | } |
507 | 510 |
|
508 | 511 | private void createGraphSourceIndexLabel(HugeClient sourceClient, |
|
0 commit comments