ChangeUtil: Avoid RefDatabase#getRefs

Change-Id: I9c2ce8e233fc4e74f5ff738e0185c27fe79a4cc1
This commit is contained in:
Dave Borowitz
2019-01-04 16:37:24 -08:00
parent 1acab02049
commit a9d003ed93
4 changed files with 32 additions and 19 deletions

View File

@@ -15,8 +15,8 @@
package com.google.gerrit.server; package com.google.gerrit.server;
import static java.util.Comparator.comparingInt; import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import com.google.common.io.BaseEncoding; import com.google.common.io.BaseEncoding;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@@ -24,9 +24,11 @@ import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.io.IOException; import java.io.IOException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import org.eclipse.jgit.lib.ObjectId; import java.util.Set;
import java.util.stream.Stream;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
@@ -66,18 +68,25 @@ public class ChangeUtil {
/** /**
* Get the next patch set ID from a previously-read map of refs below the change prefix. * Get the next patch set ID from a previously-read map of refs below the change prefix.
* *
* @param changeRefs map of ref suffix to SHA-1, where the keys are ref names with the {@code * @param changeRefNames existing full change ref names with the same change ID as {@code id}.
* refs/changes/CD/ABCD/} prefix stripped. All refs should be under {@code id}'s change ref
* prefix.
* @param id previous patch set ID. * @param id previous patch set ID.
* @return next unused patch set ID for the same change, skipping any IDs whose corresponding ref * @return next unused patch set ID for the same change, skipping any IDs whose corresponding ref
* names appear in the {@code changeRefs} map. * names appear in the {@code changeRefs} map.
*/ */
public static PatchSet.Id nextPatchSetIdFromChangeRefsMap( public static PatchSet.Id nextPatchSetIdFromChangeRefs(
Map<String, ObjectId> changeRefs, PatchSet.Id id) { Collection<String> changeRefNames, PatchSet.Id id) {
int prefixLen = id.getParentKey().toRefPrefix().length(); return nextPatchSetIdFromChangeRefs(changeRefNames.stream(), id);
}
private static PatchSet.Id nextPatchSetIdFromChangeRefs(
Stream<String> changeRefNames, PatchSet.Id id) {
Set<PatchSet.Id> existing =
changeRefNames
.map(PatchSet.Id::fromRef)
.filter(psId -> psId != null && psId.getParentKey().equals(id.getParentKey()))
.collect(toSet());
PatchSet.Id next = nextPatchSetId(id); PatchSet.Id next = nextPatchSetId(id);
while (changeRefs.containsKey(next.toRefName().substring(prefixLen))) { while (existing.contains(next)) {
next = nextPatchSetId(next); next = nextPatchSetId(next);
} }
return next; return next;
@@ -88,7 +97,7 @@ public class ChangeUtil {
* *
* <p>This patch set ID may or may not be available in the database; callers that want a * <p>This patch set ID may or may not be available in the database; callers that want a
* previously-unused ID should use {@link #nextPatchSetIdFromAllRefsMap} or {@link * previously-unused ID should use {@link #nextPatchSetIdFromAllRefsMap} or {@link
* #nextPatchSetIdFromChangeRefsMap}. * #nextPatchSetIdFromChangeRefs}.
* *
* @param id previous patch set ID. * @param id previous patch set ID.
* @return next patch set ID for the same change, incrementing by 1. * @return next patch set ID for the same change, incrementing by 1.
@@ -106,9 +115,11 @@ public class ChangeUtil {
* names appear in the repository. * names appear in the repository.
*/ */
public static PatchSet.Id nextPatchSetId(Repository git, PatchSet.Id id) throws IOException { public static PatchSet.Id nextPatchSetId(Repository git, PatchSet.Id id) throws IOException {
return nextPatchSetIdFromChangeRefsMap( return nextPatchSetIdFromChangeRefs(
Maps.transformValues( git.getRefDatabase()
git.getRefDatabase().getRefs(id.getParentKey().toRefPrefix()), Ref::getObjectId), .getRefsByPrefix(id.getParentKey().toRefPrefix())
.stream()
.map(Ref::getName),
id); id);
} }

View File

@@ -174,8 +174,10 @@ public class RebaseChangeOp implements BatchUpdateOp {
baseCommitId.name()); baseCommitId.name());
rebasedPatchSetId = rebasedPatchSetId =
ChangeUtil.nextPatchSetIdFromChangeRefsMap( ChangeUtil.nextPatchSetIdFromChangeRefs(
ctx.getRepoView().getRefs(originalPatchSet.getId().getParentKey().toRefPrefix()), ctx.getRepoView()
.getRefs(originalPatchSet.getId().getParentKey().toRefPrefix())
.keySet(),
notes.getChange().currentPatchSetId()); notes.getChange().currentPatchSetId());
patchSetInserter = patchSetInserter =
patchSetInserterFactory patchSetInserterFactory

View File

@@ -97,8 +97,8 @@ public class CherryPick extends SubmitStrategy {
// tip. // tip.
args.rw.parseBody(toMerge); args.rw.parseBody(toMerge);
psId = psId =
ChangeUtil.nextPatchSetIdFromChangeRefsMap( ChangeUtil.nextPatchSetIdFromChangeRefs(
ctx.getRepoView().getRefs(getId().toRefPrefix()), ctx.getRepoView().getRefs(getId().toRefPrefix()).keySet(),
toMerge.change().currentPatchSetId()); toMerge.change().currentPatchSetId());
RevCommit mergeTip = args.mergeTip.getCurrentTip(); RevCommit mergeTip = args.mergeTip.getCurrentTip();
args.rw.parseBody(mergeTip); args.rw.parseBody(mergeTip);

View File

@@ -137,8 +137,8 @@ public class RebaseSubmitStrategy extends SubmitStrategy {
// RebaseAlways means we modify commit message. // RebaseAlways means we modify commit message.
args.rw.parseBody(toMerge); args.rw.parseBody(toMerge);
newPatchSetId = newPatchSetId =
ChangeUtil.nextPatchSetIdFromChangeRefsMap( ChangeUtil.nextPatchSetIdFromChangeRefs(
ctx.getRepoView().getRefs(getId().toRefPrefix()), ctx.getRepoView().getRefs(getId().toRefPrefix()).keySet(),
toMerge.change().currentPatchSetId()); toMerge.change().currentPatchSetId());
RevCommit mergeTip = args.mergeTip.getCurrentTip(); RevCommit mergeTip = args.mergeTip.getCurrentTip();
args.rw.parseBody(mergeTip); args.rw.parseBody(mergeTip);