// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.googlesource.gerrit.plugins.xdocs;

import com.google.common.cache.LoadingCache;
import com.google.gerrit.httpd.resources.Resource;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import com.googlesource.gerrit.plugins.xdocs.XDocLoader;
import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters.FormatterProvider;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;

import java.io.IOException;

@Singleton
public class XDocCache {
  private final LoadingCache<String, Resource> cache;
  private final GitRepositoryManager repoManager;

  @Inject
  XDocCache(
      @Named(XDocLoader.Module.X_DOC_RESOURCES) LoadingCache<String, Resource> cache,
      GitRepositoryManager repoManager) {
    this.cache = cache;
    this.repoManager = repoManager;
  }

  public Resource get(FormatterProvider formatter, Project.NameKey project,
      String file, ObjectId revId) {
    ObjectId metaConfigRevId;
    try {
      Repository repo = repoManager.openRepository(project);
      try {
        metaConfigRevId = repo.resolve(RefNames.REFS_CONFIG);
      } finally {
        repo.close();
      }
    } catch (IOException e) {
      return null;
    }

    return cache.getUnchecked((new XDocResourceKey(formatter.getName(),
        project, file, revId, metaConfigRevId)).asString());
  }
}

@@ -825,32 +825,35 @@
 rit.
-reviewdb.client.RefNames
+server.project.ProjectCache
 ;%0Aim
@@ -886,32 +886,28 @@
 ver.
-git.GitRepositoryManager
+project.ProjectState
 ;%0Aim
@@ -1015,65 +1015,8 @@
 d;%0A%0A
-import com.googlesource.gerrit.plugins.xdocs.XDocLoader;%0A
 impo
@@ -1138,77 +1138,8 @@
 tId;
-%0Aimport org.eclipse.jgit.lib.Repository;%0A%0Aimport java.io.IOException;
 %0A%0A@S
@@ -1246,40 +1246,33 @@
 nal 
-GitRepositoryManager repoManager
+ProjectCache projectCache
 ;%0A%0A 
@@ -1389,40 +1389,33 @@
     
-GitRepositoryManager repoManager
+ProjectCache projectCache
 ) %7B%0A
@@ -1451,33 +1451,35 @@
 his.
-repoManager = repoManager
+projectCache = projectCache
 ;%0A  
@@ -1602,282 +1602,130 @@
     
-Ob
+Pro
 ject
-Id metaConfigRevId;%0A    try %7B%0A      Repository repo = repoManager.openRepository(project);%0A      try %7B%0A        metaConfigRevId = repo.resolve(RefNames.REFS_CONFIG);%0A      %7D finally %7B%0A        repo.close();%0A      %7D%0A    %7D catch (IOException e) %7B%0A      return null;%0A    %7D%0A
+State p = projectCache.get(project);%0A    ObjectId metaConfigRevId = p != null ? p.getConfig().getRevision() : null;
 %0A   

Use revision of project config instead of loading it from repo