mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 11:39:39 +00:00 
			
		
		
		
	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:
		@ -68,6 +68,11 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
 | 
				
			|||||||
    chatLogStore = getPersistentStore("chat-logs")
 | 
					    chatLogStore = getPersistentStore("chat-logs")
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  override fun onDisable() {
 | 
				
			||||||
 | 
					    persistentStores.values.forEach { store -> store.close() }
 | 
				
			||||||
 | 
					    persistentStores.clear()
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private fun registerCommandExecutor(name: String, executor: CommandExecutor) {
 | 
					  private fun registerCommandExecutor(name: String, executor: CommandExecutor) {
 | 
				
			||||||
    registerCommandExecutor(listOf(name), executor)
 | 
					    registerCommandExecutor(listOf(name), executor)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -17,8 +17,7 @@ fun Server.allPlayerStatisticsOf(
 | 
				
			|||||||
  material: Material? = null,
 | 
					  material: Material? = null,
 | 
				
			||||||
  entityType: EntityType? = null,
 | 
					  entityType: EntityType? = null,
 | 
				
			||||||
  order: SortOrder = SortOrder.Ascending
 | 
					  order: SortOrder = SortOrder.Ascending
 | 
				
			||||||
) = allPlayers
 | 
					) = allPlayers.map { player ->
 | 
				
			||||||
  .map { player ->
 | 
					 | 
				
			||||||
  player to if (material != null) {
 | 
					  player to if (material != null) {
 | 
				
			||||||
    player.getStatistic(statistic, material)
 | 
					    player.getStatistic(statistic, material)
 | 
				
			||||||
  } else if (entityType != null) {
 | 
					  } else if (entityType != null) {
 | 
				
			||||||
@ -26,5 +25,4 @@ fun Server.allPlayerStatisticsOf(
 | 
				
			|||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    player.getStatistic(statistic)
 | 
					    player.getStatistic(statistic)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  }
 | 
					}.sortedBy(order) { it.second }
 | 
				
			||||||
  .sortedBy(order) { it.second }
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -8,11 +8,14 @@ import org.bukkit.command.CommandSender
 | 
				
			|||||||
class StoreStatsCommand(private val plugin: FoundationCorePlugin) : CommandExecutor {
 | 
					class StoreStatsCommand(private val plugin: FoundationCorePlugin) : CommandExecutor {
 | 
				
			||||||
  override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
 | 
					  override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
 | 
				
			||||||
    plugin.persistentStores.forEach { (name, store) ->
 | 
					    plugin.persistentStores.forEach { (name, store) ->
 | 
				
			||||||
      store.transact { tx ->
 | 
					      val counts = store.transact { tx ->
 | 
				
			||||||
        val types = tx.entityTypes
 | 
					        tx.entityTypes.associateWith { type -> tx.getAll(type).size() }.toSortedMap()
 | 
				
			||||||
        val counts = types.associateWith { type -> tx.getAll(type).size() }.toSortedMap()
 | 
					 | 
				
			||||||
        sender.sendMessage("Store $name ->", *counts.map { "  ${it.key} -> ${it.value} entries" }.toTypedArray())
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      sender.sendMessage(
 | 
				
			||||||
 | 
					        "Store $name ->",
 | 
				
			||||||
 | 
					        *counts.map { "  ${it.key} -> ${it.value} entries" }.toTypedArray()
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return true
 | 
					    return true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -2,21 +2,31 @@ package cloud.kubelet.foundation.core.persist
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import cloud.kubelet.foundation.core.FoundationCorePlugin
 | 
					import cloud.kubelet.foundation.core.FoundationCorePlugin
 | 
				
			||||||
import jetbrains.exodus.entitystore.Entity
 | 
					import jetbrains.exodus.entitystore.Entity
 | 
				
			||||||
 | 
					import jetbrains.exodus.entitystore.EntityIterable
 | 
				
			||||||
import jetbrains.exodus.entitystore.PersistentEntityStores
 | 
					import jetbrains.exodus.entitystore.PersistentEntityStores
 | 
				
			||||||
import jetbrains.exodus.entitystore.StoreTransaction
 | 
					import jetbrains.exodus.entitystore.StoreTransaction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : AutoCloseable {
 | 
					class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : AutoCloseable {
 | 
				
			||||||
  private val fileStorePath = corePlugin.pluginDataPath.resolve("persistence/${fileStoreName}")
 | 
					  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 ->
 | 
					  fun create(entityTypeName: String, populate: Entity.() -> Unit) = transact { tx ->
 | 
				
			||||||
    val entity = tx.newEntity(entityTypeName)
 | 
					    val entity = tx.newEntity(entityTypeName)
 | 
				
			||||||
    populate(entity)
 | 
					    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) }
 | 
					    transact { tx -> tx.find(entityTypeName, propertyName, value) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  override fun close() {
 | 
					  override fun close() {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user