Skip to content

Commit

Permalink
Add index on slug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Apr 4, 2024
1 parent edda17d commit bc3825f
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/fr/shikkanime/entities/Anime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.util.*
name = "anime",
indexes = [
Index(name = "idx_anime_country_code", columnList = "country_code"),
Index(name = "idx_anime_slug", columnList = "slug"),
]
)
@Indexed
Expand Down Expand Up @@ -44,7 +45,7 @@ class Anime(
)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
var simulcasts: MutableSet<Simulcast> = mutableSetOf(),
@Column(nullable = true)
@Column(nullable = false, unique = true)
var slug: String? = null,
@Column(nullable = false, name = "last_release_date_time")
var lastReleaseDateTime: ZonedDateTime = releaseDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.shikkanime.entities.ShikkEntity
import fr.shikkanime.utils.Database
import jakarta.persistence.EntityManager
import jakarta.persistence.TypedQuery
import jakarta.persistence.criteria.CriteriaQuery
import org.hibernate.ScrollMode
import org.hibernate.jpa.AvailableHints
import org.hibernate.query.Query
Expand Down Expand Up @@ -41,6 +42,11 @@ abstract class AbstractRepository<E : ShikkEntity> {
.setHint(AvailableHints.HINT_READ_ONLY, true)
}

fun <T> createReadOnlyQuery(entityManager: EntityManager, criteriaQuery: CriteriaQuery<T>): TypedQuery<T> {
return entityManager.createQuery(criteriaQuery)
.setHint(AvailableHints.HINT_READ_ONLY, true)
}

