Search in sources :

Example 36 with HugeEdge

use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.

the class GraphTransaction method queryEdgesByIds.

protected Iterator<Edge> queryEdgesByIds(Object[] edgeIds, boolean verifyId) {
    Query.checkForceCapacity(edgeIds.length);
    // NOTE: allowed duplicated edges if query by duplicated ids
    List<Id> ids = InsertionOrderUtil.newList();
    Map<Id, HugeEdge> edges = new HashMap<>(edgeIds.length);
    IdQuery query = new IdQuery(HugeType.EDGE);
    for (Object edgeId : edgeIds) {
        HugeEdge edge;
        EdgeId id = HugeEdge.getIdValue(edgeId, !verifyId);
        if (id == null) {
            continue;
        }
        if (id.direction() == Directions.IN) {
            id = id.switchDirection();
        }
        if (this.removedEdges.containsKey(id)) {
            // The record has been deleted
            continue;
        } else if ((edge = this.addedEdges.get(id)) != null || (edge = this.updatedEdges.get(id)) != null) {
            if (edge.expired()) {
                continue;
            }
            // Found from local tx
            edges.put(edge.id(), edge);
        } else {
            // Prepare to query from backend store
            query.query(id);
        }
        ids.add(id);
    }
    if (!query.empty()) {
        // Query from backend store
        if (edges.isEmpty() && query.idsSize() == ids.size()) {
            /*
                 * Sort at the lower layer and return directly if there is no
                 * local vertex and duplicated id.
                 */
            Iterator<HugeEdge> it = this.queryEdgesFromBackend(query);
            @SuppressWarnings({ "unchecked", "rawtypes" }) Iterator<Edge> r = (Iterator) it;
            return r;
        }
        query.mustSortByInput(false);
        Iterator<HugeEdge> it = this.queryEdgesFromBackend(query);
        QueryResults.fillMap(it, edges);
    }
    return new MapperIterator<>(ids.iterator(), id -> {
        Edge edge = edges.get(id);
        return edge;
    });
}
Also used : MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) HashMap(java.util.HashMap) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) ListIterator(com.baidu.hugegraph.iterator.ListIterator) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) Iterator(java.util.Iterator) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 37 with HugeEdge

use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.

the class RamTable method loadFromDB.

private void loadFromDB() throws Exception {
    Query query = new Query(HugeType.VERTEX);
    query.capacity(this.verticesCapacityHalf * 2L);
    query.limit(Query.NO_LIMIT);
    Iterator<Vertex> vertices = this.graph.vertices(query);
    // switch concurrent loading here
    boolean concurrent = true;
    if (concurrent) {
        try (LoadTraverser traverser = new LoadTraverser()) {
            traverser.load(vertices);
        }
        return;
    }
    Iterator<Edge> adjEdges;
    Id lastId = IdGenerator.ZERO;
    while (vertices.hasNext()) {
        Id vertex = (Id) vertices.next().id();
        LOG.info("scan from hbase {} loadfromDB", vertex);
        if (vertex.compareTo(lastId) < 0) {
            throw new HugeException("The ramtable feature is not " + "supported by %s backend", this.graph.backend());
        }
        ensureNumberId(vertex);
        lastId = vertex;
        adjEdges = this.graph.adjacentEdges(vertex);
        if (adjEdges.hasNext()) {
            HugeEdge edge = (HugeEdge) adjEdges.next();
            this.addEdge(true, edge);
        }
        while (adjEdges.hasNext()) {
            HugeEdge edge = (HugeEdge) adjEdges.next();
            this.addEdge(false, edge);
        }
    }
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) 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) HugeException(com.baidu.hugegraph.HugeException)

Example 38 with HugeEdge

use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.

the class FusiformSimilarityTraverser method matchMinNeighborCount.

private boolean matchMinNeighborCount(HugeVertex vertex, Directions direction, String label, int minNeighbors, long degree) {
    Iterator<Edge> edges;
    long neighborCount;
    EdgeLabel edgeLabel = null;
    Id labelId = null;
    if (label != null) {
        edgeLabel = this.graph().edgeLabel(label);
        labelId = edgeLabel.id();
    }
    if (edgeLabel != null && edgeLabel.frequency() == Frequency.SINGLE) {
        edges = this.edgesOfVertex(vertex.id(), direction, labelId, minNeighbors);
        neighborCount = IteratorUtils.count(edges);
    } else {
        edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
        Set<Id> neighbors = newIdSet();
        while (edges.hasNext()) {
            Id target = ((HugeEdge) edges.next()).id().otherVertexId();
            neighbors.add(target);
            if (neighbors.size() >= minNeighbors) {
                break;
            }
        }
        neighborCount = neighbors.size();
    }
    return neighborCount >= minNeighbors;
}
Also used : EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 39 with HugeEdge

use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.

