Remove some logic around old legacy backup versions + minor optimizations

This commit is contained in:
arkon 2021-02-06 12:15:34 -05:00
parent e8c35ae4e1
commit bbee093c63
2 changed files with 27 additions and 66 deletions

View file

@ -53,30 +53,14 @@ import kotlin.math.max
class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : AbstractBackupManager(context) {
var parserVersion: Int = version
private set
var parser: Gson = initParser()
/**
* Set version of parser
*
* @param version version of parser
*/
internal fun setVersion(version: Int) {
this.parserVersion = version
parser = initParser()
}
private fun initParser(): Gson = when (parserVersion) {
2 ->
GsonBuilder()
.registerTypeAdapter<MangaImpl>(MangaTypeAdapter.build())
.registerTypeHierarchyAdapter<ChapterImpl>(ChapterTypeAdapter.build())
.registerTypeAdapter<CategoryImpl>(CategoryTypeAdapter.build())
.registerTypeAdapter<DHistory>(HistoryTypeAdapter.build())
.registerTypeHierarchyAdapter<TrackImpl>(TrackTypeAdapter.build())
.create()
val parser: Gson = when (version) {
2 -> GsonBuilder()
.registerTypeAdapter<MangaImpl>(MangaTypeAdapter.build())
.registerTypeHierarchyAdapter<ChapterImpl>(ChapterTypeAdapter.build())
.registerTypeAdapter<CategoryImpl>(CategoryTypeAdapter.build())
.registerTypeAdapter<DHistory>(HistoryTypeAdapter.build())
.registerTypeHierarchyAdapter<TrackImpl>(TrackTypeAdapter.build())
.create()
else -> throw Exception("Unknown backup version")
}
@ -308,7 +292,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
*/
internal fun restoreCategoriesForManga(manga: Manga, categories: List<String>) {
val dbCategories = databaseHelper.getCategories().executeAsBlocking()
val mangaCategoriesToUpdate = mutableListOf<MangaCategory>()
val mangaCategoriesToUpdate = ArrayList<MangaCategory>(categories.size)
for (backupCategoryStr in categories) {
for (dbCategory in dbCategories) {
if (backupCategoryStr == dbCategory.name) {
@ -332,7 +316,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
*/
internal fun restoreHistoryForManga(history: List<DHistory>) {
// List containing history to be updated
val historyToBeUpdated = mutableListOf<History>()
val historyToBeUpdated = ArrayList<History>(history.size)
for ((url, lastRead) in history) {
val dbHistory = databaseHelper.getHistoryByChapterUrl(url).executeAsBlocking()
// Check if history already in database and update
@ -361,14 +345,14 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
* @param tracks the track list to restore.
*/
internal fun restoreTrackForManga(manga: Manga, tracks: List<Track>) {
// Fix foreign keys with the current manga id
tracks.map { it.manga_id = manga.id!! }
// Get tracks from database
val dbTracks = databaseHelper.getTracks(manga).executeAsBlocking()
val trackToUpdate = mutableListOf<Track>()
val trackToUpdate = ArrayList<Track>(tracks.size)
tracks.forEach { track ->
// Fix foreign keys with the current manga id
track.manga_id = manga.id!!
val service = trackManager.getService(track.sync_id)
if (service != null && service.isLogged) {
var isInDatabase = false
@ -423,12 +407,13 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
chapter.copyFrom(dbChapter)
break
}
}
// Filter the chapters that couldn't be found.
chapters.filter { it.id != null }
chapters.map { it.manga_id = manga.id }
updateChapters(chapters)
chapter.manga_id = manga.id
}
// Filter the chapters that couldn't be found.
updateChapters(chapters.filter { it.id != null })
return true
}
}

View file

@ -68,7 +68,7 @@ class BackupTest {
fun setup() {
app = RuntimeEnvironment.application
context = app.applicationContext
legacyBackupManager = LegacyBackupManager(context)
legacyBackupManager = LegacyBackupManager(context, 2)
db = legacyBackupManager.databaseHelper
// Mock the source manager
@ -84,6 +84,8 @@ class BackupTest {
root.add(Backup.MANGAS, mangaEntries)
root.add(Backup.CATEGORIES, categoryEntries)
clearJson()
}
/**
@ -91,9 +93,6 @@ class BackupTest {
*/
@Test
fun testRestoreEmptyCategory() {
// Initialize json with version 2
initializeJsonTest(2)
// Create backup of empty database
legacyBackupManager.backupCategories(categoryEntries)
@ -110,9 +109,6 @@ class BackupTest {
*/
@Test
fun testRestoreSingleCategory() {
// Initialize json with version 2
initializeJsonTest(2)
// Create category and add to json
val category = addSingleCategory("category")
@ -130,9 +126,6 @@ class BackupTest {
*/
@Test
fun testRestoreMultipleCategories() {
// Initialize json with version 2
initializeJsonTest(2)
// Create category and add to json
val category = addSingleCategory("category")
val category2 = addSingleCategory("category2")
@ -161,9 +154,6 @@ class BackupTest {
*/
@Test
fun testRestoreManga() {
// Initialize json with version 2
initializeJsonTest(2)
// Add manga to database
val manga = getSingleManga("One Piece")
manga.viewer = 3
@ -227,9 +217,6 @@ class BackupTest {
*/
@Test
fun testRestoreChapters() {
// Initialize json with version 2
initializeJsonTest(2)
// Insert manga
val manga = getSingleManga("One Piece")
manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
@ -266,9 +253,6 @@ class BackupTest {
*/
@Test
fun restoreHistoryForManga() {
// Initialize json with version 2
initializeJsonTest(2)
val manga = getSingleManga("One Piece")
manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
@ -300,9 +284,6 @@ class BackupTest {
*/
@Test
fun restoreTrackForManga() {
// Initialize json with version 2
initializeJsonTest(2)
// Create mangas
val manga = getSingleManga("One Piece")
val manga2 = getSingleManga("Bleach")
@ -359,31 +340,26 @@ class BackupTest {
assertThat(trackDB[0].last_chapter_read).isEqualTo(10)
}
fun clearJson() {
private fun clearJson() {
root = JsonObject()
information = JsonObject()
mangaEntries = JsonArray()
categoryEntries = JsonArray()
}
fun initializeJsonTest(version: Int) {
clearJson()
legacyBackupManager.setVersion(version)
}
fun addSingleCategory(name: String): Category {
private fun addSingleCategory(name: String): Category {
val category = Category.create(name)
val catJson = legacyBackupManager.parser.toJsonTree(category)
categoryEntries.add(catJson)
return category
}
fun clearDatabase() {
private fun clearDatabase() {
db.deleteMangas().executeAsBlocking()
db.deleteHistory().executeAsBlocking()
}
fun getSingleHistory(chapter: Chapter): DHistory {
private fun getSingleHistory(chapter: Chapter): DHistory {
return DHistory(chapter.url, 1000)
}