Skip to content

Commit

Permalink
Improve perf sync
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Sep 2, 2024
1 parent bd9a334 commit becb7d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ class DatabaseHelper(
}

suspend fun updateFeedItemReadAndBookmarked(
isRead: Boolean,
isBookmarked: Boolean,
urlHash: String,
syncedFeedItems: List<SyncedFeedItem>,
) = dbRef.transactionWithContext(backgroundDispatcher) {
dbRef.feedItemQueries.updateFeedItemReadAndBookmarked(
is_read = isRead,
is_bookmarked = isBookmarked,
url_hash = urlHash,
)
syncedFeedItems.forEach { syncedFeedItem ->
dbRef.feedItemQueries.updateFeedItemReadAndBookmarked(
is_read = syncedFeedItem.isRead,
is_bookmarked = syncedFeedItem.isBookmarked,
url_hash = syncedFeedItem.id,
)
}
}

suspend fun getFeedItemsForSync(): List<SyncedFeedItem> = withContext(backgroundDispatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,26 @@ internal class FeedSyncer(
}

val appUrlHashes = appDatabaseHelper.getAllFeedSourceIds().toSet()

val syncFeedSources = syncedDatabaseHelper.getAllFeedSources()

if (syncFeedSources.isNotEmpty()) {
val syncedUrlHashes = mutableSetOf<String>()

syncFeedSources.forEach { syncedFeedSource ->
syncedUrlHashes.add(syncedFeedSource.id)
appDatabaseHelper.insertFeedSource(
listOf(
ParsedFeedSource(
id = syncedFeedSource.id,
url = syncedFeedSource.url,
title = syncedFeedSource.title,
category = syncedFeedSource.categoryId?.let {
FeedSourceCategory(
id = it.value,
title = "", // not necessary here, the caller doesn't use the name
)
},
logoUrl = syncedFeedSource.logoUrl,
),
),
)
}
val syncedUrlHashes = syncFeedSources.map { it.id }.toSet()
appDatabaseHelper.insertFeedSource(
syncFeedSources.map { syncedFeedSource ->
ParsedFeedSource(
id = syncedFeedSource.id,
url = syncedFeedSource.url,
title = syncedFeedSource.title,
category = syncedFeedSource.categoryId?.let {
FeedSourceCategory(
id = it.value,
title = "", // not necessary here, the caller doesn't use the name
)
},
logoUrl = syncedFeedSource.logoUrl,
)
},
)

val urlHashesToDelete = appUrlHashes - syncedUrlHashes
logger.d { "appUrlHash: $appUrlHashes" }
Expand Down Expand Up @@ -105,19 +101,15 @@ internal class FeedSyncer(
val syncFeedSourceCategories = syncedDatabaseHelper.getAllFeedSourceCategories()

if (syncFeedSourceCategories.isNotEmpty()) {
val syncedIds = mutableSetOf<String>()

syncFeedSourceCategories.forEach { syncedFeedSourceCategory ->
syncedIds.add(syncedFeedSourceCategory.id)
appDatabaseHelper.insertCategories(
listOf(
FeedSourceCategory(
id = syncedFeedSourceCategory.id,
title = syncedFeedSourceCategory.title,
),
),
)
}
val syncedIds = syncFeedSourceCategories.map { it.id }.toSet()
appDatabaseHelper.insertCategories(
syncFeedSourceCategories.map { syncedFeedSourceCategory ->
FeedSourceCategory(
id = syncedFeedSourceCategory.id,
title = syncedFeedSourceCategory.title,
)
},
)

// Sync deletions
val idsToDelete = appIds - syncedIds
Expand Down Expand Up @@ -149,13 +141,9 @@ internal class FeedSyncer(
val syncFeedItems = syncedDatabaseHelper.getAllFeedItems()

if (syncFeedItems.isNotEmpty()) {
syncFeedItems.forEach { syncedFeedItem ->
appDatabaseHelper.updateFeedItemReadAndBookmarked(
isRead = syncedFeedItem.isRead,
isBookmarked = syncedFeedItem.isBookmarked,
urlHash = syncedFeedItem.id,
)
}
appDatabaseHelper.updateFeedItemReadAndBookmarked(
syncedFeedItems = syncFeedItems,
)
}

val currentTimestamp = Clock.System.now().toEpochMilliseconds()
Expand Down

0 comments on commit becb7d9

Please sign in to comment.