jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1025990 - in /jackrabbit/branches/2.1: ./ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/cache/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core...
Date Thu, 21 Oct 2010 13:25:22 GMT
Author: jukka
Date: Thu Oct 21 13:25:20 2010
New Revision: 1025990

URL: http://svn.apache.org/viewvc?rev=1025990&view=rev
Log:
2.1: Merged revisions 1003542, 1003773 and 1004182 (JCR-2699)

Added:
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/cache/AbstractCache.java
      - copied unchanged from r1004182, jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/cache/AbstractCache.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/cache/ConcurrentCache.java
      - copied unchanged from r1004182, jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/cache/ConcurrentCache.java
    jackrabbit/branches/2.1/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/cache/
      - copied from r1004182, jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/cache/
    jackrabbit/branches/2.1/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/cache/ConcurrentCacheTest.java
      - copied unchanged from r1004182, jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/cache/ConcurrentCacheTest.java
Removed:
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleCache.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/LRUNodeIdCache.java
Modified:
    jackrabbit/branches/2.1/   (props changed)
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateCache.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
    jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ManagedMLRUItemStateCacheFactory.java

Propchange: jackrabbit/branches/2.1/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct 21 13:25:20 2010
@@ -2,4 +2,4 @@
 /jackrabbit/sandbox/JCR-1456:774917-886178
 /jackrabbit/sandbox/JCR-2170:812417-816332
 /jackrabbit/sandbox/tripod-JCR-2209:795441-795863
-/jackrabbit/trunk:931121,931479,931483-931484,931504,931609,931613,931838,931919,932318-932319,933144,933197,933203,933213,933216,933554,933646,933694,934405,934412,934849,935557,936668,938099,945528,950440,950680,955222,955229,955307,955852,961487,961626,964362,965539,986682,986686,986715,991144,996810,1001707,1002065-1002066,1002084,1002101-1002102,1002168,1002170,1002589,1002608,1002657,1002729,1003423,1003470
+/jackrabbit/trunk:931121,931479,931483-931484,931504,931609,931613,931838,931919,932318-932319,933144,933197,933203,933213,933216,933554,933646,933694,934405,934412,934849,935557,936668,938099,945528,950440,950680,955222,955229,955307,955852,961487,961626,964362,965539,986682,986686,986715,991144,996810,1001707,1002065-1002066,1002084,1002101-1002102,1002168,1002170,1002589,1002608,1002657,1002729,1003423,1003470,1003542,1003773,1004182

Modified: jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java?rev=1025990&r1=1025989&r2=1025990&view=diff
==============================================================================
--- jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
(original)
+++ jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java
Thu Oct 21 13:25:20 2010
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.core.persi
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.apache.jackrabbit.core.cache.ConcurrentCache;
 import org.apache.jackrabbit.core.fs.FileSystemResource;
 import org.apache.jackrabbit.core.fs.FileSystem;
 import org.apache.jackrabbit.core.state.ItemState;
@@ -37,9 +38,7 @@ import org.apache.jackrabbit.core.persis
 import org.apache.jackrabbit.core.persistence.PersistenceManager;
 import org.apache.jackrabbit.core.persistence.util.BundleBinding;
 import org.apache.jackrabbit.core.util.StringIndex;
-import org.apache.jackrabbit.core.persistence.util.BundleCache;
 import org.apache.jackrabbit.core.persistence.util.HashMapIndex;
-import org.apache.jackrabbit.core.persistence.util.LRUNodeIdCache;
 import org.apache.jackrabbit.core.persistence.util.NodePropBundle;
 import org.apache.jackrabbit.spi.Name;
 import org.apache.jackrabbit.spi.commons.name.NameConstants;
@@ -68,7 +67,7 @@ import javax.jcr.PropertyType;
  * included in the bundle but generated when required.
  * <p/>
  * In order to increase performance, there are 2 caches maintained. One is the
