From 67bd06c2ec756fc7e7707f0d22d864962e40941c Mon Sep 17 00:00:00 2001 From: FxRayHughes Date: Thu, 1 Aug 2024 12:47:49 +0800 Subject: [PATCH 1/5] =?UTF-8?q?[player-database]=20+=20=E7=BB=99Database?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80=E4=BA=9B=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=20getList=20remove=20+=20=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8F=AA?= =?UTF-8?q?=E8=AF=BB=E5=AE=B9=E5=99=A8=E7=B1=BB=E5=9E=8B=20+=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E6=9E=84=E9=80=A0=20playerDatabase=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20+=20=E4=BD=BFSQLite=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=B9=9F=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8?= =?UTF-8?q?=E5=90=8D=20+=20=E4=BB=A5=E4=B8=8A=E4=BF=AE=E6=94=B9=E6=96=B9?= =?UTF-8?q?=E5=90=91=E4=B8=BA=E6=94=AF=E6=8C=81=E5=A4=9A=E8=A1=A8=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E6=95=B0=E6=8D=AE=E7=81=B5=E6=B4=BB=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/taboolib/expansion/Database.kt | 35 ++++++++++++++++- .../taboolib/expansion/DatabaseHandler.kt | 39 ++++++++++++++++++- .../expansion/DatabaseHandlerForBukkit.kt | 10 ++++- .../expansion/ReadOnlyDataContainer.kt | 27 +++++++++++++ .../main/kotlin/taboolib/expansion/TypeSQL.kt | 6 +-- .../kotlin/taboolib/expansion/TypeSQLite.kt | 10 ++--- 6 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt index de6caab02..4b0cc2200 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt @@ -29,6 +29,10 @@ class Database(val type: Type, val dataSource: DataSource = type.host().createDa } operator fun set(user: String, name: String, data: String) { + if (data.isEmpty()) { + remove(user, name) + return + } if (get(user, name) == null) { type.tableVar().insert(dataSource, "user", "key", "value") { value(user, name, data) } } else { @@ -38,4 +42,33 @@ class Database(val type: Type, val dataSource: DataSource = type.host().createDa } } } -} \ No newline at end of file + + // 查询某个User的Key对应的Value + fun select(user: String, key: String): String? { + return type.tableVar().select(dataSource) { + rows("key", "value") + where("user" eq user and ("key" eq key)) + }.firstOrNull { + getString("value") + } + } + + // 根据Key获取所有数据 + // return: Map + fun getList(name: String): MutableMap { + return type.tableVar().select(dataSource) { + rows("value") + where("key" eq name) + }.map { + getString("user") to getString("value") + }.toMap(ConcurrentHashMap()) + } + + fun remove(user: String, name: String) { + type.tableVar().delete(dataSource) { + where("user" eq user and ("key" eq name)) + } + } + + +} diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt index a63470a06..a14e1c504 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt @@ -15,6 +15,8 @@ var playerDatabase: Database? = null val playerDataContainer = ConcurrentHashMap() +val playerReadOnlyDataContainer = ConcurrentHashMap() + fun setupPlayerDatabase( conf: ConfigurationSection, table: String = conf.getString("table", "")!!, @@ -49,10 +51,33 @@ fun setupPlayerDatabase( setupPlayerDatabase(conf, table, flags, clearFlags, ssl) } +fun buildPlayerDatabase( + conf: ConfigurationSection, + table: String = conf.getString("table", "")!!, + flags: List = emptyList(), + clearFlags: Boolean = false, + ssl: String? = null, +): Database { + val hostSQL = HostSQL(conf) + if (clearFlags) { + hostSQL.flags.clear() + } + hostSQL.flags.addAll(flags) + if (ssl != null) { + hostSQL.flags -= "useSSL=false" + hostSQL.flags += "sslMode=$ssl" + } + return Database(TypeSQL(hostSQL, table)) +} + fun setupPlayerDatabase(file: File = newFile(getDataFolder(), "data.db")) { playerDatabase = Database(TypeSQLite(file)) } +fun setupPlayerDatabase(file: File = newFile(getDataFolder(), "data.db"), tableName: String) { + playerDatabase = Database(TypeSQLite(file, tableName)) +} + fun ProxyPlayer.getDataContainer(): DataContainer { return playerDataContainer[uniqueId] ?: error("unavailable") } @@ -76,4 +101,16 @@ fun UUID.releasePlayerDataContainer() { fun ProxyPlayer.releaseDataContainer() { playerDataContainer.remove(uniqueId) -} \ No newline at end of file +} + +// 获取只读容器 +fun UUID.getReadOnlyDataContainer(): ReadOnlyDataContainer { + return playerReadOnlyDataContainer.computeIfAbsent(this) { + ReadOnlyDataContainer(this.toString(), playerDatabase!!) + } +} + +// 释放只读容器 +fun UUID.releaseReadOnlyDataContainer() { + playerReadOnlyDataContainer.remove(this) +} diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandlerForBukkit.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandlerForBukkit.kt index 0ca861314..c9fd9ce3d 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandlerForBukkit.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandlerForBukkit.kt @@ -13,4 +13,12 @@ fun Player.setupDataContainer(usernameMode: Boolean = false) { fun Player.releaseDataContainer() { adaptPlayer(this).releaseDataContainer() -} \ No newline at end of file +} + +fun Player.getReadOnlyDataContainer(): ReadOnlyDataContainer { + return this.uniqueId.getReadOnlyDataContainer() +} + +fun Player.releaseReadOnlyDataContainer() { + this.uniqueId.releaseReadOnlyDataContainer() +} diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt new file mode 100644 index 000000000..4889801c7 --- /dev/null +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt @@ -0,0 +1,27 @@ +package taboolib.expansion + +// 只读数据容器 +class ReadOnlyDataContainer(val user: String, val database: Database) { + + val source = database[user] + + operator fun get(key: String): String? { + return source[key] + } + + fun keys(): Set { + return source.keys + } + + fun values(): Map { + return source + } + + fun size(): Int { + return source.size + } + + override fun toString(): String { + return "ReadOnlyDataContainer(user='$user', source=$source)" + } +} diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQL.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQL.kt index da584c740..8c65a62cc 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQL.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQL.kt @@ -7,7 +7,7 @@ class TypeSQL(val host: Host, table: String) : Type() { val tableVar = Table(table, host) { add { id() } add("user") { - type(ColumnTypeSQL.VARCHAR, 36) { + type(ColumnTypeSQL.VARCHAR, 64) { options(ColumnOptionSQL.KEY) } } @@ -17,7 +17,7 @@ class TypeSQL(val host: Host, table: String) : Type() { } } add("value") { - type(ColumnTypeSQL.VARCHAR, 128) + type(ColumnTypeSQL.LONGTEXT) } } @@ -28,4 +28,4 @@ class TypeSQL(val host: Host, table: String) : Type() { override fun tableVar(): Table<*, *> { return tableVar } -} \ No newline at end of file +} diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQLite.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQLite.kt index 25962872e..cf4fbdcf8 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQLite.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/TypeSQLite.kt @@ -8,19 +8,19 @@ import taboolib.module.database.Table import taboolib.module.database.getHost import java.io.File -class TypeSQLite(file: File) : Type() { +class TypeSQLite(file: File, tableName: String? = null) : Type() { val host = newFile(file).getHost() - val tableVar = Table(pluginId, host) { + val tableVar = Table(tableName ?: pluginId, host) { add("user") { - type(ColumnTypeSQLite.TEXT, 36) + type(ColumnTypeSQLite.TEXT, 64) } add("key") { type(ColumnTypeSQLite.TEXT, 64) } add("value") { - type(ColumnTypeSQLite.TEXT, 128) + type(ColumnTypeSQLite.TEXT) } } @@ -31,4 +31,4 @@ class TypeSQLite(file: File) : Type() { override fun tableVar(): Table<*, *> { return tableVar } -} \ No newline at end of file +} From cfa880c76377151da1ff72ea9048159133338bf9 Mon Sep 17 00:00:00 2001 From: FxRayHughes Date: Thu, 1 Aug 2024 15:03:08 +0800 Subject: [PATCH 2/5] =?UTF-8?q?[player-database]=20+=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=B8=AA=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E8=AF=BB=E7=B1=BB=E5=9E=8B=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E4=BC=9A=E5=91=A8=E6=9C=9F=E5=90=8C=E6=AD=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expansion/ReadOnlyDataContainer.kt | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt index 4889801c7..ee3ab9af4 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt @@ -1,9 +1,14 @@ package taboolib.expansion +import taboolib.common.Inject +import taboolib.common.LifeCycle +import taboolib.common.platform.Awake +import taboolib.common.platform.function.submitAsync + // 只读数据容器 class ReadOnlyDataContainer(val user: String, val database: Database) { - val source = database[user] + var source = database[user] operator fun get(key: String): String? { return source[key] @@ -24,4 +29,25 @@ class ReadOnlyDataContainer(val user: String, val database: Database) { override fun toString(): String { return "ReadOnlyDataContainer(user='$user', source=$source)" } + + fun update() { + source = database[user] + } + + + @Inject + internal companion object { + + // 延迟更新时间 + var syncTick = 80L + + @Awake(LifeCycle.ACTIVE) + private fun update() { + submitAsync(period = syncTick) { + playerReadOnlyDataContainer.values.forEach { + runCatching { it.update() } + } + } + } + } } From 4860e169bda4cac167131c55bca610d58da915ab Mon Sep 17 00:00:00 2001 From: FxRayHughes Date: Thu, 1 Aug 2024 15:23:43 +0800 Subject: [PATCH 3/5] =?UTF-8?q?[player-database]=20+=20=E7=BB=99=E5=86=99?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=A9=BF=E9=80=8F=E7=BC=93=E5=AD=98=E7=9A=84set=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/taboolib/expansion/DataContainer.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DataContainer.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DataContainer.kt index 51bb613bf..947f2f539 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DataContainer.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DataContainer.kt @@ -23,6 +23,11 @@ class DataContainer(val user: String, val database: Database) { save(key) } + // 穿透缓存的写数据库方法 + operator fun set(targetUser: String, key: String, value: Any) { + database[targetUser, key] = value.toString() + } + fun setDelayed(key: String, value: Any, delay: Long = 3L, timeUnit: TimeUnit = TimeUnit.SECONDS) { source[key] = value.toString() updateMap[key] = System.currentTimeMillis() - timeUnit.toMillis(delay) @@ -67,4 +72,4 @@ class DataContainer(val user: String, val database: Database) { playerDataContainer.values.forEach { it.checkUpdate() } } } -} \ No newline at end of file +} From 86ff4648574832844ada4c6a9a24cd3e75a4b0ae Mon Sep 17 00:00:00 2001 From: FxRayHughes Date: Thu, 1 Aug 2024 15:40:02 +0800 Subject: [PATCH 4/5] =?UTF-8?q?[player-database]=20+=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BA=86=E5=88=97=E6=9F=A5=E8=AF=A2=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/taboolib/expansion/Database.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt index 4b0cc2200..293290da1 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/Database.kt @@ -46,7 +46,7 @@ class Database(val type: Type, val dataSource: DataSource = type.host().createDa // 查询某个User的Key对应的Value fun select(user: String, key: String): String? { return type.tableVar().select(dataSource) { - rows("key", "value") + rows("value") where("user" eq user and ("key" eq key)) }.firstOrNull { getString("value") @@ -57,13 +57,22 @@ class Database(val type: Type, val dataSource: DataSource = type.host().createDa // return: Map fun getList(name: String): MutableMap { return type.tableVar().select(dataSource) { - rows("value") + rows("user", "value") where("key" eq name) }.map { getString("user") to getString("value") }.toMap(ConcurrentHashMap()) } + fun getList(name: String, value: String): List { + return type.tableVar().select(dataSource) { + rows("user") + where("key" eq name and ("value" eq value)) + }.map { + getString("user") + } + } + fun remove(user: String, name: String) { type.tableVar().delete(dataSource) { where("user" eq user and ("key" eq name)) From 9b7a0c537c20862275f06d443180d86d9a18b433 Mon Sep 17 00:00:00 2001 From: FxRayHughes Date: Thu, 1 Aug 2024 15:53:19 +0800 Subject: [PATCH 5/5] =?UTF-8?q?[player-database]=20+=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=89=8B=E5=8A=A8=E6=9E=84=E9=80=A0SQLite=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/taboolib/expansion/DatabaseHandler.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt index a14e1c504..f9be98552 100644 --- a/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/DatabaseHandler.kt @@ -78,6 +78,10 @@ fun setupPlayerDatabase(file: File = newFile(getDataFolder(), "data.db"), tableN playerDatabase = Database(TypeSQLite(file, tableName)) } +fun buildPlayerDatabase(file: File = newFile(getDataFolder(), "data.db"), table: String): Database { + return Database(TypeSQLite(file, table)) +} + fun ProxyPlayer.getDataContainer(): DataContainer { return playerDataContainer[uniqueId] ?: error("unavailable") }