musikr.fs: optimize file tree exploration
This commit is contained in:
@@ -128,7 +128,9 @@ private class MusikrImpl(
|
|||||||
.explore(locations)
|
.explore(locations)
|
||||||
.buffer(Channel.UNLIMITED)
|
.buffer(Channel.UNLIMITED)
|
||||||
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
||||||
.onEach { onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount)) }
|
.onEach {
|
||||||
|
onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount))
|
||||||
|
}
|
||||||
val extracted =
|
val extracted =
|
||||||
extractStep
|
extractStep
|
||||||
.extract(explored)
|
.extract(explored)
|
||||||
|
|||||||
@@ -22,14 +22,20 @@ import android.content.ContentResolver
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
|
import android.util.Log
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.asFlow
|
import kotlinx.coroutines.flow.asFlow
|
||||||
|
import kotlinx.coroutines.flow.emitAll
|
||||||
import kotlinx.coroutines.flow.emptyFlow
|
import kotlinx.coroutines.flow.emptyFlow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.mapNotNull
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
import kotlinx.coroutines.flow.merge
|
import kotlinx.coroutines.flow.merge
|
||||||
@@ -48,11 +54,11 @@ internal interface DeviceFS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
|
||||||
private class DeviceFSImpl(
|
private class DeviceFSImpl(
|
||||||
private val contentResolver: ContentResolver,
|
private val contentResolver: ContentResolver,
|
||||||
private val withHidden: Boolean
|
private val withHidden: Boolean
|
||||||
) : DeviceFS {
|
) : DeviceFS {
|
||||||
|
private val explorationScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||||
override fun explore(locations: Flow<MusicLocation>, fileTree: FileTree) =
|
override fun explore(locations: Flow<MusicLocation>, fileTree: FileTree) =
|
||||||
locations.mapNotNull { location ->
|
locations.mapNotNull { location ->
|
||||||
queryRoot(location, fileTree)
|
queryRoot(location, fileTree)
|
||||||
@@ -113,11 +119,12 @@ private class DeviceFSImpl(
|
|||||||
children = emptyFlow()
|
children = emptyFlow()
|
||||||
)
|
)
|
||||||
dir.children = flow {
|
dir.children = flow {
|
||||||
|
Log.d("DeviceFS", "Querying $uri")
|
||||||
contentResolver.useQuery(
|
contentResolver.useQuery(
|
||||||
DocumentsContract.buildChildDocumentsUriUsingTree(
|
DocumentsContract.buildChildDocumentsUriUsingTree(
|
||||||
rootUri,
|
rootUri,
|
||||||
treeDocumentId
|
treeDocumentId,
|
||||||
), PROJECTION
|
), projection = PROJECTION, sortOrder = "${DocumentsContract.Document.COLUMN_DISPLAY_NAME} DESC"
|
||||||
) { cursor ->
|
) { cursor ->
|
||||||
val childUriIndex =
|
val childUriIndex =
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||||
@@ -132,6 +139,8 @@ private class DeviceFSImpl(
|
|||||||
val childSubdirUris = mutableListOf<String>()
|
val childSubdirUris = mutableListOf<String>()
|
||||||
val childFileUris = mutableListOf<String>()
|
val childFileUris = mutableListOf<String>()
|
||||||
|
|
||||||
|
val subqueries = mutableListOf<DeviceDirectory>()
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
val childId = cursor.getString(childUriIndex)
|
val childId = cursor.getString(childUriIndex)
|
||||||
val displayName = cursor.getString(displayNameIndex)
|
val displayName = cursor.getString(displayNameIndex)
|
||||||
@@ -158,6 +167,7 @@ private class DeviceFSImpl(
|
|||||||
childSubdirUris.add(subdir.uri.toString())
|
childSubdirUris.add(subdir.uri.toString())
|
||||||
emit(StreamedFile.More(subdir))
|
emit(StreamedFile.More(subdir))
|
||||||
} else {
|
} else {
|
||||||
|
Log.d("DeviceFS", "Querying file $childId")
|
||||||
val size = cursor.getLong(sizeIndex)
|
val size = cursor.getLong(sizeIndex)
|
||||||
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
|
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
|
||||||
val file =
|
val file =
|
||||||
@@ -191,18 +201,18 @@ private class DeviceFSImpl(
|
|||||||
fileTree.updateDirectory(uri, writeDir)
|
fileTree.updateDirectory(uri, writeDir)
|
||||||
emit(StreamedFile.Done)
|
emit(StreamedFile.Done)
|
||||||
}
|
}
|
||||||
}.shareIn(this, SharingStarted.Eagerly, replay = Int.MAX_VALUE)
|
}.shareIn(explorationScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
|
||||||
.takeWhile { it is StreamedFile.More }
|
.takeWhile { it is StreamedFile.More }
|
||||||
.map { (it as StreamedFile.More).value }
|
.map { (it as StreamedFile.More).value }
|
||||||
dir
|
dir
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hydrateCached(
|
private suspend fun hydrateCached(
|
||||||
cached: CachedDirectory,
|
cached: CachedDirectory,
|
||||||
parentDir: DeviceDirectory?,
|
parentDir: DeviceDirectory?,
|
||||||
path: Path,
|
path: Path,
|
||||||
fileTree: FileTree
|
fileTree: FileTree
|
||||||
): DeviceDirectory {
|
): DeviceDirectory = coroutineScope {
|
||||||
val dir = DeviceDirectoryImpl(
|
val dir = DeviceDirectoryImpl(
|
||||||
uri = cached.uri.toUri(),
|
uri = cached.uri.toUri(),
|
||||||
path = path,
|
path = path,
|
||||||
@@ -210,27 +220,36 @@ private class DeviceFSImpl(
|
|||||||
parent = parentDir,
|
parent = parentDir,
|
||||||
children = emptyFlow()
|
children = emptyFlow()
|
||||||
)
|
)
|
||||||
dir.children = merge(
|
dir.children = flow {
|
||||||
cached.subdirUris.asFlow().map { subdirUriString ->
|
emitAll(
|
||||||
val subdirUri = subdirUriString.toUri()
|
merge(
|
||||||
val cachedSubdir = requireNotNull(fileTree.queryDirectory(subdirUri)) {
|
cached.subdirUris.asFlow().map { subdirUriString ->
|
||||||
"No cached subdir for $subdirUri, malformed cache! Rescan needed."
|
val subdirUri = subdirUriString.toUri()
|
||||||
}
|
val cachedSubdir = requireNotNull(fileTree.queryDirectory(subdirUri)) {
|
||||||
hydrateCached(cachedSubdir, dir, dir.path.file(cachedSubdir.name), fileTree)
|
"No cached subdir for $subdirUri, malformed cache! Rescan needed."
|
||||||
},
|
}
|
||||||
cached.fileUris.asFlow().map {
|
hydrateCached(cachedSubdir, dir, dir.path.file(cachedSubdir.name), fileTree)
|
||||||
val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))
|
},
|
||||||
DeviceFile(
|
cached.fileUris.asFlow().map {
|
||||||
uri = it.toUri(),
|
val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))
|
||||||
path = dir.path.file(cachedFile.name),
|
DeviceFile(
|
||||||
modifiedMs = cachedFile.modifiedMs,
|
uri = it.toUri(),
|
||||||
mimeType = cachedFile.mimeType,
|
path = dir.path.file(cachedFile.name),
|
||||||
size = cachedFile.size,
|
modifiedMs = cachedFile.modifiedMs,
|
||||||
parent = dir
|
mimeType = cachedFile.mimeType,
|
||||||
)
|
size = cachedFile.size,
|
||||||
}
|
parent = dir
|
||||||
)
|
)
|
||||||
return dir
|
}
|
||||||
|
).map { StreamedFile.More(it) }
|
||||||
|
)
|
||||||
|
emit(StreamedFile.Done)
|
||||||
|
}
|
||||||
|
.flowOn(Dispatchers.IO)
|
||||||
|
.shareIn(explorationScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
|
||||||
|
.takeWhile { it is StreamedFile.More }
|
||||||
|
.map { (it as StreamedFile.More).value }
|
||||||
|
dir
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed interface StreamedFile {
|
private sealed interface StreamedFile {
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import kotlinx.coroutines.flow.emitAll
|
|||||||
import kotlinx.coroutines.flow.flatMapMerge
|
import kotlinx.coroutines.flow.flatMapMerge
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
|
import kotlinx.coroutines.flow.transform
|
||||||
import org.oxycblt.musikr.fs.Path
|
import org.oxycblt.musikr.fs.Path
|
||||||
|
|
||||||
sealed interface DeviceFSEntry {
|
sealed interface DeviceFSEntry {
|
||||||
@@ -49,14 +50,9 @@ data class DeviceFile(
|
|||||||
val parent: DeviceDirectory
|
val parent: DeviceDirectory
|
||||||
) : DeviceFSEntry
|
) : DeviceFSEntry
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
fun DeviceDirectory.flatten(): Flow<DeviceFile> = children.transform {
|
||||||
fun DeviceDirectory.flatten(): Flow<DeviceFile> = flow {
|
|
||||||
val recursive = mutableListOf<Flow<DeviceFile>>()
|
|
||||||
children.collect {
|
|
||||||
when (it) {
|
when (it) {
|
||||||
is DeviceDirectory -> recursive.add(it.flatten())
|
is DeviceDirectory -> emitAll(it.flatten())
|
||||||
is DeviceFile -> emit(it)
|
is DeviceFile -> emit(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emitAll(recursive.asFlow().flatMapMerge { it })
|
|
||||||
}
|
|
||||||
@@ -46,8 +46,9 @@ internal fun ContentResolver.safeQuery(
|
|||||||
uri: Uri,
|
uri: Uri,
|
||||||
projection: Array<out String>,
|
projection: Array<out String>,
|
||||||
selector: String? = null,
|
selector: String? = null,
|
||||||
args: Array<String>? = null
|
args: Array<String>? = null,
|
||||||
) = requireNotNull(query(uri, projection, selector, args, null)) { "ContentResolver query failed" }
|
sortOrder: String? = null
|
||||||
|
) = requireNotNull(query(uri, projection, selector, args, sortOrder)) { "ContentResolver query failed" }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A shortcut for [safeQuery] with [use] applied, automatically cleaning up the [Cursor]'s resources
|
* A shortcut for [safeQuery] with [use] applied, automatically cleaning up the [Cursor]'s resources
|
||||||
@@ -68,5 +69,6 @@ internal inline fun <reified R> ContentResolver.useQuery(
|
|||||||
projection: Array<out String>,
|
projection: Array<out String>,
|
||||||
selector: String? = null,
|
selector: String? = null,
|
||||||
args: Array<String>? = null,
|
args: Array<String>? = null,
|
||||||
|
sortOrder: String? = null,
|
||||||
block: (Cursor) -> R
|
block: (Cursor) -> R
|
||||||
) = safeQuery(uri, projection, selector, args).use(block)
|
) = safeQuery(uri, projection, selector, args, sortOrder).use(block)
|
||||||
|
|||||||
Reference in New Issue
Block a user