Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(algorithm): support single source shortest path algorithm #285

Merged
merged 24 commits into from
Mar 12, 2024

Conversation

diaohancai
Copy link
Contributor

@diaohancai diaohancai commented Nov 21, 2023

Purpose of the PR

Main Changes

Add single source shortest path algorithm.
There are 3 types of target vertex

  1. single target:
single_source_shortest_path.source_id="\"A\""
single_source_shortest_path.target_id="\"E\""
  1. multiple target:
single_source_shortest_path.source_id="\"A\""
single_source_shortest_path.target_id="\"E\", \"C\""
  1. all target:
single_source_shortest_path.source_id="\"A\""
single_source_shortest_path.target_id=*

Verifying these changes

  • Trivial rework / code cleanup without any test coverage. (No Need)
  • Already covered by existing tests, such as (please modify tests here).
  • Need tests and can be verified as follows.
    Unit test: org.apache.hugegraph.computer.algorithm.path.shortest.SingleSourceShortestPathTest#testRunAlgorithm

Does this PR potentially affect the following parts?

  • Nope
  • Dependencies (add/update license info)
  • Modify configurations
  • The public API
  • Other affects (typed here)

Documentation Status

  • Doc - TODO
  • Doc - Done
  • Doc - No Need

Copy link

codecov bot commented Dec 4, 2023

Codecov Report

Attention: Patch coverage is 68.20276% with 69 lines in your changes are missing coverage. Please review.

Project coverage is 84.97%. Comparing base (ff85e34) to head (d55c7c7).
Report is 13 commits behind head on master.

Files Patch % Lines
...orithm/path/shortest/SingleSourceShortestPath.java 62.88% 18 Missing and 18 partials ⚠️
...rg/apache/hugegraph/computer/core/util/IdUtil.java 21.42% 8 Missing and 3 partials ⚠️
...ph/computer/core/combiner/IdListMergeCombiner.java 0.00% 10 Missing ⚠️
...ache/hugegraph/computer/core/util/JsonUtilExt.java 0.00% 6 Missing ⚠️
...he/hugegraph/computer/core/graph/id/IdFactory.java 42.85% 3 Missing and 1 partial ⚠️
...ath/shortest/SingleSourceShortestPathCombiner.java 75.00% 0 Missing and 1 partial ⚠️
...m/path/shortest/SingleSourceShortestPathValue.java 96.87% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #285      +/-   ##
============================================
- Coverage     85.03%   84.97%   -0.07%     
- Complexity     3246     3359     +113     
============================================
  Files           345      360      +15     
  Lines         12298    12678     +380     
  Branches       1102     1149      +47     
============================================
+ Hits          10458    10773     +315     
- Misses         1315     1341      +26     
- Partials        525      564      +39     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Dec 26, 2023
@diaohancai diaohancai requested a review from javeme December 26, 2023 17:20
@diaohancai diaohancai changed the title feat(algorithm): support source target shortest path algorithm feat(algorithm): support single source shortest path algorithm Dec 26, 2023
@diaohancai diaohancai requested a review from javeme January 1, 2024 05:53
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Jan 25, 2024
@diaohancai diaohancai requested a review from javeme January 25, 2024 16:01

import org.apache.commons.lang3.StringUtils;

