Skip to content

Commit 6fbd2f1

Browse files
schrodaSyer10
andauthored
Feature/remove download ahead logic (#867)
* Remove download ahead logic Unnecessary on server side, should just be done by the client * Rename "autoDownloadAheadLimit" to "autoDownloadNewChaptersLimit" * Deprecate the old field * Update Stable WebUI * Update Stable WebUI --------- Co-authored-by: Syer10 <[email protected]>
1 parent 9edbc7f commit 6fbd2f1

File tree

9 files changed

+28
-132
lines changed

9 files changed

+28
-132
lines changed

buildSrc/src/main/kotlin/Constants.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const val MainClass = "suwayomi.tachidesk.MainKt"
1212
// should be bumped with each stable release
1313
val tachideskVersion = System.getenv("ProductVersion") ?: "v0.7.0"
1414

15-
val webUIRevisionTag = System.getenv("WebUIRevision") ?: "r1335"
15+
val webUIRevisionTag = System.getenv("WebUIRevision") ?: "r1397"
1616

1717
// counts commits on the current checked out branch
1818
val getTachideskRevision = {

server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/DownloadMutation.kt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import org.jetbrains.exposed.sql.transactions.transaction
77
import suwayomi.tachidesk.graphql.types.ChapterType
88
import suwayomi.tachidesk.graphql.types.DownloadStatus
99
import suwayomi.tachidesk.manga.impl.Chapter
10-
import suwayomi.tachidesk.manga.impl.Manga
1110
import suwayomi.tachidesk.manga.impl.download.DownloadManager
1211
import suwayomi.tachidesk.manga.impl.download.model.Status
1312
import suwayomi.tachidesk.manga.model.table.ChapterTable
@@ -269,20 +268,4 @@ class DownloadMutation {
269268
)
270269
}
271270
}
272-
273-
data class DownloadAheadInput(
274-
val clientMutationId: String? = null,
275-
val mangaIds: List<Int> = emptyList(),
276-
val latestReadChapterIds: List<Int>? = null,
277-
)
278-
279-
data class DownloadAheadPayload(val clientMutationId: String?)
280-
281-
fun downloadAhead(input: DownloadAheadInput): DownloadAheadPayload {
282-
val (clientMutationId, mangaIds, latestReadChapterIds) = input
283-
284-
Manga.downloadAhead(mangaIds, latestReadChapterIds ?: emptyList())
285-
286-
return DownloadAheadPayload(clientMutationId)
287-
}
288271
}

