AccountResolver: Query account index for getting suggestions

If available query the account index to make suggestions, instead of
accessing the database. This is a preparation for moving external IDs
and account properties from ReviewDb into git.

Change-Id: I5d253147bc6f763040cebd789800272ead12bde7
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-07-13 14:38:29 +02:00
parent 82b70db262
commit e615d01809
4 changed files with 52 additions and 16 deletions

View File

@@ -14,9 +14,13 @@
package com.google.gerrit.server.account;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountExternalId;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.index.account.AccountIndexCollection;
import com.google.gerrit.server.query.account.InternalAccountQuery;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -34,14 +38,22 @@ public class AccountResolver {
private final Realm realm;
private final AccountByEmailCache byEmail;
private final AccountCache byId;
private final AccountIndexCollection accountIndexes;
private final Provider<InternalAccountQuery> accountQueryProvider;
private final Provider<ReviewDb> schema;
@Inject
AccountResolver(final Realm realm, final AccountByEmailCache byEmail,
final AccountCache byId, final Provider<ReviewDb> schema) {
AccountResolver(Realm realm,
AccountByEmailCache byEmail,
AccountCache byId,
AccountIndexCollection accountIndexes,
Provider<InternalAccountQuery> accountQueryProvider,
Provider<ReviewDb> schema) {
this.realm = realm;
this.byEmail = byEmail;
this.byId = byId;
this.accountIndexes = accountIndexes;
this.accountQueryProvider = accountQueryProvider;
this.schema = schema;
}
@@ -178,6 +190,17 @@ public class AccountResolver {
// At this point we have no clue. Just perform a whole bunch of suggestions
// and pray we come up with a reasonable result list.
//
if (accountIndexes.getSearchIndex() != null) {
return FluentIterable
.from(accountQueryProvider.get().byDefault(nameOrEmail))
.transform(new Function<AccountState, Account.Id>() {
@Override
public Account.Id apply(AccountState accountState) {
return accountState.getAccount().getId();
}
}).toSet();
}
Set<Account.Id> result = new HashSet<>();
String a = nameOrEmail;
String b = nameOrEmail + "\u9fa5";

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.query.account;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.account.AccountState;
@@ -23,12 +25,28 @@ import com.google.gerrit.server.index.account.AccountField;
import com.google.gerrit.server.query.Predicate;
import com.google.gerrit.server.query.QueryBuilder;
import java.util.List;
public class AccountPredicates {
public static boolean hasActive(Predicate<AccountState> p) {
return QueryBuilder.find(p, AccountPredicate.class,
AccountField.ACTIVE.getName()) != null;
}
static Predicate<AccountState> defaultPredicate(String query) {
// Adapt the capacity of this list when adding more default predicates.
List<Predicate<AccountState>> preds = Lists.newArrayListWithCapacity(3);
Integer id = Ints.tryParse(query);
if (id != null) {
preds.add(id(new Account.Id(id)));
}
preds.add(equalsName(query));
preds.add(username(query));
// Adapt the capacity of the "predicates" list when adding more default
// predicates.
return Predicate.or(preds);
}
static Predicate<AccountState> id(Account.Id accountId) {
return new AccountPredicate(AccountField.ID,
AccountQueryBuilder.FIELD_ACCOUNT, accountId.toString());

View File

@@ -31,8 +31,6 @@ import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import java.util.List;
/**
* Parses a query string meant to be applied to account objects.
*/
@@ -137,24 +135,16 @@ public class AccountQueryBuilder extends QueryBuilder<AccountState> {
@Override
protected Predicate<AccountState> defaultField(String query) {
// Adapt the capacity of this list when adding more default predicates.
List<Predicate<AccountState>> preds = Lists.newArrayListWithCapacity(4);
Predicate<AccountState> defaultPredicate =
AccountPredicates.defaultPredicate(query);
if ("self".equalsIgnoreCase(query)) {
try {
preds.add(AccountPredicates.id(self()));
return Predicate.or(defaultPredicate, AccountPredicates.id(self()));
} catch (QueryParseException e) {
// Skip.
}
}
Integer id = Ints.tryParse(query);
if (id != null) {
preds.add(AccountPredicates.id(new Account.Id(id)));
}
preds.add(name(query));
preds.add(username(query));
// Adapt the capacity of the "predicates" list when adding more default
// predicates.
return Predicate.or(preds);
return defaultPredicate;
}
private Account.Id self() throws QueryParseException {

View File

@@ -57,6 +57,11 @@ public class InternalAccountQuery extends InternalQuery<AccountState> {
return this;
}
public List<AccountState> byDefault(String query)
throws OrmException {
return query(AccountPredicates.defaultPredicate(query));
}
public List<AccountState> byExternalId(String externalId)
throws OrmException {
return query(AccountPredicates.externalId(externalId));