use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class SerializerTest method parallelDeserialization.
@Test
public void parallelDeserialization() throws InterruptedException {
serialize.registerClass(1, TClass2.class, new TClass2Serializer());
final long value = 8;
final String str = "123456";
final TClass2 c = new TClass2("abcdefg", 333);
DataOutput out = serialize.getDataOutput(128);
out.putLong(value);
out.writeClassAndObject(Long.valueOf(value));
out.writeObject(c, TClass2.class);
out.writeObjectNotNull(str);
final StaticBuffer b = out.getStaticBuffer();
int numThreads = 4;
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 100000; j++) {
ReadBuffer buffer = b.asReadBuffer();
assertEquals(8, buffer.getLong());
assertEquals(value, (long) serialize.readClassAndObject(buffer));
assertEquals(c, serialize.readObject(buffer, TClass2.class));
assertEquals(str, serialize.readObjectNotNull(buffer, String.class));
}
}
});
threads[i].start();
}
for (int i = 0; i < numThreads; i++) {
threads[i].join();
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
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(Character.valueOf(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)));
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer 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.StaticBuffer 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);
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer 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);
}
}
Aggregations