Search in sources :

Example 11 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class RelationshipManager method newVertex.

private HugeVertex newVertex(Object id, String label) {
    VertexLabel vl = this.graph().vertexLabel(label);
    Id idValue = HugeVertex.getIdValue(id);
    return HugeVertex.create(this.tx(), idValue, vl);
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id)

Example 12 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class CollectionPathsTraverser method paths.

public Collection<Path> paths(Iterator<Vertex> sources, Iterator<Vertex> targets, EdgeStep step, int depth, boolean nearest, long capacity, long limit) {
    checkCapacity(capacity);
    checkLimit(limit);
    List<Id> sourceList = newList();
    while (sources.hasNext()) {
        sourceList.add(((HugeVertex) sources.next()).id());
    }
    int sourceSize = sourceList.size();
    E.checkState(sourceSize >= 1 && sourceSize <= MAX_VERTICES, "The number of source vertices must in [1, %s], " + "but got: %s", MAX_VERTICES, sourceList.size());
    List<Id> targetList = newList();
    while (targets.hasNext()) {
        targetList.add(((HugeVertex) targets.next()).id());
    }
    int targetSize = targetList.size();
    E.checkState(targetSize >= 1 && targetSize <= MAX_VERTICES, "The number of target vertices must in [1, %s], " + "but got: %s", MAX_VERTICES, sourceList.size());
    checkPositive(depth, "max depth");
    TraverseStrategy strategy = TraverseStrategy.create(depth >= this.concurrentDepth(), this.graph());
    Traverser traverser;
    if (nearest) {
        traverser = new NearestTraverser(this, strategy, sourceList, targetList, step, depth, capacity, limit);
    } else {
        traverser = new Traverser(this, strategy, sourceList, targetList, step, depth, capacity, limit);
    }
    do {
        // Forward
        traverser.forward();
        if (traverser.finished()) {
            return traverser.paths();
        }
        // Backward
        traverser.backward();
        if (traverser.finished()) {
            return traverser.paths();
        }
    } while (true);
}
Also used : TraverseStrategy(com.baidu.hugegraph.traversal.algorithm.strategy.TraverseStrategy) Id(com.baidu.hugegraph.backend.id.Id)

Example 13 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class CustomizePathsTraverser method customizedPaths.

public List<Path> customizedPaths(Iterator<Vertex> vertices, List<WeightedEdgeStep> steps, boolean sorted, long capacity, long limit) {
    E.checkArgument(vertices.hasNext(), "The source vertices can't be empty");
    E.checkArgument(!steps.isEmpty(), "The steps can't be empty");
    checkCapacity(capacity);
    checkLimit(limit);
    MultivaluedMap<Id, Node> sources = newMultivalueMap();
    while (vertices.hasNext()) {
        HugeVertex vertex = (HugeVertex) vertices.next();
        Node node = sorted ? new WeightNode(vertex.id(), null, 0) : new Node(vertex.id(), null);
        sources.add(vertex.id(), node);
    }
    int stepNum = steps.size();
    int pathCount = 0;
    long access = 0;
    MultivaluedMap<Id, Node> newVertices = null;
    root: for (WeightedEdgeStep step : steps) {
        stepNum--;
        newVertices = newMultivalueMap();
        Iterator<Edge> edges;
        // Traversal vertices of previous level
        for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
            List<Node> adjacency = newList();
            edges = this.edgesOfVertex(entry.getKey(), step.step());
            while (edges.hasNext()) {
                HugeEdge edge = (HugeEdge) edges.next();
                Id target = edge.id().otherVertexId();
                for (Node n : entry.getValue()) {
                    // If have loop, skip target
                    if (n.contains(target)) {
                        continue;
                    }
                    Node newNode;
                    if (sorted) {
                        double w = step.weightBy() != null ? edge.value(step.weightBy().name()) : step.defaultWeight();
                        newNode = new WeightNode(target, n, w);
                    } else {
                        newNode = new Node(target, n);
                    }
                    adjacency.add(newNode);
                    checkCapacity(capacity, ++access, "customized paths");
                }
            }
            if (step.sample() > 0) {
                // Sample current node's adjacent nodes
                adjacency = sample(adjacency, step.sample());
            }
            // Add current node's adjacent nodes
            for (Node node : adjacency) {
                newVertices.add(node.id(), node);
                // Avoid exceeding limit
                if (stepNum == 0) {
                    if (limit != NO_LIMIT && !sorted && ++pathCount >= limit) {
                        break root;
                    }
                }
            }
        }
        // Re-init sources
        sources = newVertices;
    }
    if (stepNum != 0) {
        return ImmutableList.of();
    }
    List<Path> paths = newList();
    for (List<Node> nodes : newVertices.values()) {
        for (Node n : nodes) {
            if (sorted) {
                WeightNode wn = (WeightNode) n;
                paths.add(new WeightPath(wn.path(), wn.weights()));
            } else {
                paths.add(new Path(n.path()));
            }
        }
    }
    return paths;
}
Also used : HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) WeightedEdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep) Iterator(java.util.Iterator) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Id(com.baidu.hugegraph.backend.id.Id)

Example 14 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class StandardTaskScheduler method executeTasksOnWorker.

protected void executeTasksOnWorker(Id server) {
    String page = this.supportsPaging() ? PageInfo.PAGE_NONE : null;
    do {
        Iterator<HugeTask<Object>> tasks = this.tasks(TaskStatus.SCHEDULED, PAGE_SIZE, page);
        while (tasks.hasNext()) {
            HugeTask<?> task = tasks.next();
            this.initTaskCallable(task);
            Id taskServer = task.server();
            if (taskServer == null) {
                LOG.warn("Task '{}' may not be scheduled", task.id());
                continue;
            }
            HugeTask<?> memTask = this.tasks.get(task.id());
            if (memTask != null) {
                assert memTask.status().code() > task.status().code();
                continue;
            }
            if (taskServer.equals(server)) {
                task.status(TaskStatus.QUEUED);
                this.save(task);
                this.submitTask(task);
            }
        }
        if (page != null) {
            page = PageInfo.pageInfo(tasks);
        }
    } while (page != null);
}
Also used : Id(com.baidu.hugegraph.backend.id.Id)

Example 15 with Id

use of com.baidu.hugegraph.backend.id.Id 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)

Aggregations

Id (com.baidu.hugegraph.backend.id.Id)381 Test (org.junit.Test)124 HugeGraph (com.baidu.hugegraph.HugeGraph)104 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)71 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)54 Timed (com.codahale.metrics.annotation.Timed)41 Produces (jakarta.ws.rs.Produces)40 AuthManager (com.baidu.hugegraph.auth.AuthManager)39 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)37 ArrayList (java.util.ArrayList)35 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)33 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)33 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)32 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)32 Directions (com.baidu.hugegraph.type.define.Directions)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)25 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)25 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)24 Edge (org.apache.tinkerpop.gremlin.structure.Edge)22 HugeType (com.baidu.hugegraph.type.HugeType)21