Search in sources :

Example 1 with KneighborRecords

use of com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords 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);
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTraverser(com.baidu.hugegraph.traversal.algorithm.HugeTraverser) KneighborTraverser(com.baidu.hugegraph.traversal.algorithm.KneighborTraverser) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Id(com.baidu.hugegraph.backend.id.Id) HashSet(java.util.HashSet) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with KneighborRecords

use of com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords in project incubator-hugegraph by apache.

the class KneighborTraverser method customizedKneighbor.

public KneighborRecords customizedKneighbor(Id source, EdgeStep step, int maxDepth, long limit) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    checkPositive(maxDepth, "k-neighbor max_depth");
    checkLimit(limit);
    boolean concurrent = maxDepth >= this.concurrentDepth();
    KneighborRecords records = new KneighborRecords(concurrent, source, true);
    Consumer<Id> consumer = v -> {
        if (this.reachLimit(limit, records.size())) {
            return;
        }
        Iterator<Edge> edges = edgesOfVertex(v, step);
        while (!this.reachLimit(limit, records.size()) && edges.hasNext()) {
            Id target = ((HugeEdge) edges.next()).id().otherVertexId();
            records.addPath(v, target);
        }
    };
    while (maxDepth-- > 0) {
        records.startOneLayer(true);
        traverseIds(records.keys(), consumer, concurrent);
        records.finishOneLayer();
    }
    return records;
}
Also used : Consumer(java.util.function.Consumer) Iterator(java.util.Iterator) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Directions(com.baidu.hugegraph.type.define.Directions) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Set(java.util.Set) E(com.baidu.hugegraph.util.E) Edge(org.apache.tinkerpop.gremlin.structure.Edge) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Iterator(java.util.Iterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id)

Aggregations

HugeGraph (com.baidu.hugegraph.HugeGraph)2 Id (com.baidu.hugegraph.backend.id.Id)2 KneighborRecords (com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords)2 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)2 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)1 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)1 KneighborTraverser (com.baidu.hugegraph.traversal.algorithm.KneighborTraverser)1 Directions (com.baidu.hugegraph.type.define.Directions)1 E (com.baidu.hugegraph.util.E)1 Timed (com.codahale.metrics.annotation.Timed)1 Consumes (jakarta.ws.rs.Consumes)1 POST (jakarta.ws.rs.POST)1 Produces (jakarta.ws.rs.Produces)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Edge (org.apache.tinkerpop.gremlin.structure.Edge)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1