Skip to content

Commit

Permalink
Invalidate references for related tables (via use-case)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Oct 11, 2021
1 parent b34c59f commit d246c91
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transactionScope
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.HashSet

val Transaction.entityCache: EntityCache by transactionScope { EntityCache(this) }

Expand Down Expand Up @@ -135,8 +136,11 @@ class EntityCache(private val transaction: Transaction) {
internal fun removeTablesReferrers(insertedTables: Collection<Table>) {

val insertedTablesSet = insertedTables.toSet()
val insertedTablesWithReferenceSet = insertedTablesSet.flatMapTo(HashSet()) { t ->
t.columns.mapNotNull { it.referee?.table }
}
val tablesToRemove: List<Table> = referrers.values.flatMapTo(HashSet()) { it.keys.map { it.table } }
.filter { table -> table.columns.any { c -> c.referee?.table in insertedTablesSet } } + insertedTablesSet
.filter { table -> table.columns.any { c -> c.referee?.table in insertedTablesWithReferenceSet } } + insertedTablesWithReferenceSet

referrers.mapNotNull { (entityId, entityReferrers) ->
entityReferrers.filterKeys { it.table in tablesToRemove }.keys.forEach { entityReferrers.remove(it) }
Expand Down

0 comments on commit d246c91

Please sign in to comment.