1
mirror of https://github.com/CarmJos/MineSQL.git synced 2024-09-19 20:25:45 +00:00

refactor(api): 重新设计API结构

This commit is contained in:
Carm Jos 2022-12-18 04:45:34 +08:00
parent fa30bdad81
commit 350b8452b0
8 changed files with 244 additions and 143 deletions

View File

@ -1,15 +1,22 @@
package cc.carm.plugin.minesql;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
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 javax.sql.DataSource;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.logging.Logger;
interface IMineSQL {
@NotNull SQLRegistry getRegistry();
@NotNull File getPluginFolder();
default @NotNull File getSourceFolder() {
@ -18,4 +25,38 @@ interface IMineSQL {
@NotNull Logger getLogger();
@NotNull SQLRegistry getRegistry();
/**
* 创建一个新的 SQLManager 实例
*
* @param name 实例名称
* @param configuration SQLManager 实例的配置
* @return {@link SQLManager} 实例
* @throws Exception 若创建失败则抛出异常
*/
@NotNull SQLManager create(@NotNull String name,
@NotNull SQLSourceConfig configuration) throws Exception;
/**
* 创建一个新的 SQLManager 实例
*
* @param name 实例名称
* @param properties SQLManager 实例的配置文件
* @return {@link SQLManager} 实例
* @throws Exception 若创建失败则抛出异常
*/
@NotNull SQLManager create(@NotNull String name,
@NotNull Properties properties) throws Exception;
@NotNull SQLManager create(@NotNull String name, @NotNull DataSource source) throws Exception;
/**
* 终止并关闭一个 SQLManager 实例
*
* @param manager SQLManager实例
* @param activeQueries 终止前仍未被关闭的SQLQuery列表
*/
void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries);
}

View File

@ -1,8 +1,18 @@
package cc.carm.plugin.minesql;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
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 javax.sql.DataSource;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.logging.Logger;
public class MineSQL {
@ -17,10 +27,6 @@ public class MineSQL {
return instance.getLogger();
}
public static SQLRegistry getRegistry() {
return instance.getRegistry();
}
/**
* @return 数据库源文件所在目录非插件数据目录
*/
@ -28,4 +34,75 @@ public class MineSQL {
return instance.getSourceFolder();
}
/**
* 得到管理器注册池
*
* @return {@link SQLRegistry} 注册池
*/
public static SQLRegistry getRegistry() {
return instance.getRegistry();
}
/**
* 创建一个新的 SQLManager 实例
*
* @param name 实例名称
* @param configuration SQLManager 实例的配置
* @return {@link SQLManager} 实例
* @throws Exception 若创建失败则抛出异常
*/
public static @NotNull SQLManager create(@NotNull String name,
@NotNull SQLSourceConfig configuration) throws Exception {
return instance.create(name, configuration);
}
/**
* 创建一个新的 SQLManager 实例
*
* @param name 实例名称
* @param properties SQLManager 实例的配置文件
* @return {@link SQLManager} 实例
* @throws Exception 若创建失败则抛出异常
*/
public static @NotNull SQLManager create(@NotNull String name,
@NotNull Properties properties) throws Exception {
return instance.create(name, properties);
}
public static @NotNull SQLManager create(@NotNull String name, @NotNull DataSource source) throws Exception {
return instance.create(name, source);
}
/**
* 终止并关闭一个 SQLManager 实例
*
* @param manager SQLManager实例
* @param activeQueries 终止前仍未被关闭的SQLQuery列表
*/
public static void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries) {
instance.shutdown(manager, activeQueries);
}
/**
* 终止并关闭一个 SQLManager 实例
*
* @param manager SQLManager实例
* @param forceClose 是否强制关闭进行中的查询
*/
public static void shutdown(SQLManager manager, boolean forceClose) {
shutdown(manager, (unclosedQueries) -> {
if (forceClose) unclosedQueries.values().forEach(SQLQuery::close);
});
}
/**
* 终止并关闭一个 SQLManager 实例
* <br>若在终止时仍有活跃的查询则将会强制关闭
*
* @param manager SQLManager实例
*/
public static void shutdown(SQLManager manager) {
shutdown(manager, true);
}
}

