Replace AccountAccess#firstNById(int) by looking at user refs

Remove AccountAccess#firstNById(int) and find the first n accounts by
looking at the user refs in All-Users instead. Since change I81491a253
it is ensured that we have a user branch for each account.

This is a preparation step for migrating the accounts from ReviewDb to
NoteDb.

Change-Id: I0d8a74d6d2de253b4311b8e91517568c1a09f2a8
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-04-13 09:23:24 +02:00
parent 5a48534cb0
commit 986802ea3f
3 changed files with 38 additions and 11 deletions

View File

@@ -25,16 +25,17 @@ import com.google.gerrit.httpd.WebSession;
import com.google.gerrit.httpd.template.SiteHeaderFooter;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountException;
import com.google.gerrit.server.account.AccountManager;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.Accounts;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.account.AuthResult;
import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.query.account.InternalAccountQuery;
import com.google.gwtexpui.server.CacheHeaders;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -54,8 +55,10 @@ import org.w3c.dom.Element;
@SuppressWarnings("serial")
@Singleton
class BecomeAnyAccountLoginServlet extends HttpServlet {
private final SchemaFactory<ReviewDb> schema;
private final DynamicItem<WebSession> webSession;
private final SchemaFactory<ReviewDb> schema;
private final Accounts accounts;
private final AccountCache accountCache;
private final AccountManager accountManager;
private final SiteHeaderFooter headers;
private final InternalAccountQuery accountQuery;
@@ -64,11 +67,15 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
BecomeAnyAccountLoginServlet(
DynamicItem<WebSession> ws,
SchemaFactory<ReviewDb> sf,
Accounts a,
AccountCache ac,
AccountManager am,
SiteHeaderFooter shf,
InternalAccountQuery aq) {
webSession = ws;
schema = sf;
accounts = a;
accountCache = ac;
accountManager = am;
headers = shf;
accountQuery = aq;
@@ -149,8 +156,8 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
Element userlistElement = HtmlDomUtil.find(doc, "userlist");
try (ReviewDb db = schema.open()) {
ResultSet<Account> accounts = db.accounts().firstNById(100);
for (Account a : accounts) {
for (Account.Id accountId : accounts.firstNIds(100)) {
Account a = accountCache.get(accountId).getAccount();
String displayName;
if (a.getUserName() != null) {
displayName = a.getUserName();
@@ -159,7 +166,7 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
} else if (a.getPreferredEmail() != null) {
displayName = a.getPreferredEmail();
} else {
displayName = a.getId().toString();
displayName = accountId.toString();
}
Element linkElement = doc.createElement("a");

View File

@@ -41,9 +41,6 @@ public interface AccountAccess extends Access<Account, Account.Id> {
ResultSet<Account> suggestByPreferredEmail(String nameA, String nameB, int limit)
throws OrmException;
@Query("ORDER BY accountId LIMIT ?")
ResultSet<Account> firstNById(int n) throws OrmException;
@Query("ORDER BY accountId")
ResultSet<Account> all() throws OrmException;
}

View File

@@ -14,6 +14,9 @@
package com.google.gerrit.server.account;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.config.AllUsersName;
@@ -21,7 +24,9 @@ import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import org.eclipse.jgit.lib.Repository;
/** Class to access accounts. */
@@ -36,6 +41,16 @@ public class Accounts {
this.allUsersName = allUsersName;
}
/**
* Returns the first n account IDs.
*
* @param n the number of account IDs that should be returned
* @return first n account IDs
*/
public List<Account.Id> firstNIds(int n) throws IOException {
return readUserRefs().sorted(comparing(id -> id.get())).limit(n).collect(toList());
}
/**
* Checks if any account exists.
*
@@ -48,13 +63,21 @@ public class Accounts {
}
public static boolean hasAnyAccount(Repository repo) throws IOException {
return readUserRefs(repo).findAny().isPresent();
}
private Stream<Account.Id> readUserRefs() throws IOException {
try (Repository repo = repoManager.openRepository(allUsersName)) {
return readUserRefs(repo);
}
}
private static Stream<Account.Id> readUserRefs(Repository repo) throws IOException {
return repo.getRefDatabase()
.getRefs(RefNames.REFS_USERS)
.values()
.stream()
.map(r -> Account.Id.fromRef(r.getName()))
.filter(Objects::nonNull)
.findAny()
.isPresent();
.filter(Objects::nonNull);
}
}