From 6eb4ef70a9e85cd1068761a3d98dc3c6282e07cc Mon Sep 17 00:00:00 2001 From: zhoney Date: Wed, 9 Dec 2020 19:54:44 +0800 Subject: [PATCH] export new implemented oltp traversals (#1289) * export new implemented oltp traversals * move EdgeStep, WeightedEdgeStep and RepeatEdgeStep to steps package Change-Id: I0e501dcffd7c38dbd6afe76e82000935160bee36 --- .../hugegraph/api/traversers/CountAPI.java | 15 ++- .../api/traversers/CustomizedPathsAPI.java | 23 ++-- .../api/traversers/FusiformSimilarityAPI.java | 4 +- .../api/traversers/JaccardSimilarityAPI.java | 2 +- .../api/traversers/KneighborAPI.java | 2 +- .../hugegraph/api/traversers/KoutAPI.java | 2 +- .../traversers/MultiNodeShortestPathAPI.java | 2 +- .../hugegraph/api/traversers/PathsAPI.java | 2 +- .../api/traversers/TemplatePathsAPI.java | 32 +++--- .../api/traversers/TraverserAPI.java | 13 +-- .../algorithm/CollectionPathsTraverser.java | 1 + .../traversal/algorithm/CountTraverser.java | 35 ++---- .../algorithm/CustomizePathsTraverser.java | 50 ++------ .../CustomizedCrosspointsTraverser.java | 1 + .../FusiformSimilarityTraverser.java | 15 ++- .../traversal/algorithm/HugeTraverser.java | 26 +++-- .../algorithm/JaccardSimilarTraverser.java | 3 +- .../algorithm/KneighborTraverser.java | 3 +- .../traversal/algorithm/KoutTraverser.java | 3 +- .../MultiNodeShortestPathTraverser.java | 3 +- .../algorithm/NeighborRankTraverser.java | 1 + .../traversal/algorithm/OltpTraverser.java | 1 + .../traversal/algorithm/PathTraverser.java | 1 + .../algorithm/ShortestPathTraverser.java | 8 +- .../algorithm/TemplatePathsTraverser.java | 28 +---- .../algorithm/{ => steps}/EdgeStep.java | 48 +++++++- .../algorithm/steps/RepeatEdgeStep.java | 108 ++++++++++++++++++ .../algorithm/steps/WeightedEdgeStep.java | 104 +++++++++++++++++ .../strategy/ConcurrentTraverseStrategy.java | 2 +- .../strategy/SingleTraverseStrategy.java | 2 +- .../algorithm/strategy/TraverseStrategy.java | 2 +- .../assembly/static/conf/gremlin-server.yaml | 14 ++- 32 files changed, 384 insertions(+), 172 deletions(-) rename hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/{ => steps}/EdgeStep.java (71%) create mode 100644 hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/RepeatEdgeStep.java create mode 100644 hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/WeightedEdgeStep.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CountAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CountAPI.java index e6d95a6868..05817a4b9a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CountAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CountAPI.java @@ -43,6 +43,7 @@ import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.structure.HugeVertex; import com.baidu.hugegraph.traversal.algorithm.CountTraverser; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; @@ -77,7 +78,7 @@ public String post(@Context GraphManager manager, request.dedupSize); HugeGraph g = graph(manager, graph); - List steps = steps(g, request); + List steps = steps(g, request); CountTraverser traverser = new CountTraverser(g); long count = traverser.count(sourceId, steps, request.containsTraversed, request.dedupSize); @@ -85,10 +86,9 @@ public String post(@Context GraphManager manager, return manager.serializer(g).writeMap(ImmutableMap.of("count", count)); } - private static List steps(HugeGraph graph, - CountRequest request) { + private static List steps(HugeGraph graph, CountRequest request) { int stepSize = request.steps.size(); - List steps = new ArrayList<>(stepSize); + List steps = new ArrayList<>(stepSize); for (Step step : request.steps) { steps.add(step.jsonToStep(graph)); } @@ -136,10 +136,9 @@ public String toString() { this.degree, this.skipDegree); } - private CountTraverser.Step jsonToStep(HugeGraph graph) { - return new CountTraverser.Step(graph, this.direction, this.labels, - this.properties, this.degree, - this.skipDegree); + private EdgeStep jsonToStep(HugeGraph graph) { + return new EdgeStep(graph, this.direction, this.labels, + this.properties, this.degree, this.skipDegree); } } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CustomizedPathsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CustomizedPathsAPI.java index e49f808e0c..41321871da 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CustomizedPathsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/CustomizedPathsAPI.java @@ -51,6 +51,7 @@ import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; +import com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; @@ -87,7 +88,7 @@ public String post(@Context GraphManager manager, HugeGraph g = graph(manager, graph); Iterator sources = request.sources.vertices(g); - List steps = step(g, request); + List steps = step(g, request); boolean sorted = request.sortBy != SortBy.NONE; CustomizePathsTraverser traverser = new CustomizePathsTraverser(g); @@ -116,10 +117,10 @@ public String post(@Context GraphManager manager, return manager.serializer(g).writePaths("paths", paths, false, iter); } - private static List step(HugeGraph graph, - PathRequest req) { + private static List step(HugeGraph graph, + PathRequest req) { int stepSize = req.steps.size(); - List steps = new ArrayList<>(stepSize); + List steps = new ArrayList<>(stepSize); for (Step step : req.steps) { steps.add(step.jsonToStep(graph)); } @@ -181,15 +182,11 @@ public String toString() { this.sample); } - private CustomizePathsTraverser.Step jsonToStep(HugeGraph g) { - return new CustomizePathsTraverser.Step(g, this.direction, - this.labels, - this.properties, - this.degree, - this.skipDegree, - this.weightBy, - this.defaultWeight, - this.sample); + private WeightedEdgeStep jsonToStep(HugeGraph g) { + return new WeightedEdgeStep(g, this.direction, this.labels, + this.properties, this.degree, + this.skipDegree, this.weightBy, + this.defaultWeight, this.sample); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/FusiformSimilarityAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/FusiformSimilarityAPI.java index e6ef2858b7..dda9845fab 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/FusiformSimilarityAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/FusiformSimilarityAPI.java @@ -100,13 +100,11 @@ public String post(@Context GraphManager manager, Iterator sources = request.sources.vertices(g); E.checkArgument(sources != null && sources.hasNext(), "The source vertices can't be empty"); - EdgeLabel edgeLabel = request.label == null ? - null : g.edgeLabel(request.label); FusiformSimilarityTraverser traverser = new FusiformSimilarityTraverser(g); SimilarsMap result = traverser.fusiformSimilarity( - sources, request.direction, edgeLabel, + sources, request.direction, request.label, request.minNeighbors, request.alpha, request.minSimilars, request.top, request.groupProperty, request.minGroups, diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/JaccardSimilarityAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/JaccardSimilarityAPI.java index bd6fdbc443..ebc4334147 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/JaccardSimilarityAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/JaccardSimilarityAPI.java @@ -45,7 +45,7 @@ import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.structure.HugeVertex; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java index 4cd2d02ace..d5120b7c63 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KneighborAPI.java @@ -51,7 +51,7 @@ import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.structure.HugeVertex; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.algorithm.KneighborTraverser; import com.baidu.hugegraph.type.define.Directions; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java index d8548ba1cf..adb95a6b75 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/KoutAPI.java @@ -52,7 +52,7 @@ import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.structure.HugeVertex; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser.Node; import com.baidu.hugegraph.traversal.algorithm.KoutTraverser; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/MultiNodeShortestPathAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/MultiNodeShortestPathAPI.java index 5a129975b6..642724f000 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/MultiNodeShortestPathAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/MultiNodeShortestPathAPI.java @@ -42,7 +42,7 @@ import com.baidu.hugegraph.backend.query.QueryResults; import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.algorithm.MultiNodeShortestPathTraverser; import com.baidu.hugegraph.util.E; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java index 27ea55b2eb..3564825fd5 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/PathsAPI.java @@ -50,7 +50,7 @@ import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.traversal.algorithm.CollectionPathsTraverser; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.algorithm.PathsTraverser; import com.baidu.hugegraph.type.define.Directions; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TemplatePathsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TemplatePathsAPI.java index c0e13d8fc0..fd84b8d1dd 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TemplatePathsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TemplatePathsAPI.java @@ -19,6 +19,9 @@ package com.baidu.hugegraph.api.traversers; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_PATHS_LIMIT; + import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -43,14 +46,12 @@ import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.algorithm.TemplatePathsTraverser; +import com.baidu.hugegraph.traversal.algorithm.steps.RepeatEdgeStep; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonProperty; -import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY; -import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_PATHS_LIMIT; - @Path("graphs/{graph}/traversers/templatepaths") @Singleton public class TemplatePathsAPI extends TraverserAPI { @@ -81,8 +82,7 @@ public String post(@Context GraphManager manager, HugeGraph g = graph(manager, graph); Iterator sources = request.sources.vertices(g); Iterator targets = request.targets.vertices(g); - List steps = - steps(g, request.steps); + List steps = steps(g, request.steps); TemplatePathsTraverser traverser = new TemplatePathsTraverser(g); Set paths; @@ -105,16 +105,22 @@ public String post(@Context GraphManager manager, return manager.serializer(g).writePaths("paths", paths, false, iter); } - private static List steps( - HugeGraph g, List steps) { - List edgeSteps = - new ArrayList<>(steps.size()); - for (RepeatEdgeStep step : steps) { + private static List steps(HugeGraph g, + List steps) { + List edgeSteps = new ArrayList<>(steps.size()); + for (TemplatePathStep step : steps) { edgeSteps.add(repeatEdgeStep(g, step)); } return edgeSteps; } + private static RepeatEdgeStep repeatEdgeStep(HugeGraph graph, + TemplatePathStep step) { + return new RepeatEdgeStep(graph, step.direction, step.labels, + step.properties, step.degree, + step.skipDegree, step.maxTimes); + } + private static class Request { @JsonProperty("sources") @@ -122,7 +128,7 @@ private static class Request { @JsonProperty("targets") public Vertices targets; @JsonProperty("steps") - public List steps; + public List steps; @JsonProperty("with_ring") public boolean withRing = false; @JsonProperty("capacity") @@ -143,14 +149,14 @@ public String toString() { } } - protected static class RepeatEdgeStep extends Step { + protected static class TemplatePathStep extends Step { @JsonProperty("max_times") public int maxTimes = 1; @Override public String toString() { - return String.format("RepeatEdgeStep{direction=%s,labels=%s," + + return String.format("TemplatePathStep{direction=%s,labels=%s," + "properties=%s,degree=%s,skipDegree=%s," + "maxTimes=%s}", this.direction, this.labels, this.properties, diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TraverserAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TraverserAPI.java index c9af8595da..d6141d95e4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TraverserAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/TraverserAPI.java @@ -24,8 +24,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.api.API; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; -import com.baidu.hugegraph.traversal.algorithm.TemplatePathsTraverser; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.fasterxml.jackson.annotation.JsonProperty; @@ -38,16 +37,6 @@ protected static EdgeStep step(HugeGraph graph, Step step) { step.degree, step.skipDegree); } - protected static TemplatePathsTraverser.RepeatEdgeStep repeatEdgeStep( - HugeGraph graph, TemplatePathsAPI.RepeatEdgeStep step) { - return new TemplatePathsTraverser.RepeatEdgeStep(graph, step.direction, - step.labels, - step.properties, - step.degree, - step.skipDegree, - step.maxTimes); - } - protected static class Step { @JsonProperty("direction") diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CollectionPathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CollectionPathsTraverser.java index 8261e7e5c7..da76202e5c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CollectionPathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CollectionPathsTraverser.java @@ -30,6 +30,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeVertex; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.strategy.TraverseStrategy; import com.baidu.hugegraph.util.E; import com.google.common.collect.ImmutableList; 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 b8ce2c258f..ee27b40c44 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 @@ -22,7 +22,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; import org.apache.commons.lang.mutable.MutableLong; @@ -34,7 +33,7 @@ import com.baidu.hugegraph.iterator.FilterIterator; import com.baidu.hugegraph.iterator.FlatMapperIterator; import com.baidu.hugegraph.structure.HugeEdge; -import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.util.E; public class CountTraverser extends HugeTraverser { @@ -48,7 +47,7 @@ public CountTraverser(HugeGraph graph) { super(graph); } - public long count(Id source, List steps, + public long count(Id source, List steps, boolean containsTraversed, long dedupSize) { E.checkNotNull(source, "source vertex id"); this.checkVertexExist(source, "source vertex"); @@ -63,45 +62,45 @@ public long count(Id source, List steps, } int stepNum = steps.size(); - Step firstStep = steps.get(0); + EdgeStep firstStep = steps.get(0); if (stepNum == 1) { // Just one step, query count and return - long edgesCount = this.edgesCount(source, firstStep.edgeStep); + long edgesCount = this.edgesCount(source, firstStep); this.count.add(edgesCount); return this.count.longValue(); } // Multiple steps, construct first step to iterator - Iterator edges = this.edgesOfVertex(source, firstStep); + Iterator edges = this.edgesOfVertexWithCount(source, firstStep); // Wrap steps to Iterator except last step for (int i = 1; i < stepNum - 1; i++) { - Step currentStep = steps.get(i); + EdgeStep currentStep = steps.get(i); edges = new FlatMapperIterator<>(edges, (edge) -> { Id target = ((HugeEdge) edge).id().otherVertexId(); - return this.edgesOfVertex(target, currentStep); + return this.edgesOfVertexWithCount(target, currentStep); }); } // The last step, just query count - Step lastStep = steps.get(stepNum - 1); + EdgeStep lastStep = steps.get(stepNum - 1); while (edges.hasNext()) { Id target = ((HugeEdge) edges.next()).id().otherVertexId(); if (this.dedup(target)) { continue; } // Count last layer vertices(without dedup size) - long edgesCount = this.edgesCount(target, lastStep.edgeStep); + long edgesCount = this.edgesCount(target, lastStep); this.count.add(edgesCount); } return this.count.longValue(); } - private Iterator edgesOfVertex(Id source, Step step) { + private Iterator edgesOfVertexWithCount(Id source, EdgeStep step) { if (this.dedup(source)) { return QueryResults.emptyIterator(); } - Iterator flatten = this.edgesOfVertex(source, step.edgeStep); + Iterator flatten = this.edgesOfVertex(source, step); return new FilterIterator<>(flatten, e -> { if (this.containsTraversed) { // Count intermediate vertices @@ -138,16 +137,4 @@ private boolean reachDedup() { return this.dedupSize != NO_LIMIT && this.dedupSet.size() >= this.dedupSize; } - - public static class Step { - - private final EdgeStep edgeStep; - - public Step(HugeGraph g, Directions direction, List labels, - Map properties, long degree, - long skipDegree) { - this.edgeStep = new EdgeStep(g, direction, labels, properties, - degree, skipDegree); - } - } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizePathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizePathsTraverser.java index c5edb3e5e7..4c1f73eb09 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizePathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizePathsTraverser.java @@ -32,10 +32,9 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; -import com.baidu.hugegraph.schema.PropertyKey; import com.baidu.hugegraph.structure.HugeEdge; import com.baidu.hugegraph.structure.HugeVertex; -import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep; import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; import com.google.common.collect.ImmutableList; @@ -48,7 +47,7 @@ public CustomizePathsTraverser(HugeGraph graph) { } public List customizedPaths(Iterator vertices, - List steps, boolean sorted, + List steps, boolean sorted, long capacity, long limit) { E.checkArgument(vertices.hasNext(), "The source vertices can't be empty"); @@ -68,7 +67,7 @@ public List customizedPaths(Iterator vertices, int pathCount = 0; long access = 0; MultivaluedMap newVertices = null; - root : for (Step step : steps) { + root : for (WeightedEdgeStep step : steps) { stepNum--; newVertices = newMultivalueMap(); Iterator edges; @@ -76,7 +75,7 @@ public List customizedPaths(Iterator vertices, // Traversal vertices of previous level for (Map.Entry> entry : sources.entrySet()) { List adjacency = new ArrayList<>(); - edges = this.edgesOfVertex(entry.getKey(), step.edgeStep); + edges = this.edgesOfVertex(entry.getKey(), step.step()); while (edges.hasNext()) { HugeEdge edge = (HugeEdge) edges.next(); Id target = edge.id().otherVertexId(); @@ -87,9 +86,9 @@ public List customizedPaths(Iterator vertices, } Node newNode; if (sorted) { - double w = step.weightBy != null ? - edge.value(step.weightBy.name()) : - step.defaultWeight; + double w = step.weightBy() != null ? + edge.value(step.weightBy().name()) : + step.defaultWeight(); newNode = new WeightNode(target, n, w); } else { newNode = new Node(target, n); @@ -100,9 +99,9 @@ public List customizedPaths(Iterator vertices, } } - if (step.sample > 0) { + if (step.sample() > 0) { // Sample current node's adjacent nodes - adjacency = sample(adjacency, step.sample); + adjacency = sample(adjacency, step.sample()); } // Add current node's adjacent nodes @@ -238,35 +237,4 @@ private void calcTotalWeight() { this.totalWeight = sum; } } - - public static class Step { - - private final EdgeStep edgeStep; - private final PropertyKey weightBy; - private final double defaultWeight; - private final long sample; - - public Step(HugeGraph g, Directions direction, List labels, - Map properties, - long degree, long skipDegree, - String weightBy, double defaultWeight, long sample) { - E.checkArgument(sample > 0L || sample == NO_LIMIT, - "The sample must be > 0 or == -1, but got: %s", - sample); - E.checkArgument(degree == NO_LIMIT || degree >= sample, - "Degree must be greater than or equal to sample," + - " but got degree %s and sample %s", - degree, sample); - - this.edgeStep = new EdgeStep(g, direction, labels, properties, - degree, skipDegree); - if (weightBy != null) { - this.weightBy = g.propertyKey(weightBy); - } else { - this.weightBy = null; - } - this.defaultWeight = defaultWeight; - this.sample = sample; - } - } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizedCrosspointsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizedCrosspointsTraverser.java index 23fdc499eb..4345646d55 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizedCrosspointsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/CustomizedCrosspointsTraverser.java @@ -37,6 +37,7 @@ import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeEdge; import com.baidu.hugegraph.structure.HugeVertex; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java index 17a534ab0e..320833f1cd 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/FusiformSimilarityTraverser.java @@ -56,7 +56,7 @@ public FusiformSimilarityTraverser(HugeGraph graph) { } public SimilarsMap fusiformSimilarity(Iterator vertices, - Directions direction, EdgeLabel label, + Directions direction, String label, int minNeighbors, double alpha, int minSimilars, int top, String groupProperty, int minGroups, @@ -92,7 +92,7 @@ public SimilarsMap fusiformSimilarity(Iterator vertices, private Set fusiformSimilarityForVertex( HugeVertex vertex, Directions direction, - EdgeLabel label, int minNeighbors, double alpha, + String label, int minNeighbors, double alpha, int minSimilars, int top, String groupProperty, int minGroups, long degree, long capacity, boolean withIntermediary) { @@ -102,7 +102,7 @@ private Set fusiformSimilarityForVertex( // Ignore current vertex if its neighbors number is not enough return ImmutableSet.of(); } - Id labelId = label == null ? null : label.id(); + Id labelId = this.getEdgeLabelId(label); // Get similar nodes and counts Iterator edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree); @@ -209,12 +209,17 @@ private static void checkGroupArgs(String groupProperty, int minGroups) { private boolean matchMinNeighborCount(HugeVertex vertex, Directions direction, - EdgeLabel edgeLabel, + String label, int minNeighbors, long degree) { Iterator edges; long neighborCount; - Id labelId = edgeLabel == null ? null : edgeLabel.id(); + EdgeLabel edgeLabel = null; + Id labelId = null; + if (label != null) { + edgeLabel = this.graph().edgeLabel(label); + labelId = edgeLabel.id(); + } if (edgeLabel != null && edgeLabel.frequency() == Frequency.SINGLE) { edges = this.edgesOfVertex(vertex.id(), direction, labelId, minNeighbors); 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 88197b9d36..4ac2e2ab3d 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 @@ -52,6 +52,7 @@ import com.baidu.hugegraph.iterator.MapperIterator; import com.baidu.hugegraph.schema.SchemaLabel; import com.baidu.hugegraph.structure.HugeEdge; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.optimize.TraversalUtil; import com.baidu.hugegraph.type.HugeType; import com.baidu.hugegraph.type.define.Directions; @@ -193,10 +194,10 @@ protected Iterator edgesOfVertex(Id source, Directions dir, } protected Iterator edgesOfVertex(Id source, EdgeStep edgeStep) { - if (edgeStep.properties == null || edgeStep.properties.isEmpty()) { + if (edgeStep.properties() == null || edgeStep.properties().isEmpty()) { Iterator edges = this.edgesOfVertex(source, - edgeStep.direction, - edgeStep.labels, + edgeStep.direction(), + edgeStep.labels(), edgeStep.limit()); return edgeStep.skipSuperNodeIfNeeded(edges); } @@ -204,7 +205,7 @@ protected Iterator edgesOfVertex(Id source, EdgeStep edgeStep) { } protected Iterator edgesOfVertexWithSK(Id source, EdgeStep edgeStep) { - assert edgeStep.properties != null && !edgeStep.properties.isEmpty(); + assert edgeStep.properties() != null && !edgeStep.properties().isEmpty(); return this.edgesOfVertex(source, edgeStep, true); } @@ -212,14 +213,14 @@ private Iterator edgesOfVertex(Id source, EdgeStep edgeStep, boolean mustAllSK) { Id[] edgeLabels = edgeStep.edgeLabels(); Query query = GraphTransaction.constructEdgesQuery(source, - edgeStep.direction, + edgeStep.direction(), edgeLabels); ConditionQuery filter = null; if (mustAllSK) { - this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties); + this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties()); } else { filter = (ConditionQuery) query.copy(); - this.fillFilterByProperties(filter, edgeStep.properties); + this.fillFilterByProperties(filter, edgeStep.properties()); } query.capacity(Query.NO_CAPACITY); if (edgeStep.limit() != NO_LIMIT) { @@ -270,19 +271,20 @@ private void fillFilterByProperties(Query query, protected long edgesCount(Id source, EdgeStep edgeStep) { Id[] edgeLabels = edgeStep.edgeLabels(); Query query = GraphTransaction.constructEdgesQuery(source, - edgeStep.direction, + edgeStep.direction(), edgeLabels); - this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties); + this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties()); query.aggregate(Aggregate.AggregateFunc.COUNT, null); query.capacity(Query.NO_CAPACITY); query.limit(Query.NO_LIMIT); long count = graph().queryNumber(query).longValue(); - if (edgeStep.degree == NO_LIMIT || count < edgeStep.degree) { + if (edgeStep.degree() == NO_LIMIT || count < edgeStep.degree()) { return count; - } else if (edgeStep.skipDegree != 0L && count >= edgeStep.skipDegree) { + } else if (edgeStep.skipDegree() != 0L && + count >= edgeStep.skipDegree()) { return 0L; } else { - return edgeStep.degree; + return edgeStep.degree(); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java index 82e679c250..ba62b24df5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/JaccardSimilarTraverser.java @@ -31,6 +31,7 @@ import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; @@ -75,7 +76,7 @@ public Map jaccardSimilars(Id source, EdgeStep step, Map results; if (3 >= this.concurrentDepth() && - step.direction == Directions.BOTH) { + step.direction() == Directions.BOTH) { results = this.jaccardSimilarsConcurrent(source, step, capacity); } else { results = this.jaccardSimilarsSingle(source, step, capacity); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java index 3c68021b5e..1fadd50147 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KneighborTraverser.java @@ -24,6 +24,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; @@ -72,7 +73,7 @@ public Set customizedKneighbor(Id source, EdgeStep step, checkLimit(limit); boolean single = maxDepth < this.concurrentDepth() || - step.direction != Directions.BOTH; + step.direction() != Directions.BOTH; return this.customizedKneighbor(source, step, maxDepth, limit, single); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java index fcfddf6bbc..a6fa743dd6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/KoutTraverser.java @@ -24,6 +24,7 @@ import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; @@ -102,7 +103,7 @@ public Set customizedKout(Id source, EdgeStep step, int maxDepth, Set results; boolean single = maxDepth < this.concurrentDepth() || - step.direction != Directions.BOTH; + step.direction() != Directions.BOTH; results = this.customizedKout(source, step, maxDepth, nearest, capacity, single); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/MultiNodeShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/MultiNodeShortestPathTraverser.java index 633b5bd79f..c4978f3c3c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/MultiNodeShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/MultiNodeShortestPathTraverser.java @@ -32,6 +32,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeVertex; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; @@ -61,7 +62,7 @@ public List multiNodeShortestPath(Iterator vertices, }); if (maxDepth >= this.concurrentDepth() && - step.direction == Directions.BOTH || + step.direction() == Directions.BOTH || vertexCount > 10) { return this.multiNodeShortestPathConcurrent(pairs, step, maxDepth, capacity); 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 9e0c59248f..f9060cb692 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 @@ -34,6 +34,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeEdge; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.OrderLimitMap; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java index 15f2db7871..b8367b3794 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/OltpTraverser.java @@ -39,6 +39,7 @@ import com.baidu.hugegraph.config.CoreOptions; import com.baidu.hugegraph.iterator.FilterIterator; import com.baidu.hugegraph.structure.HugeEdge; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.util.Consumers; import jersey.repackaged.com.google.common.base.Objects; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathTraverser.java index 9551009e5e..df9f017ccb 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PathTraverser.java @@ -30,6 +30,7 @@ import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeEdge; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.strategy.TraverseStrategy; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT; 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 aaec88379b..17b757c6bf 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 @@ -32,6 +32,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeEdge; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; import com.google.common.collect.ImmutableList; @@ -89,9 +90,10 @@ public Path shortestPath(Id sourceV, Id targetV, Directions dir, public Path shortestPath(Id sourceV, Id targetV, EdgeStep step, int depth, long capacity) { - return this.shortestPath(sourceV, targetV, step.direction, - new ArrayList<>(step.labels.values()), - depth, step.degree, step.skipDegree, capacity); + return this.shortestPath(sourceV, targetV, step.direction(), + new ArrayList<>(step.labels().values()), + depth, step.degree(), step.skipDegree(), + capacity); } public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir, diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/TemplatePathsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/TemplatePathsTraverser.java index 458fbe47b6..3e4bdc5b00 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/TemplatePathsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/TemplatePathsTraverser.java @@ -31,8 +31,9 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.structure.HugeVertex; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.RepeatEdgeStep; import com.baidu.hugegraph.traversal.algorithm.strategy.TraverseStrategy; -import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; public class TemplatePathsTraverser extends HugeTraverser { @@ -68,7 +69,7 @@ public Set templatePaths(Iterator sources, int totalSteps = 0; for (RepeatEdgeStep step : steps) { - totalSteps += step.maxTimes; + totalSteps += step.maxTimes(); } TraverseStrategy strategy = TraverseStrategy.create( totalSteps >= this.concurrentDepth(), @@ -112,7 +113,7 @@ public Traverser(HugeTraverser traverser, TraverseStrategy strategy, this.steps = steps; this.withRing = withRing; for (RepeatEdgeStep step : steps) { - this.totalSteps += step.maxTimes; + this.totalSteps += step.maxTimes(); } this.sourceIndex = 0; @@ -278,25 +279,4 @@ public boolean lastSuperStep() { this.targetIndex == this.sourceIndex + 1; } } - - public static class RepeatEdgeStep extends EdgeStep { - - private int maxTimes = 1; - - public RepeatEdgeStep(HugeGraph g, Directions direction, - List labels, - Map properties, long degree, - long skipDegree, int maxTimes) { - super(g, direction, labels, properties, degree, skipDegree); - this.maxTimes = maxTimes; - } - - private int remainTimes() { - return this.maxTimes; - } - - private void decreaseTimes() { - this.maxTimes--; - } - } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/EdgeStep.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java similarity index 71% rename from hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/EdgeStep.java rename to hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java index f27e435b84..ed5ffc66d4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/EdgeStep.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/EdgeStep.java @@ -17,8 +17,9 @@ * under the License. */ -package com.baidu.hugegraph.traversal.algorithm; +package com.baidu.hugegraph.traversal.algorithm.steps; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE; import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT; import java.util.HashMap; @@ -31,9 +32,12 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.schema.EdgeLabel; +import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.optimize.TraversalUtil; import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.E; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; public class EdgeStep { @@ -43,6 +47,28 @@ public class EdgeStep { protected final long degree; protected final long skipDegree; + public EdgeStep(HugeGraph g, Directions direction) { + this(g, direction, ImmutableList.of()); + } + + public EdgeStep(HugeGraph g, List labels) { + this(g, Directions.BOTH, labels); + } + + public EdgeStep(HugeGraph g, Map properties) { + this(g, Directions.BOTH, ImmutableList.of(), properties); + } + + public EdgeStep(HugeGraph g, Directions direction, List labels) { + this(g, direction, labels, ImmutableMap.of()); + } + + public EdgeStep(HugeGraph g, Directions direction, List labels, + Map properties) { + this(g, direction, labels, properties, + Long.valueOf(DEFAULT_DEGREE), 0L); + } + public EdgeStep(HugeGraph g, Directions direction, List labels, Map properties, long degree, long skipDegree) { @@ -74,6 +100,26 @@ public EdgeStep(HugeGraph g, Directions direction, List labels, this.skipDegree = skipDegree; } + public Directions direction() { + return this.direction; + } + + public Map labels() { + return this.labels; + } + + public Map properties() { + return this.properties; + } + + public long degree() { + return this.degree; + } + + public long skipDegree() { + return this.skipDegree; + } + public Id[] edgeLabels() { int elsSize = this.labels.size(); Id[] edgeLabels = this.labels.keySet().toArray(new Id[elsSize]); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/RepeatEdgeStep.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/RepeatEdgeStep.java new file mode 100644 index 0000000000..d264e81056 --- /dev/null +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/RepeatEdgeStep.java @@ -0,0 +1,108 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.traversal.algorithm.steps; + +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE; + +import java.util.List; +import java.util.Map; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.type.define.Directions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class RepeatEdgeStep extends EdgeStep { + + private int maxTimes = 1; + + public RepeatEdgeStep(HugeGraph g, Directions direction) { + this(g, direction, ImmutableList.of()); + } + + public RepeatEdgeStep(HugeGraph g, Directions direction, int maxTimes) { + this(g, direction); + this.maxTimes = maxTimes; + } + + public RepeatEdgeStep(HugeGraph g, List labels) { + this(g, Directions.BOTH, labels); + } + + public RepeatEdgeStep(HugeGraph g, List labels, int maxTimes) { + this(g, labels); + this.maxTimes = maxTimes; + } + + public RepeatEdgeStep(HugeGraph g, Map properties) { + this(g, Directions.BOTH, ImmutableList.of(), properties); + } + + public RepeatEdgeStep(HugeGraph g, Map properties, + int maxTimes) { + this(g, properties); + this.maxTimes = maxTimes; + } + + public RepeatEdgeStep(HugeGraph g, Directions direction, + List labels) { + this(g, direction, labels, ImmutableMap.of()); + } + + public RepeatEdgeStep(HugeGraph g, Directions direction, + List labels, int maxTimes) { + this(g, direction, labels); + this.maxTimes = maxTimes; + } + + public RepeatEdgeStep(HugeGraph g, Directions direction, + List labels, + Map properties) { + this(g, direction, labels, properties, + Long.valueOf(DEFAULT_DEGREE), 0L, 1); + } + + public RepeatEdgeStep(HugeGraph g, Directions direction, + List labels, + Map properties, int maxTimes) { + this(g, direction, labels, properties); + this.maxTimes = maxTimes; + } + + public RepeatEdgeStep(HugeGraph g, Directions direction, + List labels, + Map properties, long degree, + long skipDegree, int maxTimes) { + super(g, direction, labels, properties, degree, skipDegree); + this.maxTimes = maxTimes; + } + + public int remainTimes() { + return this.maxTimes; + } + + public void decreaseTimes() { + this.maxTimes--; + } + + public int maxTimes() { + return this.maxTimes; + } +} diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/WeightedEdgeStep.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/WeightedEdgeStep.java new file mode 100644 index 0000000000..4928dfe71c --- /dev/null +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/steps/WeightedEdgeStep.java @@ -0,0 +1,104 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.traversal.algorithm.steps; + +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_SAMPLE; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.NO_LIMIT; + +import java.util.List; +import java.util.Map; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.schema.PropertyKey; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class WeightedEdgeStep { + + private final EdgeStep edgeStep; + private final PropertyKey weightBy; + private final double defaultWeight; + private final long sample; + + public WeightedEdgeStep(HugeGraph g, Directions direction) { + this(g, direction, ImmutableList.of()); + } + + public WeightedEdgeStep(HugeGraph g, List labels) { + this(g, Directions.BOTH, labels); + } + + public WeightedEdgeStep(HugeGraph g, Map properties) { + this(g, Directions.BOTH, ImmutableList.of(), properties); + } + + public WeightedEdgeStep(HugeGraph g, Directions direction, List labels) { + this(g, direction, labels, ImmutableMap.of()); + } + + public WeightedEdgeStep(HugeGraph g, Directions direction, List labels, + Map properties) { + this(g, direction, labels, properties, + Long.valueOf(DEFAULT_DEGREE), 0L, null, 0.0D, + Long.valueOf(DEFAULT_SAMPLE)); + } + + public WeightedEdgeStep(HugeGraph g, Directions direction, List labels, + Map properties, + long degree, long skipDegree, + String weightBy, double defaultWeight, long sample) { + E.checkArgument(sample > 0L || sample == NO_LIMIT, + "The sample must be > 0 or == -1, but got: %s", + sample); + E.checkArgument(degree == NO_LIMIT || degree >= sample, + "Degree must be greater than or equal to sample," + + " but got degree %s and sample %s", + degree, sample); + + this.edgeStep = new EdgeStep(g, direction, labels, properties, + degree, skipDegree); + if (weightBy != null) { + this.weightBy = g.propertyKey(weightBy); + } else { + this.weightBy = null; + } + this.defaultWeight = defaultWeight; + this.sample = sample; + } + + public EdgeStep step() { + return this.edgeStep; + } + + public PropertyKey weightBy() { + return this.weightBy; + } + + public double defaultWeight() { + return this.defaultWeight; + } + + public long sample() { + return this.sample; + } +} diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/ConcurrentTraverseStrategy.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/ConcurrentTraverseStrategy.java index f856e0a47c..69203433a8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/ConcurrentTraverseStrategy.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/ConcurrentTraverseStrategy.java @@ -27,7 +27,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.OltpTraverser; public class ConcurrentTraverseStrategy extends OltpTraverser diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/SingleTraverseStrategy.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/SingleTraverseStrategy.java index 822949d185..817d188feb 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/SingleTraverseStrategy.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/SingleTraverseStrategy.java @@ -28,7 +28,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; import com.baidu.hugegraph.traversal.algorithm.OltpTraverser; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/TraverseStrategy.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/TraverseStrategy.java index cb81ad87f8..9bfc2759ba 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/TraverseStrategy.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/strategy/TraverseStrategy.java @@ -26,7 +26,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; -import com.baidu.hugegraph.traversal.algorithm.EdgeStep; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; import com.baidu.hugegraph.traversal.algorithm.HugeTraverser; public interface TraverseStrategy { diff --git a/hugegraph-dist/src/assembly/static/conf/gremlin-server.yaml b/hugegraph-dist/src/assembly/static/conf/gremlin-server.yaml index 69640a3adb..8787491fe9 100644 --- a/hugegraph-dist/src/assembly/static/conf/gremlin-server.yaml +++ b/hugegraph-dist/src/assembly/static/conf/gremlin-server.yaml @@ -20,15 +20,27 @@ scriptEngines: { com.baidu.hugegraph.backend.id.IdGenerator, com.baidu.hugegraph.type.define.Directions, com.baidu.hugegraph.type.define.NodeRole, - com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser, + com.baidu.hugegraph.traversal.algorithm.CollectionPathsTraverser, + com.baidu.hugegraph.traversal.algorithm.CountTraverser, com.baidu.hugegraph.traversal.algorithm.CustomizedCrosspointsTraverser, + com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser, com.baidu.hugegraph.traversal.algorithm.FusiformSimilarityTraverser, com.baidu.hugegraph.traversal.algorithm.HugeTraverser, + com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser, + com.baidu.hugegraph.traversal.algorithm.KneighborTraverser, + com.baidu.hugegraph.traversal.algorithm.KoutTraverser, + com.baidu.hugegraph.traversal.algorithm.MultiNodeShortestPathTraverser, com.baidu.hugegraph.traversal.algorithm.NeighborRankTraverser, com.baidu.hugegraph.traversal.algorithm.PathsTraverser, com.baidu.hugegraph.traversal.algorithm.PersonalRankTraverser, + com.baidu.hugegraph.traversal.algorithm.SameNeighborTraverser, com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser, + com.baidu.hugegraph.traversal.algorithm.SingleSourceShortestPathTraverser, com.baidu.hugegraph.traversal.algorithm.SubGraphTraverser, + com.baidu.hugegraph.traversal.algorithm.TemplatePathsTraverser, + com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep, + com.baidu.hugegraph.traversal.algorithm.steps.RepeatEdgeStep, + com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep, com.baidu.hugegraph.traversal.optimize.Text, com.baidu.hugegraph.traversal.optimize.TraversalUtil, com.baidu.hugegraph.util.DateUtil