Search in sources :

Example 6 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class EdgeSerializer method getQuery.

public SliceQuery getQuery(InternalRelationType type, Direction dir, TypedInterval[] sortKey) {
    Preconditions.checkNotNull(type);
    Preconditions.checkNotNull(dir);
    Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
    StaticBuffer sliceStart = null, sliceEnd = null;
    RelationCategory rt = type.isPropertyKey() ? RelationCategory.PROPERTY : RelationCategory.EDGE;
    if (dir == Direction.BOTH) {
        assert type.isEdgeLabel();
        sliceStart = IDHandler.getRelationType(type.longId(), getDirID(Direction.OUT, rt), type.isInvisibleType());
        sliceEnd = IDHandler.getRelationType(type.longId(), getDirID(Direction.IN, rt), type.isInvisibleType());
        assert sliceStart.compareTo(sliceEnd) < 0;
        sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
    } else {
        DirectionID dirID = getDirID(dir, rt);
        DataOutput colStart = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY);
        DataOutput colEnd = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY);
        IDHandler.writeRelationType(colStart, type.longId(), dirID, type.isInvisibleType());
        IDHandler.writeRelationType(colEnd, type.longId(), dirID, type.isInvisibleType());
        long[] sortKeyIDs = type.getSortKey();
        Preconditions.checkArgument(sortKey.length >= sortKeyIDs.length);
        assert colStart.getPosition() == colEnd.getPosition();
        int keyStartPos = colStart.getPosition();
        int keyEndPos = -1;
        for (int i = 0; i < sortKey.length && sortKey[i] != null; i++) {
            PropertyKey propertyKey = sortKey[i].key;
            Interval interval = sortKey[i].interval;
            if (i >= sortKeyIDs.length) {
                assert !type.multiplicity().isUnique(dir);
                assert propertyKey == ImplicitKey.JANUSGRAPHID || propertyKey == ImplicitKey.ADJACENT_ID;
                assert propertyKey != ImplicitKey.ADJACENT_ID || (i == sortKeyIDs.length);
                assert propertyKey != ImplicitKey.JANUSGRAPHID || (!type.multiplicity().isConstrained() && (i == sortKeyIDs.length || i == sortKeyIDs.length + 1));
                assert colStart.getPosition() == colEnd.getPosition();
                assert interval == null || interval.isPoints();
                keyEndPos = colStart.getPosition();
            } else {
                assert !type.multiplicity().isConstrained();
                assert propertyKey.longId() == sortKeyIDs[i];
            }
            if (interval == null || interval.isEmpty()) {
                break;
            }
            if (interval.isPoints()) {
                if (propertyKey == ImplicitKey.JANUSGRAPHID || propertyKey == ImplicitKey.ADJACENT_ID) {
                    assert !type.multiplicity().isUnique(dir);
                    VariableLong.writePositiveBackward(colStart, (Long) interval.getStart());
                    VariableLong.writePositiveBackward(colEnd, (Long) interval.getEnd());
                } else {
                    writeInline(colStart, propertyKey, interval.getStart(), InlineType.KEY);
                    writeInline(colEnd, propertyKey, interval.getEnd(), InlineType.KEY);
                }
            } else {
                if (interval.getStart() != null)
                    writeInline(colStart, propertyKey, interval.getStart(), InlineType.KEY);
                if (interval.getEnd() != null)
                    writeInline(colEnd, propertyKey, interval.getEnd(), InlineType.KEY);
                switch(type.getSortOrder()) {
                    case ASC:
                        sliceStart = colStart.getStaticBuffer();
                        sliceEnd = colEnd.getStaticBuffer();
                        if (!interval.startInclusive())
                            sliceStart = BufferUtil.nextBiggerBuffer(sliceStart);
                        if (interval.endInclusive())
                            sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
                        break;
                    case DESC:
                        sliceEnd = colStart.getStaticBufferFlipBytes(keyStartPos, colStart.getPosition());
                        sliceStart = colEnd.getStaticBufferFlipBytes(keyStartPos, colEnd.getPosition());
                        if (interval.startInclusive())
                            sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
                        if (!interval.endInclusive())
                            sliceStart = BufferUtil.nextBiggerBuffer(sliceStart);
                        break;
                    default:
                        throw new AssertionError(type.getSortOrder().toString());
                }
                assert sliceStart.compareTo(sliceEnd) <= 0;
                break;
            }
        }
        if (sliceStart == null) {
            assert sliceEnd == null && colStart.getPosition() == colEnd.getPosition();
            if (keyEndPos < 0)
                keyEndPos = colStart.getPosition();
            switch(type.getSortOrder()) {
                case ASC:
                    sliceStart = colStart.getStaticBuffer();
                    break;
                case DESC:
                    sliceStart = colStart.getStaticBufferFlipBytes(keyStartPos, keyEndPos);
                    break;
                default:
                    throw new AssertionError(type.getSortOrder().toString());
            }
            sliceEnd = BufferUtil.nextBiggerBuffer(sliceStart);
        }
    }
    return new SliceQuery(sliceStart, sliceEnd, type.name());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) RelationCategory(org.janusgraph.graphdb.internal.RelationCategory) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) PropertyKey(org.janusgraph.core.PropertyKey) SliceQuery(org.janusgraph.diskstorage.keycolumnvalue.SliceQuery) DirectionID(org.janusgraph.graphdb.database.idhandling.IDHandler.DirectionID) Interval(org.janusgraph.util.datastructures.Interval)

