Use a map factory when resolving permissions for cached data

This commit is contained in:
Luck 2020-08-30 09:14:33 +01:00
parent cc1d8984da
commit 5e48224c78
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 20 additions and 12 deletions

View File

@ -51,6 +51,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.IntFunction;
/** /**
* Abstract implementation of {@link CachedDataManager}. * Abstract implementation of {@link CachedDataManager}.
@ -132,11 +133,13 @@ public abstract class AbstractCachedDataManager implements CachedDataManager {
/** /**
* Resolves the owners permissions data for the given {@link QueryOptions}. * Resolves the owners permissions data for the given {@link QueryOptions}.
* *
* @param accumulator the accumulator to add resolved permissions to * @param mapFactory a function to create a map instance to return the results in
* @param queryOptions the query options * @param queryOptions the query options
* @param <M> the map type
* @return the resolved permissions
*/ */
protected abstract void resolvePermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions); protected abstract <M extends Map<String, Boolean>> M resolvePermissions(IntFunction<M> mapFactory, QueryOptions queryOptions);
/** /**
* Resolves the owners meta data for the given {@link QueryOptions}. * Resolves the owners meta data for the given {@link QueryOptions}.
@ -150,9 +153,7 @@ public abstract class AbstractCachedDataManager implements CachedDataManager {
Objects.requireNonNull(queryOptions, "queryOptions"); Objects.requireNonNull(queryOptions, "queryOptions");
CacheMetadata metadata = getMetadataForQueryOptions(queryOptions); CacheMetadata metadata = getMetadataForQueryOptions(queryOptions);
ConcurrentHashMap<String, Boolean> sourcePermissions = new ConcurrentHashMap<>(); ConcurrentHashMap<String, Boolean> sourcePermissions = resolvePermissions(ConcurrentHashMap::new, queryOptions);
resolvePermissions(sourcePermissions, queryOptions);
return new PermissionCache(queryOptions, metadata, getCalculatorFactory(), sourcePermissions); return new PermissionCache(queryOptions, metadata, getCalculatorFactory(), sourcePermissions);
} }

View File

@ -35,6 +35,7 @@ import net.luckperms.api.node.ChatMetaType;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;
import java.util.Map; import java.util.Map;
import java.util.function.IntFunction;
/** /**
* Holds an easily accessible cache of a holders data in a number of contexts * Holds an easily accessible cache of a holders data in a number of contexts
@ -74,8 +75,8 @@ public abstract class HolderCachedDataManager<T extends PermissionHolder> extend
} }
@Override @Override
protected void resolvePermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions) { protected <M extends Map<String, Boolean>> M resolvePermissions(IntFunction<M> mapFactory, QueryOptions queryOptions) {
this.holder.exportPermissions(accumulator, queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)); return this.holder.exportPermissions(mapFactory, queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND));
} }
@Override @Override

View File

@ -67,6 +67,7 @@ import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -357,9 +358,11 @@ public abstract class PermissionHolder {
return (List) inheritanceTree; return (List) inheritanceTree;
} }
public void exportPermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) { public <M extends Map<String, Boolean>> M exportPermissions(IntFunction<M> mapFactory, QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) {
List<Node> entries = resolveInheritedNodes(queryOptions); List<Node> entries = resolveInheritedNodes(queryOptions);
processExportedPermissions(accumulator, entries, convertToLowercase, resolveShorthand); M map = mapFactory.apply(entries.size());
processExportedPermissions(map, entries, convertToLowercase, resolveShorthand);
return map;
} }
private static void processExportedPermissions(Map<String, Boolean> accumulator, List<Node> entries, boolean convertToLowercase, boolean resolveShorthand) { private static void processExportedPermissions(Map<String, Boolean> accumulator, List<Node> entries, boolean convertToLowercase, boolean resolveShorthand) {

View File

@ -47,6 +47,7 @@ import net.luckperms.api.node.ChatMetaType;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;
import java.util.Map; import java.util.Map;
import java.util.function.IntFunction;
public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManager implements CalculatorFactory { public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManager implements CalculatorFactory {
private static final MetaStackDefinition DEFAULT_META_STACK = new SimpleMetaStackDefinition( private static final MetaStackDefinition DEFAULT_META_STACK = new SimpleMetaStackDefinition(
@ -83,8 +84,10 @@ public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManage
} }
@Override @Override
protected void resolvePermissions(Map<String, Boolean> accumulator, QueryOptions queryOptions) { protected <M extends Map<String, Boolean>> M resolvePermissions(IntFunction<M> mapFactory, QueryOptions queryOptions) {
this.subject.resolveAllPermissions(accumulator, queryOptions); M map = mapFactory.apply(16);
this.subject.resolveAllPermissions(map, queryOptions);
return map;
} }
@Override @Override