Search in sources :

Example 11 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

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;
        List<SerialEntry> entries = new ArrayList<SerialEntry>(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(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) LoggerFactory(org.slf4j.LoggerFactory) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 12 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

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 e) {
        }
    }
    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(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) Test(org.junit.Test)

Example 13 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class SerializerTest method testSerializedOrder.

@Test
public void testSerializedOrder() {
    serialize.registerClass(1, TClass1.class, new TClass1Serializer());
    Map<Class, Factory> sortTypes = new HashMap<Class, Factory>();
    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)), 0.0);
        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(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) LoggerFactory(org.slf4j.LoggerFactory) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 14 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

the class SerializerTest method testStringCompression.

@Test
public void testStringCompression() {
    //ASCII encoding
    for (int t = 0; t < 100; t++) {
        String x = getRandomString(StringSerializer.TEXT_COMRPESSION_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_COMRPESSION_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(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 15 with DataOutput

use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.

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(new Byte((byte) 0));
    out.writeObjectNotNull(Short.MIN_VALUE);
    out.writeObjectNotNull(Short.MAX_VALUE);
    out.writeObjectNotNull(new Short((short) 0));
    out.writeObjectNotNull(Character.MIN_VALUE);
    out.writeObjectNotNull(Character.MAX_VALUE);
    out.writeObjectNotNull(new Character('a'));
    out.writeObjectNotNull(Integer.MIN_VALUE);
    out.writeObjectNotNull(Integer.MAX_VALUE);
    out.writeObjectNotNull(new Integer(0));
    out.writeObjectNotNull(Long.MIN_VALUE);
    out.writeObjectNotNull(Long.MAX_VALUE);
    out.writeObjectNotNull(new Long(0));
    out.writeObjectNotNull(new Float((float) 0.0));
    out.writeObjectNotNull(new Double(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).floatValue(), 1e-20);
    assertEquals(0.0, serialize.readObjectNotNull(b, Double.class).doubleValue(), 1e-20);
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) Test(org.junit.Test)

Aggregations

DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)34 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)12 Test (org.junit.Test)11 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)8 StaticArrayEntry (com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry)2 DirectionID (com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.DirectionID)2 ImplicitKey (com.thinkaurelius.titan.graphdb.types.system.ImplicitKey)2 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)2 LoggerFactory (org.slf4j.LoggerFactory)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 WriteBuffer (com.thinkaurelius.titan.diskstorage.WriteBuffer)1 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)1 WriteByteBuffer (com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer)1 IDHandler (com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler)1 Serializer (com.thinkaurelius.titan.graphdb.database.serialize.Serializer)1 StandardSerializer (com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer)1 RelationCategory (com.thinkaurelius.titan.graphdb.internal.RelationCategory)1 EdgeDirection (com.thinkaurelius.titan.graphdb.relations.EdgeDirection)1