Author: tv
Date: Fri Oct 21 16:02:29 2016
New Revision: 1766058

URL: http://svn.apache.org/viewvc?rev=1766058&view=rev
Log:
Replace SortedPreferentialArray with JDK ConcurrentSkipListSet

Removed:
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/struct/SortedPreferentialArray.java
    
commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/SortedPrefArrayUnitTest.java
Modified:
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java?rev=1766058&r1=1766057&r2=1766058&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
 Fri Oct 21 16:02:29 2016
@@ -33,6 +33,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentSkipListSet;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -53,7 +54,6 @@ import org.apache.commons.jcs.engine.sta
 import org.apache.commons.jcs.engine.stats.behavior.IStats;
 import org.apache.commons.jcs.utils.struct.AbstractLRUMap;
 import org.apache.commons.jcs.utils.struct.LRUMap;
-import org.apache.commons.jcs.utils.struct.SortedPreferentialArray;
 import org.apache.commons.jcs.utils.timing.ElapsedTimer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -114,10 +114,11 @@ public class IndexedDiskCache<K, V> exte
     private boolean queueInput = false;
 
     /** list where puts made during optimization are made */
-    private final LinkedList<IndexedDiskElementDescriptor> queuedPutList = new 
LinkedList<IndexedDiskElementDescriptor>();
+    private final ConcurrentSkipListSet<IndexedDiskElementDescriptor> 
queuedPutList =
+            new ConcurrentSkipListSet<IndexedDiskElementDescriptor>(new 
PositionComparator());
 
     /** RECYLCE BIN -- array of empty spots */
-    private SortedPreferentialArray<IndexedDiskElementDescriptor> recycle;
+    private ConcurrentSkipListSet<IndexedDiskElementDescriptor> recycle;
 
     /** User configurable parameters */
     private final IndexedDiskCacheAttributes cattr;
@@ -198,7 +199,7 @@ public class IndexedDiskCache<K, V> exte
                 doOptimizeRealTime();
             }
         }
