2014-04-22 14:40:18 -04:00
|
|
|
package com.moparisthebest.jdbc;
|
|
|
|
|
2017-06-18 21:58:55 -04:00
|
|
|
import com.moparisthebest.jdbc.util.CacheUtil;
|
|
|
|
|
2014-04-22 14:40:18 -04:00
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.util.Calendar;
|
2017-05-16 12:32:34 -04:00
|
|
|
import java.util.HashMap;
|
2017-05-17 14:01:08 -04:00
|
|
|
import java.util.LinkedHashMap;
|
2017-05-16 12:32:34 -04:00
|
|
|
import java.util.Map;
|
2017-05-17 14:01:08 -04:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2014-04-22 14:40:18 -04:00
|
|
|
|
2017-05-17 14:01:08 -04:00
|
|
|
/**
|
|
|
|
* Maps the same as ResultSetMapper except caches constructor and field mappings
|
|
|
|
*
|
|
|
|
* @see ResultSetMapper
|
|
|
|
*/
|
2014-04-22 14:40:18 -04:00
|
|
|
public class CachingResultSetMapper extends ResultSetMapper {
|
|
|
|
|
2017-05-17 14:01:08 -04:00
|
|
|
protected final Map<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>> cache;
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with optional maxEntries, expiring old ones in LRU fashion
|
|
|
|
*
|
|
|
|
* @param cal optional calendar for date/time values
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
* @param maxEntries max cached compiled entries to keep in cache, < 1 means unlimited
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final Calendar cal, final int arrayMaxLength, final int maxEntries) {
|
2014-04-22 14:40:18 -04:00
|
|
|
super(cal, arrayMaxLength);
|
2017-06-18 21:58:55 -04:00
|
|
|
cache = CacheUtil.getCache(maxEntries);
|
2017-05-17 14:01:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with unlimited cache
|
|
|
|
*
|
|
|
|
* @param cal optional calendar for date/time values
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final Calendar cal, final int arrayMaxLength) {
|
|
|
|
this(cal, arrayMaxLength, 0); // default unlimited cache
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with unlimited cache
|
|
|
|
*
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final int arrayMaxLength) {
|
|
|
|
this(null, arrayMaxLength);
|
2014-04-22 14:40:18 -04:00
|
|
|
}
|
|
|
|
|
2017-05-17 14:01:08 -04:00
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with unlimited cache
|
|
|
|
*/
|
2014-04-22 14:40:18 -04:00
|
|
|
public CachingResultSetMapper() {
|
2017-05-17 14:01:08 -04:00
|
|
|
this(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with custom cache implementation
|
|
|
|
*
|
|
|
|
* @param cal optional calendar for date/time values
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
* @param cache any Map implementation for cache you wish, does not need to handle null keys or values
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final Calendar cal, final int arrayMaxLength, final Map<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>> cache) {
|
|
|
|
super(cal, arrayMaxLength);
|
|
|
|
if (cache == null)
|
|
|
|
throw new IllegalArgumentException("cache cannot be null");
|
|
|
|
this.cache = cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with custom cache implementation
|
|
|
|
*
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
* @param cache any Map implementation for cache you wish, does not need to handle null keys or values
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final int arrayMaxLength, final Map<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>> cache) {
|
|
|
|
this(null, arrayMaxLength, cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with custom cache implementation
|
|
|
|
*
|
|
|
|
* @param cache any Map implementation for cache you wish, does not need to handle null keys or values
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final Map<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>> cache) {
|
|
|
|
this(-1, cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with optionally threadSafe cache implementation
|
|
|
|
*
|
|
|
|
* @param cal optional calendar for date/time values
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
* @param threadSafe true uses a thread-safe cache implementation (currently ConcurrentHashMap), false uses regular HashMap
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final Calendar cal, final int arrayMaxLength, final boolean threadSafe) {
|
2017-06-18 21:58:55 -04:00
|
|
|
this(cal, arrayMaxLength, CacheUtil.<CachingRowToObjectMapper.ResultSetKey, CachingRowToObjectMapper.FieldMapping<?>>getCache(threadSafe));
|
2017-05-17 14:01:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with optionally threadSafe cache implementation
|
|
|
|
*
|
|
|
|
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
|
|
|
|
* @param threadSafe true uses a thread-safe cache implementation (currently ConcurrentHashMap), false uses regular HashMap
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final int arrayMaxLength, final boolean threadSafe) {
|
|
|
|
this(null, arrayMaxLength, threadSafe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CachingResultSetMapper with optionally threadSafe cache implementation
|
|
|
|
*
|
|
|
|
* @param threadSafe true uses a thread-safe cache implementation (currently ConcurrentHashMap), false uses regular HashMap
|
|
|
|
*/
|
|
|
|
public CachingResultSetMapper(final boolean threadSafe) {
|
|
|
|
this(-1, threadSafe);
|
2014-04-22 14:40:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-18 14:26:23 -04:00
|
|
|
public <K, T> RowMapper<K, T> getRowMapper(ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType, Class<K> mapKeyType) {
|
2017-05-17 17:32:48 -04:00
|
|
|
return new CachingRowToObjectMapper<K, T>(cache, resultSet, returnTypeClass, cal, mapValType, mapKeyType);
|
2014-04-22 14:40:18 -04:00
|
|
|
}
|
|
|
|
}
|