Merge "Add option to skip diff computations when requesting change details."

This commit is contained in:
Alice Kober-Sotzek
2019-07-12 11:43:25 +00:00
committed by Gerrit Code Review
5 changed files with 89 additions and 6 deletions

View File

@@ -318,6 +318,11 @@ effect.
A change's mergeability can be requested separately by calling the A change's mergeability can be requested separately by calling the
link:#get-mergeable[get-mergeable] endpoint. link:#get-mergeable[get-mergeable] endpoint.
-- --
[[skip_diffstat]]
--
* `SKIP_DIFFSTAT`: skip the 'insertions' and 'deletions' field in link:#change-info[ChangeInfo].
For large trees, their computation may be expensive.
--
[[submittable]] [[submittable]]
-- --

View File

@@ -248,12 +248,16 @@ public interface ChangeApi {
* <ul> * <ul>
* <li>{@code CHECK} is omitted, to skip consistency checks. * <li>{@code CHECK} is omitted, to skip consistency checks.
* <li>{@code SKIP_MERGEABLE} is omitted, so the {@code mergeable} bit <em>is</em> set. * <li>{@code SKIP_MERGEABLE} is omitted, so the {@code mergeable} bit <em>is</em> set.
* <li>{@code SKIP_DIFFSTAT} is omitted to ensure diffstat calculations.
* </ul> * </ul>
*/ */
default ChangeInfo get() throws RestApiException { default ChangeInfo get() throws RestApiException {
return get( return get(
EnumSet.complementOf( EnumSet.complementOf(
EnumSet.of(ListChangesOption.CHECK, ListChangesOption.SKIP_MERGEABLE))); EnumSet.of(
ListChangesOption.CHECK,
ListChangesOption.SKIP_MERGEABLE,
ListChangesOption.SKIP_DIFFSTAT)));
} }
/** {@link #get(ListChangesOption...)} with no options included. */ /** {@link #get(ListChangesOption...)} with no options included. */

View File

@@ -75,7 +75,13 @@ public enum ListChangesOption implements ListOption {
TRACKING_IDS(21), TRACKING_IDS(21),
/** Skip mergeability data */ /** Skip mergeability data */
SKIP_MERGEABLE(22); SKIP_MERGEABLE(22),
/**
* Skip diffstat computation that compute the insertions field (number of lines inserted) and
* deletions field (number of lines deleted)
*/
SKIP_DIFFSTAT(23);
private final int value; private final int value;

View File

@@ -28,6 +28,7 @@ import static com.google.gerrit.extensions.client.ListChangesOption.LABELS;
import static com.google.gerrit.extensions.client.ListChangesOption.MESSAGES; import static com.google.gerrit.extensions.client.ListChangesOption.MESSAGES;
import static com.google.gerrit.extensions.client.ListChangesOption.REVIEWED; import static com.google.gerrit.extensions.client.ListChangesOption.REVIEWED;
import static com.google.gerrit.extensions.client.ListChangesOption.REVIEWER_UPDATES; import static com.google.gerrit.extensions.client.ListChangesOption.REVIEWER_UPDATES;
import static com.google.gerrit.extensions.client.ListChangesOption.SKIP_DIFFSTAT;
import static com.google.gerrit.extensions.client.ListChangesOption.SKIP_MERGEABLE; import static com.google.gerrit.extensions.client.ListChangesOption.SKIP_MERGEABLE;
import static com.google.gerrit.extensions.client.ListChangesOption.SUBMITTABLE; import static com.google.gerrit.extensions.client.ListChangesOption.SUBMITTABLE;
import static com.google.gerrit.extensions.client.ListChangesOption.TRACKING_IDS; import static com.google.gerrit.extensions.client.ListChangesOption.TRACKING_IDS;
@@ -516,10 +517,12 @@ public class ChangeJson {
out.submittable = submittable(cd); out.submittable = submittable(cd);
} }
} }
Optional<ChangedLines> changedLines = cd.changedLines(); if (!has(SKIP_DIFFSTAT)) {
if (changedLines.isPresent()) { Optional<ChangedLines> changedLines = cd.changedLines();
out.insertions = changedLines.get().insertions; if (changedLines.isPresent()) {
out.deletions = changedLines.get().deletions; out.insertions = changedLines.get().insertions;
out.deletions = changedLines.get().deletions;
}
} }
out.isPrivate = in.isPrivate() ? true : null; out.isPrivate = in.isPrivate() ? true : null;
out.workInProgress = in.isWorkInProgress() ? true : null; out.workInProgress = in.isWorkInProgress() ? true : null;

View File

