Merge "Get assignee from ChangeIndex"

This commit is contained in:
David Pursehouse
2016-09-22 12:39:10 +00:00
committed by Gerrit Code Review
6 changed files with 35 additions and 12 deletions

View File

@@ -24,6 +24,7 @@ import static com.google.gerrit.server.index.change.ChangeField.PROJECT;
import static com.google.gerrit.server.index.change.ChangeIndexRewriter.CLOSED_STATUSES;
import static com.google.gerrit.server.index.change.ChangeIndexRewriter.OPEN_STATUSES;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.FluentIterable;
@@ -118,6 +119,7 @@ public class LuceneChangeIndex implements ChangeIndex {
private static final String ADDED_FIELD = ChangeField.ADDED.getName();
private static final String APPROVAL_FIELD = ChangeField.APPROVAL.getName();
private static final String ASSIGNEE_FIELD = ChangeField.ASSIGNEE.getName();
private static final String CHANGE_FIELD = ChangeField.CHANGE.getName();
private static final String DELETED_FIELD = ChangeField.DELETED.getName();
private static final String MERGEABLE_FIELD = ChangeField.MERGEABLE.getName();
@@ -477,6 +479,9 @@ public class LuceneChangeIndex implements ChangeIndex {
if (fields.contains(REVIEWEDBY_FIELD)) {
decodeReviewedBy(doc, cd);
}
if(fields.contains(ASSIGNEE_FIELD)) {
decodeAssignee(doc, cd);
}
if (fields.contains(HASHTAG_FIELD)) {
decodeHashtags(doc, cd);
}
@@ -551,6 +556,18 @@ public class LuceneChangeIndex implements ChangeIndex {
}
}
private void decodeAssignee(Multimap<String, IndexableField> doc, ChangeData cd) {
IndexableField af = Iterables.getFirst(doc.get(ASSIGNEE_FIELD), null);
Account.Id assignee = null;
if (af != null) {
int id = af.numericValue().intValue();
if (id > 0) {
assignee = new Account.Id(id);
}
}
cd.setAssignee(Optional.fromNullable(assignee));
}
private void decodeHashtags(Multimap<String, IndexableField> doc, ChangeData cd) {
Collection<IndexableField> hashtag = doc.get(HASHTAG_FIELD);
Set<String> hashtags = Sets.newHashSetWithExpectedSize(hashtag.size());

View File

@@ -323,8 +323,8 @@ public class ChangeField {
@Override
public Integer get(ChangeData input, FillArgs args)
throws OrmException {
Account.Id id = input.assignee();
return id != null ? id.get() : NO_ASSIGNEE;
Optional<Account.Id> id = input.assignee();
return id.isPresent() ? id.get().get() : NO_ASSIGNEE;
}
};

View File

@@ -56,6 +56,7 @@ import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.InternalChangeQuery;
import com.google.common.base.Optional;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -389,8 +390,8 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
/**
* @return an Account.Id of the user assigned to this change.
*/
public Account.Id getAssignee() {
return state.assignee();
public Optional<Account.Id> getAssignee() {
return Optional.fromNullable(state.assignee());
}
/**

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.project;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.LabelType;
@@ -354,10 +355,10 @@ public class ChangeControl {
/** Is this user assigned to this change? */
public boolean isAssignee() {
Account.Id currentAssignee = notes.getAssignee();
if (currentAssignee != null && getUser().isIdentifiedUser()) {
Optional<Account.Id> currentAssignee = notes.getAssignee();
if (currentAssignee.isPresent() && getUser().isIdentifiedUser()) {
Account.Id id = getUser().getAccountId();
return id.equals(currentAssignee);
return id.equals(currentAssignee.get());
}
return false;
}

View File

@@ -345,7 +345,7 @@ public class ChangeData {
private Optional<ChangedLines> changedLines;
private SubmitTypeRecord submitTypeRecord;
private Boolean mergeable;
private Account.Id assignee;
private Optional<Account.Id> assignee;
private Set<String> hashtags;
private Set<Account.Id> editsByUser;
private Set<Account.Id> reviewedBy;
@@ -1163,16 +1163,20 @@ public class ChangeData {
this.reviewedBy = reviewedBy;
}
public Account.Id assignee() throws OrmException {
public Optional<Account.Id> assignee() throws OrmException {
if (assignee == null) {
if (!lazyLoad) {
return null;
return Optional.absent();
}
assignee = notes().getAssignee();
}
return assignee;
}
public void setAssignee(Optional<Account.Id> assignee) {
this.assignee = assignee;
}
public Set<String> hashtags() throws OrmException {
if (hashtags == null) {
if (!lazyLoad) {

View File

@@ -591,14 +591,14 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
update.commit();
ChangeNotes notes = newNotes(c);
assertThat(notes.getAssignee()).isEqualTo(otherUserId);
assertThat(notes.getAssignee().get()).isEqualTo(otherUserId);
update = newUpdate(c, changeOwner);
update.setAssignee(changeOwner.getAccountId());
update.commit();
notes = newNotes(c);
assertThat(notes.getAssignee()).isEqualTo(changeOwner.getAccountId());
assertThat(notes.getAssignee().get()).isEqualTo(changeOwner.getAccountId());
}