Cleanup sponge delegate classes

This commit is contained in:
Luck 2017-04-03 10:27:31 +01:00
parent 4cfd8a7e2c
commit 25f31d0bb8
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 212 additions and 145 deletions

View File

@ -992,33 +992,44 @@ public abstract class PermissionHolder {
/** /**
* Clear all of the holders permission nodes * Clear all of the holders permission nodes
*/ */
public void clearNodes() { public boolean clearNodes() {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
nodes.clear(); nodes.clear();
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
if (before.size() == after.size()) {
return false;
} }
public void clearNodes(ContextSet contextSet) { plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
}
public boolean clearNodes(ContextSet contextSet) {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
nodes.removeAll(contextSet.makeImmutable()); nodes.removeAll(contextSet.makeImmutable());
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
if (before.size() == after.size()) {
return false;
} }
public void clearParents(boolean giveDefault) { plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); return true;
}
public boolean clearParents(boolean giveDefault) {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
boolean b = nodes.values().removeIf(Node::isGroupNode); boolean b = nodes.values().removeIf(Node::isGroupNode);
if (!b) { if (!b) {
return; return false;
} }
} }
if (this instanceof User && giveDefault) { if (this instanceof User && giveDefault) {
@ -1027,19 +1038,20 @@ public abstract class PermissionHolder {
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
public void clearParents(ContextSet contextSet, boolean giveDefault) { public boolean clearParents(ContextSet contextSet, boolean giveDefault) {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
SortedSet<Node> nodes = this.nodes.get(contextSet.makeImmutable()); SortedSet<Node> nodes = this.nodes.get(contextSet.makeImmutable());
if (nodes == null) { if (nodes == null) {
return; return false;
} }
boolean b = nodes.removeIf(Node::isGroupNode); boolean b = nodes.removeIf(Node::isGroupNode);
if (!b) { if (!b) {
return; return false;
} }
} }
if (this instanceof User && giveDefault) { if (this instanceof User && giveDefault) {
@ -1048,80 +1060,90 @@ public abstract class PermissionHolder {
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
public void clearMeta() { public boolean clearMeta() {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
if (!nodes.values().removeIf(n -> n.isMeta() || n.isPrefix() || n.isSuffix())) { if (!nodes.values().removeIf(n -> n.isMeta() || n.isPrefix() || n.isSuffix())) {
return; return false;
} }
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
public void clearMeta(ContextSet contextSet) { public boolean clearMeta(ContextSet contextSet) {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
SortedSet<Node> nodes = this.nodes.get(contextSet.makeImmutable()); SortedSet<Node> nodes = this.nodes.get(contextSet.makeImmutable());
if (nodes == null) { if (nodes == null) {
return; return false;
} }
boolean b = nodes.removeIf(n -> n.isMeta() || n.isPrefix() || n.isSuffix()); boolean b = nodes.removeIf(n -> n.isMeta() || n.isPrefix() || n.isSuffix());
if (!b) { if (!b) {
return; return false;
} }
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
public void clearMetaKeys(String key, boolean temp) { public boolean clearMetaKeys(String key, boolean temp) {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
boolean b = this.nodes.values().removeIf(n -> n.isMeta() && (n.isTemporary() == temp) && n.getMeta().getKey().equalsIgnoreCase(key)); boolean b = this.nodes.values().removeIf(n -> n.isMeta() && (n.isTemporary() == temp) && n.getMeta().getKey().equalsIgnoreCase(key));
if (!b) { if (!b) {
return; return false;
} }
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
public void clearMetaKeys(String key, ContextSet contextSet, boolean temp) { public boolean clearMetaKeys(String key, ContextSet contextSet, boolean temp) {
ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getNodes().values());
synchronized (nodes) { synchronized (nodes) {
SortedSet<Node> nodes = this.nodes.get(contextSet.makeImmutable()); SortedSet<Node> nodes = this.nodes.get(contextSet.makeImmutable());
if (nodes == null) { if (nodes == null) {
return; return false;
} }
boolean b = nodes.removeIf(n -> n.isMeta() && (n.isTemporary() == temp) && n.getMeta().getKey().equalsIgnoreCase(key)); boolean b = nodes.removeIf(n -> n.isMeta() && (n.isTemporary() == temp) && n.getMeta().getKey().equalsIgnoreCase(key));
if (!b) { if (!b) {
return; return false;
} }
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getNodes().values());
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
public void clearTransientNodes() { public boolean clearTransientNodes() {
ImmutableSet<Node> before = ImmutableSet.copyOf(getTransientNodes().values()); ImmutableSet<Node> before = ImmutableSet.copyOf(getTransientNodes().values());
synchronized (transientNodes) { synchronized (transientNodes) {
transientNodes.clear(); transientNodes.clear();
} }
invalidateCache(); invalidateCache();
ImmutableSet<Node> after = ImmutableSet.copyOf(getTransientNodes().values()); ImmutableSet<Node> after = ImmutableSet.copyOf(getTransientNodes().values());
if (before.size() == after.size()) {
return false;
}
plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after);
return true;
} }
/** /**

View File

@ -154,9 +154,14 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
* Clear all of the users permission nodes * Clear all of the users permission nodes
*/ */
@Override @Override
public void clearNodes() { public boolean clearNodes() {
super.clearNodes(); boolean ret = super.clearNodes();
if (!ret) {
return false;
}
getPlugin().getUserManager().giveDefaultIfNeeded(this, false); getPlugin().getUserManager().giveDefaultIfNeeded(this, false);
return true;
} }
public void clearNodes(boolean giveDefault) { public void clearNodes(boolean giveDefault) {

View File

@ -29,7 +29,6 @@ import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils;
import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.NodeFactory;
import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.core.model.PermissionHolder;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.sponge.service.proxy.Util; import me.lucko.luckperms.sponge.service.proxy.Util;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
@ -46,7 +45,6 @@ import java.util.Set;
public class SpongeMigrationUtils { public class SpongeMigrationUtils {
public static void migrateSubject(Subject subject, PermissionHolder holder, int priority) { public static void migrateSubject(Subject subject, PermissionHolder holder, int priority) {
if (holder instanceof Group) { if (holder instanceof Group) {
MigrationUtils.setGroupWeight((Group) holder, priority); MigrationUtils.setGroupWeight((Group) holder, priority);
} }
@ -56,13 +54,8 @@ public class SpongeMigrationUtils {
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : perms.entrySet()) { for (Map.Entry<Set<Context>, Map<String, Boolean>> e : perms.entrySet()) {
ContextSet context = Util.convertContexts(e.getKey()); ContextSet context = Util.convertContexts(e.getKey());
ExtractedContexts extractedContexts = ExtractedContexts.generate(context);
ContextSet contexts = extractedContexts.getContextSet();
String server = extractedContexts.getServer();
String world = extractedContexts.getWorld();
for (Map.Entry<String, Boolean> perm : e.getValue().entrySet()) { for (Map.Entry<String, Boolean> perm : e.getValue().entrySet()) {
holder.setPermission(NodeFactory.newBuilder(perm.getKey()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(perm.getValue()).build()); holder.setPermission(NodeFactory.newBuilder(perm.getKey()).withExtraContext(context).setValue(perm.getValue()).build());
} }
} }
@ -71,18 +64,13 @@ public class SpongeMigrationUtils {
for (Map.Entry<Set<Context>, Map<String, String>> e : opts.entrySet()) { for (Map.Entry<Set<Context>, Map<String, String>> e : opts.entrySet()) {
ContextSet context = Util.convertContexts(e.getKey()); ContextSet context = Util.convertContexts(e.getKey());
ExtractedContexts extractedContexts = ExtractedContexts.generate(context);
ContextSet contexts = extractedContexts.getContextSet();
String server = extractedContexts.getServer();
String world = extractedContexts.getWorld();
for (Map.Entry<String, String> opt : e.getValue().entrySet()) { for (Map.Entry<String, String> opt : e.getValue().entrySet()) {
if (opt.getKey().equalsIgnoreCase("prefix")) { if (opt.getKey().equalsIgnoreCase("prefix")) {
holder.setPermission(NodeFactory.makePrefixNode(priority, opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); holder.setPermission(NodeFactory.makePrefixNode(priority, opt.getValue()).withExtraContext(context).setValue(true).build());
} else if (opt.getKey().equalsIgnoreCase("suffix")) { } else if (opt.getKey().equalsIgnoreCase("suffix")) {
holder.setPermission(NodeFactory.makeSuffixNode(priority, opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); holder.setPermission(NodeFactory.makeSuffixNode(priority, opt.getValue()).withExtraContext(context).setValue(true).build());
} else { } else {
holder.setPermission(NodeFactory.makeMetaNode(opt.getKey(), opt.getValue()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); holder.setPermission(NodeFactory.makeMetaNode(opt.getKey(), opt.getValue()).withExtraContext(context).setValue(true).build());
} }
} }
} }
@ -92,34 +80,29 @@ public class SpongeMigrationUtils {
for (Map.Entry<Set<Context>, List<Subject>> e : parents.entrySet()) { for (Map.Entry<Set<Context>, List<Subject>> e : parents.entrySet()) {
ContextSet context = Util.convertContexts(e.getKey()); ContextSet context = Util.convertContexts(e.getKey());
ExtractedContexts extractedContexts = ExtractedContexts.generate(context);
ContextSet contexts = extractedContexts.getContextSet();
String server = extractedContexts.getServer();
String world = extractedContexts.getWorld();
for (Subject s : e.getValue()) { for (Subject s : e.getValue()) {
if (!s.getContainingCollection().getIdentifier().equalsIgnoreCase(PermissionService.SUBJECTS_GROUP)) { if (!s.getContainingCollection().getIdentifier().equalsIgnoreCase(PermissionService.SUBJECTS_GROUP)) {
continue; // LuckPerms does not support persisting other subject types. continue; // LuckPerms does not support persisting other subject types.
} }
holder.setPermission(NodeFactory.newBuilder("group." + MigrationUtils.standardizeName(s.getIdentifier())).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); holder.setPermission(NodeFactory.newBuilder("group." + MigrationUtils.standardizeName(s.getIdentifier())).withExtraContext(context).setValue(true).build());
} }
} }
} }
public static void migrateSubjectData(SubjectData from, SubjectData to) { public static void migrateSubjectData(SubjectData from, SubjectData to) {
for (Map.Entry<Set<Context>, Map<String, String>> e : from.getAllOptions().entrySet()) {
for (Map.Entry<String, String> e1 : e.getValue().entrySet()) {
to.setOption(e.getKey(), e1.getKey(), e1.getValue());
}
}
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : from.getAllPermissions().entrySet()) { for (Map.Entry<Set<Context>, Map<String, Boolean>> e : from.getAllPermissions().entrySet()) {
for (Map.Entry<String, Boolean> e1 : e.getValue().entrySet()) { for (Map.Entry<String, Boolean> e1 : e.getValue().entrySet()) {
to.setPermission(e.getKey(), e1.getKey(), Tristate.fromBoolean(e1.getValue())); to.setPermission(e.getKey(), e1.getKey(), Tristate.fromBoolean(e1.getValue()));
} }
} }
for (Map.Entry<Set<Context>, Map<String, String>> e : from.getAllOptions().entrySet()) {
for (Map.Entry<String, String> e1 : e.getValue().entrySet()) {
to.setOption(e.getKey(), e1.getKey(), e1.getValue());
}
}
for (Map.Entry<Set<Context>, List<Subject>> e : from.getAllParents().entrySet()) { for (Map.Entry<Set<Context>, List<Subject>> e : from.getAllParents().entrySet()) {
for (Subject s : e.getValue()) { for (Subject s : e.getValue()) {
to.addParent(e.getKey(), s); to.addParent(e.getKey(), s);

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.caching.MetaAccumulator; import me.lucko.luckperms.common.caching.MetaAccumulator;
import me.lucko.luckperms.common.core.DataMutateResult;
import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.NodeFactory;
import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.core.model.PermissionHolder;
@ -48,8 +49,8 @@ import org.spongepowered.api.service.permission.PermissionService;
import co.aikar.timings.Timing; import co.aikar.timings.Timing;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -72,16 +73,19 @@ public class LuckPermsSubjectData implements LPSubjectData {
@Override @Override
public Map<ImmutableContextSet, Map<String, Boolean>> getPermissions() { public Map<ImmutableContextSet, Map<String, Boolean>> getPermissions() {
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_PERMISSIONS)) { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_PERMISSIONS)) {
Map<ImmutableContextSet, Map<String, Boolean>> perms = new HashMap<>(); Map<ImmutableContextSet, ImmutableMap.Builder<String, Boolean>> perms = new HashMap<>();
for (Node n : enduring ? holder.getNodes().values() : holder.getTransientNodes().values()) { for (Map.Entry<ImmutableContextSet, Collection<Node>> e : (enduring ? holder.getNodes() : holder.getTransientNodes()).asMap().entrySet()) {
ContextSet contexts = n.getFullContexts(); ImmutableMap.Builder<String, Boolean> results = ImmutableMap.builder();
perms.computeIfAbsent(contexts.makeImmutable(), cs -> new HashMap<>()).put(n.getPermission(), n.getValue()); for (Node n : e.getValue()) {
results.put(n.getPermission(), n.getValue());
}
perms.put(e.getKey(), results);
} }
ImmutableMap.Builder<ImmutableContextSet, Map<String, Boolean>> map = ImmutableMap.builder(); ImmutableMap.Builder<ImmutableContextSet, Map<String, Boolean>> map = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> e : perms.entrySet()) { for (Map.Entry<ImmutableContextSet, ImmutableMap.Builder<String, Boolean>> e : perms.entrySet()) {
map.put(e.getKey(), ImmutableMap.copyOf(e.getValue())); map.put(e.getKey(), e.getValue().build());
} }
return map.build(); return map.build();
} }
@ -127,10 +131,15 @@ public class LuckPermsSubjectData implements LPSubjectData {
@Override @Override
public boolean clearPermissions() { public boolean clearPermissions() {
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PERMISSIONS)) { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PERMISSIONS)) {
boolean ret;
if (enduring) { if (enduring) {
holder.clearNodes(); ret = holder.clearNodes();
} else { } else {
holder.clearTransientNodes(); ret = holder.clearTransientNodes();
}
if (!ret) {
return false;
} }
if (holder instanceof User) { if (holder instanceof User) {
@ -145,36 +154,50 @@ public class LuckPermsSubjectData implements LPSubjectData {
@Override @Override
public boolean clearPermissions(@NonNull ContextSet set) { public boolean clearPermissions(@NonNull ContextSet set) {
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PERMISSIONS)) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PERMISSIONS)) {
List<Node> toRemove = streamNodes(enduring) boolean ret;
if (enduring) {
ret = holder.clearNodes(set);
} else {
List<Node> toRemove = streamNodes(false)
.filter(n -> n.getFullContexts().equals(set)) .filter(n -> n.getFullContexts().equals(set))
.collect(Collectors.toList()); .collect(Collectors.toList());
toRemove.forEach(makeUnsetConsumer(enduring)); toRemove.forEach(makeUnsetConsumer(false));
ret = !toRemove.isEmpty();
}
if (!ret) {
return false;
}
if (holder instanceof User) { if (holder instanceof User) {
service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false);
} }
objectSave(holder); objectSave(holder);
return !toRemove.isEmpty(); return true;
} }
} }
@Override @Override
public Map<ImmutableContextSet, Set<SubjectReference>> getParents() { public Map<ImmutableContextSet, Set<SubjectReference>> getParents() {
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_PARENTS)) { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_PARENTS)) {
Map<ImmutableContextSet, Set<SubjectReference>> parents = new HashMap<>(); Map<ImmutableContextSet, ImmutableSet.Builder<SubjectReference>> parents = new HashMap<>();
for (Node n : enduring ? holder.getNodes().values() : holder.getTransientNodes().values()) { for (Map.Entry<ImmutableContextSet, Collection<Node>> e : (enduring ? holder.getNodes() : holder.getTransientNodes()).asMap().entrySet()) {
if (!n.isGroupNode()) continue; ImmutableSet.Builder<SubjectReference> results = ImmutableSet.builder();
for (Node n : e.getValue()) {
ContextSet contexts = n.getFullContexts(); if (n.isGroupNode()) {
parents.computeIfAbsent(contexts.makeImmutable(), cs -> new HashSet<>()).add(service.getGroupSubjects().get(n.getGroupName()).toReference()); results.add(service.getGroupSubjects().get(n.getGroupName()).toReference());
}
}
parents.put(e.getKey(), results);
} }
ImmutableMap.Builder<ImmutableContextSet, Set<SubjectReference>> map = ImmutableMap.builder(); ImmutableMap.Builder<ImmutableContextSet, Set<SubjectReference>> map = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, Set<SubjectReference>> e : parents.entrySet()) { for (Map.Entry<ImmutableContextSet, ImmutableSet.Builder<SubjectReference>> e : parents.entrySet()) {
map.put(e.getKey(), ImmutableSet.copyOf(e.getValue())); map.put(e.getKey(), e.getValue().build());
} }
return map.build(); return map.build();
} }
@ -185,17 +208,22 @@ public class LuckPermsSubjectData implements LPSubjectData {
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_ADD_PARENT)) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_ADD_PARENT)) {
if (subject.getCollection().equals(PermissionService.SUBJECTS_GROUP)) { if (subject.getCollection().equals(PermissionService.SUBJECTS_GROUP)) {
LPSubject permsSubject = subject.resolve(service); LPSubject permsSubject = subject.resolve(service);
DataMutateResult result;
if (enduring) { if (enduring) {
holder.setPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier()) result = holder.setPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier())
.withExtraContext(contexts) .withExtraContext(contexts)
.build()); .build());
} else { } else {
holder.setTransientPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier()) result = holder.setTransientPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier())
.withExtraContext(contexts) .withExtraContext(contexts)
.build()); .build());
} }
if (!result.asBoolean()) {
return false;
}
objectSave(holder); objectSave(holder);
return true; return true;
} }
@ -208,17 +236,22 @@ public class LuckPermsSubjectData implements LPSubjectData {
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_REMOVE_PARENT)) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_REMOVE_PARENT)) {
if (subject.getCollection().equals(PermissionService.SUBJECTS_GROUP)) { if (subject.getCollection().equals(PermissionService.SUBJECTS_GROUP)) {
LPSubject permsSubject = subject.resolve(service); LPSubject permsSubject = subject.resolve(service);
DataMutateResult result;
if (enduring) { if (enduring) {
holder.unsetPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier()) result = holder.unsetPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier())
.withExtraContext(contexts) .withExtraContext(contexts)
.build()); .build());
} else { } else {
holder.unsetTransientPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier()) result = holder.unsetTransientPermission(NodeFactory.newBuilder("group." + permsSubject.getIdentifier())
.withExtraContext(contexts) .withExtraContext(contexts)
.build()); .build());
} }
if (!result.asBoolean()) {
return false;
}
objectSave(holder); objectSave(holder);
return true; return true;
} }
@ -229,37 +262,58 @@ public class LuckPermsSubjectData implements LPSubjectData {
@Override @Override
public boolean clearParents() { public boolean clearParents() {
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PARENTS)) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PARENTS)) {
List<Node> toRemove = streamNodes(enduring) boolean ret;
if (enduring) {
ret = holder.clearParents(true);
} else {
List<Node> toRemove = streamNodes(false)
.filter(Node::isGroupNode) .filter(Node::isGroupNode)
.collect(Collectors.toList()); .collect(Collectors.toList());
toRemove.forEach(makeUnsetConsumer(enduring)); toRemove.forEach(makeUnsetConsumer(false));
ret = !toRemove.isEmpty();
if (holder instanceof User) { if (ret && holder instanceof User) {
service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false);
} }
}
if (!ret) {
return false;
}
objectSave(holder); objectSave(holder);
return !toRemove.isEmpty(); return true;
} }
} }
@Override @Override
public boolean clearParents(@NonNull ContextSet set) { public boolean clearParents(@NonNull ContextSet set) {
try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PARENTS)) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PARENTS)) {
List<Node> toRemove = streamNodes(enduring) boolean ret;
if (enduring) {
ret = holder.clearParents(set, true);
} else {
List<Node> toRemove = streamNodes(false)
.filter(Node::isGroupNode) .filter(Node::isGroupNode)
.filter(n -> n.getFullContexts().equals(set)) .filter(n -> n.getFullContexts().equals(set))
.collect(Collectors.toList()); .collect(Collectors.toList());
toRemove.forEach(makeUnsetConsumer(enduring)); toRemove.forEach(makeUnsetConsumer(false));
ret = !toRemove.isEmpty();
if (holder instanceof User) { if (ret && holder instanceof User) {
service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false);
} }
}
if (!ret) {
return false;
}
objectSave(holder); objectSave(holder);
return !toRemove.isEmpty(); return true;
} }
} }
@ -429,13 +483,21 @@ public class LuckPermsSubjectData implements LPSubjectData {
} }
private void objectSave(PermissionHolder t) { private void objectSave(PermissionHolder t) {
if (!enduring) {
// don't bother saving to primary storage. just refresh
if (t instanceof User) {
((User) t).getRefreshBuffer().request();
} else {
service.getPlugin().getUpdateTaskBuffer().request();
}
} else {
if (t instanceof User) { if (t instanceof User) {
service.getPlugin().getStorage().saveUser(((User) t)) service.getPlugin().getStorage().saveUser(((User) t))
.thenRunAsync(() -> ((User) t).getRefreshBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor()); .thenRunAsync(() -> ((User) t).getRefreshBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor());
} } else {
if (t instanceof Group) {
service.getPlugin().getStorage().saveGroup((Group) t) service.getPlugin().getStorage().saveGroup((Group) t)
.thenRunAsync(() -> service.getPlugin().getUpdateTaskBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor()); .thenRunAsync(() -> service.getPlugin().getUpdateTaskBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor());
} }
} }
} }
}

