use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.
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(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(() -> {
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 org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.
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((byte) 0);
out.writeObjectNotNull(Short.MIN_VALUE);
out.writeObjectNotNull(Short.MAX_VALUE);
out.writeObjectNotNull((short) 0);
out.writeObjectNotNull(Character.MIN_VALUE);
out.writeObjectNotNull(Character.MAX_VALUE);
out.writeObjectNotNull('a');
out.writeObjectNotNull(Integer.MIN_VALUE);
out.writeObjectNotNull(Integer.MAX_VALUE);
out.writeObjectNotNull(0);
out.writeObjectNotNull(Long.MIN_VALUE);
out.writeObjectNotNull(Long.MAX_VALUE);
out.writeObjectNotNull(0L);
out.writeObjectNotNull((float) 0.0);
out.writeObjectNotNull(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), 1e-20);
assertEquals(0.0, serialize.readObjectNotNull(b, Double.class), 1e-20);
}
use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.
the class SerializerTest method largeWriteTest.
@Test
public void largeWriteTest() {
// 26 chars
final String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final StringBuilder str = new StringBuilder();
for (int i = 0; i < 100; i++) str.append(base);
DataOutput out = serialize.getDataOutput(128);
out.writeObjectNotNull(str.toString());
ReadBuffer b = out.getStaticBuffer().asReadBuffer();
if (printStats)
log.debug(bufferStats(b));
assertEquals(str.toString(), serialize.readObjectNotNull(b, String.class));
assertFalse(b.hasRemaining());
}
use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.
the class SerializerTest method testSerializedOrder.
@Test
public void testSerializedOrder() {
serialize.registerClass(1, TClass1.class, new TClass1Serializer());
final Map<Class, Factory> sortTypes = new HashMap<>();
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)));
Object c1o = serialize.readObjectByteOrder(s1.asReadBuffer(), type.getKey());
Object c2o = serialize.readObjectByteOrder(s2.asReadBuffer(), type.getKey());
assertEquals(c1, c1o);
assertEquals(c2, c2o);
}
}
use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.
the class SerializerTest method jsonSerialization.
private <T extends JsonNode> void jsonSerialization(Class<T> type, String jsonContent) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
T jsonNode = type.cast(objectMapper.readTree(jsonContent));
DataOutput out = serialize.getDataOutput(128);
out.writeObjectNotNull(jsonNode);
ReadBuffer b = out.getStaticBuffer().asReadBuffer();
assertEquals(jsonNode, serialize.readObjectNotNull(b, type));
}
Aggregations