Implement Sponge subject lookup queries using CachedData from LP common

This commit is contained in:
Luck 2018-03-07 23:50:23 +00:00
parent 6d2ea659a4
commit bf69d5314e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
33 changed files with 1473 additions and 843 deletions

View File

@ -43,7 +43,7 @@ import me.lucko.luckperms.bukkit.model.server.LPDefaultsMap;
import me.lucko.luckperms.bukkit.model.server.LPPermissionMap;
import me.lucko.luckperms.bukkit.model.server.LPSubscriptionMap;
import me.lucko.luckperms.bukkit.vault.VaultHookManager;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.calculators.PlatformCalculatorFactory;
import me.lucko.luckperms.common.commands.CommandPermission;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.config.ConfigKeys;
@ -139,7 +139,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin {
}
@Override
protected CalculatorFactory provideCalculatorFactory() {
protected PlatformCalculatorFactory provideCalculatorFactory() {
return new BukkitCalculatorFactory(this);
}

View File

@ -34,7 +34,7 @@ import me.lucko.luckperms.bungee.contexts.RedisBungeeCalculator;
import me.lucko.luckperms.bungee.listeners.BungeeConnectionListener;
import me.lucko.luckperms.bungee.listeners.BungeePermissionCheckListener;
import me.lucko.luckperms.bungee.messaging.BungeeMessagingFactory;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.calculators.PlatformCalculatorFactory;
import me.lucko.luckperms.common.commands.CommandManager;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter;
@ -120,7 +120,7 @@ public class LPBungeePlugin extends AbstractLuckPermsPlugin {
}
@Override
protected CalculatorFactory provideCalculatorFactory() {
protected PlatformCalculatorFactory provideCalculatorFactory() {
return new BungeeCalculatorFactory(this);
}

View File

@ -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)
);
}
}

View File

@ -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());
}
}

View File

@ -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)
);
}
}

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -41,9 +41,4 @@ public interface CalculatorFactory {
*/
PermissionCalculator build(Contexts contexts, PermissionCalculatorMetadata metadata);
/**
* Invalidates all calculators build by this factory
*/
void invalidateAll();
}

View File

@ -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();
}

View File

@ -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();

View File

@ -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;
}

View File

@ -27,7 +27,7 @@ package me.lucko.luckperms.nukkit;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LuckPermsApi;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.calculators.PlatformCalculatorFactory;
import me.lucko.luckperms.common.commands.CommandPermission;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.config.ConfigKeys;
@ -133,7 +133,7 @@ public class LPNukkitPlugin extends AbstractLuckPermsPlugin {
}
@Override
protected CalculatorFactory provideCalculatorFactory() {
protected PlatformCalculatorFactory provideCalculatorFactory() {
return new NukkitCalculatorFactory(this);
}

View File

@ -26,12 +26,10 @@
package me.lucko.luckperms.sponge.service.model;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory;
import org.spongepowered.api.plugin.PluginContainer;
@ -40,7 +38,6 @@ import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.text.Text;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;
@ -79,8 +76,5 @@ public interface LPPermissionService {
void registerContextCalculator(ContextCalculator<Subject> calculator);
// utils
ImmutableList<LPSubjectReference> sortSubjects(Collection<LPSubjectReference> s);
void invalidateAllCaches(LPSubject.CacheLevel cacheLevel);
void invalidateAllCaches();
}

View File

@ -82,17 +82,6 @@ public interface LPSubject {
}
void invalidateCaches(CacheLevel cacheLevel);
/**
* The level of cache for invalidation
*
* Invalidating at {@link #PARENT} will also invalidate at
* {@link #PERMISSION} and {@link #OPTION}, and invalidating at
* {@link #PERMISSION} will also invalidate at {@link #OPTION}.
*/
enum CacheLevel {
PARENT, PERMISSION, OPTION
}
void invalidateCaches();
}

View File

