refactor(backup/restore): Migrate bulk insert chapter to SQLDelight

This commit is contained in:
Ahmad Ansori Palembani 2024-08-17 06:39:52 +07:00
parent 16a51f00c4
commit fac21dbab7
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
7 changed files with 32 additions and 10 deletions

View file

@ -15,21 +15,23 @@ import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.util.chapter.ChapterUtil
import eu.kanade.tachiyomi.util.manga.MangaUtil
import eu.kanade.tachiyomi.util.system.launchNow
import kotlin.math.max
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.domain.category.interactor.GetCategories
import yokai.domain.chapter.interactor.GetChapter
import yokai.domain.chapter.interactor.InsertChapter
import yokai.domain.library.custom.model.CustomMangaInfo
import yokai.domain.manga.interactor.GetManga
import yokai.domain.manga.interactor.InsertManga
import yokai.domain.manga.interactor.UpdateManga
import kotlin.math.max
class MangaBackupRestorer(
private val db: DatabaseHelper = Injekt.get(),
private val customMangaManager: CustomMangaManager = Injekt.get(),
private val getCategories: GetCategories = Injekt.get(),
private val getChapter: GetChapter = Injekt.get(),
private val insertChapter: InsertChapter = Injekt.get(),
private val getManga: GetManga = Injekt.get(),
private val insertManga: InsertManga = Injekt.get(),
private val updateManga: UpdateManga = Injekt.get(),
@ -137,7 +139,7 @@ class MangaBackupRestorer(
val newChapters = chapters.groupBy { it.id != null }
newChapters[true]?.let { db.updateKnownChaptersBackup(it).executeAsBlocking() }
newChapters[false]?.let { db.insertChapters(it).executeAsBlocking() }
newChapters[false]?.let { insertChapter.awaitBulk(it) }
}
private suspend fun restoreExtras(

View file

@ -67,6 +67,7 @@ interface ChapterQueries : DbProvider {
)
.prepare()
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
fun insertChapters(chapters: List<Chapter>) = db.put().objects(chapters).prepare()
fun updateKnownChaptersBackup(chapters: List<Chapter>) = db.put()

View file

@ -16,6 +16,7 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.migration.MigrationFlags
import eu.kanade.tachiyomi.util.system.launchNow
import eu.kanade.tachiyomi.util.system.launchUI
import java.util.Date
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.withContext
@ -24,7 +25,6 @@ import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import yokai.domain.library.custom.model.CustomMangaInfo.Companion.getMangaInfo
import yokai.domain.ui.UiPreferences
import java.util.*
class MigrationProcessAdapter(
val controller: MigrationListController,
@ -129,6 +129,7 @@ class MigrationProcessAdapter(
sourceFinished()
}
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
private fun migrateMangaInternal(
prevSource: Source?,
source: Source,
@ -143,6 +144,7 @@ class MigrationProcessAdapter(
companion object {
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
fun migrateMangaInternal(
flags: Int,
db: DatabaseHelper,

View file

@ -116,4 +116,24 @@ class ChapterRepositoryImpl(private val handler: DatabaseHandler) : ChapterRepos
chaptersQueries.selectLastInsertedRowId()
}
}
override suspend fun insertBulk(chapters: List<Chapter>) =
handler.await(true) {
chapters.forEach { chapter ->
chaptersQueries.insert(
mangaId = chapter.manga_id!!,
url = chapter.url,
name = chapter.name,
scanlator = chapter.scanlator,
read = chapter.read,
bookmark = chapter.bookmark,
lastPageRead = chapter.last_page_read.toLong(),
pagesLeft = chapter.pages_left.toLong(),
chapterNumber = chapter.chapter_number.toDouble(),
sourceOrder = chapter.source_order.toLong(),
dateFetch = chapter.date_fetch,
dateUpload = chapter.date_upload,
)
}
}
}

View file

@ -23,4 +23,5 @@ interface ChapterRepository {
suspend fun updateAll(updates: List<ChapterUpdate>): Boolean
suspend fun insert(chapter: Chapter): Long?
suspend fun insertBulk(chapters: List<Chapter>)
}

View file

@ -7,4 +7,6 @@ class InsertChapter(
private val chapterRepository: ChapterRepository,
) {
suspend fun await(chapter: Chapter) = chapterRepository.insert(chapter)
suspend fun awaitBulk(chapters: List<Chapter>) = chapterRepository.insertBulk(chapters)
}

View file

@ -1,7 +1,6 @@
package yokai.domain.chapter.interactor
import eu.kanade.tachiyomi.data.database.models.MangaChapter
import eu.kanade.tachiyomi.util.lang.sqLite
import yokai.domain.chapter.ChapterRepository
import yokai.util.limitAndOffset
@ -11,11 +10,6 @@ class RecentChapter(
suspend fun await(filterScanlators: Boolean, isResuming: Boolean, search: String = "", offset: Long = 0L): List<MangaChapter> {
val (limit, actualOffset) = limitAndOffset(true, isResuming, offset)
return chapterRepository.getRecents(
filterScanlators,
search.sqLite,
limit,
actualOffset,
)
return chapterRepository.getRecents(filterScanlators, search, limit, actualOffset)
}
}