Search in sources :

Example 41 with HugeVertex

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

the class GraphTransaction method constructVertex.

@Watched(prefix = "graph")
public HugeVertex constructVertex(boolean verifyVL, Object... keyValues) {
    HugeElement.ElementKeys elemKeys = HugeElement.classifyKeys(keyValues);
    if (possibleOlapVertex(elemKeys)) {
        Id id = HugeVertex.getIdValue(elemKeys.id());
        HugeVertex vertex = HugeVertex.create(this, id, VertexLabel.OLAP_VL);
        ElementHelper.attachProperties(vertex, keyValues);
        Iterator<HugeProperty<?>> iterator = vertex.getProperties().iterator();
        assert iterator.hasNext();
        if (iterator.next().propertyKey().olap()) {
            return vertex;
        }
    }
    VertexLabel vertexLabel = this.checkVertexLabel(elemKeys.label(), verifyVL);
    Id id = HugeVertex.getIdValue(elemKeys.id());
    List<Id> keys = this.graph().mapPkName2Id(elemKeys.keys());
    // Check whether id match with id strategy
    this.checkId(id, keys, vertexLabel);
    // Create HugeVertex
    HugeVertex vertex = HugeVertex.create(this, null, vertexLabel);
    // Set properties
    ElementHelper.attachProperties(vertex, keyValues);
    // Assign vertex id
    if (this.params().mode().maintaining() && vertexLabel.idStrategy() == IdStrategy.AUTOMATIC) {
        // Resume id for AUTOMATIC id strategy in restoring mode
        vertex.assignId(id, true);
    } else {
        vertex.assignId(id);
    }
    return vertex;
}
Also used : HugeProperty(com.baidu.hugegraph.structure.HugeProperty) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeElement(com.baidu.hugegraph.structure.HugeElement) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 42 with HugeVertex

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

the class GraphTransaction method removeVertexProperty.

@Watched(prefix = "graph")
public <V> void removeVertexProperty(HugeVertexProperty<V> prop) {
    HugeVertex vertex = prop.element();
    PropertyKey propKey = prop.propertyKey();
    E.checkState(vertex != null, "No owner for removing property '%s'", prop.key());
    // Maybe have ever been removed (compatible with tinkerpop)
    if (!vertex.hasProperty(propKey.id())) {
        // PropertyTest shouldAllowRemovalFromVertexWhenAlreadyRemoved()
        return;
    }
    // Check is removing primary key
    List<Id> primaryKeyIds = vertex.schemaLabel().primaryKeys();
    E.checkArgument(!primaryKeyIds.contains(propKey.id()), "Can't remove primary key '%s'", prop.key());
    // Remove property in memory for new created vertex
    if (vertex.fresh()) {
        // The owner will do property update
        vertex.removeProperty(propKey.id());
        return;
    }
    // Check is updating property of added/removed vertex
    E.checkArgument(!this.addedVertices.containsKey(vertex.id()) || this.updatedVertices.containsKey(vertex.id()), "Can't remove property '%s' for adding-state vertex", prop.key());
    E.checkArgument(!this.removedVertices.containsKey(vertex.id()), "Can't remove property '%s' for removing-state vertex", prop.key());
    // Do property update
    this.lockForUpdateProperty(vertex.schemaLabel(), prop, () -> {
        // Update old vertex to remove index (with the property)
        this.indexTx.updateVertexIndex(vertex, true);
        // Update(remove) vertex property
        HugeProperty<?> removed = vertex.removeProperty(propKey.id());
        this.propertyUpdated(vertex, null, removed);
    });
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 43 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex 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 44 with HugeVertex

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

the class ServerInfoManager method save.

private Id save(HugeServerInfo serverInfo) {
    return this.call(() -> {
        // Construct vertex from server info
        HugeServerInfo.Schema schema = HugeServerInfo.schema(this.graph);
        if (!schema.existVertexLabel(HugeServerInfo.P.SERVER)) {
            throw new HugeException("Schema is missing for %s '%s'", HugeServerInfo.P.SERVER, serverInfo);
        }
        HugeVertex vertex = this.tx().constructVertex(false, serverInfo.asArray());
        // Add or update server info in backend store
        vertex = this.tx().addVertex(vertex);
        return vertex.id();
    });
}
Also used : HugeException(com.baidu.hugegraph.HugeException) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 45 with HugeVertex

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

the class ServerInfoManager method save.

private int save(Collection<HugeServerInfo> serverInfos) {
    return this.call(() -> {
        if (serverInfos.isEmpty()) {
            return serverInfos.size();
        }
        HugeServerInfo.Schema schema = HugeServerInfo.schema(this.graph);
        if (!schema.existVertexLabel(HugeServerInfo.P.SERVER)) {
            throw new HugeException("Schema is missing for %s", HugeServerInfo.P.SERVER);
        }
        // Save server info in batch
        GraphTransaction tx = this.tx();
        int updated = 0;
        for (HugeServerInfo server : serverInfos) {
            if (!server.updated()) {
                continue;
            }
            HugeVertex vertex = tx.constructVertex(false, server.asArray());
            tx.addVertex(vertex);
            updated++;
        }
        // NOTE: actually it is auto-commit, to be improved
        tx.commitOrRollback();
        return updated;
    });
}
Also used : GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeException(com.baidu.hugegraph.HugeException) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Aggregations

HugeVertex (com.baidu.hugegraph.structure.HugeVertex)58 Id (com.baidu.hugegraph.backend.id.Id)27 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)26 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)18 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)16 Test (org.junit.Test)16 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)11 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)10 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)9 HugeGraph (com.baidu.hugegraph.HugeGraph)8 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)8 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)8 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)7 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)7 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)6 HugeException (com.baidu.hugegraph.HugeException)5 HugeConfig (com.baidu.hugegraph.config.HugeConfig)5 HugeProperty (com.baidu.hugegraph.structure.HugeProperty)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3