You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by si...@apache.org on 2012/02/15 10:19:24 UTC
svn commit: r1244411 - in
/incubator/directmemory/trunk/directmemory-cache/src:
main/java/org/apache/directmemory/cache/
main/java/org/apache/directmemory/serialization/
test/java/org/apache/directmemory/serialization/test/
Author: simonetripodi
Date: Wed Feb 15 09:19:23 2012
New Revision: 1244411
URL: http://svn.apache.org/viewvc?rev=1244411&view=rev
Log:
before proceeding to modularization, restyled Serializer APIs (and related impl + references) in order to have safe type inference
Modified:
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffSerializerV1.java
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffWithLinkedBufferSerializer.java
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/Serializer.java
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/StandardSerializer.java
incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/DummyPojoSerializer.java
incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/SerializerTest.java
Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java Wed Feb 15 09:19:23 2012
@@ -128,7 +128,7 @@ public class CacheServiceImpl
{
try
{
- byte[] payload = serializer.serialize( object, object.getClass() );
+ byte[] payload = serializer.serialize( object );
Pointer ptr = putByteArray( key, payload, expiresIn );
ptr.clazz = object.getClass();
return ptr;
@@ -152,7 +152,7 @@ public class CacheServiceImpl
Pointer p = map.get( key );
try
{
- p = memoryManager.update( p, serializer.serialize( object, object.getClass() ) );
+ p = memoryManager.update( p, serializer.serialize( object ) );
p.clazz = object.getClass();
return p;
}
Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffSerializerV1.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffSerializerV1.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffSerializerV1.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffSerializerV1.java Wed Feb 15 09:19:23 2012
@@ -19,34 +19,40 @@ package org.apache.directmemory.serializ
* under the License.
*/
-import com.dyuproject.protostuff.LinkedBuffer;
-import com.dyuproject.protostuff.ProtostuffIOUtil;
-import com.dyuproject.protostuff.Schema;
-import com.dyuproject.protostuff.runtime.RuntimeSchema;
-import org.apache.directmemory.measures.Ram;
+import static com.dyuproject.protostuff.LinkedBuffer.allocate;
+import static com.dyuproject.protostuff.ProtostuffIOUtil.mergeFrom;
+import static com.dyuproject.protostuff.ProtostuffIOUtil.toByteArray;
+import static com.dyuproject.protostuff.runtime.RuntimeSchema.getSchema;
import java.io.IOException;
+import org.apache.directmemory.measures.Ram;
+
+import com.dyuproject.protostuff.LinkedBuffer;
+import com.dyuproject.protostuff.Schema;
+
public class ProtoStuffSerializerV1
implements Serializer
{
static int serBufferSize = Ram.Kb( 3 );
- /* (non-Javadoc)
- * @see org.apache.directmemory.utils.Serializer#serialize(java.lang.Object, java.lang.Class)
- */
- @SuppressWarnings( "unchecked" )
- public byte[] serialize( Object obj, @SuppressWarnings( "rawtypes" ) Class clazz )
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> byte[] serialize( T obj )
throws IOException
{
- @SuppressWarnings( "rawtypes" ) Schema schema = RuntimeSchema.getSchema( clazz );
- final LinkedBuffer buffer = LinkedBuffer.allocate( serBufferSize );
+ @SuppressWarnings( "unchecked" ) // type should be safe since got directly from the obj
+ final Class<T> clazz = (Class<T>) obj.getClass();
+ final Schema<T> schema = getSchema( clazz );
+ final LinkedBuffer buffer = allocate( serBufferSize );
byte[] protostuff = null;
try
{
- protostuff = ProtostuffIOUtil.toByteArray( obj, schema, buffer );
+ protostuff = toByteArray( obj, schema, buffer );
}
finally
{
@@ -55,17 +61,17 @@ public class ProtoStuffSerializerV1
return protostuff;
}
- /* (non-Javadoc)
- * @see org.apache.directmemory.utils.Serializer#deserialize(byte[], java.lang.Class)
- */
- @SuppressWarnings( "unchecked" )
- public Object deserialize( byte[] source, @SuppressWarnings( "rawtypes" ) Class clazz )
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> T deserialize( byte[] source, Class<T> clazz )
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
- final Object object = clazz.newInstance();
- @SuppressWarnings( "rawtypes" )
- final Schema schema = RuntimeSchema.getSchema( clazz );
- ProtostuffIOUtil.mergeFrom( source, object, schema );
+ final T object = clazz.newInstance();
+ final Schema<T> schema = getSchema( clazz );
+ mergeFrom( source, object, schema );
return object;
}
+
}
Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffWithLinkedBufferSerializer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffWithLinkedBufferSerializer.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffWithLinkedBufferSerializer.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/ProtoStuffWithLinkedBufferSerializer.java Wed Feb 15 09:19:23 2012
@@ -19,14 +19,17 @@ package org.apache.directmemory.serializ
* under the License.
*/
+import static com.dyuproject.protostuff.LinkedBuffer.allocate;
+import static com.dyuproject.protostuff.ProtostuffIOUtil.mergeFrom;
+import static com.dyuproject.protostuff.ProtostuffIOUtil.toByteArray;
+import static com.dyuproject.protostuff.runtime.RuntimeSchema.getSchema;
+
import java.io.IOException;
import org.apache.directmemory.measures.Ram;
import com.dyuproject.protostuff.LinkedBuffer;
-import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
-import com.dyuproject.protostuff.runtime.RuntimeSchema;
public final class ProtoStuffWithLinkedBufferSerializer
implements Serializer
@@ -56,7 +59,7 @@ public final class ProtoStuffWithLinkedB
{
protected LinkedBuffer initialValue()
{
- return LinkedBuffer.allocate( bufferSize );
+ return allocate( bufferSize );
}
};
@@ -72,20 +75,23 @@ public final class ProtoStuffWithLinkedB
}
- /* (non-Javadoc)
- * @see org.apache.directmemory.utils.Serializer#serialize(java.lang.Object, java.lang.Class)
- */
- @SuppressWarnings( "unchecked" )
- public byte[] serialize( Object obj, @SuppressWarnings( "rawtypes" ) Class clazz )
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> byte[] serialize( T obj )
throws IOException
{
- @SuppressWarnings( "rawtypes" ) Schema schema = RuntimeSchema.getSchema( clazz );
+ @SuppressWarnings( "unchecked" ) // type should be safe since got directly from the obj
+ final Class<T> clazz = (Class<T>) obj.getClass();
+ final Schema<T> schema = getSchema( clazz );
+
final LinkedBuffer buffer = localBuffer.get();
byte[] protostuff = null;
try
{
- protostuff = ProtostuffIOUtil.toByteArray( obj, schema, buffer );
+ protostuff = toByteArray( obj, schema, buffer );
}
finally
{
@@ -94,16 +100,16 @@ public final class ProtoStuffWithLinkedB
return protostuff;
}
- /* (non-Javadoc)
- * @see org.apache.directmemory.utils.Serializer#deserialize(byte[], java.lang.Class)
- */
- @SuppressWarnings( "unchecked" )
- public Object deserialize( byte[] source, @SuppressWarnings( "rawtypes" ) Class clazz )
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> T deserialize( byte[] source, Class<T> clazz )
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
- Object object = clazz.newInstance();
- @SuppressWarnings( "rawtypes" ) Schema schema = RuntimeSchema.getSchema( clazz );
- ProtostuffIOUtil.mergeFrom( source, object, schema );
+ T object = clazz.newInstance();
+ Schema<T> schema = getSchema( clazz );
+ mergeFrom( source, object, schema );
return object;
}
}
Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/Serializer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/Serializer.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/Serializer.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/Serializer.java Wed Feb 15 09:19:23 2012
@@ -24,10 +24,10 @@ import java.io.IOException;
public interface Serializer
{
- byte[] serialize( Object obj, @SuppressWarnings( { "rawtypes", "unchecked" } ) Class clazz )
+ <T> byte[] serialize( T obj )
throws IOException;
- Object deserialize( byte[] source, @SuppressWarnings( { "rawtypes", "unchecked" } ) Class clazz )
+ <T> T deserialize( byte[] source, Class<T> clazz )
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException;
-}
\ No newline at end of file
+}
Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/StandardSerializer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/StandardSerializer.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/StandardSerializer.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/serialization/StandardSerializer.java Wed Feb 15 09:19:23 2012
@@ -24,13 +24,16 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.io.Serializable;
public class StandardSerializer
implements Serializer
{
- public byte[] serialize( Object obj, @SuppressWarnings( { "rawtypes", "unchecked" } ) Class clazz )
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> byte[] serialize( T obj )
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -41,13 +44,18 @@ public class StandardSerializer
return baos.toByteArray();
}
- public Serializable deserialize( byte[] source, @SuppressWarnings( { "rawtypes", "unchecked" } ) Class clazz )
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <T> T deserialize( byte[] source, Class<T> clazz )
throws IOException, ClassNotFoundException
{
ByteArrayInputStream bis = new ByteArrayInputStream( source );
ObjectInputStream ois = new ObjectInputStream( bis );
- Serializable obj = (Serializable) ois.readObject();
+ T obj = clazz.cast( ois.readObject() );
ois.close();
return obj;
}
+
}
Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/DummyPojoSerializer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/DummyPojoSerializer.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/DummyPojoSerializer.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/DummyPojoSerializer.java Wed Feb 15 09:19:23 2012
@@ -19,6 +19,11 @@ package org.apache.directmemory.serializ
* under the License.
*/
+import static com.dyuproject.protostuff.LinkedBuffer.*;
+import static com.dyuproject.protostuff.runtime.RuntimeSchema.*;
+
+import static com.dyuproject.protostuff.ProtostuffIOUtil.*;
+
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
@@ -38,24 +43,22 @@ public final class DummyPojoSerializer
public DummyPojoSerializer()
{
- data = ProtostuffIOUtil.toByteArray( pojo, RuntimeSchema.getSchema( DummyPojo.class ),
- LinkedBuffer.allocate( 2048 ) );
+ data = toByteArray( pojo, getSchema( DummyPojo.class ),
+ allocate( 2048 ) );
}
@Override
- public Object deserialize( byte[] source, @SuppressWarnings( { "rawtypes", "unchecked" } ) Class clazz )
+ public <T> T deserialize( byte[] source, Class<T> clazz )
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
// testing puts only
- return pojo;
+ return (T) pojo;
}
@Override
- public byte[] serialize( Object obj, @SuppressWarnings( { "rawtypes", "unchecked" } ) Class clazz )
+ public <T> byte[] serialize( T obj )
throws IOException
{
-// byte[] ser = new byte[data.length];
-// System.arraycopy(data, 0, ser, 0, data.length);
return data;
}
Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/SerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/SerializerTest.java?rev=1244411&r1=1244410&r2=1244411&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/SerializerTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/serialization/test/SerializerTest.java Wed Feb 15 09:19:23 2012
@@ -64,7 +64,7 @@ public class SerializerTest
for ( int i = 0; i < howMany; i++ )
{
long split = stopWatch.start();
- final byte[] array = serializer.serialize( pojo, pojo.getClass() );
+ final byte[] array = serializer.serialize( pojo );
stopWatch.stop( split );
long split2 = stopWatch2.start();
DummyPojo check = (DummyPojo) serializer.deserialize( array, pojo.getClass() );