musikr: buffer discovered playlist files during exploration

This commit is contained in:
jay
2026-11-12 18:40:40 -05:00
parent 38bd9cd260
commit 39deae7644
5 changed files with 35 additions and 23 deletions
@@ -19,9 +19,11 @@
package org.oxycblt.musikr
import android.content.Context
import java.util.Collections
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import org.oxycblt.musikr.fs.File
import org.oxycblt.musikr.pipeline.EvaluateStep
import org.oxycblt.musikr.pipeline.ExploreStep
import org.oxycblt.musikr.pipeline.Explored
@@ -117,7 +119,8 @@ private class MusikrImpl(
var explored = 0
var loaded = 0
val exploredChannel = Channel<Explored>(Channel.UNLIMITED)
val exploredTask = exploreStep.explore(this, exploredChannel)
val playlistFiles = Collections.synchronizedList(mutableListOf<File>())
val exploredTask = exploreStep.explore(this, exploredChannel, playlistFiles)
val trackedExploredChannel = Channel<Explored>(Channel.UNLIMITED)
val trackedExploredTask =
tryAsyncWith(trackedExploredChannel, Dispatchers.Main) {
@@ -139,7 +142,7 @@ private class MusikrImpl(
}
onProgress(IndexingProgress.Indeterminate)
}
val library = evaluateStep.evaluate(trackedExtractedChannel)
val library = evaluateStep.evaluate(trackedExtractedChannel, playlistFiles)
merge(exploredTask, extractedTask, trackedExploredTask, trackedExtractedTask).await()
LibraryResultImpl(config, library)
}
@@ -24,6 +24,7 @@ import org.oxycblt.musikr.BuildConfig
import org.oxycblt.musikr.Config
import org.oxycblt.musikr.Interpretation
import org.oxycblt.musikr.MutableLibrary
import org.oxycblt.musikr.fs.File
import org.oxycblt.musikr.graph.MusicGraph
import org.oxycblt.musikr.model.LibraryFactory
import org.oxycblt.musikr.playlist.db.StoredPlaylists
@@ -31,7 +32,10 @@ import org.oxycblt.musikr.playlist.interpret.PlaylistInterpreter
import org.oxycblt.musikr.tag.interpret.TagInterpreter
internal interface EvaluateStep {
suspend fun evaluate(extractedMusic: Channel<Extracted>): MutableLibrary
suspend fun evaluate(
extractedMusic: Channel<Extracted>,
playlistFiles: List<File>,
): MutableLibrary
companion object {
fun new(context: Context, config: Config, interpretation: Interpretation): EvaluateStep =
@@ -52,16 +56,23 @@ private class EvaluateStepImpl(
private val storedPlaylists: StoredPlaylists,
private val libraryFactory: LibraryFactory,
) : EvaluateStep {
override suspend fun evaluate(extractedMusic: Channel<Extracted>): MutableLibrary {
override suspend fun evaluate(
extractedMusic: Channel<Extracted>,
playlistFiles: List<File>,
): MutableLibrary {
val builder = MusicGraph.builder()
for (extracted in extractedMusic) {
when (extracted) {
is RawSong -> builder.add(tagInterpreter.interpret(extracted))
is RawPlaylist -> builder.add(playlistInterpreter.interpret(extracted.file))
is NotAudio -> {}
is InvalidSong -> {}
}
}
// Stored playlists enter the graph here, after the songs, so that a reconcile pass can
// update them first (added in a later change; playlistFiles is threaded for it).
for (playlistFile in storedPlaylists.read()) {
builder.add(playlistInterpreter.interpret(playlistFile))
}
val graph = builder.build()
// Render graph to Graphviz in debug mode
@@ -36,7 +36,16 @@ import org.oxycblt.musikr.util.merge
import org.oxycblt.musikr.util.tryAsyncWith
internal interface ExploreStep {
suspend fun explore(scope: CoroutineScope, explored: Channel<Explored>): Deferred<Result<Unit>>
/**
* Explore the file system for music, streaming the results into [explored]. Discovered
* playlist files are buffered into [playlistFiles] for reconciliation once the library is
* fully extracted.
*/
suspend fun explore(
scope: CoroutineScope,
explored: Channel<Explored>,
playlistFiles: MutableList<File>,
): Deferred<Result<Unit>>
companion object {
fun from(context: Context, config: Config): ExploreStep =
@@ -48,6 +57,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
override suspend fun explore(
scope: CoroutineScope,
explored: Channel<Explored>,
playlistFiles: MutableList<File>,
): Deferred<Result<Unit>> {
val files = Channel<File>(Channel.UNLIMITED)
val filesTask = fs.explore(files)
@@ -62,6 +72,10 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
file.mimeType != "application/x-ogg" &&
file.mimeType != "application/octet-stream")
) {
if (file.mimeType == M3U.MIME_TYPE) {
// Not audio, but still wanted: buffer for the reconcile pass.
playlistFiles.add(file)
}
return@mapParallel Finalized(NotAudio)
}
when (val cacheResult = storage.cache.read(file)) {
@@ -100,26 +114,14 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage)
}
}
}
val playlists = Channel<Explored>(Channel.UNLIMITED)
val playlistsTask =
scope.tryAsyncWith(playlists, Dispatchers.IO) {
for (playlist in storage.storedPlaylists.read()) {
val rawPlaylist = RawPlaylist(playlist)
it.send(rawPlaylist)
}
}
val mergeTask =
scope.tryAsyncWith(explored, Dispatchers.Main) {
for (item in finalized) {
it.send(item.explored)
}
for (playlist in playlists) {
it.send(playlist)
}
}
return scope.merge(filesTask, classifiedTask, exploredTask, playlistsTask, mergeTask)
return scope.merge(filesTask, classifiedTask, exploredTask, mergeTask)
}
private sealed interface Classified
@@ -73,7 +73,6 @@ private class ExtractStepImpl(
scope.mapParallel(PARALLELISM, explored, extract, Dispatchers.IO) { item ->
when (item) {
is RawSong -> Finalized(item)
is RawPlaylist -> Finalized(item)
is NewSong -> {
when (val result = metadataExtractor.extract(item.file)) {
is MetadataResult.Success ->
@@ -21,7 +21,6 @@ package org.oxycblt.musikr.pipeline
import org.oxycblt.musikr.covers.Cover
import org.oxycblt.musikr.fs.File
import org.oxycblt.musikr.metadata.Properties
import org.oxycblt.musikr.playlist.PlaylistFile
import org.oxycblt.musikr.tag.parse.ParsedTags
internal sealed interface PipelineItem
@@ -48,8 +47,6 @@ internal data object InvalidSong : Extracted.Invalid
internal data object NotAudio : Explored.Known, Extracted.Valid
internal data class RawPlaylist(val file: PlaylistFile) : Explored.Known, Extracted.Valid
internal data class RawSong(
val file: File,
val properties: Properties,