Skip to content

Commit

Permalink
Remove transaction on select
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Jun 25, 2024
1 parent ff12124 commit 36c6c6f
Show file tree
Hide file tree
Showing 18 changed files with 598 additions and 625 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/fr/shikkanime/entities/EpisodeMapping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import java.util.*
columnList = "anime_uuid, episode_type, season, number",
unique = true
),
Index(
name = "idx_episode_mapping_anime_uuid",
columnList = "anime_uuid"
),
]
)
@Cacheable
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/fr/shikkanime/entities/EpisodeVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ import java.time.ZonedDateTime
import java.util.*

@Entity
@Table(name = "episode_variant")
@Table(
name = "episode_variant",
indexes = [
Index(
name = "idx_episode_variant_mapping_uuid",
columnList = "mapping_uuid"
),
Index(
name = "idx_episode_variant_release_date_mapping_uuid",
columnList = "release_date_time, mapping_uuid"
),
]
)
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class EpisodeVariant(
Expand Down
34 changes: 15 additions & 19 deletions src/main/kotlin/fr/shikkanime/repositories/AbstractRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ abstract class AbstractRepository<E : ShikkEntity> {

protected abstract fun getEntityClass(): Class<E>

protected fun <T> inTransaction(block: (EntityManager) -> T): T {
val entityManager = database.entityManager
val transaction = entityManager.transaction
protected fun <T> inTransaction(block: () -> T): T {
val transaction = database.entityManager.transaction
transaction.begin()
val result: T

try {
result = block(entityManager)
result = block()
database.entityManager.flush()
transaction.commit()
} catch (e: Exception) {
transaction.rollback()
throw e
} finally {
entityManager.close()
database.entityManager.clear()
}

return result
Expand Down Expand Up @@ -64,45 +64,41 @@ abstract class AbstractRepository<E : ShikkEntity> {
}

open fun findAll(): List<E> {
return inTransaction {
val cb = it.criteriaBuilder
val query = cb.createQuery(getEntityClass())
query.from(getEntityClass())
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
query.from(getEntityClass())

createReadOnlyQuery(it, query)
.resultList
}
return createReadOnlyQuery(database.entityManager, query)
.resultList
}

open fun find(uuid: UUID): E? {
return inTransaction {
it.find(getEntityClass(), uuid)
}
return database.entityManager.find(getEntityClass(), uuid)
}

fun save(entity: E): E {
return inTransaction {
it.persist(entity)
database.entityManager.persist(entity)
entity
}
}

fun update(entity: E): E {
return inTransaction {
it.merge(entity)
database.entityManager.merge(entity)
entity
}
}

fun delete(entity: E) {
inTransaction {
it.remove(entity)
database.entityManager.remove(entity)
}
}

fun deleteAll() {
inTransaction {
it.createQuery("DELETE FROM ${getEntityClass().simpleName}").executeUpdate()
database.entityManager.createQuery("DELETE FROM ${getEntityClass().simpleName}").executeUpdate()
}
}
}
139 changes: 63 additions & 76 deletions src/main/kotlin/fr/shikkanime/repositories/AnimeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ class AnimeRepository : AbstractRepository<Anime>() {
override fun getEntityClass() = Anime::class.java

fun preIndex() {
inTransaction {
val searchSession = Search.session(it)
val indexer = searchSession.massIndexer(getEntityClass())
indexer.startAndWait()
}
val searchSession = Search.session(database.entityManager)
val indexer = searchSession.massIndexer(getEntityClass())
indexer.startAndWait()
}

override fun findAll(): List<Anime> {
return inTransaction {
val cb = it.criteriaBuilder
val query = cb.createQuery(getEntityClass())
query.from(getEntityClass())

val list = it.createQuery(query).resultList
list.forEach { anime -> Hibernate.initialize(anime.mappings) }
list
}
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
query.from(getEntityClass())

val list = createReadOnlyQuery(database.entityManager, query).resultList
list.forEach { anime -> Hibernate.initialize(anime.mappings) }
return list
}

fun findAllBy(
Expand All @@ -42,33 +38,31 @@ class AnimeRepository : AbstractRepository<Anime>() {
limit: Int,
status: Status? = null
): Pageable<Anime> {
return inTransaction { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

val predicates = mutableListOf<Predicate>()
simulcast?.let { predicates.add(cb.equal(root.join(Anime_.simulcasts), it)) }
countryCode?.let { predicates.add(cb.equal(root[Anime_.countryCode], it)) }
status?.let { predicates.add(cb.equal(root[Anime_.status], it)) }
query.where(*predicates.toTypedArray())

val orders = sort.mapNotNull { sortParameter ->
val order = if (sortParameter.order == SortParameter.Order.ASC) cb::asc else cb::desc

val field = when (sortParameter.field) {
"name" -> root[Anime_.name]
"releaseDateTime" -> root[Anime_.releaseDateTime]
"lastReleaseDateTime" -> root[Anime_.lastReleaseDateTime]
else -> null
}

field?.let { order(it) }
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

val predicates = mutableListOf<Predicate>()
simulcast?.let { predicates.add(cb.equal(root.join(Anime_.simulcasts), it)) }
countryCode?.let { predicates.add(cb.equal(root[Anime_.countryCode], it)) }
status?.let { predicates.add(cb.equal(root[Anime_.status], it)) }
query.where(*predicates.toTypedArray())

val orders = sort.mapNotNull { sortParameter ->
val order = if (sortParameter.order == SortParameter.Order.ASC) cb::asc else cb::desc

val field = when (sortParameter.field) {
"name" -> root[Anime_.name]
"releaseDateTime" -> root[Anime_.releaseDateTime]
"lastReleaseDateTime" -> root[Anime_.lastReleaseDateTime]
else -> null
}

query.orderBy(orders)
buildPageableQuery(createReadOnlyQuery(entityManager, query), page, limit)
field?.let { order(it) }
}

query.orderBy(orders)
return buildPageableQuery(createReadOnlyQuery(database.entityManager, query), page, limit)
}

fun findAllByName(name: String, countryCode: CountryCode?, page: Int, limit: Int): Pageable<Anime> {
Expand All @@ -79,9 +73,7 @@ class AnimeRepository : AbstractRepository<Anime>() {
.where { w -> findWhere(w, name, countryCode) }
.fetch((limit * page) - limit, limit) as SearchResult<Anime>

return inTransaction {
Pageable(searchResult.hits(), page, limit, searchResult.total().hitCount())
}
return Pageable(searchResult.hits(), page, limit, searchResult.total().hitCount())
}

private fun findWhere(
Expand All @@ -96,50 +88,45 @@ class AnimeRepository : AbstractRepository<Anime>() {
}

fun findAllUuidImageAndBanner(): List<Tuple> {
return inTransaction { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createTupleQuery()
val root = query.from(getEntityClass())
query.multiselect(root[Anime_.uuid], root[Anime_.image], root[Anime_.banner])
entityManager.createQuery(query).resultList
}
val cb = database.entityManager.criteriaBuilder
val query = cb.createTupleQuery()
val root = query.from(getEntityClass())
query.multiselect(root[Anime_.uuid], root[Anime_.image], root[Anime_.banner])
return createReadOnlyQuery(database.entityManager, query)
.resultList
}

fun findBySlug(countryCode: CountryCode, slug: String): Anime? {
return inTransaction { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

query.where(
cb.and(
cb.equal(root[Anime_.countryCode], countryCode),
cb.equal(root[Anime_.slug], slug)
)
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

query.where(
cb.and(
cb.equal(root[Anime_.countryCode], countryCode),
cb.equal(root[Anime_.slug], slug)
)
)

createReadOnlyQuery(entityManager, query)
.resultList
.firstOrNull()
}
return createReadOnlyQuery(database.entityManager, query)
.resultList
.firstOrNull()
}

fun findByName(countryCode: CountryCode, name: String?): Anime? {
return inTransaction {
val cb = it.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

query.where(
cb.and(
cb.equal(root[Anime_.countryCode], countryCode),
cb.equal(cb.lower(root[Anime_.name]), name?.lowercase())
)
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

query.where(
cb.and(
cb.equal(root[Anime_.countryCode], countryCode),
cb.equal(cb.lower(root[Anime_.name]), name?.lowercase())
)
)

it.createQuery(query)
.resultList
.firstOrNull()
}
return database.entityManager.createQuery(query)
.resultList
.firstOrNull()
}
}
30 changes: 13 additions & 17 deletions src/main/kotlin/fr/shikkanime/repositories/ConfigRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@ class ConfigRepository : AbstractRepository<Config>() {
override fun getEntityClass() = Config::class.java

fun findAllByName(name: String): List<Config> {
return inTransaction {
val cb = it.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())
query.where(cb.like(cb.lower(root[Config_.propertyKey]), "%${name.lowercase()}%"))
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())
query.where(cb.like(cb.lower(root[Config_.propertyKey]), "%${name.lowercase()}%"))

createReadOnlyQuery(it, query)
.resultList
}
return createReadOnlyQuery(database.entityManager, query)
.resultList
}

fun findByName(name: String): Config? {
return inTransaction {
val cb = it.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())
query.where(cb.equal(cb.lower(root[Config_.propertyKey]), name.lowercase()))
val cb = database.entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())
query.where(cb.equal(cb.lower(root[Config_.propertyKey]), name.lowercase()))

createReadOnlyQuery(it, query)
.resultList
.firstOrNull()
}
return createReadOnlyQuery(database.entityManager, query)
.resultList
.firstOrNull()
}
}
Loading

0 comments on commit 36c6c6f

Please sign in to comment.