Update of /var/cvs/src/org/mmbase/cache
In directory james.mmbase.org:/tmp/cvs-serv9820

Modified Files:
      Tag: MMBase-1_8
        Cache.java CacheImplementationInterface.java 
        QueryResultCache.java 
Log Message:
  MMB-1667, changed locking object, and made it explicit


See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/cache
See also: http://www.mmbase.org/jira/browse/MMB-1667


Index: Cache.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/cache/Cache.java,v
retrieving revision 1.36.2.2
retrieving revision 1.36.2.3
diff -u -b -r1.36.2.2 -r1.36.2.3
--- Cache.java  16 Jun 2008 13:23:29 -0000      1.36.2.2
+++ Cache.java  24 Jun 2008 09:52:22 -0000      1.36.2.3
@@ -21,7 +21,7 @@
  * A base class for all Caches. Extend this class for other caches.
  *
  * @author Michiel Meeuwissen
- * @version $Id: Cache.java,v 1.36.2.2 2008/06/16 13:23:29 sdeboer Exp $
+ * @version $Id: Cache.java,v 1.36.2.3 2008/06/24 09:52:22 michiel Exp $
  */
 abstract public class Cache implements SizeMeasurable, Map {
 
@@ -34,6 +34,10 @@
      * @since MMBase-1.8
      */
     private CacheImplementationInterface implementation;
+    /**
+     * @since MMBase-1.8.6
+     */
+    protected Object lock;
 
     /**
      * The number of times an element was succesfully retrieved from this 
cache.
@@ -52,6 +56,8 @@
 
     public Cache(int size) {
         implementation = new LRUCache(size);
+        lock           = implementation.getLock();
+
         log.service("Creating cache " + getName() + ": " + getDescription());
     }
 
@@ -116,6 +122,13 @@
     }
 
     /**
+     * @since MMBase-1.8.6
+     */
+    public Class getImplementation() {
+        return implementation.getClass();
+    }
+
+    /**
      * Checks whether the key object should be cached.
      * This method returns <code>false</code> if either the current cache is 
inactive, or the object to cache
      * has a cache policy associated that prohibits caching of the object.


Index: CacheImplementationInterface.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/cache/CacheImplementationInterface.java,v
retrieving revision 1.5
retrieving revision 1.5.2.1
diff -u -b -r1.5 -r1.5.2.1
--- CacheImplementationInterface.java   23 Feb 2006 17:36:55 -0000      1.5
+++ CacheImplementationInterface.java   24 Jun 2008 09:52:22 -0000      1.5.2.1
@@ -17,7 +17,7 @@
  * An implementation of this interface has to be thread-safe to guarantee 
correctness.
  *
  * @author Michiel Meeuwissen
- * @version $Id: CacheImplementationInterface.java,v 1.5 2006/02/23 17:36:55 
michiel Exp $
+ * @version $Id: CacheImplementationInterface.java,v 1.5.2.1 2008/06/24 
09:52:22 michiel Exp $
  * @since MMBase-1.8
  */
 public interface CacheImplementationInterface extends Map {
@@ -42,4 +42,6 @@
      */
     void config(Map configuration);
 
+    Object getLock();
+
 }


Index: QueryResultCache.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/cache/QueryResultCache.java,v
retrieving revision 1.34.2.4
retrieving revision 1.34.2.5
diff -u -b -r1.34.2.4 -r1.34.2.5
--- QueryResultCache.java       17 Sep 2007 16:54:39 -0000      1.34.2.4
+++ QueryResultCache.java       24 Jun 2008 09:52:22 -0000      1.34.2.5
@@ -32,7 +32,7 @@
  * @author Daniel Ockeloen
  * @author Michiel Meeuwissen
  * @author Bunst Eunders
- * @version $Id: QueryResultCache.java,v 1.34.2.4 2007/09/17 16:54:39 pierre 
Exp $
+ * @version $Id: QueryResultCache.java,v 1.34.2.5 2008/06/24 09:52:22 michiel 
Exp $
  * @since MMBase-1.7
  * @see org.mmbase.storage.search.SearchQuery
  */
@@ -112,11 +112,13 @@
      * @param query 
      * @param queryResult
      */
-    public synchronized Object put(SearchQuery query, List queryResult) {
+    public Object put(SearchQuery query, List queryResult) {
         if (!checkCachePolicy(query)) return null;
+        synchronized(lock) {
         increaseCounters(query, typeCounters);
         return super.put(query, queryResult);
     }
+    }
 
     /**
      * @throws ClassCastException if key not a SearchQuery or value not a List.
@@ -136,11 +138,13 @@
      *
      * @param key A SearchQuery object.
      */
-    public synchronized Object remove(SearchQuery query) {
+    public Object remove(SearchQuery query) {
+        synchronized(lock) {
         Object result = super.remove(query);
-        decreaseCounters(query, typeCounters);
+            if (result != null) decreaseCounters(query, typeCounters);
         return result;
     }
+    }
 
     private void increaseCounters(SearchQuery query, Map counters) {
         for (Iterator iter = query.getSteps().iterator(); iter.hasNext();) {
@@ -149,8 +153,7 @@
             if (counters.containsKey(stepName)) {
                 int count = ((Integer) counters.get(stepName)).intValue();
                 counters.put(stepName, new Integer(count + 1));
-            }
-            else {
+            } else {
                 counters.put(stepName, new Integer(1));
             }
         }
@@ -164,8 +167,7 @@
                 int count = ((Integer) counters.get(stepName)).intValue();
                 if (count > 1) {
                     counters.put(stepName, new Integer(count - 1));
-                }
-                else {
+                } else {
                     counters.remove(stepName);
                 }
             }
@@ -232,12 +234,14 @@
     }
 
     private boolean containsType(NodeEvent event) {
+        synchronized(lock) {
         if (typeCounters.containsKey("object")) {
             return true;
         }
         if (typeCounters.containsKey(event.getBuilderName())) {
             return true;
         }
+
         MMBase mmb = MMBase.getMMBase();
         MMObjectBuilder destBuilder = mmb.getBuilder(event.getBuilderName());
         if (destBuilder == null) {  // builder is not even available
@@ -251,6 +255,7 @@
         }
         return false;
     }
+    }
 
     protected int nodeChanged(Event event) throws IllegalArgumentException{
         if (log.isDebugEnabled()) {
@@ -258,7 +263,7 @@
         }
         Set cacheKeys;
         Map oldTypeCounters;
-        synchronized(this) {
+        synchronized(lock) {
             cacheKeys = new HashSet(keySet());
             oldTypeCounters = new HashMap(typeCounters);
         }
@@ -268,12 +273,12 @@
 
         evaluate(event, cacheKeys, removeKeys, foundTypeCounters);
 
-        synchronized(this) {
             Iterator removeIter = removeKeys.iterator();
             while(removeIter.hasNext()) {
                 remove(removeIter.next());
             }
             
+        synchronized(lock) {
             // types in the oldTypesCounter which are not in the typeCounters 
are removed during the 
             // evaluation of the keys and are not relevant anymore.
             for (Iterator iter = typeCounters.keySet().iterator(); 
iter.hasNext();) {
@@ -288,15 +293,13 @@
                             int newValue = foundValue + (guessedValue - 
oldValue);
                             foundTypeCounters.put(type, new Integer(newValue));
                         }
-                    }
-                    else {
+                    } else {
                         int guessedValue = ((Integer) 
typeCounters.get(type)).intValue();
                         int foundValue = ((Integer) 
foundTypeCounters.get(type)).intValue();
                         int newValue = foundValue + guessedValue;
                         foundTypeCounters.put(type, new Integer(newValue));
                     }
-                }
-                else {
+                } else {
                     Integer guessedValue = (Integer) typeCounters.get(type);
                     foundTypeCounters.put(type, guessedValue);
                 }
@@ -306,6 +309,7 @@
         return removeKeys.size();
     }
 
+    // if I
     private void evaluate(Event event, Set cacheKeys, Set removeKeys, Map 
foundTypeCounters) {
         int evaluatedResults = cacheKeys.size();
         long startTime = System.currentTimeMillis();
@@ -333,8 +337,7 @@
 
             if (shouldRelease) {
                 removeKeys.add(key);
-            }
-            else {
+            } else {
                 increaseCounters(key, foundTypeCounters);
             }
         }
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to