Cache context lookups & refactor Sponge subjects

This commit is contained in:
Luck 2016-10-27 16:32:14 +01:00
parent 0198068a13
commit 0a4d337a53
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
11 changed files with 183 additions and 99 deletions

View File

@ -26,7 +26,6 @@ import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.bukkit.LPBukkitPlugin;
import me.lucko.luckperms.common.users.User;
import org.bukkit.Bukkit;
@ -67,7 +66,7 @@ public class LPPermissible extends PermissibleBase {
public Contexts calculateContexts() {
return new Contexts(
plugin.getContextManager().giveApplicableContext(parent, new MutableContextSet()),
plugin.getContextManager().getApplicableContext(parent),
plugin.getConfiguration().isIncludingGlobalPerms(),
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
true,

View File

@ -23,7 +23,6 @@
package me.lucko.luckperms.bungee;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.api.event.events.UserFirstLoginEvent;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.core.UuidCache;
@ -72,7 +71,7 @@ public class BungeeListener extends AbstractListener implements Listener {
}
Contexts contexts = new Contexts(
plugin.getContextManager().giveApplicableContext(player, MutableContextSet.empty()),
plugin.getContextManager().getApplicableContext(player),
plugin.getConfiguration().isIncludingGlobalPerms(),
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
true,

View File

@ -22,19 +22,43 @@
package me.lucko.luckperms.common.contexts;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import me.lucko.luckperms.api.context.ContextListener;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.IContextCalculator;
import me.lucko.luckperms.api.context.MutableContextSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
public class ContextManager<T> {
private final List<IContextCalculator<T>> calculators = new CopyOnWriteArrayList<>();
private final List<ContextListener<T>> listeners = new CopyOnWriteArrayList<>();
private final LoadingCache<T, ContextSet> cache = CacheBuilder.newBuilder()
.expireAfterWrite(50L, TimeUnit.MILLISECONDS)
.build(new CacheLoader<T, ContextSet>() {
@Override
public ContextSet load(T t) {
return calculateApplicableContext(t, MutableContextSet.empty()).makeImmutable();
}
});
private MutableContextSet calculateApplicableContext(T subject, MutableContextSet accumulator) {
for (IContextCalculator<T> calculator : calculators) {
calculator.giveApplicableContext(subject, accumulator);
}
return accumulator;
}
public ContextSet getApplicableContext(T subject) {
return cache.getUnchecked(subject);
}
public void registerCalculator(IContextCalculator<T> calculator) {
listeners.forEach(calculator::addListener);
calculators.add(calculator);
@ -47,20 +71,4 @@ public class ContextManager<T> {
listeners.add(listener);
}
public MutableContextSet giveApplicableContext(T subject, MutableContextSet accumulator) {
for (IContextCalculator<T> calculator : calculators) {
calculator.giveApplicableContext(subject, accumulator);
}
return accumulator;
}
public boolean isContextApplicable(T subject, Map.Entry<String, String> context) {
for (IContextCalculator<T> calculator : calculators) {
if (calculator.isContextApplicable(subject, context)) {
return true;
}
}
return false;
}
}

View File

@ -27,7 +27,6 @@ import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.utils.AbstractListener;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.network.ClientConnectionEvent;
@ -76,7 +75,7 @@ public class SpongeListener extends AbstractListener {
// Attempt to pre-process some permissions for the user to save time later. Might not work, but it's better than nothing.
Optional<Player> p = e.getCause().first(Player.class);
if (p.isPresent()) {
MutableContextSet context = plugin.getContextManager().giveApplicableContext(p.get(), MutableContextSet.empty());
MutableContextSet context = MutableContextSet.fromSet(plugin.getContextManager().getApplicableContext(p.get()));
List<String> worlds = plugin.getGame().getServer().getWorlds().stream()
.map(World::getName)
@ -84,13 +83,13 @@ public class SpongeListener extends AbstractListener {
plugin.doAsync(() -> {
UserData data = user.getUserData();
data.preCalculate(plugin.getService().calculateContexts(LuckPermsService.convertContexts(context)));
data.preCalculate(plugin.getService().calculateContexts(context));
for (String world : worlds) {
MutableContextSet modified = MutableContextSet.fromSet(context);
modified.removeAll("world");
modified.add("world", world);
data.preCalculate(plugin.getService().calculateContexts(LuckPermsService.convertContexts(modified)));
data.preCalculate(plugin.getService().calculateContexts(modified));
}
});
}

View File

@ -129,7 +129,7 @@ public class MigrationPermissionsEx extends SubCommand<Object> {
contexts.removeAll("world");
for (Map.Entry<String, String> opt : e.getValue().entrySet()) {
if (opt.getKey().equalsIgnoreCase("prefix") || opt.getKey().equalsIgnoreCase("prefix")) {
if (opt.getKey().equalsIgnoreCase("prefix") || opt.getKey().equalsIgnoreCase("suffix")) {
try {
holder.setPermission(new Node.Builder(opt.getKey().toLowerCase() + ".100." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build());
} catch (ObjectAlreadyHasException ignored) {}

View File

@ -23,15 +23,14 @@
package me.lucko.luckperms.sponge.service;
import com.google.common.collect.ImmutableList;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.api.LocalizedNode;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.groups.Group;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.NodeTree;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection;
@ -40,26 +39,22 @@ import org.spongepowered.api.util.Tristate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import static me.lucko.luckperms.api.MetaUtils.unescapeCharacters;
@EqualsAndHashCode(of = "group")
public class LuckPermsGroupSubject implements Subject {
@Getter
@EqualsAndHashCode(of = "group", callSuper = false)
public class LuckPermsGroupSubject extends LuckPermsSubject {
public static LuckPermsGroupSubject wrapGroup(Group group, LuckPermsService service) {
return new LuckPermsGroupSubject(group, service);
}
@Getter
private Group group;
@Getter(value = AccessLevel.NONE)
private LuckPermsService service;
@Getter
private Group group;
private LuckPermsSubjectData subjectData;
@Getter
private LuckPermsSubjectData transientSubjectData;
private LuckPermsGroupSubject(Group group, LuckPermsService service) {
@ -85,37 +80,32 @@ public class LuckPermsGroupSubject implements Subject {
}
@Override
public boolean hasPermission(@NonNull Set<Context> contexts, @NonNull String node) {
return getPermissionValue(contexts, node).asBoolean();
}
@Override
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String node) {
protected Tristate getPermissionValue(ContextSet contexts, String permission) {
Map<String, Boolean> permissions = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
.map(LocalizedNode::getNode)
.collect(Collectors.toMap(Node::getPermission, Node::getValue));
Tristate t = NodeTree.of(permissions).get(node);
Tristate t = NodeTree.of(permissions).get(permission);
if (t != Tristate.UNDEFINED) {
return t;
}
t = service.getGroupSubjects().getDefaults().getPermissionValue(contexts, node);
t = service.getGroupSubjects().getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission);
if (t != Tristate.UNDEFINED) {
return t;
}
t = service.getDefaults().getPermissionValue(contexts, node);
t = service.getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission);
return t;
}
@Override
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
public boolean isChildOf(ContextSet contexts, Subject parent) {
return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
}
@Override
public List<Subject> getParents(@NonNull Set<Context> contexts) {
public List<Subject> getParents(ContextSet contexts) {
List<Subject> subjects = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
.map(LocalizedNode::getNode)
.filter(Node::isGroupNode)
@ -123,43 +113,43 @@ public class LuckPermsGroupSubject implements Subject {
.map(s -> service.getGroupSubjects().get(s))
.collect(Collectors.toList());
subjects.addAll(service.getGroupSubjects().getDefaults().getParents(contexts));
subjects.addAll(service.getDefaults().getParents(contexts));
subjects.addAll(service.getGroupSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
subjects.addAll(service.getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
return ImmutableList.copyOf(subjects);
}
@Override
public Optional<String> getOption(Set<Context> set, String s) {
public Optional<String> getOption(ContextSet contexts, String s) {
Optional<String> option;
if (s.equalsIgnoreCase("prefix")) {
option = getChatMeta(set, true);
option = getChatMeta(contexts, true);
} else if (s.equalsIgnoreCase("suffix")) {
option = getChatMeta(set, false);
option = getChatMeta(contexts, false);
} else {
option = getMeta(set, s);
option = getMeta(contexts, s);
}
if (option.isPresent()) {
return option;
}
option = service.getGroupSubjects().getDefaults().getOption(set, s);
option = service.getGroupSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
if (option.isPresent()) {
return option;
}
return service.getDefaults().getOption(set, s);
return service.getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
}
@Override
public Set<Context> getActiveContexts() {
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
public ContextSet getActiveContextSet() {
return service.getPlugin().getContextManager().getApplicableContext(this);
}
private Optional<String> getChatMeta(Set<Context> contexts, boolean prefix) {
private Optional<String> getChatMeta(ContextSet contexts, boolean prefix) {
int priority = Integer.MIN_VALUE;
String meta = null;
@ -182,7 +172,7 @@ public class LuckPermsGroupSubject implements Subject {
return meta == null ? Optional.empty() : Optional.of(unescapeCharacters(meta));
}
private Optional<String> getMeta(Set<Context> contexts, String key) {
private Optional<String> getMeta(ContextSet contexts, String key) {
for (Node n : group.getAllNodesFiltered(service.calculateContexts(contexts))) {
if (!n.getValue()) {
continue;

View File

@ -140,9 +140,9 @@ public class LuckPermsService implements PermissionService {
plugin.getContextManager().registerCalculator(new SpongeCalculatorLink(contextCalculator));
}
public Contexts calculateContexts(Set<Context> contexts) {
public Contexts calculateContexts(ContextSet contextSet) {
return new Contexts(
LuckPermsService.convertContexts(contexts),
contextSet,
plugin.getConfiguration().isIncludingGlobalPerms(),
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
true,

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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 lombok.NonNull;
import me.lucko.luckperms.api.context.ContextSet;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.util.Tristate;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public abstract class LuckPermsSubject implements Subject {
protected abstract Tristate getPermissionValue(ContextSet contexts, String permission);
protected abstract boolean isChildOf(ContextSet contexts, Subject parent);
protected abstract List<Subject> getParents(ContextSet contexts);
protected abstract Optional<String> getOption(ContextSet contexts, String s);
protected abstract ContextSet getActiveContextSet();
@Override
@Deprecated
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) {
return getPermissionValue(LuckPermsService.convertContexts(contexts), permission);
}
@Override
@Deprecated
public boolean hasPermission(@NonNull Set<Context> contexts, @NonNull String permission) {
return getPermissionValue(LuckPermsService.convertContexts(contexts), permission).asBoolean();
}
@Override
@Deprecated
public boolean hasPermission(@NonNull String permission) {
return getPermissionValue(getActiveContextSet(), permission).asBoolean();
}
@Override
@Deprecated
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
return isChildOf(LuckPermsService.convertContexts(contexts), parent);
}
@Override
@Deprecated
public boolean isChildOf(@NonNull Subject parent) {
return isChildOf(getActiveContextSet(), parent);
}
@Override
@Deprecated
public List<Subject> getParents(@NonNull Set<Context> contexts) {
return getParents(LuckPermsService.convertContexts(contexts));
}
@Override
@Deprecated
public List<Subject> getParents() {
return getParents(getActiveContextSet());
}
@Override
@Deprecated
public Optional<String> getOption(@NonNull Set<Context> contexts, @NonNull String s) {
return getOption(LuckPermsService.convertContexts(contexts), s);
}
@Override
@Deprecated
public Optional<String> getOption(@NonNull String key) {
return getOption(getActiveContextSet(), key);
}
@Override
@Deprecated
public Set<Context> getActiveContexts() {
return LuckPermsService.convertContexts(getActiveContextSet());
}
}

View File

@ -23,40 +23,35 @@
package me.lucko.luckperms.sponge.service;
import com.google.common.collect.ImmutableList;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.api.caching.MetaData;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.users.User;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection;
import org.spongepowered.api.util.Tristate;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
@EqualsAndHashCode(of = "user")
public class LuckPermsUserSubject implements Subject {
@Getter
@EqualsAndHashCode(of = "user", callSuper = false)
public class LuckPermsUserSubject extends LuckPermsSubject {
public static LuckPermsUserSubject wrapUser(User user, LuckPermsService service) {
return new LuckPermsUserSubject(user, service);
}
@Getter
private User user;
@Getter(value = AccessLevel.NONE)
private LuckPermsService service;
@Getter
private User user;
private LuckPermsSubjectData subjectData;
@Getter
private LuckPermsSubjectData transientSubjectData;
private LuckPermsUserSubject(User user, LuckPermsService service) {
@ -102,26 +97,19 @@ public class LuckPermsUserSubject implements Subject {
}
@Override
public boolean hasPermission(Set<Context> contexts, String permission) {
return getPermissionValue(contexts, permission).asBoolean();
protected Tristate getPermissionValue(ContextSet contexts, String permission) {
return !hasData() ?
Tristate.UNDEFINED :
LuckPermsService.convertTristate(user.getUserData().getPermissionData(service.calculateContexts(contexts)).getPermissionValue(permission));
}
@Override
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) {
if (hasData()) {
return LuckPermsService.convertTristate(user.getUserData().getPermissionData(service.calculateContexts(contexts)).getPermissionValue(permission));
}
return Tristate.UNDEFINED;
}
@Override
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
protected boolean isChildOf(ContextSet contexts, Subject parent) {
return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
}
@Override
public List<Subject> getParents(Set<Context> contexts) {
protected List<Subject> getParents(ContextSet contexts) {
ImmutableList.Builder<Subject> subjects = ImmutableList.builder();
if (hasData()) {
@ -137,14 +125,14 @@ public class LuckPermsUserSubject implements Subject {
}
}
subjects.addAll(service.getUserSubjects().getDefaults().getParents(contexts));
subjects.addAll(service.getDefaults().getParents(contexts));
subjects.addAll(service.getUserSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
subjects.addAll(service.getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
return subjects.build();
}
@Override
public Optional<String> getOption(Set<Context> contexts, String s) {
protected Optional<String> getOption(ContextSet contexts, String s) {
if (hasData()) {
MetaData data = user.getUserData().getMetaData(service.calculateContexts(contexts));
if (s.equalsIgnoreCase("prefix")) {
@ -164,16 +152,16 @@ public class LuckPermsUserSubject implements Subject {
}
}
Optional<String> v = service.getUserSubjects().getDefaults().getOption(contexts, s);
Optional<String> v = service.getUserSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
if (v.isPresent()) {
return v;
}
return service.getDefaults().getOption(contexts, s);
return service.getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
}
@Override
public Set<Context> getActiveContexts() {
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
protected ContextSet getActiveContextSet() {
return service.getPlugin().getContextManager().getApplicableContext(this);
}
}

View File

@ -25,7 +25,6 @@ package me.lucko.luckperms.sponge.service.persisted;
import com.google.common.collect.ImmutableList;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.utils.BufferedRequest;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import org.spongepowered.api.command.CommandSource;
@ -186,6 +185,6 @@ public class PersistedSubject implements Subject {
@Override
public Set<Context> getActiveContexts() {
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().getApplicableContext(this));
}
}

View File

@ -25,7 +25,6 @@ package me.lucko.luckperms.sponge.service.simple;
import com.google.common.collect.ImmutableList;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.service.context.Context;
@ -152,6 +151,6 @@ public class SimpleSubject implements Subject {
@Override
public Set<Context> getActiveContexts() {
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().getApplicableContext(this));
}
}