OPENJPA-703: Use template for QueryStatistics.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@745123 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-02-17 15:41:28 +00:00
parent 2582d4de0b
commit 556e09b69f
4 changed files with 23 additions and 23 deletions

View File

@ -57,7 +57,7 @@ public class PreparedQueryCacheImpl implements PreparedQueryCache {
// Key: Query identifier Value: Reason why excluded // Key: Query identifier Value: Reason why excluded
private final Map<String, String> _uncachables; private final Map<String, String> _uncachables;
private List<String> _exclusionPatterns; private List<String> _exclusionPatterns;
private final QueryStatistics _stats; private final QueryStatistics<String> _stats;
private ReentrantLock _lock = new ReentrantLock(); private ReentrantLock _lock = new ReentrantLock();
private Log _log; private Log _log;
private Localizer _loc = Localizer.forPackage(PreparedQueryCacheImpl.class); private Localizer _loc = Localizer.forPackage(PreparedQueryCacheImpl.class);
@ -65,7 +65,7 @@ public class PreparedQueryCacheImpl implements PreparedQueryCache {
public PreparedQueryCacheImpl() { public PreparedQueryCacheImpl() {
_delegate = new HashMap<String, PreparedQuery>(); _delegate = new HashMap<String, PreparedQuery>();
_uncachables = new HashMap<String, String>(); _uncachables = new HashMap<String, String>();
_stats = new QueryStatistics.Default(); _stats = new QueryStatistics.Default<String>();
} }
public Boolean register(String id, Query query, FetchConfiguration hints) { public Boolean register(String id, Query query, FetchConfiguration hints) {
@ -264,7 +264,7 @@ public class PreparedQueryCacheImpl implements PreparedQueryCache {
} }
} }
public QueryStatistics getStatistics() { public QueryStatistics<String> getStatistics() {
return _stats; return _stats;
} }

View File

@ -188,5 +188,5 @@ public interface PreparedQueryCache extends Configurable {
/** /**
* Gets the simple statistics for executed queries. * Gets the simple statistics for executed queries.
*/ */
public QueryStatistics getStatistics(); public QueryStatistics<String> getStatistics();
} }

View File

@ -37,13 +37,13 @@ import java.util.Map;
* @author Pinaki Poddar * @author Pinaki Poddar
* *
*/ */
public interface QueryStatistics extends Serializable { public interface QueryStatistics<T> extends Serializable {
/** /**
* Record that the given query has been executed. The boolean parameter * Record that the given query has been executed. The boolean parameter
* designates whether the executed query is a cached version. * designates whether the executed query is a cached version.
*/ */
void recordExecution(String query, boolean cached); void recordExecution(T query, boolean cached);
/** /**
* Gets number of total query execution since last reset. * Gets number of total query execution since last reset.
@ -58,12 +58,12 @@ public interface QueryStatistics extends Serializable {
/** /**
* Gets number of executions for the given query since last reset. * Gets number of executions for the given query since last reset.
*/ */
public long getExecutionCount(String query); public long getExecutionCount(T query);
/** /**
* Gets number of executions for the given query since start. * Gets number of executions for the given query since start.
*/ */
public long getTotalExecutionCount(String query); public long getTotalExecutionCount(T query);
/** /**
* Gets number of total query execution that are cached since last reset. * Gets number of total query execution that are cached since last reset.
@ -79,13 +79,13 @@ public interface QueryStatistics extends Serializable {
* Gets number of executions for the given query that are cached since * Gets number of executions for the given query that are cached since
* last reset. * last reset.
*/ */
public long getHitCount(String query); public long getHitCount(T query);
/** /**
* Gets number of executions for the given query that are cached since * Gets number of executions for the given query that are cached since
* start. * start.
*/ */
public long getTotalHitCount(String query); public long getTotalHitCount(T query);
/** /**
* Gets the time of last reset. * Gets the time of last reset.
@ -111,12 +111,12 @@ public interface QueryStatistics extends Serializable {
* A default implementation. * A default implementation.
* *
*/ */
public static class Default implements QueryStatistics { public static class Default<T> implements QueryStatistics<T> {
private static final int ARRAY_SIZE = 2; private static final int ARRAY_SIZE = 2;
private long[] astat = new long[ARRAY_SIZE]; private long[] astat = new long[ARRAY_SIZE];
private long[] stat = new long[ARRAY_SIZE]; private long[] stat = new long[ARRAY_SIZE];
private Map<String, long[]> stats = new HashMap<String, long[]>(); private Map<T, long[]> stats = new HashMap<T, long[]>();
private Map<String, long[]> astats = new HashMap<String, long[]>(); private Map<T, long[]> astats = new HashMap<T, long[]>();
private Date start = new Date(); private Date start = new Date();
private Date since = start; private Date since = start;
@ -131,11 +131,11 @@ public interface QueryStatistics extends Serializable {
return astat[READ]; return astat[READ];
} }
public long getExecutionCount(String query) { public long getExecutionCount(T query) {
return getCount(stats, query, READ); return getCount(stats, query, READ);
} }
public long getTotalExecutionCount(String query) { public long getTotalExecutionCount(T query) {
return getCount(astats, query, READ); return getCount(astats, query, READ);
} }
@ -147,15 +147,15 @@ public interface QueryStatistics extends Serializable {
return astat[HIT]; return astat[HIT];
} }
public long getHitCount(String query) { public long getHitCount(T query) {
return getCount(stats, query, HIT); return getCount(stats, query, HIT);
} }
public long getTotalHitCount(String query) { public long getTotalHitCount(T query) {
return getCount(astats, query, HIT); return getCount(astats, query, HIT);
} }
private long getCount(Map<String, long[]> target, String query, int i) { private long getCount(Map<T, long[]> target, T query, int i) {
long[] row = target.get(query); long[] row = target.get(query);
return (row == null) ? 0 : row[i]; return (row == null) ? 0 : row[i];
} }
@ -174,14 +174,14 @@ public interface QueryStatistics extends Serializable {
since = new Date(); since = new Date();
} }
private void addSample(String query, int index) { private void addSample(T query, int index) {
stat[index]++; stat[index]++;
astat[index]++; astat[index]++;
addSample(stats, query, index); addSample(stats, query, index);
addSample(astats, query, index); addSample(astats, query, index);
} }
private void addSample(Map<String, long[]> target, String query, int i) { private void addSample(Map<T, long[]> target, T query, int i) {
long[] row = target.get(query); long[] row = target.get(query);
if (row == null) { if (row == null) {
row = new long[ARRAY_SIZE]; row = new long[ARRAY_SIZE];
@ -190,7 +190,7 @@ public interface QueryStatistics extends Serializable {
target.put(query, row); target.put(query, row);
} }
public void recordExecution(String query, boolean cached) { public void recordExecution(T query, boolean cached) {
addSample(query, READ); addSample(query, READ);
if (cached) if (cached)
addSample(query, HIT); addSample(query, HIT);
@ -210,7 +210,7 @@ public interface QueryStatistics extends Serializable {
out.println("\tSince Start \tSince Reset \t\tQuery"); out.println("\tSince Start \tSince Reset \t\tQuery");
} }
int i = 0; int i = 0;
for (String key : stats.keySet()) { for (T key : stats.keySet()) {
i++; i++;
long[] arow = astats.get(key); long[] arow = astats.get(key);
if (since == start) { if (since == start) {

View File

@ -587,7 +587,7 @@ public class QueryImpl implements OpenJPAQuerySPI, Serializable {
Boolean registered = cache.register(_id, _query, fetch); Boolean registered = cache.register(_id, _query, fetch);
boolean alreadyCached = (registered == null); boolean alreadyCached = (registered == null);
String lang = _query.getLanguage(); String lang = _query.getLanguage();
QueryStatistics stats = cache.getStatistics(); QueryStatistics<String> stats = cache.getStatistics();
if (alreadyCached && LANG_PREPARED_SQL.equals(lang)) { if (alreadyCached && LANG_PREPARED_SQL.equals(lang)) {
PreparedQuery pq = _em.getPreparedQuery(_id); PreparedQuery pq = _em.getPreparedQuery(_id);
if (pq.isInitialized()) { if (pq.isInitialized()) {