Search in sources :

Example 1 with StandardEdge

use of org.janusgraph.graphdb.relations.StandardEdge in project janusgraph by JanusGraph.

the class StandardJanusGraphTx method addEdge.

public JanusGraphEdge addEdge(JanusGraphVertex outVertex, JanusGraphVertex inVertex, EdgeLabel label) {
    verifyWriteAccess(outVertex, inVertex);
    outVertex = ((InternalVertex) outVertex).it();
    inVertex = ((InternalVertex) inVertex).it();
    Preconditions.checkNotNull(label);
    Multiplicity multiplicity = label.multiplicity();
    TransactionLock uniqueLock = getUniquenessLock(outVertex, (InternalRelationType) label, inVertex);
    uniqueLock.lock(LOCK_TIMEOUT);
    try {
        // Check uniqueness
        if (config.hasVerifyUniqueness()) {
            if (multiplicity == Multiplicity.SIMPLE) {
                if (!Iterables.isEmpty(query(outVertex).type(label).direction(Direction.OUT).adjacent(inVertex).edges()))
                    throw new SchemaViolationException("An edge with the given label already exists between the pair of vertices and the label [%s] is simple", label.name());
            }
            if (multiplicity.isUnique(Direction.OUT)) {
                if (!Iterables.isEmpty(query(outVertex).type(label).direction(Direction.OUT).edges()))
                    throw new SchemaViolationException("An edge with the given label already exists on the out-vertex and the label [%s] is out-unique", label.name());
            }
            if (multiplicity.isUnique(Direction.IN)) {
                if (!Iterables.isEmpty(query(inVertex).type(label).direction(Direction.IN).edges()))
                    throw new SchemaViolationException("An edge with the given label already exists on the in-vertex and the label [%s] is in-unique", label.name());
            }
        }
        StandardEdge edge = new StandardEdge(IDManager.getTemporaryRelationID(temporaryIds.nextID()), label, (InternalVertex) outVertex, (InternalVertex) inVertex, ElementLifeCycle.New);
        if (config.hasAssignIDsImmediately())
            graph.assignID(edge);
        connectRelation(edge);
        return edge;
    } finally {
        uniqueLock.unlock();
    }
}
Also used : StandardEdge(org.janusgraph.graphdb.relations.StandardEdge)

Example 2 with StandardEdge

use of org.janusgraph.graphdb.relations.StandardEdge in project janusgraph by JanusGraph.

the class StandardJanusGraphTx method addEdge.

public JanusGraphEdge addEdge(Long id, JanusGraphVertex outVertex, JanusGraphVertex inVertex, EdgeLabel label) {
    verifyWriteAccess(outVertex, inVertex);
    outVertex = ((InternalVertex) outVertex).it();
    inVertex = ((InternalVertex) inVertex).it();
    Preconditions.checkNotNull(label);
    checkConnectionConstraintOrCreateConnectionConstraint(outVertex, inVertex, label);
    Multiplicity multiplicity = label.multiplicity();
    TransactionLock uniqueLock = getUniquenessLock(outVertex, (InternalRelationType) label, inVertex);
    uniqueLock.lock(LOCK_TIMEOUT);
    try {
        // Check uniqueness
        if (config.hasVerifyUniqueness()) {
            if (multiplicity == Multiplicity.SIMPLE) {
                if (!Iterables.isEmpty(query(outVertex).type(label).direction(Direction.OUT).adjacent(inVertex).edges()))
                    throw new SchemaViolationException("An edge with the given label already exists between the pair of vertices and the label [%s] is simple", label.name());
            }
            if (multiplicity.isUnique(Direction.OUT)) {
                if (!Iterables.isEmpty(query(outVertex).type(label).direction(Direction.OUT).edges()))
                    throw new SchemaViolationException("An edge with the given label already exists on the out-vertex and the label [%s] is out-unique", label.name());
            }
            if (multiplicity.isUnique(Direction.IN)) {
                if (!Iterables.isEmpty(query(inVertex).type(label).direction(Direction.IN).edges()))
                    throw new SchemaViolationException("An edge with the given label already exists on the in-vertex and the label [%s] is in-unique", label.name());
            }
        }
        long edgeId = id == null ? IDManager.getTemporaryRelationID(temporaryIds.nextID()) : id;
        StandardEdge edge = new StandardEdge(edgeId, label, (InternalVertex) outVertex, (InternalVertex) inVertex, ElementLifeCycle.New);
        if (config.hasAssignIDsImmediately() && id == null)
            graph.assignID(edge);
        connectRelation(edge);
        return edge;
    } finally {
        uniqueLock.unlock();
    }
}
Also used : Multiplicity(org.janusgraph.core.Multiplicity) SchemaViolationException(org.janusgraph.core.SchemaViolationException) StandardEdge(org.janusgraph.graphdb.relations.StandardEdge) TransactionLock(org.janusgraph.graphdb.transaction.lock.TransactionLock) ReentrantTransactionLock(org.janusgraph.graphdb.transaction.lock.ReentrantTransactionLock)

