Skip to content

Commit

Permalink
Merge pull request #110 from JetBrains/adopt-orientdb-listeners-api-u…
Browse files Browse the repository at this point in the history
…pdate

Adopt orientdb listeners api update
  • Loading branch information
leostryuk authored Oct 17, 2024
2 parents 0443108 + 45d9ecf commit a8db159
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package jetbrains.exodus.database

import jetbrains.exodus.entitystore.orientdb.OEntityId
import jetbrains.exodus.entitystore.EntityId
import kotlin.reflect.KProperty

interface DNQListener<in T : Any> {
Expand All @@ -26,21 +26,86 @@ interface DNQListener<in T : Any> {
fun updatedSyncBeforeConstraints(old: T, current: T)
fun updatedSync(old: T, current: T)

fun removedSyncBeforeConstraints(removed: T, requestListenerStorage: () -> DnqListenerTransientData<T>)
fun removedSync(removed: OEntityId, requestListenerStorage: () -> DnqListenerTransientData<T>)
/**
* Processes an entity that has been removed before applying constraints. Links and properties of the removed entity are still available and can be stored in removedEntityData for further use.
*
* @param removed The entity that has been removed.
* @param removedEntityData Data related to the removed entity.
*/
fun removedSyncBeforeConstraints(removed: T, removedEntityData: RemovedEntityData<T>)
/**
* Processes an entity that has been removed. If any property or link is required in the removedSync handler, it should be stored in the removedSyncBeforeConstraints with removedEntityData.storeValue(...)
*
* @param removedEntityData Data related to the removed entity.
*/
fun removedSync(removedEntityData: RemovedEntityData<T>)
}

interface DnqListenerTransientData<out T> {
/**
* Interface representing data related to a removed entity.
*
* @param E The type of the removed entity.
*/
interface RemovedEntityData<out E> {

/**
* The entity that has been removed. Mostly it's {@link com.jetbrains.teamsys.dnq.database.RemovedTransientEntity}. Fields and links should be accessed only within removedSyncBeforeConstraints
*/
val removed: E
/**
* Unique identifier of the removed entity.
*/
val removedId: EntityId
/**
* Retrieves the value associated with the given property name.
*
* @param name The name of the property whose value is to be retrieved.
* @return The value of the specified property, or null if not found.
*/
fun <T> getValue(name: String): T?
/**
* Retrieves the value associated with the specified property.
*
* @param T the type of the property.
* @param property the property whose value is to be retrieved.
* @return the value of the specified property, or null if the property does not have a value.
*/
fun <T> getValue(property: KProperty<T>): T?
/**
* Stores a value associated with the given name.
*
* @param name The name with which the specified value is to be associated.
* @param value The value to be stored.
*/
fun <T> storeValue(name: String, value: T)
fun getRemoved(): T
fun setRemoved(entity: Any)
/**
* Stores a given value associated with the specified property.
*
* @param T The type of the property and value.
* @param property The property, whose associated value is to be stored.
* @param value The value to be stored.
*/
fun <T> storeValue(property: KProperty<T>, value: T)
}

open class BasicRemovedEntityData<out E>(override val removed: E, override val removedId: EntityId) : RemovedEntityData<E> {
private val data = HashMap<String, Any>()

override fun <T> getValue(name: String): T? {
@Suppress("UNCHECKED_CAST")
return data[name] as? T
}

override fun <T> getValue(property: KProperty<T>): T? {
@Suppress("UNCHECKED_CAST")
return data[property.name] as T?
}

fun <T> getValue(property: KProperty<T>): T? {
return getValue(property.name) as T?
override fun <T> storeValue(name: String, value: T) {
data[name] = value as Any
}

fun <T> storeValue(property: KProperty<T>, value: T) {
storeValue(property.name, value)
override fun <T> storeValue(property: KProperty<T>, value: T) {
data[property.name] = value as Any
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package jetbrains.exodus.database

import jetbrains.exodus.entitystore.Entity
import jetbrains.exodus.entitystore.EntityId
import jetbrains.exodus.entitystore.EntityIterable
import jetbrains.exodus.entitystore.StoreTransaction

Expand Down Expand Up @@ -75,7 +76,9 @@ interface TransientStoreSession : StoreTransaction {

fun setUpgradeHook(hook: Runnable?)

fun <T>getListenerTransientData(listener: DNQListener<*>): DnqListenerTransientData<T>
fun <T> createRemovedEntityData(listener: DNQListener<*>, entity: TransientEntity): BasicRemovedEntityData<T>

fun <T> getRemovedEntityData(listener: DNQListener<*>, entityId: EntityId): BasicRemovedEntityData<T>

val originalValuesProvider: TransientEntityOriginalValuesProvider
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ open class PersistentEntityIterableWrapper(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import jetbrains.exodus.database.*
import jetbrains.exodus.entitystore.*
import jetbrains.exodus.entitystore.orientdb.OStoreTransaction
import jetbrains.exodus.entitystore.orientdb.OVertexEntity
import kotlin.reflect.KProperty

class ReadOnlyTransientSession(
private val store: TransientEntityStoreImpl,
Expand Down Expand Up @@ -132,24 +131,13 @@ class ReadOnlyTransientSession(

override val entitiesUpdater = ReadonlyTransientEntitiesUpdater()

override fun <T>getListenerTransientData(listener: DNQListener<*>): DnqListenerTransientData<T> {
return object :DnqListenerTransientData<T> {
override fun <T> getValue(name: String) = null

override fun <T> storeValue(name: String, value: T) = Unit

override fun getRemoved(): T {
throw IllegalStateException("")
}

override fun <T> getValue(property: KProperty<T>): T? = null

override fun <T> storeValue(property: KProperty<T>, value: T) {
throw IllegalStateException("")
}
override fun <T> createRemovedEntityData(listener: DNQListener<*>, entity: TransientEntity): BasicRemovedEntityData<T> {
throw IllegalStateException("Transient session is readonly. There should not be any changes")
}

override fun setRemoved(entity: Any) = Unit
}
override fun <T> getRemovedEntityData(listener: DNQListener<*>, entityId: EntityId): BasicRemovedEntityData<T> {
throw IllegalStateException("Transient session is readonly. There should not be any changes")
}

override val originalValuesProvider = TransientEntityOriginalValuesProviderImpl(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ open class RemovedTransientEntity(

//region simple unwrapping


//endregion

override fun getProperty(propertyName: String): Comparable<*>? {
return entity.getProperty(propertyName)
}
Expand Down Expand Up @@ -271,6 +268,8 @@ open class RemovedTransientEntity(
return OEntityIterableBase.EMPTY
}

//endregion

override fun getLinks(linkNames: MutableCollection<String>): EntityIterable {
throw IllegalStateException("Entity is removed")
}
Expand Down Expand Up @@ -396,6 +395,7 @@ internal class RemovedLinksEntityIterable(
throw IllegalStateException("Must not be called")
}


override fun getFirst(): Entity {
return entities.first()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class TransientSessionImpl(
private var loadedIds: EntityIdSet = EntityIdSetFactory.newSet()
private val hashCode = (Math.random() * Integer.MAX_VALUE).toInt()
private var allowRunnables = true
internal val sessionListenersData =
IdentityHashMap<DNQListener<*>, DnqListenerTransientData<*>>()
internal val removedEntitiesData =
IdentityHashMap<DNQListener<*>, MutableMap<EntityId, RemovedEntityData<*>>>()

val stack = if (TransientEntityStoreImpl.logger.isDebugEnabled) Throwable() else null

Expand Down Expand Up @@ -540,13 +540,19 @@ class TransientSessionImpl(
}
}

override fun <T> getListenerTransientData(listener: DNQListener<*>): DnqListenerTransientData<T> {
val result = sessionListenersData.getOrPut(listener) {
DnqListenerTransientDataImpl<T>()
override fun <T> createRemovedEntityData(listener: DNQListener<*>, entity: TransientEntity): BasicRemovedEntityData<T> {
val map = removedEntitiesData.getOrPut(listener) {
hashMapOf()
}
val data = BasicRemovedEntityData(entity, entity.id)
map[entity.id] = data
@Suppress("UNCHECKED_CAST")
return data as BasicRemovedEntityData<T>
}

override fun <T> getRemovedEntityData(listener: DNQListener<*>, entityId: EntityId): BasicRemovedEntityData<T> {
@Suppress("UNCHECKED_CAST")
return result as DnqListenerTransientData<T>
return removedEntitiesData[listener]?.get(entityId) as BasicRemovedEntityData<T>
}

private fun addLoadedId(id: EntityId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package jetbrains.exodus.entitystore
import jetbrains.exodus.core.dataStructures.hash.HashMap
import jetbrains.exodus.database.*
import jetbrains.exodus.database.exceptions.DataIntegrityViolationException
import jetbrains.exodus.entitystore.orientdb.OEntityId
import mu.KLogging
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
Expand Down Expand Up @@ -130,6 +129,7 @@ open class TransientChangesMultiplexer :
}
}

@Suppress("unused")
fun close() {
logger.debug { "Cleaning EventsMultiplexer listeners" }

Expand Down Expand Up @@ -204,20 +204,16 @@ open class TransientChangesMultiplexer :
Where.SYNC_BEFORE_FLUSH_BEFORE_CONSTRAINTS -> when (c.changeType) {
EntityChangeType.ADD -> listeners.visit(true) { it.addedSyncBeforeConstraints(c.transientEntity) }
EntityChangeType.UPDATE -> listeners.visit(true) { it.updatedSyncBeforeConstraints(c.snapshotEntity, c.transientEntity) }
EntityChangeType.REMOVE -> listeners.visit(true) {
it.removedSyncBeforeConstraints(c.snapshotEntity) {
session.getListenerTransientData(it)
}
EntityChangeType.REMOVE -> listeners.visit(true) { listener ->
listener.removedSyncBeforeConstraints(c.snapshotEntity, session.createRemovedEntityData(listener, c.snapshotEntity))
}
}

Where.SYNC_AFTER_FLUSH -> when (c.changeType) {
EntityChangeType.ADD -> listeners.visit { it.addedSync(c.transientEntity) }
EntityChangeType.UPDATE -> listeners.visit { it.updatedSync(c.snapshotEntity, c.transientEntity) }
EntityChangeType.REMOVE -> listeners.visit {
it.removedSync(c.snapshotEntity.id as OEntityId) {
session.getListenerTransientData(it)
}
EntityChangeType.REMOVE -> listeners.visit { listener ->
listener.removedSync(session.getRemovedEntityData(listener, c.snapshotEntity.id))
}
}
}
Expand Down
Loading

0 comments on commit a8db159

Please sign in to comment.