Search in sources :

Example 1 with TClass1Serializer

use of org.janusgraph.graphdb.serializer.attributes.TClass1Serializer 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 2 with TClass1Serializer

use of org.janusgraph.graphdb.serializer.attributes.TClass1Serializer in project janusgraph by JanusGraph.

the class SerializerTest method objectWriteReadTest.

@Test
public void objectWriteReadTest() {
    serialize.registerClass(2, TClass1.class, new TClass1Serializer());
    serialize.registerClass(80342, TClass2.class, new TClass2Serializer());
    serialize.registerClass(999, TEnum.class, new TEnumSerializer());
    objectWriteRead();
}
Also used : TClass2Serializer(org.janusgraph.graphdb.serializer.attributes.TClass2Serializer) TEnumSerializer(org.janusgraph.graphdb.serializer.attributes.TEnumSerializer) TClass1Serializer(org.janusgraph.graphdb.serializer.attributes.TClass1Serializer) Test(org.junit.jupiter.api.Test)

Example 3 with TClass1Serializer

use of org.janusgraph.graphdb.serializer.attributes.TClass1Serializer in project janusgraph by JanusGraph.

the class SerializerSpeedTest method performanceTestObjectSerialization.

@RepeatedTest(10)
public void performanceTestObjectSerialization() {
    serialize.registerClass(2, TClass1.class, new TClass1Serializer());
    serialize.registerClass(80342, TClass2.class, new TClass2Serializer());
    serialize.registerClass(999, TEnum.class, new TEnumSerializer());
    int runs = 1000000;
    for (int i = 0; i < runs; i++) {
        objectWriteRead();
    }
}
Also used : TClass2Serializer(org.janusgraph.graphdb.serializer.attributes.TClass2Serializer) TEnumSerializer(org.janusgraph.graphdb.serializer.attributes.TEnumSerializer) TClass1Serializer(org.janusgraph.graphdb.serializer.attributes.TClass1Serializer) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 4 with TClass1Serializer

use of org.janusgraph.graphdb.serializer.attributes.TClass1Serializer in project janusgraph by JanusGraph.

the class SerializerTest method testSerializationMixture.

@Test
public void testSerializationMixture() {
    serialize.registerClass(1, TClass1.class, new TClass1Serializer());
    for (int t = 0; t < 1000; t++) {
        DataOutput out = serialize.getDataOutput(128);
        int num = random.nextInt(100) + 1;
        final List<SerialEntry> entries = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            Map.Entry<Class, Factory> type = Iterables.get(TYPES.entrySet(), random.nextInt(TYPES.size()));
            Object element = type.getValue().newInstance();
            boolean notNull = true;
            if (random.nextDouble() < 0.5) {
                notNull = false;
                if (random.nextDouble() < 0.2)
                    element = null;
            }
            entries.add(new SerialEntry(element, type.getKey(), notNull));
            if (notNull)
                out.writeObjectNotNull(element);
            else
                out.writeObject(element, type.getKey());
        }
        StaticBuffer sb = out.getStaticBuffer();
        ReadBuffer in = sb.asReadBuffer();
        for (SerialEntry entry : entries) {
            Object read;
            if (entry.notNull)
                read = serialize.readObjectNotNull(in, entry.clazz);
            else
                read = serialize.readObject(in, entry.clazz);
            if (entry.object == null)
                assertNull(read);
            else if (entry.clazz.isArray()) {
                assertEquals(Array.getLength(entry.object), Array.getLength(read));
                for (int i = 0; i < Array.getLength(read); i++) {
                    assertEquals(Array.get(entry.object, i), Array.get(read, i));
                }
            } else
                assertEquals(entry.object, read);
        }
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ArrayList(java.util.ArrayList) LoggerFactory(org.slf4j.LoggerFactory) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) 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 5 with TClass1Serializer

use of org.janusgraph.graphdb.serializer.attributes.TClass1Serializer 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)

Aggregations

TClass1Serializer (org.janusgraph.graphdb.serializer.attributes.TClass1Serializer)5 Test (org.junit.jupiter.api.Test)4 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)2 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)2 TClass2Serializer (org.janusgraph.graphdb.serializer.attributes.TClass2Serializer)2 TEnumSerializer (org.janusgraph.graphdb.serializer.attributes.TEnumSerializer)2 SpatialContextFactory (org.locationtech.spatial4j.context.SpatialContextFactory)2 LoggerFactory (org.slf4j.LoggerFactory)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TClass1 (org.janusgraph.graphdb.serializer.attributes.TClass1)1 TClass2 (org.janusgraph.graphdb.serializer.attributes.TClass2)1 RepeatedTest (org.junit.jupiter.api.RepeatedTest)1