Search in sources :

Example 21 with Id

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

the class NeighborRankTraverser method contributeNewLayer.

private Ranks contributeNewLayer(List<Adjacencies> adjacencies, Ranks lastLayerRanks, int capacity) {
    Ranks newLayerRanks = new Ranks(capacity);
    for (Adjacencies adjacenciesV : adjacencies) {
        Id source = adjacenciesV.source();
        long degree = adjacenciesV.degree();
        for (Node node : adjacenciesV.nodes()) {
            double rank = newLayerRanks.getOrDefault(node.id(), 0.0);
            rank += (lastLayerRanks.get(source) * this.alpha / degree);
            newLayerRanks.put(node.id(), rank);
        }
    }
    return newLayerRanks;
}
Also used : Id(com.baidu.hugegraph.backend.id.Id)

Example 22 with Id

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

the class NeighborRankTraverser method neighborRank.

public List<Map<Id, Double>> neighborRank(Id source, List<Step> steps) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    E.checkArgument(!steps.isEmpty(), "The steps can't be empty");
    MultivaluedMap<Id, Node> sources = newMultivalueMap();
    sources.add(source, new Node(source, null));
    boolean sameLayerTransfer = true;
    long access = 0;
    // Results: ranks of each layer
    List<Ranks> ranks = newList();
    ranks.add(Ranks.of(source, 1.0));
    for (Step step : steps) {
        Ranks lastLayerRanks = ranks.get(ranks.size() - 1);
        Map<Id, Double> sameLayerIncrRanks = newMap();
        List<Adjacencies> adjacencies = newList();
        MultivaluedMap<Id, Node> newVertices = newMultivalueMap();
        // Traversal vertices of previous level
        for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
            Id vertex = entry.getKey();
            Iterator<Edge> edges = this.edgesOfVertex(vertex, step.edgeStep);
            Adjacencies adjacenciesV = new Adjacencies(vertex);
            Set<Id> sameLayerNodesV = newIdSet();
            Map<Integer, Set<Id>> prevLayerNodesV = newMap();
            while (edges.hasNext()) {
                HugeEdge edge = (HugeEdge) edges.next();
                Id target = edge.id().otherVertexId();
                // Determine whether it belongs to the same layer
                if (this.belongToSameLayer(sources.keySet(), target, sameLayerNodesV)) {
                    continue;
                }
                /*
                     * Determine whether it belongs to the previous layers,
                     * if it belongs to, update the weight, but don't pass
                     * any more
                     */
                if (this.belongToPrevLayers(ranks, target, prevLayerNodesV)) {
                    continue;
                }
                for (Node n : entry.getValue()) {
                    // If have loop, skip target
                    if (n.contains(target)) {
                        continue;
                    }
                    Node newNode = new Node(target, n);
                    adjacenciesV.add(newNode);
                    // Add adjacent nodes to sources of next step
                    newVertices.add(target, newNode);
                    checkCapacity(this.capacity, ++access, "neighbor rank");
                }
            }
            long degree = sameLayerNodesV.size() + prevLayerNodesV.size() + adjacenciesV.nodes().size();
            if (degree == 0L) {
                continue;
            }
            adjacenciesV.degree(degree);
            adjacencies.add(adjacenciesV);
            double incr = lastLayerRanks.getOrDefault(vertex, 0.0) * this.alpha / degree;
            // Merge the increment of the same layer node
            this.mergeSameLayerIncrRanks(sameLayerNodesV, incr, sameLayerIncrRanks);
            // Adding contributions to the previous layers
            this.contributePrevLayers(ranks, incr, prevLayerNodesV);
        }
        Ranks newLayerRanks;
        if (sameLayerTransfer) {
            // First contribute to last layer, then pass to the new layer
            this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
            newLayerRanks = this.contributeNewLayer(adjacencies, lastLayerRanks, step.capacity);
        } else {
            // First pass to the new layer, then contribute to last layer
            newLayerRanks = this.contributeNewLayer(adjacencies, lastLayerRanks, step.capacity);
            this.contributeLastLayer(sameLayerIncrRanks, lastLayerRanks);
        }
        ranks.add(newLayerRanks);
        // Re-init sources
        sources = newVertices;
    }
    return this.topRanks(ranks, steps);
}
Also used : Set(java.util.Set) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) List(java.util.List) Id(com.baidu.hugegraph.backend.id.Id) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) Map(java.util.Map) OrderLimitMap(com.baidu.hugegraph.util.OrderLimitMap) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 23 with Id

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

