AccountState: Remove generic key/value cache

AccountState grew a per-account, generic cache of properties. This is
bad, because it makes AccountState mutable (values are added).
AccountState is part of AccountCacheImpl's cache, so it should be
immutable.

We removed all callers on googlesource.com and there are no callers in
Gerrit core or core plugins, so this commit removes this functionality.

Change-Id: Icc0cac05466d650e89626d2493d999ff089d5ea9
This commit is contained in:
Patrick Hiesel
2019-07-23 13:25:17 +02:00
parent aee4c00e54
commit e4ce4bcc05

View File

@@ -18,8 +18,6 @@ import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USE
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.FluentLogger;
@@ -28,8 +26,6 @@ import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.client.EditPreferencesInfo;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.CurrentUser.PropertyKey;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.ProjectWatches.NotifyType;
import com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey;
import com.google.gerrit.server.account.externalids.ExternalId;
@@ -145,7 +141,6 @@ public class AccountState {
private final GeneralPreferencesInfo generalPreferences;
private final DiffPreferencesInfo diffPreferences;
private final EditPreferencesInfo editPreferences;
private Cache<IdentifiedUser.PropertyKey<Object>, Object> properties;
private AccountState(
Account account,
@@ -228,61 +223,6 @@ public class AccountState {
return editPreferences;
}
/**
* Lookup a previously stored property.
*
* <p>All properties are automatically cleared when the account cache invalidates the {@code
* AccountState}. This method is thread-safe.
*
* @param key unique property key.
* @return previously stored value, or {@code null}.
*/
@Nullable
public <T> T get(PropertyKey<T> key) {
Cache<PropertyKey<Object>, Object> p = properties(false);
if (p != null) {
@SuppressWarnings("unchecked")
T value = (T) p.getIfPresent(key);
return value;
}
return null;
}
/**
* Store a property for later retrieval.
*
* <p>This method is thread-safe.
*
* @param key unique property key.
* @param value value to store; or {@code null} to clear the value.
*/
public <T> void put(PropertyKey<T> key, @Nullable T value) {
Cache<PropertyKey<Object>, Object> p = properties(value != null);
if (p != null) {
@SuppressWarnings("unchecked")
PropertyKey<Object> k = (PropertyKey<Object>) key;
if (value != null) {
p.put(k, value);
} else {
p.invalidate(k);
}
}
}
private synchronized Cache<PropertyKey<Object>, Object> properties(boolean allocate) {
if (properties == null && allocate) {
properties =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.initialCapacity(16)
// Use weakKeys to ensure plugins that garbage collect will also
// eventually release data held in any still live AccountState.
.weakKeys()
.build();
}
return properties;
}
@Override
public String toString() {
MoreObjects.ToStringHelper h = MoreObjects.toStringHelper(this);