Skip to content

Commit

Permalink
export new implemented oltp traversals (apache#1289)
Browse files Browse the repository at this point in the history
* export new implemented oltp traversals
* move EdgeStep, WeightedEdgeStep and RepeatEdgeStep to steps package

Change-Id: I0e501dcffd7c38dbd6afe76e82000935160bee36
  • Loading branch information
zhoney authored and tmljob committed Dec 10, 2020
1 parent 08082f4 commit 6eb4ef7
Show file tree
Hide file tree
Showing 32 changed files with 384 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,18 +78,17 @@ public String post(@Context GraphManager manager,
request.dedupSize);

HugeGraph g = graph(manager, graph);
List<CountTraverser.Step> steps = steps(g, request);
List<EdgeStep> steps = steps(g, request);
CountTraverser traverser = new CountTraverser(g);
long count = traverser.count(sourceId, steps, request.containsTraversed,
request.dedupSize);

return manager.serializer(g).writeMap(ImmutableMap.of("count", count));
}

private static List<CountTraverser.Step> steps(HugeGraph graph,
CountRequest request) {
private static List<EdgeStep> steps(HugeGraph graph, CountRequest request) {
int stepSize = request.steps.size();
List<CountTraverser.Step> steps = new ArrayList<>(stepSize);
List<EdgeStep> steps = new ArrayList<>(stepSize);
for (Step step : request.steps) {
steps.add(step.jsonToStep(graph));
}
Expand Down Expand Up @@ -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);
}
}
}
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 @@ -100,13 +100,11 @@ public String post(@Context GraphManager manager,
Iterator<Vertex> 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,
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,24 +105,30 @@ 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;
}

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")
public Vertices sources;
@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 +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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")
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 @@ -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;
Expand All @@ -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 {
Expand All @@ -48,7 +47,7 @@ public CountTraverser(HugeGraph graph) {
super(graph);
}

public long count(Id source, List<Step> steps,
public long count(Id source, List<EdgeStep> steps,
boolean containsTraversed, long dedupSize) {
E.checkNotNull(source, "source vertex id");
this.checkVertexExist(source, "source vertex");
Expand All @@ -63,45 +62,45 @@ public long count(Id source, List<Step> 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<Edge> edges = this.edgesOfVertex(source, firstStep);
Iterator<Edge> 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<Edge> edgesOfVertex(Id source, Step step) {
private Iterator<Edge> edgesOfVertexWithCount(Id source, EdgeStep step) {
if (this.dedup(source)) {
return QueryResults.emptyIterator();
}
Iterator<Edge> flatten = this.edgesOfVertex(source, step.edgeStep);
Iterator<Edge> flatten = this.edgesOfVertex(source, step);
return new FilterIterator<>(flatten, e -> {
if (this.containsTraversed) {
// Count intermediate vertices
Expand Down Expand Up @@ -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<String> labels,
Map<String, Object> properties, long degree,
long skipDegree) {
this.edgeStep = new EdgeStep(g, direction, labels, properties,
degree, skipDegree);
}
}
}
Loading

0 comments on commit 6eb4ef7

Please sign in to comment.