use of com.thinkaurelius.titan.diskstorage.ReadBuffer 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);
}
}
}
use of com.thinkaurelius.titan.diskstorage.ReadBuffer 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());
}
use of com.thinkaurelius.titan.diskstorage.ReadBuffer 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);
}
}
use of com.thinkaurelius.titan.diskstorage.ReadBuffer 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);
}
use of com.thinkaurelius.titan.diskstorage.ReadBuffer in project titan by thinkaurelius.
the class SerializerTestCommon method multipleStringWrite.
protected void multipleStringWrite() {
//26 chars
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int no = 100;
DataOutput out = serialize.getDataOutput(128);
for (int i = 0; i < no; i++) {
String str = base + (i + 1);
out.writeObjectNotNull(str);
}
ReadBuffer b = out.getStaticBuffer().asReadBuffer();
if (printStats)
log.debug(bufferStats(b));
for (int i = 0; i < no; i++) {
String str = base + (i + 1);
String read = serialize.readObjectNotNull(b, String.class);
assertEquals(str, read);
}
assertFalse(b.hasRemaining());
}
Aggregations