@Test public void stringSerialization() { // Characters DataOutput out = serialize.getDataOutput(((int) Character.MAX_VALUE) * 2 + 8, true); 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, true); DataOutput out2 = serialize.getDataOutput(32 + 5, true); String s1 = RandomGenerator.randomString(1, 32); String s2 = RandomGenerator.randomString(1, 32); out1.writeObjectNotNull(s1); out2.writeObjectNotNull(s2); StaticBuffer b1 = out1.getStaticBuffer(); StaticBuffer b2 = out2.getStaticBuffer(); assertEquals(s1, serialize.readObjectNotNull(b1.asReadBuffer(), String.class)); assertEquals(s2, serialize.readObjectNotNull(b2.asReadBuffer(), String.class)); assertEquals( s1 + " vs " + s2, Integer.signum(s1.compareTo(s2)), Integer.signum(b1.compareTo(b2))); } }
@Test public void classSerialization() { DataOutput out = serialize.getDataOutput(128, true); out.writeObjectNotNull(Boolean.class); out.writeObjectNotNull(Byte.class); out.writeObjectNotNull(Double.class); ReadBuffer b = out.getStaticBuffer().asReadBuffer(); assertEquals(Boolean.class, serialize.readObjectNotNull(b, Class.class)); assertEquals(Byte.class, serialize.readObjectNotNull(b, Class.class)); assertEquals(Double.class, serialize.readObjectNotNull(b, Class.class)); }
@Test public void objectWriteRead() { // serialize.registerClass(short[].class); // serialize.registerClass(TestClass.class); DataOutput out = serialize.getDataOutput(128, true); String str = "This is a test"; int i = 5; TestClass c = new TestClass(5, 8, new short[] {1, 2, 3, 4, 5}, TestEnum.Two); Number n = new Double(3.555); out.writeObjectNotNull(str); out.putInt(i); out.writeObject(c, TestClass.class); out.writeClassAndObject(n); ReadBuffer b = out.getStaticBuffer().asReadBuffer(); if (printStats) log.debug(bufferStats(b)); String str2 = serialize.readObjectNotNull(b, String.class); assertEquals(str, str2); if (printStats) log.debug(bufferStats(b)); assertEquals(b.getInt(), i); TestClass c2 = serialize.readObject(b, TestClass.class); assertEquals(c, c2); if (printStats) log.debug(bufferStats(b)); assertEquals(n, serialize.readClassAndObject(b)); if (printStats) log.debug(bufferStats(b)); assertFalse(b.hasRemaining()); }
@Test public void enumSerializeTest() { DataOutput out = serialize.getDataOutput(128, true); out.writeObjectNotNull(TestEnum.Two); ReadBuffer b = out.getStaticBuffer().asReadBuffer(); if (printStats) log.debug(bufferStats(b)); assertEquals(TestEnum.Two, serialize.readObjectNotNull(b, TestEnum.class)); assertFalse(b.hasRemaining()); }
@Test public void largeWriteTest() { String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 chars String str = ""; for (int i = 0; i < 100; i++) str += base; DataOutput out = serialize.getDataOutput(128, true); out.writeObjectNotNull(str); ReadBuffer b = out.getStaticBuffer().asReadBuffer(); if (printStats) log.debug(bufferStats(b)); assertEquals(str, serialize.readObjectNotNull(b, String.class)); assertFalse(b.hasRemaining()); }
@Test public void longWriteTest() { String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 chars int no = 100; DataOutput out = serialize.getDataOutput(128, true); 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()); }
@Test public void primitiveSerialization() { DataOutput out = serialize.getDataOutput(128, true); 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(FloatSerializer.MIN_VALUE); out.writeObjectNotNull(FloatSerializer.MAX_VALUE); out.writeObjectNotNull(new Float((float) 0.0)); out.writeObjectNotNull(DoubleSerializer.MIN_VALUE); out.writeObjectNotNull(DoubleSerializer.MAX_VALUE); 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( FloatSerializer.MIN_VALUE, serialize.readObjectNotNull(b, Float.class).floatValue(), 1e-20); assertEquals( FloatSerializer.MAX_VALUE, serialize.readObjectNotNull(b, Float.class).floatValue(), 1e-20); assertEquals(0.0, serialize.readObjectNotNull(b, Float.class).floatValue(), 1e-20); assertEquals( DoubleSerializer.MIN_VALUE, serialize.readObjectNotNull(b, Double.class).doubleValue(), 1e-40); assertEquals( DoubleSerializer.MAX_VALUE, serialize.readObjectNotNull(b, Double.class).doubleValue(), 1e-40); assertEquals(0.0, serialize.readObjectNotNull(b, Double.class).doubleValue(), 1e-20); }
protected void loadRelations( Iterable<Entry> entries, VertexRelationLoader loader, InternalTitanTransaction tx) { Map<String, TitanType> etCache = new HashMap<String, TitanType>(); TitanType titanType = null; for (Entry entry : entries) { ByteBuffer column = entry.getColumn(); int dirID = IDHandler.getDirectionID(column.get(column.position())); long etid = IDHandler.readEdgeType(column, idManager); if (titanType == null || titanType.getID() != etid) { titanType = getTypeFromID(etid, tx); } Object[] keys = null; if (!titanType.isSimple()) { TypeDefinition def = ((InternalTitanType) titanType).getDefinition(); String[] keysig = def.getKeySignature(); keys = new Object[keysig.length]; for (int i = 0; i < keysig.length; i++) keys[i] = readInline(column, getEdgeType(keysig[i], etCache, tx)); } long edgeid = 0; if (!titanType.isFunctional()) { edgeid = VariableLong.readPositive(column); } ByteBuffer value = entry.getValue(); if (titanType.isEdgeLabel()) { long nodeIDDiff = VariableLong.read(value); if (titanType.isFunctional()) edgeid = VariableLong.readPositive(value); assert edgeid > 0; long otherid = loader.getVertexId() + nodeIDDiff; assert dirID == 3 || dirID == 2; Direction dir = dirID == 3 ? Direction.IN : Direction.OUT; if (!tx.isDeletedRelation(edgeid)) loader.loadEdge(edgeid, (TitanLabel) titanType, dir, otherid); } else { assert titanType.isPropertyKey(); assert dirID == 0; TitanKey propType = ((TitanKey) titanType); Object attribute = null; if (hasGenericDataType(propType)) { attribute = serializer.readClassAndObject(value); } else { attribute = serializer.readObjectNotNull(value, propType.getDataType()); } assert attribute != null; if (titanType.isFunctional()) edgeid = VariableLong.readPositive(value); assert edgeid > 0; if (!tx.isDeletedRelation(edgeid)) loader.loadProperty(edgeid, propType, attribute); } // Read value inline edges if any if (!titanType.isSimple()) { TypeDefinition def = ((InternalTitanType) titanType).getDefinition(); // First create all keys buffered above String[] keysig = def.getKeySignature(); for (int i = 0; i < keysig.length; i++) { createInlineEdge(loader, getEdgeType(keysig[i], etCache, tx), keys[i]); } // value signature for (String str : def.getCompactSignature()) readLabel(loader, value, getEdgeType(str, etCache, tx)); // Third: read rest while (value.hasRemaining()) { TitanType type = (TitanType) tx.getExistingVertex(IDHandler.readInlineEdgeType(value, idManager)); readLabel(loader, value, type); } } } }