From 78566d08ad1cac99b70dbe8148a8c72c7d933d55 Mon Sep 17 00:00:00 2001 From: Kenneth Endfinger Date: Thu, 23 Dec 2021 21:26:10 -0500 Subject: [PATCH] Refactor persistence into it's own feature. --- ...erverExtensions.kt => BukkitExtensions.kt} | 0 .../foundation/core/FoundationCorePlugin.kt | 2 ++ .../features/persist/PersistenceFeature.kt | 18 +++++++++++++++ .../{ => features}/persist/PersistentStore.kt | 2 +- .../PersistentStoreCommand.kt | 9 ++++---- .../features/persist/PluginPersistence.kt | 23 +++++++++++++++++++ .../{ => features}/persist/XodusExtensions.kt | 2 +- .../core/features/stats/StatsFeature.kt | 22 +++--------------- 8 files changed, 53 insertions(+), 25 deletions(-) rename foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/{ServerExtensions.kt => BukkitExtensions.kt} (100%) create mode 100644 foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistenceFeature.kt rename foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/{ => features}/persist/PersistentStore.kt (96%) rename foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/{stats => persist}/PersistentStoreCommand.kt (87%) create mode 100644 foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PluginPersistence.kt rename foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/{ => features}/persist/XodusExtensions.kt (78%) diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/ServerExtensions.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/BukkitExtensions.kt similarity index 100% rename from foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/ServerExtensions.kt rename to foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/BukkitExtensions.kt diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt index e04e83e..4528dc5 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt @@ -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.update.UpdateFeature import cloud.kubelet.foundation.core.features.world.WorldFeature +import cloud.kubelet.foundation.core.features.persist.PersistenceFeature import org.koin.dsl.module import java.nio.file.Path @@ -37,6 +38,7 @@ class FoundationCorePlugin : FoundationPlugin() { } override fun createFeatures() = listOf( + PersistenceFeature(), BackupFeature(), DevFeature(), PlayerFeature(), diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistenceFeature.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistenceFeature.kt new file mode 100644 index 0000000..7d1c16f --- /dev/null +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistenceFeature.kt @@ -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() + + override fun disable() { + persistence.value.unload() + } + + override fun module(): Module = module { + single { PluginPersistence() } + } +} diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistentStore.kt similarity index 96% rename from foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt rename to foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistentStore.kt index ab42bd8..078cff8 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistentStore.kt @@ -1,4 +1,4 @@ -package cloud.kubelet.foundation.core.persist +package cloud.kubelet.foundation.core.features.persist import cloud.kubelet.foundation.core.FoundationCorePlugin import jetbrains.exodus.entitystore.Entity diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/PersistentStoreCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistentStoreCommand.kt similarity index 87% rename from foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/PersistentStoreCommand.kt rename to foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistentStoreCommand.kt index 41d7a7f..c7ef55e 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/PersistentStoreCommand.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PersistentStoreCommand.kt @@ -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.CommandExecutor import org.bukkit.command.CommandSender @@ -23,7 +24,7 @@ class PersistentStoreCommand( when (args[0]) { "stats" -> { - statsFeature.persistentStores.forEach { (name, store) -> + statsFeature.persistence.value.stores.forEach { (name, store) -> val counts = store.transact { entityTypes.associateWith { type -> getAll(type).size() }.toSortedMap() } @@ -42,7 +43,7 @@ class PersistentStoreCommand( val storeName = args[1] val entityTypeName = args[2] - val store = statsFeature.getPersistentStore(storeName) + val store = statsFeature.persistence.value.store(storeName) store.transact { val entities = getAll(entityTypeName).take(3) for (entity in entities) { @@ -61,7 +62,7 @@ class PersistentStoreCommand( val storeName = args[1] val entityTypeName = args[2] - val store = statsFeature.getPersistentStore(storeName) + val store = statsFeature.persistence.value.store(storeName) store.transact { store.deleteAllEntities(entityTypeName) } diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PluginPersistence.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PluginPersistence.kt new file mode 100644 index 0000000..ab5c638 --- /dev/null +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/PluginPersistence.kt @@ -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() + + val stores = ConcurrentHashMap() + + /** + * 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() + } +} diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/XodusExtensions.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/XodusExtensions.kt similarity index 78% rename from foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/XodusExtensions.kt rename to foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/XodusExtensions.kt index 2852d95..94dfd81 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/XodusExtensions.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/persist/XodusExtensions.kt @@ -1,4 +1,4 @@ -package cloud.kubelet.foundation.core.persist +package cloud.kubelet.foundation.core.features.persist import jetbrains.exodus.entitystore.Entity diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/StatsFeature.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/StatsFeature.kt index 9c1a1cc..48a2dc3 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/StatsFeature.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/features/stats/StatsFeature.kt @@ -1,39 +1,23 @@ 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.persist.PersistentStore -import cloud.kubelet.foundation.core.persist.setAllProperties +import cloud.kubelet.foundation.core.features.persist.* import io.papermc.paper.event.player.AsyncChatEvent import net.kyori.adventure.text.TextComponent import org.bukkit.event.EventHandler import org.koin.core.component.inject import java.time.Instant -import java.util.concurrent.ConcurrentHashMap class StatsFeature : Feature() { - private val plugin = inject() + internal val persistence = inject() private lateinit var chatLogStore: PersistentStore - // TODO: Move persistence stuff to its own module. - internal val persistentStores = ConcurrentHashMap() override fun enable() { - chatLogStore = getPersistentStore("chat-logs") + chatLogStore = persistence.value.store("chat-logs") 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 private fun logOnChatMessage(e: AsyncChatEvent) { val player = e.player