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 6a2d4be..65871d3 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 @@ -60,7 +60,7 @@ class FoundationCorePlugin : JavaPlugin(), Listener { registerCommandExecutor(listOf("adventure", "a"), GamemodeCommand(GameMode.ADVENTURE)) registerCommandExecutor(listOf("spectator", "sp"), GamemodeCommand(GameMode.SPECTATOR)) registerCommandExecutor(listOf("leaderboard", "lb"), LeaderboardCommand()) - registerCommandExecutor(listOf("pstorestats"), StoreStatsCommand(this)) + registerCommandExecutor(listOf("pstore"), PersistentStoreCommand(this)) val log = slF4JLogger log.info("Features:") @@ -128,4 +128,4 @@ class FoundationCorePlugin : JavaPlugin(), Listener { const val BACKUP_ENABLED = true } -} \ No newline at end of file +} diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt new file mode 100644 index 0000000..5a9e148 --- /dev/null +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt @@ -0,0 +1,67 @@ +package cloud.kubelet.foundation.core.command + +import cloud.kubelet.foundation.core.FoundationCorePlugin +import jetbrains.exodus.entitystore.Entity +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import java.util.* + +class PersistentStoreCommand(private val plugin: FoundationCorePlugin) : CommandExecutor { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (args.isEmpty()) { + sender.sendMessage("Invalid Command Usage.") + return true + } + + when (args[0]) { + "stats" -> { + plugin.persistentStores.forEach { (name, store) -> + val counts = store.transact { + entityTypes.associateWith { type -> getAll(type).size() }.toSortedMap() + } + + sender.sendMessage( + "Store $name ->", + *counts.map { " ${it.key} -> ${it.value} entries" }.toTypedArray() + ) + } + } + "sample" -> { + if (args.size != 3) { + sender.sendMessage("Invalid Subcommand Usage.") + return true + } + + val storeName = args[1] + val entityTypeName = args[2] + val store = plugin.getPersistentStore(storeName) + val random = Random() + store.transact { + val entities = getAll(entityTypeName) + val results = mutableListOf() + for (entity in entities) { + if (random.nextBoolean()) { + results.add(entity) + } + + if (results.size == 3) { + break + } + } + + for (result in results) { + sender.sendMessage( + "Entity ${result.id.localId} ->", + *result.propertyNames.map { " ${it}: ${result.getProperty(it)}" }.toTypedArray() + ) + } + } + } + else -> { + sender.sendMessage("Unknown Subcommand.") + } + } + return true + } +} diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/StoreStatsCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/StoreStatsCommand.kt deleted file mode 100644 index 475251c..0000000 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/StoreStatsCommand.kt +++ /dev/null @@ -1,22 +0,0 @@ -package cloud.kubelet.foundation.core.command - -import cloud.kubelet.foundation.core.FoundationCorePlugin -import org.bukkit.command.Command -import org.bukkit.command.CommandExecutor -import org.bukkit.command.CommandSender - -class StoreStatsCommand(private val plugin: FoundationCorePlugin) : CommandExecutor { - override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { - plugin.persistentStores.forEach { (name, store) -> - 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 - } -} 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/persist/PersistentStore.kt index f862cc1..ca0b5f0 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt @@ -10,7 +10,7 @@ class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : private val fileStorePath = corePlugin.pluginDataPath.resolve("persistence/${fileStoreName}") private val entityStore = PersistentEntityStores.newInstance(fileStorePath.toFile()) - fun transact(block: (StoreTransaction) -> R): R { + fun transact(block: StoreTransaction.() -> R): R { var result: R? = null entityStore.executeInTransaction { tx -> result = block(tx) @@ -18,16 +18,15 @@ class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : return result!! } - fun create(entityTypeName: String, populate: Entity.() -> Unit) = transact { tx -> - val entity = tx.newEntity(entityTypeName) - populate(entity) + fun create(entityTypeName: String, populate: Entity.() -> Unit) = transact { + populate(newEntity(entityTypeName)) } - fun getAll(entityTypeName: String) = - transact { tx -> tx.getAll(entityTypeName) } + fun getAll(entityTypeName: String, block: (EntityIterable) -> R): R = + transact { block(getAll(entityTypeName)) } - fun find(entityTypeName: String, propertyName: String, value: Comparable): EntityIterable = - transact { tx -> tx.find(entityTypeName, propertyName, value) } + fun find(entityTypeName: String, propertyName: String, value: Comparable, block: (EntityIterable) -> R): R = + transact { block(find(entityTypeName, propertyName, value)) } override fun close() { entityStore.close() diff --git a/foundation-core/src/main/resources/plugin.yml b/foundation-core/src/main/resources/plugin.yml index d6657dc..088f596 100644 --- a/foundation-core/src/main/resources/plugin.yml +++ b/foundation-core/src/main/resources/plugin.yml @@ -45,7 +45,7 @@ commands: aliases: - lb permission: foundation.command.leaderboard - pstorestats: - description: Persistent Store Stats - usage: /pstorestats - permission: foundation.command.pstorestats + pstore: + description: Persistent Store Manager + usage: /pstore + permission: foundation.command.pstore