Refactor storage type config read

This commit is contained in:
Luck 2019-04-23 21:57:43 +01:00
parent 494ab6787f
commit 2c62de9658
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 25 additions and 55 deletions

View File

@ -89,10 +89,8 @@ public class MigrationPowerfulPerms extends SubCommand<Object> {
return CommandResult.STATE_ERROR;
}
String method = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
StorageType type = StorageType.parse(method);
if (type == null || type != StorageType.MYSQL) {
StorageType type = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
if (type != StorageType.MYSQL) {
// We need to load the Hikari/MySQL stuff.
plugin.getDependencyManager().loadStorageDependencies(ImmutableSet.of(StorageType.MYSQL));
}

View File

@ -72,7 +72,7 @@ public class ApiConfiguration implements LPConfiguration {
@Override
public @NonNull String getStorageMethod() {
return this.handle.get(ConfigKeys.STORAGE_METHOD);
return this.handle.get(ConfigKeys.STORAGE_METHOD).getName();
}
@Override
@ -83,7 +83,7 @@ public class ApiConfiguration implements LPConfiguration {
@Override
public @NonNull Map<String, String> getSplitStorageOptions() {
return this.handle.get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream()
.collect(ImmutableCollectors.toMap(e -> e.getKey().name().toLowerCase(), Map.Entry::getValue));
.collect(ImmutableCollectors.toMap(e -> e.getKey().name().toLowerCase(), e -> e.getValue().getName()));
}
@Override

View File

@ -43,6 +43,7 @@ import me.lucko.luckperms.common.primarygroup.AllParentsByWeightHolder;
import me.lucko.luckperms.common.primarygroup.ParentsByWeightHolder;
import me.lucko.luckperms.common.primarygroup.PrimaryGroupHolder;
import me.lucko.luckperms.common.primarygroup.StoredHolder;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.storage.implementation.split.SplitStorageType;
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
import me.lucko.luckperms.common.util.ImmutableCollectors;
@ -474,7 +475,9 @@ public final class ConfigKeys {
/**
* The name of the storage method being used
*/
public static final ConfigKey<String> STORAGE_METHOD = enduringKey(lowercaseStringKey("storage-method", "h2"));
public static final ConfigKey<StorageType> STORAGE_METHOD = enduringKey(customKey(c -> {
return StorageType.parse(c.getString("storage-method", "h2"), StorageType.H2);
}));
/**
* If storage files should be monitored for changes
@ -489,13 +492,13 @@ public final class ConfigKeys {
/**
* The options for split storage
*/
public static final ConfigKey<Map<SplitStorageType, String>> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> {
EnumMap<SplitStorageType, String> map = new EnumMap<>(SplitStorageType.class);
map.put(SplitStorageType.USER, c.getString("split-storage.methods.user", "h2").toLowerCase());
map.put(SplitStorageType.GROUP, c.getString("split-storage.methods.group", "h2").toLowerCase());
map.put(SplitStorageType.TRACK, c.getString("split-storage.methods.track", "h2").toLowerCase());
map.put(SplitStorageType.UUID, c.getString("split-storage.methods.uuid", "h2").toLowerCase());
map.put(SplitStorageType.LOG, c.getString("split-storage.methods.log", "h2").toLowerCase());
public static final ConfigKey<Map<SplitStorageType, StorageType>> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> {
EnumMap<SplitStorageType, StorageType> map = new EnumMap<>(SplitStorageType.class);
map.put(SplitStorageType.USER, StorageType.parse(c.getString("split-storage.methods.user", "h2"), StorageType.H2));
map.put(SplitStorageType.GROUP, StorageType.parse(c.getString("split-storage.methods.group", "h2"), StorageType.H2));
map.put(SplitStorageType.TRACK, StorageType.parse(c.getString("split-storage.methods.track", "h2"), StorageType.H2));
map.put(SplitStorageType.UUID, StorageType.parse(c.getString("split-storage.methods.uuid", "h2"), StorageType.H2));
map.put(SplitStorageType.LOG, StorageType.parse(c.getString("split-storage.methods.log", "h2"), StorageType.H2));
return ImmutableMap.copyOf(map);
}));

View File

@ -115,7 +115,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
// now the configuration is loaded, we can create a storage factory and load initial dependencies
StorageFactory storageFactory = new StorageFactory(this);
Set<StorageType> storageTypes = storageFactory.getRequiredTypes(StorageType.H2);
Set<StorageType> storageTypes = storageFactory.getRequiredTypes();
this.dependencyManager.loadStorageDependencies(storageTypes);
// register listeners
@ -132,7 +132,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
}
// initialise storage
this.storage = storageFactory.getInstance(StorageType.H2);
this.storage = storageFactory.getInstance();
this.messagingService = provideMessagingFactory().getInstance();
// setup the update task buffer

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.common.storage;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
@ -59,45 +58,20 @@ public class StorageFactory {
this.plugin = plugin;
}
public Set<StorageType> getRequiredTypes(StorageType defaultMethod) {
public Set<StorageType> getRequiredTypes() {
if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) {
return this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream()
.map(e -> {
StorageType type = StorageType.parse(e.getValue());
if (type == null) {
this.plugin.getLogger().severe("Storage method for " + e.getKey() + " - " + e.getValue() + " not recognised. " +
"Using the default instead.");
type = defaultMethod;
}
return type;
})
.collect(ImmutableCollectors.toEnumSet(StorageType.class));
return ImmutableSet.copyOf(this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).values());
} else {
String method = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
StorageType type = StorageType.parse(method);
if (type == null) {
this.plugin.getLogger().severe("Storage method '" + method + "' not recognised. Using the default instead.");
type = defaultMethod;
}
return ImmutableSet.of(type);
return ImmutableSet.of(this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD));
}
}
public Storage getInstance(StorageType defaultMethod) {
public Storage getInstance() {
Storage storage;
if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) {
this.plugin.getLogger().info("Loading storage provider... [SPLIT STORAGE]");
Map<SplitStorageType, StorageType> mappedTypes = this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream()
.map(e -> {
StorageType type = StorageType.parse(e.getValue());
if (type == null) {
type = defaultMethod;
}
return Maps.immutableEntry(e.getKey(), type);
})
.collect(ImmutableCollectors.toEnumMap(SplitStorageType.class, Map.Entry::getKey, Map.Entry::getValue));
Map<SplitStorageType, StorageType> mappedTypes = this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS);
Map<StorageType, StorageImplementation> backing = mappedTypes.values().stream()
.distinct()
.collect(ImmutableCollectors.toEnumMap(StorageType.class, e -> e, this::createNewImplementation));
@ -106,12 +80,7 @@ public class StorageFactory {
storage = new Storage(this.plugin, new SplitStorage(this.plugin, backing, mappedTypes));
} else {
String method = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
StorageType type = StorageType.parse(method);
if (type == null) {
type = defaultMethod;
}
StorageType type = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
this.plugin.getLogger().info("Loading storage provider... [" + type.name() + "]");
storage = makeInstance(type);
}

View File

@ -63,7 +63,7 @@ public enum StorageType {
this.identifiers = ImmutableList.copyOf(identifiers);
}
public static StorageType parse(String name) {
public static StorageType parse(String name, StorageType def) {
for (StorageType t : values()) {
for (String id : t.getIdentifiers()) {
if (id.equalsIgnoreCase(name)) {
@ -71,7 +71,7 @@ public enum StorageType {
}
}
}
return null;
return def;
}
public String getName() {