View File

@ -1,32 +1,39 @@
package cc.carm.plugin.minesql.api;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;
import java.util.function.Consumer;
/**
* 入口类
*/
public interface SQLRegistry {
/**
* 获取原生注册的指定名称的 SQLManager 实例
*
* @param name 要获取的 SQLManager 实例名称, 如果为 null 则获取首个实例
* @return {@link SQLManager} 实例
*/
default @Nullable SQLManager get(@Nullable String name) {
return getOptional(name).orElse(null);
}
/**
* 获取原生注册的指定名称的 SQLManager 实例并要求其不得为空
*
* @param name 要获取的 SQLManager 实例名称, 如果为 null 则获取首个实例
* @return {@link SQLManager} 实例
* @throws NullPointerException 若不存在对应实例则抛出空指针异常
*/
@NotNull SQLManager get(@Nullable String name) throws NullPointerException;
default @NotNull SQLManager getNotNull(@Nullable String name) throws NullPointerException {
return getOptional(name).orElseThrow(() -> new NullPointerException("并不存在ID为 #" + name + " 的SQLManager."));
}
/**
* 获取原生注册的指定名称的 SQLManager 实例
@ -45,57 +52,21 @@ public interface SQLRegistry {
@NotNull Map<String, ? extends SQLManager> list();
/**
* 创建并注册一个新的 SQLManager 实例
* 注册一个新的 SQLManager 实例
*
* @param name 实例名称
* @param configuration SQLManager 实例的配置
* @return {@link SQLManager} 实例
* @throws Exception 若创建失败则抛出异常
* @param sqlManager 实例
* @throws IllegalStateException 当所要注册的实例已经存在时抛出
*/
@NotNull SQLManager create(@NotNull String name,
@NotNull SQLSourceConfig configuration) throws Exception;
void register(@NotNull String name, @NotNull SQLManager sqlManager) throws IllegalStateException;
/**
* 创建并注册一个新的 SQLManager 实例
* 从注册池中注销一个新的 SQLManager 实例
*
* @param name 实例名称
* @param properties SQLManager 实例的配置文件
* @return {@link SQLManager} 实例
* @throws Exception 若创建失败则抛出异常
* @return 所被注销的 {@link SQLManager}
* @throws NullPointerException 当所要注销的实例不存在时抛出
*/
@NotNull SQLManager create(@NotNull String name,
@NotNull Properties properties) throws Exception;
@NotNull SQLManager create(@NotNull String name, @NotNull DataSource source) throws Exception;
/**
* 终止并关闭一个 SQLManager 实例
*
* @param manager SQLManager实例
* @param activeQueries 终止前仍未被关闭的SQLQuery列表
*/
void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries);
/**
* 终止并关闭一个 SQLManager 实例
*
* @param manager SQLManager实例
* @param forceClose 是否强制关闭进行中的查询
*/
default void shutdown(SQLManager manager, boolean forceClose) {
shutdown(manager, (unclosedQueries) -> {
if (forceClose) unclosedQueries.values().forEach(SQLQuery::close);
});
}
/**
* 终止并关闭一个 SQLManager 实例
* <br>若在终止时仍有活跃的查询则将会强制关闭
*
* @param manager SQLManager实例
*/
default void shutdown(SQLManager manager) {
shutdown(manager, true);
}
@NotNull SQLManager unregister(@NotNull String name) throws NullPointerException;
}

View File

