Search in sources :

Example 31 with DataOutput

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

the class SerializerTest method testLegacyNonJtsSerialization.

@Test
public void testLegacyNonJtsSerialization() throws Exception {
    final SpatialContextFactory factory = new SpatialContextFactory();
    factory.geo = true;
    final SpatialContext context = new SpatialContext(factory);
    BinaryCodec binaryCodec = new BinaryCodec(context, factory);
    Shape[] shapes = new Shape[] { context.getShapeFactory().pointXY(2.5, 0.5), context.getShapeFactory().rect(2.5, 3.5, 0.5, 1.5), context.getShapeFactory().circle(2.5, 0.5, DistanceUtils.dist2Degrees(5, DistanceUtils.EARTH_MEAN_RADIUS_KM)) };
    DataOutput out = serialize.getDataOutput(128);
    for (final Shape shape : shapes) {
        // manually serialize with non-JTS codec
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(1);
        try (DataOutputStream dataOutput = new DataOutputStream(outputStream)) {
            binaryCodec.writeShape(dataOutput, shape);
            dataOutput.flush();
        }
        outputStream.flush();
        byte[] bytes = outputStream.toByteArray();
        VariableLong.writePositive(out, bytes.length);
        out.putBytes(bytes);
    }
    // deserialize with standard serializer
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(Geoshape.geoshape(shapes[0]), serialize.readObjectNotNull(b, Geoshape.class));
    assertEquals(Geoshape.geoshape(shapes[1]), serialize.readObjectNotNull(b, Geoshape.class));
    assertEquals(Geoshape.geoshape(shapes[2]), serialize.readObjectNotNull(b, Geoshape.class));
}
Also used : BinaryCodec(org.locationtech.spatial4j.io.BinaryCodec) DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) DataOutputStream(java.io.DataOutputStream) Geoshape(org.janusgraph.core.attribute.Geoshape) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 32 with DataOutput

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

the class SerializerTest method classSerialization.

@Test
public void classSerialization() {
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(Boolean.class);
    out.writeObjectNotNull(Byte.class);
    out.writeObjectNotNull(Double.class);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(Boolean.class, serialize.readObjectNotNull(b, Class.class));
    assertEquals(Byte.class, serialize.readObjectNotNull(b, Class.class));
    assertEquals(Double.class, serialize.readObjectNotNull(b, Class.class));
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Test(org.junit.jupiter.api.Test)

Example 33 with DataOutput

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

the class SerializerTest method testObjectVerification.

@Test
public void testObjectVerification() {
    serialize.registerClass(2, TClass1.class, new TClass1Serializer());
    TClass1 t1 = new TClass1(24223, 0.25f);
    DataOutput out = serialize.getDataOutput(128);
    out.writeClassAndObject(t1);
    out.writeClassAndObject(null);
    out.writeObject(t1, TClass1.class);
    out.writeObject(null, TClass1.class);
    // Test failure
    for (Object o : new Object[] { new TClass2("abc", 2), Calendar.getInstance(), Lists.newArrayList() }) {
        try {
            out.writeObjectNotNull(o);
            fail();
        } catch (Exception ignored) {
        }
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(t1, serialize.readClassAndObject(b));
    assertNull(serialize.readClassAndObject(b));
    assertEquals(t1, serialize.readObject(b, TClass1.class));
    assertNull(serialize.readObject(b, TClass1.class));
    assertFalse(b.hasRemaining());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TClass1(org.janusgraph.graphdb.serializer.attributes.TClass1) IOException(java.io.IOException) TClass1Serializer(org.janusgraph.graphdb.serializer.attributes.TClass1Serializer) Test(org.junit.jupiter.api.Test)

Example 34 with DataOutput

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

the class ManagementSystem method commit.

@Override
public synchronized void commit() {
    ensureOpen();
    // Commit config changes
    if (transactionalConfig.hasMutations()) {
        DataOutput out = graph.getDataSerializer().getDataOutput(128);
        out.writeObjectNotNull(MgmtLogType.CONFIG_MUTATION);
        transactionalConfig.logMutations(out);
        sysLog.add(out.getStaticBuffer());
    }
    transactionalConfig.commit();
    // Commit underlying transaction
    transaction.commit();
    // Communicate schema changes
    if (!updatedTypes.isEmpty() || evictGraphFromCache) {
        managementLogger.sendCacheEviction(updatedTypes, evictGraphFromCache, updatedTypeTriggers, getOpenInstancesInternal());
        for (JanusGraphSchemaVertex schemaVertex : updatedTypes) {
            schemaCache.expireSchemaElement(schemaVertex.longId());
            for (JanusGraphTransaction tx : graph.getOpenTransactions()) {
                tx.expireSchemaElement(schemaVertex.longId());
            }
        }
    }
    if (graphShutdownRequired)
        graph.close();
    close();
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)

Example 35 with DataOutput

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

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

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