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 +} 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..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 @@ -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,42 @@ 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("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("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)) + } + } + + +} 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..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 @@ -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,37 @@ 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 buildPlayerDatabase(file: File = newFile(getDataFolder(), "data.db"), table: String): Database { + return Database(TypeSQLite(file, table)) +} + fun ProxyPlayer.getDataContainer(): DataContainer { return playerDataContainer[uniqueId] ?: error("unavailable") } @@ -76,4 +105,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..ee3ab9af4 --- /dev/null +++ b/expansion/expansion-player-database/src/main/kotlin/taboolib/expansion/ReadOnlyDataContainer.kt @@ -0,0 +1,53 @@ +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) { + + var 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)" + } + + 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() } + } + } + } + } +} 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 +}