Improve query findFollowsByTarget & add tests

This commit is contained in:
2021-04-27 18:52:36 +02:00
parent 76e4033a22
commit 371483ccde
8 changed files with 228 additions and 303 deletions

View File

@@ -74,21 +74,24 @@ sealed class FollowRepository<IN : TargetRef, OUT : TargetRef>(override var requ
target: Entity,
bulkSize: Int = 300
): Flow<FollowForView<IN>> = flow {
var nextPage = 1
do {
val paginate = findFollowsByTarget(target, nextPage, bulkSize)
paginate.result.forEach {
var lastId: UUID? = null
while (true) {
val result = findFollowsByTarget(target, lastId, bulkSize)
if (result.count() == 0) {
break
}
result.forEach {
emit(it)
}
nextPage = paginate.currentPage + 1
} while (!paginate.isLastPage())
lastId = result.last().id
}
}
abstract fun findFollowsByTarget(
target: Entity,
page: Int = 1,
lastId: UUID?,
limit: Int = 300
): Paginated<FollowForView<IN>>
): List<FollowForView<IN>>
}
class FollowArticleRepository(requester: Requester) : FollowRepository<ArticleRef, ArticleForView>(requester) {
@@ -109,14 +112,14 @@ class FollowArticleRepository(requester: Requester) : FollowRepository<ArticleRe
override fun findFollowsByTarget(
target: Entity,
page: Int,
lastId: UUID?,
limit: Int
): Paginated<FollowForView<ArticleRef>> {
): List<FollowForView<ArticleRef>> {
return requester
.getFunction("find_follows_article_by_target")
.select(
page,
limit,
"start_id" to lastId,
"limit" to limit,
"target_id" to target.id
)
}
@@ -140,9 +143,9 @@ class FollowConstitutionRepository(requester: Requester) : FollowRepository<Cons
override fun findFollowsByTarget(
target: Entity,
page: Int,
lastId: UUID?,
limit: Int
): Paginated<FollowForView<ConstitutionRef>> {
): List<FollowForView<ConstitutionRef>> {
TODO("Not yet implemented")
}
}
@@ -165,9 +168,9 @@ class FollowCitizenRepository(requester: Requester) : FollowRepository<CitizenRe
override fun findFollowsByTarget(
target: Entity,
page: Int,
lastId: UUID?,
limit: Int
): Paginated<FollowForView<CitizenRef>> {
): List<FollowForView<CitizenRef>> {
TODO("Not yet implemented")
}
}

View File

@@ -1,20 +1,21 @@
create or replace function find_follows_article_by_target(
_target_id uuid,
"limit" int default 50,
"offset" int default 0,
out resource json,
out total int
_limit int default 50,
_start_id uuid default null,
out resource json
) language plpgsql as
$$
declare
_version_id uuid = (select version_id from article where id = _target_id);
_start_at timestamp default '2000-01-01 00:00:00'::timestamp;
_article_creator_id uuid = (select created_by_id from article where id = _target_id);
begin
select json_agg(t), (
select count(f.id)
from follow f
join article a on f.target_id = a.id
where a.version_id = _version_id)
into resource, total
if _start_id is not null then
select created_at into _start_at from follow where id = _start_id;
end if;
select json_agg(t)
into resource
from (
select
f.id,
@@ -22,11 +23,17 @@ begin
f.target_reference,
json_build_object('id', f.target_id) as target,
find_citizen_by_id_with_user(f.created_by_id) as created_by
from follow_article as f
join article a on f.target_id = a.id
where a.version_id = _version_id
from follow as f
left join article a on f.target_reference = 'article'::regclass and f.target_id = a.id
where (
(f.target_reference = 'article'::regclass and a.version_id = _version_id)
or
(f.target_reference = 'citizen'::regclass and f.target_id = _article_creator_id)
)
and f.created_at >= _start_at
and (_start_id is null or f.id != _start_id)
order by f.created_at
limit "limit" offset "offset"
limit _limit
) as t;
end
$$;