You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2010/09/28 12:34:09 UTC

svn commit: r1002102 - /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java

Author: jukka
Date: Tue Sep 28 10:34:08 2010
New Revision: 1002102

URL: http://svn.apache.org/viewvc?rev=1002102&view=rev
Log:
JCR-2699: Improve read/write concurrency

Make LRUNodeIdCache synchronized and use the LinkedHashMap class instead of the LinkedMap from Commons Collections (LinkedHashMap supports access-ordering, which makes it better for a LRU map)

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java?rev=1002102&r1=1002101&r2=1002102&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java Tue Sep 28 10:34:08 2010
@@ -16,7 +16,9 @@
  */
 package org.apache.jackrabbit.core.persistence.util;
 
-import org.apache.commons.collections.map.LinkedMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 import org.apache.jackrabbit.core.id.NodeId;
 import org.slf4j.LoggerFactory;
 import org.slf4j.Logger;
@@ -34,7 +36,7 @@ public class LRUNodeIdCache {
     /**
      * The maximum number of ids to cache
      */
-    private long maxSize = 10240;
+    private static final int maxSize = 10240;
 
     /**
      * the number of cache hits
@@ -49,7 +51,14 @@ public class LRUNodeIdCache {
     /**
      * the map of cached ids
      */
-    private final LinkedMap missing = new LinkedMap();
+    @SuppressWarnings("serial")
+    private final LinkedHashMap<NodeId, NodeId> missing =
+        new LinkedHashMap<NodeId, NodeId>(maxSize * 2, 0.75f, true) {
+            @Override
+            protected boolean removeEldestEntry(Map.Entry<NodeId, NodeId> e) {
+                return size() > maxSize;
+            }
+        };
 
     /**
      * Checks if the given id is contained in this cached.
@@ -58,31 +67,25 @@ public class LRUNodeIdCache {
      * @return <code>true</code> if the id is cached;
      *         <code>false</code> otherwise.
      */
-    public boolean contains(NodeId id) {
-        Object o = missing.remove(id);
-        if (o == null) {
-            misses++;
-        } else {
-            missing.put(id, id);
+    public synchronized boolean contains(NodeId id) {
+        boolean rv = missing.get(id) != null;
+        if (rv) {
             hits++;
+        } else {
+            misses++;
         }
         if (log.isInfoEnabled() && (hits + misses) % 10000 == 0) {
             log.info("num=" + missing.size() + "/" + maxSize + " hits=" + hits + " miss=" + misses);
         }
-        return o != null;
+        return rv;
     }
 
     /**
      * Puts the given id to this cache.
      * @param id the id to put.
      */
-    public void put(NodeId id) {
-        if (!missing.containsKey(id)) {
-            if (missing.size() == maxSize) {
-                missing.remove(0);
-            }
-            missing.put(id, id);
-        }
+    public synchronized void put(NodeId id) {
+        missing.put(id, id);
     }
 
     /**
@@ -91,14 +94,14 @@ public class LRUNodeIdCache {
      * @return <code>true</code> if the id was cached;
      *         <code>false</code> otherwise.
      */
-    public boolean remove(NodeId id) {
+    public synchronized boolean remove(NodeId id) {
         return missing.remove(id) != null;
     }
 
     /**
      * Clears this cache.
      */
-    public void clear() {
+    public synchronized void clear() {
         missing.clear();
     }