Cache constructor in CachingResultSetMapper too

This commit is contained in:
Travis Burtrum 2017-05-17 13:49:36 -04:00
parent f97ad82389
commit ccbbb3929e
3 changed files with 26 additions and 8 deletions

View File

@ -7,7 +7,7 @@ import java.util.Map;
public class CachingResultSetMapper extends ResultSetMapper { public class CachingResultSetMapper extends ResultSetMapper {
protected final Map<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping> cache = new HashMap<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping>(); protected final Map<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>> cache = new HashMap<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>>();
public CachingResultSetMapper(Calendar cal, int arrayMaxLength) { public CachingResultSetMapper(Calendar cal, int arrayMaxLength) {
super(cal, arrayMaxLength); super(cal, arrayMaxLength);

View File

@ -1,6 +1,7 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import java.lang.reflect.AccessibleObject; import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays; import java.util.Arrays;
@ -9,12 +10,14 @@ import java.util.Map;
public class CachingRowToObjectMapper<T> extends RowToObjectMapper<T> { public class CachingRowToObjectMapper<T> extends RowToObjectMapper<T> {
protected final Map<ResultSetKey, FieldMapping> cache; protected final Map<ResultSetKey, FieldMapping<T>> cache;
protected final ResultSetKey keys; protected final ResultSetKey keys;
public CachingRowToObjectMapper(final Map<ResultSetKey, FieldMapping> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType) { public CachingRowToObjectMapper(final Map<ResultSetKey, FieldMapping<?>> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType) {
super(resultSet, returnTypeClass, cal, mapValType); super(resultSet, returnTypeClass, cal, mapValType);
this.cache = cache; @SuppressWarnings("unchecked")
final Map<ResultSetKey, FieldMapping<T>> genericCache = (Map<ResultSetKey, FieldMapping<T>>) (Object) cache; // ridiculous ain't it?
this.cache = genericCache;
try { try {
keys = new ResultSetKey(super.getKeysFromResultSet(), _returnTypeClass); keys = new ResultSetKey(super.getKeysFromResultSet(), _returnTypeClass);
//System.out.printf("keys: %s\n", keys); //System.out.printf("keys: %s\n", keys);
@ -30,17 +33,20 @@ public class CachingRowToObjectMapper<T> extends RowToObjectMapper<T> {
@Override @Override
protected void getFieldMappings() throws SQLException { protected void getFieldMappings() throws SQLException {
FieldMapping fm = cache.get(keys); final FieldMapping<T> fm = cache.get(keys);
if (fm == null) { if (fm == null) {
//System.out.printf("cache miss, keys: %s\n", keys); //System.out.printf("cache miss, keys: %s\n", keys);
// generate and put into cache // generate and put into cache
super.getFieldMappings(); super.getFieldMappings();
cache.put(keys, new FieldMapping(_fields, _fieldTypes)); cache.put(keys, new FieldMapping<T>(_fields, _fieldTypes, resultSetConstructor, constructor));
} else { } else {
//System.out.printf("cache hit, keys: %s\n", keys); //System.out.printf("cache hit, keys: %s\n", keys);
// load from cache // load from cache
_fields = fm._fields; _fields = fm._fields;
_fieldTypes = fm._fieldTypes; _fieldTypes = fm._fieldTypes;
resultSetConstructor = fm.resultSetConstructor;
constructor = fm.constructor;
constructorLoaded = true;
} }
} }
@ -79,13 +85,17 @@ public class CachingRowToObjectMapper<T> extends RowToObjectMapper<T> {
} }
} }
static class FieldMapping { static class FieldMapping<T> {
public final AccessibleObject[] _fields; public final AccessibleObject[] _fields;
public final int[] _fieldTypes; public final int[] _fieldTypes;
public final boolean resultSetConstructor;
public final Constructor<? extends T> constructor;
private FieldMapping(AccessibleObject[] _fields, int[] _fieldTypes) { public FieldMapping(final AccessibleObject[] _fields, final int[] _fieldTypes, final boolean resultSetConstructor, final Constructor<? extends T> constructor) {
this._fields = _fields; this._fields = _fields;
this._fieldTypes = _fieldTypes; this._fieldTypes = _fieldTypes;
this.resultSetConstructor = resultSetConstructor;
this.constructor = constructor;
} }
} }
} }

View File

@ -68,6 +68,14 @@ public class CompilingRowToObjectMapper<T> extends RowToObjectMapper<T> {
} }
} }
// todo: generate/cache these to make it faster for Map and MapCollection? maybe just getKey and getVal methods instead
/*
@Override
public <E> E extractColumnValue(final int index, final Class<E> classType) throws SQLException {
return super.extractColumnValue(index, classType);
}
*/
@Override @Override
protected String[] getKeysFromResultSet() throws SQLException { protected String[] getKeysFromResultSet() throws SQLException {
if (keys == null) if (keys == null)