Reduce the number of config lookups required when calculating contexts

This commit is contained in:
Luck 2018-05-12 01:51:38 +01:00
parent a927ca659f
commit 1639879106
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
8 changed files with 53 additions and 69 deletions

View File

@ -130,6 +130,14 @@ public class Contexts {
return new Contexts(contextSet.makeImmutable(), ImmutableSet.copyOf(settings)); return new Contexts(contextSet.makeImmutable(), ImmutableSet.copyOf(settings));
} }
private static EnumSet<LookupSetting> asEnumSet(Set<LookupSetting> settings) {
if (settings instanceof EnumSet<?>) {
return ((EnumSet<LookupSetting>) settings);
} else {
return EnumSet.copyOf(settings);
}
}
/** /**
* Creates a new {@link Contexts} instance. * Creates a new {@link Contexts} instance.
* *
@ -141,7 +149,7 @@ public class Contexts {
Objects.requireNonNull(contextSet, "contextSet"); Objects.requireNonNull(contextSet, "contextSet");
Objects.requireNonNull(settings, "settings"); Objects.requireNonNull(settings, "settings");
EnumSet<LookupSetting> settingsCopy = EnumSet.copyOf(settings); EnumSet<LookupSetting> settingsCopy = asEnumSet(settings);
if (contextSet.isEmpty() && DEFAULT_SETTINGS.equals(settingsCopy)) { if (contextSet.isEmpty() && DEFAULT_SETTINGS.equals(settingsCopy)) {
return GLOBAL; return GLOBAL;
} }

View File

@ -26,6 +26,7 @@
package me.lucko.luckperms.bukkit.contexts; package me.lucko.luckperms.bukkit.contexts;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LookupSetting;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.bukkit.LPBukkitPlugin;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
@ -33,6 +34,8 @@ import me.lucko.luckperms.common.contexts.AbstractContextManager;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.EnumSet;
public class BukkitContextManager extends AbstractContextManager<Player> { public class BukkitContextManager extends AbstractContextManager<Player> {
public BukkitContextManager(LPBukkitPlugin plugin) { public BukkitContextManager(LPBukkitPlugin plugin) {
super(plugin, Player.class); super(plugin, Player.class);
@ -40,14 +43,12 @@ public class BukkitContextManager extends AbstractContextManager<Player> {
@Override @Override
public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { public Contexts formContexts(Player subject, ImmutableContextSet contextSet) {
return Contexts.of( EnumSet<LookupSetting> settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS);
contextSet, if (subject.isOp()) {
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), settings = EnumSet.copyOf(settings);
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), settings.add(LookupSetting.IS_OP);
true, }
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), return Contexts.of(contextSet, settings);
subject.isOp()
);
} }
} }

View File

@ -28,7 +28,6 @@ package me.lucko.luckperms.bungee.contexts;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.bungee.LPBungeePlugin; import me.lucko.luckperms.bungee.LPBungeePlugin;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.contexts.AbstractContextManager; import me.lucko.luckperms.common.contexts.AbstractContextManager;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -40,14 +39,6 @@ public class BungeeContextManager extends AbstractContextManager<ProxiedPlayer>
@Override @Override
public Contexts formContexts(ProxiedPlayer subject, ImmutableContextSet contextSet) { public Contexts formContexts(ProxiedPlayer subject, ImmutableContextSet contextSet) {
return Contexts.of( return formContexts(contextSet);
contextSet,
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),
true,
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),
false
);
} }
} }

View File

@ -26,6 +26,7 @@
package me.lucko.luckperms.common.api.delegates.misc; package me.lucko.luckperms.common.api.delegates.misc;
import me.lucko.luckperms.api.LPConfiguration; import me.lucko.luckperms.api.LPConfiguration;
import me.lucko.luckperms.api.LookupSetting;
import me.lucko.luckperms.common.config.ConfigKey; import me.lucko.luckperms.common.config.ConfigKey;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.config.LuckPermsConfiguration;
@ -52,22 +53,22 @@ public class ApiConfiguration implements LPConfiguration {
@Override @Override
public boolean getIncludeGlobalPerms() { public boolean getIncludeGlobalPerms() {
return this.handle.get(ConfigKeys.INCLUDING_GLOBAL_PERMS); return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.INCLUDE_NODES_SET_WITHOUT_SERVER);
} }
@Override @Override
public boolean getIncludeGlobalWorldPerms() { public boolean getIncludeGlobalWorldPerms() {
return this.handle.get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS); return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.INCLUDE_NODES_SET_WITHOUT_WORLD);
} }
@Override @Override
public boolean getApplyGlobalGroups() { public boolean getApplyGlobalGroups() {
return this.handle.get(ConfigKeys.APPLYING_GLOBAL_GROUPS); return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.APPLY_PARENTS_SET_WITHOUT_SERVER);
} }
@Override @Override
public boolean getApplyGlobalWorldGroups() { public boolean getApplyGlobalWorldGroups() {
return this.handle.get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS); return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.APPLY_PARENTS_SET_WITHOUT_WORLD);
} }
@Nonnull @Nonnull

View File

@ -28,6 +28,9 @@ package me.lucko.luckperms.common.config;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LookupSetting;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.metastacking.MetaStackDefinition; import me.lucko.luckperms.api.metastacking.MetaStackDefinition;
import me.lucko.luckperms.common.assignments.AssignmentRule; import me.lucko.luckperms.common.assignments.AssignmentRule;
import me.lucko.luckperms.common.config.keys.AbstractKey; import me.lucko.luckperms.common.config.keys.AbstractKey;
@ -53,6 +56,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.EnumSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -89,24 +93,19 @@ public class ConfigKeys {
})); }));
/** /**
* If permissions without a server context should be included. * The lookup settings for contexts (care should be taken to not mutate this method)
*/ */
public static final ConfigKey<Boolean> INCLUDING_GLOBAL_PERMS = BooleanKey.of("include-global", true); public static final ConfigKey<EnumSet<LookupSetting>> LOOKUP_SETTINGS = AbstractKey.of(c -> {
return EnumSet.copyOf(Contexts.of(
/** ContextSet.empty(),
* If permissions without a world context should be included. c.getBoolean("include-global", true),
*/ c.getBoolean("include-global-world", true),
public static final ConfigKey<Boolean> INCLUDING_GLOBAL_WORLD_PERMS = BooleanKey.of("include-global-world", true); true,
c.getBoolean("apply-global-groups", true),
/** c.getBoolean("apply-global-world-groups", true),
* If groups without a server context should be included. false
*/ ).getSettings());
public static final ConfigKey<Boolean> APPLYING_GLOBAL_GROUPS = BooleanKey.of("apply-global-groups", true); });
/**
* If groups without a world context should be included.
*/
public static final ConfigKey<Boolean> APPLYING_GLOBAL_WORLD_GROUPS = BooleanKey.of("apply-global-world-groups", true);
/** /**
* # If the servers own UUID cache/lookup facility should be used when there is no record for a player in the LuckPerms cache. * # If the servers own UUID cache/lookup facility should be used when there is no record for a player in the LuckPerms cache.

View File

@ -140,15 +140,7 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
@Override @Override
public Contexts formContexts(ImmutableContextSet contextSet) { public Contexts formContexts(ImmutableContextSet contextSet) {
return Contexts.of( return Contexts.of(contextSet, this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS));
contextSet,
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),
true,
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),
false
);
} }
@Override @Override

View File

@ -26,6 +26,7 @@
package me.lucko.luckperms.nukkit.contexts; package me.lucko.luckperms.nukkit.contexts;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LookupSetting;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.contexts.AbstractContextManager; import me.lucko.luckperms.common.contexts.AbstractContextManager;
@ -33,6 +34,8 @@ import me.lucko.luckperms.nukkit.LPNukkitPlugin;
import cn.nukkit.Player; import cn.nukkit.Player;
import java.util.EnumSet;
public class NukkitContextManager extends AbstractContextManager<Player> { public class NukkitContextManager extends AbstractContextManager<Player> {
public NukkitContextManager(LPNukkitPlugin plugin) { public NukkitContextManager(LPNukkitPlugin plugin) {
super(plugin, Player.class); super(plugin, Player.class);
@ -40,14 +43,12 @@ public class NukkitContextManager extends AbstractContextManager<Player> {
@Override @Override
public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { public Contexts formContexts(Player subject, ImmutableContextSet contextSet) {
return Contexts.of( EnumSet<LookupSetting> settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS);
contextSet, if (subject.isOp()) {
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), settings = EnumSet.copyOf(settings);
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), settings.add(LookupSetting.IS_OP);
true, }
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), return Contexts.of(contextSet, settings);
subject.isOp()
);
} }
} }

View File

@ -27,7 +27,6 @@ package me.lucko.luckperms.sponge.contexts;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.contexts.AbstractContextManager; import me.lucko.luckperms.common.contexts.AbstractContextManager;
import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.LPSpongePlugin;
@ -40,14 +39,6 @@ public class SpongeContextManager extends AbstractContextManager<Subject> {
@Override @Override
public Contexts formContexts(Subject subject, ImmutableContextSet contextSet) { public Contexts formContexts(Subject subject, ImmutableContextSet contextSet) {
return Contexts.of( return formContexts(contextSet);
contextSet,
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),
true,
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),
false
);
} }
} }