use of com.baidu.hugegraph.traversal.algorithm.records.KoutRecords in project incubator-hugegraph by apache.
the class KoutTraverser method customizedKout.
public KoutRecords customizedKout(Id source, EdgeStep step, int maxDepth, boolean nearest, long capacity, long limit) {
E.checkNotNull(source, "source vertex id");
this.checkVertexExist(source, "source vertex");
checkPositive(maxDepth, "k-out max_depth");
checkCapacity(capacity);
checkLimit(limit);
long[] depth = new long[1];
depth[0] = maxDepth;
boolean concurrent = maxDepth >= this.concurrentDepth();
KoutRecords records = new KoutRecords(concurrent, source, nearest);
Consumer<Id> consumer = v -> {
if (this.reachLimit(limit, depth[0], records.size())) {
return;
}
Iterator<Edge> edges = edgesOfVertex(v, step);
while (!this.reachLimit(limit, depth[0], records.size()) && edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
records.addPath(v, target);
this.checkCapacity(capacity, records.accessed(), depth[0]);
}
};
while (depth[0]-- > 0) {
records.startOneLayer(true);
this.traverseIds(records.keys(), consumer, concurrent);
records.finishOneLayer();
}
return records;
}
use of com.baidu.hugegraph.traversal.algorithm.records.KoutRecords in project incubator-hugegraph by apache.
the class KoutAPI 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 kout from source vertex '{}', " + "with step '{}', max_depth '{}', nearest '{}', " + "count_only '{}', capacity '{}', limit '{}', " + "with_vertex '{}' and with_path '{}'", graph, request.source, request.step, request.maxDepth, request.nearest, request.countOnly, request.capacity, request.limit, request.withVertex, request.withPath);
HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);
EdgeStep step = step(g, request.step);
KoutRecords results;
try (KoutTraverser traverser = new KoutTraverser(g)) {
results = traverser.customizedKout(sourceId, step, request.maxDepth, request.nearest, request.capacity, 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("kout", neighbors, size, paths, iter);
}
Aggregations