Search in sources :

Example 16 with DataOutput

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

the class SerializerTest method parallelDeserialization.

@Test
public void parallelDeserialization() throws InterruptedException {
    serialize.registerClass(1, TClass2.class, new TClass2Serializer());
    final long value = 8;
    final String str = "123456";
    final TClass2 c = new TClass2("abcdefg", 333);
    DataOutput out = serialize.getDataOutput(128);
    out.putLong(value);
    out.writeClassAndObject(value);
    out.writeObject(c, TClass2.class);
    out.writeObjectNotNull(str);
    final StaticBuffer b = out.getStaticBuffer();
    int numThreads = 4;
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread(() -> {
            for (int j = 0; j < 100000; j++) {
                ReadBuffer buffer = b.asReadBuffer();
                assertEquals(8, buffer.getLong());
                assertEquals(value, (long) serialize.readClassAndObject(buffer));
                assertEquals(c, serialize.readObject(buffer, TClass2.class));
                assertEquals(str, serialize.readObjectNotNull(buffer, String.class));
            }
        });
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].join();
    }
}
Also used : TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TClass2Serializer(org.janusgraph.graphdb.serializer.attributes.TClass2Serializer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with DataOutput

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

the class SerializerTest method primitiveSerialization.

@Test
public void primitiveSerialization() {
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(Boolean.FALSE);
    out.writeObjectNotNull(Boolean.TRUE);
    out.writeObjectNotNull(Byte.MIN_VALUE);
    out.writeObjectNotNull(Byte.MAX_VALUE);
    out.writeObjectNotNull((byte) 0);
    out.writeObjectNotNull(Short.MIN_VALUE);
    out.writeObjectNotNull(Short.MAX_VALUE);
    out.writeObjectNotNull((short) 0);
    out.writeObjectNotNull(Character.MIN_VALUE);
    out.writeObjectNotNull(Character.MAX_VALUE);
    out.writeObjectNotNull('a');
    out.writeObjectNotNull(Integer.MIN_VALUE);
    out.writeObjectNotNull(Integer.MAX_VALUE);
    out.writeObjectNotNull(0);
    out.writeObjectNotNull(Long.MIN_VALUE);
    out.writeObjectNotNull(Long.MAX_VALUE);
    out.writeObjectNotNull(0L);
    out.writeObjectNotNull((float) 0.0);
    out.writeObjectNotNull(0.0);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(Boolean.FALSE, serialize.readObjectNotNull(b, Boolean.class));
    assertEquals(Boolean.TRUE, serialize.readObjectNotNull(b, Boolean.class));
    assertEquals(Byte.MIN_VALUE, serialize.readObjectNotNull(b, Byte.class).longValue());
    assertEquals(Byte.MAX_VALUE, serialize.readObjectNotNull(b, Byte.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Byte.class).longValue());
    assertEquals(Short.MIN_VALUE, serialize.readObjectNotNull(b, Short.class).longValue());
    assertEquals(Short.MAX_VALUE, serialize.readObjectNotNull(b, Short.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Short.class).longValue());
    assertEquals(Character.MIN_VALUE, serialize.readObjectNotNull(b, Character.class).charValue());
    assertEquals(Character.MAX_VALUE, serialize.readObjectNotNull(b, Character.class).charValue());
    assertEquals(new Character('a'), serialize.readObjectNotNull(b, Character.class));
    assertEquals(Integer.MIN_VALUE, serialize.readObjectNotNull(b, Integer.class).longValue());
    assertEquals(Integer.MAX_VALUE, serialize.readObjectNotNull(b, Integer.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Integer.class).longValue());
    assertEquals(Long.MIN_VALUE, serialize.readObjectNotNull(b, Long.class).longValue());
    assertEquals(Long.MAX_VALUE, serialize.readObjectNotNull(b, Long.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Long.class).longValue());
    assertEquals(0.0, serialize.readObjectNotNull(b, Float.class), 1e-20);
    assertEquals(0.0, serialize.readObjectNotNull(b, Double.class), 1e-20);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Test(org.junit.jupiter.api.Test)

Example 18 with DataOutput

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

the class SerializerTest method largeWriteTest.

@Test
public void largeWriteTest() {
    // 26 chars
    final String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    final StringBuilder str = new StringBuilder();
    for (int i = 0; i < 100; i++) str.append(base);
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(str.toString());
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    if (printStats)
        log.debug(bufferStats(b));
    assertEquals(str.toString(), serialize.readObjectNotNull(b, String.class));
    assertFalse(b.hasRemaining());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Test(org.junit.jupiter.api.Test)

Example 19 with DataOutput

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

the class SerializerTest method testSerializedOrder.

@Test
public void testSerializedOrder() {
    serialize.registerClass(1, TClass1.class, new TClass1Serializer());
    final Map<Class, Factory> sortTypes = new HashMap<>();
    for (Map.Entry<Class, Factory> entry : TYPES.entrySet()) {
        if (serialize.isOrderPreservingDatatype(entry.getKey()))
            sortTypes.put(entry.getKey(), entry.getValue());
    }
    assertEquals(10, sortTypes.size());
    for (int t = 0; t < 3000000; t++) {
        DataOutput o1 = serialize.getDataOutput(64);
        DataOutput o2 = serialize.getDataOutput(64);
        Map.Entry<Class, Factory> type = Iterables.get(sortTypes.entrySet(), random.nextInt(sortTypes.size()));
        Comparable c1 = (Comparable) type.getValue().newInstance();
        Comparable c2 = (Comparable) type.getValue().newInstance();
        o1.writeObjectByteOrder(c1, type.getKey());
        o2.writeObjectByteOrder(c2, type.getKey());
        StaticBuffer s1 = o1.getStaticBuffer();
        StaticBuffer s2 = o2.getStaticBuffer();
        assertEquals(Math.signum(c1.compareTo(c2)), Math.signum(s1.compareTo(s2)));
        Object c1o = serialize.readObjectByteOrder(s1.asReadBuffer(), type.getKey());
        Object c2o = serialize.readObjectByteOrder(s2.asReadBuffer(), type.getKey());
        assertEquals(c1, c1o);
        assertEquals(c2, c2o);
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) HashMap(java.util.HashMap) LoggerFactory(org.slf4j.LoggerFactory) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) HashMap(java.util.HashMap) Map(java.util.Map) TClass1Serializer(org.janusgraph.graphdb.serializer.attributes.TClass1Serializer) Test(org.junit.jupiter.api.Test)

Example 20 with DataOutput

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

the class SerializerTest method jsonSerialization.

private <T extends JsonNode> void jsonSerialization(Class<T> type, String jsonContent) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    T jsonNode = type.cast(objectMapper.readTree(jsonContent));
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(jsonNode);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(jsonNode, serialize.readObjectNotNull(b, type));
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper)

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