Permalink
Browse files

Notes on where to integrate custom converters.

  • Loading branch information...
olivergierke committed Jul 9, 2012
1 parent 78820a2 commit 51835b9a0feaa2eefb8e10195db3f3b92dcb71ee
Showing with 14 additions and 3 deletions.
  1. +14 −3 src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java
@@ -77,7 +77,7 @@ public void setGemfireInstantiators(Map<Class<?>, EntityInstantiator> gemfireIns
this.instantiators = new EntityInstantiators(gemfireInstantiators);
}
- /*
+ /*
* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
@@ -92,6 +92,8 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
*/
public Object fromData(Class<?> type, final PdxReader reader) {
+ // TODO: check for custom serializer (PDX)
+
final GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
GemfirePropertyValueProvider propertyValueProvider = new GemfirePropertyValueProvider(reader);
@@ -111,6 +113,8 @@ public void doWithPersistentProperty(GemfirePersistentProperty persistentPropert
return;
}
+ // TODO: check for custom serializer (Spring Converter - primitives)
+
Object value = reader.readField(persistentProperty.getName());
try {
@@ -130,15 +134,22 @@ public void doWithPersistentProperty(GemfirePersistentProperty persistentPropert
*/
public boolean toData(Object value, final PdxWriter writer) {
+ // TODO: check for custom serializer (PDX)
+
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass());
final BeanWrapper<PersistentEntity<Object, ?>, Object> wrapper = BeanWrapper.create(value, conversionService);
entity.doWithProperties(new PropertyHandler<GemfirePersistentProperty>() {
public void doWithPersistentProperty(GemfirePersistentProperty persistentProperty) {
+ // TODO: check for custom serializer (Spring Converter)
+
try {
- Object value = wrapper.getProperty(persistentProperty);
- writer.writeObject(persistentProperty.getName(), value);
+
+ Object propertyValue = wrapper.getProperty(persistentProperty);
+ // TODO: use the following to improve serialization performance
+ // writer.writeField(persistentProperty.getName(), propertyValue, (Class) persistentProperty.getType());
+ writer.writeObject(persistentProperty.getName(), propertyValue);
} catch (Exception e) {
throw new MappingException("Could not write value for property " + persistentProperty.toString(), e);
}

3 comments on commit 51835b9

@dturanski

This comment has been minimized.

Show comment Hide comment
@dturanski

dturanski Jul 9, 2012

Member

This is helpful. Do you and/or @markpollack recall what the use case was?

Member

dturanski replied Jul 9, 2012

This is helpful. Do you and/or @markpollack recall what the use case was?

@olivergierke

This comment has been minimized.

Show comment Hide comment
@olivergierke

olivergierke Jul 9, 2012

Owner

The main driver for this is that people might want to avoid the cost of accessing bean properties by reflection but rather call the appropriate methods themselves. You might also wanna have a look at the EntityInstantiators API to allow short circuiting the actual object instantiation in the read case. Currently it always uses the BeanWrapper. The Neo4j guys achieved a lot of performance improvements in customer projects manually instantiating a bean instead of doing it via reflection.

Owner

olivergierke replied Jul 9, 2012

The main driver for this is that people might want to avoid the cost of accessing bean properties by reflection but rather call the appropriate methods themselves. You might also wanna have a look at the EntityInstantiators API to allow short circuiting the actual object instantiation in the read case. Currently it always uses the BeanWrapper. The Neo4j guys achieved a lot of performance improvements in customer projects manually instantiating a bean instead of doing it via reflection.

@dturanski

This comment has been minimized.

Show comment Hide comment
@dturanski

dturanski Jul 27, 2012

Member

@olivergierke @markpollack I could use your input. For the custom PDX serializer piece, I'm thinking about injecting

private Map <Class<?>,PdxSerializer> customSerializers;

Then delegate if a custom serializer registered for the class. That seems pretty straightforward.

As I understand this requirement, Implementing spring converters at the field level is a bit more complicated. It seems like at a minimum we need to know the target type and the converter should be bound to the entity type/property name (e.g. dateOfBirth stored as String and converted to Date but not all Strings for the same entity are converted).

It seems like the core conversion framework has some ability to do this, e.g.,:
TypeDescriptor sourceType = new TypeDescriptor(persistentProperty.getField());
and override GenericConversionService.getConverter() GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) to determine if there is a match.

Thoughts?

Member

dturanski replied Jul 27, 2012

@olivergierke @markpollack I could use your input. For the custom PDX serializer piece, I'm thinking about injecting

private Map <Class<?>,PdxSerializer> customSerializers;

Then delegate if a custom serializer registered for the class. That seems pretty straightforward.

As I understand this requirement, Implementing spring converters at the field level is a bit more complicated. It seems like at a minimum we need to know the target type and the converter should be bound to the entity type/property name (e.g. dateOfBirth stored as String and converted to Date but not all Strings for the same entity are converted).

It seems like the core conversion framework has some ability to do this, e.g.,:
TypeDescriptor sourceType = new TypeDescriptor(persistentProperty.getField());
and override GenericConversionService.getConverter() GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) to determine if there is a match.

Thoughts?

Please sign in to comment.