Example 7 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class EdgeSerializer method writeRelation.

public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
    assert type == relation.getType() || (type.getBaseType() != null && 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 {
        Preconditions.checkArgument(relation.isProperty(), "Given relation is not property");
        Object value = ((JanusGraphVertexProperty) 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;
    return new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) LongArrayList(com.carrotsearch.hppc.LongArrayList) LongSet(com.carrotsearch.hppc.LongSet) EdgeDirection(org.janusgraph.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) LongHashSet(com.carrotsearch.hppc.LongHashSet) Multiplicity(org.janusgraph.core.Multiplicity) ImplicitKey(org.janusgraph.graphdb.types.system.ImplicitKey) PropertyKey(org.janusgraph.core.PropertyKey) DirectionID(org.janusgraph.graphdb.database.idhandling.IDHandler.DirectionID)

Example 8 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class ParameterSerializer method write.

@Override
public void write(WriteBuffer buffer, Parameter attribute) {
    DataOutput out = (DataOutput) buffer;
    out.writeObjectNotNull(attribute.key());
    out.writeClassAndObject(attribute.value());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput)

Example 9 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class ManagementLogger method sendCacheEviction.

public void sendCacheEviction(Set<JanusGraphSchemaVertex> updatedTypes, final boolean evictGraphFromCache, List<Callable<Boolean>> updatedTypeTriggers, Set<String> openInstances) {
    Preconditions.checkArgument(CollectionUtils.isNotEmpty(openInstances), "openInstances cannot be null or empty");
    long evictionId = evictionTriggerCounter.incrementAndGet();
    evictionTriggerMap.put(evictionId, new EvictionTrigger(evictionId, updatedTypeTriggers, graph));
    DataOutput out = graph.getDataSerializer().getDataOutput(128);
    out.writeObjectNotNull(MgmtLogType.CACHED_TYPE_EVICTION);
    VariableLong.writePositive(out, evictionId);
    VariableLong.writePositive(out, updatedTypes.size());
    for (JanusGraphSchemaVertex type : updatedTypes) {
        assert type.hasId();
        VariableLong.writePositive(out, type.longId());
    }
    if (evictGraphFromCache) {
        out.writeObjectNotNull(EVICT);
    } else {
        out.writeObjectNotNull(DO_NOT_EVICT);
    }
    sysLog.add(out.getStaticBuffer());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)

Example 10 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

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(org.janusgraph.graphdb.database.serialize.DataOutput)

Aggregations

DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)39 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)16 Test (org.junit.jupiter.api.Test)14 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)10 HashMap (java.util.HashMap)3 Map (java.util.Map)3 StaticArrayEntry (org.janusgraph.diskstorage.util.StaticArrayEntry)3 TClass1Serializer (org.janusgraph.graphdb.serializer.attributes.TClass1Serializer)3 TClass2 (org.janusgraph.graphdb.serializer.attributes.TClass2)3 SpatialContextFactory (org.locationtech.spatial4j.context.SpatialContextFactory)3 EnumMap (java.util.EnumMap)2 PropertyKey (org.janusgraph.core.PropertyKey)2 Geoshape (org.janusgraph.core.attribute.Geoshape)2 DirectionID (org.janusgraph.graphdb.database.idhandling.IDHandler.DirectionID)2 RelationCategory (org.janusgraph.graphdb.internal.RelationCategory)2 TClass1 (org.janusgraph.graphdb.serializer.attributes.TClass1)2 TEnum (org.janusgraph.graphdb.serializer.attributes.TEnum)2 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)2 LoggerFactory (org.slf4j.LoggerFactory)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1