seade 2003/06/20 03:54:09
Modified: src/java/org/apache/torque/util Tag: TORQUE_3_0_BRANCH
LargeSelect.java
xdocs Tag: TORQUE_3_0_BRANCH changes.xml
Log:
Now implements Serializable, provides better debugging information and further
methods for maintaining search parameters.
A thread safety issue when calling invalidateResult has been repaired (thanks to
Soteri Panagou <[EMAIL PROTECTED]>).
Revision Changes Path
No revision
No revision
1.8.2.1 +109 -28 db-torque/src/java/org/apache/torque/util/LargeSelect.java
Index: LargeSelect.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/util/LargeSelect.java,v
retrieving revision 1.8
retrieving revision 1.8.2.1
diff -u -r1.8 -r1.8.2.1
--- LargeSelect.java 27 Nov 2002 11:31:00 -0000 1.8
+++ LargeSelect.java 20 Jun 2003 10:54:08 -0000 1.8.2.1
@@ -3,7 +3,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -57,9 +57,12 @@
import java.sql.Connection;
import java.sql.SQLException;
+import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;
+import java.util.Set;
+import java.io.Serializable;
import java.lang.reflect.Method;
import org.apache.log4j.Category;
@@ -166,7 +169,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Scott Eade</a>
* @version $Id$
*/
-public class LargeSelect implements Runnable
+public class LargeSelect implements Runnable, Serializable
{
/** The log. */
@@ -203,6 +206,8 @@
* longer required.
*/
private volatile boolean killThread = false;
+ /** A flag that indicates whether or not the query thread is running. */
+ private volatile boolean threadRunning = false;
/**
* An indication of whether or not the current query has completed
* processing.
@@ -582,7 +587,7 @@
{
try
{
- Thread.currentThread().sleep(500);
+ Thread.sleep(500);
}
catch (InterruptedException e)
{
@@ -793,6 +798,7 @@
{
log.error(e);
}
+ threadRunning = false;
}
}
@@ -801,13 +807,17 @@
*
* @param initialSize the initial size for each block.
*/
- private void startQuery(int initialSize)
+ private synchronized void startQuery(int initialSize)
{
- pageSize = initialSize;
- currentlyFilledTo = -1;
- queryCompleted = false;
- thread = new Thread(this);
- thread.start();
+ if (!threadRunning)
+ {
+ pageSize = initialSize;
+ currentlyFilledTo = -1;
+ queryCompleted = false;
+ thread = new Thread(this);
+ thread.start();
+ threadRunning = true;
+ }
}
/**
@@ -816,21 +826,24 @@
*
* @throws TorqueException if a sleep is interrupted.
*/
- private void stopQuery() throws TorqueException
+ private synchronized void stopQuery() throws TorqueException
{
- killThread = true;
- while (thread.isAlive())
+ if (threadRunning)
{
- try
- {
- Thread.currentThread().sleep(100);
- }
- catch (InterruptedException e)
+ killThread = true;
+ while (thread.isAlive())
{
- throw new TorqueException("Unexpected interruption", e);
+ try
+ {
+ Thread.sleep(100);
+ }
+ catch (InterruptedException e)
+ {
+ throw new TorqueException("Unexpected interruption", e);
+ }
}
+ killThread = false;
}
- killThread = false;
}
/**
@@ -1102,7 +1115,7 @@
*
* @throws TorqueException if a sleep is interrupted.
*/
- public void invalidateResult() throws TorqueException
+ public synchronized void invalidateResult() throws TorqueException
{
stopQuery();
blockBegin = 0;
@@ -1133,29 +1146,97 @@
*/
public String getSearchParam(String name)
{
+ return getSearchParam(name, null);
+ }
+
+ /**
+ * Retrieve a search parameter. This acts as a convenient place to store
+ * parameters that relate to the LargeSelect to make it easy to get at them
+ * in order to repopulate search parameters on a form when the next page of
+ * results is retrieved - they in no way effect the operation of
+ * LargeSelect.
+ *
+ * @param name the search parameter key to retrieve.
+ * @param defaultValue the default value to return if the key is not found.
+ * @return the value of the search parameter.
+ */
+ public String getSearchParam(String name, String defaultValue)
+ {
if (null == params)
{
- return "";
+ return defaultValue;
}
- return (String) params.get(name);
+ String value = (String) params.get(name);
+ return null == value ? defaultValue : value;
}
/**
- * Set a parameter used to retrieve the last set of results.
+ * Set a search parameter. If the value is <code>null</code> then the
+ * key will be removed from the parameters.
*
* @param name the search parameter key to set.
* @param value the value of the search parameter to store.
*/
public void setSearchParam(String name, String value)
{
- if (null == params)
+ if (null == value)
{
- params = new Hashtable();
+ removeSearchParam(name);
}
- if (name != null && value != null)
+ else
+ {
+ if (null != name)
+ {
+ if (null == params)
+ {
+ params = new Hashtable();
+ }
+ params.put(name, value);
+ }
+ }
+ }
+
+ /**
+ * Remove a value from the search parameters.
+ *
+ * @param name the search parameter key to remove.
+ */
+ public void removeSearchParam(String name)
+ {
+ if (null != params)
+ {
+ params.remove(name);
+ }
+ }
+
+ /**
+ * Provide something useful for debugging purposes.
+ *
+ * @return some basic information about this instance of LargeSelect.
+ */
+ public String toString()
+ {
+ StringBuffer result = new StringBuffer();
+ result.append("LargeSelect - TotalRecords: ");
+ result.append(getTotalRecords());
+ result.append(" TotalsFinalised: ");
+ result.append(getTotalsFinalized());
+ result.append("\nParameters:");
+ if (null == params || params.size() == 0)
{
- params.put(name, value);
+ result.append(" No parameters have been set.");
}
+ else
+ {
+ Set keys = params.keySet();
+ for (Iterator iter = keys.iterator(); iter.hasNext();)
+ {
+ String key = (String) iter.next();
+ String val = (String) params.get(key);
+ result.append("\n ").append(key).append(": ").append(val);
+ }
+ }
+ return result.toString();
}
}
No revision
No revision
1.54.2.14 +15 -0 db-torque/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/db-torque/xdocs/changes.xml,v
retrieving revision 1.54.2.13
retrieving revision 1.54.2.14
diff -u -r1.54.2.13 -r1.54.2.14
--- changes.xml 20 Jun 2003 08:45:59 -0000 1.54.2.13
+++ changes.xml 20 Jun 2003 10:54:09 -0000 1.54.2.14
@@ -16,6 +16,21 @@
Changes between Torque 3.0.1 and 3.0.2.<br/>
</p>
+<subsection name="Other changes">
+<p>
+ <ul>
+ <li>
+ LargeSelect now implements Serializable, provides better debugging
+ information and further methods for maintaining search parameters.
+ </li>
+ <li>
+ LargeSelect is now thread safe when calling invalidateResult
+ (thanks to Soteri Panagou <[EMAIL PROTECTED]>).
+ </li>
+ </ul>
+</p>
+</subsection>
+
<subsection name="Fixed bugs">
<p>
<ul>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]