Search in sources :

Example 26 with DataOutput

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

the class SerializerTestCommon method objectWriteRead.

protected void objectWriteRead() {
    TClass1 t1 = new TClass1(3245234223423433123L, 0.333f);
    TClass2 t2 = new TClass2("This is a test", 4234234);
    TEnum t3 = TEnum.THREE;
    TEnum t4 = TEnum.TWO;
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(t1);
    out.writeClassAndObject(t2);
    out.writeObject(t3, TEnum.class);
    out.writeClassAndObject(t4);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(t1, serialize.readObjectNotNull(b, TClass1.class));
    assertEquals(t2, serialize.readClassAndObject(b));
    assertEquals(t3, serialize.readObject(b, TEnum.class));
    assertEquals(t4, serialize.readClassAndObject(b));
    assertFalse(b.hasRemaining());
}
Also used : TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TClass1(org.janusgraph.graphdb.serializer.attributes.TClass1) TEnum(org.janusgraph.graphdb.serializer.attributes.TEnum)

Example 27 with DataOutput

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

the class SerializerTest method testStringCompression.

@Test
public void testStringCompression() {
    // ASCII encoding
    for (int t = 0; t < 100; t++) {
        String x = getRandomString(StringSerializer.TEXT_COMPRESSION_THRESHOLD - 1, ASCII_VALUE);
        assertEquals(x.length() + 1, getStringBuffer(x).length());
    }
    // SMAZ Encoding
    // String[] texts = {
    // "To Sherlock Holmes she is always the woman. I have seldom heard him mention her under any other name. In his eyes she eclipses and predominates the whole of her sex.",
    // "His manner was not effusive. It seldom was; but he was glad, I think, to see me. With hardly a word spoken, but with a kindly eye, he waved me to an armchair",
    // "I could not help laughing at the ease with which he explained his process of deduction.",
    // "A man entered who could hardly have been less than six feet six inches in height, with the chest and limbs of a Hercules. His dress was rich with a richness which would, in England"
    // };
    // for (String text : texts) {
    // assertTrue(text.length()> StringSerializer.TEXT_COMPRESSION_THRESHOLD);
    // StaticBuffer s = getStringBuffer(text);
    // //            System.out.println(String.format("String length [%s] -> byte size [%s]",text.length(),s.length()));
    // assertTrue(text.length()>s.length()); //Test that actual compression is happening
    // }
    // Gzip Encoding
    String[] patterns = { "aQd>@!as/df5h", "sdfodoiwk", "sdf", "ab", "asdfwewefefwdfkajhqwkdhj" };
    int targetLength = StringSerializer.LONG_COMPRESSION_THRESHOLD * 5;
    for (String pattern : patterns) {
        StringBuilder sb = new StringBuilder(targetLength);
        for (int i = 0; i < targetLength / pattern.length(); i++) sb.append(pattern);
        String text = sb.toString();
        assertTrue(text.length() > StringSerializer.LONG_COMPRESSION_THRESHOLD);
        StaticBuffer s = getStringBuffer(text);
        // System.out.println(String.format("String length [%s] -> byte size [%s]",text.length(),s.length()));
        // Test that radical compression is happening
        assertTrue(text.length() > s.length() * 10);
    }
    for (int t = 0; t < 10000; t++) {
        String x = STRING_FACTORY.newInstance();
        DataOutput o = serialize.getDataOutput(64);
        o.writeObject(x, String.class);
        ReadBuffer r = o.getStaticBuffer().asReadBuffer();
        String y = serialize.readObject(r, String.class);
        assertEquals(x, y);
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

Example 28 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput 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) THashMapSerializer(org.janusgraph.graphdb.serializer.attributes.THashMapSerializer) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 29 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput 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 30 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput 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(Integer.signum(s1.compareTo(s2)), Integer.signum(b1.compareTo(b2)), s1 + " vs " + s2);
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

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