From 2321ed35c249c7224c0e0bc824c36cf82011c3ec Mon Sep 17 00:00:00 2001 From: carm Date: Mon, 19 Dec 2022 18:07:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E6=94=AF=E6=8C=81=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E5=88=9B=E5=BB=BA=E6=B3=A8=E5=86=8C=E6=B1=A0=EF=BC=8C?= =?UTF-8?q?=E4=BE=BF=E4=BA=8E=E6=8F=92=E4=BB=B6=E5=86=85=E9=83=A8=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/pom.xml | 2 +- .../java/cc/carm/plugin/minesql/IMineSQL.java | 2 + .../java/cc/carm/plugin/minesql/MineSQL.java | 9 +++ .../carm/plugin/minesql/api/SQLRegistry.java | 3 +- core/pom.xml | 2 +- .../cc/carm/plugin/minesql/MineSQLCore.java | 46 +++++++++++- .../carm/plugin/minesql/MineSQLRegistry.java | 70 ++----------------- .../minesql/command/MineSQLCommand.java | 3 +- platforms/bukkit/pom.xml | 2 +- platforms/bungee/pom.xml | 2 +- platforms/velocity/pom.xml | 2 +- plugin/pom.xml | 2 +- pom.xml | 2 +- 13 files changed, 70 insertions(+), 77 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index 816d670..50c3fff 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,7 +5,7 @@ minesql-parent cc.carm.plugin - 1.0.0 + 1.1.0 4.0.0 diff --git a/api/src/main/java/cc/carm/plugin/minesql/IMineSQL.java b/api/src/main/java/cc/carm/plugin/minesql/IMineSQL.java index db65be5..cbf04f2 100644 --- a/api/src/main/java/cc/carm/plugin/minesql/IMineSQL.java +++ b/api/src/main/java/cc/carm/plugin/minesql/IMineSQL.java @@ -27,6 +27,8 @@ interface IMineSQL { @NotNull SQLRegistry getRegistry(); + @NotNull SQLRegistry createRegistry(); + /** * 创建一个新的 SQLManager 实例 * diff --git a/api/src/main/java/cc/carm/plugin/minesql/MineSQL.java b/api/src/main/java/cc/carm/plugin/minesql/MineSQL.java index 77e4408..e3bdc1d 100644 --- a/api/src/main/java/cc/carm/plugin/minesql/MineSQL.java +++ b/api/src/main/java/cc/carm/plugin/minesql/MineSQL.java @@ -43,6 +43,15 @@ public class MineSQL { return instance.getRegistry(); } + /** + * 创建一个独立的管理器注册池。 + * + * @return {@link SQLRegistry} + */ + public static @NotNull SQLRegistry createRegistry() { + return instance.createRegistry(); + } + /** * 创建一个新的 SQLManager 实例 * diff --git a/api/src/main/java/cc/carm/plugin/minesql/api/SQLRegistry.java b/api/src/main/java/cc/carm/plugin/minesql/api/SQLRegistry.java index 12fce93..952a935 100644 --- a/api/src/main/java/cc/carm/plugin/minesql/api/SQLRegistry.java +++ b/api/src/main/java/cc/carm/plugin/minesql/api/SQLRegistry.java @@ -12,8 +12,7 @@ import java.util.Optional; * 入口类 */ public interface SQLRegistry { - - + /** * 获取原生注册的指定名称的 SQLManager 实例 * diff --git a/core/pom.xml b/core/pom.xml index 1162f49..defcce6 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -5,7 +5,7 @@ minesql-parent cc.carm.plugin - 1.0.0 + 1.1.0 4.0.0 diff --git a/core/src/main/java/cc/carm/plugin/minesql/MineSQLCore.java b/core/src/main/java/cc/carm/plugin/minesql/MineSQLCore.java index 800e46d..eaae6f5 100644 --- a/core/src/main/java/cc/carm/plugin/minesql/MineSQLCore.java +++ b/core/src/main/java/cc/carm/plugin/minesql/MineSQLCore.java @@ -29,6 +29,8 @@ import java.util.logging.Logger; public class MineSQLCore implements IMineSQL { + protected static MineSQLCore instance; + public static final String REPO_OWNER = "CarmJos"; public static final String REPO_NAME = "MineSQL"; @@ -39,6 +41,7 @@ public class MineSQLCore implements IMineSQL { protected final PluginConfiguration config; public MineSQLCore(MineSQLPlatform platform) { + instance = this; this.platform = platform; getLogger().info("加载配置文件..."); @@ -50,18 +53,59 @@ public class MineSQLCore implements IMineSQL { MineSQL.initializeAPI(this); getLogger().info("初始化注册池..."); - this.registry = new MineSQLRegistry(this); + this.registry = createRegistry(); + + Map dbProperties = readProperties(); + Map dbConfigurations = readConfigurations(); + + if (dbProperties.isEmpty() && dbConfigurations.isEmpty()) { + getLogger().warning("未检测到任何数据库配置,将不会预创建任何SQLManager。"); + return; + } + + dbProperties.forEach((id, properties) -> { + try { + getLogger().info("正在初始化数据库 #" + id + " ..."); + SQLManagerImpl sqlManager = create(id, properties); + this.registry.getManagers().put(id, sqlManager); + getLogger().info("完成成初始化数据库 #" + id + " 。"); + } catch (Exception ex) { + getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage()); + ex.printStackTrace(); + } + }); + + dbConfigurations.forEach((id, configuration) -> { + try { + getLogger().info("正在初始化数据库 #" + id + " ..."); + SQLManagerImpl sqlManager = create(id, configuration); + this.registry.getManagers().put(id, sqlManager); + getLogger().info("完成初始化数据库 #" + id + " 。"); + } catch (Exception ex) { + getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage()); + ex.printStackTrace(); + } + }); } public MineSQLPlatform getPlatform() { return platform; } + public static MineSQLCore getInstance() { + return instance; + } + @Override public @NotNull MineSQLRegistry getRegistry() { return this.registry; } + @Override + public @NotNull MineSQLRegistry createRegistry() { + return new MineSQLRegistry(); + } + @Override public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull SQLSourceConfig conf) { BeeDataSourceConfig config = new BeeDataSourceConfig(); diff --git a/core/src/main/java/cc/carm/plugin/minesql/MineSQLRegistry.java b/core/src/main/java/cc/carm/plugin/minesql/MineSQLRegistry.java index 7023133..a64e0f2 100644 --- a/core/src/main/java/cc/carm/plugin/minesql/MineSQLRegistry.java +++ b/core/src/main/java/cc/carm/plugin/minesql/MineSQLRegistry.java @@ -1,84 +1,24 @@ package cc.carm.plugin.minesql; import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.manager.SQLManagerImpl; import cc.carm.plugin.minesql.api.SQLRegistry; -import cc.carm.plugin.minesql.api.source.SQLSourceConfig; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; -import java.util.*; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; public class MineSQLRegistry implements SQLRegistry { - private static MineSQLRegistry instance; - - protected ExecutorService executorPool; - protected MineSQLCore core; private final HashMap managers = new HashMap<>(); - protected MineSQLRegistry(@NotNull MineSQLCore core) { - this.core = core; - MineSQLRegistry.instance = this; - this.executorPool = Executors.newFixedThreadPool(2, (r) -> { - Thread thread = new Thread(r, "EasySQLRegistry"); - thread.setDaemon(true); - return thread; - }); - - Map dbProperties = core.readProperties(); - Map dbConfigurations = core.readConfigurations(); - - if (dbProperties.isEmpty() && dbConfigurations.isEmpty()) { - core.getLogger().warning("未检测到任何数据库配置,将不会预创建任何SQLManager。"); - return; - } - - dbProperties.forEach((id, properties) -> { - try { - core.getLogger().info("正在初始化数据库 #" + id + " ..."); - SQLManagerImpl sqlManager = this.core.create(id, properties); - this.managers.put(id, sqlManager); - core.getLogger().info("完成成初始化数据库 #" + id + " 。"); - } catch (Exception ex) { - core.getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage()); - ex.printStackTrace(); - } - }); - - dbConfigurations.forEach((id, configuration) -> { - try { - core.getLogger().info("正在初始化数据库 #" + id + " ..."); - SQLManagerImpl sqlManager = this.core.create(id, configuration); - this.managers.put(id, sqlManager); - core.getLogger().info("完成初始化数据库 #" + id + " 。"); - } catch (Exception ex) { - core.getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage()); - ex.printStackTrace(); - } - }); - - } - protected HashMap getManagers() { return managers; } - public ExecutorService getExecutor() { - return executorPool; - } - - public static MineSQLRegistry getInstance() { - return instance; - } - - public MineSQLCore getCore() { - return core; - } - @Override public @NotNull Optional<@Nullable SQLManager> getOptional(@Nullable String id) { return Optional.of(this.managers.get(id)); @@ -104,5 +44,5 @@ public class MineSQLRegistry implements SQLRegistry { if (manager == null) throw new NullPointerException("不存在ID为 " + name + " 的SQLManager实例。"); return manager; } - + } diff --git a/core/src/main/java/cc/carm/plugin/minesql/command/MineSQLCommand.java b/core/src/main/java/cc/carm/plugin/minesql/command/MineSQLCommand.java index 1d9ee50..5f4c7df 100644 --- a/core/src/main/java/cc/carm/plugin/minesql/command/MineSQLCommand.java +++ b/core/src/main/java/cc/carm/plugin/minesql/command/MineSQLCommand.java @@ -3,7 +3,6 @@ package cc.carm.plugin.minesql.command; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.SQLQuery; import cc.carm.plugin.minesql.MineSQLCore; -import cc.carm.plugin.minesql.MineSQLRegistry; import cc.carm.plugin.minesql.util.VersionReader; import co.aikar.commands.BaseCommand; import co.aikar.commands.CommandHelp; @@ -66,7 +65,7 @@ public class MineSQLCommand extends BaseCommand { issuer.sendMessage("§c只有后台执行才能使用此命令。"); return; } - Map runningManagers = MineSQLRegistry.getInstance().list(); + Map runningManagers = MineSQLCore.getInstance().getRegistry().list(); if (runningManagers.isEmpty()) { issuer.sendMessage("§r当前无正在运行的数据库管理器。"); } else { diff --git a/platforms/bukkit/pom.xml b/platforms/bukkit/pom.xml index 38aba57..613e285 100644 --- a/platforms/bukkit/pom.xml +++ b/platforms/bukkit/pom.xml @@ -5,7 +5,7 @@ minesql-parent cc.carm.plugin - 1.0.0 + 1.1.0 ../../pom.xml 4.0.0 diff --git a/platforms/bungee/pom.xml b/platforms/bungee/pom.xml index dea739e..60255f9 100644 --- a/platforms/bungee/pom.xml +++ b/platforms/bungee/pom.xml @@ -5,7 +5,7 @@ minesql-parent cc.carm.plugin - 1.0.0 + 1.1.0 ../../pom.xml 4.0.0 diff --git a/platforms/velocity/pom.xml b/platforms/velocity/pom.xml index 3acc85d..3df0bb7 100644 --- a/platforms/velocity/pom.xml +++ b/platforms/velocity/pom.xml @@ -5,7 +5,7 @@ minesql-parent cc.carm.plugin - 1.0.0 + 1.1.0 ../../pom.xml 4.0.0 diff --git a/plugin/pom.xml b/plugin/pom.xml index 11ef9c6..14c04f0 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -5,7 +5,7 @@ minesql-parent cc.carm.plugin - 1.0.0 + 1.1.0 4.0.0 diff --git a/pom.xml b/pom.xml index 15c7355..e7925bc 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ cc.carm.plugin minesql-parent pom - 1.0.0 + 1.1.0 api core