QueryAccounts: Support parameter 'start'

The 'start' query parameter can be supplied to skip a number of
accounts from the list.

Change-Id: I31497d0db3ac6049a645fe2d29984797bd6cfcae
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-06-30 14:21:19 +02:00
parent afa0be9b96
commit 6115eae5d0
6 changed files with 44 additions and 1 deletions

View File

@@ -49,6 +49,9 @@ returned.
] ]
---- ----
The `S` or `start` query parameter can be supplied to skip a number
of accounts from the list.
[[suggest-account]] [[suggest-account]]
To get account suggestions set the parameter `suggest` and provide the To get account suggestions set the parameter `suggest` and provide the
typed substring as query `q`. If a result limit `n` is not specified, typed substring as query `q`. If a result limit `n` is not specified,

View File

@@ -140,6 +140,7 @@ public interface Accounts {
abstract class QueryRequest { abstract class QueryRequest {
private String query; private String query;
private int limit; private int limit;
private int start;
/** /**
* Executes query and returns a list of accounts. * Executes query and returns a list of accounts.
@@ -165,6 +166,15 @@ public interface Accounts {
return this; return this;
} }
/**
* Set number of accounts to skip.
* Optional; no accounts are skipped when not provided.
*/
public QueryRequest withStart(int start) {
this.start = start;
return this;
}
public String getQuery() { public String getQuery() {
return query; return query;
} }
@@ -172,6 +182,10 @@ public interface Accounts {
public int getLimit() { public int getLimit() {
return limit; return limit;
} }
public int getStart() {
return start;
}
} }
/** /**

View File

@@ -63,6 +63,7 @@ public class QueryAccounts implements RestReadView<TopLevelResource> {
private boolean suggest; private boolean suggest;
private int suggestLimit = 10; private int suggestLimit = 10;
private String query; private String query;
private Integer start;
@Option(name = "--suggest", metaVar = "SUGGEST", usage = "suggest users") @Option(name = "--suggest", metaVar = "SUGGEST", usage = "suggest users")
public void setSuggest(boolean suggest) { public void setSuggest(boolean suggest) {
@@ -87,6 +88,12 @@ public class QueryAccounts implements RestReadView<TopLevelResource> {
this.query = query; this.query = query;
} }
@Option(name = "--start", aliases = {"-S"}, metaVar = "CNT",
usage = "Number of accounts to skip")
public void setStart(int start) {
this.start = start;
}
@Inject @Inject
QueryAccounts(AccountControl.Factory accountControlFactory, QueryAccounts(AccountControl.Factory accountControlFactory,
AccountLoader.Factory accountLoaderFactory, AccountLoader.Factory accountLoaderFactory,
@@ -139,6 +146,9 @@ public class QueryAccounts implements RestReadView<TopLevelResource> {
if (!suggest) { if (!suggest) {
throw new MethodNotAllowedException(); throw new MethodNotAllowedException();
} }
if (start != null) {
throw new MethodNotAllowedException("option start not allowed");
}
matches = queryFromDb(); matches = queryFromDb();
} }
@@ -151,6 +161,10 @@ public class QueryAccounts implements RestReadView<TopLevelResource> {
throw new MethodNotAllowedException("query disabled"); throw new MethodNotAllowedException("query disabled");
} }
if (start != null) {
queryProcessor.setStart(start);
}
Map<Account.Id, AccountInfo> matches = new LinkedHashMap<>(); Map<Account.Id, AccountInfo> matches = new LinkedHashMap<>();
try { try {
Predicate<AccountState> queryPred; Predicate<AccountState> queryPred;

View File

@@ -123,6 +123,7 @@ public class AccountsImpl implements Accounts {
QueryAccounts myQueryAccounts = queryAccountsProvider.get(); QueryAccounts myQueryAccounts = queryAccountsProvider.get();
myQueryAccounts.setQuery(r.getQuery()); myQueryAccounts.setQuery(r.getQuery());
myQueryAccounts.setLimit(r.getLimit()); myQueryAccounts.setLimit(r.getLimit());
myQueryAccounts.setStart(r.getStart());
return myQueryAccounts.apply(TopLevelResource.INSTANCE); return myQueryAccounts.apply(TopLevelResource.INSTANCE);
} catch (OrmException e) { } catch (OrmException e) {
throw new RestApiException("Cannot retrieve suggested accounts", e); throw new RestApiException("Cannot retrieve suggested accounts", e);

View File

@@ -57,6 +57,6 @@ public class AccountQueryProcessor extends QueryProcessor<AccountState> {
protected Predicate<AccountState> enforceVisibility( protected Predicate<AccountState> enforceVisibility(
Predicate<AccountState> pred) { Predicate<AccountState> pred) {
return new AndSource<>(pred, return new AndSource<>(pred,
new AccountIsVisibleToPredicate(accountControlFactory.get())); new AccountIsVisibleToPredicate(accountControlFactory.get()), start);
} }
} }

View File

@@ -256,6 +256,17 @@ public abstract class AbstractQueryAccountsTest extends GerritServerTests {
assertQuery(newQuery(domain).withLimit(2), user1, user2); assertQuery(newQuery(domain).withLimit(2), user1, user2);
} }
@Test
public void withStart() throws Exception {
String domain = name("test.com");
AccountInfo user1 = newAccountWithEmail("user1", "user1@" + domain);
AccountInfo user2 = newAccountWithEmail("user2", "user2@" + domain);
AccountInfo user3 = newAccountWithEmail("user3", "user3@" + domain);
assertQuery(domain, user1, user2, user3);
assertQuery(newQuery(domain).withStart(1), user2, user3);
}
protected AccountInfo newAccount(String username) throws Exception { protected AccountInfo newAccount(String username) throws Exception {
return newAccountWithEmail(username, null); return newAccountWithEmail(username, null);
} }