Add full name to account index and query by full name in AccountResolver

Add a new field for the full name to the account index that allows
exact lookups by full name. In AccountResolver do lookups by full name
via the account index, if an account index exists.

This is a preparation for moving the account properties from ReviewDb
into git.

Change-Id: I1dc2eae98280b70354b48f6333d23b89acc8501a
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-07-13 15:35:15 +02:00
parent e615d01809
commit 38b73e2358
5 changed files with 35 additions and 8 deletions

View File

@@ -182,15 +182,14 @@ public class AccountResolver {
return Collections.singleton(id); return Collections.singleton(id);
} }
List<Account> m = schema.get().accounts().byFullName(nameOrEmail).toList();
if (m.size() == 1) {
return Collections.singleton(m.get(0).getId());
}
// 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) { if (accountIndexes.getSearchIndex() != null) {
List<AccountState> m = accountQueryProvider.get().byFullName(nameOrEmail);
if (m.size() == 1) {
return Collections.singleton(m.get(0).getAccount().getId());
}
// At this point we have no clue. Just perform a whole bunch of suggestions
// and pray we come up with a reasonable result list.
return FluentIterable return FluentIterable
.from(accountQueryProvider.get().byDefault(nameOrEmail)) .from(accountQueryProvider.get().byDefault(nameOrEmail))
.transform(new Function<AccountState, Account.Id>() { .transform(new Function<AccountState, Account.Id>() {
@@ -201,6 +200,13 @@ public class AccountResolver {
}).toSet(); }).toSet();
} }
List<Account> m = schema.get().accounts().byFullName(nameOrEmail).toList();
if (m.size() == 1) {
return Collections.singleton(m.get(0).getId());
}
// At this point we have no clue. Just perform a whole bunch of suggestions
// and pray we come up with a reasonable result list.
Set<Account.Id> result = new HashSet<>(); Set<Account.Id> result = new HashSet<>();
String a = nameOrEmail; String a = nameOrEmail;
String b = nameOrEmail + "\u9fa5"; String b = nameOrEmail + "\u9fa5";

View File

@@ -84,6 +84,15 @@ public class AccountField {
} }
}; };
public static final FieldDef<AccountState, String> FULL_NAME =
new FieldDef.Single<AccountState, String>("full_name", FieldType.EXACT,
false) {
@Override
public String get(AccountState input, FillArgs args) {
return input.getAccount().getFullName();
}
};
public static final FieldDef<AccountState, String> ACTIVE = public static final FieldDef<AccountState, String> ACTIVE =
new FieldDef.Single<AccountState, String>( new FieldDef.Single<AccountState, String>(
"inactive", FieldType.EXACT, false) { "inactive", FieldType.EXACT, false) {

View File

@@ -33,6 +33,9 @@ public class AccountSchemaDefinitions extends SchemaDefinitions<AccountState> {
static final Schema<AccountState> V2 = static final Schema<AccountState> V2 =
schema(V1, AccountField.WATCHED_PROJECT); schema(V1, AccountField.WATCHED_PROJECT);
static final Schema<AccountState> V3 =
schema(V2, AccountField.FULL_NAME);
public static final AccountSchemaDefinitions INSTANCE = public static final AccountSchemaDefinitions INSTANCE =
new AccountSchemaDefinitions(); new AccountSchemaDefinitions();

View File

@@ -66,6 +66,10 @@ public class AccountPredicates {
return new AccountPredicate(AccountField.EXTERNAL_ID, externalId); return new AccountPredicate(AccountField.EXTERNAL_ID, externalId);
} }
static Predicate<AccountState> fullName(String fullName) {
return new AccountPredicate(AccountField.FULL_NAME, fullName);
}
public static Predicate<AccountState> isActive() { public static Predicate<AccountState> isActive() {
return new AccountPredicate(AccountField.ACTIVE, "1"); return new AccountPredicate(AccountField.ACTIVE, "1");
} }

View File

@@ -67,6 +67,11 @@ public class InternalAccountQuery extends InternalQuery<AccountState> {
return query(AccountPredicates.externalId(externalId)); return query(AccountPredicates.externalId(externalId));
} }
public List<AccountState> byFullName(String fullName)
throws OrmException {
return query(AccountPredicates.fullName(fullName));
}
public List<AccountState> byWatchedProject(Project.NameKey project) public List<AccountState> byWatchedProject(Project.NameKey project)
throws OrmException { throws OrmException {
return query(AccountPredicates.watchedProject(project)); return query(AccountPredicates.watchedProject(project));