Skip to content

Commit

Permalink
feat: Add MySQL-Variant source
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Oct 30, 2024
1 parent dd9cbe8 commit d89ae72
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.aiyostudio.esync.internal.handler.CacheHandler
import com.aiyostudio.esync.internal.handler.ModuleHandler
import com.aiyostudio.esync.internal.handler.RepositoryHandler
import com.aiyostudio.esync.internal.plugin.EfficientSyncBukkit
import com.aiyostudio.esync.internal.repository.MysqlVariantRepositoryImpl
import com.aiyostudio.esync.internal.util.LoggerUtil
import com.aiyostudio.supermarketpremium.internal.config.i18n.I18n
import org.bukkit.Bukkit
Expand Down Expand Up @@ -60,8 +61,14 @@ object SyncConfig {
sourceConfig.getString("user"),
sourceConfig.getString("password")
)
"mysql-variant" -> MysqlVariantRepositoryImpl(
sourceConfig.getString("url"),
sourceConfig.getString("user"),
sourceConfig.getString("password")
)
else -> throw NullPointerException("Failed to initialize repository.")
}
RepositoryHandler.repository?.run { this.init() }

LoggerUtil.print("&6 * &fSync source: &e${RepositoryHandler.repository?.id ?: "NONE"}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.aiyostudio.esync.internal.repository

import com.aiyostudio.esync.common.repository.impl.MysqlRepositoryImpl
import com.aiyostudio.esync.internal.plugin.EfficientSyncBukkit
import com.aystudio.core.bukkit.util.mysql.MySqlStorageHandler
import java.sql.Connection

class MysqlVariantRepositoryImpl(
url: String,
user: String,
password: String
) : MysqlRepositoryImpl(url, user, password) {
override val id: String = "MySQL-Variant"
private val source = MySqlStorageHandler(
EfficientSyncBukkit.instance,
url,
user,
password,
*sql
)

override fun init() {
source.setCheckConnection(true)
source.reconnectionQueryTable = esyncDataTable
}

override fun getConnection(): Connection {
return source.dataSource.connection
}
}
19 changes: 14 additions & 5 deletions bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ language: "zh_CN"
# 同步设置
# Sync settings
sync:
# 当前类型, 支持: MySQL, PostgreSQL
# Current type, Supports: MySQL, PostgreSQL
type: mysql
# 当前类型, 支持: MySQL(type: mysql), PostgreSQL(type: postgres)
# 变种:
# - MySQLVariant(type: mysql_variant,使用 AyCore 内置简易连接池或 HikariCP)
# Current type, Supports: MySQL(id: mysql), PostgreSQL(id: postgres)
# Variant:
# - MySQLVariant(type: mysql-variant, from AyCore, Support HikariCP or built-in simpel pool)
type: mysql-variant
# 源参数设置
# Source parameters
sources:
Expand All @@ -19,6 +23,11 @@ sync:
url: "jdbc:postgresql://localhost:5432/postgres"
user: "postgres"
password: "postgres"
# mysql_variant
mysql-variant:
url: "jdbc:mysql://localhost/minecraft?useSSL=false&autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf-8"
user: "root"
password: "root"
# 自动解锁
# Automatic unlock
auto-unlock:
Expand Down Expand Up @@ -54,14 +63,14 @@ modules:
# Player status, including: health, buffs
player-status:
enable: true
# 每次登录都清空玩家背包
# 每次登录都清空玩家药水效果
# Clear potion every time
always-clear: true
# 末影箱
# Ender chest
ender-chest:
enable: true
# 每次登录都清空玩家背包
# 每次登录都清空玩家末影箱
# Clear ender-chest every time
always-clear: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import java.util.UUID
interface IRepository {
val id: String

fun init()

fun isExists(uuid: UUID, module: String): Boolean

fun queryData(uuid: UUID, module: String): ByteArray?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,38 @@ import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference

class MysqlRepositoryImpl(
open class MysqlRepositoryImpl(
private val url: String,
private val user: String,
private val password: String
) : IRepository {
private val esyncDataTable = "`esync_data`"
protected val esyncDataTable = "`esync_data`"
protected val sql = arrayOf(
"""
CREATE TABLE IF NOT EXISTS $esyncDataTable
(
`id` BIGINT AUTO_INCREMENT NOT NULL,
`owner_uuid` VARCHAR(40) NOT NULL,
`module` VARCHAR(100) NOT NULL,
`data` MEDIUMBLOB NOT NULL,
`state` ENUM ('COMPLETE', 'WAITING', 'LOCKED'),
PRIMARY KEY (`id`),
INDEX idx_owner (owner_uuid),
INDEX idx_state (state),
INDEX idx_module (module)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
""".trimIndent()
)
override val id = "MySQL"

override fun init() {
sql.forEach { line ->
this.connect {
it.use { conn -> conn.prepareStatement(line).executeUpdate() }
}
}
}

override fun isExists(uuid: UUID, module: String): Boolean {
val result = AtomicBoolean(false)
this.connect {
Expand All @@ -33,12 +57,12 @@ class MysqlRepositoryImpl(

override fun queryData(uuid: UUID, module: String): ByteArray? {
val result = AtomicReference<ByteArray>()
this.connect{ conn ->
this.connect { conn ->
val sql = "SELECT data FROM $esyncDataTable WHERE owner_uuid = ? AND module = ?"
conn.prepareStatement(sql).use { statement ->
statement.setString(1, uuid.toString())
statement.setString(2, module)
statement.executeQuery().use rsUse@ { rs ->
statement.executeQuery().use rsUse@{ rs ->
if (!rs.next()) {
return@rsUse
}
Expand Down Expand Up @@ -120,7 +144,7 @@ class MysqlRepositoryImpl(
}

private fun connectTransaction(uuid: UUID, module: String, block: (connect: Connection, id: Int) -> Unit) {
with(DriverManager.getConnection(url, user, password)) {
with(this.getConnection()) {
this.autoCommit = false
try {
this.transactionIsolation = Connection.TRANSACTION_READ_COMMITTED
Expand All @@ -146,12 +170,16 @@ class MysqlRepositoryImpl(
}

private fun connect(block: (connect: Connection) -> Unit) {
with(DriverManager.getConnection(url, user, password)) {
with(this.getConnection()) {
try {
block(this)
} finally {
this.close()
}
}
}

open fun getConnection(): Connection {
return DriverManager.getConnection(url, user, password)
}
}

0 comments on commit d89ae72

Please sign in to comment.