You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by as...@apache.org on 2006/06/19 23:39:15 UTC
svn commit: r415432 - in /jakarta/jcs/trunk/src:
java/org/apache/jcs/auxiliary/remote/server/
java/org/apache/jcs/utils/struct/
test/org/apache/jcs/auxiliary/disk/jdbc/hsql/
test/org/apache/jcs/utils/struct/
Author: asmuts
Date: Mon Jun 19 14:39:15 2006
New Revision: 415432
URL: http://svn.apache.org/viewvc?rev=415432&view=rev
Log:
I fixed bug in sorted preferential array remove method.
It was generating error log messages from an array index out of bounds exception.
The new call to system arraycopy didn't work. I reverted it to the old manual copy.
I made a test that exposed the bug and verified the fix.
I increased the JCS temp build version and added an entry to the changes.xml
Modified:
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
jakarta/jcs/trunk/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheUnitTest.java
jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SortedPrefArrayUnitTest.java
Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java?rev=415432&r1=415431&r2=415432&view=diff
==============================================================================
--- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java (original)
+++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java Mon Jun 19 14:39:15 2006
@@ -28,7 +28,7 @@
* <servlet>
<servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
<servlet-class>
- com.travelocity.lmd.jcs.JCSRemoteCacheStartupServlet
+ org.apache.jcs.auxiliary.remote.server.RemoteCacheStartupServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java?rev=415432&r1=415431&r2=415432&view=diff
==============================================================================
--- jakarta/jcs/trunk/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java (original)
+++ jakarta/jcs/trunk/src/java/org/apache/jcs/utils/struct/SortedPreferentialArray.java Mon Jun 19 14:39:15 2006
@@ -26,13 +26,13 @@
* Insertion time is n, search is log(n)
*
*
- * Clients must manage thread safety on previous version. I synchronized the public methods to add
- * easy thread safety. I synchronized all public methods that make modifications.
- *
+ * Clients must manage thread safety on previous version. I synchronized the
+ * public methods to add easy thread safety. I synchronized all public methods
+ * that make modifications.
+ *
*/
public class SortedPreferentialArray
{
-
private static final Log log = LogFactory.getLog( SortedPreferentialArray.class );
// prefer large means that the smallest will be removed when full.
@@ -295,7 +295,7 @@
}
catch ( Exception e )
{
- log.error( "Problem removing from array.", e );
+ log.error( "Problem removing from array. pos [" + pos + "] " + obj, e );
}
if ( log.isDebugEnabled() )
@@ -340,7 +340,7 @@
int pos = findNearestLargerOrEqualPosition( obj );
// see if the previous will do to handle the empty insert spot position
if ( pos == curSize )
- { //&& curSize < maxSize ) {
+ { // && curSize < maxSize ) {
// pos will be > 0 if it equals curSize, we check for this above.
if ( obj.compareTo( array[pos - 1] ) <= 0 )
{
@@ -382,7 +382,6 @@
*/
private int findNearestLargerOrEqualPosition( Comparable obj )
{
-
// do nothing if a null was passed in
if ( obj == null )
{
@@ -412,18 +411,18 @@
// do with this, depending on the preference setting
if ( obj.compareTo( getSmallest() ) <= 0 )
{
- //LESS THAN OR EQUAL TO SMALLEST
+ // LESS THAN OR EQUAL TO SMALLEST
if ( log.isDebugEnabled() )
{
log.debug( obj + " is smaller than or equal to " + getSmallest() );
}
greaterPos = 0;
done = true;
- //return greaterPos;
+ // return greaterPos;
}
else
{
- //GREATER THAN SMALLEST
+ // GREATER THAN SMALLEST
if ( log.isDebugEnabled() )
{
log.debug( obj + " is bigger than " + getSmallest() );
@@ -457,7 +456,7 @@
}
}
- ///////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////
// begin binary search for insertion spot
while ( !done )
{
@@ -519,7 +518,7 @@
if ( ( greaterPos != -1 ) && greaterPos - curPos < 0 )
{
done = true;
- break; //return greaterPos;
+ break; // return greaterPos;
}
else
{
@@ -537,7 +536,7 @@
}
}
} // end while
- ///////////////////////////////////////////////////////////////////////
+ // /////////////////////////////////////////////////////////////////////
if ( log.isDebugEnabled() )
{
@@ -565,16 +564,41 @@
private void remove( int position )
{
if ( position >= curSize || position < 0 )
+ {
throw new IndexOutOfBoundsException( "position=" + position + " must be less than curSize=" + curSize );
- curSize--;
+ }
+ //curSize--;
if ( position < curSize )
- System.arraycopy( array, position + 1, array, position, curSize );
+ {
+ try
+ {
+ // this didn't work, I'm leaving it here for now.
+ //System.arraycopy( array, position + 1, array, position, curSize );
+
+ // suffle left from removal point
+ int end = curSize - 1;
+ for (int i = position; i < end; i++)
+ {
+ array[i] = array[i + 1];
+ }
+ curSize--;
+ }
+ catch ( IndexOutOfBoundsException ibe )
+ {
+ // throw this, log details for debugging. This shouldn't happen.
+ log.warn( "Caught index out of bounds exception. "
+ + "called 'System.arraycopy( array, position + 1, array, position, curSize );' "
+ + "array.lengh [" + array.length + "] position [" + position + "] curSize [" + curSize + "]" );
+ throw ibe;
+ }
+ }
return;
}
/**
* Debugging method to return a human readable display of array data.
+ *
* @return
*/
protected String dumpArray()
@@ -591,5 +615,4 @@
}
return buf.toString();
}
-
}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheUnitTest.java?rev=415432&r1=415431&r2=415432&view=diff
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheUnitTest.java Mon Jun 19 14:39:15 2006
@@ -51,7 +51,7 @@
public void testBasicPutRemove()
throws Exception
{
- int items = 200;
+ int items = 20;
String region = "testCache";
@@ -107,7 +107,7 @@
String region = "removeAllAllowed";
JCS jcs = JCS.getInstance( region );
- int items = 200;
+ int items = 20;
// Add items to cache
@@ -147,7 +147,7 @@
String region = "noRemoveAll";
JCS jcs = JCS.getInstance( region );
- int items = 200;
+ int items = 20;
// Add items to cache
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SortedPrefArrayUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SortedPrefArrayUnitTest.java?rev=415432&r1=415431&r2=415432&view=diff
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SortedPrefArrayUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SortedPrefArrayUnitTest.java Mon Jun 19 14:39:15 2006
@@ -22,7 +22,7 @@
* Tests the SortedPrefArray used by the recycle bin.
*
* @author aaronsm
- *
+ *
*/
public class SortedPrefArrayUnitTest
extends TestCase
@@ -59,11 +59,10 @@
public void testLargePref()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
- //array.setPreferLarge( false );
+ // array.setPreferLarge( false );
array.setPreferLarge( true );
String[] elem = {
"10",
@@ -122,7 +121,6 @@
assertEquals( "Size was not as expected.", ( maxSize - 1 ), array.size() );
System.out.println( array.dumpArray() );
-
}
/**
@@ -133,7 +131,6 @@
public void testEmptyTake()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
array.setPreferLarge( true );
@@ -142,7 +139,6 @@
String taken = (String) array.takeNearestLargerOrEqual( String.valueOf( i ) );
assertNull( "taken should be null, since nothing was in the array", taken );
}
-
}
/**
@@ -153,7 +149,6 @@
public void testNullInsertion()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
array.setPreferLarge( true );
@@ -222,11 +217,10 @@
public void testNullTake()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
array.setPreferLarge( true );
-
+
try
{
String taken = (String) array.takeNearestLargerOrEqual( null );
@@ -236,9 +230,8 @@
{
fail( "Got a null pointer trying to take with a null" );
}
-
}
-
+
/**
* Verify that we don't get an npe when taking from an array of only one
*
@@ -247,14 +240,13 @@
public void testSingleItemTake()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
array.setPreferLarge( true );
-
+
array.add( "10" );
System.out.println( array.dumpArray() );
-
+
try
{
String taken = (String) array.takeNearestLargerOrEqual( "09" );
@@ -265,9 +257,8 @@
{
fail( "Got a null pointer trying to take with a null" );
}
-
}
-
+
/**
* Verify that we don't get an npe when taking from an array of only one
*
@@ -276,13 +267,12 @@
public void testSingleItemTakeLarger()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
array.setPreferLarge( true );
-
+
array.add( "10" );
-
+
try
{
String taken = (String) array.takeNearestLargerOrEqual( "11" );
@@ -292,8 +282,7 @@
{
fail( "Got a null pointer trying to take with a null" );
}
-
- }
+ }
/**
* Verify that we don't get an npe when taking from an array of none
@@ -303,11 +292,10 @@
public void testSingleItemTakeLargerEmpty()
throws Exception
{
-
int maxSize = 25;
SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
array.setPreferLarge( true );
-
+
try
{
String taken = (String) array.takeNearestLargerOrEqual( "11" );
@@ -317,6 +305,111 @@
{
fail( "Got a null pointer trying to take with a null" );
}
+ }
+
+ /**
+ * Test taking the largest item.
+ *
+ * @exception Exception
+ */
+ public void testTakeLargestItem()
+ throws Exception
+ {
+ int maxSize = 9;
+
+ SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
+ // array.setPreferLarge( false );
+ array.setPreferLarge( true );
+ String[] elem = {
+ "01",
+ "02",
+ "03",
+ "04",
+ "05",
+ "08",
+ "07",
+ "06",
+ "09",};
+
+ // put more than the max in a random order
+ for ( int i = 0; i < elem.length; i++ )
+ {
+ array.add( elem[i] );
+ System.out.println( array.dumpArray() );
+ }
- }
+ assertEquals( "Size was not as expected.", maxSize, array.size() );
+
+ // this is a fragile test, since it relies on a hardcoded array
+ String smallest = (String) array.getSmallest();
+ assertEquals( "smallest is not as expected", "01", smallest );
+
+ String largest = (String) array.getLargest();
+ assertEquals( "Largest is not as expected", "09", largest );
+
+ // this should take 96;
+ String taken = (String) array.takeNearestLargerOrEqual( "09" );
+ assertEquals( "Taken is not as expected", "09", taken );
+ assertEquals( "Size was not as expected.", ( maxSize - 1 ), array.size() );
+
+ System.out.println( "testTakeLastItem" + array.dumpArray() );
+ }
+
+ /**
+ * Test taking every last item.
+ *
+ * @exception Exception
+ */
+ public void testTakeEveryLastItem()
+ throws Exception
+ {
+ int maxSize = 9;
+
+ SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
+ // array.setPreferLarge( false );
+ array.setPreferLarge( true );
+ String[] elem = {
+ "01",
+ "02",
+ "03",
+ "04",
+ "05",
+ "08",
+ "07",
+ "06",
+ "09",};
+
+ // put more than the max in a random order
+ for ( int i = 0; i < elem.length; i++ )
+ {
+ array.add( elem[i] );
+ System.out.println( array.dumpArray() );
+ }
+
+ assertEquals( "Size was not as expected.", maxSize, array.size() );
+
+ // this is a fragile test, since it relies on a hardcoded array
+ String smallest = (String) array.getSmallest();
+ assertEquals( "smallest is not as expected", "01", smallest );
+
+ String largest = (String) array.getLargest();
+ assertEquals( "Largest is not as expected", "09", largest );
+
+ // this should take 96;
+ String taken = (String) array.takeNearestLargerOrEqual( "09" );
+ assertEquals( "Taken is not as expected", "09", taken );
+ assertEquals( "Size was not as expected.", ( maxSize - 1 ), array.size() );
+
+ System.out.println( "testTakeEveryLastItem" + array.dumpArray() );
+
+ // take the rest
+ // put more than the max in a random order
+ for ( int i = elem.length -1; i >= 0; i-- )
+ {
+ array.takeNearestLargerOrEqual( elem[i] );
+ }
+ System.out.println( "testTakeEveryLastItem" + array.dumpArray() );
+
+ assertEquals( "There should nothing left.", 0, array.size() );
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-dev-help@jakarta.apache.org