Search in sources :

Example 1 with Direction

use of com.tinkerpop.blueprints.Direction in project orientdb by orientechnologies.

the class OrientGraphNoTx method removeEdgesInternal.

@Override
void removeEdgesInternal(final OrientVertex vertex, final ODocument iVertex, final OIdentifiable iVertexToRemove, final boolean iAlsoInverse, final boolean useVertexFieldsForEdgeLabels, final boolean autoScaleEdgeType) {
    Exception lastException = null;
    boolean forceReload = false;
    final int maxRetries = getMaxRetries();
    for (int retry = 0; retry < maxRetries; ++retry) {
        try {
            for (String fieldName : iVertex.fieldNames()) {
                final OPair<Direction, String> connection = vertex.getConnection(Direction.BOTH, fieldName);
                if (connection == null)
                    // SKIP THIS FIELD
                    continue;
                removeEdges(this, iVertex, fieldName, iVertexToRemove, iAlsoInverse, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
            }
            // OK
            return;
        } catch (Exception e) {
            forceReload = true;
            lastException = e;
        }
    }
    if (lastException instanceof RuntimeException)
        // CANNOT REVERT CHANGES, RETRY
        throw (RuntimeException) lastException;
    throw OException.wrapException(new OrientGraphModificationException("Error on removing edges after vertex (" + iVertex.getIdentity() + ") delete in non tx environment"), lastException);
}
Also used : Direction(com.tinkerpop.blueprints.Direction) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OException(com.orientechnologies.common.exception.OException)

Example 2 with Direction

use of com.tinkerpop.blueprints.Direction in project pentaho-metaverse by pentaho.

the class BlueprintsGraphMetaverseReader method findLink.

@Override
public IMetaverseLink findLink(String leftNodeID, String linkType, String rightNodeID, Direction direction) {
    Vertex vertex = getGraph().getVertex(leftNodeID);
    if (vertex == null) {
        return null;
    }
    Iterable<Edge> edges = linkType == null ? vertex.getEdges(direction) : vertex.getEdges(direction, linkType);
    IMetaverseLink link = new MetaverseLink();
    IMetaverseNode node1 = new MetaverseNode(vertex);
    Direction opDirection = direction == Direction.IN ? Direction.OUT : Direction.IN;
    Vertex vertex2 = null;
    if (rightNodeID != null) {
        Iterator<Edge> it = edges.iterator();
        while (it.hasNext()) {
            Edge edge = it.next();
            if (rightNodeID.equals((String) edge.getVertex(opDirection).getId())) {
                vertex2 = edge.getVertex(opDirection);
                IMetaverseNode node2 = new MetaverseNode(vertex2);
                String label = edge.getLabel();
                link.setLabel(label);
                String localized = Messages.getString(MetaverseUtil.MESSAGE_PREFIX_LINKTYPE + label);
                if (!localized.startsWith("!")) {
                    link.setProperty(DictionaryConst.PROPERTY_TYPE_LOCALIZED, localized);
                }
                if (direction == Direction.OUT) {
                    link.setFromNode(node1);
                    link.setToNode(node2);
                } else {
                    link.setFromNode(node2);
                    link.setToNode(node1);
                }
                return link;
            }
        }
    }
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) IMetaverseNode(org.pentaho.metaverse.api.IMetaverseNode) MetaverseLink(org.pentaho.dictionary.MetaverseLink) IMetaverseLink(org.pentaho.metaverse.api.IMetaverseLink) IMetaverseLink(org.pentaho.metaverse.api.IMetaverseLink) MetaverseNode(org.pentaho.metaverse.impl.MetaverseNode) IMetaverseNode(org.pentaho.metaverse.api.IMetaverseNode) Edge(com.tinkerpop.blueprints.Edge) Direction(com.tinkerpop.blueprints.Direction)

Example 3 with Direction

use of com.tinkerpop.blueprints.Direction in project atlas by apache.

the class Titan0GraphManagement method createEdgeIndex.

@Override
public void createEdgeIndex(String label, String indexName, AtlasEdgeDirection edgeDirection, List<AtlasPropertyKey> propertyKeys) {
    EdgeLabel edgeLabel = management.getEdgeLabel(label);
    if (edgeLabel == null) {
        edgeLabel = management.makeEdgeLabel(label).make();
    }
    Direction direction = TitanObjectFactory.createDirection(edgeDirection);
    PropertyKey[] keys = TitanObjectFactory.createPropertyKeys(propertyKeys);
    management.buildEdgeIndex(edgeLabel, indexName, direction, keys);
}
Also used : EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) AtlasEdgeLabel(org.apache.atlas.repository.graphdb.AtlasEdgeLabel) AtlasEdgeDirection(org.apache.atlas.repository.graphdb.AtlasEdgeDirection) Direction(com.tinkerpop.blueprints.Direction) AtlasPropertyKey(org.apache.atlas.repository.graphdb.AtlasPropertyKey) PropertyKey(com.thinkaurelius.titan.core.PropertyKey)

