/** * Creates the file and set the block size in bytes. * <p> * @param file * @param blockSizeBytes * @throws IOException */ public BlockDisk( File file, int blockSizeBytes ) throws IOException { this( file, blockSizeBytes, new StandardSerializer() ); }
/** * Creates the file and set the block size in bytes. * <p> * @param file * @param blockSizeBytes * @throws FileNotFoundException */ public BlockDisk( File file, int blockSizeBytes ) throws FileNotFoundException { this( file, blockSizeBytes, new StandardSerializer() ); }
/** * Total from the start to the endPostion. * <p> * @param elements * @param startPosition * @param endPosition * @return size * @throws IOException */ public static <K, V> long totalSize( ICacheElement<K, V>[] elements, int startPosition, int endPosition ) throws IOException { StandardSerializer serializer = new StandardSerializer(); long total = 0; for ( int i = startPosition; i < endPosition; i++ ) { int tileSize = serializer.serialize( elements[i] ).length + IndexedDisk.HEADER_SIZE_BYTES; total += tileSize; } return total; }
public void testJCS156() throws Exception { // SETUP String fileName = "testJCS156"; File file = new File( rafDir, fileName + ".data" ); file.delete(); int blockSizeBytes = 4096; BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); long offset = disk.calculateByteOffsetForBlockAsLong(Integer.MAX_VALUE); assertTrue("Must not wrap round", offset > 0); assertEquals(Integer.MAX_VALUE*4096L,offset); file.delete(); } }
/** * Verify that it says we need two blocks if the total size will fit. * <p> * @throws Exception */ public void testCalculateBlocksNeededDouble() throws Exception { // SETUP String fileName = "testCalculateBlocksNeededDouble"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2 - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] ); // Verify assertEquals( "Wrong number of blocks", 2, result ); }
/** * Test writing and reading an element within a single block size. * <p> * @throws Exception */ public void testWriteAndRead_SingleBlockElement() throws Exception { // SETUP String fileName = "testWriteAndRead_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks = disk.write( new byte[bytes] ); byte[] result = (byte[]) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); }
/** * Verify null for null. * <p> * @throws IOException */ public void testgGetSerializedCacheElement_null() throws IOException { // SETUP IElementSerializer elementSerializer = new StandardSerializer(); ICacheElement<String, String> before = null; // DO WORK ICacheElementSerialized<String, String> result = SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer ); // VERIFY assertNull( "Should get null for null", result ); }
/** * Verify null for null. * <p> * @throws Exception */ public void testgGetDeSerializedCacheElement_null() throws Exception { // SETUP IElementSerializer elementSerializer = new StandardSerializer(); ICacheElementSerialized<String, String> before = null; // DO WORK ICacheElement<String, String> result = SerializationConversionUtil.getDeSerializedCacheElement( before, elementSerializer ); // VERIFY assertNull( "Should get null for null", result ); }
/** * Verify null for null. * <p> * @throws IOException */ public void testgGetSerializedCacheElement_null() throws IOException { // SETUP IElementSerializer elementSerializer = new StandardSerializer(); ICacheElement<String, String> before = null; // DO WORK ICacheElementSerialized<String, String> result = SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer ); // VERIFY assertNull( "Should get null for null", result ); }
/** * Verify null for null. * <p> * @throws Exception */ public void testgGetDeSerializedCacheElement_null() throws Exception { // SETUP IElementSerializer elementSerializer = new StandardSerializer(); ICacheElementSerialized<String, String> before = null; // DO WORK ICacheElement<String, String> result = SerializationConversionUtil.getDeSerializedCacheElement( before, elementSerializer ); // VERIFY assertNull( "Should get null for null", result ); }
/** * 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 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 testWrite_SingleBlockElement() throws Exception { // SETUP String fileName = "testWrite_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks = disk.write( new byte[bytes] ); // VERIFY 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 simple back and forth with a string. *<p> * @throws Exception */ public void testSimpleBackAndForth() throws Exception { // SETUP StandardSerializer serializer = new StandardSerializer(); String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231"; // DO WORK String after = (String) serializer.deSerialize( serializer.serialize( before ), null ); // VERIFY assertEquals( "Before and after should be the same.", before, after ); }
/** * Test simple back and forth with a string. *<p> * @throws Exception */ public void testSimpleBackAndForth() throws Exception { // SETUP StandardSerializer serializer = new StandardSerializer(); String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231"; // DO WORK String after = (String) serializer.deSerialize( serializer.serialize( before ), null ); // VERIFY assertEquals( "Before and after should be the same.", before, after ); }
/** * Test serialization with a null object. Verify that we don't get an error. *<p> * @throws Exception */ public void testNullInput() throws Exception { // SETUP StandardSerializer serializer = new StandardSerializer(); String before = null; // DO WORK byte[] serialized = serializer.serialize( before ); //System.out.println( "testNullInput " + serialized ); String after = (String) serializer.deSerialize( serialized, null ); //System.out.println( "testNullInput " + after ); // VERIFY assertNull( "Should have nothing.", after ); }
/** * Test serialization with a null object. Verify that we don't get an error. *<p> * @throws Exception */ public void testNullInput() throws Exception { // SETUP StandardSerializer serializer = new StandardSerializer(); String before = null; // DO WORK byte[] serialized = serializer.serialize( before ); //System.out.println( "testNullInput " + serialized ); String after = (String) serializer.deSerialize( serialized, null ); //System.out.println( "testNullInput " + after ); // VERIFY assertNull( "Should have nothing.", after ); }
/** * Test writing an element that takes two blocks. * <p> * @throws Exception */ public void testWrite_DoubleBlockElement() throws Exception { // SETUP String fileName = "testWriteDoubleBlockElement"; 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(), 2 ); int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 2, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing an element that takes two blocks. * <p> * @throws Exception */ public void testWrite_DoubleBlockElement() throws Exception { // SETUP String fileName = "testWriteDoubleBlockElement"; 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(), 2 ); int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 2, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** Create the listener for testing */ @Override protected void setUp() throws Exception { factory = new LateralTCPCacheFactory(); factory.initialize(); cacheMgr = CompositeCacheManager.getInstance(); cacheEventLogger = new MockCacheEventLogger(); elementSerializer = new StandardSerializer(); listener = new LateralTCPDiscoveryListener( factory.getName(), cacheMgr ); }
/** Create the listener for testing */ @Override protected void setUp() throws Exception { factory = new LateralTCPCacheFactory(); factory.initialize(); cacheMgr = CompositeCacheManager.getInstance(); cacheEventLogger = new MockCacheEventLogger(); elementSerializer = new StandardSerializer(); listener = new LateralTCPDiscoveryListener( factory.getName(), cacheMgr ); }