Changeset 10652 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/cache/JCSCacheManager.java
r10570 r10652 45 45 private static long maxObjectTTL = -1; 46 46 private static final String PREFERENCE_PREFIX = "jcs.cache"; 47 public static BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);47 public static final BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true); 48 48 49 49 private static final AuxiliaryCacheFactory diskCacheFactory = … … 173 173 if (cachePath != null && cacheDirLock != null) { 174 174 IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName); 175 /*176 * BlockDiskCache never optimizes the file, so when file size is reduced, it will never be truncated to desired size.177 *178 * If for some mysterious reason, file size is greater than the value set in preferences, just use the whole file. If the user179 * wants to reduce the file size, (s)he may just go to preferences and there it should be handled (by removing old file)180 */181 if (USE_BLOCK_CACHE.get()) {182 File diskCacheFile = new File(cachePath + File.separator + diskAttributes.getCacheName() + ".data");183 maxDiskObjects = (int) Math.max(maxDiskObjects, diskCacheFile.length()/1024);184 }185 175 try { 186 176 if (cc.getAuxCaches().length == 0) { … … 210 200 private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) { 211 201 IDiskCacheAttributes ret; 202 removeStaleFiles(cachePath + File.separator + cacheName, (USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2")); 203 cacheName = cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2"); 204 212 205 if (USE_BLOCK_CACHE.get()) { 213 206 BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes(); 214 blockAttr.setMaxKeySize(maxDiskObjects); 207 /* 208 * BlockDiskCache never optimizes the file, so when file size is reduced, it will never be truncated to desired size. 209 * 210 * If for some mysterious reason, file size is greater than the value set in preferences, just use the whole file. If the user 211 * wants to reduce the file size, (s)he may just go to preferences and there it should be handled (by removing old file) 212 */ 213 File diskCacheFile = new File(cachePath + File.separator + cacheName + ".data"); 214 if (diskCacheFile.exists()) { 215 blockAttr.setMaxKeySize((int) Math.max(maxDiskObjects, diskCacheFile.length()/1024)); 216 } else { 217 blockAttr.setMaxKeySize(maxDiskObjects); 218 } 215 219 blockAttr.setBlockSizeBytes(4096); // use 4k blocks 216 220 ret = blockAttr; … … 227 231 ret.setDiskPath(cachePath); 228 232 } 229 ret.setCacheName(cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2")); 230 231 removeStaleFiles(cachePath + File.separator + cacheName, (USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2")); 233 ret.setCacheName(cacheName); 234 232 235 return ret; 233 236 } -
trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCacheManagerTest.java
r10559 r10652 2 2 package org.openstreetmap.josm.data.cache; 3 3 4 import static org.junit.Assert.assertEquals; 5 6 import java.io.File; 7 import java.io.FileOutputStream; 4 8 import java.io.IOException; 5 9 import java.util.logging.Logger; 6 10 11 import org.apache.commons.jcs.access.CacheAccess; 12 import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes; 7 13 import org.junit.BeforeClass; 8 14 import org.junit.Test; … … 31 37 Logger.getLogger("org.apache.commons.jcs").warning("{switch:0}"); 32 38 } 39 40 @Test 41 public void testUseBigDiskFile() throws IOException { 42 if (JCSCacheManager.USE_BLOCK_CACHE.get()) { 43 // test only when using block cache 44 File cacheFile = new File("foobar/testUseBigDiskFile_BLOCK_v2.data"); 45 if (!cacheFile.exists()) { 46 cacheFile.createNewFile(); 47 } 48 try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile, false)) { 49 fileOutputStream.getChannel().truncate(0); 50 fileOutputStream.write(new byte[1024*1024*10]); // create 10MB empty file 51 } 52 53 CacheAccess<Object, Object> cache = JCSCacheManager.getCache("testUseBigDiskFile", 1, 100, "foobar"); 54 assertEquals("BlockDiskCache use file size to calculate its size", 10*1024, 55 ((BlockDiskCacheAttributes)cache.getCacheControl().getAuxCaches()[0].getAuxiliaryCacheAttributes()).getMaxKeySize()); 56 } 57 } 58 33 59 }
Note:
See TracChangeset
for help on using the changeset viewer.