Author: tv
Date: Thu Jul 5 20:01:39 2012
New Revision: 1357857
URL: http://svn.apache.org/viewvc?rev=1357857&view=rev
Log:
Experimental cleanup of the interface
Attempt to fix a long standing bug: getResults() does not return if the
background thread dies unexpectedly.
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java?rev=1357857&r1=1357856&r2=1357857&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/LargeSelect.java
Thu Jul 5 20:01:39 2012
@@ -22,7 +22,6 @@ package org.apache.torque.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
-import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Hashtable;
@@ -68,14 +67,14 @@ import org.apache.torque.sql.SqlBuilder;
* provides various methods for indicating how many records and pages it is
* currently aware of and for presenting this information to users.
*
- * <p><code>LargeSelect</code> utilises the <code>Criteria</code> methods
+ * <p><code>LargeSelect</code> utilizes the <code>Criteria</code> methods
* <code>setOffset()</code> and <code>setLimit()</code> to limit the amount of
* data retrieved from the database - these values are either passed through to
* the DBMS when supported (efficient with the caveat below) or handled by
* the Village API when it is not (not so efficient). At time of writing
* <code>Criteria</code> will only pass the offset and limit through to MySQL
* and PostgreSQL (with a few changes to <code>DBOracle</code> and <code>
- * BasePeer</code> Oracle support can be implemented by utilising the <code>
+ * BasePeer</code> Oracle support can be implemented by utilizing the <code>
* rownum</code> pseudo column).
*
* <p>As <code>LargeSelect</code> must re-execute the query each time the user
@@ -89,16 +88,6 @@ import org.apache.torque.sql.SqlBuilder;
* </code> allow you to override this for a specific instance of
* <code>LargeSelect</code> or future instances respectively.
*
- * <p>The constructors allow you to specify the name of the class to use
- * to build the returnd rows. This works by using reflection to find
- * the <code>addSelectColumns(Criteria)</code>
- * method to add the necessary select columns to the criteria (only if it
- * doesn't already contain any).
- * This allows you to use any of the Torque generated Peer classes, but also
- * makes it fairly simple to construct business object classes that can be used
- * for this purpose (simply copy and customise the <code>addSelectColumns()
- * </code> method from an existing Peer class).
- *
* <p>Typically you will create a <code>LargeSelect</code> using your <code>
* Criteria</code> (perhaps created from the results of a search parameter
* page), page size, memory page limit, return class name (for which you may
@@ -187,16 +176,11 @@ public class LargeSelect<T> implements R
/** The criteria used for the query. */
private CriteriaInterface<?> criteria = null;
+
/** The last page of results that were returned. */
private transient List<T> lastResults;
/**
- * The class that is possibly used to construct the criteria and used
- * to transform the Village Records into the desired OM or business
objects.
- */
- private Class<?> returnBuilderClass = null;
-
- /**
* The Mapper used to map the database records into objects.
*/
private RecordMapper<T> mapper = null;
@@ -212,7 +196,7 @@ public class LargeSelect<T> implements R
* unknown (default: ">"). You can use <code>setMoreIndicator()</code>
* to change this to whatever value you like (e.g. "more than").
*/
- private static String moreIndicator = DEFAULT_MORE_INDICATOR;
+ private String moreIndicator = DEFAULT_MORE_INDICATOR;
/**
* The default value for the maximum number of pages of data to be retained
@@ -224,7 +208,7 @@ public class LargeSelect<T> implements R
* The maximum number of pages of data to be retained in memory. Use
* <code>setMemoryPageLimit()</code> to provide your own value.
*/
- private static int memoryPageLimit = DEFAULT_MEMORY_LIMIT_PAGES;
+ private int memoryPageLimit = DEFAULT_MEMORY_LIMIT_PAGES;
/**
* The number of milliseconds to sleep when the result of a query
@@ -251,115 +235,64 @@ public class LargeSelect<T> implements R
* pages of results in memory.
*
* @param criteria object used by BasePeer to build the query. In order to
- * allow this class to utilise database server implemented offsets and
+ * allow this class to utilize database server implemented offsets and
* limits (when available), the provided criteria must not have any limit
or
- * offset defined. If the criteria does not include the definition of any
- * select columns the <code>addSelectColumns(Criteria)</code> method of
- * the class named as <code>returnBuilderClassName</code> will be used to
- * add them.
+ * offset defined. The criteria <b>must</b> however include the definition
of
+ * select columns.
* @param pageSize number of rows to return in one block.
- * @param returnBuilderClassName The name of the class that will be used to
- * build the result records (may implement <code>addSelectColumns(Criteria)
- * </code> and must implement <code>populateObjects(List)</code>).
+ * @param recordMapper the mapper that will be used to build the result
records
+ *
* @throws IllegalArgumentException if <code>criteria</code> uses one or
- * both of offset and limit, if <code>pageSize</code> is less than 1, or if
- * problems are experienced locating and invoking either one or both of
- * <code>addSelectColumns(Criteria)</code> and <code> populateObjects(List)
- * </code> in the class named <code>returnBuilderClassName</code>.
+ * both of offset and limit, if <code>pageSize</code> is less than 1 or
+ * the Criteria object does not contain SELECT columns.
*/
public LargeSelect(
CriteriaInterface<?> criteria,
int pageSize,
- String returnBuilderClassName,
RecordMapper<T> recordMapper)
{
this(
criteria,
pageSize,
- LargeSelect.memoryPageLimit,
- returnBuilderClassName,
+ LargeSelect.DEFAULT_MEMORY_LIMIT_PAGES,
recordMapper);
}
/**
* Creates a LargeSelect whose results are returned as a <code>List</code>
- * containing a maximum of <code>pageSize</code> objects of the type
- * defined within the class named <code>returnBuilderClassName</code> at a
+ * containing a maximum of <code>pageSize</code> objects of the type T at a
* time, maintaining a maximum of <code>memoryPageLimit</code> pages of
* results in memory.
*
* @param criteria object used by BasePeer to build the query. In order to
- * allow this class to utilise database server implemented offsets and
+ * allow this class to utilize database server implemented offsets and
* limits (when available), the provided criteria must not have any limit
or
- * offset defined. If the criteria does not include the definition of any
- * select columns the <code>addSelectColumns(Criteria)</code> method of
- * the class named as <code>returnBuilderClassName</code> will be used to
- * add them.
+ * offset defined. The criteria <b>must</b> however include the definition
of
+ * select columns.
* @param pageSize number of rows to return in one block.
* @param memoryPageLimit maximum number of pages worth of rows to be held
* in memory at one time.
- * @param returnBuilderClassName The name of the class that will be used to
- * build the result records (may implement <code>addSelectColumns(Criteria)
- * </code> and must implement <code>populateObjects(List)</code>).
+ * @param recordMapper the mapper that will be used to build the result
records
+ *
* @throws IllegalArgumentException if <code>criteria</code> uses one or
* both of offset and limit, if <code>pageSize</code> or <code>
- * memoryLimitPages</code> are less than 1, or if problems are experienced
- * locating and invoking either one or both of <code>
- * addSelectColumns(Criteria)</code> and <code>
populateObjects(List)</code>
- * in the class named <code>returnBuilderClassName</code>.
+ * memoryLimitPages</code> are less than 1 or the Criteria object does not
+ * contain SELECT columns..
*/
public LargeSelect(
CriteriaInterface<?> criteria,
int pageSize,
int memoryPageLimit,
- String returnBuilderClassName,
RecordMapper<T> recordMapper)
{
this.mapper = recordMapper;
- try
- {
- this.returnBuilderClass = Class.forName(returnBuilderClassName);
- // Add the select columns if necessary.
- if (criteria.getSelectColumns().size() == 0)
- {
- Class<?>[] argTypes = {criteria.getClass()};
- Method selectColumnAdder =
- returnBuilderClass.getMethod("addSelectColumns", argTypes);
- Object[] theArgs = {criteria};
- selectColumnAdder.invoke(returnBuilderClass.newInstance(),
- theArgs);
- }
- }
- catch (Exception e)
+ if (criteria.getSelectColumns().size() == 0)
{
throw new IllegalArgumentException(
- "The class named as returnBuilderClassName does not "
- + "provide the necessary facilities - see javadoc.");
+ "The Criteria object does not contain SELECT columns.");
}
- init(criteria, pageSize, memoryPageLimit);
- }
-
- /**
- * Called by the constructors to start the query.
- *
- * @param criteria Object used by <code>BasePeer</code> to build the query.
- * In order to allow this class to utilise database server implemented
- * offsets and limits (when available), the provided criteria must not have
- * any limit or offset defined.
- * @param pageSize number of rows to return in one block.
- * @param memoryLimitPages maximum number of pages worth of rows to be held
- * in memory at one time.
- * @throws IllegalArgumentException if <code>criteria</code> uses one or
- * both of offset and limit and if <code>pageSize</code> or
- * <code>memoryLimitPages</code> are less than 1;
- */
- private void init(
- CriteriaInterface<?> criteria,
- int pageSize,
- int memoryLimitPages)
- {
if (criteria.getOffset() != 0 || criteria.getLimit() != -1)
{
throw new IllegalArgumentException(
@@ -372,14 +305,14 @@ public class LargeSelect<T> implements R
"pageSize must be greater than zero.");
}
- if (memoryLimitPages < 1)
+ if (memoryPageLimit < 1)
{
throw new IllegalArgumentException(
"memoryPageLimit must be greater than zero.");
}
this.pageSize = pageSize;
- this.memoryLimit = pageSize * memoryLimitPages;
+ this.memoryLimit = pageSize * memoryPageLimit;
this.criteria = criteria;
dbName = criteria.getDbName();
blockEnd = blockBegin + memoryLimit - 1;
@@ -430,7 +363,7 @@ public class LargeSelect<T> implements R
* Provide access to the results from the current page.
*
* @return a <code>List</code> of query results containing a maximum of
- * <code>pageSize</code> reslts.
+ * <code>pageSize</code> results.
* @throws TorqueException if invoking the <code>populateObjects()<code>
* method runs into problems or a sleep is unexpectedly interrupted.
*/
@@ -444,7 +377,7 @@ public class LargeSelect<T> implements R
* Gets the previous page of rows.
*
* @return a <code>List</code> of query results containing a maximum of
- * <code>pageSize</code> reslts.
+ * <code>pageSize</code> results.
* @throws TorqueException if invoking the <code>populateObjects()<code>
* method runs into problems or a sleep is unexpectedly interrupted.
*/
@@ -510,7 +443,7 @@ public class LargeSelect<T> implements R
+ ") exceeds memory limit (" + memoryLimit + ").");
}
- // Request was for a block of rows which should be in progess.
+ // Request was for a block of rows which should be in progress.
// If the rows have not yet been returned, wait for them to be
// retrieved.
if (start >= blockBegin && (start + size - 1) <= blockEnd)
@@ -762,6 +695,9 @@ public class LargeSelect<T> implements R
{
Torque.closeConnection(conn);
threadRunning = false;
+
+ // Make sure getResults() finally returns if we die.
+ queryCompleted = true;
if (log.isDebugEnabled())
{
log.debug("Exiting query thread");
@@ -928,17 +864,17 @@ public class LargeSelect<T> implements R
* @param moreIndicator the indicator to use in place of the default
* (">").
*/
- public static void setMoreIndicator(String moreIndicator)
+ public void setMoreIndicator(String moreIndicator)
{
- LargeSelect.moreIndicator = moreIndicator;
+ this.moreIndicator = moreIndicator;
}
/**
* Retrieve the more pages/records indicator.
*/
- public static String getMoreIndicator()
+ public String getMoreIndicator()
{
- return LargeSelect.moreIndicator;
+ return this.moreIndicator;
}
/**
@@ -949,9 +885,9 @@ public class LargeSelect<T> implements R
* @param memoryPageLimit the maximum number of pages to be in memory
* at one time.
*/
- public static void setMemoryPageLimit(int memoryPageLimit)
+ public void setMemoryPageLimit(int memoryPageLimit)
{
- LargeSelect.memoryPageLimit = memoryPageLimit;
+ this.memoryPageLimit = memoryPageLimit;
}
/**
@@ -959,9 +895,9 @@ public class LargeSelect<T> implements R
* when a constructor with no memory page limit is used - the memory limit
* will be this number multiplied by the page size.
*/
- public static int getMemoryPageLimit()
+ public int getMemoryPageLimit()
{
- return LargeSelect.memoryPageLimit;
+ return this.memoryPageLimit;
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]