Don't re-use PermissionCache instances

This commit is contained in:
Luck committed 2020-07-03 15:18:58 +01:00
1 parent 426c8565fd
commit 8b506b6a95
8 files changed
+54 -102

No files matched your search

@@ -46,12 +46,12 @@ import net.luckperms.api.node.ChatMetaType;
import net.luckperms.api.query.QueryOptions;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
@@ -135,10 +135,10 @@ public abstract class AbstractCachedDataManager implements CachedDataManager {
/**
* Resolves the owners permissions data for the given {@link QueryOptions}.
*
* @param accumulator the accumulator to add resolved permissions to
* @param queryOptions the query options
* @return a map of permissions to back the {@link PermissionCache}
*/
protected abstract Map<String, Boolean> resolvePermissions(QueryOptions queryOptions);
protected abstract void resolvePermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions);
/**
* Resolves the owners meta data for the given {@link QueryOptions}.
@@ -148,31 +148,24 @@ public abstract class AbstractCachedDataManager implements CachedDataManager {
*/
protected abstract void resolveMeta(MetaAccumulator accumulator, QueryOptions queryOptions);
private PermissionCache calculatePermissions(QueryOptions queryOptions, PermissionCache data) {
private PermissionCache calculatePermissions(QueryOptions queryOptions) {
Objects.requireNonNull(queryOptions, "queryOptions");
CacheMetadata metadata = getMetadataForQueryOptions(queryOptions);
if (data == null) {
CacheMetadata metadata = getMetadataForQueryOptions(queryOptions);
data = new PermissionCache(queryOptions, metadata, getCalculatorFactory());
}
ConcurrentHashMap<String, Boolean> sourcePermissions = new ConcurrentHashMap<>();
resolvePermissions(sourcePermissions, queryOptions);
data.setPermissions(resolvePermissions(queryOptions));
return data;
return new PermissionCache(queryOptions, metadata, getCalculatorFactory(), sourcePermissions);
}
private MetaCache calculateMeta(QueryOptions queryOptions, MetaCache data) {
private MetaCache calculateMeta(QueryOptions queryOptions) {
Objects.requireNonNull(queryOptions, "queryOptions");
if (data == null) {
CacheMetadata metadata = getMetadataForQueryOptions(queryOptions);
data = new MetaCache(this.plugin, queryOptions, metadata);
}
CacheMetadata metadata = getMetadataForQueryOptions(queryOptions);
MetaAccumulator accumulator = newAccumulator(queryOptions);
resolveMeta(accumulator, queryOptions);
data.loadMeta(accumulator);
return data;
return new MetaCache(this.plugin, queryOptions, metadata, accumulator);
}
@Override
@@ -236,25 +229,15 @@ public abstract class AbstractCachedDataManager implements CachedDataManager {
public @NonNull CompletableFuture<? extends C> reload(@NonNull QueryOptions queryOptions) {
Objects.requireNonNull(queryOptions, "queryOptions");
// get the previous value - we can reuse the same instance
C previous = this.cache.getIfPresent(queryOptions);
// invalidate the previous value until we're done recalculating
this.cache.invalidate(queryOptions);
clearRecent();
// request recalculation from the cache
if (previous != null) {
return CompletableFuture.supplyAsync(
() -> this.cache.get(queryOptions, c -> this.cacheLoader.reload(c, previous)),
CaffeineFactory.executor()
);
} else {
return CompletableFuture.supplyAsync(
() -> this.cache.get(queryOptions),
CaffeineFactory.executor()
);
}
return CompletableFuture.supplyAsync(
() -> this.cache.get(queryOptions),
CaffeineFactory.executor()
);
}
@Override
@@ -284,17 +267,8 @@ public abstract class AbstractCachedDataManager implements CachedDataManager {
}
private interface Loader<K, V> extends CacheLoader<K, V> {
@NonNull V load(@NonNull K key, @Nullable V oldValue);
@Override
default @NonNull V load(@NonNull K key) {
return load(key, null);
}
@Override
default @NonNull V reload(@NonNull K key, @NonNull V oldValue) {
return load(key, oldValue);
}
@NonNull V load(@NonNull K key);
}
private MetaStackDefinition getMetaStackDefinition(QueryOptions queryOptions, ChatMetaType type) {
@@ -74,8 +74,8 @@ public abstract class HolderCachedDataManager<T extends PermissionHolder> extend
}
@Override
protected Map<String, Boolean> resolvePermissions(QueryOptions queryOptions) {
return this.holder.exportPermissions(queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND));
protected void resolvePermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions) {
this.holder.exportPermissions(accumulator, queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND));
}
@Override
@@ -56,8 +56,8 @@ public class MetaCache extends SimpleMetaCache implements CachedMetaData {
/** The object name passed to the verbose handler when checks are made */
private final String verboseCheckTarget;
public MetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, CacheMetadata metadata) {
super(plugin, queryOptions);
public MetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, CacheMetadata metadata, MetaAccumulator sourceMeta) {
super(plugin, queryOptions, sourceMeta);
this.plugin = plugin;
this.metadata = metadata;
@@ -68,13 +68,13 @@ public class PermissionCache implements CachedPermissionData {
*/
private final PermissionCalculator calculator;
public PermissionCache(QueryOptions queryOptions, CacheMetadata metadata, CalculatorFactory calculatorFactory) {
public PermissionCache(QueryOptions queryOptions, CacheMetadata metadata, CalculatorFactory calculatorFactory, ConcurrentHashMap<String, Boolean> sourcePermissions) {
this.queryOptions = queryOptions;
this.permissions = new ConcurrentHashMap<>();
this.permissions = sourcePermissions;
this.permissionsUnmodifiable = Collections.unmodifiableMap(this.permissions);
this.calculator = calculatorFactory.build(queryOptions, metadata);
this.calculator.setSourcePermissions(this.permissions); // Initial setup.
this.calculator.setSourcePermissions(this.permissions);
}
@Override
@@ -82,19 +82,6 @@ public class PermissionCache implements CachedPermissionData {
this.calculator.invalidateCache();
}
private void setPermissionsInternal(Map<String, Boolean> permissions) {
this.permissions.clear();
this.permissions.putAll(permissions);
this.calculator.setSourcePermissions(this.permissions);
invalidateCache();
}
public void setPermissions(Map<String, Boolean> toApply) {
if (!this.permissions.equals(toApply)) {
setPermissionsInternal(toApply);
}
}
public PermissionCalculator getCalculator() {
return this.calculator;
}
@@ -58,24 +58,22 @@ public class SimpleMetaCache implements CachedMetaData {
private final QueryOptions queryOptions;
/* The data */
protected Map<String, List<String>> meta = ImmutableMap.of();
protected Map<String, String> flattenedMeta = ImmutableMap.of();
protected SortedMap<Integer, String> prefixes = ImmutableSortedMap.of();
protected SortedMap<Integer, String> suffixes = ImmutableSortedMap.of();
protected int weight = 0;
protected String primaryGroup = null;
private MetaStackDefinition prefixDefinition = null;
private MetaStackDefinition suffixDefinition = null;
private String prefix = null;
private String suffix = null;
protected final Map<String, List<String>> meta;
protected final Map<String, String> flattenedMeta;
protected final SortedMap<Integer, String> prefixes;
protected final SortedMap<Integer, String> suffixes;
protected final int weight;
protected final String primaryGroup;
private final MetaStackDefinition prefixDefinition;
private final MetaStackDefinition suffixDefinition;
private final String prefix;
private final String suffix;
public SimpleMetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions) {
public SimpleMetaCache(LuckPermsPlugin plugin, QueryOptions queryOptions, MetaAccumulator sourceMeta) {
this.plugin = plugin;
this.queryOptions = queryOptions;
}
public void loadMeta(MetaAccumulator meta) {
this.meta = Multimaps.asMap(ImmutableListMultimap.copyOf(meta.getMeta()));
this.meta = Multimaps.asMap(ImmutableListMultimap.copyOf(sourceMeta.getMeta()));
MetaValueSelector metaValueSelector = this.queryOptions.option(MetaValueSelector.KEY)
.orElseGet(() -> this.plugin.getConfiguration().get(ConfigKeys.META_VALUE_SELECTOR));
@@ -95,14 +93,14 @@ public class SimpleMetaCache implements CachedMetaData {
}
this.flattenedMeta = builder.build();
this.prefixes = ImmutableSortedMap.copyOfSorted(meta.getPrefixes());
this.suffixes = ImmutableSortedMap.copyOfSorted(meta.getSuffixes());
this.weight = meta.getWeight();
this.primaryGroup = meta.getPrimaryGroup();
this.prefixDefinition = meta.getPrefixDefinition();
this.suffixDefinition = meta.getSuffixDefinition();
this.prefix = meta.getPrefix();
this.suffix = meta.getSuffix();
this.prefixes = ImmutableSortedMap.copyOfSorted(sourceMeta.getPrefixes());
this.suffixes = ImmutableSortedMap.copyOfSorted(sourceMeta.getSuffixes());
this.weight = sourceMeta.getWeight();
this.primaryGroup = sourceMeta.getPrimaryGroup();
this.prefixDefinition = sourceMeta.getPrefixDefinition();
this.suffixDefinition = sourceMeta.getSuffixDefinition();
this.prefix = sourceMeta.getPrefix();
this.suffix = sourceMeta.getSuffix();
}
public String getMetaValue(String key, MetaCheckEvent.Origin origin) {
@@ -58,7 +58,6 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -358,18 +357,17 @@ public abstract class PermissionHolder {
return (List) inheritanceTree;
}
public Map<String, Boolean> exportPermissions(QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) {
public void exportPermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) {
List<Node> entries = resolveInheritedNodes(queryOptions);
return processExportedPermissions(entries, convertToLowercase, resolveShorthand);
processExportedPermissions(accumulator, entries, convertToLowercase, resolveShorthand);
}
private static Map<String, Boolean> processExportedPermissions(List<Node> entries, boolean convertToLowercase, boolean resolveShorthand) {
Map<String, Boolean> map = new HashMap<>(entries.size());
private static void processExportedPermissions(Map<String, Boolean> accumulator, List<Node> entries, boolean convertToLowercase, boolean resolveShorthand) {
for (Node node : entries) {
if (convertToLowercase) {
map.putIfAbsent(node.getKey().toLowerCase(), node.getValue());
accumulator.putIfAbsent(node.getKey().toLowerCase(), node.getValue());
} else {
map.putIfAbsent(node.getKey(), node.getValue());
accumulator.putIfAbsent(node.getKey(), node.getValue());
}
}
@@ -378,15 +376,13 @@ public abstract class PermissionHolder {
Collection<String> shorthand = node.resolveShorthand();
for (String s : shorthand) {
if (convertToLowercase) {
map.putIfAbsent(s.toLowerCase(), node.getValue());
accumulator.putIfAbsent(s.toLowerCase(), node.getValue());
} else {
map.putIfAbsent(s, node.getValue());
accumulator.putIfAbsent(s, node.getValue());
}
}
}
}
return map;
}
public MetaAccumulator accumulateMeta(QueryOptions queryOptions) {
@@ -89,18 +89,15 @@ public abstract class CalculatedSubject implements LPSubject {
return permissions;
}
public Map<String, Boolean> resolveAllPermissions(QueryOptions filter) {
public void resolveAllPermissions(Map<String, Boolean> accumulator, QueryOptions filter) {
SubjectInheritanceGraph graph = new SubjectInheritanceGraph(filter);
Map<String, Boolean> result = new HashMap<>();
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (Map.Entry<String, Boolean> entry : subject.getCombinedPermissions(filter).entrySet()) {
result.putIfAbsent(entry.getKey(), entry.getValue());
accumulator.putIfAbsent(entry.getKey(), entry.getValue());
}
}
return result;
}
public Set<LPSubjectReference> getCombinedParents(QueryOptions filter) {
@@ -83,8 +83,8 @@ public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManage
}
@Override
protected Map<String, Boolean> resolvePermissions(QueryOptions queryOptions) {
return this.subject.resolveAllPermissions(queryOptions);
protected void resolvePermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions) {
this.subject.resolveAllPermissions(accumulator, queryOptions);
}
@Override