Search in sources :

Example 1 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class EdgeSerializer method writeRelation.

public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
    assert type == relation.getType() || type.getBaseType().equals(relation.getType());
    Direction dir = EdgeDirection.fromPosition(position);
    Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
    long typeid = type.longId();
    DirectionID dirID = getDirID(dir, relation.isProperty() ? RelationCategory.PROPERTY : RelationCategory.EDGE);
    DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY);
    int valuePosition;
    IDHandler.writeRelationType(out, typeid, dirID, type.isInvisibleType());
    Multiplicity multiplicity = type.multiplicity();
    long[] sortKey = type.getSortKey();
    assert !multiplicity.isConstrained() || sortKey.length == 0 : type.name();
    int keyStartPos = out.getPosition();
    if (!multiplicity.isConstrained()) {
        writeInlineTypes(sortKey, relation, out, tx, InlineType.KEY);
    }
    int keyEndPos = out.getPosition();
    long relationId = relation.longId();
    //How multiplicity is handled for edges and properties is slightly different
    if (relation.isEdge()) {
        long otherVertexId = relation.getVertex((position + 1) % 2).longId();
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                valuePosition = out.getPosition();
                VariableLong.writePositive(out, otherVertexId);
            } else {
                VariableLong.writePositiveBackward(out, otherVertexId);
                valuePosition = out.getPosition();
            }
            VariableLong.writePositive(out, relationId);
        } else {
            VariableLong.writePositiveBackward(out, otherVertexId);
            VariableLong.writePositiveBackward(out, relationId);
            valuePosition = out.getPosition();
        }
    } else {
        assert relation.isProperty();
        Preconditions.checkArgument(relation.isProperty());
        Object value = ((TitanVertexProperty) relation).value();
        Preconditions.checkNotNull(value);
        PropertyKey key = (PropertyKey) type;
        assert key.dataType().isInstance(value);
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                //Cardinality=SINGLE
                valuePosition = out.getPosition();
                writePropertyValue(out, key, value);
            } else {
                //Cardinality=SET
                writePropertyValue(out, key, value);
                valuePosition = out.getPosition();
            }
            VariableLong.writePositive(out, relationId);
        } else {
            assert multiplicity.getCardinality() == Cardinality.LIST;
            VariableLong.writePositiveBackward(out, relationId);
            valuePosition = out.getPosition();
            writePropertyValue(out, key, value);
        }
    }
    //Write signature
    long[] signature = type.getSignature();
    writeInlineTypes(signature, relation, out, tx, InlineType.SIGNATURE);
    //Write remaining properties
    LongSet writtenTypes = new LongHashSet(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 (PropertyKey t : relation.getPropertyKeysDirect()) {
        if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) {
            remainingTypes.add(t.longId());
        }
    }
    //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) {
        PropertyKey t = tx.getExistingPropertyKey(tid);
        writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL);
    }
    assert valuePosition > 0;
    StaticArrayEntry entry = new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
    return entry;
}
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(org.apache.tinkerpop.gremlin.structure.Direction) StaticArrayEntry(com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry) LongHashSet(com.carrotsearch.hppc.LongHashSet) ImplicitKey(com.thinkaurelius.titan.graphdb.types.system.ImplicitKey) DirectionID(com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.DirectionID)

Example 2 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class KCVSConfiguration method object2StaticBuffer.

private <O> StaticBuffer object2StaticBuffer(final O value) {
    if (value == null)
        throw Graph.Variables.Exceptions.variableValueCanNotBeNull();
    if (!serializer.validDataType(value.getClass()))
        throw Graph.Variables.Exceptions.dataTypeOfVariableValueNotSupported(value);
    DataOutput out = serializer.getDataOutput(128);
    out.writeClassAndObject(value);
    return out.getStaticBuffer();
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)

Example 3 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class KCVSLog method writeMessage.

private Entry writeMessage(KCVSMessage msg) {
    StaticBuffer content = msg.getContent();
    DataOutput out = manager.serializer.getDataOutput(8 + 8 + manager.senderId.length() + 2 + content.length());
    Instant rawTimestamp = msg.getTimestamp();
    Preconditions.checkArgument(rawTimestamp.isAfter(Instant.EPOCH));
    out.putLong(times.getTime(rawTimestamp));
    out.writeObjectNotNull(manager.senderId);
    out.putLong(numMsgCounter.incrementAndGet());
    final int valuePos = out.getPosition();
    out.putBytes(content);
    return new StaticArrayEntry(out.getStaticBuffer(), valuePos);
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) Instant(java.time.Instant)

Example 4 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class TransactionLogHeader method serializeHeader.

private DataOutput serializeHeader(Serializer serializer, int capacity, LogTxStatus status, EnumMap<LogTxMeta, Object> meta) {
    Preconditions.checkArgument(status != null && meta != null, "Invalid status or meta");
    DataOutput out = serializer.getDataOutput(capacity);
    out.putLong(times.getTime(txTimestamp));
    VariableLong.writePositive(out, transactionId);
    out.writeObjectNotNull(status);
    Preconditions.checkArgument(meta.size() < Byte.MAX_VALUE, "Too much meta data: %s", meta.size());
    out.putByte(VariableLong.unsignedByte(meta.size()));
    for (Map.Entry<LogTxMeta, Object> metaentry : meta.entrySet()) {
        assert metaentry.getValue() != null;
        out.putByte(VariableLong.unsignedByte(metaentry.getKey().ordinal()));
        out.writeObjectNotNull(metaentry.getValue());
    }
    return out;
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)

Example 5 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class KCVSLog method getLogKey.

private StaticBuffer getLogKey(final int partitionId, final int bucketId, final int timeslice) {
    Preconditions.checkArgument(partitionId >= 0 && partitionId < (1 << manager.partitionBitWidth));
    Preconditions.checkArgument(bucketId >= 0 && bucketId < numBuckets);
    DataOutput o = manager.serializer.getDataOutput(3 * 4);
    //Offset to put significant bits in front
    o.putInt((partitionId << (32 - manager.partitionBitWidth)));
    o.putInt(bucketId);
    o.putInt(timeslice);
    return o.getStaticBuffer();
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)

Aggregations

DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)34 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)12 Test (org.junit.Test)11 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)8 StaticArrayEntry (com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry)2 DirectionID (com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.DirectionID)2 ImplicitKey (com.thinkaurelius.titan.graphdb.types.system.ImplicitKey)2 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)2 LoggerFactory (org.slf4j.LoggerFactory)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 WriteBuffer (com.thinkaurelius.titan.diskstorage.WriteBuffer)1 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)1 WriteByteBuffer (com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer)1 IDHandler (com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler)1 Serializer (com.thinkaurelius.titan.graphdb.database.serialize.Serializer)1 StandardSerializer (com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer)1 RelationCategory (com.thinkaurelius.titan.graphdb.internal.RelationCategory)1 EdgeDirection (com.thinkaurelius.titan.graphdb.relations.EdgeDirection)1