Skip to content

Commit

Permalink
Merge pull request #575 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix CrunchyrollPlatform.kt predicate cache
  • Loading branch information
Ziedelth authored Jul 18, 2024
2 parents 2508fc3 + ba5538d commit 3916f63
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
13 changes: 6 additions & 7 deletions src/main/kotlin/fr/shikkanime/platforms/CrunchyrollPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,19 @@ class CrunchyrollPlatform :
val lastWeek = zonedDateTime.minusWeeks(1)
val lastWeekStartOfTheDay = lastWeek.withHour(0).withMinute(0).withSecond(0).withNano(0)

episodeVariantService.findAllByDateRange(
null,
episodeVariantService.findAllMappingUuidAndIdentifierByDateRange(
countryCode,
lastWeekStartOfTheDay,
lastWeek.plusSeconds(1),
getPlatform()
).forEach { episodeVariant ->
episodeMappingCacheService.findNextEpisode(episodeVariant.mapping!!)?.let {
logger.warning("Next episode already exists for ${episodeVariant.identifier}")
).forEach { pair ->
episodeMappingCacheService.findNextEpisode(pair.first)?.let {
logger.warning("Next episode already exists for ${pair.second}")
return@forEach
}

val crunchyrollId = getCrunchyrollId(episodeVariant.identifier!!) ?: run {
logger.warning("Crunchyroll ID not found in ${episodeVariant.identifier}")
val crunchyrollId = getCrunchyrollId(pair.second) ?: run {
logger.warning("Crunchyroll ID not found in ${pair.second}")
return@forEach
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.services.MemberFollowAnimeService
import jakarta.persistence.Tuple
import java.time.ZonedDateTime
import java.util.*

class EpisodeVariantRepository : AbstractRepository<EpisodeVariant>() {
@Inject
Expand All @@ -20,7 +21,6 @@ class EpisodeVariantRepository : AbstractRepository<EpisodeVariant>() {
countryCode: CountryCode,
start: ZonedDateTime,
end: ZonedDateTime,
platform: Platform? = null
): List<EpisodeVariant> {
return database.entityManager.use { entityManager ->
val cb = entityManager.criteriaBuilder
Expand All @@ -39,18 +39,43 @@ class EpisodeVariantRepository : AbstractRepository<EpisodeVariant>() {
predicates.add(animePredicate)
}

platform?.let {
val platformPredicate = cb.equal(root[EpisodeVariant_.platform], platform)
predicates.add(platformPredicate)
}

query.where(cb.and(*predicates.toTypedArray()))

createReadOnlyQuery(entityManager, query)
.resultList
}
}

fun findAllMappingUuidAndIdentifierByDateRange(
countryCode: CountryCode,
start: ZonedDateTime,
end: ZonedDateTime,
platform: Platform
): List<Pair<UUID, String>> {
return database.entityManager.use { entityManager ->
val cb = entityManager.criteriaBuilder
val query = cb.createTupleQuery()
val root = query.from(getEntityClass())

query.multiselect(
root[EpisodeVariant_.mapping][EpisodeMapping_.uuid],
root[EpisodeVariant_.identifier]
)

query.where(
cb.and(
cb.equal(root[EpisodeVariant_.mapping][EpisodeMapping_.anime][Anime_.countryCode], countryCode),
cb.between(root[EpisodeVariant_.releaseDateTime], start, end),
cb.equal(root[EpisodeVariant_.platform], platform)
)
)

createReadOnlyQuery(entityManager, query)
.resultList
.map { it[0] as UUID to it[1] as String }
}
}

fun findAllTypeIdentifier(): List<Tuple> {
return database.entityManager.use {
val cb = it.criteriaBuilder
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/fr/shikkanime/services/EpisodeVariantService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ class EpisodeVariantService : AbstractService<EpisodeVariant, EpisodeVariantRepo
countryCode: CountryCode,
start: ZonedDateTime,
end: ZonedDateTime,
platform: Platform? = null
) = episodeVariantRepository.findAllByDateRange(member, countryCode, start, end, platform)
) = episodeVariantRepository.findAllByDateRange(member, countryCode, start, end)

fun findAllMappingUuidAndIdentifierByDateRange(
countryCode: CountryCode,
start: ZonedDateTime,
end: ZonedDateTime,
platform: Platform
) = episodeVariantRepository.findAllMappingUuidAndIdentifierByDateRange(countryCode, start, end, platform)

fun findAllTypeIdentifier() = episodeVariantRepository.findAllTypeIdentifier()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class EpisodeMappingCacheService : AbstractCacheService {
)
}

private val findNextEpisodeCache = MapCache<EpisodeMapping, EpisodeMapping?>(classes = listOf(EpisodeMapping::class.java)) {
episodeMappingService.findNextEpisode(it)
private val findNextEpisodeCache = MapCache<UUID, EpisodeMapping?>(classes = listOf(EpisodeMapping::class.java)) {
episodeMappingService.find(it)?.let { em -> episodeMappingService.findNextEpisode(em) }
}

fun findAllBy(
Expand All @@ -95,5 +95,5 @@ class EpisodeMappingCacheService : AbstractCacheService {
number
)]

fun findNextEpisode(episode: EpisodeMapping) = findNextEpisodeCache[episode]
fun findNextEpisode(uuid: UUID) = findNextEpisodeCache[uuid]
}

0 comments on commit 3916f63

Please sign in to comment.