Implement Sponge subject lookup queries using CachedData from LP common
This commit is contained in:
@@ -0,0 +1,388 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.caching;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.CacheLoader;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
|
||||
import me.lucko.luckperms.api.ChatMetaType;
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.FullySatisfiedContexts;
|
||||
import me.lucko.luckperms.api.caching.CachedData;
|
||||
import me.lucko.luckperms.api.caching.MetaContexts;
|
||||
import me.lucko.luckperms.common.caching.type.MetaAccumulator;
|
||||
import me.lucko.luckperms.common.caching.type.MetaCache;
|
||||
import me.lucko.luckperms.common.caching.type.PermissionCache;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculator;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculatorMetadata;
|
||||
import me.lucko.luckperms.common.metastacking.SimpleMetaStack;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Abstract implementation of {@link CachedData}.
|
||||
*/
|
||||
public abstract class AbstractCachedData implements CachedData {
|
||||
|
||||
/**
|
||||
* The plugin instance
|
||||
*/
|
||||
protected final LuckPermsPlugin plugin;
|
||||
|
||||
/**
|
||||
* The cache used for {@link PermissionCache} instances.
|
||||
*/
|
||||
private final LoadingCache<Contexts, PermissionCache> permission = Caffeine.newBuilder()
|
||||
.expireAfterAccess(2, TimeUnit.MINUTES)
|
||||
.build(new PermissionCacheLoader());
|
||||
|
||||
/**
|
||||
* The cache used for {@link MetaCache} instances.
|
||||
*/
|
||||
private final LoadingCache<MetaContexts, MetaCache> meta = Caffeine.newBuilder()
|
||||
.expireAfterAccess(2, TimeUnit.MINUTES)
|
||||
.build(new MetaCacheLoader());
|
||||
|
||||
public AbstractCachedData(LuckPermsPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link PermissionCalculatorMetadata} instance for the given {@link Contexts}.
|
||||
*
|
||||
* @param contexts the contexts the permission calculator is for
|
||||
* @return the metadata instance
|
||||
*/
|
||||
protected abstract PermissionCalculatorMetadata getMetadataForContexts(Contexts contexts);
|
||||
|
||||
/**
|
||||
* Gets the {@link CalculatorFactory} used to build {@link PermissionCalculator}s.
|
||||
*
|
||||
* @return the calculator factory
|
||||
*/
|
||||
protected abstract CalculatorFactory getCalculatorFactory();
|
||||
|
||||
/**
|
||||
* Upgrades the given {@link Contexts} to a {@link MetaContexts} instance using the default settings.
|
||||
*
|
||||
* @param contexts the contexts to upgrade
|
||||
* @return a meta contexts instance
|
||||
*/
|
||||
protected abstract MetaContexts getDefaultMetaContexts(Contexts contexts);
|
||||
|
||||
/**
|
||||
* Resolves the owners permissions data according to the specification
|
||||
* outlined by {@link FullySatisfiedContexts}.
|
||||
*
|
||||
* @return a map of permissions to back the {@link PermissionCache}
|
||||
*/
|
||||
protected abstract Map<String, Boolean> resolvePermissions();
|
||||
|
||||
/**
|
||||
* Resolves the owners permissions data in the given {@link Contexts}.
|
||||
*
|
||||
* @param contexts the contexts
|
||||
* @return a map of permissions to back the {@link PermissionCache}
|
||||
*/
|
||||
protected abstract Map<String, Boolean> resolvePermissions(Contexts contexts);
|
||||
|
||||
/**
|
||||
* Resolves the owners meta data according to the specification
|
||||
* outlined by {@link FullySatisfiedContexts}.
|
||||
*
|
||||
* @param accumulator the accumulator to add resolved meta to
|
||||
*/
|
||||
protected abstract void resolveMeta(MetaAccumulator accumulator);
|
||||
|
||||
/**
|
||||
* Resolves the owners meta data in the given {@link Contexts}.
|
||||
*
|
||||
* @param accumulator the accumulator to add resolved meta to
|
||||
* @param contexts the contexts
|
||||
*/
|
||||
protected abstract void resolveMeta(MetaAccumulator accumulator, MetaContexts contexts);
|
||||
|
||||
/**
|
||||
* Calculates a {@link PermissionCache} instance.
|
||||
*
|
||||
* @param contexts the contexts to calculate in
|
||||
* @param data an old data instance to try to reuse - ignored if null
|
||||
* @return the calculated instance
|
||||
*/
|
||||
private PermissionCache calculatePermissions(Contexts contexts, PermissionCache data) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
if (data == null) {
|
||||
PermissionCalculatorMetadata metadata = getMetadataForContexts(contexts);
|
||||
data = new PermissionCache(contexts, metadata, getCalculatorFactory());
|
||||
}
|
||||
|
||||
if (contexts == Contexts.allowAll()) {
|
||||
data.setPermissions(resolvePermissions());
|
||||
} else {
|
||||
data.setPermissions(resolvePermissions(contexts));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a {@link MetaCache} instance.
|
||||
*
|
||||
* @param contexts the contexts to calculate in
|
||||
* @param data an old data instance to try to reuse - ignored if null
|
||||
* @return the calculated instance
|
||||
*/
|
||||
private MetaCache calculateMeta(MetaContexts contexts, MetaCache data) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
if (data == null) {
|
||||
data = new MetaCache(contexts);
|
||||
}
|
||||
|
||||
MetaAccumulator accumulator = newAccumulator(contexts);
|
||||
if (contexts.getContexts() == Contexts.allowAll()) {
|
||||
resolveMeta(accumulator);
|
||||
} else {
|
||||
resolveMeta(accumulator, contexts);
|
||||
}
|
||||
data.loadMeta(accumulator);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public PermissionCache getPermissionData(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return this.permission.get(contexts);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache getMetaData(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return this.meta.get(contexts);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache getMetaData(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return getMetaData(getDefaultMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public PermissionCache calculatePermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return calculatePermissions(contexts, null);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache calculateMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return calculateMeta(contexts, null);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache calculateMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return calculateMeta(getDefaultMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculatePermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.permission.refresh(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.meta.refresh(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
recalculateMeta(getDefaultMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<PermissionCache> reloadPermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
// get the previous value - to use when recalculating
|
||||
PermissionCache previous = this.permission.getIfPresent(contexts);
|
||||
|
||||
// invalidate the entry
|
||||
this.permission.invalidate(contexts);
|
||||
|
||||
// repopulate the cache
|
||||
return CompletableFuture.supplyAsync(() -> this.permission.get(contexts, c -> calculatePermissions(c, previous)));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<MetaCache> reloadMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
// get the previous value - to use when recalculating
|
||||
MetaCache previous = this.meta.getIfPresent(contexts);
|
||||
|
||||
// invalidate the entry
|
||||
this.meta.invalidate(contexts);
|
||||
|
||||
// repopulate the cache
|
||||
return CompletableFuture.supplyAsync(() -> this.meta.get(contexts, c -> calculateMeta(c, previous)));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<MetaCache> reloadMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return reloadMeta(getDefaultMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculatePermissions() {
|
||||
Set<Contexts> keys = this.permission.asMap().keySet();
|
||||
keys.forEach(this::recalculatePermissions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateMeta() {
|
||||
Set<MetaContexts> keys = this.meta.asMap().keySet();
|
||||
keys.forEach(this::recalculateMeta);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<Void> reloadPermissions() {
|
||||
Set<Contexts> keys = new HashSet<>(this.permission.asMap().keySet());
|
||||
return CompletableFuture.allOf(keys.stream().map(this::reloadPermissions).toArray(CompletableFuture[]::new));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<Void> reloadMeta() {
|
||||
Set<MetaContexts> keys = new HashSet<>(this.meta.asMap().keySet());
|
||||
return CompletableFuture.allOf(keys.stream().map(this::reloadMeta).toArray(CompletableFuture[]::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preCalculate(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
// pre-calculate just by requesting the data from this cache.
|
||||
// if the data isn't already loaded, it will be calculated.
|
||||
getPermissionData(contexts);
|
||||
getMetaData(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidatePermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.permission.invalidate(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.meta.invalidate(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.meta.invalidate(getDefaultMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidatePermissionCalculators() {
|
||||
this.permission.asMap().values().forEach(PermissionCache::invalidateCache);
|
||||
}
|
||||
|
||||
public void invalidateCaches() {
|
||||
this.permission.invalidateAll();
|
||||
this.meta.invalidateAll();
|
||||
}
|
||||
|
||||
public void doCacheCleanup() {
|
||||
this.permission.cleanUp();
|
||||
this.meta.cleanUp();
|
||||
}
|
||||
|
||||
private final class PermissionCacheLoader implements CacheLoader<Contexts, PermissionCache> {
|
||||
@Override
|
||||
public PermissionCache load(@Nonnull Contexts contexts) {
|
||||
return calculatePermissions(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionCache reload(@Nonnull Contexts contexts, @Nonnull PermissionCache oldData) {
|
||||
return calculatePermissions(contexts, oldData);
|
||||
}
|
||||
}
|
||||
|
||||
private final class MetaCacheLoader implements CacheLoader<MetaContexts, MetaCache> {
|
||||
@Override
|
||||
public MetaCache load(@Nonnull MetaContexts contexts) {
|
||||
return calculateMeta(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaCache reload(@Nonnull MetaContexts contexts, @Nonnull MetaCache oldData) {
|
||||
return calculateMeta(contexts, oldData);
|
||||
}
|
||||
}
|
||||
|
||||
private static MetaAccumulator newAccumulator(MetaContexts contexts) {
|
||||
return new MetaAccumulator(
|
||||
new SimpleMetaStack(contexts.getPrefixStackDefinition(), ChatMetaType.PREFIX),
|
||||
new SimpleMetaStack(contexts.getSuffixStackDefinition(), ChatMetaType.SUFFIX)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,8 +25,11 @@
|
||||
|
||||
package me.lucko.luckperms.common.caching;
|
||||
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.caching.GroupData;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculatorMetadata;
|
||||
import me.lucko.luckperms.common.model.Group;
|
||||
import me.lucko.luckperms.common.references.HolderType;
|
||||
|
||||
/**
|
||||
* Holds an easily accessible cache of a groups's data in a number of contexts
|
||||
@@ -38,7 +41,7 @@ public class GroupCachedData extends HolderCachedData<Group> implements GroupDat
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getHolderName() {
|
||||
return this.holder.getName();
|
||||
protected PermissionCalculatorMetadata getMetadataForContexts(Contexts contexts) {
|
||||
return PermissionCalculatorMetadata.of(HolderType.GROUP, this.holder.getFriendlyName(), contexts.getContexts());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,305 +25,56 @@
|
||||
|
||||
package me.lucko.luckperms.common.caching;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.CacheLoader;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
|
||||
import me.lucko.luckperms.api.ChatMetaType;
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.caching.CachedData;
|
||||
import me.lucko.luckperms.api.caching.MetaContexts;
|
||||
import me.lucko.luckperms.common.caching.type.MetaAccumulator;
|
||||
import me.lucko.luckperms.common.caching.type.MetaCache;
|
||||
import me.lucko.luckperms.common.caching.type.PermissionCache;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculatorMetadata;
|
||||
import me.lucko.luckperms.common.metastacking.SimpleMetaStack;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.model.PermissionHolder;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Holds an easily accessible cache of a holders data in a number of contexts
|
||||
*/
|
||||
public abstract class HolderCachedData<T extends PermissionHolder> implements CachedData {
|
||||
public abstract class HolderCachedData<T extends PermissionHolder> extends AbstractCachedData {
|
||||
|
||||
/**
|
||||
* The holder whom this data instance is representing
|
||||
*/
|
||||
protected final T holder;
|
||||
|
||||
/**
|
||||
* The cache used for {@link PermissionCache} instances.
|
||||
*/
|
||||
private final LoadingCache<Contexts, PermissionCache> permission = Caffeine.newBuilder()
|
||||
.expireAfterAccess(2, TimeUnit.MINUTES)
|
||||
.build(new PermissionCacheLoader());
|
||||
|
||||
/**
|
||||
* The cache used for {@link MetaCache} instances.
|
||||
*/
|
||||
private final LoadingCache<MetaContexts, MetaCache> meta = Caffeine.newBuilder()
|
||||
.expireAfterAccess(2, TimeUnit.MINUTES)
|
||||
.build(new MetaCacheLoader());
|
||||
|
||||
public HolderCachedData(T holder) {
|
||||
super(holder.getPlugin());
|
||||
this.holder = holder;
|
||||
}
|
||||
|
||||
protected abstract String getHolderName();
|
||||
|
||||
/**
|
||||
* Calculates a {@link PermissionCache} instance.
|
||||
*
|
||||
* @param contexts the contexts to calculate in
|
||||
* @param data an old data instance to try to reuse - ignored if null
|
||||
* @return the calculated instance
|
||||
*/
|
||||
private PermissionCache calculatePermissions(Contexts contexts, PermissionCache data) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
if (data == null) {
|
||||
PermissionCalculatorMetadata metadata = PermissionCalculatorMetadata.of(this.holder.getType(), getHolderName(), contexts.getContexts());
|
||||
data = new PermissionCache(contexts, metadata, this.holder.getPlugin().getCalculatorFactory());
|
||||
}
|
||||
|
||||
if (contexts == Contexts.allowAll()) {
|
||||
data.setPermissions(this.holder.exportNodesAndShorthand(true));
|
||||
} else {
|
||||
data.setPermissions(this.holder.exportNodesAndShorthand(contexts, true));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a {@link MetaCache} instance.
|
||||
*
|
||||
* @param contexts the contexts to calculate in
|
||||
* @param data an old data instance to try to reuse - ignored if null
|
||||
* @return the calculated instance
|
||||
*/
|
||||
private MetaCache calculateMeta(MetaContexts contexts, MetaCache data) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
if (data == null) {
|
||||
data = new MetaCache(contexts);
|
||||
}
|
||||
|
||||
if (contexts.getContexts() == Contexts.allowAll()) {
|
||||
data.loadMeta(this.holder.accumulateMeta(newAccumulator(contexts)));
|
||||
} else {
|
||||
data.loadMeta(this.holder.accumulateMeta(newAccumulator(contexts), contexts.getContexts()));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public PermissionCache getPermissionData(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return this.permission.get(contexts);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache getMetaData(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return this.meta.get(contexts);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache getMetaData(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return getMetaData(this.holder.getPlugin().getContextManager().formMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public PermissionCache calculatePermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return calculatePermissions(contexts, null);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache calculateMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return calculateMeta(contexts, null);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MetaCache calculateMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return calculateMeta(this.holder.getPlugin().getContextManager().formMetaContexts(contexts));
|
||||
protected CalculatorFactory getCalculatorFactory() {
|
||||
return this.plugin.getCalculatorFactory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculatePermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.permission.refresh(contexts);
|
||||
protected MetaContexts getDefaultMetaContexts(Contexts contexts) {
|
||||
return this.plugin.getContextManager().formMetaContexts(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.meta.refresh(contexts);
|
||||
protected Map<String, Boolean> resolvePermissions() {
|
||||
return this.holder.exportNodesAndShorthand(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
recalculateMeta(this.holder.getPlugin().getContextManager().formMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<PermissionCache> reloadPermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
// get the previous value - to use when recalculating
|
||||
PermissionCache previous = this.permission.getIfPresent(contexts);
|
||||
|
||||
// invalidate the entry
|
||||
this.permission.invalidate(contexts);
|
||||
|
||||
// repopulate the cache
|
||||
return CompletableFuture.supplyAsync(() -> this.permission.get(contexts, c -> calculatePermissions(c, previous)));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<MetaCache> reloadMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
// get the previous value - to use when recalculating
|
||||
MetaCache previous = this.meta.getIfPresent(contexts);
|
||||
|
||||
// invalidate the entry
|
||||
this.meta.invalidate(contexts);
|
||||
|
||||
// repopulate the cache
|
||||
return CompletableFuture.supplyAsync(() -> this.meta.get(contexts, c -> calculateMeta(c, previous)));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<MetaCache> reloadMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
return reloadMeta(this.holder.getPlugin().getContextManager().formMetaContexts(contexts));
|
||||
protected Map<String, Boolean> resolvePermissions(Contexts contexts) {
|
||||
return this.holder.exportNodesAndShorthand(contexts, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculatePermissions() {
|
||||
Set<Contexts> keys = this.permission.asMap().keySet();
|
||||
keys.forEach(this::recalculatePermissions);
|
||||
protected void resolveMeta(MetaAccumulator accumulator) {
|
||||
this.holder.accumulateMeta(accumulator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateMeta() {
|
||||
Set<MetaContexts> keys = this.meta.asMap().keySet();
|
||||
keys.forEach(this::recalculateMeta);
|
||||
protected void resolveMeta(MetaAccumulator accumulator, MetaContexts contexts) {
|
||||
this.holder.accumulateMeta(accumulator, contexts.getContexts());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<Void> reloadPermissions() {
|
||||
Set<Contexts> keys = new HashSet<>(this.permission.asMap().keySet());
|
||||
return CompletableFuture.allOf(keys.stream().map(this::reloadPermissions).toArray(CompletableFuture[]::new));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CompletableFuture<Void> reloadMeta() {
|
||||
Set<MetaContexts> keys = new HashSet<>(this.meta.asMap().keySet());
|
||||
return CompletableFuture.allOf(keys.stream().map(this::reloadMeta).toArray(CompletableFuture[]::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preCalculate(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
|
||||
// pre-calculate just by requesting the data from this cache.
|
||||
// if the data isn't already loaded, it will be calculated.
|
||||
getPermissionData(contexts);
|
||||
getMetaData(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidatePermissions(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.permission.invalidate(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateMeta(@Nonnull MetaContexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.meta.invalidate(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateMeta(@Nonnull Contexts contexts) {
|
||||
Objects.requireNonNull(contexts, "contexts");
|
||||
this.meta.invalidate(this.holder.getPlugin().getContextManager().formMetaContexts(contexts));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidatePermissionCalculators() {
|
||||
this.permission.asMap().values().forEach(PermissionCache::invalidateCache);
|
||||
}
|
||||
|
||||
public void invalidateCaches() {
|
||||
this.permission.invalidateAll();
|
||||
this.meta.invalidateAll();
|
||||
}
|
||||
|
||||
public void doCacheCleanup() {
|
||||
this.permission.cleanUp();
|
||||
this.meta.cleanUp();
|
||||
}
|
||||
|
||||
private final class PermissionCacheLoader implements CacheLoader<Contexts, PermissionCache> {
|
||||
@Override
|
||||
public PermissionCache load(@Nonnull Contexts contexts) {
|
||||
return calculatePermissions(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionCache reload(@Nonnull Contexts contexts, @Nonnull PermissionCache oldData) {
|
||||
return calculatePermissions(contexts, oldData);
|
||||
}
|
||||
}
|
||||
|
||||
private final class MetaCacheLoader implements CacheLoader<MetaContexts, MetaCache> {
|
||||
@Override
|
||||
public MetaCache load(@Nonnull MetaContexts contexts) {
|
||||
return calculateMeta(contexts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaCache reload(@Nonnull MetaContexts contexts, @Nonnull MetaCache oldData) {
|
||||
return calculateMeta(contexts, oldData);
|
||||
}
|
||||
}
|
||||
|
||||
private static MetaAccumulator newAccumulator(MetaContexts contexts) {
|
||||
return new MetaAccumulator(
|
||||
new SimpleMetaStack(contexts.getPrefixStackDefinition(), ChatMetaType.PREFIX),
|
||||
new SimpleMetaStack(contexts.getSuffixStackDefinition(), ChatMetaType.SUFFIX)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,8 +25,11 @@
|
||||
|
||||
package me.lucko.luckperms.common.caching;
|
||||
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.caching.UserData;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculatorMetadata;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.references.HolderType;
|
||||
|
||||
/**
|
||||
* Holds an easily accessible cache of a user's data in a number of contexts
|
||||
@@ -38,7 +41,7 @@ public class UserCachedData extends HolderCachedData<User> implements UserData {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getHolderName() {
|
||||
return this.holder.getFriendlyName();
|
||||
protected PermissionCalculatorMetadata getMetadataForContexts(Contexts contexts) {
|
||||
return PermissionCalculatorMetadata.of(HolderType.USER, this.holder.getFriendlyName(), contexts.getContexts());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,10 @@ public class MetaAccumulator {
|
||||
}
|
||||
}
|
||||
|
||||
public void accumulateMeta(String key, String value) {
|
||||
this.meta.put(key, value);
|
||||
}
|
||||
|
||||
public void accumulateWeight(int weight) {
|
||||
this.weight = Math.max(this.weight, weight);
|
||||
}
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ import com.google.common.collect.MapMaker;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class AbstractCalculatorFactory implements CalculatorFactory {
|
||||
public abstract class AbstractCalculatorFactory implements PlatformCalculatorFactory {
|
||||
private final Set<PermissionCalculator> calculators = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
||||
|
||||
protected PermissionCalculator registerCalculator(PermissionCalculator calculator) {
|
||||
|
||||
@@ -41,9 +41,4 @@ public interface CalculatorFactory {
|
||||
*/
|
||||
PermissionCalculator build(Contexts contexts, PermissionCalculatorMetadata metadata);
|
||||
|
||||
/**
|
||||
* Invalidates all calculators build by this factory
|
||||
*/
|
||||
void invalidateAll();
|
||||
|
||||
}
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.calculators;
|
||||
|
||||
/**
|
||||
* Extension of {@link CalculatorFactory} which keeps a record of produced
|
||||
* calculators and provides a means to invalidate them all in bulk.
|
||||
*/
|
||||
public interface PlatformCalculatorFactory extends CalculatorFactory {
|
||||
|
||||
/**
|
||||
* Invalidates all calculators build by this factory
|
||||
*/
|
||||
void invalidateAll();
|
||||
|
||||
}
|
||||
+6
-6
@@ -43,12 +43,12 @@ import javax.annotation.Nonnull;
|
||||
* Contains the standard {@link MetaStackElement}s provided by LuckPerms.
|
||||
*/
|
||||
public final class StandardStackElements {
|
||||
private static final HighestPriority HIGHEST_PRIORITY = new HighestPriority();
|
||||
private static final LowestPriority LOWEST_PRIORITY = new LowestPriority();
|
||||
private static final HighestPriorityOwn HIGHEST_PRIORITY_OWN = new HighestPriorityOwn();
|
||||
private static final LowestPriorityOwn LOWEST_PRIORITY_OWN = new LowestPriorityOwn();
|
||||
private static final HighestPriorityInherited HIGHEST_PRIORITY_INHERITED = new HighestPriorityInherited();
|
||||
private static final LowestPriorityInherited LOWEST_PRIORITY_INHERITED = new LowestPriorityInherited();
|
||||
public static final HighestPriority HIGHEST_PRIORITY = new HighestPriority();
|
||||
public static final LowestPriority LOWEST_PRIORITY = new LowestPriority();
|
||||
public static final HighestPriorityOwn HIGHEST_PRIORITY_OWN = new HighestPriorityOwn();
|
||||
public static final LowestPriorityOwn LOWEST_PRIORITY_OWN = new LowestPriorityOwn();
|
||||
public static final HighestPriorityInherited HIGHEST_PRIORITY_INHERITED = new HighestPriorityInherited();
|
||||
public static final LowestPriorityInherited LOWEST_PRIORITY_INHERITED = new LowestPriorityInherited();
|
||||
|
||||
public static Optional<MetaStackElement> parseFromString(LuckPermsPlugin plugin, String s) {
|
||||
s = s.toLowerCase();
|
||||
|
||||
@@ -32,7 +32,7 @@ import me.lucko.luckperms.common.api.LuckPermsApiProvider;
|
||||
import me.lucko.luckperms.common.buffers.BufferedRequest;
|
||||
import me.lucko.luckperms.common.buffers.UpdateTaskBuffer;
|
||||
import me.lucko.luckperms.common.caching.handlers.CachedStateManager;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.PlatformCalculatorFactory;
|
||||
import me.lucko.luckperms.common.commands.sender.Sender;
|
||||
import me.lucko.luckperms.common.commands.utils.CommandUtils;
|
||||
import me.lucko.luckperms.common.config.AbstractConfiguration;
|
||||
@@ -80,7 +80,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
|
||||
private BufferedRequest<Void> updateTaskBuffer;
|
||||
private InheritanceHandler inheritanceHandler;
|
||||
private CachedStateManager cachedStateManager;
|
||||
private CalculatorFactory calculatorFactory;
|
||||
private PlatformCalculatorFactory calculatorFactory;
|
||||
private LuckPermsApiProvider apiProvider;
|
||||
private EventFactory eventFactory;
|
||||
|
||||
@@ -231,7 +231,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
|
||||
protected abstract MessagingFactory<?> provideMessagingFactory();
|
||||
protected abstract void registerCommands();
|
||||
protected abstract void setupManagers();
|
||||
protected abstract CalculatorFactory provideCalculatorFactory();
|
||||
protected abstract PlatformCalculatorFactory provideCalculatorFactory();
|
||||
protected abstract void setupContextManager();
|
||||
protected abstract void setupPlatformHooks();
|
||||
protected abstract void registerApiOnPlatform(LuckPermsApi api);
|
||||
@@ -314,7 +314,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CalculatorFactory getCalculatorFactory() {
|
||||
public PlatformCalculatorFactory getCalculatorFactory() {
|
||||
return this.calculatorFactory;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user