mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2026-04-02 04:40:19 +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:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user