Author: asmuts Date: Tue Aug 11 17:15:38 2009 New Revision: 803209 URL: http://svn.apache.org/viewvc?rev=803209&view=rev Log: Fixed bug in block disk cache. It couldn't handle items with more than 127 blocks. Now it can. (Note: for performance reasons, you should try to fit your items in as few blocks as possible.)
Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/block/BlockDisk.java jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/block/BlockDisk.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/block/BlockDisk.java?rev=803209&r1=803208&r2=803209&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/block/BlockDisk.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/block/BlockDisk.java Tue Aug 11 17:15:38 2009 @@ -177,7 +177,7 @@ int[] blocks = new int[numBlocksNeeded]; // get them from the empty list or take the next one - for ( short i = 0; i < numBlocksNeeded; i++ ) + for ( int i = 0; i < numBlocksNeeded; i++ ) { Integer emptyBlock = (Integer) emptyBlocks.takeFirst(); if ( emptyBlock != null ) @@ -194,7 +194,7 @@ byte[][] chunks = getBlockChunks( data, numBlocksNeeded ); // write the blocks - for ( byte i = 0; i < numBlocksNeeded; i++ ) + for ( int i = 0; i < numBlocksNeeded; i++ ) { int position = calculateByteOffsetForBlock( blocks[i] ); write( position, chunks[i] ); Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java?rev=803209&r1=803208&r2=803209&view=diff ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java (original) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java Tue Aug 11 17:15:38 2009 @@ -47,15 +47,39 @@ } /** + * Test writing a null object within a single block size. + * <p> + * @throws Exception + */ + public void testWrite_NullBlockElement() + throws Exception + { + // SETUP + String fileName = "testWrite_NullBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); + + // DO WORK + int[] blocks = disk.write( null ); + + // VERIFY + System.out.println( "testWrite_NullBlockElement " + disk ); + assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() ); + assertEquals( "Wrong number of blocks returned.", 1, blocks.length ); + assertEquals( "Wrong block returned.", 0, blocks[0] ); + } + + /** * Test writing an element within a single block size. * <p> * @throws Exception */ - public void testWriteSingleBlockElement() + public void testWrite_SingleBlockElement() throws Exception { // SETUP - String fileName = "testWriteSingleBlockElement"; + String fileName = "testWrite_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); @@ -76,11 +100,11 @@ * <p> * @throws Exception */ - public void testWriteAndReadSingleBlockElement() + public void testWriteAndRead_SingleBlockElement() throws Exception { // SETUP - String fileName = "testWriteAndReadSingleBlockElement"; + String fileName = "testWriteAndRead_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); @@ -100,11 +124,11 @@ * <p> * @throws Exception */ - public void testWriteTwoSingleBlockElements() + public void testWrite_TwoSingleBlockElements() throws Exception { // SETUP - String fileName = "testWriteSingleBlockElement"; + String fileName = "testWrite_TwoSingleBlockElements"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); @@ -149,7 +173,7 @@ * <p> * @throws Exception */ - public void testWriteDoubleBlockElement() + public void testWrite_DoubleBlockElement() throws Exception { // SETUP @@ -168,6 +192,33 @@ assertEquals( "Wrong number of blocks returned.", 2, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); } + + /** + * Test writing an element that takes 128 blocks. There was a byte in a for loop that limited the number to 127. I fixed this. + * <p> + * @throws Exception + */ + public void testWrite_128BlockElement() + throws Exception + { + // SETUP + int numBlocks = 128; + + String fileName = "testWrite_128BlockElement"; + File file = new File( rafDir, fileName + ".data" ); + BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); + + // DO WORK + // byte arrays encur 27 bytes of serialization overhead. + int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocks ); + int[] blocks = disk.write( new byte[bytes] ); + + // VERIFY + System.out.println( "testWriteDoubleBlockElement " + disk ); + assertEquals( "Wrong number of blocks recorded.", numBlocks, disk.getNumberOfBlocks() ); + assertEquals( "Wrong number of blocks returned.", numBlocks, blocks.length ); + assertEquals( "Wrong block returned.", 0, blocks[0] ); + } /** * Test writing and reading elements that do not fit within a single block. @@ -259,7 +310,7 @@ File file = new File( rafDir, fileName + ".data" ); file.delete(); int blockSizeBytes = 4096;//1024; - BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); + BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); String string = "This is my big string ABCDEFGH"; StringBuffer sb = new StringBuffer(); @@ -277,10 +328,10 @@ // VERIFY System.out.println( string ); System.out.println( result ); - System.out.println( disk ); + System.out.println( disk ); assertEquals( "Wrong item retured.", string, result ); } - + /** * Verify that the block disk can handle a big string. * <p> @@ -294,18 +345,17 @@ File file = new File( rafDir, fileName + ".data" ); file.delete(); int blockSizeBytes = 47;//4096;//1024; - BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); + BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); String string = "abcdefghijklmnopqrstuvwxyz1234567890"; string += string; string += string; - // DO WORK int[] blocks = disk.write( string ); String result = (String) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", string, result ); - } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: jcs-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: jcs-dev-h...@jakarta.apache.org