AccountCache#get: Allow fetching any collection of IDs rather than a Set
The order of the provided IDs is reserved in the result map.
Release-Notes: skip
Google-Bug: b/414541591
Change-Id: I5f9cc56f4634c6bfb72b5fdc600aecec1b9ff2dc
Forward-Compatible: checked
diff --git a/java/com/google/gerrit/server/account/AccountCache.java b/java/com/google/gerrit/server/account/AccountCache.java
index 5ae7345..addddcd 100644
--- a/java/com/google/gerrit/server/account/AccountCache.java
+++ b/java/com/google/gerrit/server/account/AccountCache.java
@@ -14,12 +14,12 @@
package com.google.gerrit.server.account;
+import com.google.common.collect.ImmutableMap;
import com.google.gerrit.common.UsedAt;
import com.google.gerrit.common.UsedAt.Project;
import com.google.gerrit.entities.Account;
-import java.util.Map;
+import java.util.Collection;
import java.util.Optional;
-import java.util.Set;
import org.eclipse.jgit.lib.ObjectId;
/** Caches important (but small) account state to avoid database hits. */
@@ -35,18 +35,20 @@
Optional<AccountState> get(Account.Id accountId);
/**
- * Returns a {@code Map} of {@code Account.Id} to {@code AccountState} for the given account IDs.
- * If not cached yet the accounts are loaded. If an account can't be loaded (e.g. because it is
- * missing), the entry will be missing from the result.
+ * Returns a {@code ImmutableMap} of {@code Account.Id} to {@code AccountState} for the given
+ * account IDs. If not cached yet the accounts are loaded. If an account can't be loaded (e.g.
+ * because it is missing), the entry will be missing from the result. The order of the provided
+ * account IDs is preserved in the result.
*
* <p>Loads accounts in parallel if applicable.
*
* @param accountIds IDs of the account that should be retrieved
* @return {@code Map} of {@code Account.Id} to {@code AccountState} instances for the given
* account IDs, if an account can't be loaded (e.g. because it is missing), the entry will be
- * missing from the result
+ * missing from the result. The order of the provided * account IDs is preserved in the
+ * result.
*/
- Map<Account.Id, AccountState> get(Set<Account.Id> accountIds);
+ ImmutableMap<Account.Id, AccountState> get(Collection<Account.Id> accountIds);
/**
* Returns an {@code AccountState} instance for the given account ID. If not cached yet the
diff --git a/java/com/google/gerrit/server/account/AccountCacheImpl.java b/java/com/google/gerrit/server/account/AccountCacheImpl.java
index 87adb22..48dd5e2 100644
--- a/java/com/google/gerrit/server/account/AccountCacheImpl.java
+++ b/java/com/google/gerrit/server/account/AccountCacheImpl.java
@@ -44,6 +44,7 @@
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import java.io.IOException;
+import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
@@ -146,7 +147,7 @@
}
@Override
- public Map<Account.Id, AccountState> get(Set<Account.Id> accountIds) {
+ public ImmutableMap<Account.Id, AccountState> get(Collection<Account.Id> accountIds) {
try (TraceTimer ignored =
TraceContext.newTimer(
"Loading accounts", Metadata.builder().resourceCount(accountIds.size()).build())) {
diff --git a/java/com/google/gerrit/server/account/InternalAccountDirectory.java b/java/com/google/gerrit/server/account/InternalAccountDirectory.java
index c6ffce5..2c6bb6c 100644
--- a/java/com/google/gerrit/server/account/InternalAccountDirectory.java
+++ b/java/com/google/gerrit/server/account/InternalAccountDirectory.java
@@ -20,6 +20,7 @@
import static java.util.stream.Stream.concat;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.gerrit.entities.Account;
import com.google.gerrit.extensions.common.AccountInfo;
@@ -44,7 +45,6 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
-import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
@@ -108,7 +108,7 @@
Set<FillOptions> fillOptionsWithoutSecondaryEmails =
Sets.difference(options, EnumSet.of(FillOptions.SECONDARY_EMAILS));
Set<Account.Id> ids = stream(in).map(a -> Account.id(a._accountId)).collect(toSet());
- Map<Account.Id, AccountState> accountStates = accountCache.get(ids);
+ ImmutableMap<Account.Id, AccountState> accountStates = accountCache.get(ids);
for (AccountInfo info : in) {
Account.Id id = Account.id(info._accountId);
AccountState state = accountStates.get(id);
@@ -131,7 +131,7 @@
@Override
public void fillAccountAttributeInfo(Iterable<? extends AccountAttribute> in) {
Set<Account.Id> ids = stream(in).map(a -> Account.id(a.accountId)).collect(toSet());
- Map<Account.Id, AccountState> accountStates = accountCache.get(ids);
+ ImmutableMap<Account.Id, AccountState> accountStates = accountCache.get(ids);
for (AccountAttribute accountAttribute : in) {
Account.Id id = Account.id(accountAttribute.accountId);
AccountState accountState = accountStates.get(id);
diff --git a/java/com/google/gerrit/server/notedb/CommitRewriter.java b/java/com/google/gerrit/server/notedb/CommitRewriter.java
index ead2e15..895ae9d 100644
--- a/java/com/google/gerrit/server/notedb/CommitRewriter.java
+++ b/java/com/google/gerrit/server/notedb/CommitRewriter.java
@@ -1178,7 +1178,7 @@
return Optional.of(AccountTemplateUtil.getAccountTemplate(account.get()));
}
// Retrieve reviewer accounts from cache and try to match by their name.
- Map<Account.Id, AccountState> missingAccountStateReviewers =
+ ImmutableMap<Account.Id, AccountState> missingAccountStateReviewers =
accountCache.get(
changeFixProgress.parsedAccounts.entrySet().stream()
.filter(entry -> !entry.getValue().isPresent())
diff --git a/java/com/google/gerrit/server/restapi/change/ReviewerRecommender.java b/java/com/google/gerrit/server/restapi/change/ReviewerRecommender.java
index 2adb9fc..9d4a463 100644
--- a/java/com/google/gerrit/server/restapi/change/ReviewerRecommender.java
+++ b/java/com/google/gerrit/server/restapi/change/ReviewerRecommender.java
@@ -19,6 +19,7 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.Nullable;
@@ -240,7 +241,7 @@
private ImmutableList<Account.Id> getMatchingReviewers(
ImmutableList<ChangeData> changes, String query) {
ImmutableList<Account.Id> reviewerIds = getReviewers(changes);
- Map<Account.Id, AccountState> reviewerStates =
+ ImmutableMap<Account.Id, AccountState> reviewerStates =
accountCache.get(ImmutableSet.copyOf(reviewerIds));
return reviewerIds.stream()
.filter(reviewerId -> accountMatchesQuery(reviewerStates.get(reviewerId), query))
diff --git a/java/com/google/gerrit/testing/FakeAccountCache.java b/java/com/google/gerrit/testing/FakeAccountCache.java
index 330b602..92ca7e6 100644
--- a/java/com/google/gerrit/testing/FakeAccountCache.java
+++ b/java/com/google/gerrit/testing/FakeAccountCache.java
@@ -20,10 +20,10 @@
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.util.time.TimeUtil;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
-import java.util.Set;
import org.eclipse.jgit.lib.ObjectId;
/** Fake implementation of {@link AccountCache} for testing. */
@@ -53,7 +53,8 @@
}
@Override
- public synchronized Map<Account.Id, AccountState> get(Set<Account.Id> accountIds) {
+ public synchronized ImmutableMap<Account.Id, AccountState> get(
+ Collection<Account.Id> accountIds) {
return ImmutableMap.copyOf(Maps.filterKeys(byId, accountIds::contains));
}