Search in sources :

Example 16 with Id

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

the class JaccardSimilarTraverser method jaccardSimilarity.

public double jaccardSimilarity(Id vertex, Id other, Directions dir, String label, long degree) {
    E.checkNotNull(vertex, "vertex id");
    E.checkNotNull(other, "the other vertex id");
    this.checkVertexExist(vertex, "vertex");
    this.checkVertexExist(other, "other vertex");
    E.checkNotNull(dir, "direction");
    checkDegree(degree);
    Id labelId = this.getEdgeLabelId(label);
    Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(vertex, dir, labelId, degree));
    Set<Id> targetNeighbors = IteratorUtils.set(this.adjacentVertices(other, dir, labelId, degree));
    return jaccardSimilarity(sourceNeighbors, targetNeighbors);
}
Also used : Id(com.baidu.hugegraph.backend.id.Id)

Example 17 with Id

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

the class JaccardSimilarTraverser method jaccardSimilarsConcurrent.

public Map<Id, Double> jaccardSimilarsConcurrent(Id source, EdgeStep step, long capacity) {
    AtomicLong count = new AtomicLong(0L);
    Set<Id> accessed = ConcurrentHashMap.newKeySet();
    accessed.add(source);
    reachCapacity(count.incrementAndGet(), capacity);
    // Query neighbors
    Set<Id> layer1s = this.adjacentVertices(source, step);
    reachCapacity(count.get() + layer1s.size(), capacity);
    count.addAndGet(layer1s.size());
    if (layer1s.isEmpty()) {
        return ImmutableMap.of();
    }
    Map<Id, Double> results = new ConcurrentHashMap<>();
    Set<Id> layer2All = ConcurrentHashMap.newKeySet();
    this.traverseIds(layer1s.iterator(), id -> {
        // Skip if accessed already
        if (accessed.contains(id)) {
            return;
        }
        Set<Id> layer2s = this.adjacentVertices(id, step);
        if (layer2s.isEmpty()) {
            results.put(id, 0.0D);
        }
        layer2All.addAll(layer2s);
        reachCapacity(count.get() + layer2All.size(), capacity);
        double jaccardSimilarity = this.jaccardSimilarity(layer1s, layer2s);
        results.put(id, jaccardSimilarity);
        accessed.add(id);
    });
    count.addAndGet(layer2All.size());
    this.traverseIds(layer2All.iterator(), id -> {
        // Skip if accessed already
        if (accessed.contains(id)) {
            return;
        }
        Set<Id> layer3s = this.adjacentVertices(id, step);
        reachCapacity(count.get() + layer3s.size(), capacity);
        if (layer3s.isEmpty()) {
            results.put(id, 0.0D);
        }
        double jaccardSimilarity = this.jaccardSimilarity(layer1s, layer3s);
        results.put(id, jaccardSimilarity);
        accessed.add(id);
    });
    return results;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Id(com.baidu.hugegraph.backend.id.Id) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 18 with Id

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

the class KoutTraverser method kout.

public Set<Id> kout(Id sourceV, Directions dir, String label, int depth, boolean nearest, long degree, long capacity, long limit) {
    E.checkNotNull(sourceV, "source vertex id");
    this.checkVertexExist(sourceV, "source vertex");
    E.checkNotNull(dir, "direction");
    checkPositive(depth, "k-out max_depth");
    checkDegree(degree);
    checkCapacity(capacity);
    checkLimit(limit);
    if (capacity != NO_LIMIT) {
        // Capacity must > limit because sourceV is counted in capacity
        E.checkArgument(capacity >= limit && limit != NO_LIMIT, "Capacity can't be less than limit, " + "but got capacity '%s' and limit '%s'", capacity, limit);
    }
    Id labelId = this.getEdgeLabelId(label);
    Set<Id> latest = newIdSet();
    latest.add(sourceV);
    Set<Id> all = newIdSet();
    all.add(sourceV);
    long remaining = capacity == NO_LIMIT ? NO_LIMIT : capacity - latest.size();
    while (depth-- > 0) {
        // Just get limit nodes in last layer if limit < remaining capacity
        if (depth == 0 && limit != NO_LIMIT && (limit < remaining || remaining == NO_LIMIT)) {
            remaining = limit;
        }
        if (nearest) {
            latest = this.adjacentVertices(sourceV, latest, dir, labelId, all, degree, remaining);
            all.addAll(latest);
        } else {
            latest = this.adjacentVertices(sourceV, latest, dir, labelId, null, degree, remaining);
        }
        if (capacity != NO_LIMIT) {
            // Update 'remaining' value to record remaining capacity
            remaining -= latest.size();
            if (remaining <= 0 && depth > 0) {
                throw new HugeException("Reach capacity '%s' while remaining depth '%s'", capacity, depth);
            }
        }
    }
    return latest;
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) HugeException(com.baidu.hugegraph.HugeException)

Example 19 with Id

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

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

the class NeighborRankTraverser method contributePrevLayers.

private void contributePrevLayers(List<Ranks> ranks, double incr, Map<Integer, Set<Id>> prevLayerNodesV) {
    for (Map.Entry<Integer, Set<Id>> e : prevLayerNodesV.entrySet()) {
        Ranks prevLayerRanks = ranks.get(e.getKey());
        for (Id node : e.getValue()) {
            double oldRank = prevLayerRanks.get(node);
            prevLayerRanks.put(node, oldRank + incr);
        }
    }
}
Also used : Set(java.util.Set) Id(com.baidu.hugegraph.backend.id.Id) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) OrderLimitMap(com.baidu.hugegraph.util.OrderLimitMap)

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