Search in sources :

Example 1 with EdgeStep

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

the class CountTraverser method count.

public long count(Id source, List<EdgeStep> steps, boolean containsTraversed, long dedupSize) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    E.checkArgument(steps != null && !steps.isEmpty(), "The steps can't be empty");
    checkDedupSize(dedupSize);
    this.containsTraversed = containsTraversed;
    this.dedupSize = dedupSize;
    if (this.containsTraversed) {
        this.count.increment();
    }
    int stepNum = steps.size();
    EdgeStep firstStep = steps.get(0);
    if (stepNum == 1) {
        // Just one step, query count and return
        long edgesCount = this.edgesCount(source, firstStep);
        this.count.add(edgesCount);
        return this.count.longValue();
    }
    // Multiple steps, construct first step to iterator
    Iterator<Edge> edges = this.edgesOfVertexWithCount(source, firstStep);
    // Wrap steps to Iterator except last step
    for (int i = 1; i < stepNum - 1; i++) {
        EdgeStep currentStep = steps.get(i);
        edges = new FlatMapperIterator<>(edges, (edge) -> {
            Id target = ((HugeEdge) edge).id().otherVertexId();
            return this.edgesOfVertexWithCount(target, currentStep);
        });
    }
    // The last step, just query count
    EdgeStep lastStep = steps.get(stepNum - 1);
    while (edges.hasNext()) {
        Id target = ((HugeEdge) edges.next()).id().otherVertexId();
        if (this.dedup(target)) {
            continue;
        }
        // Count last layer vertices(without dedup size)
        long edgesCount = this.edgesCount(target, lastStep);
        this.count.add(edgesCount);
    }
    return this.count.longValue();
}
Also used : Iterator(java.util.Iterator) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Set(java.util.Set) List(java.util.List) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) HugeGraph(com.baidu.hugegraph.HugeGraph) Id(com.baidu.hugegraph.backend.id.Id) E(com.baidu.hugegraph.util.E) MutableLong(org.apache.commons.lang.mutable.MutableLong) Edge(org.apache.tinkerpop.gremlin.structure.Edge) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 2 with EdgeStep

use of com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep 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;
}
Also used : Consumer(java.util.function.Consumer) HugeException(com.baidu.hugegraph.HugeException) KoutRecords(com.baidu.hugegraph.traversal.algorithm.records.KoutRecords) Iterator(java.util.Iterator) 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) KoutRecords(com.baidu.hugegraph.traversal.algorithm.records.KoutRecords) Iterator(java.util.Iterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id)

Example 3 with EdgeStep

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

the class PathTraverser method backward.

public void backward() {
    EdgeStep currentStep = this.nextStep(false);
    if (currentStep == null) {
        return;
    }
    this.beforeTraverse(false);
    currentStep.swithDirection();
    // Traversal vertices of previous level
    this.traverseOneLayer(this.targets, currentStep, this::backward);
    currentStep.swithDirection();
    this.afterTraverse(currentStep, false);
}
Also used : EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)

Example 4 with EdgeStep

use of com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep 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 5 with EdgeStep

use of com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep 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)

Aggregations

EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)15 Id (com.baidu.hugegraph.backend.id.Id)12 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Timed (com.codahale.metrics.annotation.Timed)6 POST (jakarta.ws.rs.POST)6 Produces (jakarta.ws.rs.Produces)6 Consumes (jakarta.ws.rs.Consumes)5 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)4 E (com.baidu.hugegraph.util.E)4 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 Edge (org.apache.tinkerpop.gremlin.structure.Edge)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 Directions (com.baidu.hugegraph.type.define.Directions)3 HugeException (com.baidu.hugegraph.HugeException)2 QueryResults (com.baidu.hugegraph.backend.query.QueryResults)2 FilterIterator (com.baidu.hugegraph.iterator.FilterIterator)2 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)2