Example 4 with Direction

use of com.tinkerpop.blueprints.Direction in project titan by thinkaurelius.

the class StandardTitanGraph method commit.

public void commit(final Collection<InternalRelation> addedRelations, final Collection<InternalRelation> deletedRelations, final StandardTitanTx tx) {
    // Setup
    log.debug("Saving transaction. Added {}, removed {}", addedRelations.size(), deletedRelations.size());
    final BackendTransaction mutator = tx.getTxHandle();
    final boolean acquireLocks = tx.getConfiguration().hasAcquireLocks();
    // 1. Assign TitanVertex IDs
    if (!tx.getConfiguration().hasAssignIDsImmediately())
        idAssigner.assignIDs(addedRelations);
    Callable<List<StaticBuffer>> persist = new Callable<List<StaticBuffer>>() {

        @Override
        public List<StaticBuffer> call() throws Exception {
            // 2. Collect deleted edges
            ListMultimap<InternalVertex, InternalRelation> mutations = ArrayListMultimap.create();
            if (deletedRelations != null && !deletedRelations.isEmpty()) {
                for (InternalRelation del : deletedRelations) {
                    Preconditions.checkArgument(del.isRemoved());
                    for (int pos = 0; pos < del.getLen(); pos++) {
                        InternalVertex vertex = del.getVertex(pos);
                        if (pos == 0 || !del.isLoop())
                            mutations.put(vertex, del);
                        Direction dir = EdgeDirection.fromPosition(pos);
                        if (acquireLocks && del.getType().isUnique(dir) && ((InternalType) del.getType()).uniqueLock(dir)) {
                            Entry entry = edgeSerializer.writeRelation(del, pos, tx);
                            mutator.acquireEdgeLock(IDHandler.getKey(vertex.getID()), entry.getColumn(), entry.getValue());
                        }
                    }
                    // Update Indexes
                    if (del.isProperty()) {
                        if (acquireLocks)
                            indexSerializer.lockKeyedProperty((TitanProperty) del, mutator);
                    }
                }
            }
            ListMultimap<InternalType, InternalRelation> otherEdgeTypes = ArrayListMultimap.create();
            // 3. Sort Added Edges
            for (InternalRelation relation : addedRelations) {
                Preconditions.checkArgument(relation.isNew());
                TitanType type = relation.getType();
                // Give special treatment to edge type definitions
                if (SystemTypeManager.prepersistedSystemTypes.contains(type)) {
                    InternalType itype = (InternalType) relation.getVertex(0);
                    otherEdgeTypes.put(itype, relation);
                } else {
                    // STANDARD TitanRelation
                    for (int pos = 0; pos < relation.getLen(); pos++) {
                        InternalVertex vertex = relation.getVertex(pos);
                        if (pos == 0 || !relation.isLoop())
                            mutations.put(vertex, relation);
                        Direction dir = EdgeDirection.fromPosition(pos);
                        if (acquireLocks && relation.getType().isUnique(dir) && !vertex.isNew() && ((InternalType) relation.getType()).uniqueLock(dir)) {
                            Entry entry = edgeSerializer.writeRelation(relation, pos, tx);
                            mutator.acquireEdgeLock(IDHandler.getKey(vertex.getID()), entry.getColumn(), null);
                        }
                    }
                }
                // Update Indexes
                if (relation.isProperty()) {
                    if (acquireLocks)
                        indexSerializer.lockKeyedProperty((TitanProperty) relation, mutator);
                }
            }
            // 3. Persist
            List<StaticBuffer> mutatedVertexKeys = new ArrayList<StaticBuffer>();
            if (!otherEdgeTypes.isEmpty()) {
                mutatedVertexKeys.addAll(persist(otherEdgeTypes, tx));
                mutator.flush();
                // Register new keys with indexprovider
                for (InternalType itype : otherEdgeTypes.keySet()) {
                    if (itype.isPropertyKey() && itype.isNew())
                        indexSerializer.newPropertyKey((TitanKey) itype, mutator);
                }
            }
            if (!mutations.isEmpty())
                mutatedVertexKeys.addAll(persist(mutations, tx));
            mutator.commit();
            return mutatedVertexKeys;
        }

        @Override
        public String toString() {
            return "PersistingTransaction";
        }
    };
    List<StaticBuffer> mutatedVertexKeys = BackendOperation.execute(persist, maxWriteRetryAttempts, retryStorageWaitTime);
    for (StaticBuffer vertexKey : mutatedVertexKeys) edgeStoreCache.invalidate(vertexKey);
}
Also used : LongArrayList(com.carrotsearch.hppc.LongArrayList) ArrayList(java.util.ArrayList) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) EdgeDirection(com.thinkaurelius.titan.graphdb.relations.EdgeDirection) Direction(com.tinkerpop.blueprints.Direction) Callable(java.util.concurrent.Callable) InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) LongArrayList(com.carrotsearch.hppc.LongArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction)

