Search in sources :

Example 11 with Direction

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

the class EdgeSerializer method writeRelation.

public Entry writeRelation(InternalRelation relation, int position, StandardTitanTx tx) {
    Preconditions.checkArgument(position < relation.getLen());
    TitanType type = relation.getType();
    long typeid = type.getID();
    Direction dir = EdgeDirection.fromPosition(position);
    int dirID = getDirID(dir, relation.isProperty() ? RelationType.PROPERTY : RelationType.EDGE);
    DataOutput colOut = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY, true);
    IDHandler.writeEdgeType(colOut, typeid, dirID);
    InternalType definition = (InternalType) type;
    long[] sortKey = definition.getSortKey();
    int startPosition = colOut.getPosition();
    if (!type.isUnique(dir)) {
        writeInlineTypes(sortKey, relation, colOut, tx);
    }
    int endPosition = colOut.getPosition();
    DataOutput writer = colOut;
    long vertexIdDiff = 0;
    long relationIdDiff = relation.getID() - relation.getVertex(position).getID();
    if (relation.isEdge())
        vertexIdDiff = relation.getVertex((position + 1) % 2).getID() - relation.getVertex(position).getID();
    if (type.isUnique(dir)) {
        writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
        if (relation.isEdge())
            VariableLong.write(writer, vertexIdDiff);
        VariableLong.write(writer, relationIdDiff);
    } else {
        if (relation.isEdge())
            VariableLong.writeBackward(writer, vertexIdDiff);
        VariableLong.writeBackward(writer, relationIdDiff);
    }
    if (!type.isUnique(dir)) {
        writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
    }
    if (relation.isProperty()) {
        Preconditions.checkArgument(relation.isProperty());
        Object value = ((TitanProperty) relation).getValue();
        Preconditions.checkNotNull(value);
        TitanKey key = (TitanKey) type;
        assert key.getDataType().isInstance(value);
        if (hasGenericDataType(key)) {
            writer.writeClassAndObject(value);
        } else {
            writer.writeObjectNotNull(value);
        }
    }
    // Write signature & sort key if unique
    if (type.isUnique(dir)) {
        writeInlineTypes(sortKey, relation, writer, tx);
    }
    long[] signature = definition.getSignature();
    writeInlineTypes(signature, relation, writer, tx);
    // Write remaining properties
    LongSet writtenTypes = new LongOpenHashSet(sortKey.length + signature.length);
    if (sortKey.length > 0 || signature.length > 0) {
        for (long id : sortKey) writtenTypes.add(id);
        for (long id : signature) writtenTypes.add(id);
    }
    LongArrayList remainingTypes = new LongArrayList(8);
    for (TitanType t : relation.getPropertyKeysDirect()) {
        if (!writtenTypes.contains(t.getID())) {
            remainingTypes.add(t.getID());
        }
    }
    // Sort types before writing to ensure that value is always written the same way
    long[] remaining = remainingTypes.toArray();
    Arrays.sort(remaining);
    for (long tid : remaining) {
        TitanType t = tx.getExistingType(tid);
        writeInline(writer, t, relation.getProperty(t), true);
    }
    StaticBuffer column = ((InternalType) type).getSortOrder() == Order.DESC ? colOut.getStaticBufferFlipBytes(startPosition, endPosition) : colOut.getStaticBuffer();
    return new StaticBufferEntry(column, writer.getStaticBuffer());
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) LongArrayList(com.carrotsearch.hppc.LongArrayList) LongSet(com.carrotsearch.hppc.LongSet) EdgeDirection(com.thinkaurelius.titan.graphdb.relations.EdgeDirection) Direction(com.tinkerpop.blueprints.Direction) InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) LongOpenHashSet(com.carrotsearch.hppc.LongOpenHashSet) StaticBufferEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry)

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