Search in sources :

Example 1 with KoutTraverser

use of com.baidu.hugegraph.traversal.algorithm.KoutTraverser 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);
}
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) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) KoutTraverser(com.baidu.hugegraph.traversal.algorithm.KoutTraverser) KoutRecords(com.baidu.hugegraph.traversal.algorithm.records.KoutRecords) 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 KoutTraverser

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

the class KoutAPI method get.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("source") String source, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_depth") int depth, @QueryParam("nearest") @DefaultValue("true") boolean nearest, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, @QueryParam("capacity") @DefaultValue(DEFAULT_CAPACITY) long capacity, @QueryParam("limit") @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) {
    LOG.debug("Graph [{}] get k-out from '{}' with " + "direction '{}', edge label '{}', max depth '{}', nearest " + "'{}', max degree '{}', capacity '{}' and limit '{}'", graph, source, direction, edgeLabel, depth, nearest, maxDegree, capacity, limit);
    Id sourceId = VertexAPI.checkAndParseVertexId(source);
    Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
    HugeGraph g = graph(manager, graph);
    Set<Id> ids;
    try (KoutTraverser traverser = new KoutTraverser(g)) {
        ids = traverser.kout(sourceId, dir, edgeLabel, depth, nearest, maxDegree, capacity, limit);
    }
    return manager.serializer(g).writeList("vertices", ids);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) KoutTraverser(com.baidu.hugegraph.traversal.algorithm.KoutTraverser) Directions(com.baidu.hugegraph.type.define.Directions) Id(com.baidu.hugegraph.backend.id.Id) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Aggregations

HugeGraph (com.baidu.hugegraph.HugeGraph)2 Id (com.baidu.hugegraph.backend.id.Id)2 KoutTraverser (com.baidu.hugegraph.traversal.algorithm.KoutTraverser)2 Timed (com.codahale.metrics.annotation.Timed)2 Produces (jakarta.ws.rs.Produces)2 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)1 KoutRecords (com.baidu.hugegraph.traversal.algorithm.records.KoutRecords)1 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)1 Directions (com.baidu.hugegraph.type.define.Directions)1 Consumes (jakarta.ws.rs.Consumes)1 GET (jakarta.ws.rs.GET)1 POST (jakarta.ws.rs.POST)1 HashSet (java.util.HashSet)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1