Skip to content

Commit

Permalink
synchronization fix & test
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 26, 2024
1 parent 2c25883 commit 6c910bf
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/sirius/dorm/object/Attribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Attribute(property: PropertyEntity?, var value: Any) : Property(property)
return value
}

override fun init(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) {
override fun init(propertyDescriptor: PropertyDescriptor<Any>, value: Any?, objectManager: ObjectManager) {
this.value = value!!
}

override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) : Boolean {
override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?, objectManager: ObjectManager) : Boolean {
propertyDescriptor.validate(value)

return if ( value != this.value ) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/org/sirius/dorm/object/DataObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class DataObject(val type: ObjectDescriptor, status: Status, var state : ObjectS
}

var id: Long
get() = values[0].get(objectManager) as Long
set(value) { values[0].set(type.properties[0], value) }
get() = (values[0] as Attribute).value as Long
set(value) { (values[0]as Attribute).value = value }


fun snapshot() : List<Any> {
Expand All @@ -65,18 +65,18 @@ class DataObject(val type: ObjectDescriptor, status: Status, var state : ObjectS
}

fun <T: Relation>relation(name: String) : T {
return values[property(name).index] as T
return (values[property(name).index] as Relation).get(objectManager) as T
}

fun <T: Relation>relation(index: Int) : T {
return values[index] as T
return (values[index] as Relation).get(objectManager) as T
}

operator fun get(name: String) : Any? {
return values[property(name).index].get(objectManager)
}

operator fun set(name: String, value: Any) {
operator fun set(name: String, value: Any?) {
val property = property(name)

// take snapshot in any case
Expand All @@ -86,7 +86,7 @@ class DataObject(val type: ObjectDescriptor, status: Status, var state : ObjectS

// set raw value

values[property.index].set(property, value)
values[property.index].set(property, value, objectManager)
}

// override Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o
return this
}

override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) : Boolean {
override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?, objectManager: ObjectManager) : Boolean {
throw Error("relations don't allow to be set")
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/sirius/dorm/object/Property.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import org.sirius.dorm.persistence.entity.PropertyEntity
abstract class Property(var property: PropertyEntity?) {
abstract fun get(objectManager: ObjectManager) : Any?

abstract fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) : Boolean
abstract fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?, objectManager: ObjectManager) : Boolean

open fun init(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) {
this.set(propertyDescriptor, value)
open fun init(propertyDescriptor: PropertyDescriptor<Any>, value: Any?, objectManager: ObjectManager) {
this.set(propertyDescriptor, value, objectManager)
}

// snapshot stuff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val
return target
}

override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) : Boolean {
override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?, objectManager: ObjectManager) : Boolean {
// check loaded

if ( !isLoaded())
load(objectManager)

if ( value !== this.target) {
var inverse = inverseRelation(this.target)
if ( inverse !== null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.sirius.common.tracer.Tracer
import org.sirius.dorm.`object`.DataObject
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import org.sirius.dorm.ObjectManager
import org.sirius.dorm.transaction.Status
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -93,7 +94,7 @@ class DataObjectMapper() {
obj.entity = entity
obj["id"] = id

val reader = reader4(objectDescriptor)
val reader = reader4(objectDescriptor, state.objectManager)

for ( attribute in entity.properties)
reader.read(obj, objectDescriptor.property(attribute.attribute), attribute)
Expand All @@ -116,7 +117,7 @@ class DataObjectMapper() {

obj.id = id

val reader = reader4(objectDescriptor)
val reader = reader4(objectDescriptor, state.objectManager)

for ( i in start..end) {
val attribute = attributes[i]
Expand All @@ -132,8 +133,8 @@ class DataObjectMapper() {

// private

private fun reader4(objectDescriptor: ObjectDescriptor) : ObjectReader {
return reader.getOrPut(objectDescriptor.name) { -> ObjectReader(objectDescriptor) }
private fun reader4(objectDescriptor: ObjectDescriptor, objectManager: ObjectManager) : ObjectReader {
return reader.getOrPut(objectDescriptor.name) { -> ObjectReader(objectDescriptor, objectManager) }
}

private fun jsonReader4(objectDescriptor: ObjectDescriptor) : JSONReader {
Expand Down
21 changes: 11 additions & 10 deletions src/main/kotlin/org/sirius/dorm/persistence/ObjectReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package org.sirius.dorm.persistence
* All rights reserved
*/

import org.sirius.dorm.ObjectManager
import org.sirius.dorm.model.ObjectDescriptor
import org.sirius.dorm.model.PropertyDescriptor
import org.sirius.dorm.`object`.DataObject
Expand All @@ -13,13 +14,13 @@ import org.sirius.dorm.persistence.entity.PropertyEntity

typealias PropertyReader = (obj: DataObject, attribute: PropertyEntity) -> Unit

class ObjectReader(descriptor: ObjectDescriptor) {
class ObjectReader(descriptor: ObjectDescriptor, objectManager: ObjectManager) {
// instance data

private val reader: Array<PropertyReader> =
descriptor.properties
.filter { property -> property.name !== "id" }
.map { property -> reader4(property) }.toTypedArray()
.map { property -> reader4(property, objectManager) }.toTypedArray()

// public

Expand Down Expand Up @@ -52,7 +53,7 @@ class ObjectReader(descriptor: ObjectDescriptor) {
}
}

fun reader4(property: PropertyDescriptor<Any>): PropertyReader {
fun reader4(property: PropertyDescriptor<Any>, objectManager: ObjectManager): PropertyReader {
if ( !property.isAttribute()) {
return { obj: DataObject, attribute: PropertyEntity ->
//obj.values[property.index].property = attribute
Expand All @@ -61,31 +62,31 @@ class ObjectReader(descriptor: ObjectDescriptor) {
else
return when (property.asAttribute().baseType()) {
Boolean::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.intValue == 1)
obj.values[property.index].init(property, attribute.intValue == 1, objectManager)
}

String::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.stringValue)
obj.values[property.index].init(property, attribute.stringValue, objectManager)
}

Short::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.intValue.toShort())
obj.values[property.index].init(property, attribute.intValue.toShort(), objectManager)
}

Integer::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.intValue)
obj.values[property.index].init(property, attribute.intValue, objectManager)
}

Long::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.intValue.toLong())
obj.values[property.index].init(property, attribute.intValue.toLong(), objectManager)
}

Float::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.doubleValue.toFloat())
obj.values[property.index].init(property, attribute.doubleValue.toFloat(), objectManager)
}

Double::class.javaObjectType -> { obj: DataObject, attribute: PropertyEntity ->
obj.values[property.index].init(property, attribute.doubleValue)
obj.values[property.index].init(property, attribute.doubleValue, objectManager)
}

else -> { _: DataObject, _: PropertyEntity ->
Expand Down
71 changes: 57 additions & 14 deletions src/test/kotlin/org/sirius/dorm/DORMBenchmark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import org.sirius.dorm.query.eq
import jakarta.persistence.*
import jakarta.persistence.criteria.CriteriaBuilder
import org.junit.jupiter.api.Test
import org.sirius.common.type.base.*
import org.sirius.dorm.model.Multiplicity
import org.sirius.dorm.model.ObjectDescriptor

@Entity
@Table(name="HOBBY")
Expand All @@ -21,10 +24,9 @@ data class HobbyEntity(
var name : String,

@ManyToMany(mappedBy = "hobbies")
val persons : MutableSet<PersonEntity>
) {
val persons : MutableSet<PersonEntity>) {
override fun hashCode(): Int {
return 1
return id.hashCode()
}
}

Expand All @@ -41,14 +43,26 @@ data class PersonEntity(
@Column(name = "AGE")
var age : Int,

@Column(name = "V1")
var v1 : String,
@Column(name = "BOOL")
var bool : Boolean,

@Column(name = "STRING")
var string : String,

@Column(name = "SHORT")
var short : Short,

@Column(name = "INT")
var int : Int,

@Column(name = "V2")
var v2 : String,
@Column(name = "LONG")
var long : Long,

@Column(name = "V3")
var v3 : String,
@Column(name = "FLOAT")
var float : Float,

@Column(name = "DOUBLE")
var double : Double,

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
Expand All @@ -57,7 +71,11 @@ data class PersonEntity(
inverseJoinColumns = [JoinColumn(name = "hobby_id")]
)
val hobbies : MutableSet<HobbyEntity>
)
) {
override fun hashCode(): Int {
return id.hashCode()
}
}

internal class DORMBenchmark : AbstractTest() {
// instance data
Expand Down Expand Up @@ -85,6 +103,30 @@ internal class DORMBenchmark : AbstractTest() {
}
}

@Test
fun tes() {
var person : ObjectDescriptor?

withTransaction {
objectManager.type("small-person")
.attribute("name", string())
.register()
}

val objects = 2000

// create

measure("create ${objects} objects ", objects) {
person = objectManager.findDescriptor("small-person")
for (i in 1..objects) {
val person1 = objectManager.create(person!!)

person1["name"] = "Andi"
}
}
}

// test

//@Test
Expand All @@ -97,7 +139,7 @@ internal class DORMBenchmark : AbstractTest() {
entityManager.persist(hobby1)
entityManager.persist(hobby2)

val person = PersonEntity(0, "Andi", 58, "v1", "v2", "v3", HashSet())
val person = PersonEntity(0, "Andi", 58, false, "strung", 1, 1, 1, 1.0f, 1.0, HashSet())

entityManager.persist(person)

Expand Down Expand Up @@ -129,7 +171,7 @@ internal class DORMBenchmark : AbstractTest() {
// warm up

withTransaction {
val person = PersonEntity(0,"Andi", 58, "v1", "v2", "v3", mutableSetOf())
val person = PersonEntity(0,"Andi", 58, false, "strung", 1, 1, 1, 1.0f, 1.0, HashSet())
val hobby = HobbyEntity(0, "angeln", mutableSetOf())

entityManager.persist(hobby)
Expand All @@ -147,7 +189,7 @@ internal class DORMBenchmark : AbstractTest() {
// warm up

withTransaction {
entityManager.persist(PersonEntity(0,"Andi", 58, "v1", "v2", "v3", mutableSetOf()))
entityManager.persist(PersonEntity(0,"Andi", 58, false, "strung", 1, 1, 1, 1.0f, 1.0, HashSet()))

val builder: CriteriaBuilder = entityManager.criteriaBuilder

Expand All @@ -173,7 +215,7 @@ internal class DORMBenchmark : AbstractTest() {

measure("create ${objects} objects ", objects) {
for (i in 1..objects) {
entityManager.persist(PersonEntity(0,"Andi", 58, "v1", "v2", "v3", mutableSetOf()))
entityManager.persist(PersonEntity(0,"Andi", 58, false, "strung", 1, 1, 1, 1.0f, 1.0, HashSet()))
}
}

Expand Down Expand Up @@ -334,4 +376,5 @@ internal class DORMBenchmark : AbstractTest() {
person["name"] = "Changed"
}
}

}
Loading

0 comments on commit 6c910bf

Please sign in to comment.