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) { for (cluster in artistClusters.values) {
simplifyArtistCluster(cluster) simplifyArtistCluster(cluster)
} }
// second pass: cluster artists by mbid and identify invalid ones // second pass: check for identical uids and meld together vertices w/the same uid
// (i.e artists with the same mbid but different metadata) // to prevent broken artist objects
// this only applies to artists w/mbids, if we dont do this well meld all non-mbid val artistUidClusters = artistVertices.values.groupBy { it.preArtist.uid }
// artists into a single vertex which is incorrect for (cluster in artistUidClusters.values) {
val mbidClusters =
artistVertices.values.groupBy { it.preArtist.musicBrainzId }.filter { it.key != null }
for (cluster in mbidClusters.values) {
// everything in the cluster must have the same pre-artist // everything in the cluster must have the same pre-artist
val canon = cluster.maxBy { it.songVertices.size }.preArtist val canon = cluster.maxBy { it.songVertices.size }.preArtist
val same = cluster.all { it.preArtist == canon } val same = cluster.all { it.preArtist == canon }
if (!same) { if (!same) {
// invalid mbid setup, collapse verts to one canon vert // invalid mbid setup, strip mbids from all artists in the cluster
val simpleMbidVertex = artistVertices.getOrPut(canon) { ArtistVertex(canon) } val noMbidPreArtist = canon
val simpleMbidVertex =
artistVertices.getOrPut(noMbidPreArtist) { ArtistVertex(noMbidPreArtist) }
for (artist in cluster) { for (artist in cluster) {
meldArtistVertices(artist, simpleMbidVertex) meldArtistVertices(artist, simpleMbidVertex)
} }
@@ -277,20 +276,18 @@ private class MusicGraphBuilderImpl : MusicGraph.Builder {
for (cluster in albumClusters.values) { for (cluster in albumClusters.values) {
simplifyAlbumCluster(cluster) simplifyAlbumCluster(cluster)
} }
// second pass: cluster albums by mbid and identify invalid ones // second pass: check for identical uids and meld together vertices w/the same uid
// (i.e albums with the same mbid but different metadata) // to prevent broken album objects
// this only applies to albums w/mbids, if we dont do this well meld all non-mbid val uidClusters = albumVertices.values.groupBy { it.preAlbum.uid }
// albums into a single vertex which is incorrect for (cluster in uidClusters.values) {
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
val canon = cluster.maxBy { it.songVertices.size }.preAlbum val canon = cluster.maxBy { it.songVertices.size }.preAlbum
val same = cluster.all { it.preAlbum == canon } val same = cluster.all { it.preAlbum == canon }
if (!same) { if (!same) {
// invalid mbid setup, collapse verts to one canon vert val noMbidPreAlbum = canon
val simpleMbidVertex = val simpleMbidVertex =
albumVertices.getOrPut(canon) { AlbumVertex(canon, mutableListOf()) } albumVertices.getOrPut(noMbidPreAlbum) {
AlbumVertex(noMbidPreAlbum, mutableListOf())
}
for (album in cluster) { for (album in cluster) {
meldAlbumVertices(album, simpleMbidVertex) meldAlbumVertices(album, simpleMbidVertex)
} }
@@ -20,12 +20,10 @@ package org.oxycblt.musikr.model
import org.oxycblt.musikr.Album import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song import org.oxycblt.musikr.Song
import org.oxycblt.musikr.covers.CoverCollection import org.oxycblt.musikr.covers.CoverCollection
import org.oxycblt.musikr.tag.Date import org.oxycblt.musikr.tag.Date
import org.oxycblt.musikr.tag.interpret.PreAlbum import org.oxycblt.musikr.tag.interpret.PreAlbum
import org.oxycblt.musikr.util.update
internal interface AlbumCore { internal interface AlbumCore {
val preAlbum: PreAlbum val preAlbum: PreAlbum
@@ -42,16 +40,7 @@ internal interface AlbumCore {
class AlbumImpl internal constructor(private val core: AlbumCore) : Album { class AlbumImpl internal constructor(private val core: AlbumCore) : Album {
private val preAlbum = core.preAlbum private val preAlbum = core.preAlbum
override val uid = override val uid = preAlbum.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 name = preAlbum.name override val name = preAlbum.name
override val releaseType = preAlbum.releaseType override val releaseType = preAlbum.releaseType
override val durationMs = core.songs.sumOf { it.durationMs } override val durationMs = core.songs.sumOf { it.durationMs }
@@ -18,15 +18,12 @@
package org.oxycblt.musikr.model package org.oxycblt.musikr.model
import android.util.Log
import org.oxycblt.musikr.Album import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song import org.oxycblt.musikr.Song
import org.oxycblt.musikr.covers.CoverCollection import org.oxycblt.musikr.covers.CoverCollection
import org.oxycblt.musikr.tag.interpret.PreArtist import org.oxycblt.musikr.tag.interpret.PreArtist
import org.oxycblt.musikr.util.update
internal interface ArtistCore { internal interface ArtistCore {
val preArtist: PreArtist val preArtist: PreArtist
@@ -42,12 +39,7 @@ internal interface ArtistCore {
* @author Alexander Capehart (OxygenCobalt) * @author Alexander Capehart (OxygenCobalt)
*/ */
internal class ArtistImpl(private val core: ArtistCore) : Artist { internal class ArtistImpl(private val core: ArtistCore) : Artist {
override val uid = override val uid = core.preArtist.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 name = core.preArtist.name override val name = core.preArtist.name
override val songs = core.songs 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.Name
import org.oxycblt.musikr.tag.ReleaseType import org.oxycblt.musikr.tag.ReleaseType
import org.oxycblt.musikr.tag.ReplayGainAdjustment import org.oxycblt.musikr.tag.ReplayGainAdjustment
import org.oxycblt.musikr.util.update
internal data class PreSong( internal data class PreSong(
val v363Uid: Music.UID, val v363Uid: Music.UID,
@@ -62,7 +63,18 @@ internal data class PreAlbum(
val rawName: String?, val rawName: String?,
val releaseType: ReleaseType, val releaseType: ReleaseType,
val preArtists: PreArtistsFrom, 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 { internal sealed interface PreArtistsFrom {
val preArtists: List<PreArtist> val preArtists: List<PreArtist>
@@ -72,7 +84,12 @@ internal sealed interface PreArtistsFrom {
data class Album(override val preArtists: List<PreArtist>) : 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( internal data class PreGenre(
val name: Name, val name: Name,