From b3016dc34e3dca84c0880824922001111b782c7e Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 26 Aug 2020 14:35:51 +0800 Subject: [PATCH 1/2] fix OLTP algorithm not check if source/target vertex exist Change-Id: I068f5b3f3bb57d47c284309023cc6e9b8ab4c140 --- .../hugegraph/traversal/algorithm/CountTraverser.java | 3 ++- .../hugegraph/traversal/algorithm/HugeTraverser.java | 11 +++++++++++ .../traversal/algorithm/NeighborRankTraverser.java | 1 + .../hugegraph/traversal/algorithm/PathsTraverser.java | 2 ++ .../traversal/algorithm/PersonalRankTraverser.java | 1 + .../traversal/algorithm/ShortestPathTraverser.java | 4 ++++ .../algorithm/SingleSourceShortestPathTraverser.java | 2 ++ .../traversal/algorithm/SubGraphTraverser.java | 1 + 8 files changed, 24 insertions(+), 1 deletion(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java index 145bd696d8..0837b61623 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java @@ -50,7 +50,8 @@ public CountTraverser(HugeGraph graph) { public long count(Id source, List steps, boolean containsTraversed, long dedupSize) { - E.checkArgumentNotNull(source, "The source can't be null"); + E.checkNotNull(source, "source vertex"); + this.checkVertexExist(source); E.checkArgument(steps != null && !steps.isEmpty(), "The steps can't be empty"); checkDedupSize(dedupSize); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java index 70314198d4..e591726b56 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java @@ -92,6 +92,7 @@ public Set kout(Id sourceV, Directions dir, String label, int depth, boolean nearest, long degree, long capacity, long limit) { E.checkNotNull(sourceV, "source vertex id"); + this.checkVertexExist(sourceV); E.checkNotNull(dir, "direction"); checkPositive(depth, "k-out max_depth"); checkDegree(degree); @@ -148,6 +149,7 @@ public Set kneighbor(Id sourceV, Directions dir, String label, int depth, long degree, long limit) { E.checkNotNull(sourceV, "source vertex id"); + this.checkVertexExist(sourceV); E.checkNotNull(dir, "direction"); checkPositive(depth, "k-neighbor max_depth"); checkDegree(degree); @@ -178,6 +180,8 @@ public Set sameNeighbors(Id vertex, Id other, Directions direction, String label, long degree, long limit) { E.checkNotNull(vertex, "vertex id"); E.checkNotNull(other, "the other vertex id"); + this.checkVertexExist(vertex); + this.checkVertexExist(other); E.checkNotNull(direction, "direction"); checkDegree(degree); checkLimit(limit); @@ -201,6 +205,8 @@ public double jaccardSimilarity(Id vertex, Id other, Directions dir, String label, long degree) { E.checkNotNull(vertex, "vertex id"); E.checkNotNull(other, "the other vertex id"); + this.checkVertexExist(vertex); + this.checkVertexExist(other); E.checkNotNull(dir, "direction"); checkDegree(degree); @@ -380,6 +386,11 @@ protected Id getEdgeLabelId(Object label) { return SchemaLabel.getLabelId(this.graph, HugeType.EDGE, label); } + protected void checkVertexExist(Id vertexId) { + // Throw NotFoundException if not exist vertex with id 'vertexId' + this.graph.vertex(vertexId); + } + public static void checkDegree(long degree) { checkPositiveOrNoLimit(degree, "max degree"); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java index 5187911c90..3b05075fe0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java @@ -54,6 +54,7 @@ public NeighborRankTraverser(HugeGraph graph, double alpha, long capacity) { public List> neighborRank(Id source, List steps) { E.checkArgumentNotNull(source, "The source vertex id can't be null"); + this.checkVertexExist(source); E.checkArgument(!steps.isEmpty(), "The steps can't be empty"); MultivaluedMap sources = newMultivalueMap(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java index 683985cd15..281863f354 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java @@ -46,6 +46,8 @@ public PathSet paths(Id sourceV, Directions sourceDir, int depth, long degree, long capacity, long limit) { E.checkNotNull(sourceV, "source vertex id"); E.checkNotNull(targetV, "target vertex id"); + this.checkVertexExist(sourceV); + this.checkVertexExist(targetV); E.checkNotNull(sourceDir, "source direction"); E.checkNotNull(targetDir, "target direction"); E.checkArgument(sourceDir == targetDir || diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java index 5e084cb554..c72126c413 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java @@ -54,6 +54,7 @@ public PersonalRankTraverser(HugeGraph graph, double alpha, public Map personalRank(Id source, String label, WithLabel withLabel) { E.checkArgumentNotNull(source, "The source vertex id can't be null"); + this.checkVertexExist(source); E.checkArgumentNotNull(label, "The edge label can't be null"); Map ranks = new HashMap<>(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java index 9698658834..bedf766ef5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java @@ -44,6 +44,8 @@ public Path shortestPath(Id sourceV, Id targetV, Directions dir, long skipDegree, long capacity) { E.checkNotNull(sourceV, "source vertex id"); E.checkNotNull(targetV, "target vertex id"); + this.checkVertexExist(sourceV); + this.checkVertexExist(targetV); E.checkNotNull(dir, "direction"); checkPositive(depth, "max depth"); checkDegree(degree); @@ -84,6 +86,8 @@ public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir, long skipDegree, long capacity) { E.checkNotNull(sourceV, "source vertex id"); E.checkNotNull(targetV, "target vertex id"); + this.checkVertexExist(sourceV); + this.checkVertexExist(targetV); E.checkNotNull(dir, "direction"); checkPositive(depth, "max depth"); checkDegree(degree); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java index ed2dfd8989..27681e88f6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java @@ -50,6 +50,7 @@ public WeightedPaths singleSourceShortestPaths(Id sourceV, Directions dir, long degree, long skipDegree, long capacity, long limit) { E.checkNotNull(sourceV, "source vertex id"); + this.checkVertexExist(sourceV); E.checkNotNull(dir, "direction"); checkDegree(degree); checkCapacity(capacity); @@ -75,6 +76,7 @@ public NodeWithWeight weightedShortestPath(Id sourceV, Id targetV, String weight, long degree, long skipDegree, long capacity) { E.checkNotNull(sourceV, "source vertex id"); + this.checkVertexExist(sourceV); E.checkNotNull(dir, "direction"); E.checkNotNull(weight, "weight property"); checkDegree(degree); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java index eba6f7b62b..61cb8ea918 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java @@ -60,6 +60,7 @@ private PathSet subGraphPaths(Id sourceV, Directions dir, String label, long limit, boolean rings, boolean sourceInRing) { E.checkNotNull(sourceV, "source vertex id"); + this.checkVertexExist(sourceV); E.checkNotNull(dir, "direction"); checkPositive(depth, "max depth"); checkDegree(degree); From fb6909c0de4cef1195240add865ce8f24703c261 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 26 Aug 2020 15:22:33 +0800 Subject: [PATCH 2/2] improve exception type Change-Id: I7eb4ceaccdc4a6b2a4a7b944edc83dff64a98f5d --- .../traversal/algorithm/CountTraverser.java | 4 ++-- .../traversal/algorithm/HugeTraverser.java | 23 +++++++++++-------- .../algorithm/NeighborRankTraverser.java | 4 ++-- .../traversal/algorithm/PathsTraverser.java | 4 ++-- .../algorithm/PersonalRankTraverser.java | 4 ++-- .../algorithm/ShortestPathTraverser.java | 8 +++---- .../SingleSourceShortestPathTraverser.java | 4 ++-- .../algorithm/SubGraphTraverser.java | 2 +- 8 files changed, 29 insertions(+), 24 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java index 0837b61623..b8ce2c258f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CountTraverser.java @@ -50,8 +50,8 @@ public CountTraverser(HugeGraph graph) { public long count(Id source, List steps, boolean containsTraversed, long dedupSize) { - E.checkNotNull(source, "source vertex"); - this.checkVertexExist(source); + E.checkNotNull(source, "source vertex id"); + this.checkVertexExist(source, "source vertex"); E.checkArgument(steps != null && !steps.isEmpty(), "The steps can't be empty"); checkDedupSize(dedupSize); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java index e591726b56..0cc21abf50 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/HugeTraverser.java @@ -44,6 +44,7 @@ import com.baidu.hugegraph.backend.query.Query; import com.baidu.hugegraph.backend.query.QueryResults; import com.baidu.hugegraph.backend.tx.GraphTransaction; +import com.baidu.hugegraph.exception.NotFoundException; import com.baidu.hugegraph.iterator.ExtendableIterator; import com.baidu.hugegraph.iterator.MapperIterator; import com.baidu.hugegraph.schema.SchemaLabel; @@ -92,7 +93,7 @@ public Set kout(Id sourceV, Directions dir, String label, int depth, boolean nearest, long degree, long capacity, long limit) { E.checkNotNull(sourceV, "source vertex id"); - this.checkVertexExist(sourceV); + this.checkVertexExist(sourceV, "source vertex"); E.checkNotNull(dir, "direction"); checkPositive(depth, "k-out max_depth"); checkDegree(degree); @@ -149,7 +150,7 @@ public Set kneighbor(Id sourceV, Directions dir, String label, int depth, long degree, long limit) { E.checkNotNull(sourceV, "source vertex id"); - this.checkVertexExist(sourceV); + this.checkVertexExist(sourceV, "source vertex"); E.checkNotNull(dir, "direction"); checkPositive(depth, "k-neighbor max_depth"); checkDegree(degree); @@ -180,8 +181,8 @@ public Set sameNeighbors(Id vertex, Id other, Directions direction, String label, long degree, long limit) { E.checkNotNull(vertex, "vertex id"); E.checkNotNull(other, "the other vertex id"); - this.checkVertexExist(vertex); - this.checkVertexExist(other); + this.checkVertexExist(vertex, "vertex"); + this.checkVertexExist(other, "other vertex"); E.checkNotNull(direction, "direction"); checkDegree(degree); checkLimit(limit); @@ -205,8 +206,8 @@ public double jaccardSimilarity(Id vertex, Id other, Directions dir, String label, long degree) { E.checkNotNull(vertex, "vertex id"); E.checkNotNull(other, "the other vertex id"); - this.checkVertexExist(vertex); - this.checkVertexExist(other); + this.checkVertexExist(vertex, "vertex"); + this.checkVertexExist(other, "other vertex"); E.checkNotNull(dir, "direction"); checkDegree(degree); @@ -386,9 +387,13 @@ protected Id getEdgeLabelId(Object label) { return SchemaLabel.getLabelId(this.graph, HugeType.EDGE, label); } - protected void checkVertexExist(Id vertexId) { - // Throw NotFoundException if not exist vertex with id 'vertexId' - this.graph.vertex(vertexId); + protected void checkVertexExist(Id vertexId, String name) { + try { + this.graph.vertex(vertexId); + } catch (NotFoundException e) { + throw new IllegalArgumentException(String.format( + "The %s with id '%s' does not exist", name, vertexId), e); + } } public static void checkDegree(long degree) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java index 3b05075fe0..2203064bf4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/NeighborRankTraverser.java @@ -53,8 +53,8 @@ public NeighborRankTraverser(HugeGraph graph, double alpha, long capacity) { } public List> neighborRank(Id source, List steps) { - E.checkArgumentNotNull(source, "The source vertex id can't be null"); - this.checkVertexExist(source); + E.checkNotNull(source, "source vertex id"); + this.checkVertexExist(source, "source vertex"); E.checkArgument(!steps.isEmpty(), "The steps can't be empty"); MultivaluedMap sources = newMultivalueMap(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java index 281863f354..cda75814f5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathsTraverser.java @@ -46,8 +46,8 @@ public PathSet paths(Id sourceV, Directions sourceDir, int depth, long degree, long capacity, long limit) { E.checkNotNull(sourceV, "source vertex id"); E.checkNotNull(targetV, "target vertex id"); - this.checkVertexExist(sourceV); - this.checkVertexExist(targetV); + this.checkVertexExist(sourceV, "source vertex"); + this.checkVertexExist(targetV, "target vertex"); E.checkNotNull(sourceDir, "source direction"); E.checkNotNull(targetDir, "target direction"); E.checkArgument(sourceDir == targetDir || diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java index c72126c413..5c87d61790 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PersonalRankTraverser.java @@ -53,8 +53,8 @@ public PersonalRankTraverser(HugeGraph graph, double alpha, public Map personalRank(Id source, String label, WithLabel withLabel) { - E.checkArgumentNotNull(source, "The source vertex id can't be null"); - this.checkVertexExist(source); + E.checkNotNull(source, "source vertex id"); + this.checkVertexExist(source, "source vertex"); E.checkArgumentNotNull(label, "The edge label can't be null"); Map ranks = new HashMap<>(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java index bedf766ef5..e72cc96139 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java @@ -44,8 +44,8 @@ public Path shortestPath(Id sourceV, Id targetV, Directions dir, long skipDegree, long capacity) { E.checkNotNull(sourceV, "source vertex id"); E.checkNotNull(targetV, "target vertex id"); - this.checkVertexExist(sourceV); - this.checkVertexExist(targetV); + this.checkVertexExist(sourceV, "source vertex"); + this.checkVertexExist(targetV, "target vertex"); E.checkNotNull(dir, "direction"); checkPositive(depth, "max depth"); checkDegree(degree); @@ -86,8 +86,8 @@ public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir, long skipDegree, long capacity) { E.checkNotNull(sourceV, "source vertex id"); E.checkNotNull(targetV, "target vertex id"); - this.checkVertexExist(sourceV); - this.checkVertexExist(targetV); + this.checkVertexExist(sourceV, "source vertex"); + this.checkVertexExist(targetV, "target vertex"); E.checkNotNull(dir, "direction"); checkPositive(depth, "max depth"); checkDegree(degree); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java index 27681e88f6..053767fef3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java @@ -50,7 +50,7 @@ public WeightedPaths singleSourceShortestPaths(Id sourceV, Directions dir, long degree, long skipDegree, long capacity, long limit) { E.checkNotNull(sourceV, "source vertex id"); - this.checkVertexExist(sourceV); + this.checkVertexExist(sourceV, "source vertex"); E.checkNotNull(dir, "direction"); checkDegree(degree); checkCapacity(capacity); @@ -76,7 +76,7 @@ public NodeWithWeight weightedShortestPath(Id sourceV, Id targetV, String weight, long degree, long skipDegree, long capacity) { E.checkNotNull(sourceV, "source vertex id"); - this.checkVertexExist(sourceV); + this.checkVertexExist(sourceV, "source vertex"); E.checkNotNull(dir, "direction"); E.checkNotNull(weight, "weight property"); checkDegree(degree); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java index 61cb8ea918..f9d9bf35c3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SubGraphTraverser.java @@ -60,7 +60,7 @@ private PathSet subGraphPaths(Id sourceV, Directions dir, String label, long limit, boolean rings, boolean sourceInRing) { E.checkNotNull(sourceV, "source vertex id"); - this.checkVertexExist(sourceV); + this.checkVertexExist(sourceV, "source vertex"); E.checkNotNull(dir, "direction"); checkPositive(depth, "max depth"); checkDegree(degree);