Skip to content

Commit

Permalink
optimize: count for episodes (#710)
Browse files Browse the repository at this point in the history
* optimize: EpisodeRepository.countBySubjectId

* optimize: DefaultEpisodeService.countMatchingBySubjectId

* docs: update CHANGELOG.MD
  • Loading branch information
chivehao authored Oct 19, 2024
1 parent 5b1f3c7 commit bfebc97
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

更新日志文档,版本顺序从新到旧,最新版本在最前(上)面。

# 0.17.6

- 优化查询条目剧集数的逻辑
- 优化查询条目匹配了资源的剧集数逻辑

# 0.17.5

## 优化
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.17.5
version=0.17.6
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,18 +26,21 @@ public class DefaultEpisodeService implements EpisodeService {
private final AttachmentReferenceRepository attachmentReferenceRepository;
private final AttachmentRepository attachmentRepository;
private final ApplicationEventPublisher applicationEventPublisher;
private final DatabaseClient databaseClient;

/**
* Construct.
*/
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;
}


Expand Down Expand Up @@ -114,13 +118,12 @@ public Mono<Long> countBySubjectId(Long subjectId) {
@Override
public Mono<Long> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +10,10 @@
public interface EpisodeRepository extends R2dbcRepository<EpisodeEntity, Long> {
Flux<EpisodeEntity> findAllBySubjectId(Long subjectId);

@Query(value = "select id from episode where subject_id = :subjectId")
Flux<Long> findAllIdBySubjectId(Long subjectId);

@Query(value = "select count(id) from episode where subject_id = :subjectId")
Mono<Long> countBySubjectId(Long subjectId);

Flux<EpisodeEntity> findAllBySubjectIdOrderByGroupDescSequenceAscAirTimeAscCreateTimeAsc(
Expand Down

0 comments on commit bfebc97

Please sign in to comment.