You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/12/11 02:50:01 UTC

[1/6] groovy git commit: Implement another LRU cache

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 605bae5fa -> 319017d07


Implement another LRU cache

(cherry picked from commit 4d729e4)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/6cd6487e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/6cd6487e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/6cd6487e

Branch: refs/heads/GROOVY_2_6_X
Commit: 6cd6487e1bee8400e30fcd29f977cefaf9254c1f
Parents: 605bae5
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 09:28:36 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:49:34 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCache.java       | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/6cd6487e/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
index ff521cd..024dc915e 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -22,6 +22,7 @@ import java.lang.ref.SoftReference;
 import java.lang.ref.WeakReference;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -43,10 +44,27 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
     private final ReentrantReadWriteLock.ReadLock readLock = rwl.readLock();
     private final ReentrantReadWriteLock.WriteLock writeLock = rwl.writeLock();
 
+    /**
+     * A cache with unlimited size
+     */
     public CommonCache() {
         this(new HashMap<K, V>());
     }
 
+    /**
+     * Another LRU cache, which is slower than {@link LRUCache} but will not put same value multi-times concurrently
+     * @param initialCapacity
+     * @param maxSize
+     */
+    public CommonCache(final int initialCapacity, final int maxSize) {
+        this(new LinkedHashMap<K, V>(initialCapacity, 0.75f, true) {
+            @Override
+            protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
+                return size() > maxSize;
+            }
+        });
+    }
+
     public CommonCache(Map<K, V> map) {
         this.map = map;
     }


[3/6] groovy git commit: Refine the API of EvictableCache and javadoc

Posted by su...@apache.org.
Refine the API of EvictableCache and javadoc

(cherry picked from commit 06b2956)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/681e9650
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/681e9650
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/681e9650

Branch: refs/heads/GROOVY_2_6_X
Commit: 681e965054053fe828a9fe95f70b13d14a244800
Parents: 286b4d6
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 10:06:00 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:49:39 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCache.java     | 55 ++++++++++++++++++--
 .../groovy/runtime/memoize/EvictableCache.java  |  7 +++
 .../groovy/runtime/memoize/MemoizeCache.java    | 11 ++++
 3 files changed, 70 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/681e9650/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
index 13e668f..f24706c 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -31,7 +31,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 /**
  *
- * Represents a simple cache, which is thread safe and backed by a {@link java.util.Map} instance
+ * Represents a simple key-value cache, which is thread safe and backed by a {@link java.util.Map} instance
  *
  * @param <K> type of the keys
  * @param <V> type of the values
@@ -53,8 +53,8 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
 
     /**
      * Another LRU cache, which is slower than {@link LRUCache} but will not put same value multi-times concurrently
-     * @param initialCapacity
-     * @param maxSize
+     * @param initialCapacity initial capacity of the LRU cache
+     * @param maxSize max size of the LRU cache
      */
     public CommonCache(final int initialCapacity, final int maxSize) {
         this(new LinkedHashMap<K, V>(initialCapacity, 0.75f, true) {
@@ -65,14 +65,26 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         });
     }
 
+    /**
+     * Another LRU cache with the default initial capacity(16)
+     * @param maxSize max size of the LRU cache
+     * @see #CommonCache(int, int)
+     */
     public CommonCache(final int maxSize) {
         this(16, maxSize);
     }
 