@@ -55,12 +55,16 @@ import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS
import static com.google.gerrit.server.project.testing.TestLabels.label; import static com.google.gerrit.server.project.testing.TestLabels.label;
import static com.google.gerrit.server.project.testing.TestLabels.value; import static com.google.gerrit.server.project.testing.TestLabels.value;
import static com.google.gerrit.testing.GerritJUnit.assertThrows; import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static com.google.gerrit.truth.CacheStatsSubject.assertThat;
import static com.google.gerrit.truth.CacheStatsSubject.cloneStats;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet; import static java.util.stream.Collectors.toSet;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheStats;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@@ -156,6 +160,12 @@ import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.index.change.ChangeIndex; import com.google.gerrit.server.index.change.ChangeIndex;
import com.google.gerrit.server.index.change.ChangeIndexCollection; import com.google.gerrit.server.index.change.ChangeIndexCollection;
import com.google.gerrit.server.index.change.IndexedChangeQuery; import com.google.gerrit.server.index.change.IndexedChangeQuery;
import com.google.gerrit.server.patch.DiffSummary;
import com.google.gerrit.server.patch.DiffSummaryKey;
import com.google.gerrit.server.patch.IntraLineDiff;
import com.google.gerrit.server.patch.IntraLineDiffKey;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListKey;
import com.google.gerrit.server.project.testing.TestLabels; import com.google.gerrit.server.project.testing.TestLabels;
import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.ChangeQueryBuilder.ChangeOperatorFactory; import com.google.gerrit.server.query.change.ChangeQueryBuilder.ChangeOperatorFactory;
@@ -168,6 +178,7 @@ import com.google.gerrit.testing.FakeEmailSender.Message;
import com.google.gerrit.testing.TestTimeUtil; import com.google.gerrit.testing.TestTimeUtil;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.io.IOException; import java.io.IOException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
@@ -207,6 +218,18 @@ public class ChangeIT extends AbstractDaemonTest {
@Inject private ProjectOperations projectOperations; @Inject private ProjectOperations projectOperations;
@Inject private RequestScopeOperations requestScopeOperations; @Inject private RequestScopeOperations requestScopeOperations;
@Inject
@Named("diff")
private Cache<PatchListKey, PatchList> fileCache;
@Inject
@Named("diff_intraline")
private Cache<IntraLineDiffKey, IntraLineDiff> intraCache;
@Inject
@Named("diff_summary")
private Cache<DiffSummaryKey, DiffSummary> diffSummaryCache;
private ChangeIndexedCounter changeIndexedCounter; private ChangeIndexedCounter changeIndexedCounter;
private RegistrationHandle changeIndexedCounterHandle; private RegistrationHandle changeIndexedCounterHandle;
@@ -257,6 +280,48 @@ public class ChangeIT extends AbstractDaemonTest {
assertThat(c.owner.avatars).isNull(); assertThat(c.owner.avatars).isNull();
} }
@Test
public void diffStatShouldComputeInsertionsAndDeletions() throws Exception {
String fileName = "a_new_file.txt";
String fileContent = "First line\nSecond line\n";
PushOneCommit.Result result = createChange("Add a file", fileName, fileContent);
String triplet = project.get() + "~master~" + result.getChangeId();
ChangeInfo change = gApi.changes().id(triplet).get();
assertThat(change.insertions).isNotNull();
assertThat(change.deletions).isNotNull();
}
@Test
public void diffStatShouldSkipInsertionsAndDeletions() throws Exception {
String fileName = "a_new_file.txt";
String fileContent = "First line\nSecond line\n";
PushOneCommit.Result result = createChange("Add a file", fileName, fileContent);
String triplet = project.get() + "~master~" + result.getChangeId();
ChangeInfo change =
gApi.changes().id(triplet).get(ImmutableList.of(ListChangesOption.SKIP_DIFFSTAT));
assertThat(change.insertions).isNull();
assertThat(change.deletions).isNull();
}
@Test
public void skipDiffstatOptionAvoidsAllDiffComputations() throws Exception {
String fileName = "a_new_file.txt";
String fileContent = "First line\nSecond line\n";
PushOneCommit.Result result = createChange("Add a file", fileName, fileContent);
String triplet = project.get() + "~master~" + result.getChangeId();
CacheStats startPatch = cloneStats(fileCache.stats());
CacheStats startIntra = cloneStats(intraCache.stats());
CacheStats startSummary = cloneStats(diffSummaryCache.stats());
gApi.changes().id(triplet).get(ImmutableList.of(ListChangesOption.SKIP_DIFFSTAT));
assertThat(fileCache.stats()).since(startPatch).hasMissCount(0);
assertThat(fileCache.stats()).since(startPatch).hasHitCount(0);
assertThat(intraCache.stats()).since(startIntra).hasMissCount(0);
assertThat(intraCache.stats()).since(startIntra).hasHitCount(0);
assertThat(diffSummaryCache.stats()).since(startSummary).hasMissCount(0);
assertThat(diffSummaryCache.stats()).since(startSummary).hasHitCount(0);
}
@Test @Test
public void skipMergeable() throws Exception { public void skipMergeable() throws Exception {
PushOneCommit.Result r = createChange(); PushOneCommit.Result r = createChange();