@ -52,6 +52,19 @@ public interface LPSubjectCollection {
Predicate<String> getIdentifierValidityPredicate();
// transient has priority for all collections except default
default ResolutionOrder getResolutionOrder() {
if (isDefaultsCollection()) {
return ResolutionOrder.TRANSIENT_LAST;
} else {
return ResolutionOrder.TRANSIENT_FIRST;
}
}
default boolean isDefaultsCollection() {
return false;
}
CompletableFuture<LPSubject> loadSubject(String identifier);
Optional<LPSubject> getSubject(String identifier);

View File

@ -0,0 +1,33 @@
/*
* 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.sponge.service.model;
public enum ResolutionOrder {
TRANSIENT_FIRST,
TRANSIENT_LAST
}

View File

@ -27,7 +27,7 @@ package me.lucko.luckperms.sponge;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LuckPermsApi;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.calculators.PlatformCalculatorFactory;
import me.lucko.luckperms.common.commands.CommandManager;
import me.lucko.luckperms.common.commands.CommandPermission;
import me.lucko.luckperms.common.commands.abstraction.Command;
@ -53,7 +53,6 @@ import me.lucko.luckperms.sponge.messaging.SpongeMessagingFactory;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.event.UpdateEventHandler;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.persisted.PersistedCollection;
import me.lucko.luckperms.sponge.tasks.ServiceCacheHousekeepingTask;
@ -134,7 +133,7 @@ public class LPSpongePlugin extends AbstractLuckPermsPlugin {
}
@Override
protected CalculatorFactory provideCalculatorFactory() {
protected PlatformCalculatorFactory provideCalculatorFactory() {
return new SpongeCalculatorFactory(this);
}
@ -197,7 +196,7 @@ public class LPSpongePlugin extends AbstractLuckPermsPlugin {
((PersistedCollection) collection).loadAll();
}
}
this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT);
this.service.invalidateAllCaches();
}
private Path resolveConfig() {

View File

@ -28,19 +28,19 @@ package me.lucko.luckperms.sponge.processors;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.processors.PermissionProcessor;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
public abstract class DefaultsProcessor implements PermissionProcessor {
private final LuckPermsService service;
private final LPPermissionService service;
private final ImmutableContextSet contexts;
public DefaultsProcessor(LuckPermsService service, ImmutableContextSet contexts) {
public DefaultsProcessor(LPPermissionService service, ImmutableContextSet contexts) {
this.service = service;
this.contexts = contexts;
}
protected abstract LPSubject getTypeDefaults(LuckPermsService service);
protected abstract LPSubject getTypeDefaults(LPPermissionService service);
@Override
public Tristate hasPermission(String permission) {

View File

@ -0,0 +1,44 @@
/*
* 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.sponge.processors;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
public class FixedDefaultsProcessor extends DefaultsProcessor {
private final LPSubject defaultsSubject;
public FixedDefaultsProcessor(LPPermissionService service, ImmutableContextSet contexts, LPSubject defaultsSubject) {
super(service, contexts);
this.defaultsSubject = defaultsSubject;
}
@Override
protected LPSubject getTypeDefaults(LPPermissionService service) {
return this.defaultsSubject;
}
}

View File

@ -27,16 +27,16 @@ package me.lucko.luckperms.sponge.processors;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.processors.PermissionProcessor;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
public class GroupDefaultsProcessor extends DefaultsProcessor implements PermissionProcessor {
public GroupDefaultsProcessor(LuckPermsService service, ImmutableContextSet contexts) {
public GroupDefaultsProcessor(LPPermissionService service, ImmutableContextSet contexts) {
super(service, contexts);
}
@Override
protected LPSubject getTypeDefaults(LuckPermsService service) {
protected LPSubject getTypeDefaults(LPPermissionService service) {
return service.getGroupSubjects().getDefaults();
}
}

View File

@ -27,16 +27,16 @@ package me.lucko.luckperms.sponge.processors;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.processors.PermissionProcessor;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
public class UserDefaultsProcessor extends DefaultsProcessor implements PermissionProcessor {
public UserDefaultsProcessor(LuckPermsService service, ImmutableContextSet contexts) {
public UserDefaultsProcessor(LPPermissionService service, ImmutableContextSet contexts) {
super(service, contexts);
}
@Override
protected LPSubject getTypeDefaults(LuckPermsService service) {
protected LPSubject getTypeDefaults(LPPermissionService service) {
return service.getUserSubjects().getDefaults();
}
}

View File

@ -27,12 +27,10 @@ package me.lucko.luckperms.sponge.service;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.sponge.LPSpongePlugin;
import me.lucko.luckperms.sponge.contexts.SpongeProxiedContextCalculator;
@ -43,7 +41,6 @@ import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.persisted.PersistedCollection;
import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory;
import me.lucko.luckperms.sponge.service.storage.SubjectStorage;
@ -54,11 +51,7 @@ import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.text.Text;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -216,56 +209,14 @@ public class LuckPermsService implements LPPermissionService {
}
@Override
public ImmutableList<LPSubjectReference> sortSubjects(Collection<LPSubjectReference> s) {
List<LPSubjectReference> ret = new ArrayList<>(s);
ret.sort(Collections.reverseOrder((o1, o2) -> {
if (o1.equals(o2)) {
return 0;
}
boolean o1isGroup = o1.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP);
boolean o2isGroup = o2.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP);
if (o1isGroup != o2isGroup) {
return o1isGroup ? 1 : -1;
}
// Neither are groups
if (!o1isGroup) {
return 1;
}
Group g1 = this.plugin.getGroupManager().getIfLoaded(o1.getSubjectIdentifier());
Group g2 = this.plugin.getGroupManager().getIfLoaded(o2.getSubjectIdentifier());
boolean g1Null = g1 == null;
boolean g2Null = g2 == null;
if (g1Null != g2Null) {
return g1Null ? -1 : 1;
}
// Both are null
if (g1Null) {
return 1;
}
return Integer.compare(g1.getWeight().orElse(0), g2.getWeight().orElse(0)) == 1 ? 1 : -1;
}));
return ImmutableList.copyOf(ret);
}
@Override
public void invalidateAllCaches(LPSubject.CacheLevel cacheLevel) {
public void invalidateAllCaches() {
for (LPSubjectCollection collection : this.collections.asMap().values()) {
for (LPSubject subject : collection.getLoadedSubjects()) {
subject.invalidateCaches(cacheLevel);
subject.invalidateCaches();
}
}
if (cacheLevel != LPSubject.CacheLevel.OPTION) {
this.plugin.getCalculatorFactory().invalidateAll();
}
this.plugin.getCalculatorFactory().invalidateAll();
}
}

View File

@ -0,0 +1,71 @@
/*
* 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.sponge.service;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.sponge.service.internal.GroupSubject;
import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import org.spongepowered.api.service.permission.PermissionService;
import java.util.Comparator;
public class SubjectComparator implements Comparator<LPSubjectReference> {
private static final Comparator<LPSubjectReference> INSTANCE = new SubjectComparator();
private static final Comparator<LPSubjectReference> REVERSE = INSTANCE.reversed();
public static Comparator<LPSubjectReference> normal() {
return INSTANCE;
}
public static Comparator<LPSubjectReference> reverse() {
return REVERSE;
}
@Override
public int compare(LPSubjectReference o1, LPSubjectReference o2) {
if (o1.equals(o2)) {
return 0;
}
boolean o1isGroup = o1.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP);
boolean o2isGroup = o2.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP);
if (o1isGroup != o2isGroup) {
return o1isGroup ? 1 : -1;
}
// Neither are groups
if (!o1isGroup) {
return 1;
}
Group g1 = ((GroupSubject) o1.resolveLp().join()).getParent();
Group g2 = ((GroupSubject) o2.resolveLp().join()).getParent();
return Integer.compare(g1.getWeight().orElse(0), g2.getWeight().orElse(0)) == 1 ? 1 : -1;
}
}

View File

@ -0,0 +1,410 @@
/*
* 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.sponge.service.calculated;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.caching.MetaContexts;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.metastacking.MetaStackDefinition;
import me.lucko.luckperms.common.caching.AbstractCachedData;
import me.lucko.luckperms.common.caching.type.MetaAccumulator;
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.graph.TraversalAlgorithm;
import me.lucko.luckperms.common.metastacking.SimpleMetaStackDefinition;
import me.lucko.luckperms.common.metastacking.StandardStackElements;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.processors.MapProcessor;
import me.lucko.luckperms.common.processors.PermissionProcessor;
import me.lucko.luckperms.common.processors.WildcardProcessor;
import me.lucko.luckperms.common.verbose.CheckOrigin;
import me.lucko.luckperms.sponge.processors.FixedDefaultsProcessor;
import me.lucko.luckperms.sponge.processors.SpongeWildcardProcessor;
import me.lucko.luckperms.sponge.service.inheritance.SubjectInheritanceGraph;
import me.lucko.luckperms.sponge.service.inheritance.SubjectInheritanceGraphs;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public abstract class CalculatedSubject implements LPSubject {
private static final MetaStackDefinition DEFAULT_META_STACK = new SimpleMetaStackDefinition(
ImmutableList.of(StandardStackElements.HIGHEST_PRIORITY),
"", "", ""
);
private final LuckPermsPlugin plugin;
private final SubjectCachedData cachedData;
protected CalculatedSubject(LuckPermsPlugin plugin) {
this.plugin = plugin;
this.cachedData = new SubjectCachedData(plugin);
}
public abstract CalculatedSubjectData getSubjectData();
public abstract CalculatedSubjectData getTransientSubjectData();
public Map<String, Boolean> getCombinedPermissions(ContextSet filter) {
Map<String, Boolean> permissions;
Map<String, Boolean> merging;
switch (getParentCollection().getResolutionOrder()) {
case TRANSIENT_FIRST:
permissions = getTransientSubjectData().resolvePermissions(filter);
merging = getSubjectData().resolvePermissions(filter);
break;
case TRANSIENT_LAST:
permissions = getSubjectData().resolvePermissions(filter);
merging = getTransientSubjectData().resolvePermissions(filter);
break;
default:
throw new AssertionError();
}
for (Map.Entry<String, Boolean> entry : merging.entrySet()) {
permissions.putIfAbsent(entry.getKey(), entry.getValue());
}
return permissions;
}
public Map<String, Boolean> getCombinedPermissions() {
Map<String, Boolean> permissions;
Map<String, Boolean> merging;
switch (getParentCollection().getResolutionOrder()) {
case TRANSIENT_FIRST:
permissions = getTransientSubjectData().resolvePermissions();
merging = getSubjectData().resolvePermissions();
break;
case TRANSIENT_LAST:
permissions = getSubjectData().resolvePermissions();
merging = getTransientSubjectData().resolvePermissions();
break;
default:
throw new AssertionError();
}
for (Map.Entry<String, Boolean> entry : merging.entrySet()) {
permissions.putIfAbsent(entry.getKey(), entry.getValue());
}
return permissions;
}
public Map<String, Boolean> resolveAllPermissions(ImmutableContextSet filter) {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph(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());
}
}
return result;
}
public Map<String, Boolean> resolveAllPermissions() {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph();
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().entrySet()) {
result.putIfAbsent(entry.getKey(), entry.getValue());
}
}
return result;
}
public Set<LPSubjectReference> getCombinedParents(ContextSet filter) {
Set<LPSubjectReference> parents;
Set<LPSubjectReference> merging;
switch (getParentCollection().getResolutionOrder()) {
case TRANSIENT_FIRST:
parents = getTransientSubjectData().resolveParents(filter);
merging = getSubjectData().resolveParents(filter);
break;
case TRANSIENT_LAST:
parents = getSubjectData().resolveParents(filter);
merging = getTransientSubjectData().resolveParents(filter);
break;
default:
throw new AssertionError();
}
for (LPSubjectReference entry : merging) {
if (!parents.contains(entry)) {
parents.add(entry);
}
}
return parents;
}
public Set<LPSubjectReference> getCombinedParents() {
Set<LPSubjectReference> parents;
Set<LPSubjectReference> merging;
switch (getParentCollection().getResolutionOrder()) {
case TRANSIENT_FIRST:
parents = getTransientSubjectData().resolveParents();
merging = getSubjectData().resolveParents();
break;
case TRANSIENT_LAST:
parents = getSubjectData().resolveParents();
merging = getTransientSubjectData().resolveParents();
break;
default:
throw new AssertionError();
}
for (LPSubjectReference entry : merging) {
if (!parents.contains(entry)) {
parents.add(entry);
}
}
return parents;
}
public Set<LPSubjectReference> resolveAllParents(ImmutableContextSet filter) {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph(filter);
Set<LPSubjectReference> result = new LinkedHashSet<>();
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (LPSubjectReference entry : subject.getCombinedParents(filter)) {
if (!result.contains(entry)) {
result.add(entry);
}
}
}
return result;
}
public Set<LPSubjectReference> resolveAllParents() {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph();
Set<LPSubjectReference> result = new LinkedHashSet<>();
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (LPSubjectReference entry : subject.getCombinedParents()) {
if (!result.contains(entry)) {
result.add(entry);
}
}
}
return result;
}
public Map<String, String> getCombinedOptions(ContextSet filter) {
Map<String, String> options;
Map<String, String> merging;
switch (getParentCollection().getResolutionOrder()) {
case TRANSIENT_FIRST:
options = getTransientSubjectData().resolveOptions(filter);
merging = getSubjectData().resolveOptions(filter);
break;
case TRANSIENT_LAST:
options = getSubjectData().resolveOptions(filter);
merging = getTransientSubjectData().resolveOptions(filter);
break;
default:
throw new AssertionError();
}
for (Map.Entry<String, String> entry : merging.entrySet()) {
options.putIfAbsent(entry.getKey(), entry.getValue());
}
return options;
}
public Map<String, String> getCombinedOptions() {
Map<String, String> options;
Map<String, String> merging;
switch (getParentCollection().getResolutionOrder()) {
case TRANSIENT_FIRST:
options = getTransientSubjectData().resolveOptions();
merging = getSubjectData().resolveOptions();
break;
case TRANSIENT_LAST:
options = getSubjectData().resolveOptions();
merging = getTransientSubjectData().resolveOptions();
break;
default:
throw new AssertionError();
}
for (Map.Entry<String, String> entry : merging.entrySet()) {
options.putIfAbsent(entry.getKey(), entry.getValue());
}
return options;
}
public Map<String, String> resolveAllOptions(ImmutableContextSet filter) {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph(filter);
Map<String, String> result = new HashMap<>();
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (Map.Entry<String, String> entry : subject.getCombinedOptions(filter).entrySet()) {
result.putIfAbsent(entry.getKey(), entry.getValue());
}
}
return result;
}
public Map<String, String> resolveAllOptions() {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph();
Map<String, String> result = new HashMap<>();
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (Map.Entry<String, String> entry : subject.getCombinedOptions().entrySet()) {
result.putIfAbsent(entry.getKey(), entry.getValue());
}
}
return result;
}
public void resolveAllOptions(MetaAccumulator accumulator, ImmutableContextSet filter) {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph(filter);
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (Map.Entry<String, String> entry : subject.getCombinedOptions(filter).entrySet()) {
accumulator.accumulateMeta(entry.getKey(), entry.getValue());
}
}
}
public void resolveAllOptions(MetaAccumulator accumulator) {
SubjectInheritanceGraph graph = SubjectInheritanceGraphs.getGraph();
Iterable<CalculatedSubject> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this);
for (CalculatedSubject subject : traversal) {
for (Map.Entry<String, String> entry : subject.getCombinedOptions().entrySet()) {
accumulator.accumulateMeta(entry.getKey(), entry.getValue());
}
}
}
@Override
public Tristate getPermissionValue(ImmutableContextSet contexts, String permission) {
Contexts lookupContexts = Contexts.of(contexts, Contexts.global().getSettings());
return this.cachedData.getPermissionData(lookupContexts).getPermissionValue(permission, CheckOrigin.INTERNAL);
}
@Override
public boolean isChildOf(ImmutableContextSet contexts, LPSubjectReference parent) {
return resolveAllParents(contexts).contains(parent);
}
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
return ImmutableList.copyOf(resolveAllParents(contexts));
}
@Override
public Optional<String> getOption(ImmutableContextSet contexts, String key) {
Contexts lookupContexts = Contexts.of(contexts, Contexts.global().getSettings());
Map<String, String> meta = this.cachedData.getMetaData(lookupContexts).getMeta();
return Optional.ofNullable(meta.get(key));
}
@Override
public void performCleanup() {
this.cachedData.doCacheCleanup();
}
@Override
public void invalidateCaches() {
this.cachedData.invalidateCaches();
}
private final class SubjectCachedData extends AbstractCachedData implements CalculatorFactory {
private SubjectCachedData(LuckPermsPlugin plugin) {
super(plugin);
}
@Override
protected PermissionCalculatorMetadata getMetadataForContexts(Contexts contexts) {
return PermissionCalculatorMetadata.of(null, getParentCollection().getIdentifier() + "/" + getIdentifier(), contexts.getContexts());
}
@Override
protected CalculatorFactory getCalculatorFactory() {
return this;
}
@Override
protected MetaContexts getDefaultMetaContexts(Contexts contexts) {
return MetaContexts.of(contexts, DEFAULT_META_STACK, DEFAULT_META_STACK);
}
@Override
protected Map<String, Boolean> resolvePermissions() {
return resolveAllPermissions();
}
@Override
protected Map<String, Boolean> resolvePermissions(Contexts contexts) {
return resolveAllPermissions(contexts.getContexts().makeImmutable());
}
@Override
protected void resolveMeta(MetaAccumulator accumulator) {
resolveAllOptions(accumulator);
}
@Override
protected void resolveMeta(MetaAccumulator accumulator, MetaContexts contexts) {
resolveAllOptions(accumulator, contexts.getContexts().getContexts().makeImmutable());
}
@Override
public PermissionCalculator build(Contexts contexts, PermissionCalculatorMetadata metadata) {
ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor());
processors.add(new SpongeWildcardProcessor());
processors.add(new WildcardProcessor());
if (!getParentCollection().isDefaultsCollection()) {
processors.add(new FixedDefaultsProcessor(getService(), contexts.getContexts().makeImmutable(), getDefaults()));
}
return new PermissionCalculator(this.plugin, metadata, processors.build());
}
}
}

View File

@ -25,22 +25,16 @@
package me.lucko.luckperms.sponge.service.calculated;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.calculators.PermissionCalculator;
import me.lucko.luckperms.common.calculators.PermissionCalculatorMetadata;
import me.lucko.luckperms.common.contexts.ContextSetComparator;
import me.lucko.luckperms.common.model.NodeMapType;
import me.lucko.luckperms.common.processors.MapProcessor;
import me.lucko.luckperms.common.processors.PermissionProcessor;
import me.lucko.luckperms.common.verbose.CheckOrigin;
import me.lucko.luckperms.sponge.processors.SpongeWildcardProcessor;
import me.lucko.luckperms.sponge.service.ProxyFactory;
import me.lucko.luckperms.sponge.service.SubjectComparator;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectData;
@ -49,6 +43,7 @@ import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import org.spongepowered.api.service.permission.SubjectData;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -57,7 +52,6 @@ import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* In-memory implementation of {@link LPSubjectData}.
@ -71,22 +65,10 @@ public class CalculatedSubjectData implements LPSubjectData {
private final Map<ImmutableContextSet, Set<LPSubjectReference>> parents = new ConcurrentHashMap<>();
private final Map<ImmutableContextSet, Map<String, String>> options = new ConcurrentHashMap<>();
private final LoadingCache<ImmutableContextSet, CalculatorHolder> permissionCache;
public CalculatedSubjectData(LPSubject parentSubject, NodeMapType type, LPPermissionService service, String calculatorDisplayName) {
public CalculatedSubjectData(LPSubject parentSubject, NodeMapType type, LPPermissionService service) {
this.parentSubject = parentSubject;
this.type = type;
this.service = service;
this.permissionCache = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(contexts -> {
ImmutableList<PermissionProcessor> processors = ImmutableList.of(new MapProcessor(), new SpongeWildcardProcessor());
PermissionCalculatorMetadata calcMetadata = PermissionCalculatorMetadata.of(null, calculatorDisplayName, contexts);
CalculatorHolder holder = new CalculatorHolder(new PermissionCalculator(this.service.getPlugin(), calcMetadata, processors));
holder.setPermissions(processPermissionsMap(contexts, this.permissions));
return holder;
});
}
@Override
@ -104,26 +86,12 @@ public class CalculatedSubjectData implements LPSubjectData {
return this.type;
}
public void cleanup() {
this.permissionCache.cleanUp();
}
public void invalidateLookupCache() {
this.permissionCache.invalidateAll();
}
public Tristate getPermissionValue(ImmutableContextSet contexts, String permission) {
CalculatorHolder calculatorHolder = Objects.requireNonNull(this.permissionCache.get(contexts));
return calculatorHolder.getCalculator().getPermissionValue(permission, CheckOrigin.INTERNAL);
}
public void replacePermissions(Map<ImmutableContextSet, Map<String, Boolean>> map) {
this.permissions.clear();
for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> e : map.entrySet()) {
this.permissions.put(e.getKey(), new ConcurrentHashMap<>(e.getValue()));
}
this.permissionCache.invalidateAll();
this.service.invalidateAllCaches(LPSubject.CacheLevel.PERMISSION);
this.service.invalidateAllCaches();
}
public void replaceParents(Map<ImmutableContextSet, List<LPSubjectReference>> map) {
@ -133,7 +101,7 @@ public class CalculatedSubjectData implements LPSubjectData {
set.addAll(e.getValue());
this.parents.put(e.getKey(), set);
}
this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT);
this.service.invalidateAllCaches();
}
public void replaceOptions(Map<ImmutableContextSet, Map<String, String>> map) {
@ -141,7 +109,7 @@ public class CalculatedSubjectData implements LPSubjectData {
for (Map.Entry<ImmutableContextSet, Map<String, String>> e : map.entrySet()) {
this.options.put(e.getKey(), new ConcurrentHashMap<>(e.getValue()));
}
this.service.invalidateAllCaches(LPSubject.CacheLevel.OPTION);
this.service.invalidateAllCaches();
}
@Override
@ -153,159 +121,10 @@ public class CalculatedSubjectData implements LPSubjectData {
return map.build();
}
@Override
public CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate value) {
boolean b;
if (value == Tristate.UNDEFINED) {
Map<String, Boolean> perms = this.permissions.get(contexts);
b = perms != null && perms.remove(permission.toLowerCase()) != null;
} else {
Map<String, Boolean> perms = this.permissions.computeIfAbsent(contexts, c -> new ConcurrentHashMap<>());
b = !Objects.equals(perms.put(permission.toLowerCase(), value.asBoolean()), value.asBoolean());
}
if (b) {
this.permissionCache.invalidateAll();
this.service.invalidateAllCaches(LPSubject.CacheLevel.PERMISSION);
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> clearPermissions() {
if (this.permissions.isEmpty()) {
return CompletableFuture.completedFuture(false);
} else {
this.permissions.clear();
this.permissionCache.invalidateAll();
this.service.invalidateAllCaches(LPSubject.CacheLevel.PERMISSION);
return CompletableFuture.completedFuture(true);
}
}
@Override
public CompletableFuture<Boolean> clearPermissions(ImmutableContextSet contexts) {
Map<String, Boolean> perms = this.permissions.get(contexts);
if (perms == null) {
return CompletableFuture.completedFuture(false);
}
this.permissions.remove(contexts);
if (!perms.isEmpty()) {
this.permissionCache.invalidateAll();
this.service.invalidateAllCaches(LPSubject.CacheLevel.PERMISSION);
return CompletableFuture.completedFuture(true);
}
return CompletableFuture.completedFuture(false);
}
@Override
public ImmutableMap<ImmutableContextSet, ImmutableList<LPSubjectReference>> getAllParents() {
ImmutableMap.Builder<ImmutableContextSet, ImmutableList<LPSubjectReference>> map = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, Set<LPSubjectReference>> e : this.parents.entrySet()) {
map.put(e.getKey(), this.service.sortSubjects(e.getValue()));
}
return map.build();
}
@Override
public CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference parent) {
Set<LPSubjectReference> set = this.parents.computeIfAbsent(contexts, c -> ConcurrentHashMap.newKeySet());
boolean b = set.add(parent);
if (b) {
this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT);
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> removeParent(ImmutableContextSet contexts, LPSubjectReference parent) {
Set<LPSubjectReference> set = this.parents.get(contexts);
boolean b = set != null && set.remove(parent);
if (b) {
this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT);
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> clearParents() {
if (this.parents.isEmpty()) {
return CompletableFuture.completedFuture(false);
} else {
this.parents.clear();
this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT);
return CompletableFuture.completedFuture(true);
}
}
@Override
public CompletableFuture<Boolean> clearParents(ImmutableContextSet contexts) {
Set<LPSubjectReference> set = this.parents.get(contexts);
if (set == null) {
return CompletableFuture.completedFuture(false);
}
this.parents.remove(contexts);
this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT);
return CompletableFuture.completedFuture(!set.isEmpty());
}
@Override
public ImmutableMap<ImmutableContextSet, ImmutableMap<String, String>> getAllOptions() {
ImmutableMap.Builder<ImmutableContextSet, ImmutableMap<String, String>> map = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, Map<String, String>> e : this.options.entrySet()) {
map.put(e.getKey(), ImmutableMap.copyOf(e.getValue()));
}
return map.build();
}
@Override
public CompletableFuture<Boolean> setOption(ImmutableContextSet contexts, String key, String value) {
Map<String, String> options = this.options.computeIfAbsent(contexts, c -> new ConcurrentHashMap<>());
boolean b = !stringEquals(options.put(key.toLowerCase(), value), value);
if (b) {
this.service.invalidateAllCaches(LPSubject.CacheLevel.OPTION);
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> unsetOption(ImmutableContextSet contexts, String key) {
Map<String, String> options = this.options.get(contexts);
boolean b = options != null && options.remove(key.toLowerCase()) != null;
if (b) {
this.service.invalidateAllCaches(LPSubject.CacheLevel.OPTION);
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> clearOptions() {
if (this.options.isEmpty()) {
return CompletableFuture.completedFuture(false);
} else {
this.options.clear();
this.service.invalidateAllCaches(LPSubject.CacheLevel.OPTION);
return CompletableFuture.completedFuture(true);
}
}
@Override
public CompletableFuture<Boolean> clearOptions(ImmutableContextSet contexts) {
Map<String, String> map = this.options.get(contexts);
if (map == null) {
return CompletableFuture.completedFuture(false);
}
this.options.remove(contexts);
this.service.invalidateAllCaches(LPSubject.CacheLevel.OPTION);
return CompletableFuture.completedFuture(!map.isEmpty());
}
private static Map<String, Boolean> processPermissionsMap(ImmutableContextSet filter, Map<ImmutableContextSet, Map<String, Boolean>> input) {
public Map<String, Boolean> resolvePermissions(ContextSet filter) {
// get relevant entries
SortedMap<ImmutableContextSet, Map<String, Boolean>> sorted = new TreeMap<>(ContextSetComparator.reverse());
for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> entry : input.entrySet()) {
for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> entry : this.permissions.entrySet()) {
if (!entry.getKey().isSatisfiedBy(filter)) {
continue;
}
@ -320,32 +139,257 @@ public class CalculatedSubjectData implements LPSubjectData {
result.putIfAbsent(e.getKey(), e.getValue());
}
}
return ImmutableMap.copyOf(result);
return result;
}
public Map<String, Boolean> resolvePermissions() {
// get relevant entries
SortedMap<ImmutableContextSet, Map<String, Boolean>> sorted = new TreeMap<>(ContextSetComparator.reverse());
for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> entry : this.permissions.entrySet()) {
sorted.put(entry.getKey(), entry.getValue());
}
// flatten
Map<String, Boolean> result = new HashMap<>();
for (Map<String, Boolean> map : sorted.values()) {
for (Map.Entry<String, Boolean> e : map.entrySet()) {
result.putIfAbsent(e.getKey(), e.getValue());
}
}
return result;
}
@Override
public CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate value) {
boolean b;
if (value == Tristate.UNDEFINED) {
Map<String, Boolean> perms = this.permissions.get(contexts);
b = perms != null && perms.remove(permission.toLowerCase()) != null;
} else {
Map<String, Boolean> perms = this.permissions.computeIfAbsent(contexts, c -> new ConcurrentHashMap<>());
b = !Objects.equals(perms.put(permission.toLowerCase(), value.asBoolean()), value.asBoolean());
}
if (b) {
this.service.invalidateAllCaches();
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> clearPermissions() {
if (this.permissions.isEmpty()) {
return CompletableFuture.completedFuture(false);
} else {
this.permissions.clear();
this.service.invalidateAllCaches();
return CompletableFuture.completedFuture(true);
}
}
@Override
public CompletableFuture<Boolean> clearPermissions(ImmutableContextSet contexts) {
Map<String, Boolean> perms = this.permissions.get(contexts);
if (perms == null) {
return CompletableFuture.completedFuture(false);
}
this.permissions.remove(contexts);
if (!perms.isEmpty()) {
this.service.invalidateAllCaches();
return CompletableFuture.completedFuture(true);
}
return CompletableFuture.completedFuture(false);
}
@Override
public ImmutableMap<ImmutableContextSet, ImmutableList<LPSubjectReference>> getAllParents() {
ImmutableMap.Builder<ImmutableContextSet, ImmutableList<LPSubjectReference>> map = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, Set<LPSubjectReference>> e : this.parents.entrySet()) {
map.put(e.getKey(), ImmutableList.sortedCopyOf(SubjectComparator.reverse(), e.getValue()));
}
return map.build();
}
public Set<LPSubjectReference> resolveParents(ContextSet filter) {
// get relevant entries
SortedMap<ImmutableContextSet, Set<LPSubjectReference>> sorted = new TreeMap<>(ContextSetComparator.reverse());
for (Map.Entry<ImmutableContextSet, Set<LPSubjectReference>> entry : this.parents.entrySet()) {
if (!entry.getKey().isSatisfiedBy(filter)) {
continue;
}
sorted.put(entry.getKey(), entry.getValue());
}
// flatten
Set<LPSubjectReference> result = new LinkedHashSet<>();
for (Set<LPSubjectReference> set : sorted.values()) {
for (LPSubjectReference e : set) {
if (!result.contains(e)) {
result.add(e);
}
}
}
return result;
}
public Set<LPSubjectReference> resolveParents() {
// get relevant entries
SortedMap<ImmutableContextSet, Set<LPSubjectReference>> sorted = new TreeMap<>(ContextSetComparator.reverse());
for (Map.Entry<ImmutableContextSet, Set<LPSubjectReference>> entry : this.parents.entrySet()) {
sorted.put(entry.getKey(), entry.getValue());
}
// flatten
Set<LPSubjectReference> result = new LinkedHashSet<>();
for (Set<LPSubjectReference> set : sorted.values()) {
for (LPSubjectReference e : set) {
if (!result.contains(e)) {
result.add(e);
}
}
}
return result;
}
@Override
public CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference parent) {
Set<LPSubjectReference> set = this.parents.computeIfAbsent(contexts, c -> ConcurrentHashMap.newKeySet());
boolean b = set.add(parent);
if (b) {
this.service.invalidateAllCaches();
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> removeParent(ImmutableContextSet contexts, LPSubjectReference parent) {
Set<LPSubjectReference> set = this.parents.get(contexts);
boolean b = set != null && set.remove(parent);
if (b) {
this.service.invalidateAllCaches();
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> clearParents() {
if (this.parents.isEmpty()) {
return CompletableFuture.completedFuture(false);
} else {
this.parents.clear();
this.service.invalidateAllCaches();
return CompletableFuture.completedFuture(true);
}
}
@Override
public CompletableFuture<Boolean> clearParents(ImmutableContextSet contexts) {
Set<LPSubjectReference> set = this.parents.get(contexts);
if (set == null) {
return CompletableFuture.completedFuture(false);
}
this.parents.remove(contexts);
this.service.invalidateAllCaches();
return CompletableFuture.completedFuture(!set.isEmpty());
}
@Override
public ImmutableMap<ImmutableContextSet, ImmutableMap<String, String>> getAllOptions() {
ImmutableMap.Builder<ImmutableContextSet, ImmutableMap<String, String>> map = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, Map<String, String>> e : this.options.entrySet()) {
map.put(e.getKey(), ImmutableMap.copyOf(e.getValue()));
}
return map.build();
}
public Map<String, String> resolveOptions(ContextSet filter) {
// get relevant entries
SortedMap<ImmutableContextSet, Map<String, String>> sorted = new TreeMap<>(ContextSetComparator.reverse());
for (Map.Entry<ImmutableContextSet, Map<String, String>> entry : this.options.entrySet()) {
if (!entry.getKey().isSatisfiedBy(filter)) {
continue;
}
sorted.put(entry.getKey(), entry.getValue());
}
// flatten
Map<String, String> result = new HashMap<>();
for (Map<String, String> map : sorted.values()) {
for (Map.Entry<String, String> e : map.entrySet()) {
result.putIfAbsent(e.getKey(), e.getValue());
}
}
return result;
}
public Map<String, String> resolveOptions() {
// get relevant entries
SortedMap<ImmutableContextSet, Map<String, String>> sorted = new TreeMap<>(ContextSetComparator.reverse());
for (Map.Entry<ImmutableContextSet, Map<String, String>> entry : this.options.entrySet()) {
sorted.put(entry.getKey(), entry.getValue());
}
// flatten
Map<String, String> result = new HashMap<>();
for (Map<String, String> map : sorted.values()) {
for (Map.Entry<String, String> e : map.entrySet()) {
result.putIfAbsent(e.getKey(), e.getValue());
}
}
return result;
}
@Override
public CompletableFuture<Boolean> setOption(ImmutableContextSet contexts, String key, String value) {
Map<String, String> options = this.options.computeIfAbsent(contexts, c -> new ConcurrentHashMap<>());
boolean b = !stringEquals(options.put(key.toLowerCase(), value), value);
if (b) {
this.service.invalidateAllCaches();
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> unsetOption(ImmutableContextSet contexts, String key) {
Map<String, String> options = this.options.get(contexts);
boolean b = options != null && options.remove(key.toLowerCase()) != null;
if (b) {
this.service.invalidateAllCaches();
}
return CompletableFuture.completedFuture(b);
}
@Override
public CompletableFuture<Boolean> clearOptions() {
if (this.options.isEmpty()) {
return CompletableFuture.completedFuture(false);
} else {
this.options.clear();
this.service.invalidateAllCaches();
return CompletableFuture.completedFuture(true);
}
}
@Override
public CompletableFuture<Boolean> clearOptions(ImmutableContextSet contexts) {
Map<String, String> map = this.options.get(contexts);
if (map == null) {
return CompletableFuture.completedFuture(false);
}
this.options.remove(contexts);
this.service.invalidateAllCaches();
return CompletableFuture.completedFuture(!map.isEmpty());
}
private static boolean stringEquals(String a, String b) {
return a == null && b == null || a != null && b != null && a.equalsIgnoreCase(b);
}
private static class CalculatorHolder {
private final PermissionCalculator calculator;
private final Map<String, Boolean> permissions;
public CalculatorHolder(PermissionCalculator calculator) {
this.calculator = calculator;
this.permissions = new ConcurrentHashMap<>();
this.calculator.setSourcePermissions(this.permissions);
}
public void setPermissions(Map<String, Boolean> permissions) {
this.permissions.clear();
this.permissions.putAll(permissions);
this.calculator.setSourcePermissions(this.permissions);
this.calculator.invalidateCache();
}
public PermissionCalculator getCalculator() {
return this.calculator;
}
}
}

View File

@ -44,8 +44,8 @@ public abstract class MonitoredSubjectData extends CalculatedSubjectData {
return b;
};
public MonitoredSubjectData(LPSubject subject, NodeMapType type, LuckPermsService service, String calculatorDisplayName) {
super(subject, type, service, calculatorDisplayName);
public MonitoredSubjectData(LPSubject subject, NodeMapType type, LuckPermsService service) {
super(subject, type, service);
}
protected abstract void onUpdate(boolean success);

View File

@ -0,0 +1,50 @@
/*
* 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.sponge.service.inheritance;
import me.lucko.luckperms.common.graph.Graph;
import me.lucko.luckperms.common.graph.GraphTraversers;
import me.lucko.luckperms.common.graph.TraversalAlgorithm;
import me.lucko.luckperms.sponge.service.calculated.CalculatedSubject;
/**
* A {@link Graph} which represents an "inheritance tree" for subjects.
*/
public interface SubjectInheritanceGraph extends Graph<CalculatedSubject> {
/**
* Returns an iterable which will traverse this inheritance graph using the
* specified algorithm starting at the given node.
*
* @param algorithm the algorithm to use when traversing
* @param startNode the start node in the inheritance graph
* @return an iterable
*/
default Iterable<CalculatedSubject> traverse(TraversalAlgorithm algorithm, CalculatedSubject startNode) {
return GraphTraversers.traverseUsing(algorithm, this, startNode);
}
}

View File

@ -0,0 +1,76 @@
/*
* 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.sponge.service.inheritance;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.sponge.service.calculated.CalculatedSubject;
import java.util.stream.Collectors;
public final class SubjectInheritanceGraphs {
private static final SubjectInheritanceGraph NON_CONTEXTUAL = new NonContextual();
public static SubjectInheritanceGraph getGraph() {
return NON_CONTEXTUAL;
}
public static SubjectInheritanceGraph getGraph(ImmutableContextSet contextSet) {
return new Contextual(contextSet);
}
private static final class NonContextual implements SubjectInheritanceGraph {
@Override
public Iterable<? extends CalculatedSubject> successors(CalculatedSubject subject) {
return subject.getCombinedParents().stream()
.filter(p -> p instanceof CalculatedSubject)
.map(p -> ((CalculatedSubject) p))
.collect(Collectors.toList());
}
}
private static final class Contextual implements SubjectInheritanceGraph {
/**
* The contexts to resolve inheritance in.
*/
private final ImmutableContextSet contextSet;
private Contextual(ImmutableContextSet contextSet) {
this.contextSet = contextSet;
}
@Override
public Iterable<? extends CalculatedSubject> successors(CalculatedSubject subject) {
return subject.getCombinedParents(this.contextSet).stream()
.filter(p -> p instanceof CalculatedSubject)
.map(p -> ((CalculatedSubject) p))
.collect(Collectors.toList());
}
}
private SubjectInheritanceGraphs() {}
}

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.sponge.service.internal;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.caching.MetaData;
@ -38,12 +37,15 @@ import me.lucko.luckperms.common.verbose.CheckOrigin;
import me.lucko.luckperms.sponge.LPSpongePlugin;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.ProxyFactory;
import me.lucko.luckperms.sponge.service.SubjectComparator;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.Subject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -70,6 +72,10 @@ public abstract class HolderSubject<T extends PermissionHolder> implements LPSub
});
}
public T getParent() {
return this.parent;
}
@Override
public Subject sponge() {
return ProxyFactory.toSponge(this);
@ -102,7 +108,7 @@ public abstract class HolderSubject<T extends PermissionHolder> implements LPSub
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
ImmutableSet.Builder<LPSubjectReference> subjects = ImmutableSet.builder();
List<LPSubjectReference> subjects = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : this.parent.getCachedData().getPermissionData(this.plugin.getContextManager().formContexts(contexts)).getImmutableBacking().entrySet()) {
if (!entry.getValue()) {
@ -122,7 +128,8 @@ public abstract class HolderSubject<T extends PermissionHolder> implements LPSub
subjects.addAll(getParentCollection().getDefaults().getParents(contexts));
subjects.addAll(this.plugin.getService().getDefaults().getParents(contexts));
return getService().sortSubjects(subjects.build());
subjects.sort(SubjectComparator.reverse());
return ImmutableList.copyOf(subjects);
}
@Override
@ -154,7 +161,7 @@ public abstract class HolderSubject<T extends PermissionHolder> implements LPSub
}
@Override
public void invalidateCaches(CacheLevel cacheLevel) {
public void invalidateCaches() {
// invalidate for all changes
this.parent.getCachedData().invalidateCaches();
}

View File

@ -58,6 +58,7 @@ import java.util.function.Predicate;
public class PersistedCollection implements LPSubjectCollection {
private final LuckPermsService service;
private final String identifier;
private final boolean defaultsCollection;
private final SubjectCollection spongeProxy;
@ -67,6 +68,7 @@ public class PersistedCollection implements LPSubjectCollection {
public PersistedCollection(LuckPermsService service, String identifier) {
this.service = service;
this.identifier = identifier;
this.defaultsCollection = identifier.equals("defaults");
this.spongeProxy = ProxyFactory.toSponge(this);
}
@ -100,6 +102,11 @@ public class PersistedCollection implements LPSubjectCollection {
return Predicates.alwaysTrue();
}
@Override
public boolean isDefaultsCollection() {
return this.defaultsCollection;
}
@Override
public CompletableFuture<LPSubject> loadSubject(String identifier) {
return CompletableFuture.completedFuture(this.subjects.get(identifier.toLowerCase()));

View File

@ -25,38 +25,27 @@
package me.lucko.luckperms.sponge.service.persisted;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.buffers.BufferedRequest;
import me.lucko.luckperms.common.model.NodeMapType;
import me.lucko.luckperms.common.verbose.CheckOrigin;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.ProxyFactory;
import me.lucko.luckperms.sponge.service.calculated.CalculatedSubject;
import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData;
import me.lucko.luckperms.sponge.service.calculated.MonitoredSubjectData;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectData;
import me.lucko.luckperms.sponge.service.reference.LPSubjectReference;
import me.lucko.luckperms.sponge.service.storage.SubjectStorageModel;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.service.permission.Subject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* A simple persistable Subject implementation
*/
public class PersistedSubject implements LPSubject {
public class PersistedSubject extends CalculatedSubject implements LPSubject {
private final String identifier;
private final LuckPermsService service;
@ -65,18 +54,6 @@ public class PersistedSubject implements LPSubject {
private final PersistedSubjectData subjectData;
private final CalculatedSubjectData transientSubjectData;
private final LoadingCache<PermissionLookupKey, Tristate> permissionLookupCache = Caffeine.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.build(lookup -> lookupPermissionValue(lookup.contexts, lookup.node));
private final LoadingCache<ImmutableContextSet, ImmutableList<LPSubjectReference>> parentLookupCache = Caffeine.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.build(this::lookupParents);
private final LoadingCache<OptionLookupKey, Optional<String>> optionLookupCache = Caffeine.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.build(lookup -> lookupOptionValue(lookup.contexts, lookup.key));
private final BufferedRequest<Void> saveBuffer = new BufferedRequest<Void>(1000L, 500L, r -> PersistedSubject.this.service.getPlugin().getBootstrap().getScheduler().doAsync(r)) {
@Override
protected Void perform() {
@ -90,12 +67,11 @@ public class PersistedSubject implements LPSubject {
};
public PersistedSubject(String identifier, LuckPermsService service, PersistedCollection parentCollection) {
super(service.getPlugin());
this.identifier = identifier;
this.service = service;
this.parentCollection = parentCollection;
String displayName = parentCollection.getIdentifier() + "/" + identifier;
this.subjectData = new PersistedSubjectData(this, NodeMapType.ENDURING, service, displayName + "/p") {
this.subjectData = new PersistedSubjectData(this, NodeMapType.ENDURING, service) {
@Override
protected void onUpdate(boolean success) {
super.onUpdate(success);
@ -104,7 +80,7 @@ public class PersistedSubject implements LPSubject {
}
}
};
this.transientSubjectData = new MonitoredSubjectData(this, NodeMapType.TRANSIENT, service, displayName + "/t") {
this.transientSubjectData = new MonitoredSubjectData(this, NodeMapType.TRANSIENT, service) {
@Override
protected void onUpdate(boolean success) {
if (success) {
@ -118,34 +94,6 @@ public class PersistedSubject implements LPSubject {
this.service.getPlugin().getUpdateEventHandler().fireUpdateEvent(subjectData);
}
@Override
public void invalidateCaches(CacheLevel type) {
this.optionLookupCache.invalidateAll();
if (type == CacheLevel.OPTION) {
return;
}
this.permissionLookupCache.invalidateAll();
this.subjectData.invalidateLookupCache();
this.transientSubjectData.invalidateLookupCache();
if (type == CacheLevel.PERMISSION) {
return;
}
this.parentLookupCache.invalidateAll();
}
@Override
public void performCleanup() {
this.subjectData.cleanup();
this.transientSubjectData.cleanup();
this.permissionLookupCache.cleanUp();
this.parentLookupCache.cleanUp();
this.optionLookupCache.cleanUp();
}
public void loadData(SubjectStorageModel dataHolder) {
this.subjectData.setSave(false);
dataHolder.applyToData(this.subjectData);
@ -190,193 +138,4 @@ public class PersistedSubject implements LPSubject {
public Optional<CommandSource> getCommandSource() {
return Optional.empty();
}
private Tristate lookupPermissionValue(ImmutableContextSet contexts, String node) {
Tristate res;
// if transient has priority
if (!this.parentCollection.getIdentifier().equals("defaults")) {
res = this.transientSubjectData.getPermissionValue(contexts, node);
if (res != Tristate.UNDEFINED) {
return res;
}
res = this.subjectData.getPermissionValue(contexts, node);
if (res != Tristate.UNDEFINED) {
return res;
}
} else {
res = this.subjectData.getPermissionValue(contexts, node);
if (res != Tristate.UNDEFINED) {
return res;
}
res = this.transientSubjectData.getPermissionValue(contexts, node);
if (res != Tristate.UNDEFINED) {
return res;
}
}
for (LPSubjectReference parent : getParents(contexts)) {
res = parent.resolveLp().join().getPermissionValue(contexts, node);
if (res != Tristate.UNDEFINED) {
return res;
}
}
if (getParentCollection().getIdentifier().equalsIgnoreCase("defaults")) {
return Tristate.UNDEFINED;
}
res = getParentCollection().getDefaults().getPermissionValue(contexts, node);
if (res != Tristate.UNDEFINED) {
return res;
}
res = this.service.getDefaults().getPermissionValue(contexts, node);
return res;
}
private ImmutableList<LPSubjectReference> lookupParents(ImmutableContextSet contexts) {
List<LPSubjectReference> s = new ArrayList<>();
s.addAll(this.subjectData.getParents(contexts));
s.addAll(this.transientSubjectData.getParents(contexts));
if (!getParentCollection().getIdentifier().equalsIgnoreCase("defaults")) {
s.addAll(getParentCollection().getDefaults().getParents(contexts));
s.addAll(this.service.getDefaults().getParents(contexts));
}
return this.service.sortSubjects(s);
}
private Optional<String> lookupOptionValue(ImmutableContextSet contexts, String key) {
Optional<String> res;
// if transient has priority
if (!this.parentCollection.getIdentifier().equals("defaults")) {
res = Optional.ofNullable(this.transientSubjectData.getOptions(contexts).get(key));
if (res.isPresent()) {
return res;
}
res = Optional.ofNullable(this.subjectData.getOptions(contexts).get(key));
if (res.isPresent()) {
return res;
}
} else {
res = Optional.ofNullable(this.subjectData.getOptions(contexts).get(key));
if (res.isPresent()) {
return res;
}
res = Optional.ofNullable(this.transientSubjectData.getOptions(contexts).get(key));
if (res.isPresent()) {
return res;
}
}
for (LPSubjectReference parent : getParents(contexts)) {
res = parent.resolveLp().join().getOption(contexts, key);
if (res.isPresent()) {
return res;
}
}
if (getParentCollection().getIdentifier().equalsIgnoreCase("defaults")) {
return Optional.empty();
}
res = getParentCollection().getDefaults().getOption(contexts, key);
if (res.isPresent()) {
return res;
}
return this.service.getDefaults().getOption(contexts, key);
}
@Override
public Tristate getPermissionValue(ImmutableContextSet contexts, String node) {
Objects.requireNonNull(contexts, "contexts");
Objects.requireNonNull(node, "node");
Tristate t = this.permissionLookupCache.get(new PermissionLookupKey(node, contexts));
this.service.getPlugin().getVerboseHandler().offerCheckData(CheckOrigin.INTERNAL, getParentCollection().getIdentifier() + "/" + this.identifier, contexts, node, t);
return t;
}
@Override
public boolean isChildOf(ImmutableContextSet contexts, LPSubjectReference subject) {
Objects.requireNonNull(contexts, "contexts");
Objects.requireNonNull(subject, "subject");
if (getParentCollection().getIdentifier().equalsIgnoreCase("defaults")) {
return this.subjectData.getParents(contexts).contains(subject) ||
this.transientSubjectData.getParents(contexts).contains(subject);
} else {
return this.subjectData.getParents(contexts).contains(subject) ||
this.transientSubjectData.getParents(contexts).contains(subject) ||
getParentCollection().getDefaults().getParents(contexts).contains(subject) ||
this.service.getDefaults().getParents(contexts).contains(subject);
}
}
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
Objects.requireNonNull(contexts, "contexts");
return this.parentLookupCache.get(contexts);
}
@Override
public Optional<String> getOption(ImmutableContextSet contexts, String key) {
return this.optionLookupCache.get(new OptionLookupKey(key, contexts));
}
private static final class PermissionLookupKey {
private final String node;
private final ImmutableContextSet contexts;
public PermissionLookupKey(String node, ImmutableContextSet contexts) {
this.node = node;
this.contexts = contexts;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof PermissionLookupKey)) return false;
final PermissionLookupKey other = (PermissionLookupKey) o;
return this.node.equals(other.node) && this.contexts.equals(other.contexts);
}
@Override
public int hashCode() {
return Objects.hash(this.node, this.contexts);
}
}
public static final class OptionLookupKey {
private final String key;
private final ImmutableContextSet contexts;
public OptionLookupKey(String key, ImmutableContextSet contexts) {
this.key = key;
this.contexts = contexts;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof OptionLookupKey)) return false;
final OptionLookupKey other = (OptionLookupKey) o;
return this.key.equals(other.key) && this.contexts.equals(other.contexts);
}
@Override
public int hashCode() {
return Objects.hash(this.key, this.contexts);
}
}
}

View File

@ -36,8 +36,8 @@ public class PersistedSubjectData extends MonitoredSubjectData {
private final PersistedSubject subject;
private boolean save = true;
public PersistedSubjectData(PersistedSubject subject, NodeMapType type, LuckPermsService service, String calculatorDisplayName) {
super(subject, type, service, calculatorDisplayName);
public PersistedSubjectData(PersistedSubject subject, NodeMapType type, LuckPermsService service) {
super(subject, type, service);
this.subject = subject;
}