Search in sources :

Example 16 with ReadBuffer

use of org.janusgraph.diskstorage.ReadBuffer 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.Test)

Example 17 with ReadBuffer

use of org.janusgraph.diskstorage.ReadBuffer 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) LoggerFactory(org.slf4j.LoggerFactory) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 18 with ReadBuffer

use of org.janusgraph.diskstorage.ReadBuffer in project janusgraph by JanusGraph.

the class SerializerTest method comparableStringSerialization.

@Test
public void comparableStringSerialization() {
    // Characters
    DataOutput out = serialize.getDataOutput(((int) Character.MAX_VALUE) * 2 + 8);
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
        out.writeObjectNotNull(c);
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
        assertEquals(c, serialize.readObjectNotNull(b, Character.class).charValue());
    }
    // String
    for (int t = 0; t < 10000; t++) {
        DataOutput out1 = serialize.getDataOutput(32 + 5);
        DataOutput out2 = serialize.getDataOutput(32 + 5);
        String s1 = RandomGenerator.randomString(1, 32);
        String s2 = RandomGenerator.randomString(1, 32);
        out1.writeObjectByteOrder(s1, String.class);
        out2.writeObjectByteOrder(s2, String.class);
        StaticBuffer b1 = out1.getStaticBuffer();
        StaticBuffer b2 = out2.getStaticBuffer();
        assertEquals(s1, serialize.readObjectByteOrder(b1.asReadBuffer(), String.class));
        assertEquals(s2, serialize.readObjectByteOrder(b2.asReadBuffer(), String.class));
        assertEquals(s1 + " vs " + s2, Integer.signum(s1.compareTo(s2)), Integer.signum(b1.compareTo(b2)));
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 19 with ReadBuffer

use of org.janusgraph.diskstorage.ReadBuffer 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) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Test(org.junit.Test)

Example 20 with ReadBuffer

use of org.janusgraph.diskstorage.ReadBuffer in project janusgraph by JanusGraph.

the class SerializerTest method customHashMapSerializeTest.

@Test
public void customHashMapSerializeTest() {
    serialize.registerClass(1, HashMap.class, new THashMapSerializer());
    DataOutput out = serialize.getDataOutput(128);
    final String property1 = "property1";
    final String value1 = "value1";
    final HashMap<String, Object> hashMapIn = new HashMap<>();
    hashMapIn.put(property1, value1);
    out.writeObjectNotNull(hashMapIn);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    if (printStats)
        log.debug(bufferStats(b));
    final HashMap<String, Object> hashMapOut = serialize.readObjectNotNull(b, HashMap.class);
    assertNotNull(hashMapOut);
    assertEquals(2, hashMapOut.size());
    assertEquals(value1, hashMapOut.get(property1));
    // THashMapSerializer adds this
    assertTrue(hashMapOut.containsKey(THashMapSerializer.class.getName()));
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Test(org.junit.Test)

Aggregations

ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)21 Test (org.junit.Test)15 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)14 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)9 WriteBuffer (org.janusgraph.diskstorage.WriteBuffer)4 WriteByteBuffer (org.janusgraph.diskstorage.util.WriteByteBuffer)4 Instant (java.time.Instant)2 Serializer (org.janusgraph.graphdb.database.serialize.Serializer)2 LoggerFactory (org.slf4j.LoggerFactory)2 LongObjectHashMap (com.carrotsearch.hppc.LongObjectHashMap)1 Preconditions (com.google.common.base.Preconditions)1 Map (java.util.Map)1 Random (java.util.Random)1 StopWatch (org.apache.commons.lang.time.StopWatch)1 Direction (org.apache.tinkerpop.gremlin.structure.Direction)1 EntryMetaData (org.janusgraph.diskstorage.EntryMetaData)1 StaticArrayBuffer (org.janusgraph.diskstorage.util.StaticArrayBuffer)1 IDHandler (org.janusgraph.graphdb.database.idhandling.IDHandler)1 RelationTypeParse (org.janusgraph.graphdb.database.idhandling.IDHandler.RelationTypeParse)1 VariableLong (org.janusgraph.graphdb.database.idhandling.VariableLong)1