Changeset 10323 in josm


Ignore:
Timestamp:
2016-06-04T16:28:04+02:00 (8 years ago)
Author:
wiktorn
Message:

Block/Indexed cache enhancements.

  • introduce a BooleanProperty that switches between Block and Indexed mode
  • introduce suffix for cache names based on type
  • clean stale files on cache initialization (clears indexed files when block mode is selected and vice versa, clears also no-prefix names)

Closes: #12909

File:
1 edited

Legend:

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

    r10235 r10323  
    1919import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
    2020import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheFactory;
     21import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
     22import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory;
    2123import org.apache.commons.jcs.engine.CompositeCacheAttributes;
    2224import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes.DiskUsagePattern;
     
    2628import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    2729import org.openstreetmap.josm.Main;
     30import org.openstreetmap.josm.data.preferences.BooleanProperty;
    2831import org.openstreetmap.josm.data.preferences.IntegerProperty;
    2932
     
    4144    private static long maxObjectTTL        = -1;
    4245    private static final String PREFERENCE_PREFIX = "jcs.cache";
    43     private static final AuxiliaryCacheFactory diskCacheFactory = new BlockDiskCacheFactory();
     46    private static BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
     47
     48    private static final AuxiliaryCacheFactory diskCacheFactory =
     49            USE_BLOCK_CACHE.get() ? new BlockDiskCacheFactory() : new IndexedDiskCacheFactory();
    4450    private static FileLock cacheDirLock;
    4551
     
    163169
    164170        if (cachePath != null && cacheDirLock != null) {
    165             IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath);
    166             diskAttributes.setCacheName(cacheName);
     171            IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName);
    167172            try {
    168173                if (cc.getAuxCaches().length == 0) {
     
    190195    }
    191196
    192     private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath) {
    193         BlockDiskCacheAttributes ret = new BlockDiskCacheAttributes();
     197    private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) {
     198        IDiskCacheAttributes ret;
     199        if (USE_BLOCK_CACHE.get()) {
     200            BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes();
     201            blockAttr.setMaxKeySize(maxDiskObjects);
     202            ret = blockAttr;
     203        } else {
     204            IndexedDiskCacheAttributes indexAttr = new IndexedDiskCacheAttributes();
     205            indexAttr.setMaxKeySize(maxDiskObjects);
     206            ret = indexAttr;
     207        }
    194208        ret.setDiskLimitType(IDiskCacheAttributes.DiskLimitType.SIZE);
    195         ret.setMaxKeySize(maxDiskObjects);
    196         if (cachePath != null) {
    197             File path = new File(cachePath);
    198             if (!path.exists() && !path.mkdirs()) {
    199                 LOG.log(Level.WARNING, "Failed to create cache path: {0}", cachePath);
    200             } else {
    201                 ret.setDiskPath(path);
    202             }
    203         }
     209        File path = new File(cachePath);
     210        if (!path.exists() && !path.mkdirs()) {
     211            LOG.log(Level.WARNING, "Failed to create cache path: {0}", cachePath);
     212        } else {
     213            ret.setDiskPath(cachePath);
     214        }
     215        ret.setCacheName(cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK" : "_INDEX"));
     216
     217        removeStaleFiles(cachePath + File.separator + cacheName, (USE_BLOCK_CACHE.get() ? "_INDEX" : "_BLOCK"));
    204218        return ret;
     219    }
     220
     221    private static void removeStaleFiles(String basePathPart, String suffix) {
     222        deleteCacheFiles(basePathPart); // TODO: this can be removed around 2016.09
     223        deleteCacheFiles(basePathPart + suffix);
     224    }
     225
     226    private static void deleteCacheFiles(String basePathPart) {
     227        new File(basePathPart + ".key").delete();
     228        new File(basePathPart + ".data").delete();
    205229    }
    206230
Note: See TracChangeset for help on using the changeset viewer.