musikr: buffer discovered playlist files during exploration
This commit is contained in:
@@ -19,9 +19,11 @@
|
|||||||
package org.oxycblt.musikr
|
package org.oxycblt.musikr
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import java.util.Collections
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.pipeline.EvaluateStep
|
import org.oxycblt.musikr.pipeline.EvaluateStep
|
||||||
import org.oxycblt.musikr.pipeline.ExploreStep
|
import org.oxycblt.musikr.pipeline.ExploreStep
|
||||||
import org.oxycblt.musikr.pipeline.Explored
|
import org.oxycblt.musikr.pipeline.Explored
|
||||||
@@ -117,7 +119,8 @@ private class MusikrImpl(
|
|||||||
var explored = 0
|
var explored = 0
|
||||||
var loaded = 0
|
var loaded = 0
|
||||||
val exploredChannel = Channel<Explored>(Channel.UNLIMITED)
|
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 trackedExploredChannel = Channel<Explored>(Channel.UNLIMITED)
|
||||||
val trackedExploredTask =
|
val trackedExploredTask =
|
||||||
tryAsyncWith(trackedExploredChannel, Dispatchers.Main) {
|
tryAsyncWith(trackedExploredChannel, Dispatchers.Main) {
|
||||||
@@ -139,7 +142,7 @@ private class MusikrImpl(
|
|||||||
}
|
}
|
||||||
onProgress(IndexingProgress.Indeterminate)
|
onProgress(IndexingProgress.Indeterminate)
|
||||||
}
|
}
|
||||||
val library = evaluateStep.evaluate(trackedExtractedChannel)
|
val library = evaluateStep.evaluate(trackedExtractedChannel, playlistFiles)
|
||||||
merge(exploredTask, extractedTask, trackedExploredTask, trackedExtractedTask).await()
|
merge(exploredTask, extractedTask, trackedExploredTask, trackedExtractedTask).await()
|
||||||
LibraryResultImpl(config, library)
|
LibraryResultImpl(config, library)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.oxycblt.musikr.BuildConfig
|
|||||||
import org.oxycblt.musikr.Config
|
import org.oxycblt.musikr.Config
|
||||||
import org.oxycblt.musikr.Interpretation
|
import org.oxycblt.musikr.Interpretation
|
||||||
import org.oxycblt.musikr.MutableLibrary
|
import org.oxycblt.musikr.MutableLibrary
|
||||||
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.graph.MusicGraph
|
import org.oxycblt.musikr.graph.MusicGraph
|
||||||
import org.oxycblt.musikr.model.LibraryFactory
|
import org.oxycblt.musikr.model.LibraryFactory
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
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
|
import org.oxycblt.musikr.tag.interpret.TagInterpreter
|
||||||
|
|
||||||
internal interface EvaluateStep {
|
internal interface EvaluateStep {
|
||||||
suspend fun evaluate(extractedMusic: Channel<Extracted>): MutableLibrary
|
suspend fun evaluate(
|
||||||
|
extractedMusic: Channel<Extracted>,
|
||||||
|
playlistFiles: List<File>,
|
||||||
|
): MutableLibrary
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun new(context: Context, config: Config, interpretation: Interpretation): EvaluateStep =
|
fun new(context: Context, config: Config, interpretation: Interpretation): EvaluateStep =
|
||||||
@@ -52,16 +56,23 @@ private class EvaluateStepImpl(
|
|||||||
private val storedPlaylists: StoredPlaylists,
|
private val storedPlaylists: StoredPlaylists,
|
||||||
private val libraryFactory: LibraryFactory,
|
private val libraryFactory: LibraryFactory,
|
||||||
) : EvaluateStep {
|
) : EvaluateStep {
|
||||||
override suspend fun evaluate(extractedMusic: Channel<Extracted>): MutableLibrary {
|
override suspend fun evaluate(
|
||||||
|
extractedMusic: Channel<Extracted>,
|
||||||
|
playlistFiles: List<File>,
|
||||||
|
): MutableLibrary {
|
||||||
val builder = MusicGraph.builder()
|
val builder = MusicGraph.builder()
|
||||||
for (extracted in extractedMusic) {
|
for (extracted in extractedMusic) {
|
||||||
when (extracted) {
|
when (extracted) {
|
||||||
is RawSong -> builder.add(tagInterpreter.interpret(extracted))
|
is RawSong -> builder.add(tagInterpreter.interpret(extracted))
|
||||||
is RawPlaylist -> builder.add(playlistInterpreter.interpret(extracted.file))
|
|
||||||
is NotAudio -> {}
|
is NotAudio -> {}
|
||||||
is InvalidSong -> {}
|
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()
|
val graph = builder.build()
|
||||||
|
|
||||||
// Render graph to Graphviz in debug mode
|
// Render graph to Graphviz in debug mode
|
||||||
|
|||||||
@@ -36,7 +36,16 @@ import org.oxycblt.musikr.util.merge
|
|||||||
import org.oxycblt.musikr.util.tryAsyncWith
|
import org.oxycblt.musikr.util.tryAsyncWith
|
||||||
|
|
||||||
internal interface ExploreStep {
|
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 {
|
companion object {
|
||||||
fun from(context: Context, config: Config): ExploreStep =
|
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(
|
override suspend fun explore(
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
explored: Channel<Explored>,
|
explored: Channel<Explored>,
|
||||||
|
playlistFiles: MutableList<File>,
|
||||||
): Deferred<Result<Unit>> {
|
): Deferred<Result<Unit>> {
|
||||||
val files = Channel<File>(Channel.UNLIMITED)
|
val files = Channel<File>(Channel.UNLIMITED)
|
||||||
val filesTask = fs.explore(files)
|
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/x-ogg" &&
|
||||||
file.mimeType != "application/octet-stream")
|
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)
|
return@mapParallel Finalized(NotAudio)
|
||||||
}
|
}
|
||||||
when (val cacheResult = storage.cache.read(file)) {
|
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 =
|
val mergeTask =
|
||||||
scope.tryAsyncWith(explored, Dispatchers.Main) {
|
scope.tryAsyncWith(explored, Dispatchers.Main) {
|
||||||
for (item in finalized) {
|
for (item in finalized) {
|
||||||
it.send(item.explored)
|
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
|
private sealed interface Classified
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ private class ExtractStepImpl(
|
|||||||
scope.mapParallel(PARALLELISM, explored, extract, Dispatchers.IO) { item ->
|
scope.mapParallel(PARALLELISM, explored, extract, Dispatchers.IO) { item ->
|
||||||
when (item) {
|
when (item) {
|
||||||
is RawSong -> Finalized(item)
|
is RawSong -> Finalized(item)
|
||||||
is RawPlaylist -> Finalized(item)
|
|
||||||
is NewSong -> {
|
is NewSong -> {
|
||||||
when (val result = metadataExtractor.extract(item.file)) {
|
when (val result = metadataExtractor.extract(item.file)) {
|
||||||
is MetadataResult.Success ->
|
is MetadataResult.Success ->
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ package org.oxycblt.musikr.pipeline
|
|||||||
import org.oxycblt.musikr.covers.Cover
|
import org.oxycblt.musikr.covers.Cover
|
||||||
import org.oxycblt.musikr.fs.File
|
import org.oxycblt.musikr.fs.File
|
||||||
import org.oxycblt.musikr.metadata.Properties
|
import org.oxycblt.musikr.metadata.Properties
|
||||||
import org.oxycblt.musikr.playlist.PlaylistFile
|
|
||||||
import org.oxycblt.musikr.tag.parse.ParsedTags
|
import org.oxycblt.musikr.tag.parse.ParsedTags
|
||||||
|
|
||||||
internal sealed interface PipelineItem
|
internal sealed interface PipelineItem
|
||||||
@@ -48,8 +47,6 @@ internal data object InvalidSong : Extracted.Invalid
|
|||||||
|
|
||||||
internal data object NotAudio : Explored.Known, Extracted.Valid
|
internal data object NotAudio : Explored.Known, Extracted.Valid
|
||||||
|
|
||||||
internal data class RawPlaylist(val file: PlaylistFile) : Explored.Known, Extracted.Valid
|
|
||||||
|
|
||||||
internal data class RawSong(
|
internal data class RawSong(
|
||||||
val file: File,
|
val file: File,
|
||||||
val properties: Properties,
|
val properties: Properties,
|
||||||
|
|||||||
Reference in New Issue
Block a user