hchar 2005/01/28 02:41:10
Modified: sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref
IKey.java KeyedRefCollector.java
KeyedSoftReference.java KeyedWeakReference.java
sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks
IKeyedReadWriteLock.java KeyedReadWriteLock.java
sandbox/yajcache/src/org/apache/jcs/yajcache/soft
SoftRefCache.java SoftRefCacheCleaner.java
SoftRefFileCache.java
Log:
generalize IKey and KeyedReadWriteLock
Revision Changes Path
1.2 +2 -2
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/IKey.java
Index: IKey.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/IKey.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- IKey.java 27 Jan 2005 10:49:29 -0000 1.1
+++ IKey.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -20,6 +20,6 @@
*
* @author Hanson Char
*/
-public interface IKey {
- public String getKey();
+public interface IKey<K> {
+ public K getKey();
}
1.2 +3 -3
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedRefCollector.java
Index: KeyedRefCollector.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedRefCollector.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- KeyedRefCollector.java 27 Jan 2005 10:49:44 -0000 1.1
+++ KeyedRefCollector.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -28,16 +28,16 @@
* @author Hanson Char
*/
@CopyRightApache
-public class KeyedRefCollector implements Runnable {
+public class KeyedRefCollector<K> implements Runnable {
private static final boolean debug = true;
private Log log = debug ? LogFactory.getLog(this.getClass()) : null;
private final @NonNullable ReferenceQueue q;
- private final @NonNullable ConcurrentMap<String, ? extends IKey> synMap;
+ private final @NonNullable ConcurrentMap<K, ? extends IKey<K>> synMap;
private volatile int count;
public KeyedRefCollector(
@NonNullable ReferenceQueue<?> q,
- @NonNullable ConcurrentMap<String, ? extends IKey> synMap)
+ @NonNullable ConcurrentMap<K, ? extends IKey<K>> synMap)
{
this.q = q;
this.synMap = synMap;
1.2 +6 -4
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedSoftReference.java
Index: KeyedSoftReference.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedSoftReference.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- KeyedSoftReference.java 27 Jan 2005 10:50:05 -0000 1.1
+++ KeyedSoftReference.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -28,21 +28,23 @@
* @author Hanson Char
*/
@CopyRightApache
-public class KeyedSoftReference<T> extends SoftReference<T> implements IKey {
- private final @NonNullable String key;
+public class KeyedSoftReference<K,T> extends SoftReference<T>
+ implements IKey<K>
+{
+ private final @NonNullable K key;
// KeyedSoftRef(String key, T value) {
// super(value);
// this.key = key;
// }
- public KeyedSoftReference(@NonNullable String key, @NonNullable T
referrent,
+ public KeyedSoftReference(@NonNullable K key, @NonNullable T referrent,
ReferenceQueue<? super T> q)
{
super(referrent, q);
this.key = key;
}
@Implements(IKey.class)
- public String getKey() {
+ public K getKey() {
return this.key;
}
}
1.2 +6 -4
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedWeakReference.java
Index: KeyedWeakReference.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/lang/ref/KeyedWeakReference.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- KeyedWeakReference.java 27 Jan 2005 10:50:17 -0000 1.1
+++ KeyedWeakReference.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -23,17 +23,19 @@
/**
* @author Hanson Char
*/
-public class KeyedWeakReference<T> extends WeakReference<T> implements IKey {
- public final String key;
+public class KeyedWeakReference<K,T> extends WeakReference<T>
+ implements IKey<K>
+{
+ public final K key;
- public KeyedWeakReference(String key, T referrent,
+ public KeyedWeakReference(K key, T referrent,
ReferenceQueue<? super T> q)
{
super(referrent);
this.key = key;
}
@Implements(IKey.class)
- public String getKey() {
+ public K getKey() {
return this.key;
}
}
1.2 +3 -3
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks/IKeyedReadWriteLock.java
Index: IKeyedReadWriteLock.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks/IKeyedReadWriteLock.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- IKeyedReadWriteLock.java 27 Jan 2005 10:50:47 -0000 1.1
+++ IKeyedReadWriteLock.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -22,7 +22,7 @@
/**
* @author Hanson Char
*/
-public interface IKeyedReadWriteLock {
- public Lock readLock(String key);
- public Lock writeLock(String key);
+public interface IKeyedReadWriteLock<K> {
+ public Lock readLock(K key);
+ public Lock writeLock(K key);
}
1.2 +35 -24
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java
Index: KeyedReadWriteLock.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/concurrent/locks/KeyedReadWriteLock.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- KeyedReadWriteLock.java 27 Jan 2005 10:50:57 -0000 1.1
+++ KeyedReadWriteLock.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -29,26 +29,27 @@
/**
* @author Hanson Char
*/
-public class KeyedReadWriteLock implements IKeyedReadWriteLock {
- private final ConcurrentMap<String, KeyedWeakReference<ReadWriteLock>>
rwlMap =
- new ConcurrentHashMap<String,
KeyedWeakReference<ReadWriteLock>>();
+public class KeyedReadWriteLock<K> implements IKeyedReadWriteLock<K> {
+ private final ConcurrentMap<K, KeyedWeakReference<K,ReadWriteLock>>
rwlMap =
+ new ConcurrentHashMap<K, KeyedWeakReference<K,ReadWriteLock>>();
private final Class<? extends ReadWriteLock> rwlClass;
private final ReferenceQueue<ReadWriteLock> refQ =
new ReferenceQueue<ReadWriteLock>();
- private final KeyedRefCollector collector = new KeyedRefCollector(refQ,
rwlMap);
+ private final KeyedRefCollector<K> collector =
+ new KeyedRefCollector<K>(refQ, rwlMap);
public KeyedReadWriteLock() {
this.rwlClass = ReentrantReadWriteLock.class;
}
public KeyedReadWriteLock(Class<? extends ReadWriteLock> rwlClass) {
this.rwlClass = rwlClass;
}
- public Lock readLock(String key) {
+ public Lock readLock(K key) {
return this.readWriteLock(key).readLock();
}
- public Lock writeLock(String key) {
+ public Lock writeLock(K key) {
return this.readWriteLock(key).writeLock();
}
- private ReadWriteLock readWriteLock(String key) {
+ private ReadWriteLock readWriteLock(K key) {
ReadWriteLock newLock = null;
try {
newLock = rwlClass.newInstance();
@@ -59,32 +60,42 @@
}
return this.getLock(key, newLock);
}
- private ReadWriteLock getLock(final String key, ReadWriteLock newLock)
+ private ReadWriteLock getLock(final K key, ReadWriteLock newLock)
{
this.collector.run();
- final KeyedWeakReference<ReadWriteLock> newLockRef =
- new KeyedWeakReference<ReadWriteLock>(key, newLock, refQ);
- KeyedWeakReference<ReadWriteLock> prevRef =
- this.rwlMap.putIfAbsent(key, newLockRef);
- if (prevRef == null) {
- // succesfully deposited the new lock.
- return newLock;
- }
- ReadWriteLock prev = prevRef.get();
+ ReadWriteLock prev = null;
+ KeyedWeakReference<K,ReadWriteLock> prevRef = this.rwlMap.get(key);
- for (; prev == null; prev=prevRef.get()) {
- // Unused lock is garbage collected. So clean it up.
- rwlMap.remove(key, prevRef); // remove may fail, but that's
fine.
+ if (prevRef != null) {
+ // existing lock may exist
+ prev = prevRef.get();
+
+ if (prev != null) {
+ // existing lock
+ return prev;
+ }
+ // stale reference; doesn't matter if fail
+ this.rwlMap.remove(key, prevRef);
+ }
+ final KeyedWeakReference<K,ReadWriteLock> newLockRef =
+ new KeyedWeakReference<K,ReadWriteLock>(key, newLock, refQ);
+ do {
prevRef = this.rwlMap.putIfAbsent(key, newLockRef);
if (prevRef == null) {
// succesfully deposited the new lock.
return newLock;
}
- // data race: someone else has just put in a lock.
- }
- // Return the lock deposited by another thread.
- return prev;
+ // existing lock may exist
+ prev = prevRef.get();
+
+ if (prev != null) {
+ // exist lock
+ return prev;
+ }
+ // stale reference; doesn't matter if fail
+ this.rwlMap.remove(key, prevRef);
+ } while(true);
}
// public void removeUnusedLocks() {
// this.collector.run();
1.4 +19 -18
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCache.java
Index: SoftRefCache.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCache.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SoftRefCache.java 27 Jan 2005 11:04:56 -0000 1.3
+++ SoftRefCache.java 28 Jan 2005 10:41:10 -0000 1.4
@@ -48,8 +48,8 @@
private final @NonNullable ReferenceQueue<V> refq = new
ReferenceQueue<V>();
private final @NonNullable String name;
private final @NonNullable Class<V> valueType;
- private final @NonNullable ConcurrentMap<String, KeyedSoftReference<V>>
map;
- private final @NonNullable KeyedRefCollector collector;
+ private final @NonNullable ConcurrentMap<String,
KeyedSoftReference<String,V>> map;
+ private final @NonNullable KeyedRefCollector<String> collector;
private final @NonNullable PerCacheConfig config;
public String getName() {
@@ -62,8 +62,8 @@
@NonNullable PerCacheConfig config,
int initialCapacity, float loadFactor, int concurrencyLevel)
{
- map = new
ConcurrentHashMap<String,KeyedSoftReference<V>>(initialCapacity, loadFactor,
concurrencyLevel);
- collector = new KeyedRefCollector(refq, map);
+ map = new
ConcurrentHashMap<String,KeyedSoftReference<String,V>>(initialCapacity,
loadFactor, concurrencyLevel);
+ collector = new KeyedRefCollector<String>(refq, map);
this.name = name;
this.valueType = valueType;
this.config = config;
@@ -74,8 +74,8 @@
@NonNullable PerCacheConfig config,
int initialCapacity)
{
- map = new
ConcurrentHashMap<String,KeyedSoftReference<V>>(initialCapacity);
- collector = new KeyedRefCollector(refq, map);
+ map = new
ConcurrentHashMap<String,KeyedSoftReference<String,V>>(initialCapacity);
+ collector = new KeyedRefCollector<String>(refq, map);
this.name = name;
this.valueType = valueType;
this.config = config;
@@ -86,8 +86,8 @@
@NonNullable Class<V> valueType,
@NonNullable PerCacheConfig config)
{
- map = new ConcurrentHashMap<String,KeyedSoftReference<V>>();
- collector = new KeyedRefCollector(refq, map);
+ map = new ConcurrentHashMap<String,KeyedSoftReference<String,V>>();
+ collector = new KeyedRefCollector<String>(refq, map);
this.name = name;
this.valueType = valueType;
this.config = config;
@@ -108,7 +108,7 @@
// It's not thread-safe, but what's the worst consequence ?
public V get(@NonNullable String key) {
this.collector.run();
- KeyedSoftReference<V> ref = map.get(key);
+ KeyedSoftReference<String,V> ref = map.get(key);
if (ref == null)
return null;
@@ -168,7 +168,8 @@
}
public V put(@NonNullable String key, @NonNullable V value) {
this.collector.run();
- KeyedSoftReference<V> oldRef = map.put(key, new
KeyedSoftReference<V>(key, value, refq));
+ KeyedSoftReference<String,V> oldRef =
+ map.put(key, new KeyedSoftReference<String,V>(key, value,
refq));
if (oldRef == null)
return null;
@@ -195,7 +196,7 @@
}
public V remove(@NonNullable String key) {
this.collector.run();
- KeyedSoftReference<V> oldRef = map.remove(key);
+ KeyedSoftReference<String,V> oldRef = map.remove(key);
if (oldRef == null)
return null;
@@ -217,11 +218,11 @@
}
public @NonNullable Set<Map.Entry<String,V>> entrySet() {
this.collector.run();
- Set<Map.Entry<String,KeyedSoftReference<V>>> fromSet =
map.entrySet();
+ Set<Map.Entry<String,KeyedSoftReference<String,V>>> fromSet =
map.entrySet();
Set<Map.Entry<String,V>> toSet = new HashSet<Map.Entry<String,V>>();
- for (final Map.Entry<String,KeyedSoftReference<V>> item : fromSet) {
- KeyedSoftReference<V> ref = item.getValue();
+ for (final Map.Entry<String,KeyedSoftReference<String,V>> item :
fromSet) {
+ KeyedSoftReference<String,V> ref = item.getValue();
V val = ref.get();
if (val != null) {
@@ -233,10 +234,10 @@
}
public @NonNullable Collection<V> values() {
this.collector.run();
- Collection<KeyedSoftReference<V>> fromSet = map.values();
+ Collection<KeyedSoftReference<String,V>> fromSet = map.values();
List<V> toCol = new ArrayList<V>(fromSet.size());
- for (final KeyedSoftReference<V> ref : fromSet) {
+ for (final KeyedSoftReference<String,V> ref : fromSet) {
V val = ref.get();
if (val != null) {
@@ -252,9 +253,9 @@
}
public boolean containsValue(@NonNullable Object value) {
this.collector.run();
- Collection<KeyedSoftReference<V>> fromSet = map.values();
+ Collection<KeyedSoftReference<String,V>> fromSet = map.values();
- for (final KeyedSoftReference<V> ref : fromSet) {
+ for (final KeyedSoftReference<String,V> ref : fromSet) {
V val = ref.get();
if (value.equals(val))
1.4 +3 -2
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCacheCleaner.java
Index: SoftRefCacheCleaner.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCacheCleaner.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SoftRefCacheCleaner.java 27 Jan 2005 11:05:29 -0000 1.3
+++ SoftRefCacheCleaner.java 28 Jan 2005 10:41:10 -0000 1.4
@@ -40,14 +40,15 @@
private volatile int countDataRaceAndRemovedByOthers;
private volatile int countBye;
- <V> void cleanupKey(@NonNullable Map<String, KeyedSoftReference<V>> map,
@NonNullable String key)
+ <V> void cleanupKey(@NonNullable Map<String,
+ KeyedSoftReference<String,V>> map, @NonNullable String key)
{
V val = null;
// already garbage collected. So try to clean up the key.
if (debug)
log.debug("Try to clean up the key");
this.countTryKeyClean++;
- KeyedSoftReference<V> oldRef = map.remove(key);
+ KeyedSoftReference<String,V> oldRef = map.remove(key);
// If oldRef is null, the key has just been
// cleaned up by another thread.
if (oldRef == null) {
1.2 +22 -23
jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefFileCache.java
Index: SoftRefFileCache.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefFileCache.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SoftRefFileCache.java 27 Jan 2005 11:02:12 -0000 1.1
+++ SoftRefFileCache.java 28 Jan 2005 10:41:10 -0000 1.2
@@ -59,12 +59,11 @@
private final @NonNullable ReferenceQueue<V> refq = new
ReferenceQueue<V>();
private final @NonNullable String name;
private final @NonNullable Class<V> valueType;
- private final @NonNullable ConcurrentMap<String, KeyedSoftReference<V>>
map;
+ private final @NonNullable
ConcurrentMap<String,KeyedSoftReference<String,V>> map;
private final @NonNullable PerCacheConfig config;
-
-
- private final @NonNullable KeyedRefCollector collector;
- private final IKeyedReadWriteLock krwl = new KeyedReadWriteLock();
+
+ private final @NonNullable KeyedRefCollector<String> collector;
+ private final IKeyedReadWriteLock<String> krwl = new
KeyedReadWriteLock<String>();
// private final @NonNullable ConcurrentMap<String, CacheOp[]> synMap =
// new ConcurrentHashMap<String, CacheOp[]>();
@@ -91,9 +90,9 @@
@NonNullable PerCacheConfig config,
int initialCapacity,float loadFactor, int concurrencyLevel)
{
- map = new ConcurrentHashMap<String,KeyedSoftReference<V>>(
+ map = new ConcurrentHashMap<String,KeyedSoftReference<String,V>>(
initialCapacity, loadFactor, concurrencyLevel);
- collector = new KeyedRefCollector(refq, map);
+ collector = new KeyedRefCollector<String>(refq, map);
this.name = name;
this.valueType = valueType;
this.config = config;
@@ -103,8 +102,8 @@
@NonNullable PerCacheConfig config,
int initialCapacity)
{
- map = new
ConcurrentHashMap<String,KeyedSoftReference<V>>(initialCapacity);
- collector = new KeyedRefCollector(refq, map);
+ map = new
ConcurrentHashMap<String,KeyedSoftReference<String,V>>(initialCapacity);
+ collector = new KeyedRefCollector<String>(refq, map);
this.name = name;
this.valueType = valueType;
this.config = config;
@@ -114,8 +113,8 @@
@NonNullable Class<V> valueType,
PerCacheConfig config)
{
- map = new ConcurrentHashMap<String,KeyedSoftReference<V>>();
- collector = new KeyedRefCollector(refq, map);
+ map = new ConcurrentHashMap<String,KeyedSoftReference<String,V>>();
+ collector = new KeyedRefCollector<String>(refq, map);
this.name = name;
this.valueType = valueType;
this.config = config;
@@ -145,7 +144,7 @@
}
}
private V doGet(String key) {
- KeyedSoftReference<V> ref = map.get(key);
+ KeyedSoftReference<String,V> ref = map.get(key);
V val = null;
if (ref != null)
@@ -174,7 +173,7 @@
}
// Resurrect item back to memory.
map.putIfAbsent(key,
- new KeyedSoftReference<V>(key, val, refq));
+ new KeyedSoftReference<String,V>(key, val, refq));
}
// cache value exists.
return val;
@@ -233,8 +232,8 @@
}
}
private V doPut(@NonNullable String key, @NonNullable V value) {
- KeyedSoftReference<V> oldRef =
- map.put(key, new KeyedSoftReference<V>(key, value, refq));
+ KeyedSoftReference<String,V> oldRef =
+ map.put(key, new KeyedSoftReference<String,V>(key, value,
refq));
V ret = null;
if (oldRef != null) {
@@ -295,7 +294,7 @@
}
}
private V doRemove(@NonNullable String key) {
- KeyedSoftReference<V> oldRef = map.remove(key);
+ KeyedSoftReference<String,V> oldRef = map.remove(key);
V ret = null;
if (oldRef != null) {
@@ -335,11 +334,11 @@
}
public @NonNullable Set<Map.Entry<String,V>> entrySet() {
// this.collector.run();
- Set<Map.Entry<String,KeyedSoftReference<V>>> fromSet =
map.entrySet();
+ Set<Map.Entry<String,KeyedSoftReference<String,V>>> fromSet =
map.entrySet();
Set<Map.Entry<String,V>> toSet = new HashSet<Map.Entry<String,V>>();
- for (final Map.Entry<String, KeyedSoftReference<V>> item : fromSet) {
- KeyedSoftReference<V> ref = item.getValue();
+ for (final Map.Entry<String, KeyedSoftReference<String,V>> item :
fromSet) {
+ KeyedSoftReference<String,V> ref = item.getValue();
V val = ref.get();
if (val != null) {
@@ -351,10 +350,10 @@
}
public @NonNullable Collection<V> values() {
// this.collector.run();
- Collection<KeyedSoftReference<V>> fromSet = map.values();
+ Collection<KeyedSoftReference<String,V>> fromSet = map.values();
List<V> toCol = new ArrayList<V>(fromSet.size());
- for (final KeyedSoftReference<V> ref : fromSet) {
+ for (final KeyedSoftReference<String,V> ref : fromSet) {
V val = ref.get();
if (val != null) {
@@ -368,9 +367,9 @@
}
public boolean containsValue(@NonNullable Object value) {
// this.collector.run();
- Collection<KeyedSoftReference<V>> fromSet = map.values();
+ Collection<KeyedSoftReference<String,V>> fromSet = map.values();
- for (final KeyedSoftReference<V> ref : fromSet) {
+ for (final KeyedSoftReference<String,V> ref : fromSet) {
V val = ref.get();
if (value.equals(val))
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]