the class FusiformSimilarityTraverser method fusiformSimilarityForVertex.

private Set<Similar> fusiformSimilarityForVertex(HugeVertex vertex, Directions direction, String label, int minNeighbors, double alpha, int minSimilars, int top, String groupProperty, int minGroups, long degree, long capacity, boolean withIntermediary) {
    boolean matched = this.matchMinNeighborCount(vertex, direction, label, minNeighbors, degree);
    if (!matched) {
        // Ignore current vertex if its neighbors number is not enough
        return ImmutableSet.of();
    }
    Id labelId = this.getEdgeLabelId(label);
    // Get similar nodes and counts
    Iterator<Edge> edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
    Map<Id, MutableInt> similars = newMap();
    MultivaluedMap<Id, Id> intermediaries = new MultivaluedHashMap<>();
    Set<Id> neighbors = newIdSet();
    while (edges.hasNext()) {
        Id target = ((HugeEdge) edges.next()).id().otherVertexId();
        if (neighbors.contains(target)) {
            continue;
        }
        neighbors.add(target);
        checkCapacity(capacity, ++this.accessed, "fusiform similarity");
        Directions backDir = direction.opposite();
        Iterator<Edge> backEdges = this.edgesOfVertex(target, backDir, labelId, degree);
        Set<Id> currentSimilars = newIdSet();
        while (backEdges.hasNext()) {
            Id node = ((HugeEdge) backEdges.next()).id().otherVertexId();
            if (currentSimilars.contains(node)) {
                continue;
            }
            currentSimilars.add(node);
            if (withIntermediary) {
                intermediaries.add(node, target);
            }
            MutableInt count = similars.get(node);
            if (count == null) {
                count = new MutableInt(0);
                similars.put(node, count);
                checkCapacity(capacity, ++this.accessed, "fusiform similarity");
            }
            count.increment();
        }
    }
    // Delete source vertex
    assert similars.containsKey(vertex.id());
    similars.remove(vertex.id());
    if (similars.isEmpty()) {
        return ImmutableSet.of();
    }
    // Match alpha
    double neighborNum = neighbors.size();
    Map<Id, Double> matchedAlpha = newMap();
    for (Map.Entry<Id, MutableInt> entry : similars.entrySet()) {
        double score = entry.getValue().intValue() / neighborNum;
        if (score >= alpha) {
            matchedAlpha.put(entry.getKey(), score);
        }
    }
    if (matchedAlpha.size() < minSimilars) {
        return ImmutableSet.of();
    }
    // Sorted and topN if needed
    Map<Id, Double> topN;
    if (top > 0) {
        topN = topN(matchedAlpha, true, top);
    } else {
        topN = matchedAlpha;
    }
    // Filter by groupCount by property
    if (groupProperty != null) {
        Set<Object> values = newSet();
        // Add groupProperty value of source vertex
        values.add(vertex.value(groupProperty));
        for (Id id : topN.keySet()) {
            Vertex v = graph().vertices(id).next();
            values.add(v.value(groupProperty));
        }
        if (values.size() < minGroups) {
            return ImmutableSet.of();
        }
    }
    // Construct result
    Set<Similar> result = InsertionOrderUtil.newSet();
    for (Map.Entry<Id, Double> entry : topN.entrySet()) {
        Id similar = entry.getKey();
        double score = entry.getValue();
        List<Id> inters = withIntermediary ? intermediaries.get(similar) : ImmutableList.of();
        result.add(new Similar(similar, score, inters));
    }
    return result;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Directions(com.baidu.hugegraph.type.define.Directions) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) ImmutableMap(com.google.common.collect.ImmutableMap) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 40 with HugeEdge

use of com.baidu.hugegraph.structure.HugeEdge in project incubator-hugegraph by apache.

the class PathTraverser method traverseOne.

private void traverseOne(Id v, EdgeStep step, boolean forward) {
    if (this.reachLimit()) {
        return;
    }
    Iterator<Edge> edges = this.traverser.edgesOfVertex(v, step);
    while (edges.hasNext()) {
        HugeEdge edge = (HugeEdge) edges.next();
        Id target = edge.id().otherVertexId();
        this.processOne(v, target, forward);
    }
}
Also used : 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

HugeEdge (com.baidu.hugegraph.structure.HugeEdge)54 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)29 Id (com.baidu.hugegraph.backend.id.Id)26 Edge (org.apache.tinkerpop.gremlin.structure.Edge)22 Test (org.junit.Test)20 HugeGraph (com.baidu.hugegraph.HugeGraph)17 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)12 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)11 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)9 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)9 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)8 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)8 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)6 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)6 Iterator (java.util.Iterator)6 List (java.util.List)6 Set (java.util.Set)6 HugeException (com.baidu.hugegraph.HugeException)5 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)5 HugeConfig (com.baidu.hugegraph.config.HugeConfig)5