Properly sort storage files - closes #227

This commit is contained in:
Luck 2017-04-03 10:57:29 +01:00
parent 25f31d0bb8
commit b997ab6e3f
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 52 additions and 33 deletions

View File

@ -152,12 +152,22 @@ public class ArgumentUtils {
continue; continue;
} }
List<String> keyValue = CONTEXT_SPLITTER.splitToList(pair); int index = pair.indexOf('=');
if (keyValue.size() != 2) { if (index == -1) {
continue; 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; return set;

View File

@ -53,6 +53,7 @@ import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -171,7 +172,7 @@ public class JSONBacking extends FlatfileBacking {
data.addProperty("name", user.getName()); data.addProperty("name", user.getName());
data.addProperty("primaryGroup", user.getPrimaryGroup().getStoredValue()); data.addProperty("primaryGroup", user.getPrimaryGroup().getStoredValue());
Set<NodeDataHolder> nodes = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); Set<NodeDataHolder> nodes = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
data.add("permissions", serializePermissions(nodes)); data.add("permissions", serializePermissions(nodes));
return writeElementToFile(userFile, data); return writeElementToFile(userFile, data);
@ -270,7 +271,7 @@ public class JSONBacking extends FlatfileBacking {
JsonObject data = new JsonObject(); JsonObject data = new JsonObject();
data.addProperty("name", group.getName()); data.addProperty("name", group.getName());
Set<NodeDataHolder> nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); Set<NodeDataHolder> nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
data.add("permissions", serializePermissions(nodes)); data.add("permissions", serializePermissions(nodes));
return writeElementToFile(groupFile, data); return writeElementToFile(groupFile, data);
@ -321,7 +322,7 @@ public class JSONBacking extends FlatfileBacking {
JsonObject data = new JsonObject(); JsonObject data = new JsonObject();
data.addProperty("name", group.getName()); data.addProperty("name", group.getName());
Set<NodeDataHolder> nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); Set<NodeDataHolder> nodes = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
data.add("permissions", serializePermissions(nodes)); data.add("permissions", serializePermissions(nodes));
return writeElementToFile(groupFile, data); return writeElementToFile(groupFile, data);
}, false); }, false);

View File

@ -28,7 +28,6 @@ import com.google.common.collect.Iterables;
import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.HeldPermission;
import me.lucko.luckperms.api.Node; 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.UserIdentifier;
import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.Track; import me.lucko.luckperms.common.core.model.Track;
@ -52,6 +51,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -171,7 +171,7 @@ public class YAMLBacking extends FlatfileBacking {
values.put("name", user.getName()); values.put("name", user.getName());
values.put("primary-group", user.getPrimaryGroup().getStoredValue()); values.put("primary-group", user.getPrimaryGroup().getStoredValue());
Set<NodeDataHolder> data = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); Set<NodeDataHolder> data = user.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
values.put("permissions", serializePermissions(data)); values.put("permissions", serializePermissions(data));
return writeMapToFile(userFile, values); return writeMapToFile(userFile, values);
@ -268,7 +268,7 @@ public class YAMLBacking extends FlatfileBacking {
Map<String, Object> values = new LinkedHashMap<>(); Map<String, Object> values = new LinkedHashMap<>();
values.put("name", group.getName()); values.put("name", group.getName());
Set<NodeDataHolder> data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); Set<NodeDataHolder> data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
values.put("permissions", serializePermissions(data)); values.put("permissions", serializePermissions(data));
return writeMapToFile(groupFile, values); return writeMapToFile(groupFile, values);
} }
@ -318,7 +318,7 @@ public class YAMLBacking extends FlatfileBacking {
Map<String, Object> values = new LinkedHashMap<>(); Map<String, Object> values = new LinkedHashMap<>();
values.put("name", group.getName()); values.put("name", group.getName());
Set<NodeDataHolder> data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toSet()); Set<NodeDataHolder> data = group.getNodes().values().stream().map(NodeDataHolder::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
values.put("permissions", serializePermissions(data)); values.put("permissions", serializePermissions(data));
return writeMapToFile(groupFile, values); return writeMapToFile(groupFile, values);
}, false); }, false);
@ -547,11 +547,6 @@ public class YAMLBacking extends FlatfileBacking {
data.add(perm); data.add(perm);
} }
data.sort((o1, o2) -> PriorityComparator.get().compareStrings(
Iterables.getFirst(o1.keySet(), ""),
Iterables.getFirst(o2.keySet(), ""))
);
return data; return data;
} }
} }

View File

