From cdf35480d942503647195a6152e59d382edd009c Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 24 Jun 2012 10:44:39 +0200 Subject: [PATCH] improve cache for repository commands --- .../repository/RepositoryCacheKeyFilter.java | 80 +++++++++++++++++++ .../repository/api/BlameCommandBuilder.java | 10 ++- .../repository/api/BrowseCommandBuilder.java | 8 +- .../scm/repository/api/LogCommandBuilder.java | 6 +- .../api/RepositoryServiceFactory.java | 33 +++----- 5 files changed, 104 insertions(+), 33 deletions(-) create mode 100644 scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java new file mode 100644 index 0000000000..dc25a1a5a2 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.Filter; + +/** + * + * @author Sebastian Sdorra + * @since 1.17 + * + * @param + */ +public class RepositoryCacheKeyFilter + implements Filter +{ + + /** + * Constructs ... + * + * + * @param repositoryId + */ + public RepositoryCacheKeyFilter(String repositoryId) + { + this.repositoryId = repositoryId; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param item + * + * @return + */ + @Override + public boolean accept(T item) + { + return repositoryId.equals(item.getRepositoryId()); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String repositoryId; +} diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java index ce04f58053..93891937a5 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java @@ -46,6 +46,7 @@ import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; import sonia.scm.repository.BlameResult; import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryCacheKey; import sonia.scm.repository.RepositoryException; import sonia.scm.repository.spi.BlameCommand; import sonia.scm.repository.spi.BlameCommandRequest; @@ -62,13 +63,13 @@ import java.io.Serializable; * Sample: *
*
- * Print each line number and code of the file scm-core/pom.xml at + * Print each line number and code of the file scm-core/pom.xml at * revision 60c2f2783368:
*

  * BlameCommandBuilder blame = repositoryService.getBlameCommand();
  * BlameResult result = blame.setRevision("60c2f2783368")
  *                           .getBlameResult("scm-core/pom.xml");
- * 
+ *
  * for ( BlameLine line : result ){
  *   System.out.println(line.getLineNumber() + ": " + line.getCode());
  * }
@@ -228,7 +229,7 @@ public final class BlameCommandBuilder
    * @version        Enter version here..., 12/06/05
    * @author         Enter your name here...
    */
-  static class CacheKey implements Serializable
+  static class CacheKey implements RepositoryCacheKey, Serializable
   {
 
     /**
@@ -293,7 +294,8 @@ public final class BlameCommandBuilder
      *
      * @return
      */
-    String getRepositoryId()
+    @Override
+    public String getRepositoryId()
     {
       return repositoryId;
     }
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java
index d571fd6a71..4ca46a54ff 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java
@@ -45,6 +45,7 @@ import sonia.scm.cache.CacheManager;
 import sonia.scm.repository.BrowserResult;
 import sonia.scm.repository.PreProcessorUtil;
 import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryCacheKey;
 import sonia.scm.repository.RepositoryException;
 import sonia.scm.repository.spi.BrowseCommand;
 import sonia.scm.repository.spi.BrowseCommandRequest;
@@ -66,7 +67,7 @@ import java.io.Serializable;
  * BrowserResult result = browse.setPath("scm-core")
  *                              .setRevision("11aeec7db845")
  *                              .getBrowserResult();
- * 
+ *
  * for ( FileObject fo : result ){
  *   System.out.println( fo.getPath() );
  * }
@@ -257,7 +258,7 @@ public final class BrowseCommandBuilder
    * @version        Enter version here..., 12/06/05
    * @author         Enter your name here...
    */
-  static class CacheKey implements Serializable
+  static class CacheKey implements RepositoryCacheKey, Serializable
   {
 
     /**
@@ -322,7 +323,8 @@ public final class BrowseCommandBuilder
      *
      * @return
      */
-    String getRepositoryId()
+    @Override
+    public String getRepositoryId()
     {
       return repositoryId;
     }
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java
index 1fe3e35afb..fe6e5ebcef 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java
@@ -55,6 +55,7 @@ import sonia.scm.repository.spi.LogCommandRequest;
 
 import java.io.IOException;
 import java.io.Serializable;
+import sonia.scm.repository.RepositoryCacheKey;
 
 /**
  * LogCommandBuilder is able to show the history of a file in a
@@ -396,7 +397,7 @@ public final class LogCommandBuilder
    * @version        Enter version here..., 12/06/05
    * @author         Enter your name here...
    */
-  static class CacheKey implements Serializable
+  static class CacheKey implements RepositoryCacheKey, Serializable
   {
 
     /**
@@ -476,7 +477,8 @@ public final class LogCommandBuilder
      *
      * @return
      */
-    String getRepositoryId()
+    @Override
+    public String getRepositoryId()
     {
       return repositoryId;
     }
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java
index 78775e678a..8ec4da7f2b 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java
@@ -55,6 +55,8 @@ import sonia.scm.repository.PermissionUtil;
 import sonia.scm.repository.PostReceiveRepositoryHook;
 import sonia.scm.repository.PreProcessorUtil;
 import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryCacheKey;
+import sonia.scm.repository.RepositoryCacheKeyFilter;
 import sonia.scm.repository.RepositoryHookEvent;
 import sonia.scm.repository.RepositoryManager;
 import sonia.scm.repository.RepositoryNotFoundException;
@@ -332,6 +334,7 @@ public final class RepositoryServiceFactory
      *
      * @param repositoryId
      */
+    @SuppressWarnings("unchecked")
     private void clearCaches(final String repositoryId)
     {
       if (logger.isDebugEnabled())
@@ -339,30 +342,12 @@ public final class RepositoryServiceFactory
         logger.debug("clear caches for repository id {}", repositoryId);
       }
 
-      blameCache.removeAll(new Filter()
-      {
-        @Override
-        public boolean accept(BlameCommandBuilder.CacheKey item)
-        {
-          return repositoryId.equals(item.getRepositoryId());
-        }
-      });
-      browseCache.removeAll(new Filter()
-      {
-        @Override
-        public boolean accept(BrowseCommandBuilder.CacheKey item)
-        {
-          return repositoryId.equals(item.getRepositoryId());
-        }
-      });
-      logCache.removeAll(new Filter()
-      {
-        @Override
-        public boolean accept(LogCommandBuilder.CacheKey item)
-        {
-          return repositoryId.equals(item.getRepositoryId());
-        }
-      });
+      RepositoryCacheKeyFilter filter =
+        new RepositoryCacheKeyFilter(repositoryId);
+
+      blameCache.removeAll(filter);
+      browseCache.removeAll(filter);
+      logCache.removeAll(filter);
     }
 
     //~--- fields -------------------------------------------------------------