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/09 14:07:29 UTC
[1/6] groovy git commit: Minor refactoring: classCache and sourceCache
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_6_X 4cefea067 -> ddb2b9233
Minor refactoring: classCache and sourceCache
(cherry picked from commit 2c0a3bf)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/316329d2
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/316329d2
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/316329d2
Branch: refs/heads/GROOVY_2_6_X
Commit: 316329d24945293b5b82c80f57a950ec561bec3a
Parents: 4cefea0
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 20:22:57 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:06:37 2017 +0800
----------------------------------------------------------------------
src/main/groovy/lang/GroovyClassLoader.java | 111 +++-----------
.../runtime/memoize/EvictableMemoizeCache.java | 68 +++++++++
.../groovy/runtime/memoize/SimpleCache.java | 152 +++++++++++++++++++
3 files changed, 242 insertions(+), 89 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/316329d2/src/main/groovy/lang/GroovyClassLoader.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyClassLoader.java b/src/main/groovy/lang/GroovyClassLoader.java
index 9752f87..601a494 100644
--- a/src/main/groovy/lang/GroovyClassLoader.java
+++ b/src/main/groovy/lang/GroovyClassLoader.java
@@ -43,6 +43,8 @@ import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
+import org.codehaus.groovy.runtime.memoize.EvictableMemoizeCache;
+import org.codehaus.groovy.runtime.memoize.SimpleCache;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
@@ -70,9 +72,6 @@ import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* A ClassLoader which can load Groovy classes. The loaded classes are cached,
@@ -96,19 +95,13 @@ public class GroovyClassLoader extends URLClassLoader {
/**
* this cache contains the loaded classes or PARSING, if the class is currently parsed
*/
- protected final Map<String, Class> classCache = new HashMap<String, Class>(); // TODO should we make classCache private?
- private final ReentrantReadWriteLock rwlForClassCache = new ReentrantReadWriteLock();
- private final ReentrantReadWriteLock.ReadLock readLockForClassCache = rwlForClassCache.readLock();
- private final ReentrantReadWriteLock.WriteLock writeLockForClassCache = rwlForClassCache.writeLock();
+ protected final SimpleCache<String, Class> classCache = new SimpleCache<String, Class>();
/**
* This cache contains mappings of file name to class. It is used
* to bypass compilation.
*/
- protected final Map<String, Class> sourceCache = new HashMap<String, Class>();
- private final ReentrantReadWriteLock rwlForSourceCache = new ReentrantReadWriteLock();
- private final ReentrantReadWriteLock.ReadLock readLockForSourceCache = rwlForSourceCache.readLock();
- private final ReentrantReadWriteLock.WriteLock writeLockForSourceCache = rwlForSourceCache.writeLock();
+ protected final SimpleCache<String, Class> sourceCache = new SimpleCache<String, Class>();
private final CompilerConfiguration config;
private String sourceEncoding;
@@ -325,31 +318,17 @@ public class GroovyClassLoader extends URLClassLoader {
* @param shouldCacheSource if true then the generated class will be stored in the source cache
* @return the main class defined in the given script
*/
- public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException {
- Class answer;
- String codeSourceName = codeSource.getName();
-
- readLockForSourceCache.lock();
- try {
- answer = sourceCache.get(codeSourceName);
- if (answer != null) return answer;
- } finally {
- readLockForSourceCache.unlock();
- }
-
- writeLockForSourceCache.lock();
- try {
- // try to find the cached class again
- answer = sourceCache.get(codeSourceName);
- if (answer != null) return answer;
-
- answer = doParseClass(codeSource);
- if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer);
- } finally {
- writeLockForSourceCache.unlock();
- }
-
- return answer;
+ public Class parseClass(final GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException {
+ return sourceCache.getAndPut(
+ codeSource.getName(),
+ new EvictableMemoizeCache.ValueProvider<String, Class>() {
+ @Override
+ public Class provide(String key) {
+ return doParseClass(codeSource);
+ }
+ },
+ shouldCacheSource
+ );
}
private Class doParseClass(GroovyCodeSource codeSource) {
@@ -638,14 +617,7 @@ public class GroovyClassLoader extends URLClassLoader {
* @see #clearCache()
*/
protected Class getClassCacheEntry(String name) {
- if (name == null) return null;
-
- readLockForClassCache.lock();
- try {
- return classCache.get(name);
- } finally {
- readLockForClassCache.unlock();
- }
+ return classCache.get(name);
}
/**
@@ -657,14 +629,7 @@ public class GroovyClassLoader extends URLClassLoader {
* @see #clearCache()
*/
protected void setClassCacheEntry(Class cls) {
- String className = cls.getName();
-
- writeLockForClassCache.lock();
- try {
- classCache.put(className, cls);
- } finally {
- writeLockForClassCache.unlock();
- }
+ classCache.put(cls.getName(), cls);
}
/**
@@ -676,12 +641,7 @@ public class GroovyClassLoader extends URLClassLoader {
* @see #clearCache()
*/
protected void removeClassCacheEntry(String name) {
- writeLockForClassCache.lock();
- try {
- classCache.remove(name);
- } finally {
- writeLockForClassCache.unlock();
- }
+ classCache.remove(name);
}
/**
@@ -848,12 +808,7 @@ public class GroovyClassLoader extends URLClassLoader {
if ((oldClass != null && isSourceNewer(source, oldClass)) || (oldClass == null)) {
String name = source.toExternalForm();
- writeLockForSourceCache.lock();
- try {
- sourceCache.remove(name);
- } finally {
- writeLockForSourceCache.unlock();
- }
+ sourceCache.remove(name);
if (isFile(source)) {
try {
@@ -1052,16 +1007,7 @@ public class GroovyClassLoader extends URLClassLoader {
* @return all classes loaded by this class loader
*/
public Class[] getLoadedClasses() {
- final Collection<Class> values;
-
- readLockForClassCache.lock();
- try {
- values = classCache.values();
- } finally {
- readLockForClassCache.unlock();
- }
-
- return values.toArray(EMPTY_CLASS_ARRAY);
+ return classCache.values().toArray(EMPTY_CLASS_ARRAY);
}
/**
@@ -1076,22 +1022,9 @@ public class GroovyClassLoader extends URLClassLoader {
* @see #removeClassCacheEntry(String)
*/
public void clearCache() {
- Collection<Class> classesToClear;
+ Collection<Class> classesToClear = classCache.clear();
- writeLockForClassCache.lock();
- try {
- classesToClear = classCache.values();
- classCache.clear();
- } finally {
- writeLockForClassCache.unlock();
- }
-
- writeLockForSourceCache.lock();
- try {
- sourceCache.clear();
- } finally {
- writeLockForSourceCache.unlock();
- }
+ sourceCache.clear();
for (Class c : classesToClear) {
// Another Thread may be using an instance of this class
http://git-wip-us.apache.org/repos/asf/groovy/blob/316329d2/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java b/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
new file mode 100644
index 0000000..83b3498
--- /dev/null
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.runtime.memoize;
+
+import java.util.Collection;
+
+/**
+ * Represents an evictable memoize cache with its essential methods
+ * @param <K> type of the keys
+ * @param <V> type of the values
+ */
+public interface EvictableMemoizeCache<K, V> extends MemoizeCache<K, V> {
+ /**
+ * Remove the cached value by the key
+ * @param key
+ * @return returns false if there was no matching key
+ */
+ boolean remove(K key);
+
+ /**
+ * Clear the cache
+ * @return returns cleared values
+ */
+ Collection<V> clear();
+
+ /**
+ * Try to get the value from cache.
+ * If not found, create the value by {@link ValueProvider} and put it into the cache, at last return the value
+ * @param key
+ * @return the cached value
+ */
+ V getAndPut(K key, ValueProvider<K, V> valueProvider);
+
+ /**
+ * Get all cached values
+ * @return all cached values
+ */
+ Collection<V> values();
+
+ /**
+ * Represents a provider used to create value
+ * @param <K> type of the key
+ * @param <V> type of the value
+ */
+ interface ValueProvider<K, V> {
+ /**
+ * Provide the created value
+ * @return
+ */
+ V provide(K key);
+ }
+}
http://git-wip-us.apache.org/repos/asf/groovy/blob/316329d2/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
new file mode 100644
index 0000000..50affef
--- /dev/null
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.runtime.memoize;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ *
+ * Represents a simple cache, which is thread safe and backed by {@link java.util.HashMap}
+ *
+ * @param <K> type of the keys
+ * @param <V> type of the values
+ */
+public class SimpleCache<K, V> implements EvictableMemoizeCache<K, V> {
+ private final Map<K, V> map = new HashMap<>();
+ private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock.ReadLock readLock = rwl.readLock();
+ private final ReentrantReadWriteLock.WriteLock writeLock = rwl.writeLock();
+
+ @Override
+ public V get(K key) {
+ if (null == key) {
+ return null;
+ }
+
+ readLock.lock();
+ try {
+ return map.get(key);
+ } finally {
+ readLock.unlock();
+ }
+ }
+
+ @Override
+ public V put(K key, V value) {
+ writeLock.lock();
+ try {
+ return map.put(key, value);
+ } finally {
+ writeLock.unlock();
+ }
+
+ }
+
+ @Override
+ public V getAndPut(K key, ValueProvider<K, V> valueProvider) {
+ return getAndPut(key, valueProvider, true);
+ }
+
+ public V getAndPut(K key, ValueProvider<K, V> valueProvider, boolean shouldCache) {
+ V value;
+
+ readLock.lock();
+ try {
+ value = map.get(key);
+ if (null != value) return value;
+ } finally {
+ readLock.unlock();
+ }
+
+ writeLock.lock();
+ try {
+ // try to find the cached value again
+ value = map.get(key);
+ if (null != value) return value;
+
+ value = valueProvider.provide(key);
+ if (shouldCache) map.put(key, value);
+ } finally {
+ writeLock.unlock();
+ }
+
+ return value;
+ }
+
+
+
+ @Override
+ public Collection<V> values() {
+ return map.values();
+ }
+
+ @Override
+ public boolean remove(K key) {
+ V removedValue;
+
+ writeLock.lock();
+ try {
+ removedValue = map.remove(key);
+ } finally {
+ writeLock.unlock();
+ }
+
+ return null != removedValue;
+ }
+
+ @Override
+ public Collection<V> clear() {
+ Collection<V> values;
+
+ writeLock.lock();
+ try {
+ values = map.values();
+ map.clear();
+ } finally {
+ writeLock.unlock();
+ }
+
+ return values;
+ }
+
+ @Override
+ public void cleanUpNullReferences() {
+ writeLock.lock();
+ try {
+ List<K> keys = new LinkedList<>();
+
+ for (Map.Entry<K, V> entry : map.entrySet()) {
+ if (null == entry.getValue()) {
+ keys.add(entry.getKey());
+ }
+ }
+
+ for (K key : keys) {
+ map.remove(key);
+ }
+ } finally {
+ writeLock.unlock();
+ }
+ }
+}
[3/6] groovy git commit: Rename EvictableMemoizeCache to
EvictableCache
Posted by su...@apache.org.
Rename EvictableMemoizeCache to EvictableCache
(cherry picked from commit 8587703)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c43773a9
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c43773a9
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c43773a9
Branch: refs/heads/GROOVY_2_6_X
Commit: c43773a9d280b26d17cdcd9e466f22db507c7328
Parents: a98c342
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 20:37:33 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:06:47 2017 +0800
----------------------------------------------------------------------
src/main/groovy/lang/GroovyClassLoader.java | 4 +-
.../groovy/runtime/memoize/EvictableCache.java | 68 ++++++++++++++++++++
.../runtime/memoize/EvictableMemoizeCache.java | 68 --------------------
.../groovy/runtime/memoize/SimpleCache.java | 2 +-
4 files changed, 71 insertions(+), 71 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/c43773a9/src/main/groovy/lang/GroovyClassLoader.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyClassLoader.java b/src/main/groovy/lang/GroovyClassLoader.java
index 601a494..134d887 100644
--- a/src/main/groovy/lang/GroovyClassLoader.java
+++ b/src/main/groovy/lang/GroovyClassLoader.java
@@ -43,7 +43,7 @@ import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
-import org.codehaus.groovy.runtime.memoize.EvictableMemoizeCache;
+import org.codehaus.groovy.runtime.memoize.EvictableCache;
import org.codehaus.groovy.runtime.memoize.SimpleCache;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
@@ -321,7 +321,7 @@ public class GroovyClassLoader extends URLClassLoader {
public Class parseClass(final GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException {
return sourceCache.getAndPut(
codeSource.getName(),
- new EvictableMemoizeCache.ValueProvider<String, Class>() {
+ new EvictableCache.ValueProvider<String, Class>() {
@Override
public Class provide(String key) {
return doParseClass(codeSource);
http://git-wip-us.apache.org/repos/asf/groovy/blob/c43773a9/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
new file mode 100644
index 0000000..369d525
--- /dev/null
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.runtime.memoize;
+
+import java.util.Collection;
+
+/**
+ * Represents an evictable memoize cache with its essential methods
+ * @param <K> type of the keys
+ * @param <V> type of the values
+ */
+public interface EvictableCache<K, V> extends MemoizeCache<K, V> {
+ /**
+ * Remove the cached value by the key
+ * @param key
+ * @return returns the removed value
+ */
+ V remove(K key);
+
+ /**
+ * Clear the cache
+ * @return returns cleared values
+ */
+ Collection<V> clear();
+
+ /**
+ * Try to get the value from cache.
+ * If not found, create the value by {@link ValueProvider} and put it into the cache, at last return the value
+ * @param key
+ * @return the cached value
+ */
+ V getAndPut(K key, ValueProvider<K, V> valueProvider);
+
+ /**
+ * Get all cached values
+ * @return all cached values
+ */
+ Collection<V> values();
+
+ /**
+ * Represents a provider used to create value
+ * @param <K> type of the key
+ * @param <V> type of the value
+ */
+ interface ValueProvider<K, V> {
+ /**
+ * Provide the created value
+ * @return
+ */
+ V provide(K key);
+ }
+}
http://git-wip-us.apache.org/repos/asf/groovy/blob/c43773a9/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java b/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
deleted file mode 100644
index a5dd318..0000000
--- a/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.codehaus.groovy.runtime.memoize;
-
-import java.util.Collection;
-
-/**
- * Represents an evictable memoize cache with its essential methods
- * @param <K> type of the keys
- * @param <V> type of the values
- */
-public interface EvictableMemoizeCache<K, V> extends MemoizeCache<K, V> {
- /**
- * Remove the cached value by the key
- * @param key
- * @return returns the removed value
- */
- V remove(K key);
-
- /**
- * Clear the cache
- * @return returns cleared values
- */
- Collection<V> clear();
-
- /**
- * Try to get the value from cache.
- * If not found, create the value by {@link ValueProvider} and put it into the cache, at last return the value
- * @param key
- * @return the cached value
- */
- V getAndPut(K key, ValueProvider<K, V> valueProvider);
-
- /**
- * Get all cached values
- * @return all cached values
- */
- Collection<V> values();
-
- /**
- * Represents a provider used to create value
- * @param <K> type of the key
- * @param <V> type of the value
- */
- interface ValueProvider<K, V> {
- /**
- * Provide the created value
- * @return
- */
- V provide(K key);
- }
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/c43773a9/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index 4eb0453..b789067 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -32,7 +32,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
* @param <K> type of the keys
* @param <V> type of the values
*/
-public class SimpleCache<K, V> implements EvictableMemoizeCache<K, V> {
+public class SimpleCache<K, V> implements EvictableCache<K, V> {
private final Map<K, V> map = new HashMap<>();
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock.ReadLock readLock = rwl.readLock();
[4/6] groovy git commit: Add `@since` to the javadoc of
EvictableCache and SimpleCache
Posted by su...@apache.org.
Add `@since` to the javadoc of EvictableCache and SimpleCache
(cherry picked from commit ba570df)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/4de00efa
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/4de00efa
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/4de00efa
Branch: refs/heads/GROOVY_2_6_X
Commit: 4de00efa2b30833c6a7de19f8b88d8278e96b978
Parents: c43773a
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 21:28:30 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:06:54 2017 +0800
----------------------------------------------------------------------
src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java | 2 ++
src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java | 2 ++
2 files changed, 4 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/4de00efa/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 369d525..a882947 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
@@ -24,6 +24,8 @@ import java.util.Collection;
* Represents an evictable memoize cache with its essential methods
* @param <K> type of the keys
* @param <V> type of the values
+ *
+ * @since 2.5.0
*/
public interface EvictableCache<K, V> extends MemoizeCache<K, V> {
/**
http://git-wip-us.apache.org/repos/asf/groovy/blob/4de00efa/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index b789067..1aec2b5 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -31,6 +31,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
*
* @param <K> type of the keys
* @param <V> type of the values
+ *
+ * @since 2.5.0
*/
public class SimpleCache<K, V> implements EvictableCache<K, V> {
private final Map<K, V> map = new HashMap<>();
[2/6] groovy git commit: Refine EvictableMemoizeCache
Posted by su...@apache.org.
Refine EvictableMemoizeCache
(cherry picked from commit 1aa79fd)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a98c3426
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a98c3426
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a98c3426
Branch: refs/heads/GROOVY_2_6_X
Commit: a98c3426d256fec3d00c0184da3913b5db4754bf
Parents: 316329d
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 20:35:16 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:06:42 2017 +0800
----------------------------------------------------------------------
.../groovy/runtime/memoize/EvictableMemoizeCache.java | 4 ++--
.../org/codehaus/groovy/runtime/memoize/SimpleCache.java | 10 ++--------
2 files changed, 4 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/a98c3426/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java b/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
index 83b3498..a5dd318 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableMemoizeCache.java
@@ -29,9 +29,9 @@ public interface EvictableMemoizeCache<K, V> extends MemoizeCache<K, V> {
/**
* Remove the cached value by the key
* @param key
- * @return returns false if there was no matching key
+ * @return returns the removed value
*/
- boolean remove(K key);
+ V remove(K key);
/**
* Clear the cache
http://git-wip-us.apache.org/repos/asf/groovy/blob/a98c3426/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index 50affef..4eb0453 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -94,25 +94,19 @@ public class SimpleCache<K, V> implements EvictableMemoizeCache<K, V> {
return value;
}
-
-
@Override
public Collection<V> values() {
return map.values();
}
@Override
- public boolean remove(K key) {
- V removedValue;
-
+ public V remove(K key) {
writeLock.lock();
try {
- removedValue = map.remove(key);
+ return map.remove(key);
} finally {
writeLock.unlock();
}
-
- return null != removedValue;
}
@Override
[6/6] groovy git commit: Refine SimpleCache
Posted by su...@apache.org.
Refine SimpleCache
(cherry picked from commit 00c1ee1)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ddb2b923
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ddb2b923
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ddb2b923
Branch: refs/heads/GROOVY_2_6_X
Commit: ddb2b9233b53b1ca42700e8cebe6e8e4e4dc2560
Parents: 30fdba0
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 21:56:21 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:07:07 2017 +0800
----------------------------------------------------------------------
.../org/codehaus/groovy/runtime/memoize/SimpleCache.java | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/ddb2b923/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index 1ee86d0..f2b3287 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -95,12 +95,8 @@ public class SimpleCache<K, V> implements EvictableCache<K, V> {
return value;
}
- if (null == valueProvider) {
- return null;
- }
-
- value = valueProvider.provide(key);
- if (shouldCache) {
+ value = null == valueProvider ? null : valueProvider.provide(key);
+ if (shouldCache && null != value) {
map.put(key, value);
}
} finally {
[5/6] groovy git commit: Refine SimpleCache
Posted by su...@apache.org.
Refine SimpleCache
(cherry picked from commit 1202cd5)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/30fdba0f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/30fdba0f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/30fdba0f
Branch: refs/heads/GROOVY_2_6_X
Commit: 30fdba0f7b870b4aa266b047aa740f5a81ee83e4
Parents: 4de00ef
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 21:44:04 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:07:02 2017 +0800
----------------------------------------------------------------------
.../groovy/runtime/memoize/SimpleCache.java | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/30fdba0f/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index 1aec2b5..1ee86d0 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -71,12 +71,18 @@ public class SimpleCache<K, V> implements EvictableCache<K, V> {
}
public V getAndPut(K key, ValueProvider<K, V> valueProvider, boolean shouldCache) {
+ if (null == key) {
+ return null;
+ }
+
V value;
readLock.lock();
try {
value = map.get(key);
- if (null != value) return value;
+ if (null != value) {
+ return value;
+ }
} finally {
readLock.unlock();
}
@@ -85,10 +91,18 @@ public class SimpleCache<K, V> implements EvictableCache<K, V> {
try {
// try to find the cached value again
value = map.get(key);
- if (null != value) return value;
+ if (null != value) {
+ return value;
+ }
+
+ if (null == valueProvider) {
+ return null;
+ }
value = valueProvider.provide(key);
- if (shouldCache) map.put(key, value);
+ if (shouldCache) {
+ map.put(key, value);
+ }
} finally {
writeLock.unlock();
}