Skip to content

Commit

Permalink
Kext vertex & edge label properties filter (apache#22)
Browse files Browse the repository at this point in the history
* K-layer extended vertex filtering
* Add user password check
* same neighbors batch

Co-authored-by: zxxxhonest <[email protected]>
Co-authored-by: guoyonggang <[email protected]>
Co-authored-by: zhaoxiangxin <[email protected]>
Co-authored-by: zxxxhonest <[email protected]>
  • Loading branch information
4 people authored Sep 22, 2021
1 parent 9aa67de commit 08aafc8
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import com.baidu.hugegraph.traversal.algorithm.steps.VertexStep;
import com.baidu.hugegraph.traversal.algorithm.steps.Steps;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;
Expand Down Expand Up @@ -117,31 +117,27 @@ public String post(@Context GraphManager manager,
E.checkArgumentNotNull(request, "The request body can't be null");
E.checkArgumentNotNull(request.source,
"The source of request can't be null");
E.checkArgument(request.step != null,
E.checkArgument(request.steps != null,
"The steps of request can't be null");
if (request.countOnly) {
E.checkArgument(!request.withVertex && !request.withPath,
"Can't return vertex or path when count only");
}

LOG.debug("Graph [{}] get customized kneighbor from source vertex " +
"'{}', with step '{}', limit '{}', count_only '{}', " +
"'{}', with steps '{}', limit '{}', count_only '{}', " +
"with_vertex '{}', with_path '{}' and with_edge '{}'",
graph, request.source, request.step, request.limit,
graph, request.source, request.steps, request.limit,
request.countOnly, request.withVertex, request.withPath,
request.withEdge);

HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);

EdgeStep step = step(g, request.step);
VertexStep vStep = request.vStep == null ? null :
vertexStep(g, request.vStep);
Steps steps = steps(g, request.steps);

KneighborRecords results;
try (KneighborTraverser traverser = new KneighborTraverser(g)) {
results = traverser.customizedKneighbor(sourceId, step,
vStep,
results = traverser.customizedKneighbor(sourceId, steps,
request.maxDepth,
request.limit,
request.withEdge);
Expand Down Expand Up @@ -196,10 +192,8 @@ private static class Request {

@JsonProperty("source")
public Object source;
@JsonProperty("step")
public TraverserAPI.Step step;
@JsonProperty("vertex_step")
public TraverserAPI.VStep vStep;
@JsonProperty("steps")
public TraverserAPI.EVSteps steps;
@JsonProperty("max_depth")
public int maxDepth;
@JsonProperty("limit")
Expand All @@ -215,12 +209,12 @@ private static class Request {

@Override
public String toString() {
return String.format("PathRequest{source=%s,step=%s,vStep=%s," +
return String.format("PathRequest{source=%s,steps=%s," +
"maxDepth=%s,limit=%s,countOnly=%s," +
"withVertex=%s,withPath=%s,withEdge=%s}",
this.source, this.step, this.vStep,
this.maxDepth, this.limit,this.countOnly,
this.withVertex, this.withPath, this.withEdge);
this.source, this.steps, this.maxDepth,
this.limit,this.countOnly, this.withVertex,
this.withPath, this.withEdge);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -41,8 +40,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import com.baidu.hugegraph.traversal.algorithm.steps.VertexStep;
import com.google.common.collect.ImmutableMap;
import com.baidu.hugegraph.traversal.algorithm.steps.Steps;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;
Expand All @@ -59,7 +57,6 @@
import com.baidu.hugegraph.traversal.algorithm.HugeTraverser;
import com.baidu.hugegraph.traversal.algorithm.KoutTraverser;
import com.baidu.hugegraph.traversal.algorithm.records.KoutRecords;
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 @@ -110,15 +107,9 @@ public String get(@Context GraphManager manager,
Collection<Id> ids;
try (KoutTraverser traverser = new KoutTraverser(g)) {
if (HugeTraverser.isDeepFirstAlgorithm(algorithm)) {
List<String> lables = new ArrayList<>();
if (edgeLabel != null) {
lables.add(edgeLabel);
}
EdgeStep edgeStep = new EdgeStep(
g, dir, lables, ImmutableMap.of(),
maxDegree, 0);
Steps steps = steps(g, dir, edgeLabel, maxDegree);
KoutRecords results = traverser.deepFirstKout(
sourceId, edgeStep, null, depth,
sourceId, steps, depth,
nearest, capacity, limit, false);
ids = results.ids(limit);
} else {
Expand All @@ -144,7 +135,7 @@ public String post(@Context GraphManager manager,
E.checkArgumentNotNull(request, "The request body can't be null");
E.checkArgumentNotNull(request.source,
"The source of request can't be null");
E.checkArgument(request.step != null,
E.checkArgument(request.steps != null,
"The steps of request can't be null");
if (request.countOnly) {
E.checkArgument(!request.withVertex && !request.withPath,
Expand All @@ -153,34 +144,29 @@ public String post(@Context GraphManager manager,
HugeTraverser.checkAlgorithm(request.algorithm);

LOG.debug("Graph [{}] get customized kout from source vertex '{}', " +
"with step '{}', max_depth '{}', nearest '{}', " +
"with steps '{}', max_depth '{}', nearest '{}', " +
"count_only '{}', capacity '{}', limit '{}', " +
"with_vertex '{}', with_path '{}', with_edge '{}' " +
"and algorithm '{}'", graph, request.source, request.step,
"and algorithm '{}'", graph, request.source, request.steps,
request.maxDepth, request.nearest, request.countOnly,
request.capacity, request.limit, request.withVertex,
request.withPath, request.withEdge, request.algorithm);

HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);

EdgeStep step = step(g, request.step);
VertexStep vStep = request.vStep == null ? null :
vertexStep(g, request.vStep);
Steps steps = steps(g, request.steps);

KoutRecords results;
try (KoutTraverser traverser = new KoutTraverser(g)) {
if (HugeTraverser.isDeepFirstAlgorithm(request.algorithm)) {
results = traverser.deepFirstKout(sourceId, step,
vStep,
results = traverser.deepFirstKout(sourceId, steps,
request.maxDepth,
request.nearest,
request.capacity,
request.limit,
request.withEdge);
} else {
results = traverser.customizedKout(sourceId, step,
vStep,
results = traverser.customizedKout(sourceId, steps,
request.maxDepth,
request.nearest,
request.capacity,
Expand Down Expand Up @@ -238,10 +224,8 @@ private static class Request {

@JsonProperty("source")
public Object source;
@JsonProperty("step")
public TraverserAPI.Step step;
@JsonProperty("vertex_step")
public TraverserAPI.VStep vStep;
@JsonProperty("steps")
public TraverserAPI.EVSteps steps;
@JsonProperty("max_depth")
public int maxDepth;
@JsonProperty("nearest")
Expand All @@ -263,15 +247,14 @@ private static class Request {

@Override
public String toString() {
return String.format("KoutRequest{source=%s,step=%s," +
"vStep=%s,maxDepth=%s,nearest=%s," +
"countOnly=%s,capacity=%s,limit=%s," +
"withVertex=%s,withPath=%s,withEdge=%s," +
"algorithm=%s}", this.source, this.step,
this.vStep, this.maxDepth, this.nearest,
this.countOnly, this.capacity, this.limit,
this.withVertex, this.withPath, this.withEdge,
this.algorithm);
return String.format("KoutRequest{source=%s,steps=%s," +
"maxDepth=%s,nearest=%s,countOnly=%s," +
"capacity=%s,limit=%s,withVertex=%s," +
"withPath=%s,withEdge=%s,algorithm=%s}",
this.source, this.steps, this.maxDepth,
this.nearest, this.countOnly, this.capacity,
this.limit, this.withVertex, this.withPath,
this.withEdge, this.algorithm);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep;
import com.baidu.hugegraph.traversal.algorithm.steps.VertexStep;
import com.baidu.hugegraph.traversal.algorithm.steps.Steps;
import com.baidu.hugegraph.type.define.Directions;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -39,9 +41,36 @@ protected static EdgeStep step(HugeGraph graph, Step step) {
step.maxDegree, step.skipDegree);
}

protected static VertexStep vertexStep(HugeGraph graph,
VStep vStep) {
return new VertexStep(graph, vStep.labels, vStep.properties);
protected static Steps steps(HugeGraph graph, Directions directions,
String label, long maxDegree) {
Map<String, Map<String, Object>> eSteps = new HashMap<>();
eSteps.put(label, null);
return new Steps(graph, directions, eSteps, null,
maxDegree, 0);
}

protected static Steps steps(HugeGraph graph, EVSteps steps) {
Map<String, Map<String, Object>> eSteps = new HashMap<>();
Map<String, Map<String, Object>> vSteps = new HashMap<>();

if (steps.eSteps != null) {
Iterator<EVStepEntity> itr = steps.eSteps.iterator();
while (itr.hasNext()) {
EVStepEntity item = itr.next();
eSteps.put(item.label, item.properties);
}
}

if (steps.vSteps != null) {
Iterator<EVStepEntity> itr = steps.vSteps.iterator();
while (itr.hasNext()) {
EVStepEntity item = itr.next();
vSteps.put(item.label, item.properties);
}
}

return new Steps(graph, steps.direction, eSteps, vSteps,
steps.maxDegree, steps.skipDegree);
}

protected static class Step {
Expand All @@ -67,18 +96,43 @@ public String toString() {
}
}

protected static class VStep {
protected static class EVStepEntity {
@JsonProperty("label")
public String label;

@JsonProperty("labels")
public List<String> labels;
@JsonProperty("properties")
public Map<String, Object> properties;

@Override
public String toString() {
return String.format("VertexStep{labels=%s,properties=%s}",
this.labels, this.properties);
return String.format("EVStepEntity{label=%s,properties=%s}",
this.label, this.properties);
}
}

protected static class EVSteps {
@JsonProperty("direction")
public Directions direction;

@JsonAlias("degree")
@JsonProperty("max_degree")
public long maxDegree = Long.parseLong(DEFAULT_MAX_DEGREE);

@JsonProperty("skip_degree")
public long skipDegree = 0L;

@JsonProperty("edge_steps")
public List<EVStepEntity> eSteps;

@JsonProperty("vertex_steps")
public List<EVStepEntity> vSteps;

@Override
public String toString() {
return String.format("Steps{direction=%s,maxDegree=%s," +
"skipDegree=%s,eSteps=%s,vSteps=%s}",
this.direction, this.maxDegree,
this.skipDegree, this.eSteps, this.vSteps);
}
}
}
Loading

0 comments on commit 08aafc8

Please sign in to comment.