-        catch (Exception e)
+        catch (IOException e)
         {
             log.error(
                 logCacheName + "Failure initializing for fileName: " + 
fileName + " and directory: "
@@ -229,9 +230,8 @@ public class IndexedDiskCache<K, V> exte
      *
      * @param cattr
      * @throws IOException
-     * @throws InterruptedException
      */
-    private void initializeKeysAndData(IndexedDiskCacheAttributes cattr) 
throws IOException, InterruptedException
+    private void initializeKeysAndData(IndexedDiskCacheAttributes cattr) 
throws IOException
     {
         this.dataFile = new IndexedDisk(new File(rafDir, fileName + ".data"), 
getElementSerializer());
         this.keyFile = new IndexedDisk(new File(rafDir, fileName + ".key"), 
getElementSerializer());
@@ -279,10 +279,9 @@ public class IndexedDiskCache<K, V> exte
      * files are cleared.
      * <p>
      *
-     * @throws InterruptedException
      * @throws IOException
      */
-    private void initializeStoreFromPersistedData() throws 
InterruptedException, IOException
+    private void initializeStoreFromPersistedData() throws IOException
     {
         loadKeys();
 
@@ -313,11 +312,8 @@ public class IndexedDiskCache<K, V> exte
     /**
      * Loads the keys from the .key file. The keys are stored in a HashMap on 
disk. This is
      * converted into a LRUMap.
-     * <p>
-     *
-     * @throws InterruptedException
      */
-    protected void loadKeys() throws InterruptedException
+    protected void loadKeys()
     {
         if (log.isDebugEnabled())
         {
@@ -331,8 +327,8 @@ public class IndexedDiskCache<K, V> exte
             // create a key map to use.
             initializeKeyMap();
 
-            HashMap<K, IndexedDiskElementDescriptor> keys = 
keyFile.readObject(new IndexedDiskElementDescriptor(0, (int) keyFile
-                .length() - IndexedDisk.HEADER_SIZE_BYTES));
+            HashMap<K, IndexedDiskElementDescriptor> keys = keyFile.readObject(
+                new IndexedDiskElementDescriptor(0, (int) keyFile.length() - 
IndexedDisk.HEADER_SIZE_BYTES));
 
             if (keys != null)
             {
@@ -406,7 +402,7 @@ public class IndexedDiskCache<K, V> exte
                 isOk = 
checkForDedOverlaps(createPositionSortedDescriptorList());
             }
         }
-        catch (Exception e)
+        catch (IOException e)
         {
             log.error(e);
             isOk = false;
@@ -484,7 +480,7 @@ public class IndexedDiskCache<K, V> exte
                 log.info(logCacheName + "Finished saving keys.");
             }
         }
-        catch (Exception e)
+        catch (IOException e)
         {
             log.error(logCacheName + "Problem storing keys.", e);
         }
@@ -543,9 +539,11 @@ public class IndexedDiskCache<K, V> exte
 
                     if (doRecycle)
                     {
-                        IndexedDiskElementDescriptor rep = 
recycle.takeNearestLargerOrEqual(ded);
+                        IndexedDiskElementDescriptor rep = 
recycle.ceiling(ded);
                         if (rep != null)
                         {
+                            // remove element from recycle bin
+                            recycle.remove(rep);
                             ded = rep;
                             ded.len = data.length;
                             recycleCnt++;
@@ -600,7 +598,7 @@ public class IndexedDiskCache<K, V> exte
                 log.debug(logCacheName + "Caught 
ConcurrentModificationException." + cme);
             }
         }
-        catch (Exception e)
+        catch (IOException e)
         {
             log.error(logCacheName + "Failure updating element, key: " + 
ce.getKey() + " old: " + old, e);
         }
@@ -651,10 +649,6 @@ public class IndexedDiskCache<K, V> exte
             log.error(logCacheName + "Failure getting from disk, key = " + 
key, ioe);
             reset();
         }
-        catch (Exception e)
-        {
-            log.error(logCacheName + "Failure getting from disk, key = " + 
key, e);
-        }
         return object;
     }
 
@@ -670,34 +664,27 @@ public class IndexedDiskCache<K, V> exte
     public Map<K, ICacheElement<K, V>> processGetMatching(String pattern)
     {
         Map<K, ICacheElement<K, V>> elements = new HashMap<K, ICacheElement<K, 
V>>();
+        Set<K> keyArray = null;
+        storageLock.readLock().lock();
         try
         {
-            Set<K> keyArray = null;
-            storageLock.readLock().lock();
-            try
-            {
-                keyArray = new HashSet<K>(keyHash.keySet());
-            }
-            finally
-            {
-                storageLock.readLock().unlock();
-            }
+            keyArray = new HashSet<K>(keyHash.keySet());
+        }
+        finally
+        {
+            storageLock.readLock().unlock();
+        }
 
-            Set<K> matchingKeys = 
getKeyMatcher().getMatchingKeysFromArray(pattern, keyArray);
+        Set<K> matchingKeys = 
getKeyMatcher().getMatchingKeysFromArray(pattern, keyArray);
 
-            for (K key : matchingKeys)
+        for (K key : matchingKeys)
+        {
+            ICacheElement<K, V> element = processGet(key);
+            if (element != null)
             {
-                ICacheElement<K, V> element = processGet(key);
-                if (element != null)
-                {
-                    elements.put(key, element);
-                }
+                elements.put(key, element);
             }
         }
-        catch (Exception e)
-        {
-            log.error(logCacheName + "Failure getting matching from disk, 
pattern = " + pattern, e);
-        }
         return elements;
     }
 
@@ -808,11 +795,6 @@ public class IndexedDiskCache<K, V> exte
                 removed = performSingleKeyRemoval(key);
             }
         }
-        catch (Exception e)
-        {
-            log.error(logCacheName + "Problem removing element.", e);
-            reset = true;
-        }
         finally
         {
             storageLock.writeLock().unlock();
@@ -945,11 +927,6 @@ public class IndexedDiskCache<K, V> exte
         {
             reset();
         }
-        catch (Exception e)
-        {
-            log.error(logCacheName + "Problem removing all.", e);
-            reset();
-        }
         finally
         {
             logICacheEvent(cacheEvent);
@@ -957,7 +934,7 @@ public class IndexedDiskCache<K, V> exte
     }
 
     /**
-     * Reset effectively clears the disk cache, creating new files, 
recyclebins, and keymaps.
+     * Reset effectively clears the disk cache, creating new files, recycle 
bins, and keymaps.
      * <p>
      * It can be used to handle errors by last resort, force content update, 
or removeall.
      */
@@ -965,7 +942,7 @@ public class IndexedDiskCache<K, V> exte
     {
         if (log.isWarnEnabled())
         {
-            log.warn(logCacheName + "Reseting cache");
+            log.warn(logCacheName + "Resetting cache");
         }
 
         try
@@ -1017,12 +994,7 @@ public class IndexedDiskCache<K, V> exte
      */
     private void initializeRecycleBin()
     {
-        int recycleBinSize = cattr.getMaxRecycleBinSize() >= 0 ? 
cattr.getMaxRecycleBinSize() : 0;
-        recycle = new 
SortedPreferentialArray<IndexedDiskElementDescriptor>(recycleBinSize);
-        if (log.isDebugEnabled())
-        {
-            log.debug(logCacheName + "Set recycle max Size to 
MaxRecycleBinSize: '" + recycleBinSize + "'");
-        }
+        recycle = new ConcurrentSkipListSet<IndexedDiskElementDescriptor>();
     }
 
     /**
@@ -1165,8 +1137,6 @@ public class IndexedDiskCache<K, V> exte
      * (2) When an item on disk is updated with a value that will not fit in 
the previous slot. (3) When the max key size is
      * reached, the freed slot will be added.
      * <p>
-     * The recylebin is not a set. If a slot it added twice, it will result in 
the wrong data being returned.
-     * <p>
      *
      * @param ded
      */
@@ -1306,10 +1276,7 @@ public class IndexedDiskCache<K, V> exte
             {
                 if (!queuedPutList.isEmpty())
                 {
-                    // This is perhaps unnecessary, but the list might not be 
as sorted as we think.
-                    defragList = new 
IndexedDiskElementDescriptor[queuedPutList.size()];
-                    queuedPutList.toArray(defragList);
-                    Arrays.sort(defragList, new PositionComparator());
+                    defragList = queuedPutList.toArray(new 
IndexedDiskElementDescriptor[queuedPutList.size()]);
 
                     // pack them at the end
                     expectedNextPos = defragFile(defragList, expectedNextPos);
@@ -1317,7 +1284,7 @@ public class IndexedDiskCache<K, V> exte
                 // TRUNCATE THE FILE
                 dataFile.truncate(expectedNextPos);
             }
-            catch (Exception e)
+            catch (IOException e)
             {
                 log.error(logCacheName + "Error optimizing queued puts.", e);
             }
@@ -1441,7 +1408,7 @@ public class IndexedDiskCache<K, V> exte
     }
 
     /**
-     * Returns the size of the recyclebin in number of elements.
+     * Returns the size of the recycle bin in number of elements.
      * <p>
      *
      * @return The number of items in the bin.
@@ -1576,21 +1543,8 @@ public class IndexedDiskCache<K, V> exte
     }
 
     /**
-     * Gets basic stats for the disk cache.
-     * <p>
-     *
-     * @return String
-     */
-    @Override
-    public String getStats()
-    {
-        return getStatistics().toString();
-    }
-
-    /**
      * Returns info about the disk cache.
      * <p>
-     * (non-Javadoc)
      *
      * @see org.apache.commons.jcs.auxiliary.AuxiliaryCache#getStatistics()
      */
@@ -1609,7 +1563,7 @@ public class IndexedDiskCache<K, V> exte
             elems
                 .add(new StatElement<Long>("Data File Length", 
Long.valueOf(this.dataFile != null ? this.dataFile.length() : -1L)));
         }
-        catch (Exception e)
+        catch (IOException e)
         {
             log.error(e);
         }


Reply via email to