refactor: improve read posts search performance

This commit is contained in:
Harsh Shandilya 2024-01-29 17:22:23 +05:30
parent 89b821ebf1
commit cbd7f2fca4
6 changed files with 31 additions and 44 deletions

View file

@ -2,16 +2,15 @@ CREATE TABLE ReadPosts(
id TEXT NOT NULL PRIMARY KEY
);
selectAllPosts:
SELECT *
FROM ReadPosts;
markRead:
INSERT OR REPLACE
INSERT OR IGNORE
INTO ReadPosts(id)
VALUES (?);
markUnread:
DELETE FROM ReadPosts
WHERE id = ?;
isRead:
SELECT *
FROM ReadPosts
WHERE id = ?;

View file

@ -36,8 +36,3 @@ deletePost:
DELETE
FROM SavedPost
WHERE shortId = ?;
selectPost:
SELECT *
FROM SavedPost
WHERE shortId = ?;

View file

@ -1,5 +1,5 @@
/*
* Copyright © 2023 Harsh Shandilya.
* Copyright © 2023-2024 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
@ -23,8 +23,8 @@ class ReadPostsQueriesTest {
fun `mark post as read`() {
val id = UUID.randomUUID().toString()
postQueries.markRead(id)
assertThat(postQueries.isRead(id).executeAsOne()).isNotNull()
assertThat(postQueries.selectAllPosts().executeAsList()).contains(id)
postQueries.markUnread(id)
assertThat(postQueries.isRead(id).executeAsOneOrNull()).isNull()
assertThat(postQueries.selectAllPosts().executeAsList()).doesNotContain(id)
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright © 2021-2023 Harsh Shandilya.
* Copyright © 2021-2024 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
@ -32,7 +32,7 @@ class SavedPostQueriesTest {
@Test
fun `update post in database`() {
// Get 1 post
val post = createTestData(1)[0]
val post = createTestData(1).first()
// Insert post into DB
postQueries.insertOrReplacePost(post)
@ -46,15 +46,14 @@ class SavedPostQueriesTest {
assertThat(postsCount).isEqualTo(1)
// Check if post is updated
val postFromDb = postQueries.selectPost(post.shortId).executeAsOne()
val postFromDb = postQueries.selectAllPosts().executeAsOne()
assertThat(postFromDb.submitterName).isEqualTo("Fake name")
}
@Test
fun `get post from db`() {
// Get 1 post
val post = createTestData(1)[0]
val post = createTestData(1).first()
// Insert post into DB
postQueries.insertOrReplacePost(post)