Use project name in calls to cache

Project name is unique in a Gerrit instance therefore it can be used as
a ref qualifier instead of identity. It is way shorter therefore it will
result in a small memory optimization.

Change-Id: I80a464822e9399af01efbc48373c6579cdcb4578
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/BatchRefUpdateWithCacheUpdate.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/BatchRefUpdateWithCacheUpdate.java
index 360dfa1..a3ebb47 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/BatchRefUpdateWithCacheUpdate.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/BatchRefUpdateWithCacheUpdate.java
@@ -22,7 +22,6 @@
 import org.eclipse.jgit.lib.BatchRefUpdate;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.ProgressMonitor;
-import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.transport.PushCertificate;
 import org.eclipse.jgit.transport.ReceiveCommand;
@@ -30,17 +29,17 @@
 
 class BatchRefUpdateWithCacheUpdate extends BatchRefUpdate {
   interface Factory {
-    BatchRefUpdateWithCacheUpdate create(Repository repo, BatchRefUpdate delegate);
+    BatchRefUpdateWithCacheUpdate create(CachedRefRepository repo, BatchRefUpdate delegate);
   }
 
-  private final Repository repo;
+  private final CachedRefRepository repo;
   private final RefByNameCacheWrapper refsCache;
   private final BatchRefUpdate delegate;
 
   @Inject
   BatchRefUpdateWithCacheUpdate(
       RefByNameCacheWrapper refsCache,
-      @Assisted Repository repo,
+      @Assisted CachedRefRepository repo,
       @Assisted BatchRefUpdate delegate) {
     super(repo.getRefDatabase());
     this.refsCache = refsCache;
@@ -177,7 +176,7 @@
         .forEach(
             cmd -> {
               if (cmd.getResult() == ReceiveCommand.Result.OK) {
-                refsCache.evict(repo.getIdentifier(), cmd.getRefName());
+                refsCache.evict(repo.getProjectName(), cmd.getRefName());
               }
             });
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefDatabase.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefDatabase.java
index 79a619c..fdbca56 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefDatabase.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefDatabase.java
@@ -88,7 +88,7 @@
   @Override
   public Ref exactRef(String name) throws IOException {
     return refsCache.computeIfAbsent(
-        repo.getIdentifier(), name, () -> Optional.ofNullable(delegate.exactRef(name)));
+        repo.getProjectName(), name, () -> Optional.ofNullable(delegate.exactRef(name)));
   }
 
   @Deprecated
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefRepository.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefRepository.java
index e7960e3..86cf9b9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/CachedRefRepository.java
@@ -48,9 +48,10 @@
 
 class CachedRefRepository extends DelegateRepository {
   interface Factory {
-    CachedRefRepository create(Repository repo);
+    CachedRefRepository create(String projectName, Repository repo);
   }
 
+  private final String projectName;
   private final CachedRefDatabase refDb;
   private final RefUpdateWithCacheUpdate.Factory updateFactory;
   private final RefRenameWithCacheUpdate.Factory renameFactory;
@@ -60,8 +61,10 @@
       CachedRefDatabase.Factory refDbFactory,
       RefUpdateWithCacheUpdate.Factory updateFactory,
       RefRenameWithCacheUpdate.Factory renameFactory,
+      @Assisted String projectName,
       @Assisted Repository repo) {
     super(repo);
+    this.projectName = projectName;
     this.updateFactory = updateFactory;
     this.renameFactory = renameFactory;
     this.refDb = refDbFactory.create(this);
@@ -347,4 +350,8 @@
   public void create() throws IOException {
     getDelegate().create();
   }
+
+  public String getProjectName() {
+    return projectName;
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/GerritCachedGitRepositoryManager.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/GerritCachedGitRepositoryManager.java
index 4eb0cf1..0697306 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/GerritCachedGitRepositoryManager.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/GerritCachedGitRepositoryManager.java
@@ -37,12 +37,12 @@
 
   @Override
   public Repository openRepository(Project.NameKey name) throws IOException {
-    return repoWrapperFactory.create(repoManager.openRepository(name));
+    return repoWrapperFactory.create(name.get(), repoManager.openRepository(name));
   }
 
   @Override
   public Repository createRepository(Project.NameKey name) throws IOException {
-    return repoWrapperFactory.create(repoManager.createRepository(name));
+    return repoWrapperFactory.create(name.get(), repoManager.createRepository(name));
   }
 
   @Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefRenameWithCacheUpdate.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefRenameWithCacheUpdate.java
index 68de4ca..7bb9a46 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefRenameWithCacheUpdate.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefRenameWithCacheUpdate.java
@@ -22,12 +22,11 @@
 import org.eclipse.jgit.lib.RefRename;
 import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.RefUpdate.Result;
-import org.eclipse.jgit.lib.Repository;
 
 class RefRenameWithCacheUpdate extends RefRename {
   interface Factory {
     RefRenameWithCacheUpdate create(
-        Repository repo,
+        CachedRefRepository repo,
         RefRename delegate,
         @Assisted("src") RefUpdate src,
         @Assisted("dst") RefUpdate dst);
@@ -38,14 +37,14 @@
       EnumSet.of(Result.NEW, Result.FORCED, Result.FAST_FORWARD, Result.RENAMED);
 
   private final RefByNameCacheWrapper refsCache;
-  private final Repository repo;
+  private final CachedRefRepository repo;
   private final RefRename delegate;
   private final RefUpdate src;
 
   @Inject
   RefRenameWithCacheUpdate(
       RefByNameCacheWrapper refsCache,
-      @Assisted Repository repo,
+      @Assisted CachedRefRepository repo,
       @Assisted RefRename delegate,
       @Assisted("src") RefUpdate src,
       @Assisted("dst") RefUpdate dst) {
@@ -95,7 +94,7 @@
   public Result rename() throws IOException {
     Result r = delegate.rename();
     if (SUCCESSFUL_RENAMES.contains(r)) {
-      refsCache.evict(repo.getIdentifier(), src.getName());
+      refsCache.evict(repo.getProjectName(), src.getName());
     }
     return r;
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefUpdateWithCacheUpdate.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefUpdateWithCacheUpdate.java
index a4bee39..a8be44d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefUpdateWithCacheUpdate.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefUpdateWithCacheUpdate.java
@@ -16,7 +16,8 @@
 
 class RefUpdateWithCacheUpdate extends RefUpdate {
   interface Factory {
-    RefUpdateWithCacheUpdate create(RefDatabase refDb, Repository repo, RefUpdate delegate);
+    RefUpdateWithCacheUpdate create(
+        RefDatabase refDb, CachedRefRepository repo, RefUpdate delegate);
   }
 
   private static final String NOT_SUPPORTED_MSG = "Should never be called";
@@ -25,14 +26,14 @@
 
   private final RefByNameCacheWrapper refsCache;
   private final RefDatabase refDb;
-  private final Repository repo;
+  private final CachedRefRepository repo;
   private final RefUpdate delegate;
 
   @Inject
   RefUpdateWithCacheUpdate(
       RefByNameCacheWrapper refsCache,
       @Assisted RefDatabase refDb,
-      @Assisted Repository repo,
+      @Assisted CachedRefRepository repo,
       @Assisted RefUpdate delegate) {
     super(delegate.getRef());
     this.refsCache = refsCache;
@@ -208,7 +209,7 @@
 
   private Result evictCache(Result r) {
     if (SUCCESSFUL_UPDATES.contains(r)) {
-      refsCache.evict(repo.getIdentifier(), getName());
+      refsCache.evict(repo.getProjectName(), getName());
     }
     return r;
   }