use of com.baidu.hugegraph.traversal.algorithm.KneighborTraverser in project incubator-hugegraph by apache.
the class KneighborAPI method get.
@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("source") String sourceV, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_depth") int depth, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, @QueryParam("limit") @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) {
LOG.debug("Graph [{}] get k-neighbor from '{}' with " + "direction '{}', edge label '{}', max depth '{}', " + "max degree '{}' and limit '{}'", graph, sourceV, direction, edgeLabel, depth, maxDegree, limit);
Id source = VertexAPI.checkAndParseVertexId(sourceV);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
HugeGraph g = graph(manager, graph);
Set<Id> ids;
try (KneighborTraverser traverser = new KneighborTraverser(g)) {
ids = traverser.kneighbor(source, dir, edgeLabel, depth, maxDegree, limit);
}
return manager.serializer(g).writeList("vertices", ids);
}
use of com.baidu.hugegraph.traversal.algorithm.KneighborTraverser in project incubator-hugegraph by apache.
the class KneighborAPI method post.
@POST
@Timed
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String post(@Context GraphManager manager, @PathParam("graph") String graph, Request request) {
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, "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_vertex '{}' and with_path '{}'", graph, request.source, request.step, request.limit, request.countOnly, request.withVertex, request.withPath);
HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);
EdgeStep step = step(g, request.step);
KneighborRecords results;
try (KneighborTraverser traverser = new KneighborTraverser(g)) {
results = traverser.customizedKneighbor(sourceId, step, request.maxDepth, request.limit);
}
long size = results.size();
if (request.limit != Query.NO_LIMIT && size > request.limit) {
size = request.limit;
}
List<Id> neighbors = request.countOnly ? ImmutableList.of() : results.ids(request.limit);
HugeTraverser.PathSet paths = new HugeTraverser.PathSet();
if (request.withPath) {
paths.addAll(results.paths(request.limit));
}
Iterator<Vertex> iter = QueryResults.emptyIterator();
if (request.withVertex && !request.countOnly) {
Set<Id> ids = new HashSet<>(neighbors);
if (request.withPath) {
for (HugeTraverser.Path p : paths) {
ids.addAll(p.vertices());
}
}
if (!ids.isEmpty()) {
iter = g.vertices(ids.toArray());
}
}
return manager.serializer(g).writeNodesWithPath("kneighbor", neighbors, size, paths, iter);
}
Aggregations