From 5e48224c78401f3ac25fa555ba63140f5a56e958 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 30 Aug 2020 09:14:33 +0100 Subject: [PATCH] Use a map factory when resolving permissions for cached data --- .../cacheddata/AbstractCachedDataManager.java | 13 +++++++------ .../common/cacheddata/HolderCachedDataManager.java | 5 +++-- .../luckperms/common/model/PermissionHolder.java | 7 +++++-- .../CalculatedSubjectCachedDataManager.java | 7 +++++-- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java index 683874944..ccf2a0c4a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/AbstractCachedDataManager.java @@ -51,6 +51,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.function.Function; +import java.util.function.IntFunction; /** * 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}. - * - * @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 the map type + * @return the resolved permissions */ - protected abstract void resolvePermissions(Map accumulator, QueryOptions queryOptions); + protected abstract > M resolvePermissions(IntFunction mapFactory, QueryOptions 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"); CacheMetadata metadata = getMetadataForQueryOptions(queryOptions); - ConcurrentHashMap sourcePermissions = new ConcurrentHashMap<>(); - resolvePermissions(sourcePermissions, queryOptions); - + ConcurrentHashMap sourcePermissions = resolvePermissions(ConcurrentHashMap::new, queryOptions); return new PermissionCache(queryOptions, metadata, getCalculatorFactory(), sourcePermissions); } diff --git a/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java b/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java index 5f0b3562c..6938e7651 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/cacheddata/HolderCachedDataManager.java @@ -35,6 +35,7 @@ import net.luckperms.api.node.ChatMetaType; import net.luckperms.api.query.QueryOptions; import java.util.Map; +import java.util.function.IntFunction; /** * Holds an easily accessible cache of a holders data in a number of contexts @@ -74,8 +75,8 @@ public abstract class HolderCachedDataManager extend } @Override - protected void resolvePermissions(Map accumulator, QueryOptions queryOptions) { - this.holder.exportPermissions(accumulator, queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)); + protected > M resolvePermissions(IntFunction mapFactory, QueryOptions queryOptions) { + return this.holder.exportPermissions(mapFactory, queryOptions, true, getPlugin().getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index c88df5076..67623b00d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -67,6 +67,7 @@ import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.IntFunction; import java.util.function.Predicate; import java.util.stream.Stream; @@ -357,9 +358,11 @@ public abstract class PermissionHolder { return (List) inheritanceTree; } - public void exportPermissions(Map accumulator, QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) { + public > M exportPermissions(IntFunction mapFactory, QueryOptions queryOptions, boolean convertToLowercase, boolean resolveShorthand) { List 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 accumulator, List entries, boolean convertToLowercase, boolean resolveShorthand) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java index bb7e796ad..4a25339b6 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/model/calculated/CalculatedSubjectCachedDataManager.java @@ -47,6 +47,7 @@ import net.luckperms.api.node.ChatMetaType; import net.luckperms.api.query.QueryOptions; import java.util.Map; +import java.util.function.IntFunction; public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManager implements CalculatorFactory { private static final MetaStackDefinition DEFAULT_META_STACK = new SimpleMetaStackDefinition( @@ -83,8 +84,10 @@ public class CalculatedSubjectCachedDataManager extends AbstractCachedDataManage } @Override - protected void resolvePermissions(Map accumulator, QueryOptions queryOptions) { - this.subject.resolveAllPermissions(accumulator, queryOptions); + protected > M resolvePermissions(IntFunction mapFactory, QueryOptions queryOptions) { + M map = mapFactory.apply(16); + this.subject.resolveAllPermissions(map, queryOptions); + return map; } @Override