Cache SubjectReference instances, general cleanup

This commit is contained in:
Luck
2017-09-21 21:59:27 +01:00
Unverified
parent f9efa15781
commit 777c972bdc
36 changed files with 732 additions and 780 deletions
@@ -188,7 +188,7 @@ public final class GroupDelegate extends PermissionHolderDelegate implements Gro
if (!(o instanceof GroupDelegate)) return false;
GroupDelegate other = (GroupDelegate) o;
return this.getName() == null ? other.getName() == null : this.getName().equals(other.getName());
return this.getName().equals(other.getName());
}
public int hashCode() {
@@ -117,7 +117,7 @@ public final class TrackDelegate implements Track {
if (!(o instanceof TrackDelegate)) return false;
TrackDelegate other = (TrackDelegate) o;
return this.getName() == null ? other.getName() == null : this.getName().equals(other.getName());
return this.getName().equals(other.getName());
}
public int hashCode() {
@@ -228,7 +228,7 @@ public final class UserDelegate extends PermissionHolderDelegate implements User
if (!(o instanceof UserDelegate)) return false;
UserDelegate other = (UserDelegate) o;
return this.getUuid() == null ? other.getUuid() == null : this.getUuid().equals(other.getUuid());
return this.getUuid().equals(other.getUuid());
}
public int hashCode() {
@@ -66,8 +66,8 @@ public final class ImmutableNode implements Node {
/*
* NODE STATE
*
* This are the actual node parameters, and are
* basically what this class wraps.
* This are the actual node attributes, and are
* really just what this class wraps.
*/
@Getter
@@ -83,53 +83,6 @@ public class MongoDBBacking extends AbstractBacking {
}
}
/* MongoDB does not allow '.' or '$' in key names.
See: https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names
The following two methods convert the node maps so they can be stored. */
private static final Function<String, String> CONVERT_STRING = s -> s.replace(".", "[**DOT**]").replace("$", "[**DOLLAR**]");
private static final Function<String, String> REVERT_STRING = s -> s.replace("[**DOT**]", ".").replace("[**DOLLAR**]", "$");
private static <V> Map<String, V> convert(Map<String, V> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> CONVERT_STRING.apply(e.getKey()), Map.Entry::getValue));
}
private static <V> Map<String, V> revert(Map<String, V> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> REVERT_STRING.apply(e.getKey()), Map.Entry::getValue));
}
private static Document fromUser(User user) {
Document main = new Document("_id", user.getUuid())
.append("name", user.getName().orElse("null"))
.append("primaryGroup", user.getPrimaryGroup().getStoredValue());
Document perms = new Document();
for (Map.Entry<String, Boolean> e : convert(exportToLegacy(user.getEnduringNodes().values())).entrySet()) {
perms.append(e.getKey(), e.getValue());
}
main.append("perms", perms);
return main;
}
private static Document fromGroup(Group group) {
Document main = new Document("_id", group.getName());
Document perms = new Document();
for (Map.Entry<String, Boolean> e : convert(exportToLegacy(group.getEnduringNodes().values())).entrySet()) {
perms.append(e.getKey(), e.getValue());
}
main.append("perms", perms);
return main;
}
private static Document fromTrack(Track track) {
return new Document("_id", track.getName()).append("groups", track.getGroups());
}
private final DatastoreConfiguration configuration;
private MongoClient mongoClient;
private MongoDatabase database;
@@ -752,6 +705,53 @@ public class MongoDBBacking extends AbstractBacking {
}, null);
}
/* MongoDB does not allow '.' or '$' in key names.
See: https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names
The following two methods convert the node maps so they can be stored. */
private static final Function<String, String> CONVERT_STRING = s -> s.replace(".", "[**DOT**]").replace("$", "[**DOLLAR**]");
private static final Function<String, String> REVERT_STRING = s -> s.replace("[**DOT**]", ".").replace("[**DOLLAR**]", "$");
private static <V> Map<String, V> convert(Map<String, V> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> CONVERT_STRING.apply(e.getKey()), Map.Entry::getValue));
}
private static <V> Map<String, V> revert(Map<String, V> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> REVERT_STRING.apply(e.getKey()), Map.Entry::getValue));
}
private static Document fromUser(User user) {
Document main = new Document("_id", user.getUuid())
.append("name", user.getName().orElse("null"))
.append("primaryGroup", user.getPrimaryGroup().getStoredValue());
Document perms = new Document();
for (Map.Entry<String, Boolean> e : convert(exportToLegacy(user.getEnduringNodes().values())).entrySet()) {
perms.append(e.getKey(), e.getValue());
}
main.append("perms", perms);
return main;
}
private static Document fromGroup(Group group) {
Document main = new Document("_id", group.getName());
Document perms = new Document();
for (Map.Entry<String, Boolean> e : convert(exportToLegacy(group.getEnduringNodes().values())).entrySet()) {
perms.append(e.getKey(), e.getValue());
}
main.append("perms", perms);
return main;
}
private static Document fromTrack(Track track) {
return new Document("_id", track.getName()).append("groups", track.getGroups());
}
public static Map<String, Boolean> exportToLegacy(Iterable<Node> nodes) {
Map<String, Boolean> m = new HashMap<>();
for (Node node : nodes) {
@@ -33,6 +33,11 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import java.util.concurrent.CompletableFuture;
/**
* System wide update task for LuckPerms.
*
* <p>Ensures that all local data is consistent with the storage.</p>
*/
@AllArgsConstructor
public class UpdateTask implements Runnable {
private final LuckPermsPlugin plugin;
@@ -43,7 +48,9 @@ public class UpdateTask implements Runnable {
private final boolean initialUpdate;
/**
* Called ASYNC
* Runs the update task
*
* <p>Called <b>async</b>.</p>
*/
@Override
public void run() {
@@ -72,6 +72,7 @@ public class PermissionVault implements Runnable {
for (String e; (e = queue.poll()) != null; ) {
try {
String s = e.toLowerCase();
// only attempt an insert if we're not seen this permission before
if (knownPermissions.add(s)) {
insert(s);
}
@@ -32,6 +32,9 @@ import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* A simple hikari wrapper
*/
@RequiredArgsConstructor
public class HikariSupplier implements AutoCloseable {
@@ -39,12 +39,24 @@ import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
/**
* Uploads content to GitHub's GIST service.
*/
public class PasteUtils {
private static final String GIST_API = "https://api.github.com/gists";
private static final String SHORTEN_API = "https://git.io";
/**
* Uploads content to GIST, and returns a shortened URL.
*
* @param desc the description of the gist
* @param files the files to include in the gist (file name --> content)
* @return the url, or null
*/
public static String paste(String desc, List<Map.Entry<String, String>> files) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection();
connection = (HttpURLConnection) new URL(GIST_API).openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
@@ -82,7 +94,7 @@ public class PasteUtils {
connection.disconnect();
try {
connection = (HttpURLConnection) new URL("https://git.io").openConnection();
connection = (HttpURLConnection) new URL(SHORTEN_API).openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
@@ -66,7 +66,7 @@ public class Predicates {
public static <T> Predicate<T> isOneOf(Set<T> ta) {
return t -> {
for (T i : ta) {
if (i == t) {
if (i.equals(t)) {
return true;
}
}
@@ -79,7 +79,7 @@ public class Predicates {
}
public static <T> Predicate<T> is(T t) {
return t2 -> t == t2;
return t::equals;
}
public static <T> Predicate<T> inverse(Predicate<T> t) {
@@ -30,6 +30,9 @@ import lombok.experimental.UtilityClass;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Nashorn provider utility
*/
@UtilityClass
public class Scripting {
private static ScriptEngine SCRIPT_ENGINE = null;
@@ -171,6 +171,8 @@ public class VerboseListener {
/**
* Uploads the captured data in this listener to a paste and returns the url
*
* @param showTraces if stack traces should be included in the output
* @param attachRaw if the rawdata should be attached to the gist
* @return the url
* @see PasteUtils#paste(String, List)
*/