Get assignee from ChangeIndex

Change-Id: Ib36dedb430900590cbdc8f444903ebfbd6b7437c
This commit is contained in:
Sven Selberg
2016-09-20 14:37:43 +02:00
parent c2d215858b
commit 72f61c4936
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.CLOSED_STATUSES;
import static com.google.gerrit.server.index.change.ChangeIndexRewriter.OPEN_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.base.Throwables;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.FluentIterable; 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 ADDED_FIELD = ChangeField.ADDED.getName();
private static final String APPROVAL_FIELD = ChangeField.APPROVAL.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 CHANGE_FIELD = ChangeField.CHANGE.getName();
private static final String DELETED_FIELD = ChangeField.DELETED.getName(); private static final String DELETED_FIELD = ChangeField.DELETED.getName();
private static final String MERGEABLE_FIELD = ChangeField.MERGEABLE.getName(); private static final String MERGEABLE_FIELD = ChangeField.MERGEABLE.getName();
@@ -477,6 +479,9 @@ public class LuceneChangeIndex implements ChangeIndex {
if (fields.contains(REVIEWEDBY_FIELD)) { if (fields.contains(REVIEWEDBY_FIELD)) {
decodeReviewedBy(doc, cd); decodeReviewedBy(doc, cd);
} }
if(fields.contains(ASSIGNEE_FIELD)) {
decodeAssignee(doc, cd);
}
if (fields.contains(HASHTAG_FIELD)) { if (fields.contains(HASHTAG_FIELD)) {
decodeHashtags(doc, cd); 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) { private void decodeHashtags(Multimap<String, IndexableField> doc, ChangeData cd) {
Collection<IndexableField> hashtag = doc.get(HASHTAG_FIELD); Collection<IndexableField> hashtag = doc.get(HASHTAG_FIELD);
Set<String> hashtags = Sets.newHashSetWithExpectedSize(hashtag.size()); Set<String> hashtags = Sets.newHashSetWithExpectedSize(hashtag.size());

View File

@@ -323,8 +323,8 @@ public class ChangeField {
@Override @Override
public Integer get(ChangeData input, FillArgs args) public Integer get(ChangeData input, FillArgs args)
throws OrmException { throws OrmException {
Account.Id id = input.assignee(); Optional<Account.Id> id = input.assignee();
return id != null ? id.get() : NO_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.project.ProjectCache;
import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.InternalChangeQuery; import com.google.gerrit.server.query.change.InternalChangeQuery;
import com.google.common.base.Optional;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; 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. * @return an Account.Id of the user assigned to this change.
*/ */
public Account.Id getAssignee() { public Optional<Account.Id> getAssignee() {
return state.assignee(); 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 static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Optional;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gerrit.common.Nullable; import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.LabelType; import com.google.gerrit.common.data.LabelType;
@@ -354,10 +355,10 @@ public class ChangeControl {
/** Is this user assigned to this change? */ /** Is this user assigned to this change? */
public boolean isAssignee() { public boolean isAssignee() {
Account.Id currentAssignee = notes.getAssignee(); Optional<Account.Id> currentAssignee = notes.getAssignee();
if (currentAssignee != null && getUser().isIdentifiedUser()) { if (currentAssignee.isPresent() && getUser().isIdentifiedUser()) {
Account.Id id = getUser().getAccountId(); Account.Id id = getUser().getAccountId();
return id.equals(currentAssignee); return id.equals(currentAssignee.get());
} }
return false; return false;
} }

View File

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

View File

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