Changeset 10652 in josm


Ignore:
Timestamp:
2016-07-26T22:06:52+02:00 (8 years ago)
Author:
wiktorn
Message:

Fix not using the calculated disk cache size.

Add unit tests for #13128 - using existing file size if greater than desired cache size.

Closes #13128

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCacheManager.java

    r10570 r10652  
    4545    private static long maxObjectTTL = -1;
    4646    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);
    4848
    4949    private static final AuxiliaryCacheFactory diskCacheFactory =
     
    173173        if (cachePath != null && cacheDirLock != null) {
    174174            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 user
    179              * 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             }
    185175            try {
    186176                if (cc.getAuxCaches().length == 0) {
     
    210200    private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) {
    211201        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
    212205        if (USE_BLOCK_CACHE.get()) {
    213206            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            }
    215219            blockAttr.setBlockSizeBytes(4096); // use 4k blocks
    216220            ret = blockAttr;
     
    227231            ret.setDiskPath(cachePath);
    228232        }
    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
    232235        return ret;
    233236    }
  • trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCacheManagerTest.java

    r10559 r10652  
    22package org.openstreetmap.josm.data.cache;
    33
     4import static org.junit.Assert.assertEquals;
     5
     6import java.io.File;
     7import java.io.FileOutputStream;
    48import java.io.IOException;
    59import java.util.logging.Logger;
    610
     11import org.apache.commons.jcs.access.CacheAccess;
     12import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
    713import org.junit.BeforeClass;
    814import org.junit.Test;
     
    3137        Logger.getLogger("org.apache.commons.jcs").warning("{switch:0}");
    3238    }
     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
    3359}
Note: See TracChangeset for help on using the changeset viewer.