all: reformat

This commit is contained in:
Alexander Capehart
2026-08-29 15:29:20 -06:00
parent d19cd9dc34
commit bf6c70192b
18 changed files with 220 additions and 216 deletions
@@ -41,10 +41,9 @@ class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
private val mappingLock = Mutex()
override suspend fun read(file: File): CacheResult {
val currentMapping =
mappingLock.withLock {
mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it }
}
val currentMapping = mappingLock.withLock {
mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it }
}
val dbSong = currentMapping[file.uri] ?: return CacheResult.Miss(file)
if (dbSong.modifiedMs != file.modifiedMs) {
return CacheResult.Stale(file, dbSong.addedMs)
@@ -148,8 +147,7 @@ private constructor(private val inner: DBCache, private val writeDao: CacheWrite
return MutableDBCache(DBCache.from(db), db.writeDao())
}
internal fun from(inner: DBCache, writeDao: CacheWriteDao) =
MutableDBCache(inner, writeDao)
internal fun from(inner: DBCache, writeDao: CacheWriteDao) = MutableDBCache(inner, writeDao)
private const val BATCH_SIZE = 500
}
@@ -381,14 +381,13 @@ private class MusicGraphBuilderImpl : MusicGraph.Builder {
return
}
// No full MBID coverage, discard the MBIDs from the graph.
val strippedCluster =
cluster.map {
val noMbidPreArtist = it.preArtist.copy(musicBrainzId = null)
val simpleMbidVertex =
artistVertices.getOrPut(noMbidPreArtist) { ArtistVertex(noMbidPreArtist) }
meldArtistVertices(it, simpleMbidVertex)
simpleMbidVertex
}
val strippedCluster = cluster.map {
val noMbidPreArtist = it.preArtist.copy(musicBrainzId = null)
val simpleMbidVertex =
artistVertices.getOrPut(noMbidPreArtist) { ArtistVertex(noMbidPreArtist) }
meldArtistVertices(it, simpleMbidVertex)
simpleMbidVertex
}
simplifyArtistClusterImpl(strippedCluster)
}
@@ -457,41 +456,41 @@ private class MusicGraphBuilderImpl : MusicGraph.Builder {
return
}
// No full MBID coverage, discard the MBIDs from the graph.
val strippedMbidCluster =
cluster.map {
val noMbidPreAlbum = it.preAlbum.copy(musicBrainzId = null)
val simpleMbidVertex =
albumVertices.getOrPut(noMbidPreAlbum) {
AlbumVertex(noMbidPreAlbum, it.artistVertices.toMutableList())
}
meldAlbumVertices(it, simpleMbidVertex)
simpleMbidVertex
}
val fullAlbumArtistCoverage =
strippedMbidCluster.all { it.preAlbum.preArtists is PreArtistsFrom.Album }
val strippedMbidCluster = cluster.map {
val noMbidPreAlbum = it.preAlbum.copy(musicBrainzId = null)
val simpleMbidVertex =
albumVertices.getOrPut(noMbidPreAlbum) {
AlbumVertex(noMbidPreAlbum, it.artistVertices.toMutableList())
}
meldAlbumVertices(it, simpleMbidVertex)
simpleMbidVertex
}
val fullAlbumArtistCoverage = strippedMbidCluster.all {
it.preAlbum.preArtists is PreArtistsFrom.Album
}
if (fullAlbumArtistCoverage) {
// All albums have album artists, we can reasonably cluster around artists
// rather than just name.
val albumArtistClusters =
strippedMbidCluster.groupBy { it.preAlbum.preArtists.preArtists }
val albumArtistClusters = strippedMbidCluster.groupBy {
it.preAlbum.preArtists.preArtists
}
for (albumArtistCluster in albumArtistClusters.values) {
simplifyAlbumClusterImpl(albumArtistCluster)
}
return
}
val strippedAlbumArtistCluster =
strippedMbidCluster.map {
val noAlbumArtistPreAlbum =
it.preAlbum.copy(
preArtists = PreArtistsFrom.Individual(it.preAlbum.preArtists.preArtists)
)
val simpleAlbumArtistVertex =
albumVertices.getOrPut(noAlbumArtistPreAlbum) {
AlbumVertex(noAlbumArtistPreAlbum, it.artistVertices.toMutableList())
}
meldAlbumVertices(it, simpleAlbumArtistVertex)
simpleAlbumArtistVertex
}
val strippedAlbumArtistCluster = strippedMbidCluster.map {
val noAlbumArtistPreAlbum =
it.preAlbum.copy(
preArtists = PreArtistsFrom.Individual(it.preAlbum.preArtists.preArtists)
)
val simpleAlbumArtistVertex =
albumVertices.getOrPut(noAlbumArtistPreAlbum) {
AlbumVertex(noAlbumArtistPreAlbum, it.artistVertices.toMutableList())
}
meldAlbumVertices(it, simpleAlbumArtistVertex)
simpleAlbumArtistVertex
}
simplifyAlbumClusterImpl(strippedAlbumArtistCluster)
}
@@ -32,7 +32,6 @@ import androidx.room.RoomDatabase
import androidx.room.Transaction
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.SQLiteConnection
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteQueryBuilder
import org.oxycblt.musikr.Music
@@ -60,23 +59,36 @@ internal abstract class PlaylistDatabase : RoomDatabase() {
)
.addMigrations(
object : Migration(30, 75) {
private fun washUids(db: SupportSQLiteDatabase, table: String, column: String) {
val playlistUidMigration = db.query(
SupportSQLiteQueryBuilder.builder(table)
.columns(arrayOf(column))
.create()
).use {
val migrations = mutableListOf<Pair<String, String>>()
val playlistUidIndex = it.getColumnIndexOrThrow(column)
while (it.moveToNext()) {
val oldUid =
requireNotNull(it.getStringOrNull(playlistUidIndex)) { "catastrophic migration failure: $table has no UID" }
val newUid =
requireNotNull(Music.UID.fromString(oldUid)) { "catastrophic migration failure: $table has bad UID" }.toString()
migrations.add(oldUid to newUid)
}
migrations
}
private fun washUids(
db: SupportSQLiteDatabase,
table: String,
column: String,
) {
val playlistUidMigration =
db.query(
SupportSQLiteQueryBuilder.builder(table)
.columns(arrayOf(column))
.create()
)
.use {
val migrations = mutableListOf<Pair<String, String>>()
val playlistUidIndex = it.getColumnIndexOrThrow(column)
while (it.moveToNext()) {
val oldUid =
requireNotNull(
it.getStringOrNull(playlistUidIndex)
) {
"catastrophic migration failure: $table has no UID"
}
val newUid =
requireNotNull(Music.UID.fromString(oldUid)) {
"catastrophic migration failure: $table has bad UID"
}
.toString()
migrations.add(oldUid to newUid)
}
migrations
}
for ((old, new) in playlistUidMigration) {
db.update(
table,
@@ -85,7 +97,7 @@ internal abstract class PlaylistDatabase : RoomDatabase() {
put(column, old)
},
"$column = ?",
arrayOf(new)
arrayOf(new),
)
}
for ((old, new) in playlistUidMigration) {
@@ -96,10 +108,11 @@ internal abstract class PlaylistDatabase : RoomDatabase() {
put(column, new)
},
"$column = ?",
arrayOf(old)
arrayOf(old),
)
}
}
override fun migrate(db: SupportSQLiteDatabase) {
washUids(db, "PlaylistInfo", "playlistUid")
washUids(db, "PlaylistSongCrossRef", "playlistUid")
@@ -116,8 +116,9 @@ private class M3UImpl(private val volumeManager: VolumeManager) : M3U() {
// based on the programs that generated it. I more or less have to consider any possible
// interpretation as valid.
val interpretations = interpretPath(unlikelyToBeNull(path))
val possibilities =
interpretations.flatMap { expandInterpretation(it, workingDirectory, volumes) }
val possibilities = interpretations.flatMap {
expandInterpretation(it, workingDirectory, volumes)
}
paths.add(possibilities)
}
@@ -156,12 +157,11 @@ private class M3UImpl(private val volumeManager: VolumeManager) : M3U() {
val relativeInterpretation =
Path(workingDirectory.volume, path.components.absoluteTo(workingDirectory.components))
val volumeExactMatch = volumes.find { it.components?.contains(path.components) == true }
val volumeInterpretation =
volumeExactMatch?.let {
val components =
unlikelyToBeNull(volumeExactMatch.components).containing(path.components)
Path(volumeExactMatch, components)
}
val volumeInterpretation = volumeExactMatch?.let {
val components =
unlikelyToBeNull(volumeExactMatch.components).containing(path.components)
Path(volumeExactMatch, components)
}
return if (path.likelyAbsolute) {
listOfNotNull(volumeInterpretation, absoluteInterpretation, relativeInterpretation)
} else {
@@ -47,11 +47,10 @@ internal fun String.parseSlashPositionField() =
*
* @see transformPositionField
*/
internal fun parseXiphPositionField(pos: String?, total: String?) =
pos?.let { posStr ->
posStr.toIntOrNull()?.let { transformPositionField(it, total?.toIntOrNull()) }
?: posStr.parseSlashPositionField()
}
internal fun parseXiphPositionField(pos: String?, total: String?) = pos?.let { posStr ->
posStr.toIntOrNull()?.let { transformPositionField(it, total?.toIntOrNull()) }
?: posStr.parseSlashPositionField()
}
/**
* Transform a raw position + total field into a position a way that tolerates placeholder values.