the class NeighborRankTraverser method mergeSameLayerIncrRanks.

private void mergeSameLayerIncrRanks(Set<Id> sameLayerNodesV, double incr, Map<Id, Double> sameLayerIncrRanks) {
    for (Id node : sameLayerNodesV) {
        double oldRank = sameLayerIncrRanks.getOrDefault(node, 0.0);
        sameLayerIncrRanks.put(node, oldRank + incr);
    }
}
Also used : Id(com.baidu.hugegraph.backend.id.Id)

Example 24 with Id

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

the class IndexLabelBuilder method createWithTask.

/**
 * Create index label with async mode
 */
@Override
public SchemaElement.TaskWithSchema createWithTask() {
    HugeType type = HugeType.INDEX_LABEL;
    this.checkSchemaName(this.name);
    return this.lockCheckAndCreateSchema(type, this.name, name -> {
        IndexLabel indexLabel = this.indexLabelOrNull(name);
        if (indexLabel != null) {
            if (this.checkExist || !hasSameProperties(indexLabel)) {
                throw new ExistedException(type, name);
            }
            return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
        }
        this.checkSchemaIdIfRestoringMode(type, this.id);
        this.checkBaseType();
        this.checkIndexType();
        if (VertexLabel.OLAP_VL.name().equals(this.baseValue)) {
            return new SchemaElement.TaskWithSchema(this.build(), IdGenerator.ZERO);
        }
        SchemaLabel schemaLabel = this.loadBaseLabel();
        /*
             * If new index label is prefix of existed index label, or has
             * the same fields, fail to create new index label.
             */
        this.checkFields(schemaLabel.properties());
        this.checkRepeatIndex(schemaLabel);
        Userdata.check(this.userdata, Action.INSERT);
        // Async delete index label which is prefix of the new index label
        // TODO: use event to replace direct call
        Set<Id> removeTasks = this.removeSubIndex(schemaLabel);
        indexLabel = this.build();
        assert indexLabel.name().equals(name);
        /*
             * If not rebuild, just create index label and return.
             * The actual indexes may be rebuilt later as needed
             */
        if (!this.rebuild) {
            indexLabel.status(SchemaStatus.CREATED);
            this.graph().addIndexLabel(schemaLabel, indexLabel);
            return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
        }
        // Create index label (just schema)
        indexLabel.status(SchemaStatus.CREATING);
        this.graph().addIndexLabel(schemaLabel, indexLabel);
        try {
            // Async rebuild index
            Id rebuildTask = this.rebuildIndex(indexLabel, removeTasks);
            E.checkNotNull(rebuildTask, "rebuild-index task");
            return new SchemaElement.TaskWithSchema(indexLabel, rebuildTask);
        } catch (Throwable e) {
            this.updateSchemaStatus(indexLabel, SchemaStatus.INVALID);
            throw e;
        }
    });
}
Also used : ExistedException(com.baidu.hugegraph.exception.ExistedException) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 25 with Id

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

the class IndexLabelBuilder method removeSubIndex.

private Set<Id> removeSubIndex(SchemaLabel schemaLabel) {
    Set<Id> overrideIndexLabelIds = InsertionOrderUtil.newSet();
    for (Id id : schemaLabel.indexLabels()) {
        IndexLabel old = this.graph().indexLabel(id);
        if (!this.hasSubIndex(old)) {
            continue;
        }
        List<String> oldFields = this.graph().mapPkId2Name(old.indexFields());
        List<String> newFields = this.indexFields;
        /*
             * Remove the existed index label if:
             * 1. new unique index label is subset of existed unique index label
             * or
             * 2. existed index label is prefix of new created index label
             * (except for unique index)
             */
        if (this.indexType.isUnique() && oldFields.containsAll(newFields) || !this.indexType.isUnique() && CollectionUtil.prefixOf(oldFields, newFields)) {
            overrideIndexLabelIds.add(id);
        }
    }
    Set<Id> tasks = InsertionOrderUtil.newSet();
    for (Id id : overrideIndexLabelIds) {
        Id task = this.graph().removeIndexLabel(id);
        E.checkNotNull(task, "remove sub index label task");
        tasks.add(task);
    }
    return tasks;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id)

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