Skip to content

Commit

Permalink
drop support of persistent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
lehvolk committed Nov 3, 2020
1 parent 63f6c11 commit 915882d
Show file tree
Hide file tree
Showing 27 changed files with 228 additions and 406 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ object ConstraintsUtil {
.filterIsInstance<TransientEntity>()
.filter { sourceEntity -> !sourceEntity.isRemoved && targetEntity !in sourceEntity.getRemovedLinks(linkName) }
.takeWhile { sourceEntity ->
val violation = incomingLinkViolation ?:
createIncomingLinkViolation(sourceEntity, linkName)
val violation = incomingLinkViolation
?: createIncomingLinkViolation(sourceEntity, linkName)
.also { newViolation ->
incomingLinkViolation = newViolation
}
Expand All @@ -90,15 +90,15 @@ object ConstraintsUtil {
}

private fun createIncomingLinkViolation(linkSource: TransientEntity, linkName: String): IncomingLinkViolation {
return linkSource.persistentClassInstance
?.createIncomingLinkViolation(linkName)
return linkSource.lifecycle
?.createIncomingLinkViolation(linkName, linkSource)
?: IncomingLinkViolation(linkName)
}

private fun createIncomingLinksException(targetEntity: TransientEntity, badIncomingLinks: List<IncomingLinkViolation>): DataIntegrityViolationException {
val persistentClassInstance = targetEntity.persistentClassInstance
return if (persistentClassInstance != null) {
persistentClassInstance.createIncomingLinksException(badIncomingLinks, targetEntity)
val lifecycle = targetEntity.lifecycle
return if (lifecycle != null) {
lifecycle.createIncomingLinksException(badIncomingLinks, targetEntity)
} else {
val linkDescriptions = badIncomingLinks.map { it.description }
val displayName = targetEntity.debugPresentation
Expand Down Expand Up @@ -418,8 +418,7 @@ object ConstraintsUtil {
?.let { entityMetaData -> changedEntity to entityMetaData }
}
.flatMap { (changedEntity, entityMetaData) ->
val persistentClass = changedEntity.persistentClassInstance
val propertyConstraints = persistentClass?.propertyConstraints.orEmpty()
val propertyConstraints = changedEntity.lifecycle?.propertyConstraints(changedEntity).orEmpty()

getChangedPropertiesWithConstraints(tracker, changedEntity, propertyConstraints)
.mapNotNull { (propertyName, constraints) ->
Expand All @@ -430,7 +429,10 @@ object ConstraintsUtil {
val type = getPropertyType(propertyMetaData)
val propertyValue = getPropertyValue(changedEntity, propertyName, type)
constraints.asSequence()
.mapNotNull { it.check(changedEntity, propertyMetaData, propertyValue) }
.mapNotNull {
it as PropertyConstraint<Any?>
it.check(changedEntity, propertyMetaData, propertyValue)
}
}
}
.toCollection(HashSetDecorator())
Expand All @@ -439,8 +441,8 @@ object ConstraintsUtil {
private fun getChangedPropertiesWithConstraints(
tracker: TransientChangesTracker,
changedEntity: TransientEntity,
constrainedProperties: Map<String, Iterable<PropertyConstraint<Any?>>>
): Sequence<Pair<String, Iterable<PropertyConstraint<Any?>>>> {
constrainedProperties: Map<String, Iterable<PropertyConstraint<*>>>
): Sequence<Pair<String, Iterable<PropertyConstraint<*>>>> {
return if (changedEntity.isNew) {
// All properties with constraints
constrainedProperties
Expand Down Expand Up @@ -510,12 +512,9 @@ object ConstraintsUtil {

return if (entity.isNew || name in changedProperties.orEmpty()) {
val type = getPropertyType(entityMetaData.getPropertyMetaData(name))
val displayName = entity.persistentClassInstance
?.getPropertyDisplayName(name)
?: name

if (isPropertyUndefined(entity, name, type)) {
NullPropertyException(entity, displayName)
NullPropertyException(entity, name)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2006 - 2020 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.teamsys.dnq.database

import jetbrains.exodus.database.exceptions.CantRemoveEntityException
import jetbrains.exodus.database.exceptions.DataIntegrityViolationException
import jetbrains.exodus.entitystore.Entity
import jetbrains.exodus.query.metadata.EntityMetaData

interface EntityLifecycle {

fun onBeforeFlush(entity: Entity)

fun onRemove(entity: Entity)

fun requireIfConstraints(entity: Entity): Map<String, Iterable<PropertyConstraint<*>>>

fun propertyConstraints(entity: Entity): Map<String, Iterable<PropertyConstraint<*>>>

fun getRequiredIfProperties(emd: EntityMetaData, entity: Entity): Set<String>

fun createIncomingLinksException(linkViolations: List<IncomingLinkViolation>, entity: Entity): DataIntegrityViolationException

fun createIncomingLinkViolation(linkName: String, entity: Entity): IncomingLinkViolation?

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.jetbrains.teamsys.dnq.database

import com.jetbrains.teamsys.dnq.association.AssociationSemantics
import jetbrains.exodus.core.dataStructures.decorators.HashSetDecorator
import jetbrains.exodus.database.LinkChange
import jetbrains.exodus.database.TransientChangesTracker
import jetbrains.exodus.database.TransientEntity
Expand All @@ -27,22 +26,10 @@ object EntityMetaDataUtils {

@JvmStatic
fun getRequiredIfProperties(emd: EntityMetaData, e: Entity): Set<String> {
val persistentClassInstance = (e as? TransientEntity)
?.persistentClassInstance
val lifecycle = (e as? TransientEntity)
?.lifecycle
?: return emptySet()

return emd.getRequiredIfProperties(e)
.asSequence()
.filter { persistentClassInstance.isPropertyRequired(it, e) }
.toCollection(HashSetDecorator<String>())

}

@JvmStatic
fun getPropertyConstraints(e: Entity): Map<String, Iterable<PropertyConstraint<*>>> {
return (e as? TransientEntity)?.persistentClassInstance
?.propertyConstraints
.orEmpty()
return lifecycle.getRequiredIfProperties(emd, e)
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object EntityOperations {
val entityMetaData = modelMetaData.getEntityMetaData(txnEntity.type)
if (entityMetaData != null) {
if (callDestructorPhase) {
txnEntity.persistentClassInstance?.destructor(txnEntity)
txnEntity.lifecycle?.onRemove(txnEntity)
}
processed.add(txnEntity)
// remove associations and cascade delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ open class TransientEntityStoreImpl : TransientEntityStore {

private lateinit var _persistentStore: PersistentEntityStore

var entityLifecycle: EntityLifecycle? = null

/**
* Must be injected.
*/
Expand Down Expand Up @@ -66,8 +68,6 @@ open class TransientEntityStoreImpl : TransientEntityStore {
protected set
private var closed = false
private val enumCache = ConcurrentHashMap<String, Entity>()
private val persistentClassInstanceCache = ConcurrentHashMap<String, BasePersistentClassImpl>()
private val persistentClassInstances = ConcurrentHashMap<Class<*>, BasePersistentClassImpl>()

// fair flushLock
internal val flushLock = ReentrantLock(true)
Expand Down Expand Up @@ -305,23 +305,6 @@ open class TransientEntityStoreImpl : TransientEntityStore {

private fun getEnumKey(className: String, propName: String) = "$propName@$className"

fun getCachedPersistentClassInstance(entityType: String): BasePersistentClassImpl? {
return persistentClassInstanceCache[entityType]
}

fun getCachedPersistentClassInstance(entityType: Class<out BasePersistentClassImpl>): BasePersistentClassImpl? {
return persistentClassInstances[entityType]
}

fun setCachedPersistentClassInstance(entityType: String, instance: BasePersistentClassImpl) {
persistentClassInstanceCache[entityType] = instance
val clazz = instance.javaClass
if (persistentClassInstances[clazz] != null) {
logger.warn { "Persistent class instance already registered for: ${clazz.simpleName}" }
}
persistentClassInstances[clazz] = instance
}

private fun assertOpen() {
// this flag isn't even volatile, but this is legacy behavior
if (closed) throw IllegalStateException("Transient store is closed.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ val PersistentEntityStore.currentTransactionOrThrow
get() = currentTransaction as PersistentStoreTransaction?
?: throw IllegalStateException("There is no persistent transaction in current thread")

val TransientEntity.persistentClassInstance: BasePersistentClassImpl?
get() = (store as? TransientEntityStoreImpl)?.getCachedPersistentClassInstance(type)
val TransientEntity.lifecycle: EntityLifecycle?
get() = (store as? TransientEntityStoreImpl)?.entityLifecycle

fun TransientEntity.getLinkEx(linkName: String, session: TransientStoreSession) =
if (this is TransientEntityImpl) getLink(linkName, session) else getLink(linkName)
Original file line number Diff line number Diff line change
Expand Up @@ -470,18 +470,18 @@ class TransientSessionImpl(private val store: TransientEntityStoreImpl, private
val modelMetaData = store.modelMetaData

if (quietFlush || /* for tests only */ modelMetaData == null) {
logger.warn("Quiet intermediate commit: skip before flush triggers. " + this)
logger.warn("Quiet intermediate commit: skip before flush triggers. $this")
return
}

logger.debug { "Execute before flush triggers. ${this}" }
logger.debug { "Execute before flush triggers. $this" }

val exceptions = changedEntities
.asSequence()
.filter { !it.isRemoved }
.flatMap { entity ->
try {
entity.persistentClassInstance?.executeBeforeFlushTrigger(entity)
entity.lifecycle?.onBeforeFlush(entity)
emptySequence<DataIntegrityViolationException>()
} catch (cve: ConstraintsValidationException) {
cve.causes.asSequence()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@ object TransientStoreUtil {
}
}

@JvmStatic
@Deprecated("Use entity.persistentClassInstance instead", ReplaceWith("entity.persistentClassInstance", "com.jetbrains.teamsys.dnq.database.TransientEntityUtilKt.getPersistentClassInstance"))
fun getPersistentClassInstance(entity: Entity): BasePersistentClassImpl? {
return (entity as TransientEntity).persistentClassInstance
}

@JvmStatic
fun getSize(it: Iterable<Entity>?): Int {
val iterable = if (it is StaticTypedEntityIterable) it.instantiate() else it
Expand Down
1 change: 0 additions & 1 deletion dnq/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies {
compile 'com.google.guava:guava:28.0-jre'
compile "org.javassist:javassist:3.23.1-GA"
compile "javax.servlet:javax.servlet-api:3.0.1"
compile 'com.github.mazine:infer-hierarchy-type-parameter:1.2.0'

testCompile project(':dnq')
testCompile 'com.google.truth:truth:0.35'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlinx.dnq.util
package kotlinx.dnq

import com.jetbrains.teamsys.dnq.database.BasePersistentClassImpl
interface NamedXdEntity {

val displayName: String

open class CommonBasePersistentClass : BasePersistentClassImpl() {
override fun run() = Unit
}
Loading

0 comments on commit 915882d

Please sign in to comment.