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

export new implemented oltp traversals #1289

Merged
merged 4 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +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.EdgeStep;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +88,7 @@ public String post(@Context GraphManager manager,

HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.vertices(g);
List<CustomizePathsTraverser.Step> steps = step(g, request);
List<WeightedEdgeStep> steps = step(g, request);
boolean sorted = request.sortBy != SortBy.NONE;

CustomizePathsTraverser traverser = new CustomizePathsTraverser(g);
Expand Down Expand Up @@ -116,10 +117,10 @@ public String post(@Context GraphManager manager,
return manager.serializer(g).writePaths("paths", paths, false, iter);
}

private static List<CustomizePathsTraverser.Step> step(HugeGraph graph,
PathRequest req) {
private static List<WeightedEdgeStep> step(HugeGraph graph,
PathRequest req) {
int stepSize = req.steps.size();
List<CustomizePathsTraverser.Step> steps = new ArrayList<>(stepSize);
List<WeightedEdgeStep> steps = new ArrayList<>(stepSize);
for (Step step : req.steps) {
steps.add(step.jsonToStep(graph));
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -81,8 +82,7 @@ public String post(@Context GraphManager manager,
HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.vertices(g);
Iterator<Vertex> targets = request.targets.vertices(g);
List<TemplatePathsTraverser.RepeatEdgeStep> steps =
steps(g, request.steps);
List<RepeatEdgeStep> steps = steps(g, request.steps);

TemplatePathsTraverser traverser = new TemplatePathsTraverser(g);
Set<HugeTraverser.Path> paths;
Expand All @@ -105,11 +105,10 @@ public String post(@Context GraphManager manager,
return manager.serializer(g).writePaths("paths", paths, false, iter);
}

private static List<TemplatePathsTraverser.RepeatEdgeStep> steps(
HugeGraph g, List<RepeatEdgeStep> steps) {
List<TemplatePathsTraverser.RepeatEdgeStep> edgeSteps =
new ArrayList<>(steps.size());
for (RepeatEdgeStep step : steps) {
private static List<RepeatEdgeStep> steps(HugeGraph g,
List<TemplatePathStep> steps) {
List<RepeatEdgeStep> edgeSteps = new ArrayList<>(steps.size());
for (TemplatePathStep step : steps) {
edgeSteps.add(repeatEdgeStep(g, step));
}
return edgeSteps;
Expand All @@ -122,7 +121,7 @@ private static class Request {
@JsonProperty("targets")
public Vertices targets;
@JsonProperty("steps")
public List<RepeatEdgeStep> steps;
public List<TemplatePathStep> steps;
@JsonProperty("with_ring")
public boolean withRing = false;
@JsonProperty("capacity")
Expand All @@ -143,14 +142,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

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.traversal.algorithm.steps.RepeatEdgeStep;
import com.baidu.hugegraph.type.define.Directions;
import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -38,14 +38,11 @@ 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 RepeatEdgeStep repeatEdgeStep(
HugeGraph graph, TemplatePathsAPI.TemplatePathStep step) {
Copy link
Contributor

Choose a reason for hiding this comment

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

move to TemplatePathsAPI

return new RepeatEdgeStep(graph, step.direction, step.labels,
step.properties, step.degree,
step.skipDegree, step.maxTimes);
}

protected static class Step {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.baidu.hugegraph.iterator.FilterIterator;
import com.baidu.hugegraph.iterator.FlatMapperIterator;
import com.baidu.hugegraph.structure.HugeEdge;
import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep;
import com.baidu.hugegraph.util.E;

public class CountTraverser extends HugeTraverser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,7 +47,7 @@ public CustomizePathsTraverser(HugeGraph graph) {
}

public List<Path> customizedPaths(Iterator<Vertex> vertices,
List<Step> steps, boolean sorted,
List<WeightedEdgeStep> steps, boolean sorted,
long capacity, long limit) {
E.checkArgument(vertices.hasNext(),
"The source vertices can't be empty");
Expand All @@ -68,15 +67,15 @@ public List<Path> customizedPaths(Iterator<Vertex> vertices,
int pathCount = 0;
long access = 0;
MultivaluedMap<Id, Node> newVertices = null;
root : for (Step step : steps) {
root : for (WeightedEdgeStep step : steps) {
stepNum--;
newVertices = newMultivalueMap();
Iterator<Edge> edges;

// Traversal vertices of previous level
for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
List<Node> 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();
Expand All @@ -87,9 +86,9 @@ public List<Path> customizedPaths(Iterator<Vertex> 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);
Expand All @@ -100,9 +99,9 @@ public List<Path> customizedPaths(Iterator<Vertex> 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
Expand Down Expand Up @@ -238,58 +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) {
this(g, direction, ImmutableList.of());
}

public Step(HugeGraph g, List<String> labels) {
this(g, Directions.BOTH, labels);
}

public Step(HugeGraph g, Map<String, Object> properties) {
this(g, Directions.BOTH, ImmutableList.of(), properties);
}

public Step(HugeGraph g, Directions direction, List<String> labels) {
this(g, direction, labels, ImmutableMap.of());
}

public Step(HugeGraph g, Directions direction, List<String> labels,
Map<String, Object> properties) {
this(g, direction, labels, properties,
Long.valueOf(DEFAULT_DEGREE), 0L, null, 0.0D,
Long.valueOf(DEFAULT_SAMPLE));
}

public Step(HugeGraph g, Directions direction, List<String> labels,
Map<String, Object> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading