Add Reference/Collections
This commit is contained in:
2
.idea/kotlinc.xml
generated
2
.idea/kotlinc.xml
generated
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="KotlinJpsPluginSettings">
|
<component name="KotlinJpsPluginSettings">
|
||||||
<option name="version" value="1.9.23" />
|
<option name="version" value="2.0.0-RC2" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "1.9.23"
|
kotlin("jvm") version "2.0.0-RC2"
|
||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
id("signing")
|
id("signing")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class Datastore(
|
|||||||
private val fileManager = FileManager(directory)
|
private val fileManager = FileManager(directory)
|
||||||
private val transactionFormatter = DecimalFormat("#")
|
private val transactionFormatter = DecimalFormat("#")
|
||||||
private var nextTransactionNumber = 1L
|
private var nextTransactionNumber = 1L
|
||||||
private val data: MutableMap<Class<*>, TypeData> = ConcurrentHashMap()
|
private var data: MutableMap<Class<*>, TypeData> = ConcurrentHashMap()
|
||||||
private val indexes: MutableMap<Class<*>, MutableMap<String, PersistableIndex>> = ConcurrentHashMap()
|
private val indexes: MutableMap<Class<*>, MutableMap<String, PersistableIndex>> = ConcurrentHashMap()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -66,6 +66,18 @@ class Datastore(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return data[javaClass]!!.nextId.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getNextIdAndIncrement(javaClass: Class<Persistable>): Long {
|
||||||
|
if (data[javaClass] == null) {
|
||||||
|
synchronized(this) {
|
||||||
|
if (data[javaClass] == null) {
|
||||||
|
data[javaClass] = TypeData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return data[javaClass]!!.nextId.getAndIncrement()
|
return data[javaClass]!!.nextId.getAndIncrement()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,8 +267,7 @@ class Datastore(
|
|||||||
val versionNumber = ois.readInt()
|
val versionNumber = ois.readInt()
|
||||||
check(versionNumber == 1) { "Unsupported version number: $versionNumber" }
|
check(versionNumber == 1) { "Unsupported version number: $versionNumber" }
|
||||||
nextTransactionNumber = ois.readLong() + 1
|
nextTransactionNumber = ois.readLong() + 1
|
||||||
data.clear()
|
data = ois.readObject() as MutableMap<Class<*>, TypeData>
|
||||||
data.putAll(ois.readObject() as MutableMap<Class<*>, TypeData>)
|
|
||||||
|
|
||||||
val foundIndexes = mutableMapOf<Class<*>, MutableList<String>>()
|
val foundIndexes = mutableMapOf<Class<*>, MutableList<String>>()
|
||||||
val numberOfClassesWithIndex = ois.readInt()
|
val numberOfClassesWithIndex = ois.readInt()
|
||||||
|
|||||||
@@ -1,133 +0,0 @@
|
|||||||
package nl.astraeus.nl.astraeus.persistence
|
|
||||||
|
|
||||||
import java.io.Serializable
|
|
||||||
import kotlin.reflect.KProperty
|
|
||||||
|
|
||||||
class Reference<S : Persistable, H : Persistable>(
|
|
||||||
val cls: Class<S>,
|
|
||||||
) : Serializable {
|
|
||||||
companion object {
|
|
||||||
private const val serialVersionUID: Long = 1L
|
|
||||||
}
|
|
||||||
|
|
||||||
var id: Long = 0
|
|
||||||
|
|
||||||
operator fun getValue(thisRef: H, property: KProperty<*>): S {
|
|
||||||
return currentTransaction()?.find(cls.kotlin, id) ?: throw IllegalStateException("Reference not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
operator fun setValue(thisRef: H, property: KProperty<*>, value: S) {
|
|
||||||
id = value.id
|
|
||||||
// todo: only store if not already stored?
|
|
||||||
currentTransaction()?.store(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ListReference<S : Persistable, H : Persistable>(
|
|
||||||
val cls: Class<S>,
|
|
||||||
) : Serializable {
|
|
||||||
companion object {
|
|
||||||
private const val serialVersionUID: Long = 1L
|
|
||||||
}
|
|
||||||
|
|
||||||
var ids: ReferenceList<S> = ReferenceList(cls)
|
|
||||||
|
|
||||||
operator fun getValue(thisRef: H, property: KProperty<*>): ReferenceList<S> {
|
|
||||||
return ids
|
|
||||||
}
|
|
||||||
|
|
||||||
operator fun setValue(thisRef: H, property: KProperty<*>, value: List<S>) {
|
|
||||||
this.ids.clear()
|
|
||||||
this.ids.addAll(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ReferenceList<T : Persistable>(
|
|
||||||
val cls: Class<T>,
|
|
||||||
) : MutableList<T> {
|
|
||||||
val ids = ArrayList<Long>()
|
|
||||||
|
|
||||||
private fun checkElementIsPersisted(element: T) {
|
|
||||||
if (currentTransaction()?.find(cls.kotlin, element.id) == null) {
|
|
||||||
currentTransaction()?.store(element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override val size: Int = ids.size
|
|
||||||
override fun clear() = ids.clear()
|
|
||||||
|
|
||||||
override fun addAll(elements: Collection<T>): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addAll(index: Int, elements: Collection<T>): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun add(index: Int, element: T) {
|
|
||||||
ids.add(index, element.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun add(element: T): Boolean {
|
|
||||||
return ids.add(element.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun get(index: Int): T = currentTransaction()?.find(cls.kotlin, ids[index]) ?: throw IllegalStateException("Reference not found")
|
|
||||||
|
|
||||||
override fun isEmpty(): Boolean = ids.isEmpty()
|
|
||||||
|
|
||||||
override fun iterator(): MutableIterator<T> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun listIterator(): MutableListIterator<T> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun listIterator(index: Int): MutableListIterator<T> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun removeAt(index: Int): T {
|
|
||||||
val id = ids.removeAt(index)
|
|
||||||
|
|
||||||
return currentTransaction()?.find(cls.kotlin, id) ?: throw IllegalStateException("Reference not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun set(index: Int, element: T): T {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun retainAll(elements: Collection<T>): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun removeAll(elements: Collection<T>): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun remove(element: T): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun subList(fromIndex: Int, toIndex: Int): MutableList<T> {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun lastIndexOf(element: T): Int {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun indexOf(element: T): Int {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun containsAll(elements: Collection<T>): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun contains(element: T): Boolean {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,7 @@ import kotlin.reflect.KClass
|
|||||||
|
|
||||||
inline fun <reified T : Persistable> Query.count(): Int = this.count(T::class)
|
inline fun <reified T : Persistable> Query.count(): Int = this.count(T::class)
|
||||||
inline fun <reified T : Persistable> Query.find(id: Long): T? = this.find(T::class, id)
|
inline fun <reified T : Persistable> Query.find(id: Long): T? = this.find(T::class, id)
|
||||||
|
inline fun <reified T : Persistable> Query.all(): List<T> = this.all(T::class)
|
||||||
inline fun <reified T : Persistable> Query.search(noinline search: (T) -> Boolean): List<T> =
|
inline fun <reified T : Persistable> Query.search(noinline search: (T) -> Boolean): List<T> =
|
||||||
this.search(T::class, search)
|
this.search(T::class, search)
|
||||||
inline fun <reified T : Persistable> Query.findByIndex(
|
inline fun <reified T : Persistable> Query.findByIndex(
|
||||||
@@ -42,6 +43,9 @@ open class Query(
|
|||||||
|
|
||||||
open fun <T : Persistable> find(clazz: KClass<T>, id: Long): T? = persistent.datastore.find(clazz, id)
|
open fun <T : Persistable> find(clazz: KClass<T>, id: Long): T? = persistent.datastore.find(clazz, id)
|
||||||
|
|
||||||
|
open fun <T : Persistable> all(clazz: Class<T>): List<T> = all(clazz.kotlin)
|
||||||
|
open fun <T : Persistable> all(clazz: KClass<T>): List<T> = search(clazz) { _ -> true }
|
||||||
|
|
||||||
open fun <T : Persistable> search(clazz: KClass<T>, search: (T) -> Boolean): List<T> = persistent.datastore.search(clazz, search)
|
open fun <T : Persistable> search(clazz: KClass<T>, search: (T) -> Boolean): List<T> = persistent.datastore.search(clazz, search)
|
||||||
|
|
||||||
fun <T : Persistable> findByIndex(
|
fun <T : Persistable> findByIndex(
|
||||||
@@ -99,7 +103,7 @@ class Transaction(
|
|||||||
|
|
||||||
fun store(obj: Persistable) {
|
fun store(obj: Persistable) {
|
||||||
if (obj.id == 0L) {
|
if (obj.id == 0L) {
|
||||||
obj.id = persistent.datastore.getNextId(obj.javaClass)
|
obj.id = persistent.datastore.getNextIdAndIncrement(obj.javaClass)
|
||||||
} else if (obj.id > persistent.datastore.getNextId(obj.javaClass)) {
|
} else if (obj.id > persistent.datastore.getNextId(obj.javaClass)) {
|
||||||
persistent.datastore.setMaxId(obj.javaClass, obj.id + 1)
|
persistent.datastore.setMaxId(obj.javaClass, obj.id + 1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package nl.astraeus.nl.astraeus.persistence.reference
|
||||||
|
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
class Reference<S : Persistable>(
|
||||||
|
val cls: Class<S>,
|
||||||
|
val setter: (Long?) -> Unit,
|
||||||
|
val getter: () -> Long?,
|
||||||
|
) {
|
||||||
|
|
||||||
|
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S? {
|
||||||
|
return currentTransaction()?.find(cls.kotlin, (getter() ?: 0L))
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S?) {
|
||||||
|
if (value != null) {
|
||||||
|
// todo: only store if not already stored?
|
||||||
|
currentTransaction()?.store(value)
|
||||||
|
}
|
||||||
|
setter(value?.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val serialVersionUID: Long = 1L
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package nl.astraeus.nl.astraeus.persistence.reference
|
||||||
|
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||||
|
import java.io.Serializable
|
||||||
|
|
||||||
|
inline fun <reified T : Persistable> referenceCollection(
|
||||||
|
ids: MutableCollection<Long> = mutableListOf()
|
||||||
|
) = ReferenceCollection(T::class.java, ids)
|
||||||
|
|
||||||
|
open class ReferenceCollection<T : Persistable>(
|
||||||
|
val cls: Class<T>,
|
||||||
|
val ids: MutableCollection<Long> = mutableListOf()
|
||||||
|
) : MutableCollection<T>, Serializable {
|
||||||
|
|
||||||
|
protected fun checkElementIsPersisted(element: T) {
|
||||||
|
if (currentTransaction()?.find(cls.kotlin, element.id) == null) {
|
||||||
|
currentTransaction()?.store(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override val size: Int
|
||||||
|
get() {
|
||||||
|
return ids.size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun clear() {
|
||||||
|
ids.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isEmpty(): Boolean = ids.isEmpty()
|
||||||
|
|
||||||
|
override fun iterator(): MutableIterator<T> {
|
||||||
|
return object : MutableIterator<T> {
|
||||||
|
private var idsIterator = ids.iterator()
|
||||||
|
|
||||||
|
override fun hasNext(): Boolean = idsIterator.hasNext()
|
||||||
|
|
||||||
|
override fun next(): T = idsIterator.next().let {
|
||||||
|
currentTransaction()?.find(cls.kotlin, it)
|
||||||
|
} ?: throw IllegalStateException("Reference not found")
|
||||||
|
|
||||||
|
override fun remove() = idsIterator.remove()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun retainAll(elements: Collection<T>): Boolean {
|
||||||
|
return ids.retainAll(elements.map { it.id }.toSet())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun removeAll(elements: Collection<T>): Boolean {
|
||||||
|
return ids.removeAll(elements.map { it.id }.toSet())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun remove(element: T): Boolean {
|
||||||
|
return ids.remove(element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun containsAll(elements: Collection<T>): Boolean {
|
||||||
|
return ids.containsAll(elements.map { it.id })
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun contains(element: T): Boolean {
|
||||||
|
return ids.contains(element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addAll(elements: Collection<T>): Boolean {
|
||||||
|
for (element in elements) {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
}
|
||||||
|
return ids.addAll(elements.map { it.id })
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun add(element: T): Boolean {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
return ids.add(element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val serialVersionUID: Long = 1L
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package nl.astraeus.nl.astraeus.persistence.reference
|
||||||
|
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||||
|
import java.io.Serializable
|
||||||
|
|
||||||
|
inline fun <reified T : Persistable> referenceList(
|
||||||
|
ids: MutableList<Long> = mutableListOf()
|
||||||
|
) = ReferenceList(T::class.java, ids)
|
||||||
|
|
||||||
|
class ReferenceList<T : Persistable>(
|
||||||
|
cls: Class<T>,
|
||||||
|
val idsList: MutableList<Long> = mutableListOf()
|
||||||
|
) : ReferenceCollection<T>(cls, idsList), MutableList<T>, Serializable {
|
||||||
|
|
||||||
|
override fun add(index: Int, element: T) {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
idsList.add(index, element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addAll(index: Int, elements: Collection<T>): Boolean {
|
||||||
|
for(element in elements) {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
}
|
||||||
|
return idsList.addAll(index, elements.map { it.id })
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun get(index: Int): T = currentTransaction()?.find(cls.kotlin, idsList[index]) ?: throw IllegalStateException("Reference not found")
|
||||||
|
override fun indexOf(element: T): Int {
|
||||||
|
return idsList.indexOf(element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isEmpty(): Boolean = ids.isEmpty()
|
||||||
|
override fun lastIndexOf(element: T): Int {
|
||||||
|
return idsList.lastIndexOf(element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun listIterator(): MutableListIterator<T> {
|
||||||
|
return ReferenceListIterator(cls, idsList)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun listIterator(index: Int): MutableListIterator<T> {
|
||||||
|
return ReferenceListIterator(cls, idsList, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun removeAt(index: Int): T {
|
||||||
|
val id = idsList.removeAt(index)
|
||||||
|
|
||||||
|
return currentTransaction()?.find(cls.kotlin, id) ?: throw IllegalStateException("Reference not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun subList(fromIndex: Int, toIndex: Int): MutableList<T> {
|
||||||
|
return ReferenceList(cls, idsList.subList(fromIndex, toIndex))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun set(index: Int, element: T): T {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
idsList[index] = element.id
|
||||||
|
return element
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val serialVersionUID: Long = 1L
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package nl.astraeus.nl.astraeus.persistence.reference
|
||||||
|
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||||
|
|
||||||
|
class ReferenceListIterator<T : Persistable>(
|
||||||
|
private val cls: Class<T>,
|
||||||
|
idsList: MutableList<Long>,
|
||||||
|
index: Int = 0
|
||||||
|
) : MutableListIterator<T> {
|
||||||
|
private val idsIterator = idsList.listIterator(index)
|
||||||
|
|
||||||
|
private fun checkElementIsPersisted(element: T) {
|
||||||
|
if (currentTransaction()?.find(cls.kotlin, element.id) == null) {
|
||||||
|
currentTransaction()?.store(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun add(element: T) {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
idsIterator.add(element.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hasNext(): Boolean = idsIterator.hasNext()
|
||||||
|
override fun hasPrevious(): Boolean = idsIterator.hasPrevious()
|
||||||
|
|
||||||
|
override fun next(): T = idsIterator.next().let {
|
||||||
|
currentTransaction()?.find(cls.kotlin, it)
|
||||||
|
} ?: throw IllegalStateException("Reference not found")
|
||||||
|
|
||||||
|
override fun nextIndex(): Int = idsIterator.nextIndex()
|
||||||
|
|
||||||
|
override fun previous(): T = idsIterator.previous().let {
|
||||||
|
currentTransaction()?.find(cls.kotlin, it)
|
||||||
|
} ?: throw IllegalStateException("Reference not found")
|
||||||
|
|
||||||
|
override fun previousIndex(): Int = idsIterator.previousIndex()
|
||||||
|
|
||||||
|
override fun remove() {
|
||||||
|
idsIterator.remove()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun set(element: T) {
|
||||||
|
checkElementIsPersisted(element)
|
||||||
|
idsIterator.set(element.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,12 @@ package nl.astraeus.persistence
|
|||||||
|
|
||||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
import nl.astraeus.nl.astraeus.persistence.Persistent
|
import nl.astraeus.nl.astraeus.persistence.Persistent
|
||||||
import nl.astraeus.nl.astraeus.persistence.Reference
|
|
||||||
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||||
import nl.astraeus.nl.astraeus.persistence.find
|
import nl.astraeus.nl.astraeus.persistence.find
|
||||||
import nl.astraeus.nl.astraeus.persistence.findByIndex
|
import nl.astraeus.nl.astraeus.persistence.findByIndex
|
||||||
import nl.astraeus.nl.astraeus.persistence.index
|
import nl.astraeus.nl.astraeus.persistence.index
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.reference.ReferenceCollection
|
||||||
import org.junit.jupiter.api.Assertions.assertNotNull
|
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||||
import org.junit.jupiter.api.Assertions.assertNull
|
import org.junit.jupiter.api.Assertions.assertNull
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -19,7 +20,11 @@ class TestPersistence {
|
|||||||
override var version: Long = 0,
|
override var version: Long = 0,
|
||||||
val name: String
|
val name: String
|
||||||
) : Persistable, Cloneable {
|
) : Persistable, Cloneable {
|
||||||
//var persons: MutableList<Person> by ListReference<Person, Company>(Person::class.java)
|
val personIds: MutableList<Long> = ArrayList()
|
||||||
|
val persons: MutableCollection<Person>
|
||||||
|
get() {
|
||||||
|
return ReferenceCollection(Person::class.java, personIds)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val serialVersionUID: Long = 1L
|
private const val serialVersionUID: Long = 1L
|
||||||
@@ -35,15 +40,23 @@ class TestPersistence {
|
|||||||
override var version: Long = 0,
|
override var version: Long = 0,
|
||||||
val name: String,
|
val name: String,
|
||||||
val age: Int,
|
val age: Int,
|
||||||
|
private var companyId: Long? = null
|
||||||
) : Persistable, Cloneable {
|
) : Persistable, Cloneable {
|
||||||
var company: Company by Reference<Company, Person>(Company::class.java)
|
var company: Company?
|
||||||
|
get() = currentTransaction()?.find(Company::class, companyId ?: 0L)
|
||||||
|
set(value) {
|
||||||
|
if (value != null) {
|
||||||
|
currentTransaction()?.store(value)
|
||||||
|
}
|
||||||
|
companyId = value?.id
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val serialVersionUID: Long = 1L
|
private const val serialVersionUID: Long = 1L
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Person(id=$id, version=$version, name='$name', age=$age)"
|
return "Person(id=$id, version=$version, name='$name', age=$age, company=$companyId)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
92
src/test/kotlin/nl/astraeus/persistence/TestReferences.kt
Normal file
92
src/test/kotlin/nl/astraeus/persistence/TestReferences.kt
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package nl.astraeus.persistence
|
||||||
|
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.Persistent
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||||
|
import nl.astraeus.nl.astraeus.persistence.reference.referenceCollection
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
class TestReferences {
|
||||||
|
|
||||||
|
class Company(
|
||||||
|
override var id: Long = 0,
|
||||||
|
override var version: Long = 0,
|
||||||
|
val name: String
|
||||||
|
) : Persistable, Cloneable {
|
||||||
|
val persons: MutableCollection<Person> = referenceCollection()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val serialVersionUID: Long = 1L
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "Company(id=$id, version=$version, name='$name', persons=${persons.size})"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Person(
|
||||||
|
override var id: Long = 0,
|
||||||
|
override var version: Long = 0,
|
||||||
|
val name: String,
|
||||||
|
val age: Int,
|
||||||
|
private var companyId: Long? = null
|
||||||
|
) : Persistable, Cloneable {
|
||||||
|
var company: Company?
|
||||||
|
get() = currentTransaction()?.find(Company::class, companyId ?: 0L)
|
||||||
|
set(value) {
|
||||||
|
if (value != null) {
|
||||||
|
currentTransaction()?.store(value)
|
||||||
|
}
|
||||||
|
companyId = value?.id
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val serialVersionUID: Long = 1L
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "Person(id=$id, version=$version, name='$name', age=$age, company=$companyId)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun showTransactions() {
|
||||||
|
val log = TransactionLog(File("data", "test-references"))
|
||||||
|
|
||||||
|
log.showTransactions()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSerializeDeSerializeReferenceList() {
|
||||||
|
val pst = Persistent(
|
||||||
|
directory = File("data", "test-references"),
|
||||||
|
)
|
||||||
|
|
||||||
|
pst.transaction {
|
||||||
|
val company = find(Company::class, 1L) ?: Company(
|
||||||
|
id = 1L,
|
||||||
|
name = "ACME"
|
||||||
|
)
|
||||||
|
|
||||||
|
if (company.persons.isEmpty()) {
|
||||||
|
val person = Person(
|
||||||
|
id = 0L,
|
||||||
|
name = "John Doe",
|
||||||
|
age = 25
|
||||||
|
)
|
||||||
|
person.company = company
|
||||||
|
store(person)
|
||||||
|
company.persons.add(person)
|
||||||
|
}
|
||||||
|
|
||||||
|
store(company)
|
||||||
|
|
||||||
|
for (person in company.persons) {
|
||||||
|
println("Person: $person")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ package nl.astraeus.persistence
|
|||||||
|
|
||||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||||
import nl.astraeus.nl.astraeus.persistence.Persistent
|
import nl.astraeus.nl.astraeus.persistence.Persistent
|
||||||
import nl.astraeus.nl.astraeus.persistence.Reference
|
import nl.astraeus.nl.astraeus.persistence.reference.Reference
|
||||||
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
||||||
import nl.astraeus.nl.astraeus.persistence.count
|
import nl.astraeus.nl.astraeus.persistence.count
|
||||||
import nl.astraeus.nl.astraeus.persistence.index
|
import nl.astraeus.nl.astraeus.persistence.index
|
||||||
@@ -36,8 +36,13 @@ class TestThreaded {
|
|||||||
override var version: Long = 0,
|
override var version: Long = 0,
|
||||||
val name: String,
|
val name: String,
|
||||||
val age: Int,
|
val age: Int,
|
||||||
|
var companyId: Long? = null
|
||||||
) : Persistable, Cloneable {
|
) : Persistable, Cloneable {
|
||||||
var company: Company by Reference<Company, Person>(Company::class.java)
|
var company: Company? by Reference(
|
||||||
|
Company::class.java,
|
||||||
|
{ id -> companyId = id },
|
||||||
|
{ companyId }
|
||||||
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val serialVersionUID: Long = 1L
|
private const val serialVersionUID: Long = 1L
|
||||||
|
|||||||
Reference in New Issue
Block a user