fun buildPageableQuery(query: TypedQuery<E>, page: Int, limit: Int): Pageable<E> {
val scrollableResults = query.unwrap(Query::class.java)
.setReadOnly(true)
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/fr/shikkanime/repositories/AnimeRepository.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.shikkanime.repositories

import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.Anime_
import fr.shikkanime.entities.Pageable
import fr.shikkanime.entities.SortParameter
import fr.shikkanime.entities.enums.CountryCode
Expand Down Expand Up @@ -139,9 +140,15 @@ class AnimeRepository : AbstractRepository<Anime>() {
}

fun findBySlug(slug: String): Anime? {
return inTransaction {
createReadOnlyQuery(it, "FROM Anime WHERE slug = :slug", getEntityClass())
.setParameter("slug", slug)
return inTransaction { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

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

createReadOnlyQuery(entityManager, query)
.resultList
.firstOrNull()
?.initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ class EpisodeRepository : AbstractRepository<Episode>() {
): List<Episode> {
return inTransaction { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createQuery(Episode::class.java)
val root = query.from(Episode::class.java)
val query = cb.createQuery(getEntityClass())
val root = query.from(getEntityClass())

val countryPredicate = cb.equal(root[Episode_.anime][Anime_.countryCode], countryCode)
val datePredicate = cb.between(root[Episode_.releaseDateTime], start, end)

query.select(root).where(cb.and(countryPredicate, datePredicate))

entityManager.createQuery(query)
createReadOnlyQuery(entityManager, query)
.resultList
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/utils/StringUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object StringUtils {
private val NONLATIN: Pattern = Pattern.compile("[^\\w-]")
private val WHITESPACE: Pattern = Pattern.compile("\\s")
private val regex = "([-|!].*[-|!])|(Saison \\d*)|\\(\\d*\\)".toRegex()
private val separators = listOf(":", ",", "!", " so ")
private val separators = listOf(":", ",", "!", "", " so ")

fun getShortName(fullName: String): String {
var shortName = regex.replace(fullName, "")
Expand Down
39 changes: 39 additions & 0 deletions src/main/resources/db/changelog/2024/04/01-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.27.xsd" objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<property global="false" name="id" value="1712236123260"/>
<property global="false" name="author" value="Ziedelth"/>

<changeSet id="${id}-1" author="${author}">
<preConditions onFail="MARK_RAN">
<not>
<uniqueConstraintExists tableName="anime" columnNames="slug" constraintName="uc_anime_slug"/>
</not>
</preConditions>

<addUniqueConstraint columnNames="slug" constraintName="uc_anime_slug" tableName="anime"/>
</changeSet>

<changeSet id="${id}-2" author="${author}">
<preConditions onFail="MARK_RAN">
<not>
<indexExists indexName="idx_anime_slug" tableName="anime"/>
</not>
</preConditions>

<createIndex indexName="idx_anime_slug" tableName="anime">
<column name="slug"/>
</createIndex>
</changeSet>

<changeSet id="${id}-3" author="${author}">
<preConditions onFail="MARK_RAN">
<columnExists tableName="anime" columnName="slug"/>
</preConditions>

<addNotNullConstraint columnDataType="VARCHAR(255)" columnName="slug" tableName="anime" validate="true"/>
</changeSet>
</databaseChangeLog>
2 changes: 2 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@
<include file="/db/changelog/2024/03/06-changelog.xml"/>
<include file="/db/changelog/2024/03/07-changelog.xml"/>
<include file="/db/changelog/2024/03/08-changelog.xml"/>
<!-- April 2024 -->
<include file="/db/changelog/2024/04/01-changelog.xml"/>
</databaseChangeLog>
16 changes: 16 additions & 0 deletions src/test/kotlin/fr/shikkanime/services/EpisodeServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = releaseDateTime,
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down Expand Up @@ -90,6 +91,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = ZonedDateTime.parse("2023-12-20T00:00:00Z"),
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -111,6 +113,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = ZonedDateTime.parse("2023-12-20T00:00:00Z"),
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down Expand Up @@ -140,6 +143,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = releaseDateTime,
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down Expand Up @@ -171,6 +175,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = releaseDateTime,
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -195,6 +200,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = releaseDateTime,
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -219,6 +225,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = releaseDateTime,
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -243,6 +250,7 @@ class EpisodeServiceTest {
image = "https://www.shikkanime.com/image.png",
banner = "https://www.shikkanime.com/image.png",
releaseDateTime = releaseDateTime,
slug = "test",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down Expand Up @@ -274,6 +282,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -296,6 +305,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -317,6 +327,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -342,6 +353,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -367,6 +379,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down Expand Up @@ -395,6 +408,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -417,6 +431,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand All @@ -438,6 +453,7 @@ class EpisodeServiceTest {
image = Constant.DEFAULT_IMAGE_PREVIEW,
banner = Constant.DEFAULT_IMAGE_PREVIEW,
releaseDateTime = ZonedDateTime.parse("2023-07-10T15:30:00Z"),
slug = "synduality-noir",
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/fr/shikkanime/utils/StringUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class StringUtilsTest {
"Studio Apartment" to "Studio Apartment, Good Lighting, Angel Included",
"I Was Reincarnated as the 7th Prince" to "I Was Reincarnated as the 7th Prince so I Can Take My Time Perfecting My Magical Ability",
"Mushoku Tensei: Jobless Reincarnation" to "Mushoku Tensei: Jobless Reincarnation",
"Yuru Camp" to "Yuru Camp – Au grand air",
)

list.forEach { (expected, input) ->
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/animes/dragon-ball.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"season": "WINTER",
"year": 2024
}
]
],
"slug": "dragon-ball-z"
}
3 changes: 2 additions & 1 deletion src/test/resources/animes/naruto.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"season": "WINTER",
"year": 2024
}
]
],
"slug": "naruto"
}
3 changes: 2 additions & 1 deletion src/test/resources/animes/one-piece.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"season": "WINTER",
"year": 2024
}
]
],
"slug": "one-piece"
}
3 changes: 2 additions & 1 deletion src/test/resources/animes/two-piece.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"season": "WINTER",
"year": 2024
}
]
],
"slug": "two-piece"
}

0 comments on commit bc3825f

Please sign in to comment.