@ -4,6 +4,8 @@ import cc.carm.lib.configuration.EasyConfiguration;
import cc.carm.lib.configuration.yaml.YAMLConfigProvider;
import cc.carm.lib.easyplugin.utils.JarResourceUtils;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
import cc.carm.plugin.minesql.command.MineSQLCommand;
@ -11,13 +13,18 @@ import cc.carm.plugin.minesql.command.MineSQLHelpFormatter;
import cc.carm.plugin.minesql.conf.PluginConfiguration;
import cc.carm.plugin.minesql.conf.SQLSourceGroup;
import cc.carm.plugin.minesql.util.DBPropertiesUtil;
import cn.beecp.BeeDataSource;
import cn.beecp.BeeDataSourceConfig;
import co.aikar.commands.CommandManager;
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.Locales;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.sql.DataSource;
import java.io.File;
import java.util.*;
import java.util.function.Consumer;
import java.util.logging.Logger;
public class MineSQLCore implements IMineSQL {
@ -44,7 +51,6 @@ public class MineSQLCore implements IMineSQL {
getLogger().info("初始化注册池...");
this.registry = new MineSQLRegistry(this);
}
public MineSQLPlatform getPlatform() {
@ -56,6 +62,74 @@ public class MineSQLCore implements IMineSQL {
return this.registry;
}
@Override
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull SQLSourceConfig conf) {
BeeDataSourceConfig config = new BeeDataSourceConfig();
config.setDriverClassName(conf.getDriverClassName());
config.setJdbcUrl(conf.getJdbcURL());
Optional.ofNullable(conf.getUsername()).ifPresent(config::setUsername);
Optional.ofNullable(conf.getPassword()).ifPresent(config::setPassword);
Optional.ofNullable(conf.getSettings().getPoolName()).ifPresent(config::setPoolName);
Optional.ofNullable(conf.getSettings().getMaxPoolSize()).ifPresent(config::setMaxActive);
Optional.ofNullable(conf.getSettings().getMaxActive()).ifPresent(config::setMaxActive);
Optional.ofNullable(conf.getSettings().getIdleTimeout()).ifPresent(config::setIdleTimeout);
Optional.ofNullable(conf.getSettings().getMaxWaitTime()).ifPresent(config::setMaxWait);
Optional.ofNullable(conf.getSettings().getMaxHoldTime()).ifPresent(config::setHoldTimeout);
Optional.ofNullable(conf.getSettings().getAutoCommit()).ifPresent(config::setDefaultAutoCommit);
Optional.ofNullable(conf.getSettings().getReadOnly()).ifPresent(config::setDefaultReadOnly);
Optional.ofNullable(conf.getSettings().getSchema()).ifPresent(config::setDefaultSchema);
Optional.ofNullable(conf.getSettings().getValidationSQL()).ifPresent(config::setValidTestSql);
Optional.ofNullable(conf.getSettings().getValidationTimeout()).ifPresent(config::setValidTestTimeout);
Optional.ofNullable(conf.getSettings().getValidationInterval()).ifPresent(config::setValidAssumeTime);
return create(name, config);
}
@Override
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull Properties properties) {
return create(name, new BeeDataSourceConfig(properties));
}
@Override
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull DataSource source) {
return new SQLManagerImpl(source, name);
}
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull BeeDataSourceConfig configuration) {
return create(name, (DataSource) new BeeDataSource(configuration));
}
@Override
public void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries) {
if (activeQueries != null) activeQueries.accept(manager.getActiveQuery());
if (manager.getDataSource() instanceof BeeDataSource) {
BeeDataSource dataSource = (BeeDataSource) manager.getDataSource();
dataSource.close(); //Close bee connection pool
}
}
public void shutdownAll() {
this.registry.getManagers().forEach((k, manager) -> {
getLogger().info(" 正在关闭数据库 " + k + "...");
shutdown(manager, activeQueries -> {
if (activeQueries.isEmpty()) return;
getLogger().info(" 数据库 " + k + " 仍有 " + activeQueries.size() + " 条活动查询");
if (manager.getDataSource() instanceof BeeDataSource
&& getConfig().SETTINGS.FORCE_CLOSE.getNotNull()) {
getLogger().info(" 将强制关闭全部活跃链接...");
BeeDataSource dataSource = (BeeDataSource) manager.getDataSource();
dataSource.close(); //Close bee connection pool
}
});
});
this.registry.getManagers().clear();
}
@Override
public @NotNull File getPluginFolder() {
return getPlatform().getPluginFolder();
@ -116,7 +190,7 @@ public class MineSQLCore implements IMineSQL {
commandManager.getCommandContexts().registerContext(SQLManager.class, c -> {
String name = c.popFirstArg();
try {
return getRegistry().get(name);
return getRegistry().getNotNull(name);
} catch (NullPointerException exception) {
throw new InvalidCommandArgument("不存在名为 " + name + " 的数据库管理器。");
}
@ -147,4 +221,5 @@ public class MineSQLCore implements IMineSQL {
}
}
}

View File

@ -1,21 +1,16 @@
package cc.carm.plugin.minesql;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.plugin.minesql.api.SQLRegistry;
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
import cn.beecp.BeeDataSource;
import cn.beecp.BeeDataSourceConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import javax.sql.DataSource;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
public class MineSQLRegistry implements SQLRegistry {
@ -23,7 +18,7 @@ public class MineSQLRegistry implements SQLRegistry {
protected ExecutorService executorPool;
protected MineSQLCore core;
private final HashMap<String, SQLManagerImpl> managers = new HashMap<>();
private final HashMap<String, SQLManager> managers = new HashMap<>();
protected MineSQLRegistry(@NotNull MineSQLCore core) {
this.core = core;
@ -45,7 +40,7 @@ public class MineSQLRegistry implements SQLRegistry {
dbProperties.forEach((id, properties) -> {
try {
core.getLogger().info("正在初始化数据库 #" + id + " ...");
SQLManagerImpl sqlManager = create(id, properties);
SQLManagerImpl sqlManager = this.core.create(id, properties);
this.managers.put(id, sqlManager);
core.getLogger().info("完成成初始化数据库 #" + id + "");
} catch (Exception ex) {
@ -57,7 +52,7 @@ public class MineSQLRegistry implements SQLRegistry {
dbConfigurations.forEach((id, configuration) -> {
try {
core.getLogger().info("正在初始化数据库 #" + id + " ...");
SQLManagerImpl sqlManager = create(id, configuration);
SQLManagerImpl sqlManager = this.core.create(id, configuration);
this.managers.put(id, sqlManager);
core.getLogger().info("完成初始化数据库 #" + id + "");
} catch (Exception ex) {
@ -68,24 +63,7 @@ public class MineSQLRegistry implements SQLRegistry {
}
public void shutdownAll() {
this.managers.forEach((k, manager) -> {
getCore().getLogger().info(" 正在关闭数据库 " + k + "...");
shutdown(manager, activeQueries -> {
if (activeQueries.isEmpty()) return;
getCore().getLogger().info(" 数据库 " + k + " 仍有 " + activeQueries.size() + " 条活动查询");
if (manager.getDataSource() instanceof BeeDataSource
&& this.core.getConfig().SETTINGS.FORCE_CLOSE.getNotNull()) {
getCore().getLogger().info(" 将强制关闭全部活跃链接...");
BeeDataSource dataSource = (BeeDataSource) manager.getDataSource();
dataSource.close(); //Close bee connection pool
}
});
});
this.managers.clear();
}
protected HashMap<String, SQLManagerImpl> getManagers() {
protected HashMap<String, SQLManager> getManagers() {
return managers;
}
@ -102,70 +80,29 @@ public class MineSQLRegistry implements SQLRegistry {
}
@Override
public @NotNull SQLManagerImpl get(@Nullable String id) throws NullPointerException {
return Objects.requireNonNull(this.managers.get(id), "并不存在ID为 #" + id + " 的SQLManager.");
}
@Override
public @NotNull Optional<@Nullable SQLManagerImpl> getOptional(@Nullable String id) {
public @NotNull Optional<@Nullable SQLManager> getOptional(@Nullable String id) {
return Optional.of(this.managers.get(id));
}
@Override
@Unmodifiable
public @NotNull Map<String, SQLManagerImpl> list() {
public @NotNull Map<String, SQLManager> list() {
return Collections.unmodifiableMap(this.managers);
}
@Override
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull SQLSourceConfig conf) {
BeeDataSourceConfig config = new BeeDataSourceConfig();
config.setDriverClassName(conf.getDriverClassName());
config.setJdbcUrl(conf.getJdbcURL());
Optional.ofNullable(conf.getUsername()).ifPresent(config::setUsername);
Optional.ofNullable(conf.getPassword()).ifPresent(config::setPassword);
Optional.ofNullable(conf.getSettings().getPoolName()).ifPresent(config::setPoolName);
Optional.ofNullable(conf.getSettings().getMaxPoolSize()).ifPresent(config::setMaxActive);
Optional.ofNullable(conf.getSettings().getMaxActive()).ifPresent(config::setMaxActive);
Optional.ofNullable(conf.getSettings().getIdleTimeout()).ifPresent(config::setIdleTimeout);
Optional.ofNullable(conf.getSettings().getMaxWaitTime()).ifPresent(config::setMaxWait);
Optional.ofNullable(conf.getSettings().getMaxHoldTime()).ifPresent(config::setHoldTimeout);
Optional.ofNullable(conf.getSettings().getAutoCommit()).ifPresent(config::setDefaultAutoCommit);
Optional.ofNullable(conf.getSettings().getReadOnly()).ifPresent(config::setDefaultReadOnly);
Optional.ofNullable(conf.getSettings().getSchema()).ifPresent(config::setDefaultSchema);
Optional.ofNullable(conf.getSettings().getValidationSQL()).ifPresent(config::setValidTestSql);
Optional.ofNullable(conf.getSettings().getValidationTimeout()).ifPresent(config::setValidTestTimeout);
Optional.ofNullable(conf.getSettings().getValidationInterval()).ifPresent(config::setValidAssumeTime);
return create(name, config);
public void register(@NotNull String name, @NotNull SQLManager sqlManager) throws IllegalStateException {
if (this.managers.containsKey(name)) {
throw new IllegalStateException("已存在ID为 " + name + " 的SQLManager实例。");
}
this.managers.put(name, sqlManager);
}
@Override
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull Properties properties) {
return create(name, new BeeDataSourceConfig(properties));
}
@Override
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull DataSource source) {
return new SQLManagerImpl(source, name);
}
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull BeeDataSourceConfig configuration) {
return create(name, (DataSource) new BeeDataSource(configuration));
}
@Override
public void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries) {
if (activeQueries != null) activeQueries.accept(manager.getActiveQuery());
if (manager.getDataSource() instanceof BeeDataSource) {
BeeDataSource dataSource = (BeeDataSource) manager.getDataSource();
dataSource.close(); //Close bee connection pool
}
public @NotNull SQLManager unregister(@NotNull String name) throws NullPointerException {
SQLManager manager = this.managers.remove(name);
if (manager == null) throw new NullPointerException("不存在ID为 " + name + " 的SQLManager实例。");
return manager;
}
}

View File

@ -57,7 +57,7 @@ public class MineSQLBukkit extends EasyPlugin implements MineSQLPlatform {
@Override
protected void shutdown() {
log("终止全部数据库连接...");
this.core.getRegistry().shutdownAll();
this.core.shutdownAll();
}
@Override

View File

@ -66,7 +66,7 @@ public class MineSQLBungee extends Plugin implements MineSQLPlatform {
public void onDisable() {
outputInfo();
getLogger().info("终止全部数据库连接...");
this.core.getRegistry().shutdownAll();
this.core.shutdownAll();
}
public static MineSQLBungee getInstance() {

View File

@ -86,7 +86,7 @@ public class MineSQLVelocity implements MineSQLPlatform {
public void onShutdown(ProxyShutdownEvent event) {
outputInfo();
getLogger().info("终止全部数据库连接...");
this.core.getRegistry().shutdownAll();
this.core.shutdownAll();
}
public ProxyServer getServer() {