View File

@ -59,43 +59,6 @@ import java.util.concurrent.TimeUnit;
public class CalculatedSubjectData implements LPSubjectData { public class CalculatedSubjectData implements LPSubjectData {
private static final ContextComparator CONTEXT_COMPARATOR = new ContextComparator(); private static final ContextComparator CONTEXT_COMPARATOR = new ContextComparator();
private static <V> Map<String, V> flattenMap(ContextSet contexts, Map<ContextSet, Map<String, V>> source) {
Map<String, V> map = new HashMap<>();
SortedMap<ContextSet, Map<String, V>> ret = getRelevantEntries(contexts, source);
for (Map<String, V> m : ret.values()) {
for (Map.Entry<String, V> e : m.entrySet()) {
if (!map.containsKey(e.getKey())) {
map.put(e.getKey(), e.getValue());
}
}
}
return ImmutableMap.copyOf(map);
}
private static <K, V> SortedMap<ContextSet, Map<K, V>> getRelevantEntries(ContextSet set, Map<ContextSet, Map<K, V>> map) {
ImmutableSortedMap.Builder<ContextSet, Map<K, V>> perms = ImmutableSortedMap.orderedBy(CONTEXT_COMPARATOR);
loop:
for (Map.Entry<ContextSet, Map<K, V>> e : map.entrySet()) {
for (Map.Entry<String, String> c : e.getKey().toSet()) {
if (!set.has(c.getKey(), c.getValue())) {
continue loop;
}
}
perms.put(e.getKey().makeImmutable(), ImmutableMap.copyOf(e.getValue()));
}
return perms.build();
}
private static boolean stringEquals(String a, String b) {
return a == null && b == null || a != null && b != null && a.equalsIgnoreCase(b);
}
@Getter @Getter
private final LPSubject parentSubject; private final LPSubject parentSubject;
@ -342,6 +305,39 @@ public class CalculatedSubjectData implements LPSubjectData {
return !map.isEmpty(); return !map.isEmpty();
} }
private static <V> Map<String, V> flattenMap(ContextSet contexts, Map<ContextSet, Map<String, V>> source) {
Map<String, V> map = new HashMap<>();
SortedMap<ContextSet, Map<String, V>> ret = getRelevantEntries(contexts, source);
for (Map<String, V> m : ret.values()) {
for (Map.Entry<String, V> e : m.entrySet()) {
if (!map.containsKey(e.getKey())) {
map.put(e.getKey(), e.getValue());
}
}
}
return ImmutableMap.copyOf(map);
}
private static <K, V> SortedMap<ContextSet, Map<K, V>> getRelevantEntries(ContextSet set, Map<ContextSet, Map<K, V>> map) {
ImmutableSortedMap.Builder<ContextSet, Map<K, V>> perms = ImmutableSortedMap.orderedBy(CONTEXT_COMPARATOR);
for (Map.Entry<ContextSet, Map<K, V>> e : map.entrySet()) {
if (!e.getKey().isSatisfiedBy(set)) {
continue;
}
perms.put(e.getKey().makeImmutable(), ImmutableMap.copyOf(e.getValue()));
}
return perms.build();
}
private static boolean stringEquals(String a, String b) {
return a == null && b == null || a != null && b != null && a.equalsIgnoreCase(b);
}
private static class ContextComparator implements Comparator<ContextSet> { private static class ContextComparator implements Comparator<ContextSet> {
@Override @Override

View File

@ -32,13 +32,14 @@ import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@Getter @ToString(of = "collection")
@ToString @EqualsAndHashCode(of = "collection")
@EqualsAndHashCode
@RequiredArgsConstructor(staticName = "of") @RequiredArgsConstructor(staticName = "of")
public class SubjectCollectionReference { public class SubjectCollectionReference {
@Getter
private final String collection; private final String collection;
private WeakReference<LPSubjectCollection> ref = null; private WeakReference<LPSubjectCollection> ref = null;
public synchronized LPSubjectCollection resolve(LuckPermsService service) { public synchronized LPSubjectCollection resolve(LuckPermsService service) {

View File

@ -37,9 +37,8 @@ import org.spongepowered.api.service.permission.Subject;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
@Getter @ToString(of = {"collection", "identifier"})
@ToString @EqualsAndHashCode(of = {"collection", "identifier"})
@EqualsAndHashCode
@RequiredArgsConstructor(staticName = "of") @RequiredArgsConstructor(staticName = "of")
public class SubjectReference { public class SubjectReference {
public static SubjectReference deserialize(String s) { public static SubjectReference deserialize(String s) {
@ -51,7 +50,10 @@ public class SubjectReference {
return of(subject.getContainingCollection().getIdentifier(), subject.getIdentifier()); return of(subject.getContainingCollection().getIdentifier(), subject.getIdentifier());
} }
@Getter
private final String collection; private final String collection;
@Getter
private final String identifier; private final String identifier;
private WeakReference<LPSubject> ref = null; private WeakReference<LPSubject> ref = null;
@ -69,8 +71,4 @@ public class SubjectReference {
return s; return s;
} }
public String serialize() {
return collection + "/" + identifier;
}
} }