diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 9720ce76..807ce6ae 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -2,6 +2,11 @@ 更新日志文档,版本顺序从新到旧,最新版本在最前(上)面。 +# 0.17.6 + +- 优化查询条目剧集数的逻辑 +- 优化查询条目匹配了资源的剧集数逻辑 + # 0.17.5 ## 优化 diff --git a/gradle.properties b/gradle.properties index 8f55517b..a183058b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.17.5 +version=0.17.6 diff --git a/server/src/main/java/run/ikaros/server/core/episode/DefaultEpisodeService.java b/server/src/main/java/run/ikaros/server/core/episode/DefaultEpisodeService.java index 73cc2a03..0b05ca29 100644 --- a/server/src/main/java/run/ikaros/server/core/episode/DefaultEpisodeService.java +++ b/server/src/main/java/run/ikaros/server/core/episode/DefaultEpisodeService.java @@ -5,6 +5,7 @@ import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.r2dbc.core.DatabaseClient; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import reactor.core.publisher.Flux; @@ -25,6 +26,7 @@ public class DefaultEpisodeService implements EpisodeService { private final AttachmentReferenceRepository attachmentReferenceRepository; private final AttachmentRepository attachmentRepository; private final ApplicationEventPublisher applicationEventPublisher; + private final DatabaseClient databaseClient; /** * Construct. @@ -32,11 +34,13 @@ public class DefaultEpisodeService implements EpisodeService { public DefaultEpisodeService(EpisodeRepository episodeRepository, AttachmentReferenceRepository attachmentReferenceRepository, AttachmentRepository attachmentRepository, - ApplicationEventPublisher applicationEventPublisher) { + ApplicationEventPublisher applicationEventPublisher, + DatabaseClient databaseClient) { this.episodeRepository = episodeRepository; this.attachmentReferenceRepository = attachmentReferenceRepository; this.attachmentRepository = attachmentRepository; this.applicationEventPublisher = applicationEventPublisher; + this.databaseClient = databaseClient; } @@ -114,13 +118,12 @@ public Mono countBySubjectId(Long subjectId) { @Override public Mono countMatchingBySubjectId(Long subjectId) { Assert.isTrue(subjectId >= 0, "'subjectId' must >= 0."); - return episodeRepository.findAllBySubjectId(subjectId) - .flatMap(entity -> attachmentReferenceRepository.existsByTypeAndReferenceId( - AttachmentReferenceType.EPISODE, entity.getId())) - .filter(exists -> exists) - .collectList() - .map(List::size) - .map(Long::valueOf); + return databaseClient.sql("select count(e.ID) from EPISODE e, ATTACHMENT_REFERENCE ar " + + "where ar.TYPE = 'EPISODE' and e.ID = ar.REFERENCE_ID " + + "and e.SUBJECT_ID = :subjectId") + .bind("subjectId", subjectId) + .map(row -> row.get(0, Long.class)) + .one(); } @Override diff --git a/server/src/main/java/run/ikaros/server/store/repository/EpisodeRepository.java b/server/src/main/java/run/ikaros/server/store/repository/EpisodeRepository.java index fb0a41e0..47a89f67 100644 --- a/server/src/main/java/run/ikaros/server/store/repository/EpisodeRepository.java +++ b/server/src/main/java/run/ikaros/server/store/repository/EpisodeRepository.java @@ -1,5 +1,6 @@ package run.ikaros.server.store.repository; +import org.springframework.data.r2dbc.repository.Query; import org.springframework.data.r2dbc.repository.R2dbcRepository; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -9,6 +10,10 @@ public interface EpisodeRepository extends R2dbcRepository { Flux findAllBySubjectId(Long subjectId); + @Query(value = "select id from episode where subject_id = :subjectId") + Flux findAllIdBySubjectId(Long subjectId); + + @Query(value = "select count(id) from episode where subject_id = :subjectId") Mono countBySubjectId(Long subjectId); Flux findAllBySubjectIdOrderByGroupDescSequenceAscAirTimeAscCreateTimeAsc(