Skip to content

Commit

Permalink
EXPOSED-689 warmUpLinkedReferences() should not return all the values…
Browse files Browse the repository at this point in the history
… from cache (#2355)

* EXPOSED-689 warmUpLinkedReferences() should not return all the values from cache
  • Loading branch information
obabichevjb authored Jan 20, 2025
1 parent 07575c1 commit 36501a5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,10 @@ abstract class EntityClass<ID : Any, out T : Entity<ID>>(
val distinctRefIds = references.distinct()
val transaction = TransactionManager.current()

val inCache = transaction.entityCache.referrers[sourceRefColumn] ?: emptyMap()
val inCache = transaction.entityCache.referrers[sourceRefColumn]
?.filterKeys { distinctRefIds.contains(it) }
?: emptyMap()

val loaded = ((distinctRefIds - inCache.keys).takeIf { it.isNotEmpty() } as List<EntityID<SID>>?)?.let { idsToLoad ->
val alreadyInJoin = (dependsOnTables as? Join)?.alreadyInJoin(linkTable) ?: false
val entityTables = if (alreadyInJoin) dependsOnTables else dependsOnTables.join(linkTable, JoinType.INNER, targetRefColumn, table.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.jetbrains.exposed.sql.tests.shared.entities

import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.junit.Test

class WarmUpLinkedReferencesTests : DatabaseTestsBase() {

object Box : IntIdTable() {
val value = integer("value")
}

class EBox(id: EntityID<Int>) : IntEntity(id) {
var value by Box.value

companion object : IntEntityClass<EBox>(Box)
}

object BoxItem : IntIdTable() {
val box = reference("box", Box)

val value = integer("value")
}

class EBoxItem(id: EntityID<Int>) : IntEntity(id) {
var value by BoxItem.value
var box by EBox referencedOn BoxItem.box

companion object : IntEntityClass<EBoxItem>(BoxItem)
}

@Test
fun warmUpLinkedReferencesShouldNotReturnAllTheValueFromCache() {
withTables(Box, BoxItem) {
val boxEntities = (0..4).map {
EBox.new {
value = it
}
}

boxEntities.forEach {
EBoxItem.new {
box = it
value = it.id.value
}
}

val ids = boxEntities.map { it.id }

// Warm up all the entities to fill the cache
EBox.warmUpLinkedReferences(ids, BoxItem)

val warmedUp = EBox.warmUpLinkedReferences(ids.slice(0..2), BoxItem)
assertEquals(3, warmedUp.size)
}
}
}

0 comments on commit 36501a5

Please sign in to comment.