/** * The main program for the DiskDumper class * <p> * Creates a disk cache and then calls dump, which write out the contents to * a debug log. * <p> * @param args * The command line arguments */ public static void main( String[] args ) { if ( args.length != 1 ) { System.out.println( "Usage: java org.apache.commons.jcs.auxiliary.disk.DiskDump <cache_name>" ); System.exit( 0 ); } IndexedDiskCacheAttributes attr = new IndexedDiskCacheAttributes(); attr.setCacheName( args[0] ); attr.setDiskPath( args[0] ); IndexedDiskCache<Serializable, Serializable> dc = new IndexedDiskCache<Serializable, Serializable>( attr ); dc.dump( true ); System.exit( 0 ); } }
cattr.setCacheName( "testRecyleBinSize" ); cattr.setDiskPath( "target/test-sandbox/UnitTest" ); cattr.setMaxRecycleBinSize( numberToInsert ); cattr.setOptimizeAtRemoveCount( numberToInsert ); cattr.setMaxKeySize( numberToInsert * 2 ); cattr.setMaxPurgatorySize( numberToInsert ); IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<Integer, DiskTestObject>( cattr ); .getCacheName() );
this.maxKeySize = cattr.getMaxKeySize(); this.isRealTimeOptimizationEnabled = cattr.getOptimizeAtRemoveCount() > 0; this.isShutdownOptimizationEnabled = cattr.isOptimizeOnShutdown(); this.logCacheName = "Region [" + getCacheName() + "] "; this.diskLimitType = cattr.getDiskLimitType();
@Override public IndexedDiskCacheAttributes getCacheAttributes() { IndexedDiskCacheAttributes ret = new IndexedDiskCacheAttributes(); ret.setDiskLimitType(DiskLimitType.COUNT); return ret; } public void testRecycleBin()
/** * Test the basic get matching. * <p> * @throws Exception */ public void testPutGetMatching_SmallWait() throws Exception { // SETUP int items = 200; String cacheName = "testPutGetMatching_SmallWait"; IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName( cacheName ); cattr.setMaxKeySize( 100 ); cattr.setDiskPath( "target/test-sandbox/IndexDiskCacheUnitTest" ); IndexedDiskCache<String, String> diskCache = new IndexedDiskCache<String, String>( cattr ); // DO WORK for ( int i = 0; i <= items; i++ ) { diskCache.update( new CacheElement<String, String>( cacheName, i + ":key", cacheName + " data " + i ) ); } Thread.sleep( 500 ); Map<String, ICacheElement<String, String>> matchingResults = diskCache.getMatching( "1.8.+" ); // VERIFY assertEquals( "Wrong number returned", 10, matchingResults.size() ); //System.out.println( "matchingResults.keySet() " + matchingResults.keySet() ); //System.out.println( "\nAFTER TEST \n" + diskCache.getStats() ); }
IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes(); cattr.setCacheName( "testOptimization" ); cattr.setMaxKeySize( removeCount * 2 ); cattr.setOptimizeAtRemoveCount( removeCount ); cattr.setMaxRecycleBinSize( removeCount * 3 ); cattr.setDiskPath( "target/test-sandbox/testOptimization" ); IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<Integer, DiskTestObject>( cattr ); .createCacheElementsWithTestObjectsOfVariableSizes( numberToInsert, cattr.getCacheName() );
/** * Verify that the file size is as expected. * <p> * * @throws IOException * @throws InterruptedException */ public void testFileSize() throws IOException, InterruptedException { // SETUP IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName("testFileSize"); cattr.setDiskPath("target/test-sandbox/UnitTest"); IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<Integer, DiskTestObject>(cattr); int numberToInsert = 20; int bytes = 24; ICacheElement<Integer, DiskTestObject>[] elements = DiskTestObjectUtil.createCacheElementsWithTestObjects(numberToInsert, bytes, cattr.getCacheName()); for (int i = 0; i < elements.length; i++) { disk.processUpdate(elements[i]); } Thread.yield(); Thread.sleep(100); Thread.yield(); long expectedSize = DiskTestObjectUtil.totalSize(elements, numberToInsert); long resultSize = disk.getDataFileSize(); // System.out.println( "testFileSize stats " + disk.getStats() ); assertEquals("Wrong file size", expectedSize, resultSize); }
IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes(); cattr.setName("junit"); cattr.setCacheName("junit"); cattr.setDiskPath("."); IndexedDiskCache<String, String> idc = new IndexedDiskCache<String, String>(cattr);
/** * Verify that the overlap check returns true when there are no overlaps. */ public void testCheckForDedOverlaps_noOverlap() { // SETUP IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName( "testCheckForDedOverlaps_noOverlap" ); cattr.setDiskPath( "target/test-sandbox/UnitTest" ); IndexedDiskCache<String, String> disk = new IndexedDiskCache<String, String>( cattr ); int numDescriptors = 5; int pos = 0; IndexedDiskElementDescriptor[] sortedDescriptors = new IndexedDiskElementDescriptor[numDescriptors]; for ( int i = 0; i < numDescriptors; i++ ) { IndexedDiskElementDescriptor descriptor = new IndexedDiskElementDescriptor( pos, i * 2 ); pos = pos + ( i * 2 ) + IndexedDisk.HEADER_SIZE_BYTES; sortedDescriptors[i] = descriptor; } // DO WORK boolean result = disk.checkForDedOverlaps( sortedDescriptors ); // VERIFY assertTrue( "There should be no overlap. it should be ok", result ); }
/** * Verify that removeAll is prohibited. * <p> * @throws IOException */ public void testRemoveAll_notAllowed() throws IOException { // SETUP StringWriter stringWriter = new StringWriter(); TestLogConfigurationUtil.configureLogger( stringWriter, AbstractDiskCache.class.getName() ); IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes(); diskCacheAttributes.setAllowRemoveAll( false ); AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<String, String>( diskCacheAttributes ); String cacheName = "testRemoveAll_notAllowed"; String key = "myKey"; String value = "myValue"; IElementAttributes elementAttributes = new ElementAttributes(); ICacheElement<String, String> cacheElement = new CacheElement<String, String>( cacheName, key, value, elementAttributes ); diskCache.update( cacheElement ); // DO WORK diskCache.removeAll(); String result = stringWriter.toString(); // VERIFY assertTrue( "Should say not allowed.", result.indexOf( "set to false" ) != -1 ); assertNotNull( "Item should be in the map.", diskCache.get( key ) ); }
/** * Tries to create the root directory if it does not already exist. * <p> * * @param cattr */ private void initializeFileSystem(IndexedDiskCacheAttributes cattr) { this.rafDir = cattr.getDiskPath(); if (log.isInfoEnabled()) { log.info(logCacheName + "Cache file root directory: " + rafDir); } }
this.keyFile = new IndexedDisk(new File(rafDir, fileName + ".key"), getElementSerializer()); if (cattr.isClearDiskOnStartup())
if ( isRealTimeOptimizationEnabled && !isOptimizing && removeCount++ >= cattr.getOptimizeAtRemoveCount() ) + cattr.getOptimizeAtRemoveCount() + "]" );
/** * Test the basic get matching. * <p> * * @throws Exception */ public void testPutGetMatching_SmallWait() throws Exception { // SETUP int items = 200; String cacheName = "testPutGetMatching_SmallWait"; IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMaxKeySize(100); cattr.setDiskPath("target/test-sandbox/IndexDiskCacheUnitTest"); IndexedDiskCache<String, String> diskCache = new IndexedDiskCache<String, String>(cattr); // DO WORK for (int i = 0; i <= items; i++) { diskCache.update(new CacheElement<String, String>(cacheName, i + ":key", cacheName + " data " + i)); } Thread.sleep(500); Map<String, ICacheElement<String, String>> matchingResults = diskCache.getMatching("1.8.+"); // VERIFY assertEquals("Wrong number returned", 10, matchingResults.size()); // System.out.println( "matchingResults.keySet() " + matchingResults.keySet() ); // System.out.println( "\nAFTER TEST \n" + diskCache.getStats() ); }
IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes(); cattr.setCacheName( "testOptimization" ); cattr.setMaxKeySize( removeCount * 2 ); cattr.setOptimizeAtRemoveCount( removeCount ); cattr.setDiskPath( "target/test-sandbox/testOptimization" ); IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<Integer, DiskTestObject>( cattr ); .createCacheElementsWithTestObjectsOfVariableSizes( numberToInsert, cattr.getCacheName() );
/** * Verify that the file size is as expected. * <p> * @throws IOException * @throws InterruptedException */ public void testFileSize() throws IOException, InterruptedException { // SETUP IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName( "testFileSize" ); cattr.setDiskPath( "target/test-sandbox/UnitTest" ); IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<Integer, DiskTestObject>( cattr ); int numberToInsert = 20; int bytes = 24; ICacheElement<Integer, DiskTestObject>[] elements = DiskTestObjectUtil.createCacheElementsWithTestObjects( numberToInsert, bytes, cattr .getCacheName() ); for ( int i = 0; i < elements.length; i++ ) { disk.processUpdate( elements[i] ); } Thread.yield(); Thread.sleep( 100 ); Thread.yield(); long expectedSize = DiskTestObjectUtil.totalSize( elements, numberToInsert ); long resultSize = disk.getDataFileSize(); //System.out.println( "testFileSize stats " + disk.getStats() ); assertEquals( "Wrong file size", expectedSize, resultSize ); }
IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes(); cattr.setName("junit"); cattr.setCacheName("junit"); cattr.setDiskPath("."); IndexedDiskCache<String, String> idc = new IndexedDiskCache<String, String>(cattr);
/** * Verify that the overlap check returns true when there are no overlaps. */ public void testCheckForDedOverlaps_noOverlap() { // SETUP IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName("testCheckForDedOverlaps_noOverlap"); cattr.setDiskPath("target/test-sandbox/UnitTest"); IndexedDiskCache<String, String> disk = new IndexedDiskCache<String, String>(cattr); int numDescriptors = 5; int pos = 0; IndexedDiskElementDescriptor[] sortedDescriptors = new IndexedDiskElementDescriptor[numDescriptors]; for (int i = 0; i < numDescriptors; i++) { IndexedDiskElementDescriptor descriptor = new IndexedDiskElementDescriptor(pos, i * 2); pos = pos + (i * 2) + IndexedDisk.HEADER_SIZE_BYTES; sortedDescriptors[i] = descriptor; } // DO WORK boolean result = disk.checkForDedOverlaps(sortedDescriptors); // VERIFY assertTrue("There should be no overlap. it should be ok", result); }
@Override public IndexedDiskCacheAttributes getCacheAttributes() { IndexedDiskCacheAttributes ret = new IndexedDiskCacheAttributes(); ret.setDiskLimitType(DiskLimitType.SIZE); return ret; } public void testRecycleBin()
/** * Verify that removeAll is prohibited. * <p> * @throws IOException */ public void testRemoveAll_notAllowed() throws IOException { // SETUP StringWriter stringWriter = new StringWriter(); TestLogConfigurationUtil.configureLogger( stringWriter, AbstractDiskCache.class.getName() ); IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes(); diskCacheAttributes.setAllowRemoveAll( false ); AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<String, String>( diskCacheAttributes ); String cacheName = "testRemoveAll_notAllowed"; String key = "myKey"; String value = "myValue"; IElementAttributes elementAttributes = new ElementAttributes(); ICacheElement<String, String> cacheElement = new CacheElement<String, String>( cacheName, key, value, elementAttributes ); diskCache.update( cacheElement ); // DO WORK diskCache.removeAll(); String result = stringWriter.toString(); // VERIFY assertTrue( "Should say not allowed.", result.indexOf( "set to false" ) != -1 ); assertNotNull( "Item should be in the map.", diskCache.get( key ) ); }