Skip to content

Commit

Permalink
Remove sub-query for skipping featured posts when loading posts
Browse files Browse the repository at this point in the history
Instead we will pass a collection of IDs to skip, which will be much more efficient
  • Loading branch information
msasikanth committed Apr 18, 2024
1 parent 4550508 commit 0123ce3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,56 @@ class HomePresenter(
}

private fun init() {
observableActiveSource.activeSource
.onEach { selectedSource ->
combine(observableActiveSource.activeSource, settingsRepository.postsType) {
activeSource,
postsType ->
Pair(activeSource, postsType)
}
.onEach { (activeSource, postsType) ->
_state.update {
it.copy(activeSource = selectedSource, posts = null, featuredPosts = null)
it.copy(
activeSource = activeSource,
postsType = postsType,
featuredPosts = null,
posts = null
)
}
}
.combine(settingsRepository.postsType) { selectedSource, postsType ->
_state.update { it.copy(postsType = postsType) }
selectedSource to postsType
.flatMapLatest {
val postsType = _state.value.postsType
val activeSource = _state.value.activeSource

val unreadOnly =
when (postsType) {
PostsType.ALL,
PostsType.TODAY,
PostsType.LAST_24_HOURS -> null
PostsType.UNREAD -> true
}

val postsAfter =
when (postsType) {
PostsType.ALL,
PostsType.UNREAD -> Instant.DISTANT_PAST
PostsType.TODAY -> {
getTodayStartInstant()
}
PostsType.LAST_24_HOURS -> {
getLast24HourStart()
}
}

rssRepository.featuredPosts(
selectedFeedId = activeSource?.id,
unreadOnly = unreadOnly,
after = postsAfter
)
}
.flatMapLatest { (selectedSource, postsType) ->
.onEach { featuredPosts ->
val featuredPostIds = featuredPosts.map { it.id }
val postsType = _state.value.postsType
val activeSource = _state.value.activeSource

val unreadOnly =
when (postsType) {
PostsType.ALL,
Expand All @@ -258,24 +297,16 @@ class HomePresenter(
val posts =
createPager(config = createPagingConfig(pageSize = 20, enablePlaceholders = true)) {
rssRepository.posts(
selectedFeedId = selectedSource?.id,
selectedFeedId = activeSource?.id,
featuredPostsIds = featuredPostIds,
unreadOnly = unreadOnly,
after = postsAfter
after = postsAfter,
)
}
.flow
.cachedIn(coroutineScope)

rssRepository
.featuredPosts(
selectedFeedId = selectedSource?.id,
unreadOnly = unreadOnly,
after = postsAfter
)
.map { featuredPosts -> Pair(featuredPosts.toImmutableList(), posts) }
}
.onEach { (featuredPosts, posts) ->
_state.update { it.copy(featuredPosts = featuredPosts, posts = posts) }
_state.update { it.copy(featuredPosts = featuredPosts.toImmutableList(), posts = posts) }
}
.launchIn(coroutineScope)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ class RssRepository(

fun posts(
selectedFeedId: String?,
featuredPostsIds: List<String>,
unreadOnly: Boolean? = null,
after: Instant = Instant.DISTANT_PAST
after: Instant = Instant.DISTANT_PAST,
): PagingSource<Int, PostWithMetadata> {
return QueryPagingSource(
countQuery =
postQueries.count(
sourceId = selectedFeedId,
featuredPostsLimit = NUMBER_OF_FEATURED_POSTS,
featuredPosts = featuredPostsIds,
unreadOnly = unreadOnly,
postsAfter = after,
),
Expand All @@ -213,7 +214,7 @@ class RssRepository(
queryProvider = { limit, offset ->
postQueries.posts(
sourceId = selectedFeedId,
featuredPostsLimit = NUMBER_OF_FEATURED_POSTS,
featuredPosts = featuredPostsIds,
unreadOnly = unreadOnly,
postsAfter = after,
limit = limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,7 @@ WHERE
post.sourceId = :sourceId OR
feedGroup.id = :sourceId
) AND
-- Skip featured posts --
post.id NOT IN (
SELECT post.id FROM post
WHERE
(:unreadOnly IS NULL OR post.read != :unreadOnly) AND
(
:sourceId IS NULL OR
post.sourceId = :sourceId OR
feedGroup.id = :sourceId
) AND
post.imageUrl IS NOT NULL AND
post.date > :postsAfter
ORDER BY post.date DESC LIMIT :featuredPostsLimit
) AND
-- Skip featured posts --
post.id NOT IN :featuredPosts AND
post.date > :postsAfter
ORDER BY post.date DESC;

Expand Down Expand Up @@ -104,21 +90,7 @@ WHERE
post.sourceId = :sourceId OR
feedGroup.id = :sourceId
) AND
-- Skip featured posts --
post.id NOT IN (
SELECT post.id FROM post
WHERE
(:unreadOnly IS NULL OR post.read != :unreadOnly) AND
(
:sourceId IS NULL OR
post.sourceId = :sourceId OR
feedGroup.id = :sourceId
) AND
post.imageUrl IS NOT NULL AND
post.date > :postsAfter
ORDER BY post.date DESC LIMIT :featuredPostsLimit
) AND
-- Skip featured posts --
post.id NOT IN :featuredPosts AND
post.date > :postsAfter
ORDER BY post.date DESC
LIMIT :limit OFFSET :offset;
Expand Down

0 comments on commit 0123ce3

Please sign in to comment.