server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class SettingsMutation {
5353
updateSetting(settings.downloadsPath, serverConfig.downloadsPath)
5454
updateSetting(settings.autoDownloadNewChapters, serverConfig.autoDownloadNewChapters)
5555
updateSetting(settings.excludeEntryWithUnreadChapters, serverConfig.excludeEntryWithUnreadChapters)
56-
updateSetting(settings.autoDownloadAheadLimit, serverConfig.autoDownloadAheadLimit)
56+
updateSetting(settings.autoDownloadAheadLimit, serverConfig.autoDownloadNewChaptersLimit) // deprecated
57+
updateSetting(settings.autoDownloadNewChaptersLimit, serverConfig.autoDownloadNewChaptersLimit)
5758

5859
// extension
5960
updateSetting(settings.extensionRepos, serverConfig.extensionRepos)

server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package suwayomi.tachidesk.graphql.types
99

10+
import com.expediagroup.graphql.generator.annotations.GraphQLDeprecated
1011
import suwayomi.tachidesk.graphql.server.primitives.Node
1112
import suwayomi.tachidesk.server.ServerConfig
1213
import suwayomi.tachidesk.server.serverConfig
@@ -38,7 +39,13 @@ interface Settings : Node {
3839
val downloadsPath: String?
3940
val autoDownloadNewChapters: Boolean?
4041
val excludeEntryWithUnreadChapters: Boolean?
42+
43+
@GraphQLDeprecated(
44+
"Replaced with autoDownloadNewChaptersLimit",
45+
replaceWith = ReplaceWith("autoDownloadNewChaptersLimit"),
46+
)
4147
val autoDownloadAheadLimit: Int?
48+
val autoDownloadNewChaptersLimit: Int?
4249

4350
// extension
4451
val extensionRepos: List<String>?
@@ -99,7 +106,12 @@ data class PartialSettingsType(
99106
override val downloadsPath: String?,
100107
override val autoDownloadNewChapters: Boolean?,
101108
override val excludeEntryWithUnreadChapters: Boolean?,
109+
@GraphQLDeprecated(
110+
"Replaced with autoDownloadNewChaptersLimit",
111+
replaceWith = ReplaceWith("autoDownloadNewChaptersLimit"),
112+
)
102113
override val autoDownloadAheadLimit: Int?,
114+
override val autoDownloadNewChaptersLimit: Int?,
103115
// extension
104116
override val extensionRepos: List<String>?,
105117
// requests
@@ -152,7 +164,12 @@ class SettingsType(
152164
override val downloadsPath: String,
153165
override val autoDownloadNewChapters: Boolean,
154166
override val excludeEntryWithUnreadChapters: Boolean,
167+
@GraphQLDeprecated(
168+
"Replaced with autoDownloadNewChaptersLimit",
169+
replaceWith = ReplaceWith("autoDownloadNewChaptersLimit"),
170+
)
155171
override val autoDownloadAheadLimit: Int,
172+
override val autoDownloadNewChaptersLimit: Int,
156173
// extension
157174
override val extensionRepos: List<String>,
158175
// requests
@@ -204,7 +221,8 @@ class SettingsType(
204221
config.downloadsPath.value,
205222
config.autoDownloadNewChapters.value,
206223
config.excludeEntryWithUnreadChapters.value,
207-
config.autoDownloadAheadLimit.value,
224+
config.autoDownloadNewChaptersLimit.value, // deprecated
225+
config.autoDownloadNewChaptersLimit.value,
208226
// extension
209227
config.extensionRepos.value,
210228
// requests

server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ object Chapter {
310310
"mangaId= $mangaId, " +
311311
"prevNumberOfChapters= $prevNumberOfChapters, " +
312312
"updatedChapterList= ${updatedChapterList.size}, " +
313-
"downloadAheadLimit= ${serverConfig.autoDownloadAheadLimit.value}" +
313+
"autoDownloadNewChaptersLimit= ${serverConfig.autoDownloadNewChaptersLimit.value}" +
314314
")",
315315
)
316316

@@ -389,8 +389,8 @@ object Chapter {
389389
}
390390

391391
val firstChapterToDownloadIndex =
392-
if (serverConfig.autoDownloadAheadLimit.value > 0) {
393-
(numberOfNewChapters - serverConfig.autoDownloadAheadLimit.value).coerceAtLeast(0)
392+
if (serverConfig.autoDownloadNewChaptersLimit.value > 0) {
393+
(numberOfNewChapters - serverConfig.autoDownloadNewChaptersLimit.value).coerceAtLeast(0)
394394
} else {
395395
0
396396
}

server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Manga.kt

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import org.kodein.di.conf.global
2727
import org.kodein.di.instance
2828
import suwayomi.tachidesk.manga.impl.MangaList.proxyThumbnailUrl
2929
import suwayomi.tachidesk.manga.impl.Source.getSource
30-
import suwayomi.tachidesk.manga.impl.download.DownloadManager
31-
import suwayomi.tachidesk.manga.impl.download.DownloadManager.EnqueueInput
3230
import suwayomi.tachidesk.manga.impl.download.fileProvider.impl.MissingThumbnailException
3331
import suwayomi.tachidesk.manga.impl.track.Track
3432
import suwayomi.tachidesk.manga.impl.util.network.await
@@ -47,15 +45,11 @@ import suwayomi.tachidesk.manga.model.table.MangaStatus
4745
import suwayomi.tachidesk.manga.model.table.MangaTable
4846
import suwayomi.tachidesk.manga.model.table.toDataClass
4947
import suwayomi.tachidesk.server.ApplicationDirs
50-
import suwayomi.tachidesk.server.serverConfig
5148
import uy.kohesive.injekt.injectLazy
5249
import java.io.File
5350
import java.io.IOException
5451
import java.io.InputStream
5552
import java.time.Instant
56-
import java.util.Timer
57-
import java.util.TimerTask
58-
import java.util.concurrent.ConcurrentHashMap
5953

6054
private val logger = KotlinLogging.logger { }
6155

@@ -339,104 +333,4 @@ object Manga {
339333
clearCachedImage(applicationDirs.tempThumbnailCacheRoot, fileName)
340334
clearCachedImage(applicationDirs.thumbnailDownloadsRoot, fileName)
341335
}
342-
343-
private val downloadAheadQueue = ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<Int, Boolean>>()
344-
private var downloadAheadTimer: Timer? = null
345-
346-
private const val MANGAS_KEY = "mangaIds"
347-
private const val CHAPTERS_KEY = "chapterIds"
348-
349-
fun downloadAhead(
350-
mangaIds: List<Int>,
351-
latestReadChapterIds: List<Int> = emptyList(),
352-
) {
353-
if (serverConfig.autoDownloadAheadLimit.value == 0) {
354-
return
355-
}
356-
357-
val updateDownloadAheadQueue = { key: String, ids: List<Int> ->
358-
val idSet = downloadAheadQueue[key] ?: ConcurrentHashMap.newKeySet()
359-
idSet.addAll(ids)
360-
downloadAheadQueue[key] = idSet
361-
}
362-
363-
updateDownloadAheadQueue(MANGAS_KEY, mangaIds)
364-
updateDownloadAheadQueue(CHAPTERS_KEY, latestReadChapterIds)
365-
366-
// handle cases where this function gets called multiple times in quick succession.
367-
// this could happen in case e.g. multiple chapters get marked as read without batching the operation
368-
downloadAheadTimer?.cancel()
369-
downloadAheadTimer =
370-
Timer().apply {
371-
schedule(
372-
object : TimerTask() {
373-
override fun run() {
374-
downloadAheadChapters(
375-
downloadAheadQueue[MANGAS_KEY]?.toList().orEmpty(),
376-
downloadAheadQueue[CHAPTERS_KEY]?.toList().orEmpty(),
377-
)
378-
downloadAheadQueue.clear()
379-
}
380-
},
381-
5000,
382-
)
383-
}
384-
}
385-
386-
/**
387-
* Downloads the latest unread and not downloaded chapters for each passed manga id.
388-
*
389-
* To pass a specific chapter as the latest read chapter for a manga, it can be provided in the "latestReadChapterIds" list.
390-
* This makes it possible to handle cases, where the actual latest read chapter isn't marked as read yet.
391-
* E.g. the client marks a chapter as read and at the same time sends the "downloadAhead" mutation.
392-
* In this case, the latest read chapter could potentially be the one, that just got send to get marked as read by the client.
393-
* Without providing it in "latestReadChapterIds" it could be incorrectly included in the chapters, that will get downloaded.
394-
*
395-
* The latest read chapter will be considered the starting point.
396-
* E.g.:
397-
* - 20 chapters
398-
* - chapter 15 marked as read
399-
* - 16 - 20 marked as unread
400-
* - 10 - 14 marked as unread
401-
*
402-
* will download the unread chapters starting from chapter 15
403-
*/
404-
private fun downloadAheadChapters(
405-
mangaIds: List<Int>,
406-
latestReadChapterIds: List<Int>,
407-
) {
408-
val mangaToLatestReadChapterIndex =
409-
transaction {
410-
ChapterTable.select { (ChapterTable.manga inList mangaIds) and (ChapterTable.isRead eq true) }
411-
.orderBy(ChapterTable.sourceOrder to SortOrder.DESC).groupBy { it[ChapterTable.manga].value }
412-
}.mapValues { (_, chapters) -> chapters.firstOrNull()?.let { it[ChapterTable.sourceOrder] } ?: 0 }
413-
414-
val mangaToUnreadChaptersMap =
415-
transaction {
416-
ChapterTable.select { (ChapterTable.manga inList mangaIds) and (ChapterTable.isRead eq false) }
417-
.orderBy(ChapterTable.sourceOrder to SortOrder.DESC)
418-
.groupBy { it[ChapterTable.manga].value }
419-
}
420-
421-
val chapterIdsToDownload =
422-
mangaToUnreadChaptersMap.map { (mangaId, unreadChapters) ->
423-
val latestReadChapterIndex = mangaToLatestReadChapterIndex[mangaId] ?: 0
424-
val lastChapterToDownloadIndex =
425-
unreadChapters.indexOfLast {
426-
it[ChapterTable.sourceOrder] > latestReadChapterIndex &&
427-
it[ChapterTable.id].value !in latestReadChapterIds
428-
}
429-
val unreadChaptersToConsider = unreadChapters.subList(0, lastChapterToDownloadIndex + 1)
430-
val firstChapterToDownloadIndex =
431-
(unreadChaptersToConsider.size - serverConfig.autoDownloadAheadLimit.value).coerceAtLeast(0)
432-
unreadChaptersToConsider.subList(firstChapterToDownloadIndex, lastChapterToDownloadIndex + 1)
433-
.filter { !it[ChapterTable.isDownloaded] }
434-
.map { it[ChapterTable.id].value }
435-
}.flatten()
436-
437-
logger.info { "downloadAheadChapters: download chapters [${chapterIdsToDownload.joinToString(", ")}]" }
438-
439-
DownloadManager.dequeue(mangaIds, chapterIdsToDownload)
440-
DownloadManager.enqueue(EnqueueInput(chapterIdsToDownload))
441-
}
442336
}

server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ServerConfig(getConfig: () -> Config, val moduleName: String = SERVER_CONF
9797
val downloadsPath: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter)
9898
val autoDownloadNewChapters: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
9999
val excludeEntryWithUnreadChapters: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
100-
val autoDownloadAheadLimit: MutableStateFlow<Int> by OverrideConfigValue(IntConfigAdapter)
100+
val autoDownloadNewChaptersLimit: MutableStateFlow<Int> by OverrideConfigValue(IntConfigAdapter)
101101

102102
// extensions
103103
val extensionRepos: MutableStateFlow<List<String>> by OverrideConfigValues(StringConfigAdapter)

server/src/main/resources/server-reference.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ server.downloadAsCbz = false
2121
server.downloadsPath = ""
2222
server.autoDownloadNewChapters = false # if new chapters that have been retrieved should get automatically downloaded
2323
server.excludeEntryWithUnreadChapters = true # ignore automatic chapter downloads of entries with unread chapters
24-
server.autoDownloadAheadLimit = 0 # 0 to disable it - how many unread downloaded chapters should be available - if the limit is reached, new chapters won't be downloaded automatically. this limit will also be applied to the auto download of new chapters on an update
24+
server.autoDownloadNewChaptersLimit = 0 # 0 to disable it - how many unread downloaded chapters should be available - if the limit is reached, new chapters won't be downloaded automatically. this limit will also be applied to the auto download of new chapters on an update
2525

2626
# extension repos
2727
server.extensionRepos = [

server/src/test/resources/server-reference.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ server.socksProxyPort = ""
1111
server.downloadAsCbz = false
1212
server.autoDownloadNewChapters = false
1313
server.excludeEntryWithUnreadChapters = true
14-
server.autoDownloadAheadLimit = 0
14+
server.autoDownloadNewChaptersLimit = 0
1515

1616
# requests
1717
server.maxSourcesInParallel = 10

0 commit comments

Comments
 (0)