- * {@link BundleCache} that caches already loaded bundles. The other is the
+ * bundle cache that caches already loaded bundles. The other is the
  * {@link LRUNodeIdCache} that caches non-existent bundles. This is useful
  * because a lot of {@link #exists(NodeId)} calls are issued that would result
  * in a useless SQL execution if the desired bundle does not exist.
@@ -96,6 +95,10 @@ public abstract class AbstractBundlePers
     /** the name of the namespace-index resource */
     protected static final String RES_NS_INDEX = "/namespaces.properties";
 
+    /** Sentinel instance used to mark a non-existent bundle in the cache */
+    private static final NodePropBundle MISSING =
+        new NodePropBundle(null, new NodeId());
+
     /** the index for namespaces */
     private StringIndex nsIndex;
 
@@ -103,10 +106,7 @@ public abstract class AbstractBundlePers
     private StringIndex nameIndex;
 
     /** the cache of loaded bundles */
-    private BundleCache bundles;
-
-    /** the cache of non-existent bundles */
-    private LRUNodeIdCache missing;
+    private ConcurrentCache<NodeId, NodePropBundle> bundles;
 
     /** the persistence manager context */
     protected PMContext context;
@@ -286,25 +286,22 @@ public abstract class AbstractBundlePers
      */
     public synchronized void onExternalUpdate(ChangeLog changes) {
         for (ItemState state : changes.modifiedStates()) {
-            if (state.isNode()) {
-                bundles.remove((NodeId) state.getId());
-            } else {
-                bundles.remove(state.getParentId());
-            }
+            bundles.remove(getBundleId(state));
         }
         for (ItemState state : changes.deletedStates()) {
-            if (state.isNode()) {
-                bundles.remove((NodeId) state.getId());
-            } else {
-                bundles.remove(state.getParentId());
-            }
+            bundles.remove(getBundleId(state));
         }
         for (ItemState state : changes.addedStates()) {
-            if (state.isNode()) {
-                missing.remove((NodeId) state.getId());
-            } else {
-                missing.remove(state.getParentId());
-            }
+            // There may have been a cache miss entry
+            bundles.remove(getBundleId(state));
+        }
+    }
+
+    private NodeId getBundleId(ItemState state) {
+        if (state.isNode()) {
+            return (NodeId) state.getId();
+        } else {
+            return state.getParentId();
         }
     }
 
@@ -385,8 +382,8 @@ public abstract class AbstractBundlePers
     public void init(PMContext context) throws Exception {
         this.context = context;
         // init bundle cache
-        bundles = new BundleCache(bundleCacheSize);
-        missing = new LRUNodeIdCache();
+        bundles = new ConcurrentCache<NodeId, NodePropBundle>();
+        bundles.setMaxMemorySize(bundleCacheSize);
     }
     
     /**
@@ -397,7 +394,6 @@ public abstract class AbstractBundlePers
     public void close() throws Exception {
         // clear caches
         bundles.clear();
-        missing.clear();
     }
 
     /**
@@ -504,7 +500,6 @@ public abstract class AbstractBundlePers
         } finally {
             if (!success) {
                 bundles.clear();
-                missing.clear();
             }
         }
     }
@@ -645,18 +640,17 @@ public abstract class AbstractBundlePers
      * @throws ItemStateException if an error occurs.
      */
     private NodePropBundle getBundle(NodeId id) throws ItemStateException {
-        if (missing.contains(id)) {
-            return null;
-        }
         NodePropBundle bundle = bundles.get(id);
-        if (bundle == null) {
+        if (bundle == MISSING) {
+            return null;
+        } else if (bundle == null) {
             synchronized (this) {
                 bundle = loadBundle(id);
                 if (bundle != null) {
                     bundle.markOld();
-                    bundles.put(bundle);
+                    bundles.put(id, bundle, bundle.getSize());
                 } else {
-                    missing.put(id);
+                    bundles.put(id, MISSING, 16);
                 }
             }
         }
@@ -673,7 +667,7 @@ public abstract class AbstractBundlePers
         destroyBundle(bundle);
         bundle.removeAllProperties();
         bundles.remove(bundle.getId());
-        missing.put(bundle.getId());
+        bundles.put(bundle.getId(), MISSING, 16);
     }
 
     /**
@@ -687,11 +681,11 @@ public abstract class AbstractBundlePers
         bundle.markOld();
         log.debug("stored bundle {}", bundle.getId());
 
-        missing.remove(bundle.getId());
         // only put to cache if already exists. this is to ensure proper overwrite
         // and not creating big contention during bulk loads
-        if (bundles.contains(bundle.getId())) {
-            bundles.put(bundle);
+        if (bundles.containsKey(bundle.getId())) {
+            bundles.remove(bundle.getId());
+            bundles.put(bundle.getId(), bundle, bundle.getSize());
         }
     }
 

Modified: jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java?rev=1025990&r1=1025989&r2=1025990&view=diff
==============================================================================
--- jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java
(original)
+++ jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java
Thu Oct 21 13:25:20 2010
@@ -24,6 +24,7 @@ import javax.jcr.PropertyType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.apache.jackrabbit.core.cache.ConcurrentCache;
 import org.apache.jackrabbit.core.fs.FileSystemResource;
 import org.apache.jackrabbit.core.fs.FileSystem;
 import org.apache.jackrabbit.core.id.ItemId;
@@ -34,9 +35,7 @@ import org.apache.jackrabbit.core.persis
 import org.apache.jackrabbit.core.persistence.PMContext;
 import org.apache.jackrabbit.core.persistence.PersistenceManager;
 import org.apache.jackrabbit.core.persistence.util.BundleBinding;
-import org.apache.jackrabbit.core.persistence.util.BundleCache;
 import org.apache.jackrabbit.core.persistence.util.HashMapIndex;
-import org.apache.jackrabbit.core.persistence.util.LRUNodeIdCache;
 import org.apache.jackrabbit.core.persistence.util.NodePropBundle;
 import org.apache.jackrabbit.core.state.ItemState;
 import org.apache.jackrabbit.core.state.ChangeLog;
@@ -68,7 +67,7 @@ import org.apache.jackrabbit.spi.commons
  * included in the bundle but generated when required.
  * <p/>
  * In order to increase performance, there are 2 caches maintained. One is the
- * {@link BundleCache} that caches already loaded bundles. The other is the
+ * bundle cache that caches already loaded bundles. The other is the
  * {@link LRUNodeIdCache} that caches non-existent bundles. This is useful
  * because a lot of {@link #exists(NodeId)} calls are issued that would result
  * in a useless SQL execution if the desired bundle does not exist.
@@ -96,6 +95,10 @@ public abstract class AbstractBundlePers
     /** the name of the namespace-index resource */
     protected static final String RES_NS_INDEX = "/namespaces.properties";
 
+    /** Sentinel instance used to mark a non-existent bundle in the cache */
+    private static final NodePropBundle MISSING =
+        new NodePropBundle(null, new NodeId());
+
     /** the index for namespaces */
     private StringIndex nsIndex;
 
@@ -103,10 +106,7 @@ public abstract class AbstractBundlePers
     private StringIndex nameIndex;
 
     /** the cache of loaded bundles */
-    private BundleCache bundles;
-
-    /** the cache of non-existent bundles */
-    private LRUNodeIdCache missing;
+    private ConcurrentCache<NodeId, NodePropBundle> bundles;
 
     /** the persistence manager context */
     protected PMContext context;
@@ -286,25 +286,22 @@ public abstract class AbstractBundlePers
      */
     public synchronized void onExternalUpdate(ChangeLog changes) {
         for (ItemState state : changes.modifiedStates()) {
-            if (state.isNode()) {
-                bundles.remove((NodeId) state.getId());
-            } else {
-                bundles.remove(state.getParentId());
-            }
+            bundles.remove(getBundleId(state));
         }
         for (ItemState state : changes.deletedStates()) {
-            if (state.isNode()) {
-                bundles.remove((NodeId) state.getId());
-            } else {
-                bundles.remove(state.getParentId());
-            }
+            bundles.remove(getBundleId(state));
         }
         for (ItemState state : changes.addedStates()) {
-            if (state.isNode()) {
-                missing.remove((NodeId) state.getId());
-            } else {
-                missing.remove(state.getParentId());
-            }
+            // There may have been a cache miss entry
+            bundles.remove(getBundleId(state));
+        }
+    }
+
+    private NodeId getBundleId(ItemState state) {
+        if (state.isNode()) {
+            return (NodeId) state.getId();
+        } else {
+            return state.getParentId();
         }
     }
 
@@ -385,19 +382,18 @@ public abstract class AbstractBundlePers
     public void init(PMContext context) throws Exception {
         this.context = context;
         // init bundle cache
-        bundles = new BundleCache(bundleCacheSize);
-        missing = new LRUNodeIdCache();
+        bundles = new ConcurrentCache<NodeId, NodePropBundle>();
+        bundles.setMaxMemorySize(bundleCacheSize);
     }
 
     /**
      * {@inheritDoc}
      *
-     *  Closes the persistence manager, release acquired resourecs.
+     *  Closes the persistence manager, release acquired resources.
      */
     public void close() throws Exception {
         // clear caches
         bundles.clear();
-        missing.clear();
     }
 
     /**
@@ -502,7 +498,6 @@ public abstract class AbstractBundlePers
         } finally {
             if (!success) {
                 bundles.clear();
-                missing.clear();
             }
         }
     }
@@ -643,18 +638,17 @@ public abstract class AbstractBundlePers
      * @throws ItemStateException if an error occurs.
      */
     private NodePropBundle getBundle(NodeId id) throws ItemStateException {
-        if (missing.contains(id)) {
-            return null;
-        }
         NodePropBundle bundle = bundles.get(id);
-        if (bundle == null) {
+        if (bundle == MISSING) {
+            return null;
+        } else if (bundle == null) {
             synchronized (this) {
                 bundle = loadBundle(id);
                 if (bundle != null) {
                     bundle.markOld();
-                    bundles.put(bundle);
+                    bundles.put(id, bundle, bundle.getSize());
                 } else {
-                    missing.put(id);
+                    bundles.put(id, MISSING, 16);
                 }
             }
         }
@@ -671,7 +665,7 @@ public abstract class AbstractBundlePers
         destroyBundle(bundle);
         bundle.removeAllProperties();
         bundles.remove(bundle.getId());
-        missing.put(bundle.getId());
+        bundles.put(bundle.getId(), MISSING, 16);
     }
 
     /**
@@ -685,11 +679,11 @@ public abstract class AbstractBundlePers
         bundle.markOld();
         log.debug("stored bundle {}", bundle.getId());
 
-        missing.remove(bundle.getId());
-        // only put to cache if already exists. this is to ensure proper overwrite
-        // and not creating big contention during bulk loads
-        if (bundles.contains(bundle.getId())) {
-            bundles.put(bundle);
+        // only put to cache if already exists. this is to ensure proper
+        // overwrite and not creating big contention during bulk loads
+        if (bundles.containsKey(bundle.getId())) {
+            bundles.remove(bundle.getId());
+            bundles.put(bundle.getId(), bundle, bundle.getSize());
         }
     }
 

Modified: jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateCache.java?rev=1025990&r1=1025989&r2=1025990&view=diff
==============================================================================
--- jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateCache.java
(original)
+++ jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateCache.java
Thu Oct 21 13:25:20 2010
@@ -83,14 +83,6 @@ public interface ItemStateCache {
     boolean isEmpty();
 
     /**
-     * Informs the cache that the item was modified and the cache might need to
-     * recalculate the items caching weight.
-     *
-     * @param id the id of the item that was modified.
-     */
-    void update(ItemId id);
-
-    /**
      * Informs the cache that it is no longer in use.
      */
     void dispose();

Modified: jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java?rev=1025990&r1=1025989&r2=1025990&view=diff
==============================================================================
--- jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
(original)
+++ jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
Thu Oct 21 13:25:20 2010
@@ -162,14 +162,6 @@ public class ItemStateReferenceCache imp
     /**
      * {@inheritDoc}
      */
-    public synchronized void update(ItemId id) {
-        // delegate
-        cache.update(id);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     public synchronized boolean isEmpty() {
         // check primary cache
         return refs.isEmpty();

Modified: jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java?rev=1025990&r1=1025989&r2=1025990&view=diff
==============================================================================
--- jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
(original)
+++ jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
Thu Oct 21 13:25:20 2010
@@ -16,14 +16,9 @@
  */
 package org.apache.jackrabbit.core.state;
 
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
 import org.apache.commons.collections.map.LinkedMap;
-import org.apache.jackrabbit.core.cache.Cache;
-import org.apache.jackrabbit.core.cache.CacheAccessListener;
+import org.apache.jackrabbit.core.cache.CacheManager;
+import org.apache.jackrabbit.core.cache.ConcurrentCache;
 import org.apache.jackrabbit.core.id.ItemId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,139 +33,59 @@ import org.slf4j.LoggerFactory;
  * TODO rename class to something more appropriate, e.g. FIFOItemSateCache since
  * it doesn't use a LRU eviction policy anymore.
  */
-public class MLRUItemStateCache implements ItemStateCache, Cache {
+public class MLRUItemStateCache implements ItemStateCache {
+
     /** Logger instance */
     private static Logger log = LoggerFactory.getLogger(MLRUItemStateCache.class);
 
     /** default maximum memory to use */
     public static final int DEFAULT_MAX_MEM = 4 * 1024 * 1024;
 
-    /** the amount of memory the entries use */
-    private volatile long totalMem;
-
-    /** the maximum of memory the cache may use */
-    private volatile long maxMem;
-
     /** the number of writes */
-    private volatile long numWrites;
-
-    /** the access count */
-    private volatile long accessCount;
+    private volatile long numWrites = 0;
 
-    /** the cache access listeners */
-    private CacheAccessListener accessListener;
-
-    /**
-     * A cache for <code>ItemState</code> instances
-     */
-    private final Map<ItemId, Entry> cache;
-
-    /**
-     * Constructs a new, empty <code>ItemStateCache</code> with a maximum amount
-     * of memory of {@link #DEFAULT_MAX_MEM}.
-     */
-    public MLRUItemStateCache() {
-        this(DEFAULT_MAX_MEM);
-    }
+    private final ConcurrentCache<ItemId, ItemState> cache =
+        new ConcurrentCache<ItemId, ItemState>();
 
-    /**
-     * Constructs a new, empty <code>ItemStateCache</code> with the specified
-     * maximum memory.
-     *
-     * @param maxMem the maximum amount of memory this cache may use.
-     */
-    @SuppressWarnings("serial")
-    private MLRUItemStateCache(int maxMem) {
-        this.maxMem = maxMem;
-        this.cache = new LinkedHashMap<ItemId, MLRUItemStateCache.Entry>(
-                maxMem / 1024, 0.75f, true /* access-ordered */) {
-            @Override
-            protected boolean removeEldestEntry(Map.Entry<ItemId, Entry> e) {
-                long maxMem = MLRUItemStateCache.this.maxMem;
-                if (totalMem <= maxMem) {
-                    return false;
-                } else if (totalMem - e.getValue().size <= maxMem) {
-                    totalMem -= e.getValue().size;
-                    return true;
-                } else {
-                    shrink();
-                    return false;
-                }
-            }
-        };
+    public MLRUItemStateCache(CacheManager cacheMgr) {
+        cache.setMaxMemorySize(DEFAULT_MAX_MEM);
+        cache.setAccessListener(cacheMgr);
+        cacheMgr.add(cache);
     }
 
     //-------------------------------------------------------< ItemStateCache >
+
     /**
      * {@inheritDoc}
      */
     public boolean isCached(ItemId id) {
-        synchronized (cache) {
-            return cache.containsKey(id);
-        }
+        return cache.containsKey(id);
     }
 
     /**
      * {@inheritDoc}
      */
     public ItemState retrieve(ItemId id) {
-        touch();
-        synchronized (cache) {
-            Entry entry = cache.get(id);
-            if (entry != null) {
-                return entry.state;
-            } else {
-                return null;
-            }
-        }
+        return cache.get(id);
     }
 
     /**
      * {@inheritDoc}
      */
     public ItemState[] retrieveAll() {
-        synchronized (cache) {
-            ItemState[] states = new ItemState[cache.size()];
-            int i = 0;
-            for (Entry entry : cache.values()) {
-                states[i++] = entry.state;
-            }
-            return states;
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void update(ItemId id) {
-        touch();
-        synchronized (cache) {
-            Entry entry = cache.get(id);
-            if (entry != null) {
-                totalMem -= entry.size;
-                entry.recalc();
-                totalMem += entry.size;
-            }
-        }
+        return cache.values();
     }
 
     /**
      * {@inheritDoc}
      */
     public void cache(ItemState state) {
-        touch();
-        synchronized (cache) {
-            ItemId id = state.getId();
-            if (cache.containsKey(id)) {
-                log.warn("overwriting cached entry " + id);
-                evict(id);
-            }
-            Entry entry = new Entry(state);
-            totalMem += entry.size;
-            cache.put(id, entry);
-            if (numWrites++ % 10000 == 0 && log.isDebugEnabled()) {
-                log.debug(this + " size=" + cache.size() + ", " + totalMem + "/" + maxMem);
-            }
+        cache.put(state.getId(), state, state.calculateMemoryFootprint());
+
+        if (numWrites++ % 10000 == 0 && log.isDebugEnabled()) {
+            log.debug("Item state cache size: {}% of {} bytes",
+                    cache.getMemoryUsed() * 100 / cache.getMaxMemorySize(),
+                    cache.getMaxMemorySize());
         }
     }
 
@@ -178,135 +93,28 @@ public class MLRUItemStateCache implemen
      * {@inheritDoc}
      */
     public void evict(ItemId id) {
-        touch();
-        synchronized (cache) {
-            Entry entry = cache.remove(id);
-            if (entry != null) {
-                totalMem -= entry.size;
-            }
-        }
+        cache.remove(id);
     }
 
     /**
      * {@inheritDoc}
      */
     public void evictAll() {
-        synchronized (cache) {
-            cache.clear();
-            totalMem = 0;
-        }
+        cache.clear();
     }
 
     /**
      * {@inheritDoc}
      */
     public boolean isEmpty() {
-        synchronized (cache) {
-            return cache.isEmpty();
-        }
-    }
-
-    private void touch() {
-        accessCount++;
-        if ((accessCount % CacheAccessListener.ACCESS_INTERVAL) == 0) {
-            if (accessListener != null) {
-                accessListener.cacheAccessed();
-            }
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public long getAccessCount() {
-        return accessCount;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public long getMaxMemorySize() {
-        return maxMem;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public long getMemoryUsed() {
-        return totalMem;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void resetAccessCount() {
-        synchronized (cache) {
-            accessCount = 0;
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void setMaxMemorySize(long size) {
-        synchronized (cache) {
-            this.maxMem = size;
-
-            // remove items, if too many
-            if (totalMem > maxMem) {
-                shrink();
-            }
-        }
-    }
-
-    private void shrink() {
-        List<Map.Entry<ItemId, Entry>> list =
-            new ArrayList<Map.Entry<ItemId, Entry>>(cache.entrySet());
-        for (int i = list.size() - 1; totalMem > maxMem && i >= 0; i--) {
-            Map.Entry<ItemId, Entry> last = list.get(i);
-            totalMem -= last.getValue().size;
-            cache.remove(last.getKey());
-        }
-    }
-
-    /**
-     * Set the cache access listener. Only one listener per cache is supported.
-     *
-     * @param listener the new listener
-     */
-    public void setAccessListener(CacheAccessListener listener) {
-        this.accessListener = listener;
+        return cache.isEmpty();
     }
 
     /**
      * {@inheritDoc}
      */
     public void dispose() {
-        synchronized (cache) {
-            if (accessListener != null) {
-                accessListener.disposeCache(this);
-            }
-        }
-    }
-
-
-    /**
-     * Internal cache entry.
-     */
-    private static class Entry {
-
-        private final ItemState state;
-
-        private long size;
-
-        public Entry(ItemState state) {
-            this.state = state;
-            this.size = 64 + state.calculateMemoryFootprint();
-        }
-
-        public void recalc() {
-            size = 64 + state.calculateMemoryFootprint();
-        }
+        cache.dispose();
     }
 
 }

Modified: jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ManagedMLRUItemStateCacheFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ManagedMLRUItemStateCacheFactory.java?rev=1025990&r1=1025989&r2=1025990&view=diff
==============================================================================
--- jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ManagedMLRUItemStateCacheFactory.java
(original)
+++ jackrabbit/branches/2.1/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/state/ManagedMLRUItemStateCacheFactory.java
Thu Oct 21 13:25:20 2010
@@ -41,10 +41,7 @@ public class ManagedMLRUItemStateCacheFa
      * Create a new cache instance and link it to the cache manager.
      */
     public ItemStateCache newItemStateCache() {
-        MLRUItemStateCache cache = new MLRUItemStateCache();
-        cacheMgr.add(cache);
-        cache.setAccessListener(cacheMgr);
-        return cache;
+        return new MLRUItemStateCache(cacheMgr);
     }
 
 }



Mime
View raw message