+    /**
+     * Constructs a cache backed by the specified {@link java.util.Map} instance
+     * @param map the {@link java.util.Map} instance
+     */
     public CommonCache(Map<K, V> map) {
         this.map = map;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V get(K key) {
         if (null == key) {
@@ -87,6 +99,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V put(K key, V value) {
         writeLock.lock();
@@ -98,6 +113,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
 
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V getAndPut(K key, ValueProvider<K, V> valueProvider) {
         return getAndPut(key, valueProvider, true);
@@ -139,6 +157,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         return value;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public Collection<V> values() {
         readLock.lock();
@@ -149,6 +170,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public Set<K> keys() {
         readLock.lock();
@@ -159,6 +183,22 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean containsKey(K key) {
+        readLock.lock();
+        try {
+            return map.containsKey(key);
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public int size() {
         readLock.lock();
@@ -169,6 +209,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V remove(K key) {
         writeLock.lock();
@@ -179,6 +222,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public Collection<V> clear() {
         Collection<V> values;
@@ -194,6 +240,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         return values;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void cleanUpNullReferences() {
         writeLock.lock();

http://git-wip-us.apache.org/repos/asf/groovy/blob/681e9650/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
index acce66d..43bc8bd 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
@@ -63,6 +63,13 @@ public interface EvictableCache<K, V> extends MemoizeCache<K, V> {
     Set<K> keys();
 
     /**
+     * Determines if the cache contains an entry for the specified key.
+     * @param key key whose presence in this cache is to be tested.
+     * @return true if the cache contains a mapping for the specified key
+     */
+    boolean containsKey(K key);
+
+    /**
      * Get the size of the cache
      * @return the size of the cache
      */

http://git-wip-us.apache.org/repos/asf/groovy/blob/681e9650/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java b/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
index e7f5584..aa0eb8e 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
@@ -27,8 +27,19 @@ package org.codehaus.groovy.runtime.memoize;
  */
 public interface MemoizeCache<K, V> {
 
+    /**
+     * Associates the specified value with the specified key in the cache.
+     * @param key key with which the specified value is to be associated
+     * @param value value to be associated with the specified key
+     * @return null, or the old value if the key associated with the specified key.
+     */
     V put(K key, V value);
 
+    /**
+     * Gets a value from the cache
+     * @param key the key whose associated value is to be returned
+     * @return the value, or null, if it does not exist.
+     */
     V get(K key);
 
     /**


[5/6] groovy git commit: Use `LinkedHashMap` by default to implement `CommonCache`

Posted by su...@apache.org.
Use `LinkedHashMap` by default to implement `CommonCache`

(cherry picked from commit b2e8aa2)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/fecd7a64
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fecd7a64
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fecd7a64

Branch: refs/heads/GROOVY_2_6_X
Commit: fecd7a641f5199b7ae66c7cd611da221d709d0c8
Parents: 7888286
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 10:22:57 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:49:47 2017 +0800

----------------------------------------------------------------------
 src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/fecd7a64/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
index f24706c..f9cd925 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -21,7 +21,6 @@ package org.codehaus.groovy.runtime.memoize;
 import java.lang.ref.SoftReference;
 import java.lang.ref.WeakReference;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -48,7 +47,7 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
      * A cache with unlimited size
      */
     public CommonCache() {
-        this(new HashMap<K, V>());
+        this(new LinkedHashMap<K, V>());
     }
 
     /**


[2/6] groovy git commit: Provide one more constructor for LRU cache

Posted by su...@apache.org.
Provide one more constructor for LRU cache

(cherry picked from commit 0d8da15)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/286b4d65
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/286b4d65
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/286b4d65

Branch: refs/heads/GROOVY_2_6_X
Commit: 286b4d656363df09e1aba914d2dab2ca33a13ce8
Parents: 6cd6487
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 09:31:39 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:49:37 2017 +0800

----------------------------------------------------------------------
 src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java | 4 ++++
 1 file changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/286b4d65/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
index 024dc915e..13e668f 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -65,6 +65,10 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         });
     }
 
+    public CommonCache(final int maxSize) {
+        this(16, maxSize);
+    }
+
     public CommonCache(Map<K, V> map) {
         this.map = map;
     }


[6/6] groovy git commit: Refine CommonCache and its javadoc

Posted by su...@apache.org.
Refine CommonCache and its javadoc

(cherry picked from commit 61afa2a)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/319017d0
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/319017d0
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/319017d0

Branch: refs/heads/GROOVY_2_6_X
Commit: 319017d07cbb26e0f1dc96722f992559fe12d6cd
Parents: fecd7a6
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 10:46:12 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:49:50 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCache.java     | 21 +++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/319017d0/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
index f9cd925..1bd336d 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -44,19 +44,20 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
     private final ReentrantReadWriteLock.WriteLock writeLock = rwl.writeLock();
 
     /**
-     * A cache with unlimited size
+     * Constructs a cache with unlimited size
      */
     public CommonCache() {
         this(new LinkedHashMap<K, V>());
     }
 
     /**
-     * Another LRU cache, which is slower than {@link LRUCache} but will not put same value multi-times concurrently
+     * Constructs a cache with limited size
      * @param initialCapacity initial capacity of the LRU cache
      * @param maxSize max size of the LRU cache
+     * @param accessOrder the ordering mode - <tt>true</tt> for access-order, <tt>false</tt> for insertion-order, see the parameter accessOrder of {@link LinkedHashMap#LinkedHashMap(int, float, boolean)}
      */
-    public CommonCache(final int initialCapacity, final int maxSize) {
-        this(new LinkedHashMap<K, V>(initialCapacity, 0.75f, true) {
+    public CommonCache(final int initialCapacity, final int maxSize, final boolean accessOrder) {
+        this(new LinkedHashMap<K, V>(initialCapacity, 0.75f, accessOrder) {
             @Override
             protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
                 return size() > maxSize;
@@ -65,7 +66,17 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
     }
 
     /**
-     * Another LRU cache with the default initial capacity(16)
+     * Constructs a LRU cache with the specified initial capacity and max size.
+     * The LRU cache is slower than {@link LRUCache} but will not put same value multi-times concurrently
+     * @param initialCapacity initial capacity of the LRU cache
+     * @param maxSize max size of the LRU cache
+     */
+    public CommonCache(final int initialCapacity, final int maxSize) {
+        this(initialCapacity, maxSize, true);
+    }
+
+    /**
+     * Constructs a LRU cache with the default initial capacity(16)
      * @param maxSize max size of the LRU cache
      * @see #CommonCache(int, int)
      */


[4/6] groovy git commit: Add a testcase for the `containsKey` of `CommonCache`

Posted by su...@apache.org.
Add a testcase for the `containsKey` of `CommonCache`

(cherry picked from commit d8b8e98)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/78882864
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/78882864
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/78882864

Branch: refs/heads/GROOVY_2_6_X
Commit: 788828642f2e276a48cc47bdeac6afd7e31c0a87
Parents: 681e965
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 10:14:37 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:49:44 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCacheTest.java       | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/78882864/src/test/org/codehaus/groovy/runtime/memoize/CommonCacheTest.java
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/memoize/CommonCacheTest.java b/src/test/org/codehaus/groovy/runtime/memoize/CommonCacheTest.java
index 9bbb08b..9fb4350 100644
--- a/src/test/org/codehaus/groovy/runtime/memoize/CommonCacheTest.java
+++ b/src/test/org/codehaus/groovy/runtime/memoize/CommonCacheTest.java
@@ -102,6 +102,20 @@ public class CommonCacheTest {
     }
 
     @Test
+    public void containsKey() {
+        CommonCache<String, String> sc =
+                new CommonCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertTrue(sc.containsKey("name"));
+    }
+
+    @Test
     public void size() {
         CommonCache<String, String> sc =
                 new CommonCache<>(