@ -35,6 +35,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -81,7 +82,7 @@ public class LegacyJSONSchemaMigration implements Runnable {
Set<NodeDataHolder> nodes = perms.entrySet().stream() Set<NodeDataHolder> nodes = perms.entrySet().stream()
.map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()))
.map(NodeDataHolder::fromNode) .map(NodeDataHolder::fromNode)
.collect(Collectors.toSet()); .collect(Collectors.toCollection(LinkedHashSet::new));
if (!replacementFile.exists()) { if (!replacementFile.exists()) {
try { try {
@ -135,7 +136,7 @@ public class LegacyJSONSchemaMigration implements Runnable {
Set<NodeDataHolder> nodes = perms.entrySet().stream() Set<NodeDataHolder> nodes = perms.entrySet().stream()
.map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()))
.map(NodeDataHolder::fromNode) .map(NodeDataHolder::fromNode)
.collect(Collectors.toSet()); .collect(Collectors.toCollection(LinkedHashSet::new));
if (!replacementFile.exists()) { if (!replacementFile.exists()) {
try { try {

View File

@ -33,6 +33,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -76,7 +77,7 @@ public class LegacyYAMLSchemaMigration implements Runnable {
Set<NodeDataHolder> nodes = perms.entrySet().stream() Set<NodeDataHolder> nodes = perms.entrySet().stream()
.map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()))
.map(NodeDataHolder::fromNode) .map(NodeDataHolder::fromNode)
.collect(Collectors.toSet()); .collect(Collectors.toCollection(LinkedHashSet::new));
if (!replacementFile.exists()) { if (!replacementFile.exists()) {
try { try {
@ -127,7 +128,7 @@ public class LegacyYAMLSchemaMigration implements Runnable {
Set<NodeDataHolder> nodes = perms.entrySet().stream() Set<NodeDataHolder> nodes = perms.entrySet().stream()
.map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue())) .map(e -> NodeFactory.fromSerialisedNode(e.getKey(), e.getValue()))
.map(NodeDataHolder::fromNode) .map(NodeDataHolder::fromNode)
.collect(Collectors.toSet()); .collect(Collectors.toCollection(LinkedHashSet::new));
if (!replacementFile.exists()) { if (!replacementFile.exists()) {
try { try {

View File

@ -27,6 +27,7 @@ import lombok.experimental.UtilityClass;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.commands.utils.ArgumentUtils; 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 me.lucko.luckperms.sponge.service.references.SubjectReference;
import java.util.List; import java.util.List;
@ -86,15 +87,7 @@ public class SpongeUtils {
} }
public static String contextToString(ContextSet set) { public static String contextToString(ContextSet set) {
if (set.isEmpty()) { return Util.contextSetToString(set);
return "&bGLOBAL";
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> 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();
} }
} }

View File

@ -35,6 +35,8 @@ import com.google.gson.JsonPrimitive;
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.api.context.MutableContextSet; 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.calculated.CalculatedSubjectData;
import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.service.references.SubjectReference;
@ -164,11 +166,17 @@ public class SubjectStorageModel {
this.parents = parentsBuilder.build(); this.parents = parentsBuilder.build();
} }
private static <T> List<Map.Entry<ImmutableContextSet, T>> sortContextMap(Map<ImmutableContextSet, T> map) {
List<Map.Entry<ImmutableContextSet, T>> entries = new ArrayList<>(map.entrySet());
entries.sort((o1, o2) -> ContextSetComparator.reverse().compare(o1.getKey(), o2.getKey()));
return entries;
}
public JsonObject toJson() { public JsonObject toJson() {
JsonObject root = new JsonObject(); JsonObject root = new JsonObject();
JsonArray permissions = new JsonArray(); JsonArray permissions = new JsonArray();
for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> e : this.permissions.entrySet()) { for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> e : sortContextMap(this.permissions)) {
if (e.getValue().isEmpty()) { if (e.getValue().isEmpty()) {
continue; continue;
} }
@ -177,7 +185,12 @@ public class SubjectStorageModel {
section.add("context", contextsToJson(e.getKey())); section.add("context", contextsToJson(e.getKey()));
JsonObject data = new JsonObject(); JsonObject data = new JsonObject();
for (Map.Entry<String, Boolean> ent : e.getValue().entrySet()) {
// sort alphabetically.
List<Map.Entry<String, Boolean>> perms = new ArrayList<>(e.getValue().entrySet());
perms.sort((o1, o2) -> PriorityComparator.get().compareStrings(o1.getKey(), o2.getKey()));
for (Map.Entry<String, Boolean> ent : perms) {
data.addProperty(ent.getKey(), ent.getValue()); data.addProperty(ent.getKey(), ent.getValue());
} }
section.add("data", data); section.add("data", data);
@ -187,7 +200,7 @@ public class SubjectStorageModel {
root.add("permissions", permissions); root.add("permissions", permissions);
JsonArray options = new JsonArray(); JsonArray options = new JsonArray();
for (Map.Entry<ImmutableContextSet, Map<String, String>> e : this.options.entrySet()) { for (Map.Entry<ImmutableContextSet, Map<String, String>> e : sortContextMap(this.options)) {
if (e.getValue().isEmpty()) { if (e.getValue().isEmpty()) {
continue; continue;
} }
@ -196,7 +209,12 @@ public class SubjectStorageModel {
section.add("context", contextsToJson(e.getKey())); section.add("context", contextsToJson(e.getKey()));
JsonObject data = new JsonObject(); JsonObject data = new JsonObject();
for (Map.Entry<String, String> ent : e.getValue().entrySet()) {
// sort alphabetically.
List<Map.Entry<String, String>> opts = new ArrayList<>(e.getValue().entrySet());
opts.sort((o1, o2) -> PriorityComparator.get().compareStrings(o1.getKey(), o2.getKey()));
for (Map.Entry<String, String> ent : opts) {
data.addProperty(ent.getKey(), ent.getValue()); data.addProperty(ent.getKey(), ent.getValue());
} }
section.add("data", data); section.add("data", data);
@ -206,7 +224,7 @@ public class SubjectStorageModel {
root.add("options", options); root.add("options", options);
JsonArray parents = new JsonArray(); JsonArray parents = new JsonArray();
for (Map.Entry<ImmutableContextSet, List<SubjectReference>> e : this.parents.entrySet()) { for (Map.Entry<ImmutableContextSet, List<SubjectReference>> e : sortContextMap(this.parents)) {
if (e.getValue().isEmpty()) { if (e.getValue().isEmpty()) {
continue; continue;
} }