Merge "Make rebuild.Event Comparable"

This commit is contained in:
David Pursehouse
2016-09-17 09:33:20 +00:00
committed by Gerrit Code Review
3 changed files with 26 additions and 21 deletions

View File

@@ -26,7 +26,6 @@ import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
@@ -364,7 +363,7 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
EventList<PatchLineCommentEvent> plcel = new EventList<>();
for (Account.Id author : draftCommentEvents.keys()) {
for (PatchLineCommentEvent e :
EVENT_ORDER.sortedCopy(draftCommentEvents.get(author))) {
Ordering.natural().sortedCopy(draftCommentEvents.get(author))) {
if (!plcel.canAdd(e)) {
flushEventsToDraftUpdate(manager, plcel, change);
checkState(plcel.canAdd(e));
@@ -399,7 +398,7 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
private void sortAndFillEvents(Change change, Change noteDbChange,
List<Event> events, Integer minPsNum) {
Collections.sort(events, EVENT_ORDER);
Collections.sort(events);
events.add(new FinalUpdatesEvent(change, noteDbChange));
// Ensure the first event in the list creates the change, setting the author
@@ -566,23 +565,6 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
}
}
private static final Ordering<Event> EVENT_ORDER = new Ordering<Event>() {
@Override
public int compare(Event a, Event b) {
return ComparisonChain.start()
.compare(a.when, b.when)
.compareTrueFirst(isPatchSet(a), isPatchSet(b))
.compareTrueFirst(a.predatesChange, b.predatesChange)
.compare(a.who, b.who, ReviewDbUtil.intKeyOrdering())
.compare(a.psId, b.psId, ReviewDbUtil.intKeyOrdering().nullsLast())
.result();
}
private boolean isPatchSet(Event e) {
return e instanceof PatchSetEvent;
}
};
static void createChange(ChangeUpdate update, Change change) {
update.setSubjectForCommit("Create change");
update.setChangeId(change.getKey().get());

View File

@@ -18,8 +18,10 @@ import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.server.notedb.rebuild.ChangeRebuilderImpl.MAX_WINDOW_MS;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ComparisonChain;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDbUtil;
import com.google.gerrit.server.notedb.AbstractChangeUpdate;
import com.google.gerrit.server.notedb.ChangeUpdate;
import com.google.gwtorm.server.OrmException;
@@ -28,7 +30,7 @@ import java.io.IOException;
import java.sql.Timestamp;
import java.util.Objects;
abstract class Event {
abstract class Event implements Comparable<Event> {
// NOTE: EventList only supports direct subclasses, not an arbitrary
// hierarchy.
@@ -68,6 +70,10 @@ abstract class Event {
abstract void apply(ChangeUpdate update) throws OrmException, IOException;
protected boolean isPatchSet() {
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
@@ -76,4 +82,16 @@ abstract class Event {
.add("when", when)
.toString();
}
@Override
public int compareTo(Event other) {
return ComparisonChain.start()
.compare(this.when, other.when)
.compareTrueFirst(isPatchSet(), isPatchSet())
.compareTrueFirst(this.predatesChange, other.predatesChange)
.compare(this.who, other.who, ReviewDbUtil.intKeyOrdering())
.compare(this.psId, other.psId,
ReviewDbUtil.intKeyOrdering().nullsLast())
.result();
}
}

View File

@@ -66,6 +66,11 @@ class PatchSetEvent extends Event {
}
}
@Override
protected boolean isPatchSet() {
return true;
}
private void setRevision(ChangeUpdate update, PatchSet ps)
throws IOException {
String rev = ps.getRevision().get();