From ccbbb3929ed47228b6796bae27d63e09e5f4447d Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Wed, 17 May 2017 13:49:36 -0400 Subject: [PATCH] Cache constructor in CachingResultSetMapper too --- .../jdbc/CachingResultSetMapper.java | 2 +- .../jdbc/CachingRowToObjectMapper.java | 24 +++++++++++++------ .../jdbc/CompilingRowToObjectMapper.java | 8 +++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingResultSetMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingResultSetMapper.java index e8aa30a..8347695 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingResultSetMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingResultSetMapper.java @@ -7,7 +7,7 @@ import java.util.Map; public class CachingResultSetMapper extends ResultSetMapper { - protected final Map cache = new HashMap(); + protected final Map> cache = new HashMap>(); public CachingResultSetMapper(Calendar cal, int arrayMaxLength) { super(cal, arrayMaxLength); diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingRowToObjectMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingRowToObjectMapper.java index b867e3c..ea5b8a9 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingRowToObjectMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingRowToObjectMapper.java @@ -1,6 +1,7 @@ package com.moparisthebest.jdbc; import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Constructor; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; @@ -9,12 +10,14 @@ import java.util.Map; public class CachingRowToObjectMapper extends RowToObjectMapper { - protected final Map cache; + protected final Map> cache; protected final ResultSetKey keys; - public CachingRowToObjectMapper(final Map cache, ResultSet resultSet, Class returnTypeClass, Calendar cal, Class mapValType) { + public CachingRowToObjectMapper(final Map> cache, ResultSet resultSet, Class returnTypeClass, Calendar cal, Class mapValType) { super(resultSet, returnTypeClass, cal, mapValType); - this.cache = cache; + @SuppressWarnings("unchecked") + final Map> genericCache = (Map>) (Object) cache; // ridiculous ain't it? + this.cache = genericCache; try { keys = new ResultSetKey(super.getKeysFromResultSet(), _returnTypeClass); //System.out.printf("keys: %s\n", keys); @@ -30,17 +33,20 @@ public class CachingRowToObjectMapper extends RowToObjectMapper { @Override protected void getFieldMappings() throws SQLException { - FieldMapping fm = cache.get(keys); + final FieldMapping fm = cache.get(keys); if (fm == null) { //System.out.printf("cache miss, keys: %s\n", keys); // generate and put into cache super.getFieldMappings(); - cache.put(keys, new FieldMapping(_fields, _fieldTypes)); + cache.put(keys, new FieldMapping(_fields, _fieldTypes, resultSetConstructor, constructor)); } else { //System.out.printf("cache hit, keys: %s\n", keys); // load from cache _fields = fm._fields; _fieldTypes = fm._fieldTypes; + resultSetConstructor = fm.resultSetConstructor; + constructor = fm.constructor; + constructorLoaded = true; } } @@ -79,13 +85,17 @@ public class CachingRowToObjectMapper extends RowToObjectMapper { } } - static class FieldMapping { + static class FieldMapping { public final AccessibleObject[] _fields; public final int[] _fieldTypes; + public final boolean resultSetConstructor; + public final Constructor constructor; - private FieldMapping(AccessibleObject[] _fields, int[] _fieldTypes) { + public FieldMapping(final AccessibleObject[] _fields, final int[] _fieldTypes, final boolean resultSetConstructor, final Constructor constructor) { this._fields = _fields; this._fieldTypes = _fieldTypes; + this.resultSetConstructor = resultSetConstructor; + this.constructor = constructor; } } } diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java index 5e58086..876d6ff 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java @@ -68,6 +68,14 @@ public class CompilingRowToObjectMapper extends RowToObjectMapper { } } + // todo: generate/cache these to make it faster for Map and MapCollection? maybe just getKey and getVal methods instead + /* + @Override + public E extractColumnValue(final int index, final Class classType) throws SQLException { + return super.extractColumnValue(index, classType); + } + */ + @Override protected String[] getKeysFromResultSet() throws SQLException { if (keys == null)