jukebox
All repositories: gitoria
4.7 KB
// lib/catalog.hl — THE DERIVED TABLES (albums, artists): built from the songs table after a scan that changed anything.// An album is one albumKey (album artist + album name without the CD marker; not the folder); a song with copies counts once; an artist is one artistKey. Both get a stable primary// key (a digest of the key), so an id from the API stays valid across rebuilds. All writes to those two tables are here.import { textDigest } from 'hl:fs'import { songsTable, albumsTable, artistsTable, countOfRows } from './db.hl'import { fold, safeKey, SEP } from './util.hl'import { albumKeyOf, sortKeys } from './albums.hl'import { showOf } from './dups.hl'import { dropOrderCache } from './state.hl'static variousName = 'Various Artists'// an album name that says nothing ("Unknown album", "Unbekannt", "Unbekannter Titel"): songs with it are never taken for one compilationstatic unknownAlbum = (name) => {f = fold(name)return f == '' || f.startsWith('unknown') || f.startsWith('unbekannt')}// A COMPILATION has no album artist tag, so each of its songs would be an album of its own. When three or more different artists share an album name in one folder// (and none of them has a real album artist), those songs get the album artist "Various Artists" and one album key. Their artist key stays their own, so an artist's page// still lists the songs. Idempotent; only songs whose keys change are written. Answers how many were changed.static fixCompilations = () => {groups = {}for (s of songsTable.iterate(null, null)) {if (unknownAlbum(s.album)) { continue }if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }k = fold(s.album) + SEP + s.folderlet g = groups[k]if (g == null) { g = {} }g[fold(s.artist)] = truegroups[k] = g}let changed = 0rows = []for (s of songsTable.iterate(null, null)) { rows.push(s) }for (s of rows) {if (unknownAlbum(s.album)) { continue }if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }g = groups[fold(s.album) + SEP + s.folder]let many = 0for (a in g) { many = many + 1 }want = many >= 3 ? variousName : s.artistif (want == s.albumArtist) { continue }keys = sortKeys(want, s.album, s.disc, s.track == null ? 0 : s.track, s.title)rec = {}for (f in s) {if (f != 'id') { rec[f] = s[f] }}rec.albumArtist = wantrec.albumKey = albumKeyOf(want, s.album)rec.sortArtist = keys.sortArtistrec.sortAlbum = keys.sortAlbumsongsTable.update(s.id, rec)changed = changed + 1}return changed}static idOf = (key) => { return textDigest(key).slice(0, 16) }// Rebuild both tables from scratch (one pass over the songs, then the old rows go and the new come). Answers// { albums, artists } counts.static rebuildCatalog = () => {fixCompilations()albums = {}artists = {}dropOrderCache()for (s of songsTable.iterate(null, null)) {w = showOf(s.id, s.playCount)if (!w.shown) { continue }let a = albums[s.albumKey]if (a == null) {a = { id = idOf(s.albumKey) key = s.albumKey name = s.album albumArtist = s.albumArtist artistKey = s.albumArtist == variousName ? safeKey(fold(variousName)) : s.artistKey folder = s.folderyear = s.year songCount = 0 duration = 0 discs = 1 playCount = 0 coverId = null sortName = safeKey(fold(s.album) + '|' + s.artistKey) }}a.songCount = a.songCount + 1if (s.disc != null && s.disc > a.discs) { a.discs = s.disc }a.duration = a.duration + (s.duration == null ? 0 : s.duration)a.playCount = a.playCount + w.playsif (a.coverId == null && s.coverId != null) { a.coverId = s.coverId }if (a.year == null && s.year != null) { a.year = s.year }albums[s.albumKey] = alet r = artists[s.artistKey]if (r == null) {r = { id = idOf('artist:' + s.artistKey) key = s.artistKey name = s.albumArtist == variousName ? s.artist : s.albumArtist songCount = 0 albumCount = 0 playCount = 0 coverId = nullsortName = s.artistKey seenAlbums = {} }}r.songCount = r.songCount + 1r.playCount = r.playCount + w.playsif (r.seenAlbums[s.albumKey] == null) {r.seenAlbums[s.albumKey] = truer.albumCount = r.albumCount + 1}if (r.coverId == null && s.coverId != null) { r.coverId = s.coverId }artists[s.artistKey] = r}// the old rows outfor (old of albumsTable.find(null, null)) { albumsTable.delete(old.id) }for (old of artistsTable.find(null, null)) { artistsTable.delete(old.id) }let na = 0for (k in albums) {albumsTable.put(albums[k])na = na + 1}let nr = 0for (k in artists) {r = artists[k]artistsTable.put({ id = r.id key = r.key name = r.name songCount = r.songCount albumCount = r.albumCount playCount = r.playCount coverId = r.coverId sortName = r.sortName })nr = nr + 1}return { albums = na artists = nr }}
Branches
Latest commits
- df8f5205Records: jukebox#19-21, #11 closedmre
- b7a4e692Records: hybriel#298mre
- 9bcbb017Records: day38b waits for day39 fixmre
- f80504e1Records: day38b repro sentmre
- 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
- 68aace7fRecords: mission 003 day38bmre
- d105d8a9Records: mpackdb integrity check cleanmre
- 6e980c53Records: mpackdb rule refinementmre
- 6d335ac4Records: storage copy rulingmre
- 7eb2898dRecords: mpackdb second-opener rule until day38mre
- b011f98eRecords: style form live on day35mre
- 60d90cb2Records: L1 woken for the restartmre
- 75ae544fRecords: restart waits for the connection to closemre
- e0b75951Records: L1 merged and pushed, restart waits for an open connectionmre
- f2b5db98styles: format styles with nested rules and bare queriesmre
- f036bab4search: format styles with nested rules and bare queriesmre
- ef9a9d4asongs: format styles with nested rules and bare queriesmre
- f22f9aacmain: format styles with nested rules and bare queriesmre
- f56cd7c7Sync icons from 8176eb1 and verify mixed component sourcesmre
- d56e5054Sync theme and shared tokens from layouts 17e0f03mre