openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r652930 - in /openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent: NullSafeConcurrentHashMap.java SizedConcurrentHashMap.java
Date Fri, 02 May 2008 22:21:51 GMT
Author: pcl
Date: Fri May  2 15:21:51 2008
New Revision: 652930

URL: http://svn.apache.org/viewvc?rev=652930&view=rev
Log:
Simplify algorithm in NullSafeConcurrentHashMap; change SizedConcurrentHashMap's algorithms
to remove overflow before putting new records into cache instead of after to avoid removing
the very instances that were put.

Modified:
    openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java
    openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java

Modified: openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java?rev=652930&r1=652929&r2=652930&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java
(original)
+++ openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/NullSafeConcurrentHashMap.java
Fri May  2 15:21:51 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/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java?rev=652930&r1=652929&r2=652930&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java
(original)
+++ openjpa/branches/1.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/SizedConcurrentHashMap.java
Fri May  2 15:21:51 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



Mime
View raw message