Persistent Store Improvements

- Fix bug where find() would return Unit while still performing the operational.
- Add a getAll() function for fetching all entities of a specific type.
- Close persistent stores on plugin disable.
This commit is contained in:
Kenneth Endfinger 2021-12-22 21:11:16 -05:00
parent 5f6a663cb3
commit 9533dd5e4f
No known key found for this signature in database
GPG Key ID: C4E68E5647420E10
4 changed files with 33 additions and 17 deletions

View File

@ -68,6 +68,11 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
chatLogStore = getPersistentStore("chat-logs")
}
override fun onDisable() {
persistentStores.values.forEach { store -> store.close() }
persistentStores.clear()
}
private fun registerCommandExecutor(name: String, executor: CommandExecutor) {
registerCommandExecutor(listOf(name), executor)
}

View File

@ -17,14 +17,12 @@ fun Server.allPlayerStatisticsOf(
material: Material? = null,
entityType: EntityType? = null,
order: SortOrder = SortOrder.Ascending
) = allPlayers
.map { player ->
player to if (material != null) {
player.getStatistic(statistic, material)
} else if (entityType != null) {
player.getStatistic(statistic, entityType)
} else {
player.getStatistic(statistic)
}
) = allPlayers.map { player ->
player to if (material != null) {
player.getStatistic(statistic, material)
} else if (entityType != null) {
player.getStatistic(statistic, entityType)
} else {
player.getStatistic(statistic)
}
.sortedBy(order) { it.second }
}.sortedBy(order) { it.second }

View File

@ -8,11 +8,14 @@ import org.bukkit.command.CommandSender
class StoreStatsCommand(private val plugin: FoundationCorePlugin) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
plugin.persistentStores.forEach { (name, store) ->
store.transact { tx ->
val types = tx.entityTypes
val counts = types.associateWith { type -> tx.getAll(type).size() }.toSortedMap()
sender.sendMessage("Store $name ->", *counts.map { " ${it.key} -> ${it.value} entries" }.toTypedArray())
val counts = store.transact { tx ->
tx.entityTypes.associateWith { type -> tx.getAll(type).size() }.toSortedMap()
}
sender.sendMessage(
"Store $name ->",
*counts.map { " ${it.key} -> ${it.value} entries" }.toTypedArray()
)
}
return true
}

View File

@ -2,21 +2,31 @@ package cloud.kubelet.foundation.core.persist
import cloud.kubelet.foundation.core.FoundationCorePlugin
import jetbrains.exodus.entitystore.Entity
import jetbrains.exodus.entitystore.EntityIterable
import jetbrains.exodus.entitystore.PersistentEntityStores
import jetbrains.exodus.entitystore.StoreTransaction
class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : AutoCloseable {
private val fileStorePath = corePlugin.pluginDataPath.resolve("persistence/${fileStoreName}")
internal val entityStore = PersistentEntityStores.newInstance(fileStorePath.toFile())
private val entityStore = PersistentEntityStores.newInstance(fileStorePath.toFile())
fun transact(block: (StoreTransaction) -> Unit) = entityStore.executeInTransaction(block)
fun <R> transact(block: (StoreTransaction) -> R): R {
var result: R? = null
entityStore.executeInTransaction { tx ->
result = block(tx)
}
return result!!
}
fun create(entityTypeName: String, populate: Entity.() -> Unit) = transact { tx ->
val entity = tx.newEntity(entityTypeName)
populate(entity)
}
fun <T> find(entityTypeName: String, propertyName: String, value: Comparable<T>) =
fun getAll(entityTypeName: String) =
transact { tx -> tx.getAll(entityTypeName) }
fun <T> find(entityTypeName: String, propertyName: String, value: Comparable<T>): EntityIterable =
transact { tx -> tx.find(entityTypeName, propertyName, value) }
override fun close() {