Table for Track Searches (#833)

* Table for Track Searches

* Lint
This commit is contained in:
Mitchell Syer
2024-01-20 23:12:18 -05:00
committed by GitHub
parent 621468a183
commit 46e1e4c043
13 changed files with 228 additions and 92 deletions

View File

@@ -10,7 +10,7 @@ package suwayomi.tachidesk.manga.model.dataclass
data class TrackRecordDataClass(
val id: Int,
val mangaId: Int,
val syncId: Int,
val trackerId: Int,
val remoteId: Long,
val libraryId: Long?,
val title: String,

View File

@@ -11,8 +11,9 @@ import kotlinx.serialization.Serializable
@Serializable
data class TrackSearchDataClass(
val syncId: Int,
val mediaId: Long,
val id: Int,
val trackerId: Int,
val remoteId: Long,
val title: String,
val totalChapters: Int,
val trackingUrl: String,

View File

@@ -12,7 +12,7 @@ import org.jetbrains.exposed.sql.ReferenceOption
object TrackRecordTable : IntIdTable() {
val mangaId = reference("manga_id", MangaTable, ReferenceOption.CASCADE)
val syncId = integer("sync_id")
val trackerId = integer("sync_id")
val remoteId = long("remote_id")
val libraryId = long("library_id").nullable()
val title = varchar("title", 512)

View File

@@ -0,0 +1,105 @@
package suwayomi.tachidesk.manga.model.table
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.statements.BatchUpdateStatement
import org.jetbrains.exposed.sql.transactions.transaction
import suwayomi.tachidesk.manga.impl.track.tracker.model.TrackSearch
object TrackSearchTable : IntIdTable() {
val trackerId = integer("tracker_id")
val remoteId = long("remote_id")
val title = varchar("title", 512)
val totalChapters = integer("total_chapters")
val trackingUrl = varchar("tracking_url", 512)
val coverUrl = varchar("cover_url", 512)
val summary = varchar("summary", 4096)
val publishingStatus = varchar("publishing_status", 512)
val publishingType = varchar("publishing_type", 512)
val startDate = varchar("start_date", 128)
}
fun List<TrackSearch>.insertAll(): List<ResultRow> {
if (isEmpty()) return emptyList()
return transaction {
val trackerIds = map { it.sync_id }.toSet()
val remoteIds = map { it.media_id }.toSet()
val existing =
transaction {
TrackSearchTable.select {
TrackSearchTable.trackerId inList trackerIds and (TrackSearchTable.remoteId inList remoteIds)
}.toList()
}
val grouped = mutableMapOf<Boolean, MutableList<Pair<Int?, TrackSearch>>>()
forEach { trackSearch ->
val existingRow =
existing.find {
it[TrackSearchTable.trackerId] == trackSearch.sync_id &&
it[TrackSearchTable.remoteId] == trackSearch.media_id
}
grouped.getOrPut(existingRow != null) { mutableListOf() }
.add(existingRow?.get(TrackSearchTable.id)?.value to trackSearch)
}
val toUpdate = grouped[true]
val toInsert = grouped[false]?.map { it.second }
if (!toUpdate.isNullOrEmpty()) {
BatchUpdateStatement(TrackSearchTable).apply {
toUpdate.forEach { (id, trackSearch) ->
id ?: return@forEach
addBatch(EntityID(id, TrackSearchTable))
this[TrackSearchTable.title] = trackSearch.title.take(512)
this[TrackSearchTable.totalChapters] = trackSearch.total_chapters
this[TrackSearchTable.trackingUrl] = trackSearch.tracking_url.take(512)
this[TrackSearchTable.coverUrl] = trackSearch.cover_url.take(512)
this[TrackSearchTable.summary] = trackSearch.summary.take(4096)
this[TrackSearchTable.publishingStatus] = trackSearch.publishing_status.take(512)
this[TrackSearchTable.publishingType] = trackSearch.publishing_type.take(512)
this[TrackSearchTable.startDate] = trackSearch.start_date.take(128)
}
execute(this@transaction)
}
}
val insertedRows =
if (!toInsert.isNullOrEmpty()) {
TrackSearchTable.batchInsert(toInsert) {
this[TrackSearchTable.trackerId] = it.sync_id
this[TrackSearchTable.remoteId] = it.media_id
this[TrackSearchTable.title] = it.title.take(512)
this[TrackSearchTable.totalChapters] = it.total_chapters
this[TrackSearchTable.trackingUrl] = it.tracking_url.take(512)
this[TrackSearchTable.coverUrl] = it.cover_url.take(512)
this[TrackSearchTable.summary] = it.summary.take(4096)
this[TrackSearchTable.publishingStatus] = it.publishing_status.take(512)
this[TrackSearchTable.publishingType] = it.publishing_type.take(512)
this[TrackSearchTable.startDate] = it.start_date.take(128)
}
} else {
emptyList()
}
val updatedRows =
toUpdate?.mapNotNull { it.first }?.let { ids ->
transaction { TrackSearchTable.select { TrackSearchTable.id inList ids }.toList() }
}.orEmpty()
(insertedRows + updatedRows)
.sortedBy { row ->
indexOfFirst {
it.sync_id == row[TrackSearchTable.trackerId] &&
it.media_id == row[TrackSearchTable.remoteId]
}
}
}
}