Example 5 with Direction

use of com.tinkerpop.blueprints.Direction in project titan by thinkaurelius.

the class EdgeSerializer method parseRelation.

private RelationCache parseRelation(long vertexid, Entry data, boolean parseHeaderOnly, StandardTitanTx tx) {
    assert vertexid > 0;
    ReadBuffer column = data.getReadColumn();
    ReadBuffer value = data.getReadValue();
    LongObjectOpenHashMap properties = parseHeaderOnly ? null : new LongObjectOpenHashMap(4);
    long[] typeAndDir = IDHandler.readEdgeType(column);
    int dirID = (int) typeAndDir[1];
    long typeId = typeAndDir[0];
    Direction dir;
    RelationType rtype;
    switch(dirID) {
        case PROPERTY_DIR:
            dir = Direction.OUT;
            rtype = RelationType.PROPERTY;
            break;
        case EDGE_OUT_DIR:
            dir = Direction.OUT;
            rtype = RelationType.EDGE;
            break;
        case EDGE_IN_DIR:
            dir = Direction.IN;
            rtype = RelationType.EDGE;
            break;
        default:
            throw new IllegalArgumentException("Invalid dirID read from disk: " + dirID);
    }
    TitanType titanType = tx.getExistingType(typeId);
    InternalType def = (InternalType) titanType;
    long[] keysig = def.getSortKey();
    if (!parseHeaderOnly && !titanType.isUnique(dir)) {
        ReadBuffer sortKeyReader = def.getSortOrder() == Order.DESC ? column.invert() : column;
        readInlineTypes(keysig, properties, sortKeyReader, tx);
    }
    long relationIdDiff, vertexIdDiff = 0;
    if (titanType.isUnique(dir)) {
        if (rtype == RelationType.EDGE)
            vertexIdDiff = VariableLong.read(value);
        relationIdDiff = VariableLong.read(value);
    } else {
        // Move position to end to read backwards
        column.movePosition(column.length() - column.getPosition() - 1);
        relationIdDiff = VariableLong.readBackward(column);
        if (rtype == RelationType.EDGE)
            vertexIdDiff = VariableLong.readBackward(column);
    }
    assert relationIdDiff + vertexid > 0;
    long relationId = relationIdDiff + vertexid;
    Object other;
    switch(rtype) {
        case EDGE:
            Preconditions.checkArgument(titanType.isEdgeLabel());
            other = vertexid + vertexIdDiff;
            break;
        case PROPERTY:
            Preconditions.checkArgument(titanType.isPropertyKey());
            TitanKey key = ((TitanKey) titanType);
            other = hasGenericDataType(key) ? serializer.readClassAndObject(value) : serializer.readObjectNotNull(value, key.getDataType());
            break;
        default:
            throw new AssertionError();
    }
    assert other != null;
    if (!parseHeaderOnly) {
        // value signature & sort key if unique
        if (titanType.isUnique(dir)) {
            readInlineTypes(keysig, properties, value, tx);
        }
        readInlineTypes(def.getSignature(), properties, value, tx);
        // Third: read rest
        while (value.hasRemaining()) {
            TitanType type = tx.getExistingType(IDHandler.readInlineEdgeType(value));
            Object pvalue = readInline(value, type);
            assert pvalue != null;
            properties.put(type.getID(), pvalue);
        }
    }
    return new RelationCache(dir, typeId, relationId, other, properties);
}
Also used : RelationCache(com.thinkaurelius.titan.graphdb.relations.RelationCache) LongObjectOpenHashMap(com.carrotsearch.hppc.LongObjectOpenHashMap) EdgeDirection(com.thinkaurelius.titan.graphdb.relations.EdgeDirection) Direction(com.tinkerpop.blueprints.Direction) InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) RelationType(com.thinkaurelius.titan.graphdb.internal.RelationType)

Aggregations

Direction (com.tinkerpop.blueprints.Direction)11 InternalType (com.thinkaurelius.titan.graphdb.internal.InternalType)5 EdgeDirection (com.thinkaurelius.titan.graphdb.relations.EdgeDirection)3 Edge (com.tinkerpop.blueprints.Edge)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)2 Vertex (com.tinkerpop.blueprints.Vertex)2 LongObjectOpenHashMap (com.carrotsearch.hppc.LongObjectOpenHashMap)1 LongOpenHashSet (com.carrotsearch.hppc.LongOpenHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OMetadata (com.orientechnologies.orient.core.metadata.OMetadata)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)1