From c9e803796331b427619c2e0755b6ec00378cab88 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Wed, 17 May 2017 14:01:08 -0400 Subject: [PATCH] Cache options for CachingResultSetMapper too, added StaticCachingResultSetMapper and StaticCompilingResultSetMapper --- .../jdbc/CachingResultSetMapper.java | 122 +++++++++++++++++- .../jdbc/CachingRowToObjectMapper.java | 3 + .../jdbc/StaticCachingResultSetMapper.java | 28 ++++ .../jdbc/StaticCompilingResultSetMapper.java | 28 ++++ 4 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCachingResultSetMapper.java create mode 100644 beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCompilingResultSetMapper.java 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 8347695..98ce9b5 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 @@ -3,18 +3,132 @@ package com.moparisthebest.jdbc; import java.sql.ResultSet; import java.util.Calendar; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +/** + * Maps the same as ResultSetMapper except caches constructor and field mappings + * + * @see ResultSetMapper + */ public class CachingResultSetMapper extends ResultSetMapper { - protected final Map> cache = new HashMap>(); - - public CachingResultSetMapper(Calendar cal, int arrayMaxLength) { + protected final Map> 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) { super(cal, arrayMaxLength); + if (maxEntries > 0) { // we want a limited cache + final float loadFactor = 0.75f; // default for HashMaps + // if we set the initialCapacity this way, nothing should ever need re-sized + final int initialCapacity = ((int) Math.ceil(maxEntries / loadFactor)) + 1; + cache = new LinkedHashMap>(initialCapacity, loadFactor, true) { + @Override + protected boolean removeEldestEntry(final Map.Entry> eldest) { + return size() > maxEntries; + } + }; + } else + cache = new HashMap>(); } + /** + * 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); + } + + /** + * CachingResultSetMapper with unlimited cache + */ public CachingResultSetMapper() { - super(); + 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> 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> 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> 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) { + this(cal, arrayMaxLength, threadSafe ? + new ConcurrentHashMap>() + : + new HashMap>() + ); + } + + /** + * 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); } @Override 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 ea5b8a9..fb3b61d 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 @@ -8,6 +8,9 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Map; +/** + * Maps same as RowToObjectMapper except caches constructor and field mappings + */ public class CachingRowToObjectMapper extends RowToObjectMapper { protected final Map> cache; diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCachingResultSetMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCachingResultSetMapper.java new file mode 100644 index 0000000..ec2e867 --- /dev/null +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCachingResultSetMapper.java @@ -0,0 +1,28 @@ +package com.moparisthebest.jdbc; + +import java.util.Calendar; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Global for entire application, hopefully you know what you are doing. + */ +public class StaticCachingResultSetMapper extends CachingResultSetMapper { + + private static final Map> cache = new ConcurrentHashMap>(); + + public static final StaticCachingResultSetMapper instance = new StaticCachingResultSetMapper(); + + public StaticCachingResultSetMapper(final Calendar cal, final int arrayMaxLength) { + super(cal, arrayMaxLength, cache); + } + + public StaticCachingResultSetMapper(final int arrayMaxLength) { + super(arrayMaxLength, cache); + } + + public StaticCachingResultSetMapper() { + super(cache); + } + +} diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCompilingResultSetMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCompilingResultSetMapper.java new file mode 100644 index 0000000..1ec61b7 --- /dev/null +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/StaticCompilingResultSetMapper.java @@ -0,0 +1,28 @@ +package com.moparisthebest.jdbc; + +import java.util.Calendar; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Global for entire application, hopefully you know what you are doing. + */ +public class StaticCompilingResultSetMapper extends CompilingResultSetMapper { + + private static final Map> cache = new ConcurrentHashMap>(); + + public static final StaticCompilingResultSetMapper instance = new StaticCompilingResultSetMapper(); + + public StaticCompilingResultSetMapper(final Calendar cal, final int arrayMaxLength) { + super(cal, arrayMaxLength, cache); + } + + public StaticCompilingResultSetMapper(final int arrayMaxLength) { + super(arrayMaxLength, cache); + } + + public StaticCompilingResultSetMapper() { + super(cache); + } + +}