Cache refs/... resolutions

When `Repository.resolve` is called it recognizes if the string is a ref
reference and returns its ObjectId. Cache all requests to `refs/...` so
that reading from repository is avoided. Note that ref tip's parent
references are not cached.
Note that `PrologCompiler.call` calls `Repository.resolve` on
`refs/meta/config` and that should be improved.

Add integration test that compares the `resolve` method output with the
JGit's one for the following patterns:
* HEAD
* SHA-1: a complete or abbreviated SHA-1
* refs/...: a complete reference name
* refs/.../^: first parent of the complete reference name's tip commit
* short-name: a short reference name under refs/heads, refs/tags, or
  refs/remotes namespace
* tag-NN-gABBREV: output from describe, parsed by treating ABBREV as an
  abbreviated > SHA-1.
* id^: first parent of commit id, this is the same as id^1
* id^{commit}: ensure id is a commit

Bug: Issue 15556
Change-Id: I9a722d55b105a27ceda7e0ecbec1ce886ff896f5
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepository.java b/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepository.java
index 91e9bb6..c47e34f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepository.java
@@ -14,6 +14,7 @@
 
 package com.googlesource.gerrit.plugins.cachedrefdb;
 
+import com.google.gerrit.entities.RefNames;
 import com.google.gerrit.server.git.DelegateRepository;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
@@ -143,6 +144,13 @@
   public ObjectId resolve(String revstr)
       throws AmbiguousObjectException, IncorrectObjectTypeException, RevisionSyntaxException,
           IOException {
+    if (isCacheableReference(revstr)) {
+      Ref ref = refDb.exactRef(revstr);
+      if (ref != null) {
+        return ref.getLeaf().getObjectId();
+      }
+    }
+
     return delegate.resolve(revstr);
   }
 
@@ -355,4 +363,8 @@
   public String getProjectName() {
     return projectName;
   }
