Skip to content

Commit

Permalink
prepare to make a generic DataStorageBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Nov 13, 2024
1 parent c68b34f commit b302703
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.hsgamer.topper.agent.storage.number;

import me.hsgamer.topper.agent.storage.simple.converter.MapEntryConverter;

import java.util.Collections;
import java.util.Map;

public interface MapNumberEntryConverter<K> extends MapEntryConverter<K, Double> {
@Override
default Double toValue(Map<String, Object> map) {
Object object = map.get("value");
if (object instanceof Number) {
return ((Number) object).doubleValue();
} else {
try {
return Double.parseDouble(object.toString());
} catch (Exception e) {
return null;
}
}
}

@Override
default Map<String, Object> toRawValue(Double object) {
return Collections.singletonMap("value", object);
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
package me.hsgamer.topper.agent.storage.simple.builder;

import me.hsgamer.hscore.builder.Builder;
import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter;
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter;
import me.hsgamer.topper.agent.storage.simple.setting.DatabaseSetting;
import me.hsgamer.topper.agent.storage.simple.setting.DataStorageBuilderSetting;
import me.hsgamer.topper.agent.storage.simple.supplier.*;

import java.io.File;
import java.util.function.Supplier;
import java.util.function.Function;

public class DataStorageBuilder<K, V> extends Builder<Void, DataStorageSupplier<K, V>> {
private final File holderBaseFolder;
private final FlatEntryConverter<K, V> flatEntryConverter;
private final SqlEntryConverter<K, V> sqlEntryConverter;
private final Supplier<DataStorageSupplier<K, V>> defaultSupplier;
public class DataStorageBuilder<K, V> extends Builder<DataStorageBuilderSetting<K, V>, DataStorageSupplier<K, V>> {
private final Function<DataStorageBuilderSetting<K, V>, DataStorageSupplier<K, V>> defaultSupplier;

public DataStorageBuilder(
Supplier<DatabaseSetting> databaseSettingSupplier,
File holderBaseFolder,
FlatEntryConverter<K, V> flatEntryConverter,
SqlEntryConverter<K, V> sqlEntryConverter
) {
this.holderBaseFolder = holderBaseFolder;
this.flatEntryConverter = flatEntryConverter;
this.sqlEntryConverter = sqlEntryConverter;
this.defaultSupplier = () -> new FlatStorageSupplier<>(holderBaseFolder, flatEntryConverter);
public DataStorageBuilder() {
this.defaultSupplier = setting -> new FlatStorageSupplier<>(setting.getBaseFolder(), setting.getFlatEntryConverter());
register(defaultSupplier, "flat", "properties", "");
register(v -> new SqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "sqlite", "sqlite3");
register(v -> new NewSqliteStorageSupplier<>(databaseSettingSupplier.get(), holderBaseFolder, sqlEntryConverter), "new-sqlite", "new-sqlite3");
register(v -> new MySqlStorageSupplier<>(databaseSettingSupplier.get(), sqlEntryConverter), "mysql", "mysql-connector-java", "mysql-connector");
register(setting -> new SqliteStorageSupplier<>(setting.getDatabaseSetting(), setting.getBaseFolder(), setting.getSqlEntryConverter()), "sqlite", "sqlite3");
register(setting -> new NewSqliteStorageSupplier<>(setting.getDatabaseSetting(), setting.getBaseFolder(), setting.getSqlEntryConverter()), "new-sqlite", "new-sqlite3");
register(setting -> new MySqlStorageSupplier<>(setting.getDatabaseSetting(), setting.getSqlEntryConverter()), "mysql", "mysql-connector-java", "mysql-connector");
}

public DataStorageSupplier<K, V> buildSupplier(String type) {
return build(type, null).orElseGet(defaultSupplier);
}

public File getHolderBaseFolder() {
return holderBaseFolder;
}

public FlatEntryConverter<K, V> getFlatEntryConverter() {
return flatEntryConverter;
}

public SqlEntryConverter<K, V> getSqlEntryConverter() {
return sqlEntryConverter;
public DataStorageSupplier<K, V> buildSupplier(String type, DataStorageBuilderSetting<K, V> setting) {
return build(type, setting).orElseGet(() -> defaultSupplier.apply(setting));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.hsgamer.topper.agent.storage.simple.converter;

import java.util.Map;

public interface MapEntryConverter<K, V> {
K toKey(Map<String, Object> map);

V toValue(Map<String, Object> map);

Map<String, Object> toRawKey(K key);

Map<String, Object> toRawValue(V value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.hsgamer.topper.agent.storage.simple.setting;

import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter;
import me.hsgamer.topper.agent.storage.simple.converter.MapEntryConverter;
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter;

import java.io.File;

public interface DataStorageBuilderSetting<K, V> {
DatabaseSetting getDatabaseSetting();

File getBaseFolder();

FlatEntryConverter<K, V> getFlatEntryConverter();

MapEntryConverter<K, V> getMapEntryConverter();

SqlEntryConverter<K, V> getSqlEntryConverter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

public class FlatStorageSupplier<K, V> implements DataStorageSupplier<K, V> {
private final Logger logger = LoggerProvider.getLogger(getClass());
private final File holderBaseFolder;
private final File baseFolder;
private final FlatEntryConverter<K, V> converter;

public FlatStorageSupplier(File holderBaseFolder, FlatEntryConverter<K, V> converter) {
this.holderBaseFolder = holderBaseFolder;
public FlatStorageSupplier(File baseFolder, FlatEntryConverter<K, V> converter) {
this.baseFolder = baseFolder;
this.converter = converter;
}

@Override
public DataStorage<K, V> getStorage(String name) {
Properties properties = new Properties();
File file = new File(holderBaseFolder, name + ".properties");
File file = new File(baseFolder, name + ".properties");
Runnable loadRunnable = () -> {
try {
if (!file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.io.File;

public class NewSqliteStorageSupplier<K, V> extends SqliteStorageSupplier<K, V> {
public NewSqliteStorageSupplier(DatabaseSetting databaseSetting, File holderBaseFolder, SqlEntryConverter<K, V> converter) {
super(databaseSetting, holderBaseFolder, converter);
public NewSqliteStorageSupplier(DatabaseSetting databaseSetting, File baseFolder, SqlEntryConverter<K, V> converter) {
super(databaseSetting, baseFolder, converter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class SqliteStorageSupplier<K, V> extends SqlStorageSupplier<K, V> {
private final JavaSqlClient client;
private final AtomicReference<Connection> connectionReference = new AtomicReference<>();

public SqliteStorageSupplier(DatabaseSetting databaseSetting, File holderBaseFolder, SqlEntryConverter<K, V> converter) {
public SqliteStorageSupplier(DatabaseSetting databaseSetting, File baseHolder, SqlEntryConverter<K, V> converter) {
super(converter);
client = new JavaSqlClient(Setting.create(new SqliteFileDriver(holderBaseFolder)).setDatabaseName(databaseSetting.getDatabase()));
client = new JavaSqlClient(Setting.create(new SqliteFileDriver(baseHolder)).setDatabaseName(databaseSetting.getDatabase()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
package me.hsgamer.topper.spigot.agent.storage.simple;

import io.github.projectunified.minelib.scheduler.common.task.Task;
import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;
import me.hsgamer.hscore.bukkit.config.BukkitConfig;
import me.hsgamer.topper.agent.storage.simple.builder.DataStorageBuilder;
import me.hsgamer.topper.agent.storage.simple.converter.FlatEntryConverter;
import me.hsgamer.topper.agent.storage.simple.converter.SqlEntryConverter;
import me.hsgamer.topper.agent.storage.simple.setting.DatabaseSetting;
import me.hsgamer.topper.spigot.agent.storage.simple.supplier.ConfigStorageSupplier;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

public class SpigotDataStorageBuilder<K, V> extends DataStorageBuilder<K, V> {
private final UnaryOperator<Runnable> runTaskFunction;
private final Executor mainThreadExecutor;
public class SpigotDataStorageBuilder {
public static <K, V> void register(JavaPlugin plugin, DataStorageBuilder<K, V> builder) {
Executor mainThreadExecutor = runnable -> GlobalScheduler.get(plugin).run(runnable);

public SpigotDataStorageBuilder(JavaPlugin plugin, File holderBaseFolder, Supplier<DatabaseSetting> databaseSettingSupplier, FlatEntryConverter<K, V> flatEntryConverter, SqlEntryConverter<K, V> sqlEntryConverter) {
super(
databaseSettingSupplier,
holderBaseFolder,
flatEntryConverter,
sqlEntryConverter
);
this.runTaskFunction = runnable -> {
Task task = GlobalScheduler.get(plugin).runLater(runnable, 40L);
return task::cancel;
};
this.mainThreadExecutor = runnable -> GlobalScheduler.get(plugin).run(runnable);
register(v -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".yml", BukkitConfig::new, holderBaseFolder, flatEntryConverter), "config", "yaml", "yml");
register(v -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".json", BukkitConfig::new, holderBaseFolder, flatEntryConverter), "json");
}

public UnaryOperator<Runnable> getRunTaskFunction() {
return runTaskFunction;
}

public Executor getMainThreadExecutor() {
return mainThreadExecutor;
builder.register(setting -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".yml", BukkitConfig::new, setting.getBaseFolder(), setting.getFlatEntryConverter()), "config", "yaml", "yml");
builder.register(setting -> new ConfigStorageSupplier<>(mainThreadExecutor, name -> name + ".json", BukkitConfig::new, setting.getBaseFolder(), setting.getFlatEntryConverter()), "json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import me.hsgamer.hscore.bukkit.utils.MessageUtils;
import me.hsgamer.hscore.checker.spigotmc.SpigotVersionChecker;
import me.hsgamer.hscore.config.proxy.ConfigGenerator;
import me.hsgamer.topper.spigot.plugin.builder.NumberStorageBuilder;
import me.hsgamer.topper.spigot.plugin.builder.TopStorageBuilder;
import me.hsgamer.topper.spigot.plugin.builder.ValueProviderBuilder;
import me.hsgamer.topper.spigot.plugin.command.GetTopListCommand;
import me.hsgamer.topper.spigot.plugin.command.ReloadCommand;
import me.hsgamer.topper.spigot.plugin.config.DatabaseConfig;
import me.hsgamer.topper.spigot.plugin.config.MainConfig;
import me.hsgamer.topper.spigot.plugin.config.MessageConfig;
import me.hsgamer.topper.spigot.plugin.hook.HookSystem;
Expand All @@ -19,7 +18,6 @@
import me.hsgamer.topper.spigot.plugin.manager.TopQueryManager;
import org.bstats.bukkit.Metrics;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
Expand All @@ -28,11 +26,7 @@ public class TopperPlugin extends BasePlugin {
@Override
protected List<Object> getComponents() {
return Arrays.asList(
new NumberStorageBuilder(
this,
new File(getDataFolder(), "top"),
() -> ConfigGenerator.newInstance(DatabaseConfig.class, new BukkitConfig(this, "database.yml")).toDatabaseSetting()
),
new TopStorageBuilder(this),
new ValueProviderBuilder(),
ConfigGenerator.newInstance(MainConfig.class, new BukkitConfig(this)),
ConfigGenerator.newInstance(MessageConfig.class, new BukkitConfig(this, "messages.yml")),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.hsgamer.topper.spigot.plugin.builder;

import me.hsgamer.topper.agent.storage.simple.builder.DataStorageBuilder;
import me.hsgamer.topper.spigot.agent.storage.simple.SpigotDataStorageBuilder;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.UUID;

public class TopStorageBuilder extends DataStorageBuilder<UUID, Double> {
public TopStorageBuilder(JavaPlugin plugin) {
SpigotDataStorageBuilder.register(plugin, this);
}
}
Loading

0 comments on commit b302703

Please sign in to comment.