Refactor persistence into it's own feature.

This commit is contained in:
Kenneth Endfinger
2021-12-23 21:26:10 -05:00
parent fca1db8802
commit 78566d08ad
8 changed files with 53 additions and 25 deletions

View File

@ -7,6 +7,7 @@ import cloud.kubelet.foundation.core.features.player.PlayerFeature
import cloud.kubelet.foundation.core.features.stats.StatsFeature import cloud.kubelet.foundation.core.features.stats.StatsFeature
import cloud.kubelet.foundation.core.features.update.UpdateFeature import cloud.kubelet.foundation.core.features.update.UpdateFeature
import cloud.kubelet.foundation.core.features.world.WorldFeature import cloud.kubelet.foundation.core.features.world.WorldFeature
import cloud.kubelet.foundation.core.features.persist.PersistenceFeature
import org.koin.dsl.module import org.koin.dsl.module
import java.nio.file.Path import java.nio.file.Path
@ -37,6 +38,7 @@ class FoundationCorePlugin : FoundationPlugin() {
} }
override fun createFeatures() = listOf( override fun createFeatures() = listOf(
PersistenceFeature(),
BackupFeature(), BackupFeature(),
DevFeature(), DevFeature(),
PlayerFeature(), PlayerFeature(),

View File

@ -0,0 +1,18 @@
package cloud.kubelet.foundation.core.features.persist
import cloud.kubelet.foundation.core.abstraction.Feature
import org.koin.core.component.inject
import org.koin.core.module.Module
import org.koin.dsl.module
class PersistenceFeature : Feature() {
private val persistence = inject<PluginPersistence>()
override fun disable() {
persistence.value.unload()
}
override fun module(): Module = module {
single { PluginPersistence() }
}
}

View File

@ -1,4 +1,4 @@
package cloud.kubelet.foundation.core.persist package cloud.kubelet.foundation.core.features.persist
import cloud.kubelet.foundation.core.FoundationCorePlugin import cloud.kubelet.foundation.core.FoundationCorePlugin
import jetbrains.exodus.entitystore.Entity import jetbrains.exodus.entitystore.Entity

View File

@ -1,5 +1,6 @@
package cloud.kubelet.foundation.core.features.stats package cloud.kubelet.foundation.core.features.persist
import cloud.kubelet.foundation.core.features.stats.StatsFeature
import org.bukkit.command.Command import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
@ -23,7 +24,7 @@ class PersistentStoreCommand(
when (args[0]) { when (args[0]) {
"stats" -> { "stats" -> {
statsFeature.persistentStores.forEach { (name, store) -> statsFeature.persistence.value.stores.forEach { (name, store) ->
val counts = store.transact { val counts = store.transact {
entityTypes.associateWith { type -> getAll(type).size() }.toSortedMap() entityTypes.associateWith { type -> getAll(type).size() }.toSortedMap()
} }
@ -42,7 +43,7 @@ class PersistentStoreCommand(
val storeName = args[1] val storeName = args[1]
val entityTypeName = args[2] val entityTypeName = args[2]
val store = statsFeature.getPersistentStore(storeName) val store = statsFeature.persistence.value.store(storeName)
store.transact { store.transact {
val entities = getAll(entityTypeName).take(3) val entities = getAll(entityTypeName).take(3)
for (entity in entities) { for (entity in entities) {
@ -61,7 +62,7 @@ class PersistentStoreCommand(
val storeName = args[1] val storeName = args[1]
val entityTypeName = args[2] val entityTypeName = args[2]
val store = statsFeature.getPersistentStore(storeName) val store = statsFeature.persistence.value.store(storeName)
store.transact { store.transact {
store.deleteAllEntities(entityTypeName) store.deleteAllEntities(entityTypeName)
} }

View File

@ -0,0 +1,23 @@
package cloud.kubelet.foundation.core.features.persist
import cloud.kubelet.foundation.core.FoundationCorePlugin
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.concurrent.ConcurrentHashMap
class PluginPersistence : KoinComponent {
private val plugin = inject<FoundationCorePlugin>()
val stores = ConcurrentHashMap<String, PersistentStore>()
/**
* Fetch a persistent store by name. Make sure the name is path-safe, descriptive and consistent across server runs.
*/
fun store(name: String): PersistentStore =
stores.getOrPut(name) { PersistentStore(plugin.value, name) }
fun unload() {
stores.values.forEach { store -> store.close() }
stores.clear()
}
}

View File

@ -1,4 +1,4 @@
package cloud.kubelet.foundation.core.persist package cloud.kubelet.foundation.core.features.persist
import jetbrains.exodus.entitystore.Entity import jetbrains.exodus.entitystore.Entity

View File

@ -1,39 +1,23 @@
package cloud.kubelet.foundation.core.features.stats package cloud.kubelet.foundation.core.features.stats
import cloud.kubelet.foundation.core.FoundationCorePlugin
import cloud.kubelet.foundation.core.abstraction.Feature import cloud.kubelet.foundation.core.abstraction.Feature
import cloud.kubelet.foundation.core.persist.PersistentStore import cloud.kubelet.foundation.core.features.persist.*
import cloud.kubelet.foundation.core.persist.setAllProperties
import io.papermc.paper.event.player.AsyncChatEvent import io.papermc.paper.event.player.AsyncChatEvent
import net.kyori.adventure.text.TextComponent import net.kyori.adventure.text.TextComponent
import org.bukkit.event.EventHandler import org.bukkit.event.EventHandler
import org.koin.core.component.inject import org.koin.core.component.inject
import java.time.Instant import java.time.Instant
import java.util.concurrent.ConcurrentHashMap
class StatsFeature : Feature() { class StatsFeature : Feature() {
private val plugin = inject<FoundationCorePlugin>() internal val persistence = inject<PluginPersistence>()
private lateinit var chatLogStore: PersistentStore private lateinit var chatLogStore: PersistentStore
// TODO: Move persistence stuff to its own module.
internal val persistentStores = ConcurrentHashMap<String, PersistentStore>()
override fun enable() { override fun enable() {
chatLogStore = getPersistentStore("chat-logs") chatLogStore = persistence.value.store("chat-logs")
registerCommandExecutor("pstore", PersistentStoreCommand(this)) registerCommandExecutor("pstore", PersistentStoreCommand(this))
} }
override fun disable() {
persistentStores.values.forEach { store -> store.close() }
persistentStores.clear()
}
/**
* Fetch a persistent store by name. Make sure the name is path-safe, descriptive and consistent across server runs.
*/
fun getPersistentStore(name: String) =
persistentStores.getOrPut(name) { PersistentStore(plugin.value, name) }
@EventHandler @EventHandler
private fun logOnChatMessage(e: AsyncChatEvent) { private fun logOnChatMessage(e: AsyncChatEvent) {
val player = e.player val player = e.player