Author: adamw
Date: 2008-06-24 05:09:09 -0400 (Tue, 24 Jun 2008)
New Revision: 85
Modified:
trunk/src/main/org/jboss/envers/configuration/CustomRevisionInfoGenerator.java
trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
trunk/src/main/org/jboss/envers/mapper/MapPropertyMapper.java
trunk/src/main/org/jboss/envers/mapper/MultiPropertyMapper.java
trunk/src/main/org/jboss/envers/mapper/SinglePropertyMapper.java
trunk/src/main/org/jboss/envers/mapper/id/EmbeddedIdMapper.java
trunk/src/main/org/jboss/envers/mapper/id/SingleIdMapper.java
trunk/src/main/org/jboss/envers/mapper/id/relation/OneToManyIdMapper.java
trunk/src/main/org/jboss/envers/mapper/id/relation/OneToOneIdMapper.java
trunk/src/main/org/jboss/envers/mapper/id/relation/ToOneIdMapper.java
trunk/src/main/org/jboss/envers/reflection/ReflectionTools.java
Log:
ENVERS-21: getter and setter cache
Modified: trunk/src/main/org/jboss/envers/configuration/CustomRevisionInfoGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/CustomRevisionInfoGenerator.java 2008-06-24
08:43:49 UTC (rev 84)
+++
trunk/src/main/org/jboss/envers/configuration/CustomRevisionInfoGenerator.java 2008-06-24
09:09:09 UTC (rev 85)
@@ -39,7 +39,7 @@
Class<? extends RevisionListener>
listenerClass) {
this.entityClass = entityClass;
- revisionTimestampSetter =
ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(entityClass,
+ revisionTimestampSetter = ReflectionTools.getSetter(entityClass,
verEntCfg.getRevisionsInfoTimestampName());
if (!listenerClass.equals(RevisionListener.class)) {
Modified: trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-06-24
08:43:49 UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-06-24
09:09:09 UTC (rev 85)
@@ -151,7 +151,7 @@
versionsMetaGen.generateSecondPass(pc, pcDatas.get(pc), document);
//TODO
- writeDocument(document);
+ //writeDocument(document);
try {
cfg.addDocument(writer.write(document));
@@ -174,7 +174,7 @@
if (!verEntCfg.hasCustomRevisionInfoEntity()) {
RevisionsInfoMetadataGenerator revisionsMetaGenInfo = new
RevisionsInfoMetadataGenerator(verEntCfg);
Document helperEntity = revisionsMetaGenInfo.generate();
- writeDocument(helperEntity);
+ //writeDocument(helperEntity);
cfg.addDocument(writer.write(helperEntity));
}
} catch (DocumentException e) {
Modified: trunk/src/main/org/jboss/envers/mapper/MapPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/MapPropertyMapper.java 2008-06-24 08:43:49 UTC
(rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/MapPropertyMapper.java 2008-06-24 09:09:09 UTC
(rev 85)
@@ -22,12 +22,11 @@
package org.jboss.envers.mapper;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.reflection.ReflectionTools;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.exception.VersionsException;
import org.hibernate.property.Getter;
import org.hibernate.property.Setter;
-import org.hibernate.property.PropertyAccessor;
-import org.hibernate.property.BasicPropertyAccessor;
import org.hibernate.util.ReflectHelper;
import java.util.Map;
@@ -37,8 +36,6 @@
* @author Adam Warski (adam at warski dot org)
*/
public class MapPropertyMapper implements CompositePropertyMapperBuilder {
- protected static final PropertyAccessor BASIC_PROPERTY_ACCESSOR = new
BasicPropertyAccessor();
-
private String propertyName;
private ExtendedPropertyMapper delegate;
@@ -71,8 +68,8 @@
return;
}
- Getter getter = ReflectHelper.getGetter(obj.getClass(), propertyName);
- Setter setter = BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(), propertyName);
+ Getter getter = ReflectionTools.getGetter(obj.getClass(), propertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
try {
Object subObj =
ReflectHelper.getDefaultConstructor(getter.getReturnType()).newInstance();
Modified: trunk/src/main/org/jboss/envers/mapper/MultiPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/MultiPropertyMapper.java 2008-06-24 08:43:49
UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/MultiPropertyMapper.java 2008-06-24 09:09:09
UTC (rev 85)
@@ -21,10 +21,10 @@
*/
package org.jboss.envers.mapper;
-import org.hibernate.util.ReflectHelper;
import org.hibernate.property.Getter;
import org.hibernate.MappingException;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.reflection.ReflectionTools;
import org.jboss.envers.reader.VersionsReaderImplementor;
import java.util.Map;
@@ -32,7 +32,6 @@
/**
* @author Adam Warski (adam at warski dot org)
- * TODO: eliminate reflection on each call
*/
public class MultiPropertyMapper implements ExtendedPropertyMapper {
protected Map<String, PropertyMapper> properties;
@@ -79,24 +78,21 @@
return ret;
}
- // TODO: fix wrt to nulls and reflection
public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
boolean ret = false;
for (String propertyName : properties.keySet()) {
Getter getter;
if (newObj != null) {
- getter = ReflectHelper.getGetter(newObj.getClass(), propertyName);
+ getter = ReflectionTools.getGetter(newObj.getClass(), propertyName);
} else if (oldObj != null) {
- getter = ReflectHelper.getGetter(oldObj.getClass(), propertyName);
+ getter = ReflectionTools.getGetter(oldObj.getClass(), propertyName);
} else {
return false;
}
- if (getter != null) {
- ret |= properties.get(propertyName).mapToMapFromEntity(data,
- newObj == null ? null : getter.get(newObj),
- oldObj == null ? null : getter.get(oldObj));
- }
+ ret |= properties.get(propertyName).mapToMapFromEntity(data,
+ newObj == null ? null : getter.get(newObj),
+ oldObj == null ? null : getter.get(oldObj));
}
return ret;
Modified: trunk/src/main/org/jboss/envers/mapper/SinglePropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/SinglePropertyMapper.java 2008-06-24 08:43:49
UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/SinglePropertyMapper.java 2008-06-24 09:09:09
UTC (rev 85)
@@ -62,7 +62,7 @@
return;
}
- Setter setter = ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(),
propertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
setter.set(obj, data.get(propertyName), null);
}
Modified: trunk/src/main/org/jboss/envers/mapper/id/EmbeddedIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/id/EmbeddedIdMapper.java 2008-06-24 08:43:49
UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/id/EmbeddedIdMapper.java 2008-06-24 09:09:09
UTC (rev 85)
@@ -55,7 +55,7 @@
return;
}
- Getter getter = ReflectHelper.getGetter(obj.getClass(), idPropertyName);
+ Getter getter = ReflectionTools.getGetter(obj.getClass(), idPropertyName);
mapToMapFromId(data, getter.get(obj));
}
@@ -64,8 +64,8 @@
return;
}
- Getter getter = ReflectHelper.getGetter(obj.getClass(), idPropertyName);
- Setter setter = ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(),
idPropertyName);
+ Getter getter = ReflectionTools.getGetter(obj.getClass(), idPropertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), idPropertyName);
try {
Object subObj =
ReflectHelper.getDefaultConstructor(getter.getReturnType()).newInstance();
@@ -94,7 +94,7 @@
return null;
}
- Getter getter = ReflectHelper.getGetter(data.getClass(), idPropertyName);
+ Getter getter = ReflectionTools.getGetter(data.getClass(), idPropertyName);
return getter.get(data);
}
Modified: trunk/src/main/org/jboss/envers/mapper/id/SingleIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/id/SingleIdMapper.java 2008-06-24 08:43:49 UTC
(rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/id/SingleIdMapper.java 2008-06-24 09:09:09 UTC
(rev 85)
@@ -26,7 +26,6 @@
import org.jboss.envers.exception.VersionsException;
import org.hibernate.property.Setter;
import org.hibernate.property.Getter;
-import org.hibernate.util.ReflectHelper;
import java.util.Map;
import java.util.Set;
@@ -66,7 +65,7 @@
return;
}
- Setter setter = ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(),
beanPropertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), beanPropertyName);
setter.set(obj, data.get(propertyName), null);
}
@@ -83,7 +82,7 @@
return null;
}
- Getter getter = ReflectHelper.getGetter(data.getClass(), beanPropertyName);
+ Getter getter = ReflectionTools.getGetter(data.getClass(), beanPropertyName);
return getter.get(data);
}
@@ -97,7 +96,7 @@
if (obj == null) {
data.put(propertyName, null);
} else {
- Getter getter = ReflectHelper.getGetter(obj.getClass(), beanPropertyName);
+ Getter getter = ReflectionTools.getGetter(obj.getClass(), beanPropertyName);
data.put(propertyName, getter.get(obj));
}
}
@@ -107,8 +106,8 @@
return;
}
- Getter getter = ReflectHelper.getGetter(objFrom.getClass(), beanPropertyName);
- Setter setter =
ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(objTo.getClass(), beanPropertyName);
+ Getter getter = ReflectionTools.getGetter(objFrom.getClass(), beanPropertyName);
+ Setter setter = ReflectionTools.getSetter(objTo.getClass(), beanPropertyName);
setter.set(objTo, getter.get(objFrom), null);
}
Modified: trunk/src/main/org/jboss/envers/mapper/id/relation/OneToManyIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/id/relation/OneToManyIdMapper.java 2008-06-24
08:43:49 UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/id/relation/OneToManyIdMapper.java 2008-06-24
09:09:09 UTC (rev 85)
@@ -30,7 +30,6 @@
import org.jboss.envers.reflection.ReflectionTools;
import org.hibernate.property.Setter;
import org.hibernate.property.Getter;
-import org.hibernate.util.ReflectHelper;
import java.util.*;
@@ -64,7 +63,7 @@
Class<?> entityClass = ReflectionTools.loadClass(owningEntityName);
- Getter getter = ReflectHelper.getGetter(obj.getClass(), propertyName);
+ Getter getter = ReflectionTools.getGetter(obj.getClass(), propertyName);
Class collectionClass = getter.getReturnType();
// todo: investigate generics
@@ -79,7 +78,7 @@
throw new VersionsException("Unsupported versioned collection type:
" + collectionClass.getName());
}
- Setter setter = ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(),
propertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
setter.set(obj, value, null);
}
}
\ No newline at end of file
Modified: trunk/src/main/org/jboss/envers/mapper/id/relation/OneToOneIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/id/relation/OneToOneIdMapper.java 2008-06-24
08:43:49 UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/id/relation/OneToOneIdMapper.java 2008-06-24
09:09:09 UTC (rev 85)
@@ -60,7 +60,7 @@
Object value = versionsReader.findOneReferencing(entityClass, owningEntityName,
owningReferencePropertyName,
primaryKey, revision);
- Setter setter = ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(),
propertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
setter.set(obj, value, null);
}
}
Modified: trunk/src/main/org/jboss/envers/mapper/id/relation/ToOneIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/mapper/id/relation/ToOneIdMapper.java 2008-06-24
08:43:49 UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/mapper/id/relation/ToOneIdMapper.java 2008-06-24
09:09:09 UTC (rev 85)
@@ -76,7 +76,7 @@
createProxy(null, new ToOneDelegateSessionImplementor(versionsReader,
entityClass, entityId, revision));
}
- Setter setter = ReflectionTools.BASIC_PROPERTY_ACCESSOR.getSetter(obj.getClass(),
propertyName);
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
setter.set(obj, value, null);
}
}
Modified: trunk/src/main/org/jboss/envers/reflection/ReflectionTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/reflection/ReflectionTools.java 2008-06-24 08:43:49
UTC (rev 84)
+++ trunk/src/main/org/jboss/envers/reflection/ReflectionTools.java 2008-06-24 09:09:09
UTC (rev 85)
@@ -23,14 +23,32 @@
import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.BasicPropertyAccessor;
+import org.hibernate.property.Getter;
+import org.hibernate.property.Setter;
+import org.hibernate.util.ReflectHelper;
import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.tools.Pair;
+import org.jboss.envers.tools.ConcurrentReferenceHashMap;
+import java.util.Map;
+
+import static org.jboss.envers.tools.Pair.make;
+
/**
* @author Adam Warski (adam at warski dot org)
*/
-public class ReflectionTools {
- public static final PropertyAccessor BASIC_PROPERTY_ACCESSOR = new
BasicPropertyAccessor();
+public class ReflectionTools {
+ private static final Map<Pair<Class, String>, Getter> getterCache =
+ new ConcurrentReferenceHashMap<Pair<Class, String>, Getter>(10,
+ ConcurrentReferenceHashMap.ReferenceType.SOFT,
+ ConcurrentReferenceHashMap.ReferenceType.STRONG);
+ private static final Map<Pair<Class, String>, Setter> setterCache =
+ new ConcurrentReferenceHashMap<Pair<Class, String>, Setter>(10,
+ ConcurrentReferenceHashMap.ReferenceType.SOFT,
+ ConcurrentReferenceHashMap.ReferenceType.STRONG);
+ private static final PropertyAccessor BASIC_PROPERTY_ACCESSOR = new
BasicPropertyAccessor();
+
public static Class<?> loadClass(String name) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name);
@@ -38,4 +56,28 @@
throw new VersionsException(e);
}
}
+
+ public static Getter getGetter(Class cls, String propertyName) {
+ Pair<Class, String> key = make(cls, propertyName);
+ Getter value = getterCache.get(key);
+ if (value == null) {
+ value = ReflectHelper.getGetter(cls, propertyName);
+ // It's ok if two getter are generated concurrently
+ getterCache.put(key, value);
+ }
+
+ return value;
+ }
+
+ public static Setter getSetter(Class cls, String propertyName) {
+ Pair<Class, String> key = make(cls, propertyName);
+ Setter value = setterCache.get(key);
+ if (value == null) {
+ value = BASIC_PROPERTY_ACCESSOR.getSetter(cls, propertyName);
+ // It's ok if two setters are generated concurrently
+ setterCache.put(key, value);
+ }
+
+ return value;
+ }
}