Optimize Bukkit/Nukkit ChildProcessor refresh (#2952)

This commit is contained in:
Luck 2021-03-20 18:17:17 +00:00
parent 6b2c4bca58
commit 21f5c24847
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 32 additions and 20 deletions

View File

@ -33,8 +33,9 @@ import me.lucko.luckperms.common.calculator.result.TristateResult;
import net.luckperms.api.util.Tristate; import net.luckperms.api.util.Tristate;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* Permission Processor for Bukkits "child" permission system. * Permission Processor for Bukkits "child" permission system.
@ -43,6 +44,7 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi
private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(ChildProcessor.class); private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(ChildProcessor.class);
private final LPBukkitPlugin plugin; private final LPBukkitPlugin plugin;
private final AtomicBoolean needsRefresh = new AtomicBoolean(false);
private Map<String, TristateResult> childPermissions = Collections.emptyMap(); private Map<String, TristateResult> childPermissions = Collections.emptyMap();
public ChildProcessor(LPBukkitPlugin plugin) { public ChildProcessor(LPBukkitPlugin plugin) {
@ -51,23 +53,27 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi
@Override @Override
public TristateResult hasPermission(String permission) { public TristateResult hasPermission(String permission) {
if (this.needsRefresh.compareAndSet(true, false)) {
refresh();
}
return this.childPermissions.getOrDefault(permission, TristateResult.UNDEFINED); return this.childPermissions.getOrDefault(permission, TristateResult.UNDEFINED);
} }
@Override @Override
public void refresh() { public void refresh() {
Map<String, TristateResult> builder = new ConcurrentHashMap<>(); Map<String, TristateResult> childPermissions = new HashMap<>();
for (Map.Entry<String, Boolean> e : this.sourceMap.entrySet()) { this.sourceMap.forEach((key, value) -> {
Map<String, Boolean> children = this.plugin.getPermissionMap().getChildPermissions(e.getKey(), e.getValue()); Map<String, Boolean> children = this.plugin.getPermissionMap().getChildPermissions(key, value);
for (Map.Entry<String, Boolean> child : children.entrySet()) { children.forEach((childKey, childValue) -> {
builder.put(child.getKey(), RESULT_FACTORY.result(Tristate.of(child.getValue()), "parent: " + e.getKey())); childPermissions.put(childKey, RESULT_FACTORY.result(Tristate.of(childValue), "parent: " + key));
} });
} });
this.childPermissions = builder; this.childPermissions = childPermissions;
this.needsRefresh.set(false);
} }
@Override @Override
public void invalidate() { public void invalidate() {
refresh(); this.needsRefresh.set(true);
} }
} }

View File

@ -33,8 +33,9 @@ import me.lucko.luckperms.nukkit.LPNukkitPlugin;
import net.luckperms.api.util.Tristate; import net.luckperms.api.util.Tristate;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* Permission Processor for Nukkits "child" permission system. * Permission Processor for Nukkits "child" permission system.
@ -43,6 +44,7 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi
private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(ChildProcessor.class); private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(ChildProcessor.class);
private final LPNukkitPlugin plugin; private final LPNukkitPlugin plugin;
private final AtomicBoolean needsRefresh = new AtomicBoolean(false);
private Map<String, TristateResult> childPermissions = Collections.emptyMap(); private Map<String, TristateResult> childPermissions = Collections.emptyMap();
public ChildProcessor(LPNukkitPlugin plugin) { public ChildProcessor(LPNukkitPlugin plugin) {
@ -51,23 +53,27 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi
@Override @Override
public TristateResult hasPermission(String permission) { public TristateResult hasPermission(String permission) {
if (this.needsRefresh.compareAndSet(true, false)) {
refresh();
}
return this.childPermissions.getOrDefault(permission, TristateResult.UNDEFINED); return this.childPermissions.getOrDefault(permission, TristateResult.UNDEFINED);
} }
@Override @Override
public void refresh() { public void refresh() {
Map<String, TristateResult> builder = new ConcurrentHashMap<>(); Map<String, TristateResult> childPermissions = new HashMap<>();
for (Map.Entry<String, Boolean> e : this.sourceMap.entrySet()) { this.sourceMap.forEach((key, value) -> {
Map<String, Boolean> children = this.plugin.getPermissionMap().getChildPermissions(e.getKey(), e.getValue()); Map<String, Boolean> children = this.plugin.getPermissionMap().getChildPermissions(key, value);
for (Map.Entry<String, Boolean> child : children.entrySet()) { children.forEach((childKey, childValue) -> {
builder.put(child.getKey(), RESULT_FACTORY.result(Tristate.of(child.getValue()), "parent: " + e.getKey())); childPermissions.put(childKey, RESULT_FACTORY.result(Tristate.of(childValue), "parent: " + key));
} });
} });
this.childPermissions = builder; this.childPermissions = childPermissions;
this.needsRefresh.set(false);
} }
@Override @Override
public void invalidate() { public void invalidate() {
refresh(); this.needsRefresh.set(true);
} }
} }