Store reviewer field in the index

Prior to this change, draft changes in search results may have
resulted in touching the database, since ChangeControl#isDraftVisible
checks if the user is a reviewer, and there are not enough stored
fields to compute the reviewer set from the index. (We store approvals
on the current patch set, not all approvals; even the latter would be
insufficient with NoteDb.)

Add a new reviewer field that stores enough information to reconstruct
the whole ReviewerSet, and also to search by reviewer state. Shove
this all in one repeated field with a little comma-separated format,
similar to what we already do for labels. The current
ReviewerPredicate implementation matches the old behavior of returning
results independent of state, although it's now expressed as a
predicate tree.

Change-Id: Ie54b9e2decf847185ec741bc50bae7eb680787a0
This commit is contained in:
Dave Borowitz
2016-06-02 17:50:00 -04:00
parent 3b8ff49c6e
commit 4315f01324
12 changed files with 282 additions and 28 deletions

View File

@@ -0,0 +1,73 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.index.change;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.ReviewerSet;
import com.google.gerrit.server.notedb.ReviewerStateInternal;
import com.google.gerrit.testutil.GerritBaseTests;
import com.google.gerrit.testutil.TestTimeUtil;
import com.google.gwtorm.client.KeyUtil;
import com.google.gwtorm.server.StandardKeyEncoder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ChangeFieldTest extends GerritBaseTests {
static {
KeyUtil.setEncoderImpl(new StandardKeyEncoder());
}
@Before
public void setUp() {
TestTimeUtil.resetWithClockStep(1, TimeUnit.SECONDS);
}
@After
public void tearDown() {
TestTimeUtil.useSystemTime();
}
@Test
public void reviewerFieldValues() {
Table<ReviewerStateInternal, Account.Id, Timestamp> t =
HashBasedTable.create();
Timestamp t1 = TimeUtil.nowTs();
t.put(ReviewerStateInternal.REVIEWER, new Account.Id(1), t1);
Timestamp t2 = TimeUtil.nowTs();
t.put(ReviewerStateInternal.CC, new Account.Id(2), t2);
ReviewerSet reviewers = ReviewerSet.fromTable(t);
List<String> values = ChangeField.getReviewerFieldValues(reviewers);
assertThat(values).containsExactly(
"REVIEWER,1",
"REVIEWER,1," + t1.getTime(),
"CC,2",
"CC,2," + t2.getTime());
assertThat(ChangeField.parseReviewerFieldValues(values))
.isEqualTo(reviewers);
}
}

View File

@@ -1397,6 +1397,26 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
assertThat(actual.get(1).reviewed).isTrue();
}
@Test
public void reviewer() throws Exception {
TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(repo, newChange(repo));
Change change2 = insert(repo, newChange(repo));
insert(repo, newChange(repo));
gApi.changes()
.id(change1.getId().get())
.current()
.review(ReviewInput.approve());
gApi.changes()
.id(change2.getId().get())
.current()
.review(ReviewInput.approve());
Account.Id id = user.getAccountId();
assertQuery("reviewer:" + id, change2, change1);
}
@Test
public void byCommitsOnBranchNotMerged() throws Exception {
TestRepository<Repo> repo = createProject("repo");
@@ -1451,6 +1471,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
cd.currentApprovals();
cd.changedLines();
cd.reviewedBy();
cd.reviewers();
// TODO(dborowitz): Swap out GitRepositoryManager somehow? Will probably be
// necessary for NoteDb anyway.