You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2008/06/12 01:43:12 UTC
svn commit: r666903 - in
/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent:
NullSafeConcurrentHashMap.java SizedConcurrentHashMap.java
Author: pcl
Date: Wed Jun 11 16:43:11 2008
New Revision: 666903
URL: http://svn.apache.org/viewvc?rev=666903&view=rev
Log:
Merge from ../active. svn merge -c 652930 ../active
Modified:
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java
openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java
Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java?rev=666903&r1=666902&r2=666903&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java Wed Jun 11 16:43:11 2008
@@ -83,20 +83,21 @@
// as other threads remove the same entries, whereas the random
// iterator may return values that have been removed.
- while (!isEmpty()) {
- while (!randomKeys.isEmpty()) {
- // randomKeys contains null-masked data
- Iterator iter = randomKeys.iterator();
- Object key = iter.next();
- if (key != null && randomKeys.remove(key)) {
- Object val = super.remove(key);
- if (val != null)
- return new EntryImpl(unmaskNull(key), unmaskNull(val));
- }
+ for (Iterator iter = randomKeys.iterator(); iter.hasNext(); ) {
+ // randomKeys contains null-masked data
+ Object key = iter.next();
+ if (key != null && randomKeys.remove(key)) {
+ Object val = super.remove(key);
+ if (val != null)
+ return new EntryImpl(unmaskNull(key), unmaskNull(val));
}
+ }
- // if randomKeys is empty, fall back to non-random behavior.
- Object key = super.keySet().iterator().next();
+ // if randomKeys is empty, fall back to non-random behavior.
+ for (Iterator iter = super.keySet().iterator(); iter.hasNext(); ) {
+ Object key = iter.next();
+ if (key == null)
+ continue;
Object val = super.remove(key);
if (val != null)
return new EntryImpl(unmaskNull(key), unmaskNull(val));
Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java?rev=666903&r1=666902&r2=666903&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java Wed Jun 11 16:43:11 2008
@@ -56,18 +56,16 @@
@Override
public Object putIfAbsent(Object key, Object value) {
- Object o = super.putIfAbsent(key, value);
if (maxSize != Integer.MAX_VALUE)
- removeOverflow();
- return o;
+ removeOverflow(true);
+ return super.putIfAbsent(key, value);
}
@Override
public Object put(Object key, Object value) {
- Object o = super.put(key, value);
if (maxSize != Integer.MAX_VALUE)
- removeOverflow();
- return o;
+ removeOverflow(true);
+ return super.put(key, value);
}
public int getMaxSize() {
@@ -79,11 +77,23 @@
throw new IllegalArgumentException(String.valueOf(max));
maxSize = max;
- removeOverflow();
+ removeOverflow(false);
}
+ /**
+ * Equivalent to <code>removeOverflow(false)</code>.
+ */
protected void removeOverflow() {
- while (size() > maxSize) {
+ removeOverflow(false);
+ }
+
+ /**
+ * Removes overflow. If <code>forPut</code> is <code>true</code>, then
+ * this uses <code>size() + 1</code> when computing size.
+ */
+ protected void removeOverflow(boolean forPut) {
+ int sizeToCompareTo = forPut ? maxSize - 1 : maxSize;
+ while (size() > sizeToCompareTo) {
Entry entry = removeRandom();
// if removeRandom() returns null, break out of the loop. Of course,
// since we're not locking, the size might not actually be null