From 8aa83e56f1e52f2d3a70f764599426c9776ecebb Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 7 Nov 2011 13:17:46 +0100 Subject: [PATCH] improve repository caching --- .../sonia/scm/repository/BlameViewerUtil.java | 23 +++- .../sonia/scm/repository/CacheClearHook.java | 54 ++++++++-- .../scm/repository/ChangesetViewerUtil.java | 13 ++- .../scm/repository/PartCacheClearHook.java | 61 +++++++++++ .../scm/repository/RepositoryBrowserUtil.java | 18 +++- .../scm/repository/RepositoryCacheKey.java | 51 +++++++++ .../scm/repository/RepositoryFilter.java | 102 ++++++++++++++++++ 7 files changed, 306 insertions(+), 16 deletions(-) create mode 100644 scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java create mode 100644 scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKey.java create mode 100644 scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java diff --git a/scm-core/src/main/java/sonia/scm/repository/BlameViewerUtil.java b/scm-core/src/main/java/sonia/scm/repository/BlameViewerUtil.java index 69a7ded78b..e552a53d1f 100644 --- a/scm-core/src/main/java/sonia/scm/repository/BlameViewerUtil.java +++ b/scm-core/src/main/java/sonia/scm/repository/BlameViewerUtil.java @@ -57,7 +57,7 @@ import java.io.IOException; * @since 1.8 */ @Singleton -public class BlameViewerUtil extends CacheClearHook +public class BlameViewerUtil extends PartCacheClearHook { /** Field description */ @@ -104,8 +104,7 @@ public class BlameViewerUtil extends CacheClearHook * @throws NotSupportedFeatuerException * @throws RepositoryException */ - public BlameResult getBlame(String repositoryId, String revision, - String path) + public BlameResult getBlame(String repositoryId, String revision, String path) throws RepositoryException, NotSupportedFeatuerException, IOException { AssertUtil.assertIsNotEmpty(repositoryId); @@ -137,7 +136,7 @@ public class BlameViewerUtil extends CacheClearHook * @throws RepositoryException */ public BlameResult getBlame(Repository repository, String revision, - String path) + String path) throws RepositoryException, NotSupportedFeatuerException, IOException { AssertUtil.assertIsNotNull(repository); @@ -177,7 +176,7 @@ public class BlameViewerUtil extends CacheClearHook * @version Enter version here... * @author Enter your name here... */ - private static class BlameViewerCacheKey + private static class BlameViewerCacheKey implements RepositoryCacheKey { /** @@ -269,6 +268,20 @@ public class BlameViewerUtil extends CacheClearHook return hash; } + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getRepositoryId() + { + return repositoryId; + } + //~--- fields ------------------------------------------------------------- /** Field description */ diff --git a/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java b/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java index 7587502c00..d4dcc8ff55 100644 --- a/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java +++ b/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java @@ -38,6 +38,7 @@ package sonia.scm.repository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.Filter; import sonia.scm.cache.Cache; //~--- JDK imports ------------------------------------------------------------ @@ -63,15 +64,40 @@ public class CacheClearHook implements RepositoryHook * Method description * * @since 1.7 + * */ public void clearCache() { - if (logger.isDebugEnabled()) - { - logger.debug("clear cache"); - } + clearCache(null); + } - cache.clear(); + /** + * Method description + * + * @since 1.9 + * + * @param filter + */ + public void clearCache(Filter filter) + { + if (filter != null) + { + if (logger.isDebugEnabled()) + { + logger.debug("clear cache, with filter"); + } + + cache.removeAll(filter); + } + else + { + if (logger.isDebugEnabled()) + { + logger.debug("clear cache"); + } + + cache.clear(); + } } /** @@ -89,7 +115,9 @@ public class CacheClearHook implements RepositoryHook event.getRepository().getName()); } - cache.clear(); + Filter filter = createFilter(event); + + clearCache(filter); } //~--- get methods ---------------------------------------------------------- @@ -120,6 +148,20 @@ public class CacheClearHook implements RepositoryHook //~--- methods -------------------------------------------------------------- + /** + * Method description + * + * @since 1.9 + * + * + * @param event + * @return + */ + protected Filter createFilter(RepositoryHookEvent event) + { + return null; + } + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java index 8f12bde687..c3e2d7ad47 100644 --- a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java +++ b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java @@ -40,6 +40,7 @@ import com.google.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.Filter; import sonia.scm.NotSupportedFeatuerException; import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; @@ -57,7 +58,7 @@ import java.util.Set; * @author Sebastian Sdorra * @since 1.6 */ -public class ChangesetViewerUtil extends CacheClearHook +public class ChangesetViewerUtil extends PartCacheClearHook { /** Field description */ @@ -91,7 +92,7 @@ public class ChangesetViewerUtil extends CacheClearHook ChangesetPagingResult.class, CACHE_NAME); init(repositoryManager, cache); } - + //~--- get methods ---------------------------------------------------------- /** @@ -247,7 +248,7 @@ public class ChangesetViewerUtil extends CacheClearHook * @version Enter version here..., 11/07/24 * @author Enter your name here... */ - private class ChangesetViewerCacheKey + private class ChangesetViewerCacheKey implements RepositoryCacheKey { /** @@ -340,6 +341,12 @@ public class ChangesetViewerUtil extends CacheClearHook /** Field description */ private int start; + + @Override + public String getRepositoryId() + { + return repository; + } } diff --git a/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java b/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java new file mode 100644 index 0000000000..2a2830501e --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java @@ -0,0 +1,61 @@ +/** + * 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.9 + */ +public class PartCacheClearHook extends CacheClearHook +{ + + /** + * Method description + * + * + * @param event + * + * @return + */ + @Override + protected Filter createFilter(RepositoryHookEvent event) + { + return new RepositoryFilter(event); + } +} diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryBrowserUtil.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryBrowserUtil.java index 523a19f041..9291647784 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryBrowserUtil.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryBrowserUtil.java @@ -59,7 +59,7 @@ import java.util.List; * @since 1.6 */ @Singleton -public class RepositoryBrowserUtil extends CacheClearHook +public class RepositoryBrowserUtil extends PartCacheClearHook { /** Field description */ @@ -205,7 +205,7 @@ public class RepositoryBrowserUtil extends CacheClearHook * @version Enter version here..., 11/08/03 * @author Enter your name here... */ - private static class RepositoryBrowserCacheKey + private static class RepositoryBrowserCacheKey implements RepositoryCacheKey { /** @@ -297,6 +297,20 @@ public class RepositoryBrowserUtil extends CacheClearHook return hash; } + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getRepositoryId() + { + return repositoryId; + } + //~--- fields ------------------------------------------------------------- /** Field description */ diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKey.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKey.java new file mode 100644 index 0000000000..115f19c6b6 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKey.java @@ -0,0 +1,51 @@ +/** + * 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; + +/** + * + * @author Sebastian Sdorra + * @since 1.9 + */ +public interface RepositoryCacheKey +{ + + /** + * Method description + * + * + * @return + */ + public String getRepositoryId(); +} diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java new file mode 100644 index 0000000000..58be4d3791 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java @@ -0,0 +1,102 @@ +/** + * 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.9 + * + */ +public class RepositoryFilter implements Filter +{ + + /** + * Constructs ... + * + * + * @param repository + */ + public RepositoryFilter(Repository repository) + { + this(repository.getId()); + } + + /** + * Constructs ... + * + * + * @param event + */ + public RepositoryFilter(RepositoryHookEvent event) + { + this(event.getRepository()); + } + + /** + * Constructs ... + * + * + * @param repositoryId + */ + public RepositoryFilter(String repositoryId) + { + this.repositoryId = repositoryId; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param key + * + * @return + */ + @Override + public boolean accept(RepositoryCacheKey key) + { + return repositoryId.equals(key.getRepositoryId()); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String repositoryId; +}