From 3aa56ad5b68c7756f4ff83b48b3885e5a64aaa6f Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Tue, 13 May 2025 19:58:38 -0600 Subject: [PATCH] musikr.fs: simplify streaming tree impl --- .../main/java/org/oxycblt/musikr/Musikr.kt | 5 +++ .../org/oxycblt/musikr/covers/fs/FSCovers.kt | 2 +- .../org/oxycblt/musikr/fs/device/DeviceFS.kt | 27 +++++++++++----- .../musikr/fs/device/DeviceFSEntries.kt | 17 +++++++--- .../oxycblt/musikr/fs/device/FiniteHotFlow.kt | 31 ------------------- 5 files changed, 38 insertions(+), 44 deletions(-) delete mode 100644 musikr/src/main/java/org/oxycblt/musikr/fs/device/FiniteHotFlow.kt diff --git a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt index 27b84f1f2..29e5ff67f 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt @@ -19,6 +19,7 @@ package org.oxycblt.musikr import android.content.Context +import android.util.Log import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.buffer @@ -119,6 +120,7 @@ private class MusikrImpl( locations: List, onProgress: suspend (IndexingProgress) -> Unit ) = coroutineScope { + val start = System.currentTimeMillis() var exploredCount = 0 var extractedCount = 0 val explored = @@ -134,6 +136,9 @@ private class MusikrImpl( .onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) } .onCompletion { onProgress(IndexingProgress.Indeterminate) } val library = evaluateStep.evaluate(extracted) + Log.d( + "Musikr", + "Loaded ${library.songs.size} songs in ${System.currentTimeMillis() - start}ms") LibraryResultImpl(storage, library) } } diff --git a/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt b/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt index e194cd9b1..480a58d08 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/covers/fs/FSCovers.kt @@ -101,7 +101,7 @@ class MutableFSCovers(private val context: Context) : MutableCovers { // query to finally finish to be able to have a complete list of siblings to search for. val parent = file.parent val bestCover = - parent.children.flow() + parent.children .filterIsInstance() .map { it to coverArtScore(it) } .toList() diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt index 6b99e245e..db8fcf383 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt @@ -29,14 +29,19 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.flatMapMerge import kotlinx.coroutines.flow.flattenMerge import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.shareIn +import kotlinx.coroutines.flow.takeWhile import org.oxycblt.musikr.fs.MusicLocation import org.oxycblt.musikr.fs.Path +import org.oxycblt.musikr.fs.device.FiniteHotFlow.HotObject internal interface DeviceFS { fun explore(locations: Flow): Flow @@ -77,8 +82,13 @@ private class DeviceDirectoryImpl( contentResolver: ContentResolver, withHidden: Boolean ) : DeviceDirectory { + private sealed interface HotObject { + data class More(val value: DeviceFSEntry) : HotObject + data object Done : HotObject + } + override val uri: Uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId) - override val children = FiniteHotFlow(coroutineScope) { emit -> + override val children = flow { contentResolver.useQuery( DocumentsContract.buildChildDocumentsUriUsingTree( rootUri, @@ -109,17 +119,17 @@ private class DeviceDirectoryImpl( val lastModified = cursor.getLong(lastModifiedIndex) if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) { - emit( + val dir = DeviceDirectoryImpl( rootUri, childId, path = newPath, - parent = this, + parent = this@DeviceDirectoryImpl, coroutineScope, contentResolver, withHidden ) - ) + emit(HotObject.More(dir)) } else { val size = cursor.getLong(sizeIndex) val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId) @@ -130,13 +140,16 @@ private class DeviceDirectoryImpl( path = newPath, size = size, modifiedMs = lastModified, - parent = this + parent = this@DeviceDirectoryImpl ) - emit(file) + emit(HotObject.More(file)) } } + emit(HotObject.Done) } - } + }.shareIn(coroutineScope, SharingStarted.Lazily, replay = Int.MAX_VALUE) + .takeWhile { it is HotObject.More } + .map { (it as HotObject.More).value } private companion object { private val PROJECTION = diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFSEntries.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFSEntries.kt index 57ba874e8..bb8c1160a 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFSEntries.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFSEntries.kt @@ -22,7 +22,10 @@ import android.net.Uri import kotlinx.coroutines.Deferred import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow +import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.flatMapMerge +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOf import org.oxycblt.musikr.fs.Path @@ -33,7 +36,7 @@ sealed interface DeviceFSEntry { interface DeviceDirectory : DeviceFSEntry { val parent: DeviceDirectory? - val children: FiniteHotFlow + val children: Flow } data class DeviceFile( @@ -46,9 +49,13 @@ data class DeviceFile( ) : DeviceFSEntry @OptIn(ExperimentalCoroutinesApi::class) -fun DeviceDirectory.flatten(): Flow = children.flow().flatMapMerge { - when (it) { - is DeviceDirectory -> it.flatten() - is DeviceFile -> flowOf(it) +fun DeviceDirectory.flatten(): Flow = flow { + val recursive = mutableListOf>() + children.collect { + when (it) { + is DeviceDirectory -> recursive.add(it.flatten()) + is DeviceFile -> emit(it) + } } + emitAll(recursive.asFlow().flatMapMerge { it }) } \ No newline at end of file diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/FiniteHotFlow.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/FiniteHotFlow.kt deleted file mode 100644 index c94e0ea2d..000000000 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/FiniteHotFlow.kt +++ /dev/null @@ -1,31 +0,0 @@ -package org.oxycblt.musikr.fs.device - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.FlowCollector -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.shareIn -import kotlinx.coroutines.flow.takeWhile - -class FiniteHotFlow(scope: CoroutineScope, emitter: suspend (emit: suspend (T) -> Unit) -> Unit) { - private sealed interface HotObject { - data class More(val value: T) : HotObject - data object Done : HotObject - } - - private val sharedFlow = flow { - emitter { - emit(HotObject.More(it)) - } - emit(HotObject.Done) - }.shareIn(scope, SharingStarted.Lazily, replay = Int.MAX_VALUE) - - @Suppress("UNCHECKED_CAST") - fun flow() = flow { - sharedFlow.takeWhile { it is HotObject.More<*> }.collect { - emit((it as HotObject.More<*>).value as T) - } - } -} \ No newline at end of file