music: integrate filetreecache
This commit is contained in:
@@ -30,6 +30,7 @@ import kotlinx.coroutines.yield
|
|||||||
import org.oxycblt.auxio.image.covers.SettingCovers
|
import org.oxycblt.auxio.image.covers.SettingCovers
|
||||||
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
|
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
|
||||||
import org.oxycblt.auxio.music.shim.WriteOnlyMutableCache
|
import org.oxycblt.auxio.music.shim.WriteOnlyMutableCache
|
||||||
|
import org.oxycblt.auxio.music.shim.WriteOnlyFileTreeCache
|
||||||
import org.oxycblt.musikr.IndexingProgress
|
import org.oxycblt.musikr.IndexingProgress
|
||||||
import org.oxycblt.musikr.Interpretation
|
import org.oxycblt.musikr.Interpretation
|
||||||
import org.oxycblt.musikr.Library
|
import org.oxycblt.musikr.Library
|
||||||
@@ -39,6 +40,7 @@ import org.oxycblt.musikr.MutableLibrary
|
|||||||
import org.oxycblt.musikr.Playlist
|
import org.oxycblt.musikr.Playlist
|
||||||
import org.oxycblt.musikr.Song
|
import org.oxycblt.musikr.Song
|
||||||
import org.oxycblt.musikr.Storage
|
import org.oxycblt.musikr.Storage
|
||||||
|
import org.oxycblt.musikr.fs.device.FileTreeCache
|
||||||
import org.oxycblt.musikr.cache.MutableCache
|
import org.oxycblt.musikr.cache.MutableCache
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||||
import org.oxycblt.musikr.tag.interpret.Naming
|
import org.oxycblt.musikr.tag.interpret.Naming
|
||||||
@@ -391,7 +393,8 @@ constructor(
|
|||||||
val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID()
|
val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID()
|
||||||
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
|
val cache = if (withCache) cache else WriteOnlyMutableCache(cache)
|
||||||
val covers = settingCovers.mutate(context, newRevision)
|
val covers = settingCovers.mutate(context, newRevision)
|
||||||
val storage = Storage(cache, covers, storedPlaylists)
|
val fileTreeCache = if (withCache) FileTreeCache.from(context) else WriteOnlyFileTreeCache(FileTreeCache.from(context))
|
||||||
|
val storage = Storage(cache, covers, storedPlaylists, fileTreeCache)
|
||||||
val interpretation = Interpretation(nameFactory, separators, withHidden)
|
val interpretation = Interpretation(nameFactory, separators, withHidden)
|
||||||
val result =
|
val result =
|
||||||
Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress)
|
Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress)
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Auxio Project
|
||||||
|
* WriteOnlyFileTreeCache.kt is part of Auxio.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.oxycblt.auxio.music.shim
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import org.oxycblt.musikr.fs.device.CachedDirectory
|
||||||
|
import org.oxycblt.musikr.fs.device.CachedFile
|
||||||
|
import org.oxycblt.musikr.fs.device.FileTree
|
||||||
|
import org.oxycblt.musikr.fs.device.FileTreeCache
|
||||||
|
|
||||||
|
class WriteOnlyFileTreeCache(private val inner: FileTreeCache) : FileTreeCache {
|
||||||
|
override fun read(): FileTree {
|
||||||
|
val innerTree = inner.read()
|
||||||
|
return WriteOnlyFileTree(innerTree)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class WriteOnlyFileTree(private val innerTree: FileTree) : FileTree {
|
||||||
|
override suspend fun queryDirectory(uri: Uri): CachedDirectory? {
|
||||||
|
// Always return null to simulate a cache miss
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun updateDirectory(uri: Uri, directory: CachedDirectory) {
|
||||||
|
// Forward update operations to the inner tree
|
||||||
|
innerTree.updateDirectory(uri, directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun queryFile(uri: Uri): CachedFile? {
|
||||||
|
// Always return null to simulate a cache miss
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun updateFile(uri: Uri, file: CachedFile) {
|
||||||
|
// Forward update operations to the inner tree
|
||||||
|
innerTree.updateFile(uri, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun write() {
|
||||||
|
// Forward write operations to the inner tree
|
||||||
|
innerTree.write()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ package org.oxycblt.musikr
|
|||||||
import org.oxycblt.musikr.cache.MutableCache
|
import org.oxycblt.musikr.cache.MutableCache
|
||||||
import org.oxycblt.musikr.covers.Cover
|
import org.oxycblt.musikr.covers.Cover
|
||||||
import org.oxycblt.musikr.covers.MutableCovers
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
|
import org.oxycblt.musikr.fs.device.FileTreeCache
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||||
import org.oxycblt.musikr.tag.interpret.Naming
|
import org.oxycblt.musikr.tag.interpret.Naming
|
||||||
import org.oxycblt.musikr.tag.interpret.Separators
|
import org.oxycblt.musikr.tag.interpret.Separators
|
||||||
@@ -45,7 +46,12 @@ data class Storage(
|
|||||||
* be used during music loading and mutated when creating, renaming, or deleting playlists in
|
* be used during music loading and mutated when creating, renaming, or deleting playlists in
|
||||||
* the library.
|
* the library.
|
||||||
*/
|
*/
|
||||||
val storedPlaylists: StoredPlaylists
|
val storedPlaylists: StoredPlaylists,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A cache for file tree information to optimize file system exploration.
|
||||||
|
*/
|
||||||
|
val fileTreeCache: FileTreeCache
|
||||||
)
|
)
|
||||||
|
|
||||||
/** Configuration for how to interpret and extrapolate certain audio tags. */
|
/** Configuration for how to interpret and extrapolate certain audio tags. */
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ import kotlinx.serialization.json.Json
|
|||||||
|
|
||||||
interface FileTreeCache {
|
interface FileTreeCache {
|
||||||
fun read(): FileTree
|
fun read(): FileTree
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(context: Context): FileTreeCache = FileTreeCacheImpl(context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FileTree {
|
interface FileTree {
|
||||||
@@ -47,7 +51,7 @@ interface FileTree {
|
|||||||
|
|
||||||
// Define the sealed interface
|
// Define the sealed interface
|
||||||
@Serializable
|
@Serializable
|
||||||
sealed interface FileSystemEntry {
|
private sealed interface FileSystemEntry {
|
||||||
val uri: String
|
val uri: String
|
||||||
val modifiedMs: Long
|
val modifiedMs: Long
|
||||||
}
|
}
|
||||||
@@ -71,14 +75,13 @@ data class CachedFile(
|
|||||||
) : FileSystemEntry
|
) : FileSystemEntry
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class FileTreeData(
|
private data class FileTreeData(
|
||||||
val directories: Map<String, CachedDirectory> = mapOf(),
|
val directories: Map<String, CachedDirectory> = mapOf(),
|
||||||
val files: Map<String, CachedFile> = mapOf()
|
val files: Map<String, CachedFile> = mapOf()
|
||||||
)
|
)
|
||||||
|
|
||||||
class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
private class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "FileTreeCache"
|
|
||||||
private const val CACHE_FILENAME = "file_tree_cache.json"
|
private const val CACHE_FILENAME = "file_tree_cache.json"
|
||||||
private val json = Json {
|
private val json = Json {
|
||||||
ignoreUnknownKeys = true
|
ignoreUnknownKeys = true
|
||||||
@@ -98,17 +101,15 @@ class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
|||||||
fileTreeCache.directories.toMutableMap(),
|
fileTreeCache.directories.toMutableMap(),
|
||||||
fileTreeCache.files.toMutableMap())
|
fileTreeCache.files.toMutableMap())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Failed to read cache file", e)
|
|
||||||
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.i(TAG, "Cache file does not exist, creating new FileTree")
|
|
||||||
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FileTreeImpl(
|
private class FileTreeImpl(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val mutableDirectories: MutableMap<String, CachedDirectory> = mutableMapOf(),
|
private val mutableDirectories: MutableMap<String, CachedDirectory> = mutableMapOf(),
|
||||||
private val mutableFiles: MutableMap<String, CachedFile> = mutableMapOf()
|
private val mutableFiles: MutableMap<String, CachedFile> = mutableMapOf()
|
||||||
@@ -124,26 +125,22 @@ class FileTreeImpl(
|
|||||||
|
|
||||||
// Directory operations
|
// Directory operations
|
||||||
override suspend fun queryDirectory(uri: Uri): CachedDirectory? {
|
override suspend fun queryDirectory(uri: Uri): CachedDirectory? {
|
||||||
Log.d("FileTreeCache", "Querying directory: $uri ${mutableDirectories[uri.toString()]}")
|
|
||||||
val uriString = uri.toString()
|
val uriString = uri.toString()
|
||||||
return mutableDirectories[uriString]
|
return mutableDirectories[uriString]
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updateDirectory(uri: Uri, directory: CachedDirectory) {
|
override suspend fun updateDirectory(uri: Uri, directory: CachedDirectory) {
|
||||||
Log.d("FileTreeCache", "Updating directory: $uri")
|
|
||||||
val uriString = uri.toString()
|
val uriString = uri.toString()
|
||||||
mutableDirectories[uriString] = directory
|
mutableDirectories[uriString] = directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// File operations
|
// File operations
|
||||||
override suspend fun queryFile(uri: Uri): CachedFile? {
|
override suspend fun queryFile(uri: Uri): CachedFile? {
|
||||||
Log.d("FileTreeCache", "Querying file: $uri")
|
|
||||||
val uriString = uri.toString()
|
val uriString = uri.toString()
|
||||||
return mutableFiles[uriString]
|
return mutableFiles[uriString]
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updateFile(uri: Uri, file: CachedFile) {
|
override suspend fun updateFile(uri: Uri, file: CachedFile) {
|
||||||
Log.d("FileTreeCache", "Updating file: $uri")
|
|
||||||
val uriString = uri.toString()
|
val uriString = uri.toString()
|
||||||
mutableFiles[uriString] = file
|
mutableFiles[uriString] = file
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ import org.oxycblt.musikr.covers.Covers
|
|||||||
import org.oxycblt.musikr.fs.MusicLocation
|
import org.oxycblt.musikr.fs.MusicLocation
|
||||||
import org.oxycblt.musikr.fs.device.DeviceFS
|
import org.oxycblt.musikr.fs.device.DeviceFS
|
||||||
import org.oxycblt.musikr.fs.device.FileTreeCache
|
import org.oxycblt.musikr.fs.device.FileTreeCache
|
||||||
import org.oxycblt.musikr.fs.device.FileTreeCacheImpl
|
|
||||||
import org.oxycblt.musikr.fs.device.flatten
|
import org.oxycblt.musikr.fs.device.flatten
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||||
@@ -58,7 +57,7 @@ internal interface ExploreStep {
|
|||||||
DeviceFS.from(context, interpretation.withHidden),
|
DeviceFS.from(context, interpretation.withHidden),
|
||||||
storage.cache,
|
storage.cache,
|
||||||
storage.covers,
|
storage.covers,
|
||||||
FileTreeCacheImpl(context),
|
storage.fileTreeCache,
|
||||||
storage.storedPlaylists)
|
storage.storedPlaylists)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user