From b997ab6e3fa0a8b26d466f1aa3c67e21e894edad Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 3 Apr 2017 10:57:29 +0100 Subject: [PATCH] Properly sort storage files - closes #227 --- .../common/commands/utils/ArgumentUtils.java | 16 +++++++++-- .../common/storage/backing/JSONBacking.java | 7 +++-- .../common/storage/backing/YAMLBacking.java | 13 +++------ .../utils/LegacyJSONSchemaMigration.java | 5 ++-- .../utils/LegacyYAMLSchemaMigration.java | 5 ++-- .../sponge/commands/SpongeUtils.java | 11 ++------ .../service/storage/SubjectStorageModel.java | 28 +++++++++++++++---- 7 files changed, 52 insertions(+), 33 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java b/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java index 3b295d21..e9ade7b7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java @@ -152,12 +152,22 @@ public class ArgumentUtils { continue; } - List keyValue = CONTEXT_SPLITTER.splitToList(pair); - if (keyValue.size() != 2) { + int index = pair.indexOf('='); + if (index == -1) { continue; } - set.add(keyValue.get(0), keyValue.get(1)); + String key = pair.substring(0, index); + if (key.equals("")) { + continue; + } + + String value = pair.substring(index + 1); + if (value.equals("")) { + continue; + } + + set.add(key, value); } return set; diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java index 5a24c86a..9e8ce4d9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java @@ -53,6 +53,7 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -171,7 +172,7 @@ public class JSONBacking extends FlatfileBacking { data.addProperty("name", user.getName()); data.addProperty("primaryGroup", user.getPrimaryGroup().getStoredValue()); - Set nodes = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set nodes = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("permissions", serializePermissions(nodes)); return writeElementToFile(userFile, data); @@ -270,7 +271,7 @@ public class JSONBacking extends FlatfileBacking { JsonObject data = new JsonObject(); data.addProperty("name", group.getName()); - Set nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("permissions", serializePermissions(nodes)); return writeElementToFile(groupFile, data); @@ -321,7 +322,7 @@ public class JSONBacking extends FlatfileBacking { JsonObject data = new JsonObject(); data.addProperty("name", group.getName()); - Set nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); data.add("permissions", serializePermissions(nodes)); return writeElementToFile(groupFile, data); }, false); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java index e7828d0c..930c681b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java @@ -28,7 +28,6 @@ import com.google.common.collect.Iterables; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.common.core.PriorityComparator; import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Track; @@ -52,6 +51,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -171,7 +171,7 @@ public class YAMLBacking extends FlatfileBacking { values.put("name", user.getName()); values.put("primary-group", user.getPrimaryGroup().getStoredValue()); - Set data = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set data = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); values.put("permissions", serializePermissions(data)); return writeMapToFile(userFile, values); @@ -268,7 +268,7 @@ public class YAMLBacking extends FlatfileBacking { Map values = new LinkedHashMap<>(); values.put("name", group.getName()); - Set data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); values.put("permissions", serializePermissions(data)); return writeMapToFile(groupFile, values); } @@ -318,7 +318,7 @@ public class YAMLBacking extends FlatfileBacking { Map values = new LinkedHashMap<>(); values.put("name", group.getName()); - Set data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); + Set data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new)); values.put("permissions", serializePermissions(data)); return writeMapToFile(groupFile, values); }, false); @@ -547,11 +547,6 @@ public class YAMLBacking extends FlatfileBacking { data.add(perm); } - data.sort((o1, o2) -> PriorityComparator.get().compareStrings( - Iterables.getFirst(o1.keySet(), ""), - Iterables.getFirst(o2.keySet(), "")) - ); - return data; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java index 3586c3d5..7a89ad31 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyJSONSchemaMigration.java @@ -35,6 +35,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; @@ -81,7 +82,7 @@ public class LegacyJSONSchemaMigration implements Runnable { Set nodes = perms.entrySet().stream() .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(NodeDataHolder::fromNode) - .collect(Collectors.toSet()); + .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { try { @@ -135,7 +136,7 @@ public class LegacyJSONSchemaMigration implements Runnable { Set nodes = perms.entrySet().stream() .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(NodeDataHolder::fromNode) - .collect(Collectors.toSet()); + .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { try { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java index 8475efe3..18a87244 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/LegacyYAMLSchemaMigration.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.nio.file.Files; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; @@ -76,7 +77,7 @@ public class LegacyYAMLSchemaMigration implements Runnable { Set nodes = perms.entrySet().stream() .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(NodeDataHolder::fromNode) - .collect(Collectors.toSet()); + .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { try { @@ -127,7 +128,7 @@ public class LegacyYAMLSchemaMigration implements Runnable { Set nodes = perms.entrySet().stream() .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(NodeDataHolder::fromNode) - .collect(Collectors.toSet()); + .collect(Collectors.toCollection(LinkedHashSet::new)); if (!replacementFile.exists()) { try { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java index f4f8ecd5..ad603659 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java @@ -27,6 +27,7 @@ import lombok.experimental.UtilityClass; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; import me.lucko.luckperms.sponge.service.references.SubjectReference; import java.util.List; @@ -86,15 +87,7 @@ public class SpongeUtils { } public static String contextToString(ContextSet set) { - if (set.isEmpty()) { - return "&bGLOBAL"; - } - - StringBuilder sb = new StringBuilder(); - for (Map.Entry e : set.toSet()) { - sb.append("&f").append(e.getKey()).append("&7=&f").append(e.getValue()).append("&7, "); - } - return sb.delete(sb.length() - 2, sb.length()).toString(); + return Util.contextSetToString(set); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java index 2e133a3c..1957f0a1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java @@ -35,6 +35,8 @@ import com.google.gson.JsonPrimitive; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet; +import me.lucko.luckperms.common.core.ContextSetComparator; +import me.lucko.luckperms.common.core.PriorityComparator; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; import me.lucko.luckperms.sponge.service.references.SubjectReference; @@ -163,12 +165,18 @@ public class SubjectStorageModel { } this.parents = parentsBuilder.build(); } + + private static List> sortContextMap(Map map) { + List> entries = new ArrayList<>(map.entrySet()); + entries.sort((o1, o2) -> ContextSetComparator.reverse().compare(o1.getKey(), o2.getKey())); + return entries; + } public JsonObject toJson() { JsonObject root = new JsonObject(); JsonArray permissions = new JsonArray(); - for (Map.Entry> e : this.permissions.entrySet()) { + for (Map.Entry> e : sortContextMap(this.permissions)) { if (e.getValue().isEmpty()) { continue; } @@ -177,7 +185,12 @@ public class SubjectStorageModel { section.add("context", contextsToJson(e.getKey())); JsonObject data = new JsonObject(); - for (Map.Entry ent : e.getValue().entrySet()) { + + // sort alphabetically. + List> perms = new ArrayList<>(e.getValue().entrySet()); + perms.sort((o1, o2) -> PriorityComparator.get().compareStrings(o1.getKey(), o2.getKey())); + + for (Map.Entry ent : perms) { data.addProperty(ent.getKey(), ent.getValue()); } section.add("data", data); @@ -187,7 +200,7 @@ public class SubjectStorageModel { root.add("permissions", permissions); JsonArray options = new JsonArray(); - for (Map.Entry> e : this.options.entrySet()) { + for (Map.Entry> e : sortContextMap(this.options)) { if (e.getValue().isEmpty()) { continue; } @@ -196,7 +209,12 @@ public class SubjectStorageModel { section.add("context", contextsToJson(e.getKey())); JsonObject data = new JsonObject(); - for (Map.Entry ent : e.getValue().entrySet()) { + + // sort alphabetically. + List> opts = new ArrayList<>(e.getValue().entrySet()); + opts.sort((o1, o2) -> PriorityComparator.get().compareStrings(o1.getKey(), o2.getKey())); + + for (Map.Entry ent : opts) { data.addProperty(ent.getKey(), ent.getValue()); } section.add("data", data); @@ -206,7 +224,7 @@ public class SubjectStorageModel { root.add("options", options); JsonArray parents = new JsonArray(); - for (Map.Entry> e : this.parents.entrySet()) { + for (Map.Entry> e : sortContextMap(this.parents)) { if (e.getValue().isEmpty()) { continue; }