+
+  private boolean isCacheableReference(String ref) {
+    return ref.startsWith(RefNames.REFS) && !(ref.contains("^") || ref.contains("~"));
+  }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepositoryIT.java b/src/test/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepositoryIT.java
new file mode 100644
index 0000000..5f2c2e4
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/cachedrefdb/CachedRefRepositoryIT.java
@@ -0,0 +1,169 @@
+// Copyright (C) 2022 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.cachedrefdb;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.gerrit.entities.RefNames;
+import com.google.gerrit.extensions.registration.DynamicItem;
+import java.io.IOException;
+import java.util.Optional;
+import java.util.concurrent.Callable;
+import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
+import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
+import org.eclipse.jgit.junit.TestRepository;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.RefDatabase;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CachedRefRepositoryIT {
+  TestRepository<Repository> tr;
+  CachedRefRepository objectUnderTest;
+  TestRefByNameCacheImpl cache;
+
+  @Before
+  public void setUp() throws IOException {
+    Repository repo = new InMemoryRepository(new DfsRepositoryDescription("repo"));
+    objectUnderTest = createCachedRepository(repo);
+    tr = new TestRepository<>(repo);
+  }
+
+  @After
+  public void tearDown() {
+    // both CachedRefRepository and TestRepository call close on the underlying repo hence single
+    // close is sufficient
+    objectUnderTest.close();
+  }
+
+  @Test
+  public void shouldResolveFullRefsFromCache() throws Exception {
+    String master = RefNames.fullName("master");
+    RevCommit first = tr.update(master, tr.commit().add("first", "foo").create());
+    String tag = "test_tag";
+    String fullTag = RefNames.REFS_TAGS + tag;
+    tr.update(fullTag, tr.tag(tag, first));
+    tr.update(master, tr.commit().parent(first).add("second", "foo").create());
+
+    assertThat(cache.cacheCalled).isEqualTo(0);
+    assertThat(objectUnderTest.resolve(master)).isEqualTo(repo().resolve(master));
+    assertThat(objectUnderTest.resolve(fullTag)).isEqualTo(repo().resolve(fullTag));
+    assertThat(cache.cacheCalled).isEqualTo(2);
+  }
+
+  @Test
+  public void shouldNotResolveRefsFromCache() throws Exception {
+    String master = RefNames.fullName("master");
+    RevCommit first = tr.update(master, tr.commit().add("first", "foo").create());
+    String tag = "test_tag";
+    String fullTag = RefNames.REFS_TAGS + tag;
+    tr.update(fullTag, tr.tag(tag, first));
+    tr.update(master, tr.commit().parent(first).add("second", "foo").create());
+
+    assertThat(objectUnderTest.resolve("master")).isEqualTo(repo().resolve("master"));
+    assertThat(objectUnderTest.resolve(RefNames.HEAD)).isEqualTo(repo().resolve(RefNames.HEAD));
+    assertThat(objectUnderTest.resolve(tag)).isEqualTo(repo().resolve(tag));
+
+    String mastersParent = master + "^";
+    ObjectId resolved = objectUnderTest.resolve(mastersParent);
+    assertThat(resolved).isEqualTo(first);
+    assertThat(resolved).isEqualTo(repo().resolve(mastersParent));
+
+    String mastersParentByTilde = master + "~";
+    ObjectId resolvedByTilde = objectUnderTest.resolve(mastersParentByTilde);
+    assertThat(resolvedByTilde).isEqualTo(first);
+    assertThat(resolvedByTilde).isEqualTo(repo().resolve(mastersParent));
+
+    assertThat(cache.cacheCalled).isEqualTo(0);
+  }
+
+  @Test
+  public void shouldNotResolveSha1sFromCache() throws Exception {
+    String master = RefNames.fullName("master");
+    RevCommit first = tr.update(master, tr.commit().add("first", "foo").create());
+    String tag = "test_tag";
+    String fullTag = RefNames.REFS_TAGS + tag;
+    tr.update(fullTag, tr.tag(tag, first));
+    RevCommit second = tr.update(master, tr.commit().parent(first).add("second", "foo").create());
+
+    String secondAbbreviatedName = second.getName().substring(0, 6);
+    assertThat(objectUnderTest.resolve(second.name())).isEqualTo(repo().resolve(second.name()));
+    assertThat(objectUnderTest.resolve(secondAbbreviatedName))
+        .isEqualTo(repo().resolve(secondAbbreviatedName));
+    String parentRevString = second.name() + "^";
+    String resolvedName = objectUnderTest.resolve(parentRevString).getName();
+    assertThat(resolvedName).isEqualTo(first.getName());
+    assertThat(resolvedName).isEqualTo(repo().resolve(parentRevString).getName());
+    String ensureIdIsCommit = secondAbbreviatedName + "(commit)";
+    assertThat(objectUnderTest.resolve(ensureIdIsCommit))
+        .isEqualTo(repo().resolve(ensureIdIsCommit));
+
+    assertThat(cache.cacheCalled).isEqualTo(0);
+  }
+
+  @Test
+  public void shouldNotResolveTagAndSha1FromCache() throws Exception {
+    String master = RefNames.fullName("master");
+    RevCommit first = tr.update(master, tr.commit().add("first", "foo").create());
+    String tag = "test_tag";
+    tr.update(RefNames.REFS_TAGS + tag, tr.tag(tag, first));
+    RevCommit second = tr.update(master, tr.commit().parent(first).add("second", "foo").create());
+
+    String tagAndSha = tag + "-1-g" + second.getName().substring(0, 6);
+    assertThat(objectUnderTest.resolve(tagAndSha)).isEqualTo(repo().resolve(tagAndSha));
+
+    assertThat(cache.cacheCalled).isEqualTo(0);
+  }
+
+  private Repository repo() {
+    return tr.getRepository();
+  }
+
+  private CachedRefRepository createCachedRepository(Repository repo) {
+    cache = new TestRefByNameCacheImpl(CacheBuilder.newBuilder().build());
+    RefByNameCacheWrapper wrapper =
+        new RefByNameCacheWrapper(DynamicItem.itemOf(RefByNameCache.class, cache));
+    CachedRefDatabase.Factory refDbFactory =
+        new CachedRefDatabase.Factory() {
+          @Override
+          public CachedRefDatabase create(CachedRefRepository repo, RefDatabase delegate) {
+            return new CachedRefDatabase(wrapper, null, null, null, repo, delegate);
+          }
+        };
+    return new CachedRefRepository(refDbFactory, null, null, "repo", repo);
+  }
+
+  private static class TestRefByNameCacheImpl extends RefByNameCacheImpl {
+    private int cacheCalled;
+
+    private TestRefByNameCacheImpl(Cache<String, Optional<Ref>> refByName) {
+      super(refByName);
+      cacheCalled = 0;
+    }
+
+    @Override
+    public Ref computeIfAbsent(
+        String identifier, String ref, Callable<? extends Optional<Ref>> loader) {
+      cacheCalled++;
+      return super.computeIfAbsent(identifier, ref, loader);
+    }
+  }
+}