Example 3 with StandardEdge

use of org.janusgraph.graphdb.relations.StandardEdge in project janusgraph by JanusGraph.

the class ModificationDeserializer method parseRelation.

public static InternalRelation parseRelation(TransactionLogHeader.Modification modification, StandardJanusGraphTx tx) {
    Change state = modification.state;
    assert state.isProper();
    long outVertexId = modification.outVertexId;
    Entry relEntry = modification.relationEntry;
    InternalVertex outVertex = tx.getInternalVertex(outVertexId);
    // Special relation parsing, compare to {@link RelationConstructor}
    RelationCache relCache = tx.getEdgeSerializer().readRelation(relEntry, false, tx);
    assert relCache.direction == Direction.OUT;
    InternalRelationType type = (InternalRelationType) tx.getExistingRelationType(relCache.typeId);
    assert type.getBaseType() == null;
    InternalRelation rel;
    if (type.isPropertyKey()) {
        if (state == Change.REMOVED) {
            rel = new StandardVertexProperty(relCache.relationId, (PropertyKey) type, outVertex, relCache.getValue(), ElementLifeCycle.Removed);
        } else {
            rel = new CacheVertexProperty(relCache.relationId, (PropertyKey) type, outVertex, relCache.getValue(), relEntry);
        }
    } else {
        assert type.isEdgeLabel();
        InternalVertex otherVertex = tx.getInternalVertex(relCache.getOtherVertexId());
        if (state == Change.REMOVED) {
            rel = new StandardEdge(relCache.relationId, (EdgeLabel) type, outVertex, otherVertex, ElementLifeCycle.Removed);
        } else {
            rel = new CacheEdge(relCache.relationId, (EdgeLabel) type, outVertex, otherVertex, relEntry);
        }
    }
    if (state == Change.REMOVED && relCache.hasProperties()) {
        // copy over properties
        for (LongObjectCursor<Object> entry : relCache) {
            rel.setPropertyDirect(tx.getExistingPropertyKey(entry.key), entry.value);
        }
    }
    return rel;
}
Also used : RelationCache(org.janusgraph.graphdb.relations.RelationCache) EdgeLabel(org.janusgraph.core.EdgeLabel) StandardVertexProperty(org.janusgraph.graphdb.relations.StandardVertexProperty) Change(org.janusgraph.core.log.Change) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) StandardEdge(org.janusgraph.graphdb.relations.StandardEdge) Entry(org.janusgraph.diskstorage.Entry) CacheVertexProperty(org.janusgraph.graphdb.relations.CacheVertexProperty) CacheEdge(org.janusgraph.graphdb.relations.CacheEdge) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) PropertyKey(org.janusgraph.core.PropertyKey)

Aggregations

StandardEdge (org.janusgraph.graphdb.relations.StandardEdge)3 EdgeLabel (org.janusgraph.core.EdgeLabel)1 Multiplicity (org.janusgraph.core.Multiplicity)1 PropertyKey (org.janusgraph.core.PropertyKey)1 SchemaViolationException (org.janusgraph.core.SchemaViolationException)1 Change (org.janusgraph.core.log.Change)1 Entry (org.janusgraph.diskstorage.Entry)1 InternalRelation (org.janusgraph.graphdb.internal.InternalRelation)1 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)1 InternalVertex (org.janusgraph.graphdb.internal.InternalVertex)1 CacheEdge (org.janusgraph.graphdb.relations.CacheEdge)1 CacheVertexProperty (org.janusgraph.graphdb.relations.CacheVertexProperty)1 RelationCache (org.janusgraph.graphdb.relations.RelationCache)1 StandardVertexProperty (org.janusgraph.graphdb.relations.StandardVertexProperty)1 ReentrantTransactionLock (org.janusgraph.graphdb.transaction.lock.ReentrantTransactionLock)1 TransactionLock (org.janusgraph.graphdb.transaction.lock.TransactionLock)1