Search in sources :

Example 31 with HugeVertex

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

the class EdgeAPI method newVertex.

private static Vertex newVertex(HugeGraph g, Object id, String label) {
    VertexLabel vl = vertexLabel(g, label, "Invalid vertex label '%s'");
    Id idValue = HugeVertex.getIdValue(id);
    return new HugeVertex(g, idValue, vl);
}
Also used : 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)

Example 32 with HugeVertex

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

the class StandardHugeGraph method removeVertex.

@Override
public void removeVertex(String label, Object id) {
    if (label != null) {
        VertexLabel vl = this.vertexLabel(label);
        // It's OK even if exist adjacent edges `vl.existsLinkLabel()`
        if (!vl.existsIndexLabel()) {
            // Improve perf by removeVertex(id)
            Id idValue = HugeVertex.getIdValue(id);
            HugeVertex vertex = new HugeVertex(this, idValue, vl);
            this.removeVertex(vertex);
            return;
        }
    }
    this.vertex(id).remove();
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 33 with HugeVertex

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

the class EntityManager method delete.

public T delete(Id id) {
    T entity = null;
    Iterator<Vertex> vertices = this.tx().queryVertices(id);
    if (vertices.hasNext()) {
        HugeVertex vertex = (HugeVertex) vertices.next();
        entity = this.deser.apply(vertex);
        this.tx().removeVertex(vertex);
        this.commitOrRollback();
        assert !vertices.hasNext();
    }
    return entity;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 34 with HugeVertex

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

the class RelationshipManager method save.

private Id save(T relationship, boolean expectExists) {
    if (!this.graph().existsEdgeLabel(relationship.label())) {
        throw new HugeException("Schema is missing for %s '%s'", relationship.label(), relationship.source());
    }
    HugeVertex source = this.newVertex(relationship.source(), relationship.sourceLabel());
    HugeVertex target = this.newVertex(relationship.target(), relationship.targetLabel());
    HugeEdge edge = source.constructEdge(relationship.label(), target, relationship.asArray());
    E.checkArgument(this.exists(edge.id()) == expectExists, "Can't save %s '%s' that %s exists", this.unhideLabel(), edge.id(), expectExists ? "not" : "already");
    this.tx().addEdge(edge);
    this.commitOrRollback();
    return edge.id();
}
Also used : HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeException(com.baidu.hugegraph.HugeException) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 35 with HugeVertex

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

the class CachedGraphTransaction method commitMutation2Backend.

@Override
@Watched(prefix = "graphcache")
protected final void commitMutation2Backend(BackendMutation... mutations) {
    // Collect changes before commit
    Collection<HugeVertex> updates = this.verticesInTxUpdated();
    Collection<HugeVertex> deletions = this.verticesInTxRemoved();
    Id[] vertexIds = new Id[updates.size() + deletions.size()];
    int vertexOffset = 0;
    int edgesInTxSize = this.edgesInTxSize();
    try {
        super.commitMutation2Backend(mutations);
        // Update vertex cache
        if (this.enableCacheVertex()) {
            for (HugeVertex vertex : updates) {
                vertexIds[vertexOffset++] = vertex.id();
                if (needCacheVertex(vertex)) {
                    // Update cache
                    this.verticesCache.updateIfPresent(vertex.id(), vertex);
                } else {
                    // Skip large vertex
                    this.verticesCache.invalidate(vertex.id());
                }
            }
        }
    } finally {
        // Update removed vertex in cache whatever success or fail
        if (this.enableCacheVertex()) {
            for (HugeVertex vertex : deletions) {
                vertexIds[vertexOffset++] = vertex.id();
                this.verticesCache.invalidate(vertex.id());
            }
            if (vertexOffset > 0) {
                this.notifyChanges(Cache.ACTION_INVALIDED, HugeType.VERTEX, vertexIds);
            }
        }
        /*
             * Update edge cache if any vertex or edge changed
             * For vertex change, the edges linked with should also be updated
             * Before we find a more precise strategy, just clear all the edge cache now
             */
        boolean invalidEdgesCache = (edgesInTxSize + updates.size() + deletions.size()) > 0;
        if (invalidEdgesCache && this.enableCacheEdge()) {
            // TODO: Use a more precise strategy to update the edge cache
            this.edgesCache.clear();
            this.notifyChanges(Cache.ACTION_CLEARED, HugeType.EDGE, null);
        }
    }
}
Also used : QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

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