Author: seade Date: Mon Sep 12 06:43:59 2005 New Revision: 280339 URL: http://svn.apache.org/viewcvs?rev=280339&view=rev Log: Closer to an actual Serializable LargeSelect.
Serialization of Criteria needs to be addressed before this can progress any further. Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java db/torque/test/trunk/test-project/src/java/org/apache/torque/util/LargeSelectTest.java Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java?rev=280339&r1=280338&r2=280339&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java Mon Sep 12 06:43:59 2005 @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Set; +import java.io.IOException; +import java.io.ObjectInputStream; import java.io.Serializable; import java.lang.reflect.Method; @@ -111,16 +113,15 @@ * <p>In your template you will then use something along the lines of: * * <pre> - * #set ($largeSelect = $data.User.getTemp("someName")) - * #set ($searchop = $data.Parameters.getString("searchop")) - * #if ($searchop.equals("prev")) - * #set ($recs = $largeSelect.PreviousResults) + * #set($largeSelect = $data.User.getTemp("someName")) + * #set($searchop = $data.Parameters.getString("searchop")) + * #if($searchop.equals("prev")) + * #set($recs = $largeSelect.PreviousResults) * #else - * #if ($searchop.equals("goto")) - * #set ($recs - * = $largeSelect.getPage($data.Parameters.getInt("page", 1))) + * #if($searchop.equals("goto")) + * #set($recs = $largeSelect.getPage($data.Parameters.getInt("page", 1))) * #else - * #set ($recs = $largeSelect.NextResults) + * #set($recs = $largeSelect.NextResults) * #end * #end * </pre> @@ -141,9 +142,9 @@ private int memoryLimit; /** The record number of the first record in memory. */ - private int blockBegin = 0; + private transient int blockBegin = 0; /** The record number of the last record in memory. */ - private int blockEnd; + private transient int blockEnd; /** How much of the memory block is currently occupied with result data. */ private volatile int currentlyFilledTo = -1; @@ -151,31 +152,29 @@ private String query; /** The database name to get from Torque. */ private String dbName; - /** Used to retrieve query results from Village. */ - private QueryDataSet qds = null; /** The memory store of records. */ - private List results = null; + private transient List results = null; /** The thread that executes the query. */ - private Thread thread = null; + private transient Thread thread = null; /** * A flag used to kill the thread when the currently executing query is no * longer required. */ - private volatile boolean killThread = false; + private transient volatile boolean killThread = false; /** A flag that indicates whether or not the query thread is running. */ - private volatile boolean threadRunning = false; + private transient volatile boolean threadRunning = false; /** * An indication of whether or not the current query has completed * processing. */ - private volatile boolean queryCompleted = false; + private transient volatile boolean queryCompleted = false; /** * An indication of whether or not the totals (records and pages) are at * their final values. */ - private boolean totalsFinalized = false; + private transient boolean totalsFinalized = false; /** The cursor position in the result set. */ private int position; @@ -183,13 +182,11 @@ private int totalPages = -1; /** The total number of records known to exist. */ private int totalRecords = 0; - /** The number of the page that was last retrieved. */ - private int currentPageNumber = 0; /** The criteria used for the query. */ private Criteria criteria = null; /** The last page of results that were returned. */ - private List lastResults; + private transient List lastResults; /** * The class that is possibly used to construct the criteria and used @@ -200,24 +197,31 @@ * A reference to the method in the return builder class that will * convert the Village Records to the desired class. */ - private Method populateObjectsMethod = null; + private transient Method populateObjectsMethod = null; /** * The default value (">") used to indicate that the total number of - * records or pages is unknown. You can use <code>setMoreIndicator()</code> - * to change this to whatever value you like (e.g. "more than"). + * records or pages is unknown. */ public static final String DEFAULT_MORE_INDICATOR = ">"; + /** + * The value used to indicate that the total number of records or pages is + * 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; /** * The default value for the maximum number of pages of data to be retained - * in memory - you can provide your own default value using - * <code>setMemoryPageLimit()</code>. + * in memory. */ public static final int DEFAULT_MEMORY_LIMIT_PAGES = 5; + /** + * 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; /** A place to store search parameters that relate to this query. */ @@ -354,11 +358,6 @@ selectColumnAdder.invoke(returnBuilderClass.newInstance(), theArgs); } - - // Locate the populateObjects() method - this will be used later - Class[] argTypes = { List.class }; - populateObjectsMethod = - returnBuilderClass.getMethod("populateObjects", argTypes); } catch (Exception e) { @@ -371,6 +370,21 @@ } /** + * Access the populateObjects method. + */ + private Method getPopulateObjectsMethod() + throws SecurityException, NoSuchMethodException + { + if (null == populateObjectsMethod) + { + Class[] argTypes = { List.class }; + populateObjectsMethod + = returnBuilderClass.getMethod("populateObjects", argTypes); + } + return populateObjectsMethod; + } + + /** * Called by the constructors to start the query. * * @param criteria Object used by <code>BasePeer</code> to build the query. @@ -430,10 +444,9 @@ { if (pageNumber < 1) { - throw new IllegalArgumentException("pageNumber must be greater " - + "than zero."); + throw new IllegalArgumentException( + "pageNumber must be greater than zero."); } - currentPageNumber = pageNumber; return getResults((pageNumber - 1) * pageSize); } @@ -451,7 +464,6 @@ { return getCurrentPageResults(); } - currentPageNumber++; return getResults(position); } @@ -460,10 +472,13 @@ * * @return a <code>List</code> of query results containing a maximum of * <code>pageSize</code> reslts. + * @throws TorqueException if invoking the <code>populateObjects()<code> + * method runs into problems or a sleep is unexpectedly interrupted. */ - public List getCurrentPageResults() + public List getCurrentPageResults() throws TorqueException { - return lastResults; + return null == lastResults && position > 0 + ? getResults(position) : lastResults; } /** @@ -485,12 +500,10 @@ if (position - 2 * pageSize < 0) { start = 0; - currentPageNumber = 1; } else { start = position - 2 * pageSize; - currentPageNumber--; } return getResults(start); } @@ -638,10 +651,8 @@ Object[] theArgs = { returnResults }; try { - returnResults = - (List) populateObjectsMethod.invoke( - returnBuilderClass.newInstance(), - theArgs); + returnResults = (List) getPopulateObjectsMethod().invoke( + returnBuilderClass.newInstance(), theArgs); } catch (Exception e) { @@ -661,6 +672,8 @@ int size = pageSize; /* The connection to the database. */ Connection conn = null; + /** Used to retrieve query results from Village. */ + QueryDataSet qds = null; try { @@ -854,7 +867,7 @@ */ public int getCurrentPageNumber() { - return currentPageNumber; + return position / pageSize; } /** @@ -905,8 +918,7 @@ return totalPages; } - int tempPageCount = - getTotalRecords() / pageSize + int tempPageCount = getTotalRecords() / pageSize + (getTotalRecords() % pageSize > 0 ? 1 : 0); if (getTotalsFinalized()) @@ -1009,14 +1021,16 @@ * * @return the number of records that are included on the current page of * results. + * @throws TorqueException if invoking the <code>populateObjects()<code> + * method runs into problems or a sleep is unexpectedly interrupted. */ - public int getCurrentPageSize() + public int getCurrentPageSize() throws TorqueException { - if (null == lastResults) + if (null == getCurrentPageResults()) { return 0; } - return lastResults.size(); + return getCurrentPageResults().size(); } /** @@ -1030,17 +1044,19 @@ { return 0; } - return getCurrentPageNumber() * getPageSize() - getPageSize() + 1; + return (getCurrentPageNumber() - 1) * getPageSize() + 1; } /** * Provide the record number of the last row included on the current page. * * @return the record number of the last row of the current page. + * @throws TorqueException if invoking the <code>populateObjects()<code> + * method runs into problems or a sleep is unexpectedly interrupted. */ - public int getLastRecordNoForPage() + public int getLastRecordNoForPage() throws TorqueException { - if (0 == currentPageNumber) + if (0 == getCurrentPageNumber()) { return 0; } @@ -1054,8 +1070,10 @@ * * @return progress text in the form of "26 - 50 of > 250" where ">" * can be configured using <code>setMoreIndicator()</code>. + * @throws TorqueException if invoking the <code>populateObjects()<code> + * method runs into problems or a sleep is unexpectedly interrupted. */ - public String getRecordProgressText() + public String getRecordProgressText() throws TorqueException { StringBuffer result = new StringBuffer(); result.append(getFirstRecordNoForPage()); @@ -1122,15 +1140,13 @@ blockBegin = 0; blockEnd = 0; currentlyFilledTo = -1; - qds = null; results = null; + // TODO Perhaps store the oldPosition and immediately restart the + // query. + // oldPosition = position; position = 0; totalPages = -1; totalRecords = 0; - // todo Perhaps store the oldPageNumber and immediately restart the - // query. - // oldPageNumber = currentPageNumber; - currentPageNumber = 0; queryCompleted = false; totalsFinalized = false; lastResults = null; @@ -1209,6 +1225,20 @@ { params.remove(name); } + } + + /** + * Deserialize this LargeSelect instance. + * + * @param inputStream The serialization input stream. + * @throws IOException + * @throws ClassNotFoundException + */ + private void readObject(ObjectInputStream inputStream) + throws IOException, ClassNotFoundException + { + inputStream.defaultReadObject(); + startQuery(pageSize); } /** Modified: db/torque/test/trunk/test-project/src/java/org/apache/torque/util/LargeSelectTest.java URL: http://svn.apache.org/viewcvs/db/torque/test/trunk/test-project/src/java/org/apache/torque/util/LargeSelectTest.java?rev=280339&r1=280338&r2=280339&view=diff ============================================================================== --- db/torque/test/trunk/test-project/src/java/org/apache/torque/util/LargeSelectTest.java (original) +++ db/torque/test/trunk/test-project/src/java/org/apache/torque/util/LargeSelectTest.java Mon Sep 12 06:43:59 2005 @@ -16,6 +16,7 @@ * limitations under the License. */ +import org.apache.commons.lang.SerializationUtils; import org.apache.torque.BaseRuntimeTestCase; import org.apache.torque.TorqueException; import org.apache.torque.test.Author; @@ -217,12 +218,12 @@ // Page 0 assertEquals("Current page number", 0, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", 0, ls.getCurrentPageSize()); assertEquals("First record for page", 0, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 0, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 0, ls.getTotalPages()); assertEquals("Total records", 0, ls.getTotalRecords()); assertEquals("Page progress text", "0 of > 0", ls.getPageProgressText()); @@ -232,12 +233,12 @@ // Page 1 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); assertEquals("Current page number", 1, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 5, ls.getTotalPages()); assertEquals("Total records", 45, ls.getTotalRecords()); assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); @@ -252,7 +253,7 @@ assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); assertEquals("First record for page", 37, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 45, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 5, ls.getTotalPages()); assertEquals("Total records", 45, ls.getTotalRecords()); assertEquals("Page progress text", "5 of > 5", ls.getPageProgressText()); @@ -282,7 +283,7 @@ assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); assertEquals("Current page number", 9, ls.getCurrentPageNumber()); assertTrue("Previous results available", ls.getPreviousResultsAvailable()); - assertEquals("Next results available", false, ls.getNextResultsAvailable()); + assertFalse("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); assertEquals("First record for page", 73, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 81, ls.getLastRecordNoForPage()); @@ -315,7 +316,7 @@ // Page 1 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); assertEquals("Current page number", 1, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); @@ -341,11 +342,11 @@ // Page 1 List results = ls.getNextResults(); assertTrue("Totals finalised", ls.getTotalsFinalized()); - assertEquals("Paginated", false, ls.getPaginated()); + assertFalse("Paginated", ls.getPaginated()); assertEquals("results.size()", 1, results.size()); assertEquals("Current page number", 1, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); - assertEquals("Next results available", false, ls.getNextResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); + assertFalse("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", 1, ls.getCurrentPageSize()); assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 1, ls.getLastRecordNoForPage()); @@ -369,12 +370,12 @@ // Page 0 assertEquals("Current page number", 0, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", 0, ls.getCurrentPageSize()); assertEquals("First record for page", 0, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 0, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 0, ls.getTotalPages()); assertEquals("Total records", 0, ls.getTotalRecords()); assertEquals("Page progress text", "0 of > 0", ls.getPageProgressText()); @@ -384,12 +385,12 @@ // Page 1 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); assertEquals("Current page number", 1, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 5, ls.getTotalPages()); assertEquals("Total records", 45, ls.getTotalRecords()); assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); @@ -402,12 +403,12 @@ // Page 0 assertEquals("Current page number", 0, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", 0, ls.getCurrentPageSize()); assertEquals("First record for page", 0, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 0, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 0, ls.getTotalPages()); assertEquals("Total records", 0, ls.getTotalRecords()); assertEquals("Page progress text", "0 of > 0", ls.getPageProgressText()); @@ -417,17 +418,71 @@ // Page 1 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); assertEquals("Current page number", 1, ls.getCurrentPageNumber()); - assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); + assertFalse("Previous results available", ls.getPreviousResultsAvailable()); assertTrue("Next results available", ls.getNextResultsAvailable()); assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); - assertEquals("Totals finalised", false, ls.getTotalsFinalized()); + assertFalse("Totals finalised", ls.getTotalsFinalized()); assertEquals("Total pages", 5, ls.getTotalPages()); assertEquals("Total records", 45, ls.getTotalRecords()); assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); assertEquals("Record progress text", "1 - 9 of > 45", ls.getRecordProgressText()); } + +// /** +// * A basic serialization test. Cannot continue implementation until +// * serialization of Criteria has been implemented correctly. +// * +// * @throws TorqueException +// */ +// public void testSerialization() throws TorqueException +// { +// // TODO Serialization needs to be reviewed for LargeSelect +// // userDataSet should be marked transient and all access should be wrapped for detect and handle null _or_ readObject should rerun the query. +// +// System.out.println("criteria (before LS created) = " + criteria); +// LargeSelect ls = new LargeSelect(criteria, TEST_PAGE_SIZE, +// "org.apache.torque.test.AuthorPeer"); +// +// assertEquals("Page size", TEST_PAGE_SIZE, ls.getPageSize()); +// assertTrue("Paginated", ls.getPaginated()); +// +// List results = ls.getNextResults(); +// // Page 1 +// assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); +// assertEquals("Current page number", 1, ls.getCurrentPageNumber()); +// assertFalse("Previous results available", ls.getPreviousResultsAvailable()); +// assertTrue("Next results available", ls.getNextResultsAvailable()); +// assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); +// assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); +// assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); +// assertFalse("Totals finalised", ls.getTotalsFinalized()); +// assertEquals("Total pages", 5, ls.getTotalPages()); +// assertEquals("Total records", 45, ls.getTotalRecords()); +// assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); +// assertEquals("Record progress text", "1 - 9 of > 45", ls.getRecordProgressText()); +// +// LargeSelect lsCopy = (LargeSelect) SerializationUtils.clone(ls); +// +// assertEquals("Page size", TEST_PAGE_SIZE, lsCopy.getPageSize()); +// assertTrue("Paginated", lsCopy.getPaginated()); +// +// // Page 1 +// results = lsCopy.getCurrentPageResults(); +// assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); +// assertEquals("Current page number", 1, lsCopy.getCurrentPageNumber()); +// assertFalse("Previous results available", lsCopy.getPreviousResultsAvailable()); +// assertTrue("Next results available", lsCopy.getNextResultsAvailable()); +// assertEquals("Current page size", TEST_PAGE_SIZE, lsCopy.getCurrentPageSize()); +// assertEquals("First record for page", 1, lsCopy.getFirstRecordNoForPage()); +// assertEquals("Last record for page", 9, lsCopy.getLastRecordNoForPage()); +// assertFalse("Totals finalised", lsCopy.getTotalsFinalized()); +// assertEquals("Total pages", 5, lsCopy.getTotalPages()); +// assertEquals("Total records", 45, lsCopy.getTotalRecords()); +// assertEquals("Page progress text", "1 of > 5", lsCopy.getPageProgressText()); +// assertEquals("Record progress text", "1 - 9 of > 45", lsCopy.getRecordProgressText()); +// } // todo Add a test for getPaginated() - was previously returning false when 6 results and pageSize 5 --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]