public enum IdCategory {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why not reuse IdType

Copy link
Contributor Author

@diaohancai diaohancai Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IdType: LONG, UTF8, UUID.
IdCategory: NUMBER, STRING, UUID.
I suppose that IdType is too technical and IdCategory is more user-friendly.
In fact, they correspond one to one.·

Copy link
Contributor Author

@diaohancai diaohancai Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove IdCategory, reuse IdType if necessary.

if (this.targetQuantityType != QuantityType.ALL) {
this.targetIdSet = new IdSet();
for (String targetIdStr : this.targetIdStr.split(",")) {
targetIdSet.add(IdUtil.parseId(this.vertexIdType, targetIdStr));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems the targets id should be different types? like [111, "abc", "222", 333, U"uuid..."]

@diaohancai diaohancai requested a review from javeme February 6, 2024 14:46
@@ -40,8 +40,8 @@ public class SingleSourceShortestPathTest extends AlgorithmTestBase {
public static final String EL = "road";
public static final String PROPERTY_KEY = "distance";

public static final String SOURCE_ID = "A";
public static final String TARGET_ID = "E";
public static final String SOURCE_ID = "{\"id\": \"A\", \"idType\": \"string\"}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use id format like this (ref VertexAPI.checkAndParseVertexId()):

String SOURCE_ID = "\"abc\"";
String SOURCE_ID = "\"123\"";
String SOURCE_ID = "123";
String SOURCE_ID = "U\"uuid-xxxx\"";

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

// todo move to org.apache.hugegraph.util.JsonUtil later
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect format // TODO: xx

vertex.inactivate();
return;
}

if (vertex.numEdges() <= 0) {
// isolated vertex
LOG.debug("source vertex {} can not reach target vertex {}",
this.sourceIdStr, this.targetIdStr);
LOG.debug("source vertex {} is isolated", this.sourceId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this format is more convenient for retrieving logs: The source vertex is isolated: {}

@@ -157,17 +158,16 @@ public void compute0(ComputationContext context, Vertex vertex) {

// single target && source == target
if (this.targetQuantityType == QuantityType.SINGLE &&
this.sourceIdStr.equals(this.targetIdStr)) {
this.sourceId.equals(this.targetIdSet.value().iterator().next())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a private method targetIdOne() for this.targetIdSet.value().iterator().next()? we can also check there exist one element in targetIdOne()

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Feb 29, 2024
@diaohancai diaohancai requested a review from javeme February 29, 2024 05:27
Assert.assertEquals(IdType.UUID, IdUtil.parseId(IdCategory.UUID, uuid).idType());
String utf81 = "\"abc\"";
String utf82 = "\"222\"";
String l = "222";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idLong

Assert.assertEquals(IdType.LONG, IdUtil.parseId(IdCategory.NUMBER, "222").idType());
Assert.assertEquals(IdType.UTF8, IdUtil.parseId(IdCategory.STRING, "aaa222").idType());
Assert.assertEquals(IdType.UUID, IdUtil.parseId(IdCategory.UUID, uuid).idType());
String utf81 = "\"abc\"";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idUtf8WithString

Assert.assertEquals(IdType.UTF8, IdUtil.parseId(IdCategory.STRING, "aaa222").idType());
Assert.assertEquals(IdType.UUID, IdUtil.parseId(IdCategory.UUID, uuid).idType());
String utf81 = "\"abc\"";
String utf82 = "\"222\"";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idUtf8WithNumber

Assert.assertEquals(IdType.UTF8, IdUtil.parseId(utf81).idType());
Assert.assertEquals(IdType.UTF8, IdUtil.parseId(utf82).idType());
Assert.assertEquals(IdType.LONG, IdUtil.parseId(l).idType());
Assert.assertEquals(IdType.UUID, IdUtil.parseId(uuid).idType());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add some exception cases, like idEmpty/idDouble/uuidInvalid

if (idCategory == null) {
// automatic inference
return parseId(idStr);
if (idStr.startsWith("U\"")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also move to try-catch?

@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Mar 3, 2024
@diaohancai diaohancai requested a review from javeme March 3, 2024 05:43
Assert.assertThrows(ComputerException.class, () -> {
IdUtil.parseId(idNull).idType();
});
Assert.assertThrows(ComputerException.class, () -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also keep IllegalArgumentException?

@diaohancai diaohancai requested a review from javeme March 3, 2024 05:55
Copy link
Contributor

@javeme javeme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 3, 2024
@imbajin imbajin requested a review from coderzc March 4, 2024 08:17
@imbajin imbajin merged commit 192ef3b into apache:master Mar 12, 2024
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:XXL This PR changes 1000+ lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants