Author: ppoddar
Date: Fri Jan 30 22:06:30 2009
New Revision: 739425
URL: http://svn.apache.org/viewvc?rev=739425&view=rev
Log:
OPENJPA-703: Cleaning first Prepared Query Cache commit
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/rop/ResultList.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java?rev=739425&r1=739424&r2=739425&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
Fri Jan 30 22:06:30 2009
@@ -29,19 +29,14 @@
import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.conf.OpenJPAConfiguration;
-import org.apache.openjpa.jdbc.sql.SQLBuffer;
-import org.apache.openjpa.jdbc.sql.SelectExecutor;
import org.apache.openjpa.kernel.FetchConfiguration;
import org.apache.openjpa.kernel.PreparedQuery;
import org.apache.openjpa.kernel.PreparedQueryCache;
import org.apache.openjpa.kernel.Query;
import org.apache.openjpa.kernel.QueryHints;
import org.apache.openjpa.kernel.QueryStatistics;
-import org.apache.openjpa.kernel.QueryStatistics.Default;
import org.apache.openjpa.lib.conf.Configuration;
import org.apache.openjpa.lib.log.Log;
-import org.apache.openjpa.lib.log.LogFactory;
-import org.apache.openjpa.lib.rop.ResultList;
import org.apache.openjpa.lib.util.Localizer;
/**
@@ -49,7 +44,8 @@
*
* @author Pinaki Poddar
*
- * @since 1.3.0
+ * @since 2.0.0
+ *
* @nojavadoc
*/
public class PreparedQueryCacheImpl implements PreparedQueryCache {
@@ -101,8 +97,6 @@
}
}
-
-
/**
* Cache the given query keyed by its identifier. Does not cache if the
* identifier matches any exclusion pattern or has been marked as
@@ -120,13 +114,13 @@
}
String pattern = getMatchedExclusionPattern(id);
if (pattern != null) {
- markUncachable(q.getIdentifier(), pattern);
+ markUncachable(id, pattern);
return false;
}
- if (_log != null && _log.isTraceEnabled())
- _log.trace(_loc.get("prepared-query-cache",
q.getIdentifier(),
- q.getTargetQuery()));
- _delegate.put(q.getIdentifier(), q);
+ _delegate.put(id, q);
+ if (_log != null && _log.isTraceEnabled())
+ _log.trace(_loc.get("prepared-query-cached", id,
+ q.getTargetQuery()));
return true;
} finally {
unlock();
@@ -145,7 +139,6 @@
}
return pq;
}
-
public boolean invalidate(String id) {
lock();
@@ -288,17 +281,12 @@
* Gets the pattern that matches the given identifier.
*/
private String getMatchedExclusionPattern(String id) {
- lock();
- try {
- if (_exclusionPatterns == null ||
_exclusionPatterns.isEmpty())
- return null;
- for (String pattern : _exclusionPatterns)
- if (matches(pattern, id))
- return pattern;
+ if (_exclusionPatterns == null || _exclusionPatterns.isEmpty())
return null;
- } finally {
- unlock();
- }
+ for (String pattern : _exclusionPatterns)
+ if (matches(pattern, id))
+ return pattern;
+ return null;
}
/**
@@ -306,7 +294,7 @@
*/
private Collection<String> getMatchedKeys(String pattern,
Map<String,String> map) {
- List<String> result = new ArrayList<String>();
+ List<String> result = new ArrayList<String>();
for (Map.Entry<String, String> entry : map.entrySet()) {
if (matches(pattern, entry.getValue())) {
result.add(entry.getKey());
@@ -350,9 +338,7 @@
Object result = fetch.getHint(hint);
return result != null && "true".equalsIgnoreCase(result.toString());
}
-
-
-
+
//-------------------------------------------------------
// Configurable contract
//-------------------------------------------------------
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java?rev=739425&r1=739424&r2=739425&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
Fri Jan 30 22:06:30 2009
@@ -30,6 +30,12 @@
import org.apache.openjpa.kernel.Query;
import org.apache.openjpa.lib.rop.ResultList;
+/**
+ * Implements {...@link PreparedQuery} for SQL queries.
+ *
+ * @author Pinaki Poddar
+ *
+ */
public class PreparedQueryImpl implements PreparedQuery {
private final String _id;
private String _sql;
@@ -41,7 +47,8 @@
// Parameters of the query
private List _params;
- private List _userParams;
+ // Position of the user defined parameters in the _params list
+ private Map<Object, int[]> _userParamPositions;
/**
@@ -75,6 +82,10 @@
return _id;
}
+ /**
+ * Get the original query string which is same as the identifier of this
+ * receiver.
+ */
public String getOriginalQuery() {
return getIdentifier();
}
@@ -83,7 +94,7 @@
return _sql;
}
- public void setDatastoreAction(String sql) {
+ void setTargetQuery(String sql) {
_sql = sql;
}
@@ -96,6 +107,12 @@
q.setCandidateType(_candidate, _subclasses);
}
+ /**
+ * Initialize this receiver with post-execution result.
+ * The input argument is processed only if it is a {...@link ResultList}
with
+ * an attached {...@link SelectResultObjectProvider} as its
+ * {...@link ResultList#getUserObject() user object}.
+ */
public boolean initialize(Object result) {
boolean initialized = false;
if (result instanceof ResultList) {
@@ -106,9 +123,9 @@
SelectExecutor selector = rop.getSelect();
SQLBuffer buffer = selector == null ? null : selector.getSQL();
if (buffer != null && !selector.hasMultipleSelects()) {
- setDatastoreAction(buffer.getSQL());
- setUserParameters(buffer.getUserParameters());
+ setTargetQuery(buffer.getSQL());
setParameters(buffer.getParameters());
+ setUserParameterPositions(buffer.getUserParameters());
initialized = true;
}
}
@@ -117,7 +134,9 @@
}
/**
- * Merge the given user parameters with its own parameter.
+ * Merge the given user parameters with its own parameter. The given map
+ * must be compatible with the user parameters extracted during
+ * {...@link #initialize(Object) initialization}.
*
* @return key index starting from 1 and corresponding values.
*/
@@ -126,31 +145,46 @@
for (int i = 0; i < _params.size(); i++) {
result.put(i, _params.get(i));
}
- if (user == null)
+ if (user == null || user.isEmpty())
return result;
for (Object key : user.keySet()) {
- List<Integer> indices = findUserParameterPositions(key);
+ int[] indices = _userParamPositions.get(key);
+ if (indices == null)
+ continue;
for (int j : indices)
result.put(j, user.get(key));
}
return result;
}
- private List<Integer> findUserParameterPositions(Object key) {
- List<Integer> result = new ArrayList<Integer>();
- for (int i = 1; _userParams != null && i < _userParams.size(); i+=2) {
- if (_userParams.get(i).equals(key))
- result.add((Integer)_userParams.get(i-1));
+ /**
+ * Marks the positions of user parameters.
+ *
+ * @param list odd elements are numbers representing the position of a
+ * user parameter in the _param list. Even elements are the user parameter
+ * key. A user parameter key may appear more than once.
+ */
+ void setUserParameterPositions(List list) {
+ _userParamPositions = new HashMap<Object, int[]>();
+ for (int i = 1; list != null && i < list.size(); i+=2) {
+ Object key = list.get(i);
+ int p = (Integer)list.get(i-1);
+ int[] positions = _userParamPositions.get(key);
+ if (positions == null) {
+ positions = new int[]{p};
+ } else {
+ int[] temp = new int[positions.length+1];
+ System.arraycopy(positions, 0, temp, 0, positions.length);
+ temp[positions.length] = p;
+ positions = temp;
+ }
+ _userParamPositions.put(key, positions);
}
- return result;
- }
-
- void setUserParameters(List list) {
- _userParams = list;
}
void setParameters(List list) {
- _params = list;
+ _params = new ArrayList();
+ _params.addAll(list);
}
public String toString() {
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java?rev=739425&r1=739424&r2=739425&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
Fri Jan 30 22:06:30 2009
@@ -160,7 +160,8 @@
if (_userIndex == null)
_userIndex = new ArrayList();
for (int i = 0; i < buf._userIndex.size(); i+=2) {
- int newIndex = ((Integer)buf._userIndex.get(i)).intValue() +
paramIndex;
+ int newIndex = ((Integer)buf._userIndex.get(i)).intValue()
+ + paramIndex;
Object userParam = buf._userIndex.get(i+1);
_userIndex.add(newIndex);
_userIndex.add(userParam);
@@ -254,10 +255,12 @@
}
/**
- * Append a user parameter value for a specific column.
+ * Append a user parameter value for a specific column. User parameters
+ * are marked as opposed to the parameters inserted by the internal runtime
+ * system. This helps to reuse the buffer by reparmeterizing it with new
+ * set of user parameters while the 'internal' parameters remain
unchanged.
*
- * @param userParam if non-null, designates the user parameter from a
- * Query Expression tree.
+ * @param userParam if non-null, designates a 'user' parameter.
*/
public SQLBuffer appendValue(Object o, Column col, Parameter userParam) {
if (o == null)
Modified:
openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties?rev=739425&r1=739424&r2=739425&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties
Fri Jan 30 22:06:30 2009
@@ -115,3 +115,13 @@
batch_limit: The batch limit is set to {0}.
batch_update_info: ExecuteBatch command returns update count {0} for \
statement {1}.
+prepared-query-cached: Query "{0}" is cached as target query "{1}"
+prepared-query-not-cachable: Query "{0}" is not fit for caching.
+prepared-query-invalidate: Query "{0}" is invalidated and removed from cache.
+prepared-query-uncache-strong: Query "{0}" is permanently excluded from cache.
+prepared-query-uncache-weak: Query "{0}" is excluded temporarily due to "{1}".
+prepared-query-add-pattern: Adding a Query exclusion pattern "{0}" has caused \
+ following {1} cached queries to be removed from the cache: "{2}".
+prepared-query-remove-pattern: Removing a Query exclusion pattern "{0}" caused
\
+ following {1} queries to be re-inserted in the cache: "{2}".
+
\ No newline at end of file
Modified:
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/rop/ResultList.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/rop/ResultList.java?rev=739425&r1=739424&r2=739425&view=diff
==============================================================================
---
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/rop/ResultList.java
(original)
+++
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/rop/ResultList.java
Fri Jan 30 22:06:30 2009
@@ -32,7 +32,7 @@
* objects, and thus allow very large result sets to be obtained and
* manipulated. Note that wrapping a ResultList in another Collection will
* always instantiate the entire set of elements contained in the
- * ResultList. This may not always be desireable, since the list may
+ * ResultList. This may not always be desirable, since the list may
* be very large.
*
* @author Marc Prud'hommeaux
@@ -44,7 +44,14 @@
*/
public boolean isProviderOpen();
+ /**
+ * Get the opaque user object attached to this receiver.
+ */
public Object getUserObject();
+
+ /**
+ * Set the opaque user object to this receiver.
+ */
public void setUserObject(Object opaque);
/**