Skip to content

Commit

Permalink
Merge pull request #106 from JetBrains/automerge-fix
Browse files Browse the repository at this point in the history
Fix of automerge of entity changes after rollback
  • Loading branch information
andrii0lomakin authored Sep 18, 2024
2 parents 5f94388 + a9cffe7 commit 404b971
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 52 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext.kotlin_version = '1.8.10'
ext.exodus_version = '9.9.103'
ext.exodus_version = '9.9.105'
ext.dokka_version = '1.7.20'
ext.log4j_version = '2.17.1'
ext.google_truth_version = '1.4.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ interface TransientEntity : Entity {

override fun getStore(): TransientEntityStore

fun resetIfNew()

fun generateIdIfNew()

fun getLinksSize(linkName: String): Long

fun hasChanges(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ open class PersistentEntityIterableWrapper(
return wrappedIterable.concat(right.unwrap())
}

override fun skip(number: Int): EntityIterable {
return wrappedIterable.skip(number)
}

override fun take(number: Int): EntityIterable {
return wrappedIterable.take(number)
}

override fun skip(number: Int): EntityIterable {
return wrappedIterable.skip(number)
fun findLinks(entities: EntityIterable, linkName: String): EntityIterable {
//TODO move findLinks to interface
return (wrappedIterable as? OEntityIterableBase)?.findLinks(entities, linkName) ?: OEntityIterableBase.EMPTY
}

override fun distinct(): EntityIterable {
Expand Down Expand Up @@ -139,4 +144,4 @@ open class PersistentEntityIterableWrapper(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import jetbrains.exodus.database.LinkChangeType
import jetbrains.exodus.database.TransientEntity
import jetbrains.exodus.database.TransientEntityStore
import jetbrains.exodus.entitystore.*
import jetbrains.exodus.entitystore.iterate.EntityIterableBase
import jetbrains.exodus.entitystore.orientdb.OEntity
import jetbrains.exodus.entitystore.orientdb.iterate.OEntityIterableBase
import java.io.File
Expand Down Expand Up @@ -60,6 +61,12 @@ open class RemovedTransientEntity(
return transientEntity.store
}

override fun resetIfNew() {
}

override fun generateIdIfNew() {
}

override fun getLinksSize(linkName: String): Long {
throw IllegalStateException("Entity is removed")
}
Expand Down Expand Up @@ -254,6 +261,7 @@ open class RemovedTransientEntity(
val tx = threadSessionOrThrow as TransientSessionImpl
val tracker = threadSessionOrThrow.transientChangesTracker
val change = tracker.getChangedLinksDetailed(transientEntity)?.get(linkName)

if (change != null && change.changeType in listOf(LinkChangeType.ADD_AND_REMOVE, LinkChangeType.REMOVE)) {
val removedLinks = (change.removedEntities ?: setOf()).union(change.deletedEntities ?: setOf())
return RemovedLinksEntityIterable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import jetbrains.exodus.entitystore.*
import jetbrains.exodus.entitystore.iterate.EntityIterableBase
import jetbrains.exodus.entitystore.iterate.EntityIteratorWithPropId
import jetbrains.exodus.entitystore.orientdb.OEntity
import jetbrains.exodus.entitystore.orientdb.OEntityId
import jetbrains.exodus.entitystore.orientdb.OReadonlyVertexEntity
import java.io.File
import java.io.InputStream
Expand All @@ -31,24 +30,35 @@ import java.io.InputStream
*/
open class TransientEntityImpl : TransientEntity {
private val store: TransientEntityStore
private var persistentEntityId: OEntityId? = null
private var persistentEntity: OEntity? = null

private var readOnlyPersistentEntity: OReadonlyVertexEntity? = null
private val entityType: String by lazy(LazyThreadSafetyMode.NONE) { entity.type }

override var entity: OEntity
get() = readOnlyPersistentEntity
?: persistentEntityId?.let { persistentStore.currentTransaction?.getEntity(it) as OEntity }
?: throwWrappedPersistentEntityUndefined()
get() = persistentEntity ?: throwWrappedPersistentEntityUndefined()
set(persistentEntity) {
if (persistentEntity is OReadonlyVertexEntity) {
persistentEntityId = null
this.persistentEntity = null
readOnlyPersistentEntity = persistentEntity
} else {
persistentEntityId = persistentEntity.id
this.persistentEntity = persistentEntity
readOnlyPersistentEntity = null
}
}

override fun resetIfNew() {
if (isNew) {
persistentEntity?.resetToNew()
}
}

override fun generateIdIfNew() {
if (isNew) {
persistentEntity?.generateId()
}
}

private val threadSessionOrThrow: TransientSessionImpl
get() = store.threadSessionOrThrow as TransientSessionImpl

Expand Down Expand Up @@ -117,7 +127,7 @@ open class TransientEntityImpl : TransientEntity {

override fun getId(): EntityId {
return readOnlyPersistentEntity?.id
?: persistentEntityId
?: persistentEntity?.id
?: throwWrappedPersistentEntityUndefined()
}

Expand Down Expand Up @@ -195,7 +205,11 @@ open class TransientEntityImpl : TransientEntity {

override fun setLink(linkName: String, target: Entity?): Boolean {
if (target == null) return false
return threadSessionOrThrow.entitiesUpdater.setLink(this, linkName, target as TransientEntity)
return threadSessionOrThrow.entitiesUpdater.setLink(
this,
linkName,
target as TransientEntity
)
}

override fun setLink(linkName: String, targetId: EntityId): Boolean {
Expand All @@ -219,7 +233,11 @@ open class TransientEntityImpl : TransientEntity {

override fun addLink(linkName: String, target: Entity): Boolean {
assertIsMultipleLink(this, linkName)
return threadSessionOrThrow.entitiesUpdater.addLink(this, linkName, target as TransientEntity)
return threadSessionOrThrow.entitiesUpdater.addLink(
this,
linkName,
target as TransientEntity
)
}

override fun addLink(linkName: String, targetId: EntityId): Boolean {
Expand All @@ -234,7 +252,11 @@ open class TransientEntityImpl : TransientEntity {
}

override fun deleteLink(linkName: String, target: Entity): Boolean {
return threadSessionOrThrow.entitiesUpdater.deleteLink(this, linkName, target as TransientEntity)
return threadSessionOrThrow.entitiesUpdater.deleteLink(
this,
linkName,
target as TransientEntity
)
}

override fun deleteLink(linkName: String, targetId: EntityId): Boolean {
Expand Down Expand Up @@ -368,9 +390,14 @@ open class TransientEntityImpl : TransientEntity {
private fun getAddedRemovedLinks(linkNames: Set<String>, removed: Boolean): EntityIterable {
if (isNew) return UniversalEmptyEntityIterable

val changedLinksDetailed = threadSessionOrThrow.transientChangesTracker.getChangedLinksDetailed(this)
val changedLinksDetailed =
threadSessionOrThrow.transientChangesTracker.getChangedLinksDetailed(this)
return if (changedLinksDetailed != null) {
AddedOrRemovedLinksFromSetTransientEntityIterable.get(changedLinksDetailed, linkNames, removed)
AddedOrRemovedLinksFromSetTransientEntityIterable.get(
changedLinksDetailed,
linkNames,
removed
)
} else {
UniversalEmptyEntityIterable
}
Expand Down Expand Up @@ -403,7 +430,11 @@ open class TransientEntityImpl : TransientEntity {
override fun clearOneToMany(manyToOneLinkName: String, oneToManyLinkName: String) {
assertIsMultipleLink(this, oneToManyLinkName)

threadSessionOrThrow.entitiesUpdater.clearOneToMany(this, manyToOneLinkName, oneToManyLinkName)
threadSessionOrThrow.entitiesUpdater.clearOneToMany(
this,
manyToOneLinkName,
oneToManyLinkName
)
}

override fun createManyToMany(e1Toe2LinkName: String, e2Toe1LinkName: String, e2: Entity) {
Expand All @@ -425,10 +456,19 @@ open class TransientEntityImpl : TransientEntity {
}

override fun setOneToOne(e1Toe2LinkName: String, e2Toe1LinkName: String, e2: Entity?) {
threadSessionOrThrow.entitiesUpdater.setOneToOne(this, e1Toe2LinkName, e2Toe1LinkName, e2 as TransientEntity?)
threadSessionOrThrow.entitiesUpdater.setOneToOne(
this,
e1Toe2LinkName,
e2Toe1LinkName,
e2 as TransientEntity?
)
}

override fun removeOneToMany(manyToOneLinkName: String, oneToManyLinkName: String, many: Entity) {
override fun removeOneToMany(
manyToOneLinkName: String,
oneToManyLinkName: String,
many: Entity
) {
threadSessionOrThrow.entitiesUpdater.removeOneToMany(
this,
manyToOneLinkName,
Expand All @@ -438,14 +478,26 @@ open class TransientEntityImpl : TransientEntity {
}

override fun removeFromParent(parentToChildLinkName: String, childToParentLinkName: String) {
threadSessionOrThrow.entitiesUpdater.removeFromParent(this, parentToChildLinkName, childToParentLinkName)
threadSessionOrThrow.entitiesUpdater.removeFromParent(
this,
parentToChildLinkName,
childToParentLinkName
)
}

override fun removeChild(parentToChildLinkName: String, childToParentLinkName: String) {
threadSessionOrThrow.entitiesUpdater.removeChild(this, parentToChildLinkName, childToParentLinkName)
threadSessionOrThrow.entitiesUpdater.removeChild(
this,
parentToChildLinkName,
childToParentLinkName
)
}

override fun setChild(parentToChildLinkName: String, childToParentLinkName: String, child: Entity) {
override fun setChild(
parentToChildLinkName: String,
childToParentLinkName: String,
child: Entity
) {
threadSessionOrThrow.entitiesUpdater.setChild(
this,
parentToChildLinkName,
Expand All @@ -458,7 +510,11 @@ open class TransientEntityImpl : TransientEntity {
threadSessionOrThrow.entitiesUpdater.clearChildren(this, parentToChildLinkName)
}

override fun addChild(parentToChildLinkName: String, childToParentLinkName: String, child: Entity) {
override fun addChild(
parentToChildLinkName: String,
childToParentLinkName: String,
child: Entity
) {
threadSessionOrThrow.entitiesUpdater.addChild(
this,
parentToChildLinkName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class TransientEntityOriginalValuesProviderImpl(private val session: TransientSt
override fun getOriginalPropertyValue(e: TransientEntity, propertyName: String): Comparable<*>? {
val session = ODatabaseSession.getActiveSession()
val id = e.entity.id.asOId()

if (id.isNew) {
return null
}

val oVertex = session.load<OVertex>(id)
val onLoadValue = oVertex.getPropertyOnLoadValue<Any>(propertyName)
return if (onLoadValue is MutableSet<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import jetbrains.exodus.database.TransientEntityStore
import jetbrains.exodus.database.TransientStoreSession
import jetbrains.exodus.entitystore.QueryCancellingPolicy
import jetbrains.exodus.entitystore.orientdb.OStoreTransaction
import kotlin.math.log

internal object TransientEntityStoreExt {
fun <T> transactional(
Expand Down Expand Up @@ -94,7 +95,10 @@ internal object TransientEntityStoreExt {
try {
session.commit()
wasEx = false
} finally {
} catch (e: Exception) {
e.printStackTrace()
throw e
}finally {
if (wasEx && session.isOpened) {
session.abort()
}
Expand Down
Loading

0 comments on commit 404b971

Please sign in to comment.