Commits

Sebastian Sdorra committed 55121c6

cache sub repository map to improve performance

  • Participants
  • Parent commits 44319a9

Comments (0)

Files changed (1)

File scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitBrowseCommand.java

  */
 
 
+
 package sonia.scm.repository.spi;
 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.collect.Maps;
+
 import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
 import java.io.IOException;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
  * @author Sebastian Sdorra
  */
 public class GitBrowseCommand extends AbstractGitCommand
-        implements BrowseCommand
+  implements BrowseCommand
 {
 
   /** Field description */
    */
   @Override
   public BrowserResult getBrowserResult(BrowseCommandRequest request)
-          throws IOException, RepositoryException
+    throws IOException, RepositoryException
   {
     if (logger.isDebugEnabled())
     {
       }
 
       result = new BrowserResult(Constants.HEAD, null, null,
-                                 new ArrayList<FileObject>());
+        new ArrayList<FileObject>());
     }
 
     return result;
    * @throws RepositoryException
    */
   private void appendSubModules(List<FileObject> files,
-                                org.eclipse.jgit.lib.Repository repo,
-                                ObjectId revId, String path)
-          throws IOException, RepositoryException
+    org.eclipse.jgit.lib.Repository repo, ObjectId revId, String path)
+    throws IOException, RepositoryException
   {
     path = Util.nonNull(path);
 
-    Map<String, SubRepository> subRepositories = getSubRepositories(repo,
-                                                   revId);
+    Map<String, SubRepository> subRepositories = subrepositoryCache.get(revId);
+
+    if (subRepositories == null)
+    {
+      subRepositories = getSubRepositories(repo, revId);
+      subrepositoryCache.put(revId, subRepositories);
+    }
 
     if (subRepositories != null)
     {
    * @throws IOException
    */
   private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
-          ObjectId revId, TreeWalk treeWalk)
-          throws IOException
+    ObjectId revId, TreeWalk treeWalk)
+    throws IOException
   {
     FileObject file = null;
 
    * @return
    */
   private RevCommit getLatestCommit(org.eclipse.jgit.lib.Repository repo,
-                                    ObjectId revId, String path)
+    ObjectId revId, String path)
   {
     RevCommit result = null;
     RevWalk walk = null;
     {
       walk = new RevWalk(repo);
       walk.setTreeFilter(AndTreeFilter.create(PathFilter.create(path),
-              TreeFilter.ANY_DIFF));
+        TreeFilter.ANY_DIFF));
 
       RevCommit commit = walk.parseCommit(revId);
 
    * @throws RepositoryException
    */
   private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
-                                  ObjectId revId, String path)
-          throws IOException, RepositoryException
+    ObjectId revId, String path)
+    throws IOException, RepositoryException
   {
     BrowserResult result = null;
     RevWalk revWalk = null;
    * @throws IOException
    * @throws RepositoryException
    */
-  private Map<String, SubRepository> getSubRepositories(
-          org.eclipse.jgit.lib.Repository repo, ObjectId revision)
-          throws IOException, RepositoryException
+  private Map<String,
+    SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
+      ObjectId revision)
+    throws IOException, RepositoryException
   {
     if (logger.isDebugEnabled())
     {
       logger.debug("read submodules of {} at {}", repository.getName(),
-                   revision);
+        revision);
     }
 
     Map<String, SubRepository> subRepositories = null;
     try
     {
       new GitCatCommand(context, repository).getContent(repo, revision,
-                        PATH_MODULES, baos);
+        PATH_MODULES, baos);
       subRepositories = GitSubModuleParser.parse(baos.toString());
     }
     catch (PathNotFoundException ex)
     {
       logger.trace("could not find .gitmodules", ex);
+      subRepositories = Collections.EMPTY_MAP;
     }
 
     return subRepositories;
   }
+
+  //~--- fields ---------------------------------------------------------------
+
+  /** Field description */
+  private Map<ObjectId, Map<String, SubRepository>> subrepositoryCache =
+    Maps.newHashMap();
 }