music: generalize mbid simplification to uid

Applies to even non-mbid libraries.
This commit is contained in:
Alexander Capehart
2025-09-26 20:43:30 -06:00
parent 5acc4eefdd
commit e27247e2f0
4 changed files with 37 additions and 42 deletions
@@ -253,19 +253,18 @@ private class MusicGraphBuilderImpl : MusicGraph.Builder {
for (cluster in artistClusters.values) {
simplifyArtistCluster(cluster)
}
// second pass: cluster artists by mbid and identify invalid ones
// (i.e artists with the same mbid but different metadata)
// this only applies to artists w/mbids, if we dont do this well meld all non-mbid
// artists into a single vertex which is incorrect
val mbidClusters =
artistVertices.values.groupBy { it.preArtist.musicBrainzId }.filter { it.key != null }
for (cluster in mbidClusters.values) {
// second pass: check for identical uids and meld together vertices w/the same uid
// to prevent broken artist objects
val artistUidClusters = artistVertices.values.groupBy { it.preArtist.uid }
for (cluster in artistUidClusters.values) {
// everything in the cluster must have the same pre-artist
val canon = cluster.maxBy { it.songVertices.size }.preArtist
val same = cluster.all { it.preArtist == canon }
if (!same) {
// invalid mbid setup, collapse verts to one canon vert
val simpleMbidVertex = artistVertices.getOrPut(canon) { ArtistVertex(canon) }
// invalid mbid setup, strip mbids from all artists in the cluster
val noMbidPreArtist = canon
val simpleMbidVertex =
artistVertices.getOrPut(noMbidPreArtist) { ArtistVertex(noMbidPreArtist) }
for (artist in cluster) {
meldArtistVertices(artist, simpleMbidVertex)
}
@@ -277,20 +276,18 @@ private class MusicGraphBuilderImpl : MusicGraph.Builder {
for (cluster in albumClusters.values) {
simplifyAlbumCluster(cluster)
}
// second pass: cluster albums by mbid and identify invalid ones
// (i.e albums with the same mbid but different metadata)
// this only applies to albums w/mbids, if we dont do this well meld all non-mbid
// albums into a single vertex which is incorrect
val mbidAlbumClusters =
albumVertices.values.groupBy { it.preAlbum.musicBrainzId }.filter { it.key != null }
for (cluster in mbidAlbumClusters.values) {
// everything in the cluster must have the same pre-album
// second pass: check for identical uids and meld together vertices w/the same uid
// to prevent broken album objects
val uidClusters = albumVertices.values.groupBy { it.preAlbum.uid }
for (cluster in uidClusters.values) {
val canon = cluster.maxBy { it.songVertices.size }.preAlbum
val same = cluster.all { it.preAlbum == canon }
if (!same) {
// invalid mbid setup, collapse verts to one canon vert
val noMbidPreAlbum = canon
val simpleMbidVertex =
albumVertices.getOrPut(canon) { AlbumVertex(canon, mutableListOf()) }
albumVertices.getOrPut(noMbidPreAlbum) {
AlbumVertex(noMbidPreAlbum, mutableListOf())
}
for (album in cluster) {
meldAlbumVertices(album, simpleMbidVertex)
}
@@ -20,12 +20,10 @@ package org.oxycblt.musikr.model
import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.covers.CoverCollection
import org.oxycblt.musikr.tag.Date
import org.oxycblt.musikr.tag.interpret.PreAlbum
import org.oxycblt.musikr.util.update
internal interface AlbumCore {
val preAlbum: PreAlbum
@@ -42,16 +40,7 @@ internal interface AlbumCore {
class AlbumImpl internal constructor(private val core: AlbumCore) : Album {
private val preAlbum = core.preAlbum
override val uid =
// Attempt to use a MusicBrainz ID first before falling back to a hashed UID.
preAlbum.musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.ALBUM, it) }
?: Music.UID.auxio(Music.UID.Item.ALBUM) {
// Hash based on only names despite the presence of a date to increase stability.
// I don't know if there is any situation where an artist will have two albums with
// the exact same name, but if there is, I would love to know.
update(preAlbum.rawName)
update(preAlbum.preArtists.preArtists.mapNotNull { it.rawName })
}
override val uid = preAlbum.uid
override val name = preAlbum.name
override val releaseType = preAlbum.releaseType
override val durationMs = core.songs.sumOf { it.durationMs }
@@ -18,15 +18,12 @@
package org.oxycblt.musikr.model
import android.util.Log
import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.covers.CoverCollection
import org.oxycblt.musikr.tag.interpret.PreArtist
import org.oxycblt.musikr.util.update
internal interface ArtistCore {
val preArtist: PreArtist
@@ -42,12 +39,7 @@ internal interface ArtistCore {
* @author Alexander Capehart (OxygenCobalt)
*/
internal class ArtistImpl(private val core: ArtistCore) : Artist {
override val uid =
// Attempt to use a MusicBrainz ID first before falling back to a hashed UID.
core.preArtist.musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.ARTIST, it) }
?: Music.UID.auxio(Music.UID.Item.ARTIST) {
update(core.preArtist.rawName.also { Log.d("ArtistImpl", it.toString()) })
}
override val uid = core.preArtist.uid
override val name = core.preArtist.name
override val songs = core.songs
@@ -29,6 +29,7 @@ import org.oxycblt.musikr.tag.Disc
import org.oxycblt.musikr.tag.Name
import org.oxycblt.musikr.tag.ReleaseType
import org.oxycblt.musikr.tag.ReplayGainAdjustment
import org.oxycblt.musikr.util.update
internal data class PreSong(
val v363Uid: Music.UID,
@@ -62,7 +63,18 @@ internal data class PreAlbum(
val rawName: String?,
val releaseType: ReleaseType,
val preArtists: PreArtistsFrom,
)
) {
val uid =
// Attempt to use a MusicBrainz ID first before falling back to a hashed UID.
musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.ALBUM, it) }
?: Music.UID.auxio(Music.UID.Item.ALBUM) {
// Hash based on only names despite the presence of a date to increase stability.
// I don't know if there is any situation where an artist will have two albums with
// the exact same name, but if there is, I would love to know.
update(rawName)
update(preArtists.preArtists.mapNotNull { it.rawName })
}
}
internal sealed interface PreArtistsFrom {
val preArtists: List<PreArtist>
@@ -72,7 +84,12 @@ internal sealed interface PreArtistsFrom {
data class Album(override val preArtists: List<PreArtist>) : PreArtistsFrom
}
internal data class PreArtist(val musicBrainzId: UUID?, val name: Name, val rawName: String?)
internal data class PreArtist(val musicBrainzId: UUID?, val name: Name, val rawName: String?) {
val uid =
// Attempt to use a MusicBrainz ID first before falling back to a hashed UID.
musicBrainzId?.let { Music.UID.musicBrainz(Music.UID.Item.ARTIST, it) }
?: Music.UID.auxio(Music.UID.Item.ARTIST) { update(rawName) }
}
internal data class PreGenre(
val name: Name,