Search in sources :

Example 6 with Directions

use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.

the class BinarySerializer method writeQueryEdgePrefixCondition.

private Query writeQueryEdgePrefixCondition(ConditionQuery cq) {
    int count = 0;
    BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
    for (HugeKeys key : EdgeId.KEYS) {
        Object value = cq.condition(key);
        if (value != null) {
            count++;
        } else {
            if (key == HugeKeys.DIRECTION) {
                // Direction is null, set to OUT
                value = Directions.OUT;
            } else {
                break;
            }
        }
        if (key == HugeKeys.OWNER_VERTEX || key == HugeKeys.OTHER_VERTEX) {
            writePartitionedId(HugeType.EDGE, (Id) value, buffer);
        } else if (key == HugeKeys.DIRECTION) {
            byte t = ((Directions) value).type().code();
            buffer.write(t);
        } else if (key == HugeKeys.LABEL) {
            assert value instanceof Id;
            buffer.writeId((Id) value);
        } else if (key == HugeKeys.SORT_VALUES) {
            assert value instanceof String;
            buffer.writeStringWithEnding((String) value);
        } else {
            assert false : key;
        }
    }
    if (count > 0) {
        assert count == cq.conditionsSize();
        return prefixQuery(cq, new BinaryId(buffer.bytes(), null));
    }
    return null;
}
Also used : BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) Directions(com.baidu.hugegraph.type.define.Directions) BinaryId(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys)

Example 7 with Directions

use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.

the class PersonalRankTraverser method calcNewRanks.

private Map<Id, Double> calcNewRanks(Set<Id> outSeeds, Set<Id> inSeeds, Id label, Map<Id, Double> ranks) {
    Map<Id, Double> newRanks = newMap();
    BiFunction<Set<Id>, Directions, Set<Id>> neighborIncrRanks;
    neighborIncrRanks = (seeds, dir) -> {
        Set<Id> tmpSeeds = newIdSet();
        for (Id seed : seeds) {
            Double oldRank = ranks.get(seed);
            E.checkState(oldRank != null, "Expect rank of seed exists");
            Iterator<Id> iter = this.adjacentVertices(seed, dir, label, this.degree);
            List<Id> neighbors = IteratorUtils.list(iter);
            long degree = neighbors.size();
            if (degree == 0L) {
                newRanks.put(seed, oldRank);
                continue;
            }
            double incrRank = oldRank * this.alpha / degree;
            // Collect all neighbors increment
            for (Id neighbor : neighbors) {
                tmpSeeds.add(neighbor);
                // Assign an initial value when firstly update neighbor rank
                double rank = newRanks.getOrDefault(neighbor, 0.0);
                newRanks.put(neighbor, rank + incrRank);
            }
        }
        return tmpSeeds;
    };
    Set<Id> tmpInSeeds = neighborIncrRanks.apply(outSeeds, Directions.OUT);
    Set<Id> tmpOutSeeds = neighborIncrRanks.apply(inSeeds, Directions.IN);
    outSeeds.addAll(tmpOutSeeds);
    inSeeds.addAll(tmpInSeeds);
    return newRanks;
}
Also used : Set(java.util.Set) Directions(com.baidu.hugegraph.type.define.Directions) Id(com.baidu.hugegraph.backend.id.Id)

Example 8 with Directions

use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.

the class AllShortestPathsAPI method get.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("source") String source, @QueryParam("target") String target, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_depth") int depth, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, @QueryParam("skip_degree") @DefaultValue("0") long skipDegree, @QueryParam("capacity") @DefaultValue(DEFAULT_CAPACITY) long capacity) {
    LOG.debug("Graph [{}] get shortest path from '{}', to '{}' with " + "direction {}, edge label {}, max depth '{}', " + "max degree '{}', skipped degree '{}' and capacity '{}'", graph, source, target, direction, edgeLabel, depth, maxDegree, skipDegree, capacity);
    Id sourceId = VertexAPI.checkAndParseVertexId(source);
    Id targetId = VertexAPI.checkAndParseVertexId(target);
    Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
    HugeGraph g = graph(manager, graph);
    ShortestPathTraverser traverser = new ShortestPathTraverser(g);
    List<String> edgeLabels = edgeLabel == null ? ImmutableList.of() : ImmutableList.of(edgeLabel);
    HugeTraverser.PathSet paths = traverser.allShortestPaths(sourceId, targetId, dir, edgeLabels, depth, maxDegree, skipDegree, capacity);
    return manager.serializer(g).writePaths("paths", paths, false);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) ShortestPathTraverser(com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser) Directions(com.baidu.hugegraph.type.define.Directions) HugeTraverser(com.baidu.hugegraph.traversal.algorithm.HugeTraverser) Id(com.baidu.hugegraph.backend.id.Id) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 9 with Directions

use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.

the class RamTable method query.

private Iterator<HugeEdge> query(ConditionQuery query) {
    Id owner = query.condition(HugeKeys.OWNER_VERTEX);
    assert owner != null;
    Directions dir = query.condition(HugeKeys.DIRECTION);
    if (dir == null) {
        dir = Directions.BOTH;
    }
    Id label = query.condition(HugeKeys.LABEL);
    if (label == null) {
        label = IdGenerator.ZERO;
    }
    return this.query(owner.asLong(), dir, (int) label.asLong());
}
Also used : Directions(com.baidu.hugegraph.type.define.Directions) Id(com.baidu.hugegraph.backend.id.Id)

Example 10 with Directions

use of com.baidu.hugegraph.type.define.Directions in project incubator-hugegraph by apache.

the class JaccardSimilarityAPI method get.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("vertex") String vertex, @QueryParam("other") String other, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree) {
    LOG.debug("Graph [{}] get jaccard similarity between '{}' and '{}' " + "with direction {}, edge label {} and max degree '{}'", graph, vertex, other, direction, edgeLabel, maxDegree);
    Id sourceId = VertexAPI.checkAndParseVertexId(vertex);
    Id targetId = VertexAPI.checkAndParseVertexId(other);
    Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
    HugeGraph g = graph(manager, graph);
    double similarity;
    try (JaccardSimilarTraverser traverser = new JaccardSimilarTraverser(g)) {
        similarity = traverser.jaccardSimilarity(sourceId, targetId, dir, edgeLabel, maxDegree);
    }
    return JsonUtil.toJson(ImmutableMap.of("jaccard_similarity", similarity));
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) Directions(com.baidu.hugegraph.type.define.Directions) Id(com.baidu.hugegraph.backend.id.Id) JaccardSimilarTraverser(com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Aggregations

Id (com.baidu.hugegraph.backend.id.Id)24 Directions (com.baidu.hugegraph.type.define.Directions)24 HugeGraph (com.baidu.hugegraph.HugeGraph)15 Timed (com.codahale.metrics.annotation.Timed)14 GET (jakarta.ws.rs.GET)14 Produces (jakarta.ws.rs.Produces)14 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)6 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)3 Condition (com.baidu.hugegraph.backend.query.Condition)3 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 RangeConditions (com.baidu.hugegraph.backend.query.Condition.RangeConditions)2 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)2 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)2 BinaryId (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId)2 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)2 PathsTraverser (com.baidu.hugegraph.traversal.algorithm.PathsTraverser)2 PredictionTraverser (com.baidu.hugegraph.traversal.algorithm.PredictionTraverser)2 ShortestPathTraverser (com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser)2 SingleSourceShortestPathTraverser (com.baidu.hugegraph.traversal.algorithm.SingleSourceShortestPathTraverser)2